Logo ROOT   6.08/07
Reference Guide
THn.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Axel Naumann, Nov 2011
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_THN
13 #define ROOT_THN
14 
15 #ifndef ROOT_THnBase
16 #include "THnBase.h"
17 #endif
18 
19 #ifndef ROOT_TNDArray
20 #include "TNDArray.h"
21 #endif
22 
23 #ifndef ROOT_TArrayD
24 #include "TArrayD.h"
25 #endif
26 
27 #ifndef ROOT_TObjArray
28 #include "TObjArray.h"
29 #endif
30 
31 #ifndef ROOT_TAxis
32 #include "TAxis.h"
33 #endif
34 
35 #ifndef ROOT_TMath
36 #include "TMath.h"
37 #endif
38 
39 class TH1;
40 class TH1D;
41 class TH2D;
42 class TH3D;
43 class THnSparse;
44 class TF1;
45 
46 
47 
48 class THn: public THnBase {
49 private:
50  THn(const THn&); // Not implemented
51  THn& operator=(const THn&); // Not implemented
52 
53 protected:
54  void AllocCoordBuf() const;
55  void InitStorage(Int_t* nbins, Int_t chunkSize);
56 
57  THn(): fCoordBuf() {}
58  THn(const char* name, const char* title, Int_t dim, const Int_t* nbins,
59  const Double_t* xmin, const Double_t* xmax);
60 
61 public:
62  virtual ~THn();
63 
64  static THn* CreateHn(const char* name, const char* title, const TH1* h1) {
65  return (THn*) CreateHnAny(name, title, h1, kFALSE /*THn*/, -1);
66  }
67  static THn* CreateHn(const char* name, const char* title, const THnBase* hn) {
68  return (THn*) CreateHnAny(name, title, hn, kFALSE /*THn*/, -1);
69  }
70 
71  ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const;
72  Long64_t GetNbins() const { return GetArray().GetNbins(); }
73 
74  Long64_t GetBin(const Int_t* idx) const {
75  return GetArray().GetBin(idx);
76  }
77  Long64_t GetBin(const Double_t* x) const {
78  if (!fCoordBuf) AllocCoordBuf();
79  for (Int_t d = 0; d < fNdimensions; ++d) {
80  fCoordBuf[d] = GetAxis(d)->FindFixBin(x[d]);
81  }
82  return GetArray().GetBin(fCoordBuf);
83  }
84  Long64_t GetBin(const char* name[]) const {
85  if (!fCoordBuf) AllocCoordBuf();
86  for (Int_t d = 0; d < fNdimensions; ++d) {
87  fCoordBuf[d] = GetAxis(d)->FindBin(name[d]);
88  }
89  return GetArray().GetBin(fCoordBuf);
90  }
91 
92  Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) {
93  return const_cast<const THn*>(this)->GetBin(idx);
94  }
95  Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) {
96  return const_cast<const THn*>(this)->GetBin(x);
97  }
98  Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) {
99  return const_cast<const THn*>(this)->GetBin(name);
100  }
101 
102  void FillBin(Long64_t bin, Double_t w) {
103  // Increment the bin content of "bin" by "w",
104  // return the bin index.
105  GetArray().AddAt(bin, w);
106  if (GetCalculateErrors()) {
107  fSumw2.AddAt(bin, w * w);
108  }
109  FillBinBase(w);
110  }
111 
112  void SetBinContent(const Int_t* idx, Double_t v) {
113  // Forwards to THnBase::SetBinContent().
114  // Non-virtual, CINT-compatible replacement of a using declaration.
115  THnBase::SetBinContent(idx, v);
116  }
118  GetArray().SetAsDouble(bin, v);
119  }
121  if (!GetCalculateErrors()) Sumw2();
122  fSumw2.At(bin) = e2;
123  }
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  GetArray().AddAt(bin, v);
131  }
133  fSumw2.At(bin) += e2;
134  }
135  Double_t GetBinContent(const Int_t *idx) const {
136  // Forwards to THnBase::GetBinContent() overload.
137  // Non-virtual, CINT-compatible replacement of a using declaration.
138  return THnBase::GetBinContent(idx);
139  }
140  Double_t GetBinContent(Long64_t bin, Int_t* idx = 0) const {
141  // Get the content of bin, and set its index if idx is != 0.
142  if (idx) {
143  const TNDArray& arr = GetArray();
144  Long64_t prevCellSize = arr.GetNbins();
145  for (Int_t i = 0; i < GetNdimensions(); ++i) {
146  Long64_t cellSize = arr.GetCellSize(i);
147  idx[i] = (bin % prevCellSize) / cellSize;
148  prevCellSize = cellSize;
149  }
150  }
151  return GetArray().AtAsDouble(bin);
152  }
154  return GetCalculateErrors() ? fSumw2.At(linidx) : GetBinContent(linidx);
155  }
156 
157  virtual const TNDArray& GetArray() const = 0;
158  virtual TNDArray& GetArray() = 0;
159 
160  void Sumw2();
161 
162  TH1D* Projection(Int_t xDim, Option_t* option = "") const {
163  // Forwards to THnBase::Projection().
164  // Non-virtual, as a CINT-compatible replacement of a using
165  // declaration.
166  return THnBase::Projection(xDim, option);
167  }
168 
169  TH2D* Projection(Int_t yDim, Int_t xDim,
170  Option_t* option = "") const {
171  // Forwards to THnBase::Projection().
172  // Non-virtual, as a CINT-compatible replacement of a using
173  // declaration.
174  return THnBase::Projection(yDim, xDim, option);
175  }
176 
177  TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
178  Option_t* option = "") const {
179  // Forwards to THnBase::Projection().
180  // Non-virtual, as a CINT-compatible replacement of a using
181  // declaration.
182  return THnBase::Projection(xDim, yDim, zDim, option);
183  }
184 
185  THn* Projection(Int_t ndim, const Int_t* dim,
186  Option_t* option = "") const {
187  return (THn*) ProjectionND(ndim, dim, option);
188  }
189 
190  THn* Rebin(Int_t group) const {
191  return (THn*) RebinBase(group);
192  }
193  THn* Rebin(const Int_t* group) const {
194  return (THn*) RebinBase(group);
195  }
196 
197  void Reset(Option_t* option = "");
198 
199 protected:
200  TNDArrayT<Double_t> fSumw2; // bin error, lazy allocation happens in TNDArrayT
201  mutable Int_t* fCoordBuf; //! Temporary buffer
202 
203  ClassDef(THn, 1); //Base class for multi-dimensional histogram
204 };
205 
206 
207 
208 //______________________________________________________________________________
209 /** \class THnT
210  Templated implementation of the abstract base THn.
211  All functionality and the interfaces to be used are in THn!
212 
213  THn does not know how to store any bin content itself. Instead, this
214  is delegated to the derived, templated class: the template parameter decides
215  what the format for the bin content is. The actual storage is delegated to
216  TNDArrayT<T>.
217 
218  Typedefs exist for template parematers with ROOT's generic types:
219 
220  Templated name | Typedef | Bin content type
221  -----------------|---------------|--------------------
222  THnT<Char_t> | THnC | Char_t
223  THnT<Short_t> | THnS | Short_t
224  THnT<Int_t> | THnI | Int_t
225  THnT<Long_t> | THnL | Long_t
226  THnT<Float_t> | THnF | Float_t
227  THnT<Double_t> | THnD | Double_t
228 
229  We recommend to use THnC wherever possible, and to map its value space
230  of 256 possible values to e.g. float values outside the class. This saves an
231  enourmous amount of memory. Only if more than 256 values need to be
232  distinguished should e.g. THnS or even THnF be chosen.
233 
234  Implementation detail: the derived, templated class is kept extremely small
235  on purpose. That way the (templated thus inlined) uses of this class will
236  only create a small amount of machine code, in contrast to e.g. STL.
237 */
238 
239 template <typename T>
240 class THnT: public THn {
241 public:
242  THnT() {}
243 
244  THnT(const char* name, const char* title,
245  Int_t dim, const Int_t* nbins,
246  const Double_t* xmin, const Double_t* xmax):
247  THn(name, title, dim, nbins, xmin, xmax),
248  fArray(dim, nbins, true) {}
249 
250  const TNDArray& GetArray() const { return fArray; }
251  TNDArray& GetArray() { return fArray; }
252 
253 protected:
254  TNDArrayT<T> fArray; // bin content
255  ClassDef(THnT, 1); // multi-dimensional histogram with templated storage
256 };
257 
265 
266 #endif // ROOT_THN
void SetBinContent(Long64_t bin, Double_t v)
Definition: THn.h:117
static THn * CreateHn(const char *name, const char *title, const TH1 *h1)
Definition: THn.h:64
THn * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THn.h:185
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all bins. Public interface is THnIter.
Definition: THn.cxx:204
float xmin
Definition: THbookFile.cxx:93
Bool_t GetCalculateErrors() const
Definition: THnBase.h:146
long long Long64_t
Definition: RtypesCore.h:69
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THn.h:162
Double_t GetBinContent(Long64_t bin, Int_t *idx=0) const
Definition: THn.h:140
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Definition: THn.h:177
THnT< Int_t > THnI
Definition: THn.h:262
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
THnT()
Definition: THn.h:242
void SetBinError2(Long64_t bin, Double_t e2)
Definition: THn.h:120
Long64_t GetBin(const Double_t *x) const
Definition: THn.h:77
Double_t GetBinContent(const Int_t *idx) const
Definition: THnBase.h:177
virtual Double_t AtAsDouble(ULong64_t linidx) const =0
THnT< Long64_t > THnL64
Definition: THn.h:264
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
int nbins[3]
void FillBinBase(Double_t w)
Definition: THnBase.h:93
virtual void SetAsDouble(ULong64_t linidx, Double_t value)=0
THn & operator=(const THn &)
THn * Rebin(const Int_t *group) const
Definition: THn.h:193
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition: THnBase.h:173
Long64_t GetBin(const char *name[]) const
Definition: THn.h:84
Long64_t GetNbins() const
Definition: TNDArray.h:76
Double_t GetBinContent(const Int_t *idx) const
Definition: THn.h:135
static THn * CreateHn(const char *name, const char *title, const THnBase *hn)
Definition: THn.h:67
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
Efficient multidimensional histogram.
Definition: THnSparse.h:52
Long64_t GetBin(const char *name[], Bool_t=kTRUE)
Definition: THn.h:98
Templated implementation of the abstract base THn.
Definition: THn.h:240
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnBase.h:198
Long64_t GetNbins() const
Definition: THn.h:72
void Sumw2()
Enable calculation of errors.
Definition: THn.cxx:213
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnBase.h:187
Long64_t GetBin(const Int_t *idx, Bool_t=kTRUE)
Definition: THn.h:92
TNDArrayT< Double_t > fSumw2
Definition: THn.h:200
THn()
Definition: THn.h:57
TH1F * h1
Definition: legend1.C:5
const TNDArray & GetArray() const
Definition: THn.h:250
THnT< Double_t > THnD
Definition: THn.h:259
void InitStorage(Int_t *nbins, Int_t chunkSize)
Initialize the storage of a histogram created via Init()
Definition: THn.cxx:237
TAxis * GetAxis(Int_t dim) const
Definition: THnBase.h:135
void AllocCoordBuf() const
Create the coordinate buffer.
Definition: THn.cxx:229
tomato 3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:308
SVector< double, 2 > v
Definition: Dict.h:5
T At(const Int_t *idx) const
Definition: TNDArray.h:163
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THn.h:112
Long64_t GetBin(const Int_t *idx) const
Definition: THn.h:74
void AddBinContent(const Int_t *idx, Double_t v=1.)
Definition: THn.h:124
Iterator over THnBase bins (internal implementation).
Definition: THnBase.h:296
void AddBinError2(Long64_t bin, Double_t e2)
Definition: THn.h:132
THn * Rebin(Int_t group) const
Definition: THn.h:190
void AddAt(ULong64_t linidx, Double_t value)
Definition: TNDArray.h:186
Multidimensional histogram.
Definition: THn.h:48
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Definition: THn.h:169
void AddBinContent(Long64_t bin, Double_t v=1.)
Definition: THn.h:129
float xmax
Definition: THbookFile.cxx:93
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
Int_t * fCoordBuf
Definition: THn.h:201
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
THnT< Long_t > THnL
Definition: THn.h:263
double Double_t
Definition: RtypesCore.h:55
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnBase.h:239
The TH1 histogram class.
Definition: TH1.h:80
Long64_t GetBin(const Double_t *x, Bool_t=kTRUE)
Definition: THn.h:95
Int_t GetNdimensions() const
Definition: THnBase.h:145
Double_t GetBinError2(Long64_t linidx) const
Definition: THn.h:153
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:405
virtual ~THn()
Destruct a THn.
Definition: THn.cxx:195
THnT< Char_t > THnC
Definition: THn.h:260
void FillBin(Long64_t bin, Double_t w)
Definition: THn.h:102
Int_t fNdimensions
Definition: THnBase.h:55
1-Dim function class
Definition: TF1.h:149
Long64_t GetBin(const Int_t *idx) const
Definition: TNDArray.h:79
TNDArrayT< T > fArray
Definition: THn.h:254
THnT< Float_t > THnF
Definition: THn.h:258
Long64_t GetCellSize(Int_t dim) const
Definition: TNDArray.h:77
Multidimensional histogram base.
Definition: THnBase.h:53
const Bool_t kTRUE
Definition: Rtypes.h:91
THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin, const Double_t *xmax)
Definition: THn.h:244
void Reset(Option_t *option="")
Reset the contents of a THn.
Definition: THn.cxx:247
THnT< Short_t > THnS
Definition: THn.h:261
char name[80]
Definition: TGX11.cxx:109
virtual void AddAt(ULong64_t linidx, Double_t value)=0
TNDArray & GetArray()
Definition: THn.h:251
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:296
virtual const TNDArray & GetArray() const =0