Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
THnSparse.h
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Axel Naumann (2007-09-11)
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_THnSparse
13#define ROOT_THnSparse
14
15/*************************************************************************
16
17 THnSparse: histogramming multi-dimensional, sparse distributions in
18 a memory-efficient way.
19
20*************************************************************************/
21
22
23#include "THnBase.h"
24#include "TExMap.h"
25#include "THnSparse_Internal.h"
26
27// needed only for template instantiations of THnSparseT:
28#include "TArrayF.h"
29#include "TArrayL.h"
30#include "TArrayL64.h"
31#include "TArrayI.h"
32#include "TArrayS.h"
33#include "TArrayC.h"
34
36
37class THnSparse: public THnBase {
38 private:
39 Int_t fChunkSize; ///< Number of entries for each chunk
40 Long64_t fFilledBins; ///< Number of filled bins
41 TObjArray fBinContent; ///< Array of THnSparseArrayChunk
42 TExMap fBins; ///<! Filled bins
43 TExMap fBinsContinued; ///<! Filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1)
44 THnSparseCompactBinCoord *fCompactCoord; ///<! Compact coordinate
45
46 THnSparse& operator=(const THnSparse&) = delete;
47
48 protected:
49
53
55 void Reserve(Long64_t nbins) override;
56 void FillExMap();
57 virtual TArray* GenerateArray() const = 0;
59
60 /// Increment the bin content of "bin" by "w",
61 /// return the bin index.
62 void FillBin(Long64_t bin, Double_t w) override {
64 chunk->AddBinContent(bin % fChunkSize, w);
66 }
67 void InitStorage(Int_t* nbins, Int_t chunkSize) override;
68
69 public:
70
71 THnSparse();
72 THnSparse(const char* name, const char* title, Int_t dim,
73 const Int_t* nbins, const Double_t* xmin = nullptr, const Double_t* xmax = nullptr,
74 Int_t chunksize = 1024 * 16);
75 THnSparse(const char* name, const char* title,
76 const std::vector<TAxis>& axes,
77 Int_t chunksize = 1024 * 16);
78
79 THnSparse(const char *name, const char *title, Int_t dim, const Int_t *nbins,
80 const std::vector<std::vector<double>> &xbins, Int_t chunksize = 1024 * 16);
81
83
84 ~THnSparse() override;
85
86 static THnSparse* CreateSparse(const char* name, const char* title,
87 const TH1* h1, Int_t chunkSize = 1024 * 16) {
88 return (THnSparse*) CreateHnAny(name, title, h1, kTRUE /*sparse*/,
89 chunkSize);
90 }
91 static THnSparse* CreateSparse(const char* name, const char* title,
92 const THnBase* hn, Int_t chunkSize = 1024 * 16) {
93 return (THnSparse*) CreateHnAny(name, title, hn, kTRUE /*sparse*/,
94 chunkSize);
95 }
96
97 Int_t GetChunkSize() const { return fChunkSize; }
99
101
102 Long64_t GetNbins() const override { return fFilledBins; }
103 void SetFilledBins(Long64_t nbins) override { fFilledBins = nbins; }
104
105 Long64_t GetBin(const Int_t* idx) const override { return const_cast<THnSparse*>(this)->GetBin(idx, kFALSE); }
106 Long64_t GetBin(const Double_t* x) const override { return const_cast<THnSparse*>(this)->GetBin(x, kFALSE); }
107 Long64_t GetBin(const char* name[]) const override { return const_cast<THnSparse*>(this)->GetBin(name, kFALSE); }
108 Long64_t GetBin(const Int_t* idx, Bool_t allocate = kTRUE) override;
109 Long64_t GetBin(const Double_t* x, Bool_t allocate = kTRUE) override;
110 Long64_t GetBin(const char* name[], Bool_t allocate = kTRUE) override;
111
112 /// Forwards to THnBase::SetBinContent().
113 /// Non-virtual, CINT-compatible replacement of a using declaration.
114 void SetBinContent(const Int_t* idx, Double_t v) {
116 }
117 void SetBinContent(Long64_t bin, Double_t v) override;
118 void SetBinError2(Long64_t bin, Double_t e2) override;
119
120 /// Forwards to THnBase::AddBinContent().
121 /// Non-virtual, CINT-compatible replacement of a using declaration.
122 void AddBinContent(const Int_t* idx, Double_t v = 1.) {
124 }
125 void AddBinContent(Long64_t bin, Double_t v = 1.) override;
126 void AddBinError2(Long64_t bin, Double_t e2) override;
127
128 /// Forwards to THnBase::GetBinContent() overload.
129 /// Non-virtual, CINT-compatible replacement of a using declaration.
130 Double_t GetBinContent(const Int_t *idx) const {
131
132 return THnBase::GetBinContent(idx);
133 }
134 Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const override;
135 Double_t GetBinError2(Long64_t linidx) const override;
136
139
140 /// Forwards to THnBase::Projection().
141 /// Non-virtual, as a CINT-compatible replacement of a using
142 /// declaration.
146
147 /// Forwards to THnBase::Projection().
148 /// Non-virtual, as a CINT-compatible replacement of a using
149 /// declaration.
151 Option_t* option = "") const {
153 }
154
155 /// Forwards to THnBase::Projection().
156 /// Non-virtual, as a CINT-compatible replacement of a using
157 /// declaration.
162
163 THnSparse* Projection(Int_t ndim, const Int_t* dim,
164 Option_t* option = "") const {
165 return (THnSparse*) ProjectionND(ndim, dim, option);
166 }
167
169 return (THnSparse*) RebinBase(group);
170 }
171 THnSparse* Rebin(const Int_t* group) const {
172 return (THnSparse*) RebinBase(group);
173 }
174
175 void Reset(Option_t* option = "") override;
176 void Sumw2() override;
177
178 ClassDefOverride(THnSparse, 3); // Interfaces of sparse n-dimensional histogram
179};
180
181
182
183//______________________________________________________________________________
184/** \class THnSparseT
185 Templated implementation of the abstract base THnSparse.
186 All functionality and the interfaces to be used are in THnSparse!
187
188 THnSparse does not know how to store any bin content itself. Instead, this
189 is delegated to the derived, templated class: the template parameter decides
190 what the format for the bin content is. In fact it even defines the array
191 itself; possible implementations probably derive from TArray.
192
193 Typedefs exist for template parameters with ROOT's generic types:
194
195 Templated name | Typedef | Bin content type
196 ----------------------|---------------|--------------------
197 THnSparseT<TArrayC> | THnSparseC | Char_t
198 THnSparseT<TArrayS> | THnSparseS | Short_t
199 THnSparseT<TArrayI> | THnSparseI | Int_t
200 THnSparseT<TArrayL64> | THnSparseL | Long64_t
201 THnSparseT<TArrayF> | THnSparseF | Float_t
202 THnSparseT<TArrayD> | THnSparseD | Double_t
203
204 We recommend to use THnSparseC wherever possible, and to map its value space
205 of 256 possible values to e.g. float values outside the class. This saves an
206 enormous amount of memory. Only if more than 256 values need to be
207 distinguished should e.g. THnSparseS or even THnSparseF be chosen.
208
209 Implementation detail: the derived, templated class is kept extremely small
210 on purpose. That way the (templated thus inlined) uses of this class will
211 only create a small amount of machine code, in contrast to e.g. STL.
212*/
213
214
215template <class CONT>
216class THnSparseT: public THnSparse {
217 public:
219
220 TArray* GenerateArray() const override { return new CONT(GetChunkSize()); }
221 private:
222 ClassDefOverride(THnSparseT, 1); // Sparse n-dimensional histogram with templated content
223};
224
231
232
233#endif // ROOT_THnSparse
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
THnSparseT< TArrayL64 > THnSparseL
Definition THnSparse.h:227
THnSparseT< TArrayS > THnSparseS
Definition THnSparse.h:229
THnSparseT< TArrayF > THnSparseF
Definition THnSparse.h:226
THnSparseT< TArrayC > THnSparseC
Definition THnSparse.h:230
THnSparseT< TArrayD > THnSparseD
Definition THnSparse.h:225
THnSparseT< TArrayI > THnSparseI
Definition THnSparse.h:228
Iterator over THnBase bins (internal implementation).
Definition THnBase.h:327
Abstract array base class.
Definition TArray.h:31
This class stores a (key,value) pair using an external hash.
Definition TExMap.h:33
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:400
3-D histogram with a double per channel (see TH1 documentation)
Definition TH3.h:418
Multidimensional histogram base.
Definition THnBase.h:45
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition THnBase.h:195
TH1D * Projection(Int_t xDim, Option_t *option="") const
Project all bins into a 1-dimensional histogram, keeping only axis "xDim".
Definition THnBase.h:230
static THnBase * CreateHnAny(const char *name, const char *title, const TH1 *h1, Bool_t sparse, Int_t chunkSize=1024 *16)
Create a THn / THnSparse object from a histogram deriving from TH1.
Definition THnBase.cxx:335
THnBase * RebinBase(Int_t group) const
Combine the content of "group" neighboring bins into a new bin and return the resulting THnBase.
Definition THnBase.cxx:1250
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Project histogram onto the axes specified in dim.
Definition THnBase.h:269
Double_t GetBinContent(const Int_t *idx) const
Definition THnBase.h:201
void SetBinContent(const Int_t *idx, Double_t v)
Definition THnBase.h:211
void FillBinBase(Double_t w)
Increment the statistics due to filled weight "w",.
Definition THnBase.h:93
THnSparseArrayChunk is used internally by THnSparse.
THnSparseCompactBinCoord is a class used by THnSparse internally.
Templated implementation of the abstract base THnSparse.
Definition THnSparse.h:216
TArray * GenerateArray() const override
Definition THnSparse.h:220
Efficient multidimensional histogram.
Definition THnSparse.h:37
TH1D * Projection(Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THnSparse.h:143
Double_t GetSparseFractionBins() const
Return the amount of filled bins over all bins.
Double_t GetSparseFractionMem() const
Return the amount of used memory over memory that would be used by a non-sparse n-dimensional histogr...
THnSparse * Rebin(Int_t group) const
Definition THnSparse.h:168
void Reset(Option_t *option="") override
Clear the histogram.
Int_t GetNChunks() const
Definition THnSparse.h:98
THnSparse & operator=(const THnSparse &)=delete
void AddBinError2(Long64_t bin, Double_t e2) override
Add "e" to error of bin with index "bin", enable errors if needed.
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THnSparse.h:158
void SetBinContent(const Int_t *idx, Double_t v)
Forwards to THnBase::SetBinContent().
Definition THnSparse.h:114
THnSparseArrayChunk * GetChunk(Int_t idx) const
Definition THnSparse.h:51
THnSparseCompactBinCoord * fCompactCoord
! Compact coordinate
Definition THnSparse.h:44
Int_t GetChunkSize() const
Definition THnSparse.h:97
Double_t GetBinContent(const Int_t *idx) const
Forwards to THnBase::GetBinContent() overload.
Definition THnSparse.h:130
Long64_t fFilledBins
Number of filled bins.
Definition THnSparse.h:40
Long64_t GetBin(const Int_t *idx) const override
Definition THnSparse.h:105
TObjArray fBinContent
Array of THnSparseArrayChunk.
Definition THnSparse.h:41
Long64_t GetBin(const char *name[]) const override
Definition THnSparse.h:107
THnSparseCompactBinCoord * GetCompactCoord() const
Return THnSparseCompactBinCoord object.
void InitStorage(Int_t *nbins, Int_t chunkSize) override
Initialize the storage of a histogram created via Init()
static THnSparse * CreateSparse(const char *name, const char *title, const TH1 *h1, Int_t chunkSize=1024 *16)
Definition THnSparse.h:86
Double_t GetBinError2(Long64_t linidx) const override
Get square of the error of bin addressed by linidx as If errors are not enabled (via Sumw2() or Calc...
THnSparse()
Construct an empty THnSparse.
THnSparseArrayChunk * AddChunk()
Create a new chunk of bin content.
virtual TArray * GenerateArray() const =0
void SetBinError2(Long64_t bin, Double_t e2) override
Set error of bin with index "bin" to "e", enable errors if needed.
void FillExMap()
We have been streamed; set up fBins.
THnSparse * Rebin(const Int_t *group) const
Definition THnSparse.h:171
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THnSparse.h:150
~THnSparse() override
Destruct a THnSparse.
void FillBin(Long64_t bin, Double_t w) override
Increment the bin content of "bin" by "w", return the bin index.
Definition THnSparse.h:62
TExMap fBins
! Filled bins
Definition THnSparse.h:42
Long64_t GetBin(const Double_t *x) const override
Definition THnSparse.h:106
static THnSparse * CreateSparse(const char *name, const char *title, const THnBase *hn, Int_t chunkSize=1024 *16)
Definition THnSparse.h:91
THnSparse * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition THnSparse.h:163
TExMap fBinsContinued
! Filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1)
Definition THnSparse.h:43
void AddBinContent(const Int_t *idx, Double_t v=1.)
Forwards to THnBase::AddBinContent().
Definition THnSparse.h:122
void Reserve(Long64_t nbins) override
Initialize storage for nbins.
Int_t fChunkSize
Number of entries for each chunk.
Definition THnSparse.h:39
void Sumw2() override
Enable calculation of errors.
Long64_t GetNbins() const override
Definition THnSparse.h:102
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const override
Create an iterator over all filled bins of a THnSparse.
void SetFilledBins(Long64_t nbins) override
Definition THnSparse.h:103
Long64_t GetBinIndexForCurrentBin(Bool_t allocate)
Return the index for fCurrentBinIndex.
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntriesFast() const
Definition TObjArray.h:58
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5