Logo ROOT   6.08/07
Reference Guide
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 #ifndef ROOT_THnBase
24 #include "THnBase.h"
25 #endif
26 #ifndef ROOT_TExMap
27 #include "TExMap.h"
28 #endif
29 #ifndef ROOT_THnSparse_Internal
30 #include "THnSparse_Internal.h"
31 #endif
32 
33 // needed only for template instantiations of THnSparseT:
34 #ifndef ROOT_TArrayF
35 #include "TArrayF.h"
36 #endif
37 #ifndef ROOT_TArrayL
38 #include "TArrayL.h"
39 #endif
40 #ifndef ROOT_TArrayI
41 #include "TArrayI.h"
42 #endif
43 #ifndef ROOT_TArrayS
44 #include "TArrayS.h"
45 #endif
46 #ifndef ROOT_TArrayC
47 #include "TArrayC.h"
48 #endif
49 
50 class THnSparseCompactBinCoord;
51 
52 class THnSparse: public THnBase {
53  private:
54  Int_t fChunkSize; // number of entries for each chunk
55  Long64_t fFilledBins; // number of filled bins
56  TObjArray fBinContent; // array of THnSparseArrayChunk
57  TExMap fBins; //! filled bins
58  TExMap fBinsContinued;//! filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1)
59  THnSparseCompactBinCoord *fCompactCoord; //! compact coordinate
60 
61  THnSparse(const THnSparse&); // Not implemented
62  THnSparse& operator=(const THnSparse&); // Not implemented
63 
64  protected:
65 
66  THnSparse();
67  THnSparse(const char* name, const char* title, Int_t dim,
68  const Int_t* nbins, const Double_t* xmin, const Double_t* xmax,
69  Int_t chunksize);
70  THnSparseCompactBinCoord* GetCompactCoord() const;
72  return (THnSparseArrayChunk*) fBinContent[idx]; }
73 
75  void Reserve(Long64_t nbins);
76  void FillExMap();
77  virtual TArray* GenerateArray() const = 0;
79  void FillBin(Long64_t bin, Double_t w) {
80  // Increment the bin content of "bin" by "w",
81  // return the bin index.
82  THnSparseArrayChunk* chunk = GetChunk(bin / fChunkSize);
83  chunk->AddBinContent(bin % fChunkSize, w);
84  FillBinBase(w);
85  }
86  void InitStorage(Int_t* nbins, Int_t chunkSize);
87 
88  public:
89  virtual ~THnSparse();
90 
91  static THnSparse* CreateSparse(const char* name, const char* title,
92  const TH1* h1, Int_t chunkSize = 1024 * 16) {
93  return (THnSparse*) CreateHnAny(name, title, h1, kTRUE /*sparse*/,
94  chunkSize);
95  }
96  static THnSparse* CreateSparse(const char* name, const char* title,
97  const THnBase* hn, Int_t chunkSize = 1024 * 16) {
98  return (THnSparse*) CreateHnAny(name, title, hn, kTRUE /*sparse*/,
99  chunkSize);
100  }
101 
102  Int_t GetChunkSize() const { return fChunkSize; }
103  Int_t GetNChunks() const { return fBinContent.GetEntriesFast(); }
104 
105  ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const;
106 
107  Long64_t GetNbins() const { return fFilledBins; }
108  void SetFilledBins(Long64_t nbins) { fFilledBins = nbins; }
109 
110  Long64_t GetBin(const Int_t* idx) const { return const_cast<THnSparse*>(this)->GetBin(idx, kFALSE); }
111  Long64_t GetBin(const Double_t* x) const { return const_cast<THnSparse*>(this)->GetBin(x, kFALSE); }
112  Long64_t GetBin(const char* name[]) const { return const_cast<THnSparse*>(this)->GetBin(name, kFALSE); }
113  Long64_t GetBin(const Int_t* idx, Bool_t allocate = kTRUE);
114  Long64_t GetBin(const Double_t* x, Bool_t allocate = kTRUE);
115  Long64_t GetBin(const char* name[], Bool_t allocate = kTRUE);
116 
117  void SetBinContent(const Int_t* idx, Double_t v) {
118  // Forwards to THnBase::SetBinContent().
119  // Non-virtual, CINT-compatible replacement of a using declaration.
120  THnBase::SetBinContent(idx, v);
121  }
122  void SetBinContent(Long64_t bin, Double_t v);
123  void SetBinError2(Long64_t bin, Double_t e2);
124  void AddBinContent(const Int_t* idx, Double_t v = 1.) {
125  // Forwards to THnBase::SetBinContent().
126  // Non-virtual, CINT-compatible replacement of a using declaration.
127  THnBase::AddBinContent(idx, v);
128  }
129  void AddBinContent(Long64_t bin, Double_t v = 1.);
130  void AddBinError2(Long64_t bin, Double_t e2);
131 
132  Double_t GetBinContent(const Int_t *idx) const {
133  // Forwards to THnBase::GetBinContent() overload.
134  // Non-virtual, CINT-compatible replacement of a using declaration.
135  return THnBase::GetBinContent(idx);
136  }
137  Double_t GetBinContent(Long64_t bin, Int_t* idx = 0) const;
138  Double_t GetBinError2(Long64_t linidx) const;
139 
142 
143  TH1D* Projection(Int_t xDim, Option_t* option = "") const{
144  // Forwards to THnBase::Projection().
145  // Non-virtual, as a CINT-compatible replacement of a using
146  // declaration.
147  return THnBase::Projection(xDim, option);
148  }
149 
150  TH2D* Projection(Int_t yDim, Int_t xDim,
151  Option_t* option = "") const {
152  // Forwards to THnBase::Projection().
153  // Non-virtual, as a CINT-compatible replacement of a using
154  // declaration.
155  return THnBase::Projection(yDim, xDim, option);
156  }
157 
158  TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
159  Option_t* option = "") const {
160  // Forwards to THnBase::Projection().
161  // Non-virtual, as a CINT-compatible replacement of a using
162  // declaration.
163  return THnBase::Projection(xDim, yDim, zDim, option);
164  }
165 
166  THnSparse* Projection(Int_t ndim, const Int_t* dim,
167  Option_t* option = "") const {
168  return (THnSparse*) ProjectionND(ndim, dim, option);
169  }
170 
172  return (THnSparse*) RebinBase(group);
173  }
174  THnSparse* Rebin(const Int_t* group) const {
175  return (THnSparse*) RebinBase(group);
176  }
177 
178  void Reset(Option_t* option = "");
179  void Sumw2();
180 
181  ClassDef(THnSparse, 3); // Interfaces of sparse n-dimensional histogram
182 };
183 
184 
185 
186 //______________________________________________________________________________
187 /** \class THnSparseT
188  Templated implementation of the abstract base THnSparse.
189  All functionality and the interfaces to be used are in THnSparse!
190 
191  THnSparse does not know how to store any bin content itself. Instead, this
192  is delegated to the derived, templated class: the template parameter decides
193  what the format for the bin content is. In fact it even defines the array
194  itself; possible implementations probably derive from TArray.
195 
196  Typedefs exist for template parematers with ROOT's generic types:
197 
198  Templated name | Typedef | Bin content type
199  -----------------|--------------|--------------------
200  THnSparseT<TArrayC> | THnSparseC | Char_t
201  THnSparseT<TArrayS> | THnSparseS | Short_t
202  THnSparseT<TArrayI> | THnSparseI | Int_t
203  THnSparseT<TArrayL> | THnSparseL | Long_t
204  THnSparseT<TArrayF> | THnSparseF | Float_t
205  THnSparseT<TArrayD> | THnSparseD | Double_t
206 
207  We recommend to use THnSparseC wherever possible, and to map its value space
208  of 256 possible values to e.g. float values outside the class. This saves an
209  enourmous amount of memory. Only if more than 256 values need to be
210  distinguished should e.g. THnSparseS or even THnSparseF be chosen.
211 
212  Implementation detail: the derived, templated class is kept extremely small
213  on purpose. That way the (templated thus inlined) uses of this class will
214  only create a small amount of machine code, in contrast to e.g. STL.
215 */
216 
217 
218 template <class CONT>
219 class THnSparseT: public THnSparse {
220  public:
222  THnSparseT(const char* name, const char* title, Int_t dim,
223  const Int_t* nbins, const Double_t* xmin = 0,
224  const Double_t* xmax = 0, Int_t chunksize = 1024 * 16):
225  THnSparse(name, title, dim, nbins, xmin, xmax, chunksize) {}
226 
227  TArray* GenerateArray() const { return new CONT(GetChunkSize()); }
228  private:
229  ClassDef(THnSparseT, 1); // Sparse n-dimensional histogram with templated content
230 };
231 
238 
239 
240 #endif // ROOT_THnSparse
Abstract array base class.
Definition: TArray.h:33
Double_t GetSparseFractionMem() const
Return the amount of used memory over memory that would be used by a non-sparse n-dimensional histogr...
Definition: THnSparse.cxx:866
THnSparseArrayChunk is used internally by THnSparse.
void SetFilledBins(Long64_t nbins)
Definition: THnSparse.h:108
An array of TObjects.
Definition: TObjArray.h:39
void AddBinContent(const Int_t *idx, Double_t v=1.)
Definition: THnSparse.h:124
float xmin
Definition: THbookFile.cxx:93
long long Long64_t
Definition: RtypesCore.h:69
void FillExMap()
We have been streamed; set up fBins.
Definition: THnSparse.cxx:656
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:1056
const char Option_t
Definition: RtypesCore.h:62
THnSparseArrayChunk * AddChunk()
Create a new chunk of bin content.
Definition: THnSparse.cxx:635
Long64_t GetBin(const Int_t *idx) const
Definition: THnSparse.h:110
Double_t GetBinContent(const Int_t *idx) const
Definition: THnBase.h:177
THnSparseT< TArrayI > THnSparseI
Definition: THnSparse.h:235
Long64_t fFilledBins
Definition: THnSparse.h:55
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:197
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Double_t GetBinError2(Long64_t linidx) const
Get square of the error of bin addressed by linidx as If errors are not enabled (via Sumw2() or Calc...
Definition: THnSparse.cxx:772
int nbins[3]
void InitStorage(Int_t *nbins, Int_t chunkSize)
Initialize the storage of a histogram created via Init()
Definition: THnSparse.cxx:647
THnSparseT< TArrayS > THnSparseS
Definition: THnSparse.h:236
void FillBinBase(Double_t w)
Definition: THnBase.h:93
Int_t GetNChunks() const
Definition: THnSparse.h:103
THnSparseCompactBinCoord * fCompactCoord
filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1) ...
Definition: THnSparse.h:59
Long64_t GetNbins() const
Definition: THnSparse.h:107
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Definition: THnSparse.h:150
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition: THnBase.h:173
THnSparseT< TArrayD > THnSparseD
Definition: THnSparse.h:232
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Definition: THnSparse.h:158
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
Efficient multidimensional histogram.
Definition: THnSparse.h:52
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnBase.h:198
virtual TArray * GenerateArray() const =0
THnSparse * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnSparse.h:166
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnSparse.h:143
THnSparse * Rebin(const Int_t *group) const
Definition: THnSparse.h:174
void AddBinContent(Int_t idx, Double_t v=1.)
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnBase.h:187
TH1F * h1
Definition: legend1.C:5
THnSparseArrayChunk * GetChunk(Int_t idx) const
Definition: THnSparse.h:71
THnSparseT< TArrayF > THnSparseF
Definition: THnSparse.h:233
Long64_t GetBin(const Double_t *x) const
Definition: THnSparse.h:111
virtual ~THnSparse()
Destruct a THnSparse.
Definition: THnSparse.cxx:617
Double_t GetBinContent(const Int_t *idx) const
Definition: THnSparse.h:132
THnSparseT(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin=0, const Double_t *xmax=0, Int_t chunksize=1024 *16)
Definition: THnSparse.h:222
void SetBinError2(Long64_t bin, Double_t e2)
Set error of bin with index "bin" to "e", enable errors if needed.
Definition: THnSparse.cxx:915
TExMap fBinsContinued
filled bins
Definition: THnSparse.h:58
void Reset(Option_t *option="")
Clear the histogram.
Definition: THnSparse.cxx:963
THnSparseT< TArrayL > THnSparseL
Definition: THnSparse.h:234
TExMap fBins
Definition: THnSparse.h:57
tomato 3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:308
SVector< double, 2 > v
Definition: Dict.h:5
Iterator over THnBase bins (internal implementation).
Definition: THnBase.h:296
void Sumw2()
Enable calculation of errors.
Definition: THnSparse.cxx:949
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
THnSparseT< TArrayC > THnSparseC
Definition: THnSparse.h:237
float xmax
Definition: THbookFile.cxx:93
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnSparse.h:117
TArray * GenerateArray() const
Definition: THnSparse.h:227
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all filled bins of a THnSparse.
Definition: THnSparse.cxx:897
void Reserve(Long64_t nbins)
Initialize storage for nbins.
Definition: THnSparse.cxx:690
double Double_t
Definition: RtypesCore.h:55
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnBase.h:239
void AddBinError2(Long64_t bin, Double_t e2)
Add "e" to error of bin with index "bin", enable errors if needed.
Definition: THnSparse.cxx:932
The TH1 histogram class.
Definition: TH1.h:80
Double_t GetSparseFractionBins() const
Return the amount of filled bins over all bins.
Definition: THnSparse.cxx:855
THnSparse()
Construct an empty THnSparse.
Definition: THnSparse.cxx:590
THnSparse * Rebin(Int_t group) const
Definition: THnSparse.h:171
void FillBin(Long64_t bin, Double_t w)
Definition: THnSparse.h:79
THnSparseCompactBinCoord * GetCompactCoord() const
Return THnSparseCompactBinCoord object.
Definition: THnSparse.cxx:839
THnSparse & operator=(const THnSparse &)
Int_t fChunkSize
Definition: THnSparse.h:54
static THnSparse * CreateSparse(const char *name, const char *title, const TH1 *h1, Int_t chunkSize=1024 *16)
Definition: THnSparse.h:91
static THnSparse * CreateSparse(const char *name, const char *title, const THnBase *hn, Int_t chunkSize=1024 *16)
Definition: THnSparse.h:96
Multidimensional histogram base.
Definition: THnBase.h:53
Templated implementation of the abstract base THnSparse.
Definition: THnSparse.h:219
Int_t GetChunkSize() const
Definition: THnSparse.h:102
const Bool_t kTRUE
Definition: Rtypes.h:91
Long64_t GetBinIndexForCurrentBin(Bool_t allocate)
Return the index for fCurrentBinIndex.
Definition: THnSparse.cxx:790
TObjArray fBinContent
Definition: THnSparse.h:56
char name[80]
Definition: TGX11.cxx:109
This class stores a (key,value) pair using an external hash.
Definition: TExMap.h:35
Long64_t GetBin(const char *name[]) const
Definition: THnSparse.h:112
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:296