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