ROOT  6.06/09
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.
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 GetBinContent(const Int_t *idx) const
Definition: THnBase.h:175
THnSparseCompactBinCoord * GetCompactCoord() const
Return THnSparseCompactBinCoord object.
Definition: THnSparse.cxx:839
THnSparseArrayChunk is used internally by THnSparse.
void SetFilledBins(Long64_t nbins)
Definition: THnSparse.h:108
THnSparse * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnSparse.h:166
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
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnSparse.h:143
void FillExMap()
We have been streamed; set up fBins.
Definition: THnSparse.cxx:655
ClassDef(THnSparse, 3)
const char Option_t
Definition: RtypesCore.h:62
THnSparseArrayChunk * AddChunk()
Create a new chunk of bin content.
Definition: THnSparse.cxx:634
THnSparseT< TArrayI > THnSparseI
Definition: THnSparse.h:235
Int_t GetChunkSize() const
Definition: THnSparse.h:102
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:195
THnSparse * Rebin(Int_t group) const
Definition: THnSparse.h:171
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Long64_t GetBin(const Double_t *x) const
Definition: THnSparse.h:111
int nbins[3]
void InitStorage(Int_t *nbins, Int_t chunkSize)
Initialize the storage of a histogram created via Init()
Definition: THnSparse.cxx:646
THnSparseT< TArrayS > THnSparseS
Definition: THnSparse.h:236
void FillBinBase(Double_t w)
Definition: THnBase.h:94
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
Long64_t GetNbins() const
Definition: THnSparse.h:107
THnSparseCompactBinCoord * fCompactCoord
filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1) ...
Definition: THnSparse.h:59
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition: THnBase.h:171
THnSparseT< TArrayD > THnSparseD
Definition: THnSparse.h:232
Double_t x[n]
Definition: legend1.C:17
Efficient multidimensional histogram.
Definition: THnSparse.h:52
virtual TArray * GenerateArray() const =0
Double_t GetSparseFractionBins() const
Return the amount of filled bins over all bins.
Definition: THnSparse.cxx:855
void AddBinContent(Int_t idx, Double_t v=1.)
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnBase.h:185
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Definition: THnSparse.h:150
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnBase.h:196
TH1F * h1
Definition: legend1.C:5
THnSparse * Rebin(const Int_t *group) const
Definition: THnSparse.h:174
THnSparseT< TArrayF > THnSparseF
Definition: THnSparse.h:233
Long64_t GetBin(const Int_t *idx) const
Definition: THnSparse.h:110
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:1054
virtual ~THnSparse()
Destruct a THnSparse.
Definition: THnSparse.cxx:616
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
Int_t GetNChunks() const
Definition: THnSparse.h:103
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
3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:309
SVector< double, 2 > v
Definition: Dict.h:5
THnSparseArrayChunk * GetChunk(Int_t idx) const
Definition: THnSparse.h:71
Iterator over THnBase bins (internal implementation).
Definition: THnBase.h:294
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all filled bins of a THnSparse.
Definition: THnSparse.cxx:897
void Sumw2()
Enable calculation of errors.
Definition: THnSparse.cxx:949
THnSparseT< TArrayC > THnSparseC
Definition: THnSparse.h:237
Double_t GetBinError2(Long64_t linidx) const
Get square of the error of bin addressed by linidx as BEGIN_LATEX #sum weight^{2} END_LATEX If errors...
Definition: THnSparse.cxx:772
float xmax
Definition: THbookFile.cxx:93
ClassDef(THnSparseT, 1)
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnSparse.h:117
void Reserve(Long64_t nbins)
Initialize storage for nbins.
Definition: THnSparse.cxx:689
double Double_t
Definition: RtypesCore.h:55
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 GetBinContent(const Int_t *idx) const
Definition: THnSparse.h:132
THnSparse()
Construct an empty THnSparse.
Definition: THnSparse.cxx:589
#define name(a, b)
Definition: linkTestLib0.cpp:5
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
void FillBin(Long64_t bin, Double_t w)
Definition: THnSparse.h:79
Long64_t GetBin(const char *name[]) const
Definition: THnSparse.h:112
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Definition: THnSparse.h:158
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
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnBase.h:237
const Bool_t kTRUE
Definition: Rtypes.h:91
TArray * GenerateArray() const
Definition: THnSparse.h:227
Long64_t GetBinIndexForCurrentBin(Bool_t allocate)
Return the index for fCurrentBinIndex.
Definition: THnSparse.cxx:790
TObjArray fBinContent
Definition: THnSparse.h:56
This class stores a (key,value) pair using an external hash.
Definition: TExMap.h:35
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297