ROOT  6.06/09
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 FillBin(Long64_t bin, Double_t w) {
55  // Increment the bin content of "bin" by "w",
56  // return the bin index.
57  GetArray().AddAt(bin, w);
58  if (GetCalculateErrors()) {
59  fSumw2.AddAt(bin, w * w);
60  }
61  FillBinBase(w);
62  }
63 
64  void AllocCoordBuf() const;
65  void InitStorage(Int_t* nbins, Int_t chunkSize);
66 
67  THn(): fCoordBuf() {}
68  THn(const char* name, const char* title, Int_t dim, const Int_t* nbins,
69  const Double_t* xmin, const Double_t* xmax);
70 
71 public:
72  virtual ~THn();
73 
74  static THn* CreateHn(const char* name, const char* title, const TH1* h1) {
75  return (THn*) CreateHnAny(name, title, h1, kFALSE /*THn*/, -1);
76  }
77  static THn* CreateHn(const char* name, const char* title, const THnBase* hn) {
78  return (THn*) CreateHnAny(name, title, hn, kFALSE /*THn*/, -1);
79  }
80 
81  ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const;
82  Long64_t GetNbins() const { return GetArray().GetNbins(); }
83 
84  Long64_t GetBin(const Int_t* idx) const {
85  return GetArray().GetBin(idx);
86  }
87  Long64_t GetBin(const Double_t* x) const {
88  if (!fCoordBuf) AllocCoordBuf();
89  for (Int_t d = 0; d < fNdimensions; ++d) {
90  fCoordBuf[d] = GetAxis(d)->FindFixBin(x[d]);
91  }
92  return GetArray().GetBin(fCoordBuf);
93  }
94  Long64_t GetBin(const char* name[]) const {
95  if (!fCoordBuf) AllocCoordBuf();
96  for (Int_t d = 0; d < fNdimensions; ++d) {
97  fCoordBuf[d] = GetAxis(d)->FindBin(name[d]);
98  }
99  return GetArray().GetBin(fCoordBuf);
100  }
101 
102  Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) {
103  return const_cast<const THn*>(this)->GetBin(idx);
104  }
105  Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) {
106  return const_cast<const THn*>(this)->GetBin(x);
107  }
108  Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) {
109  return const_cast<const THn*>(this)->GetBin(name);
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
Long64_t GetBin(const Int_t *idx) const
Definition: THn.h:84
Long64_t GetCellSize(Int_t dim) const
Definition: TNDArray.h:77
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all bins. Public interface is THnIter.
Definition: THn.cxx:204
Double_t GetBinContent(const Int_t *idx) const
Definition: THnBase.h:175
Bool_t GetCalculateErrors() const
Definition: THnBase.h:147
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:74
float xmin
Definition: THbookFile.cxx:93
long long Long64_t
Definition: RtypesCore.h:69
THnT< Int_t > THnI
Definition: THn.h:262
Double_t GetBinContent(Long64_t bin, Int_t *idx=0) const
Definition: THn.h:140
void AllocCoordBuf() const
Create the coordinate buffer.
Definition: THn.cxx:229
const char Option_t
Definition: RtypesCore.h:62
Long64_t GetBin(const char *name[]) const
Definition: THn.h:94
THnT()
Definition: THn.h:242
void SetBinError2(Long64_t bin, Double_t e2)
Definition: THn.h:120
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:195
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THn.h:162
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]
THn * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THn.h:185
void FillBinBase(Double_t w)
Definition: THnBase.h:94
virtual void SetAsDouble(ULong64_t linidx, Double_t value)=0
THn & operator=(const THn &)
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition: THnBase.h:171
ClassDef(THn, 1)
Temporary buffer.
Long64_t GetBin(const Int_t *idx) const
Definition: TNDArray.h:79
static THn * CreateHn(const char *name, const char *title, const THnBase *hn)
Definition: THn.h:77
Double_t x[n]
Definition: legend1.C:17
Int_t GetNdimensions() const
Definition: THnBase.h:146
Efficient multidimensional histogram.
Definition: THnSparse.h:52
Long64_t GetBin(const char *name[], Bool_t=kTRUE)
Definition: THn.h:108
Templated implementation of the abstract base THn.
Definition: THn.h:240
void Sumw2()
Enable calculation of errors.
Definition: THn.cxx:213
Long64_t GetBin(const Double_t *x) const
Definition: THn.h:87
Double_t GetBinContent(const Int_t *idx) const
Definition: THn.h:135
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnBase.h:185
Long64_t GetBin(const Int_t *idx, Bool_t=kTRUE)
Definition: THn.h:102
TNDArrayT< Double_t > fSumw2
Definition: THn.h:200
THn()
Definition: THn.h:67
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnBase.h:196
TH1F * h1
Definition: legend1.C:5
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
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
3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:309
SVector< double, 2 > v
Definition: Dict.h:5
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THn.h:112
void AddBinContent(const Int_t *idx, Double_t v=1.)
Definition: THn.h:124
Iterator over THnBase bins (internal implementation).
Definition: THnBase.h:294
void AddBinError2(Long64_t bin, Double_t e2)
Definition: THn.h:132
Long64_t GetNbins() const
Definition: TNDArray.h:76
void AddAt(ULong64_t linidx, Double_t value)
Definition: TNDArray.h:186
Multidimensional histogram.
Definition: THn.h:48
T At(const Int_t *idx) const
Definition: TNDArray.h:163
void AddBinContent(Long64_t bin, Double_t v=1.)
Definition: THn.h:129
THn * Rebin(Int_t group) const
Definition: THn.h:190
float xmax
Definition: THbookFile.cxx:93
TAxis * GetAxis(Int_t dim) const
Definition: THnBase.h:136
Long64_t GetNbins() const
Definition: THn.h:82
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
Int_t * fCoordBuf
Definition: THn.h:201
THn * Rebin(const Int_t *group) const
Definition: THn.h:193
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
THnT< Long_t > THnL
Definition: THn.h:263
double Double_t
Definition: RtypesCore.h:55
The TH1 histogram class.
Definition: TH1.h:80
Long64_t GetBin(const Double_t *x, Bool_t=kTRUE)
Definition: THn.h:105
const TNDArray & GetArray() const
Definition: THn.h:250
#define name(a, b)
Definition: linkTestLib0.cpp:5
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:54
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:390
Int_t fNdimensions
Definition: THnBase.h:55
ClassDef(THnT, 1)
1-Dim function class
Definition: TF1.h:149
Double_t GetBinError2(Long64_t linidx) const
Definition: THn.h:153
TNDArrayT< T > fArray
Definition: THn.h:254
THnT< Float_t > THnF
Definition: THn.h:258
Multidimensional histogram base.
Definition: THnBase.h:53
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Definition: THn.h:169
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
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
virtual void AddAt(ULong64_t linidx, Double_t value)=0
TNDArray & GetArray()
Definition: THn.h:251
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Definition: THn.h:177
virtual const TNDArray & GetArray() const =0