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