Logo ROOT   6.08/07
Reference Guide
THnBase.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Axel Naumann (2011-12-20)
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_THnBase
13 #define ROOT_THnBase
14 
15 /*************************************************************************
16 
17  THnBase: Common base class for n-dimensional histogramming.
18  Defines interfaces and algorithms.
19 
20 *************************************************************************/
21 
22 
23 #ifndef ROOT_TNamed
24 #include "TNamed.h"
25 #endif
26 #ifndef ROOT_TMath
27 #include "TMath.h"
28 #endif
29 #ifndef ROOT_TFitResultPtr
30 #include "TFitResultPtr.h"
31 #endif
32 #ifndef ROOT_TObjArray
33 #include "TObjArray.h"
34 #endif
35 #ifndef ROOT_TArrayD
36 #include "TArrayD.h"
37 #endif
38 
39 class TAxis;
40 class TH1;
41 class TH1D;
42 class TH2D;
43 class TH3D;
44 class TF1;
45 class THnIter;
46 
47 namespace ROOT {
48 namespace Internal {
49  class THnBaseBinIter;
50 }
51 }
52 
53 class THnBase: public TNamed {
54 protected:
55  Int_t fNdimensions; // number of dimensions
56  TObjArray fAxes; // axes of the histogram
57  TObjArray fBrowsables; //! browser-helpers for each axis
58  Double_t fEntries; // number of entries, spread over chunks
59  Double_t fTsumw; // total sum of weights
60  Double_t fTsumw2; // total sum of weights squared; -1 if no errors are calculated
61  TArrayD fTsumwx; // total sum of weight*X for each dimension
62  TArrayD fTsumwx2; // total sum of weight*X*X for each dimension
63  Double_t *fIntegral; //! array with bin weight sums
64  enum {
67  kInvalidInt
68  } fIntegralStatus; //! status of integral
69 
70 private:
71  THnBase(const THnBase&); // Not implemented
72  THnBase& operator=(const THnBase&); // Not implemented
73 
74  protected:
76  fNdimensions(0), fEntries(0),
77  fTsumw(0), fTsumw2(-1.), fIntegral(0), fIntegralStatus(kNoInt)
78  {}
79 
80  THnBase(const char* name, const char* title, Int_t dim,
81  const Int_t* nbins, const Double_t* xmin, const Double_t* xmax);
82 
83  void UpdateXStat(const Double_t *x, Double_t w = 1.) {
84  if (GetCalculateErrors()) {
85  for (Int_t d = 0; d < fNdimensions; ++d) {
86  const Double_t xd = x[d];
87  fTsumwx[d] += w * xd;
88  fTsumwx2[d] += w * xd * xd;
89  }
90  }
91  }
92 
94  // Increment the statistics due to filled weight "w",
95  fEntries += 1;
96  if (GetCalculateErrors()) {
97  fTsumw += w;
98  fTsumw2 += w*w;
99  }
100  fIntegralStatus = kInvalidInt;
101  }
102 
103  virtual void InitStorage(Int_t* nbins, Int_t chunkSize) = 0;
104  void Init(const char* name, const char* title,
105  const TObjArray* axes, Bool_t keepTargetAxis,
106  Int_t chunkSize = 1024 * 16);
107  THnBase* CloneEmpty(const char* name, const char* title,
108  const TObjArray* axes, Bool_t keepTargetAxis) const;
109  virtual void Reserve(Long64_t /*nbins*/) {}
110  virtual void SetFilledBins(Long64_t /*nbins*/) {};
111 
112  Bool_t CheckConsistency(const THnBase *h, const char *tag) const;
113  TH1* CreateHist(const char* name, const char* title,
114  const TObjArray* axes, Bool_t keepTargetAxis) const;
115  TObject* ProjectionAny(Int_t ndim, const Int_t* dim,
116  Bool_t wantNDim, Option_t* option = "") const;
117  Bool_t PrintBin(Long64_t idx, Int_t* coord, Option_t* options) const;
118  void AddInternal(const THnBase* h, Double_t c, Bool_t rebinned);
119  THnBase* RebinBase(Int_t group) const;
120  THnBase* RebinBase(const Int_t* group) const;
121  void ResetBase(Option_t *option= "");
122 
123  static THnBase* CreateHnAny(const char* name, const char* title,
124  const TH1* h1, Bool_t sparse,
125  Int_t chunkSize = 1024 * 16);
126  static THnBase* CreateHnAny(const char* name, const char* title,
127  const THnBase* hn, Bool_t sparse,
128  Int_t chunkSize = 1024 * 16);
129 
130  public:
131  virtual ~THnBase();
132 
133  TObjArray* GetListOfAxes() { return &fAxes; }
134  const TObjArray* GetListOfAxes() const { return &fAxes; }
135  TAxis* GetAxis(Int_t dim) const { return (TAxis*)fAxes[dim]; }
136 
137  TFitResultPtr Fit(TF1 *f1 ,Option_t *option = "", Option_t *goption = "");
138  TList* GetListOfFunctions() { return 0; }
139 
140  virtual ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const = 0;
141 
142  virtual Long64_t GetNbins() const = 0;
143  Double_t GetEntries() const { return fEntries; }
144  Double_t GetWeightSum() const { return fTsumw; }
145  Int_t GetNdimensions() const { return fNdimensions; }
146  Bool_t GetCalculateErrors() const { return fTsumw2 >= 0.; }
147  void CalculateErrors(Bool_t calc = kTRUE) {
148  // Calculate errors (or not if "calc" == kFALSE)
149  if (calc) Sumw2();
150  else fTsumw2 = -1.;
151  }
152 
153  Long64_t Fill(const Double_t *x, Double_t w = 1.) {
154  UpdateXStat(x, w);
155  Long64_t bin = GetBin(x, kTRUE /*alloc*/);
156  FillBin(bin, w);
157  return bin;
158  }
159  Long64_t Fill(const char* name[], Double_t w = 1.) {
160  Long64_t bin = GetBin(name, kTRUE /*alloc*/);
161  FillBin(bin, w);
162  return bin;
163  }
164 
165  virtual void FillBin(Long64_t bin, Double_t w) = 0;
166 
167  void SetBinEdges(Int_t idim, const Double_t* bins);
168  Bool_t IsInRange(Int_t *coord) const;
169  Double_t GetBinError(const Int_t *idx) const { return GetBinError(GetBin(idx)); }
170  Double_t GetBinError(Long64_t linidx) const { return TMath::Sqrt(GetBinError2(linidx)); }
171  void SetBinError(const Int_t* idx, Double_t e) { SetBinError(GetBin(idx), e); }
172  void SetBinError(Long64_t bin, Double_t e) { SetBinError2(bin, e*e); }
173  void AddBinContent(const Int_t* x, Double_t v = 1.) { AddBinContent(GetBin(x), v); }
174  void SetEntries(Double_t entries) { fEntries = entries; }
175  void SetTitle(const char *title);
176 
177  Double_t GetBinContent(const Int_t *idx) const { return GetBinContent(GetBin(idx)); } // intentionally non-virtual
178  virtual Double_t GetBinContent(Long64_t bin, Int_t* idx = 0) const = 0;
179  virtual Double_t GetBinError2(Long64_t linidx) const = 0;
180  virtual Long64_t GetBin(const Int_t* idx) const = 0;
181  virtual Long64_t GetBin(const Double_t* x) const = 0;
182  virtual Long64_t GetBin(const char* name[]) const = 0;
183  virtual Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) = 0;
184  virtual Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) = 0;
185  virtual Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) = 0;
186 
187  void SetBinContent(const Int_t* idx, Double_t v) { SetBinContent(GetBin(idx), v); } // intentionally non-virtual
188  virtual void SetBinContent(Long64_t bin, Double_t v) = 0;
189  virtual void SetBinError2(Long64_t bin, Double_t e2) = 0;
190  virtual void AddBinError2(Long64_t bin, Double_t e2) = 0;
191  virtual void AddBinContent(Long64_t bin, Double_t v = 1.) = 0;
192 
193  Double_t GetSumw() const { return fTsumw; }
194  Double_t GetSumw2() const { return fTsumw2; }
195  Double_t GetSumwx(Int_t dim) const { return fTsumwx[dim]; }
196  Double_t GetSumwx2(Int_t dim) const { return fTsumwx2[dim]; }
197 
198  TH1D* Projection(Int_t xDim, Option_t* option = "") const {
199  // Project all bins into a 1-dimensional histogram,
200  // keeping only axis "xDim".
201  // If "option" contains "E" errors will be calculated.
202  // "A" ranges of the taget axes will be ignored.
203  // "O" original axis range of the taget axes will be
204  // kept, but only bins inside the selected range
205  // will be filled.
206  return (TH1D*) ProjectionAny(1, &xDim, false, option);
207  }
208 
209  TH2D* Projection(Int_t yDim, Int_t xDim,
210  Option_t* option = "") const {
211  // Project all bins into a 2-dimensional histogram,
212  // keeping only axes "xDim" and "yDim".
213  //
214  // WARNING: just like TH3::Project3D("yx") and TTree::Draw("y:x"),
215  // Projection(y,x) uses the first argument to define the y-axis and the
216  // second for the x-axis!
217  //
218  // If "option" contains "E" errors will be calculated.
219  // "A" ranges of the taget axes will be ignored.
220 
221  const Int_t dim[2] = {xDim, yDim};
222  return (TH2D*) ProjectionAny(2, dim, false, option);
223  }
224 
225  TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
226  Option_t* option = "") const {
227  // Project all bins into a 3-dimensional histogram,
228  // keeping only axes "xDim", "yDim", and "zDim".
229  // If "option" contains "E" errors will be calculated.
230  // "A" ranges of the taget axes will be ignored.
231  // "O" original axis range of the taget axes will be
232  // kept, but only bins inside the selected range
233  // will be filled.
234 
235  const Int_t dim[3] = {xDim, yDim, zDim};
236  return (TH3D*) ProjectionAny(3, dim, false, option);
237  }
238 
239  THnBase* ProjectionND(Int_t ndim, const Int_t* dim,
240  Option_t* option = "") const {
241  return (THnBase*)ProjectionAny(ndim, dim, kTRUE /*wantNDim*/, option);
242  }
243 
244  Long64_t Merge(TCollection* list);
245 
246  void Scale(Double_t c);
247  void Add(const THnBase* h, Double_t c=1.);
248  void Add(const TH1* hist, Double_t c=1.);
249  void Multiply(const THnBase* h);
250  void Multiply(TF1* f, Double_t c = 1.);
251  void Divide(const THnBase* h);
252  void Divide(const THnBase* h1, const THnBase* h2, Double_t c1 = 1., Double_t c2 = 1., Option_t* option="");
253  void RebinnedAdd(const THnBase* h, Double_t c=1.);
254 
255  virtual void Reset(Option_t* option = "") = 0;
256  virtual void Sumw2() = 0;
257 
258  Double_t ComputeIntegral();
259  void GetRandom(Double_t *rand, Bool_t subBinRandom = kTRUE);
260 
261  void Print(Option_t* option = "") const;
262  void PrintEntries(Long64_t from = 0, Long64_t howmany = -1, Option_t* options = 0) const;
263  void PrintBin(Int_t* coord, Option_t* options) const {
264  PrintBin(-1, coord, options);
265  }
266  void PrintBin(Long64_t idx, Option_t* options) const;
267 
268  void Browse(TBrowser *b);
269  Bool_t IsFolder() const { return kTRUE; }
270 
271  //void Draw(Option_t* option = "");
272 
273  ClassDef(THnBase, 1); // Common base for n-dimensional histogram
274 
275  friend class THnIter;
276 };
277 
278 namespace ROOT {
279 namespace Internal {
280  // Helper class for browing THnBase objects
281  class THnBaseBrowsable: public TNamed {
282  public:
283  THnBaseBrowsable(THnBase* hist, Int_t axis);
284  ~THnBaseBrowsable();
285  void Browse(TBrowser *b);
286  Bool_t IsFolder() const { return kFALSE; }
287 
288  private:
289  THnBase* fHist; // Original histogram
290  Int_t fAxis; // Axis to visualize
291  TH1* fProj; // Projection result
292  ClassDef(THnBaseBrowsable, 0); // Browser-helper for THnBase
293  };
294 
295  // Base class for iterating over THnBase bins
297  public:
298  THnBaseBinIter(Bool_t respectAxisRange):
299  fRespectAxisRange(respectAxisRange), fHaveSkippedBin(kFALSE) {}
300  virtual ~THnBaseBinIter();
301  Bool_t HaveSkippedBin() const { return fHaveSkippedBin; }
302  Bool_t RespectsAxisRange() const { return fRespectAxisRange; }
303 
304  virtual Int_t GetCoord(Int_t dim) const = 0;
305  virtual Long64_t Next(Int_t* coord = 0) = 0;
306 
307  protected:
310  };
311 }
312 }
313 
314 class THnIter: public TObject {
315 public:
316  THnIter(const THnBase* hist, Bool_t respectAxisRange = kFALSE):
317  fIter(hist->CreateIter(respectAxisRange)) {}
318  virtual ~THnIter();
319 
320  Long64_t Next(Int_t* coord = 0) {
321  // Return the next bin's index.
322  // If provided, set coord to that bin's coordinates (bin indexes).
323  // I.e. coord must point to Int_t[hist->GetNdimensions()]
324  // Returns -1 when all bins have been visited.
325  return fIter->Next(coord);
326  }
327 
328  Int_t GetCoord(Int_t dim) const { return fIter->GetCoord(dim); }
329  Bool_t HaveSkippedBin() const { return fIter->HaveSkippedBin(); }
330  Bool_t RespectsAxisRange() const { return fIter->RespectsAxisRange(); }
331 
332 private:
334  ClassDef(THnIter, 0); //Iterator over bins of a THnBase.
335 };
336 
337 #endif // ROOT_THnBase
Double_t GetBinError(const Int_t *idx) const
Definition: THnBase.h:169
An array of TObjects.
Definition: TObjArray.h:39
float xmin
Definition: THbookFile.cxx:93
Bool_t GetCalculateErrors() const
Definition: THnBase.h:146
Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: THnBase.h:269
long long Long64_t
Definition: RtypesCore.h:69
Double_t * fIntegral
Definition: THnBase.h:63
void SetEntries(Double_t entries)
Definition: THnBase.h:174
void CalculateErrors(Bool_t calc=kTRUE)
Definition: THnBase.h:147
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
Double_t fTsumw2
Definition: THnBase.h:60
Long64_t Fill(const Double_t *x, Double_t w=1.)
Definition: THnBase.h:153
return c
const char Option_t
Definition: RtypesCore.h:62
return c1
Definition: legend1.C:41
TH1 * h
Definition: legend2.C:5
Double_t GetBinContent(const Int_t *idx) const
Definition: THnBase.h:177
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
THnBaseBinIter(Bool_t respectAxisRange)
Definition: THnBase.h:298
Bool_t HaveSkippedBin() const
Definition: THnBase.h:301
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Definition: THnBase.h:225
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition: THnBase.h:173
TObjArray fAxes
Definition: THnBase.h:56
Double_t fTsumw
Definition: THnBase.h:59
THnIter(const THnBase *hist, Bool_t respectAxisRange=kFALSE)
Definition: THnBase.h:316
Double_t fEntries
browser-helpers for each axis
Definition: THnBase.h:58
Double_t x[n]
Definition: legend1.C:17
TBrowser helper for THnBase.
Definition: THnBase.h:281
#define ClassDef(name, id)
Definition: Rtypes.h:254
TList * GetListOfFunctions()
Definition: THnBase.h:138
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnBase.h:198
Long64_t Fill(const char *name[], Double_t w=1.)
Definition: THnBase.h:159
Int_t GetCoord(Int_t dim) const
Definition: THnBase.h:328
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
virtual void Reserve(Long64_t)
Definition: THnBase.h:109
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
Definition: THist.hxx:327
void SetBinContent(const Int_t *idx, Double_t v)
Definition: THnBase.h:187
Double_t GetSumw2() const
Definition: THnBase.h:194
Double_t GetSumw() const
Definition: THnBase.h:193
TObjArray fBrowsables
Definition: THnBase.h:57
TH1F * h1
Definition: legend1.C:5
A doubly linked list.
Definition: TList.h:47
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
virtual void SetFilledBins(Long64_t)
Definition: THnBase.h:110
void PrintBin(Int_t *coord, Option_t *options) const
Definition: THnBase.h:263
TAxis * GetAxis(Int_t dim) const
Definition: THnBase.h:135
Class to manage histogram axis.
Definition: TAxis.h:36
tomato 3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:308
SVector< double, 2 > v
Definition: Dict.h:5
Long64_t Next(Int_t *coord=0)
Definition: THnBase.h:320
Iterator over THnBase bins (internal implementation).
Definition: THnBase.h:296
Provides an indirection to the TFitResult class and with a semantics identical to a TFitResult pointe...
Definition: TFitResultPtr.h:33
Collection abstract base class.
Definition: TCollection.h:48
Double_t GetSumwx2(Int_t dim) const
Definition: THnBase.h:196
float xmax
Definition: THbookFile.cxx:93
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Definition: THnBase.h:209
void Reset(Detail::TBranchProxy *x)
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
Double_t GetEntries() const
Definition: THnBase.h:143
TArrayD fTsumwx2
Definition: THnBase.h:62
Double_t GetSumwx(Int_t dim) const
Definition: THnBase.h:195
void UpdateXStat(const Double_t *x, Double_t w=1.)
Definition: THnBase.h:83
void SetBinError(const Int_t *idx, Double_t e)
Definition: THnBase.h:171
return c2
Definition: legend2.C:14
double f(double x)
void Print(std::ostream &os, const OptionType &opt)
ROOT::Internal::THnBaseBinIter * fIter
Definition: THnBase.h:333
double Double_t
Definition: RtypesCore.h:55
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition: THnBase.h:239
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
Double_t GetBinError(Long64_t linidx) const
Definition: THnBase.h:170
THnBase()
Definition: THnBase.h:75
const TObjArray * GetListOfAxes() const
Definition: THnBase.h:134
The TH1 histogram class.
Definition: TH1.h:80
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
Array of doubles (64 bits per element).
Definition: TArrayD.h:29
Int_t GetNdimensions() const
Definition: THnBase.h:145
Mother of all ROOT objects.
Definition: TObject.h:37
Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: THnBase.h:286
Double_t GetWeightSum() const
Definition: THnBase.h:144
Bool_t RespectsAxisRange() const
Definition: THnBase.h:330
Int_t fNdimensions
Definition: THnBase.h:55
Bool_t HaveSkippedBin() const
Definition: THnBase.h:329
Iterator over THnBase bins.
Definition: THnBase.h:314
1-Dim function class
Definition: TF1.h:149
Bool_t RespectsAxisRange() const
Definition: THnBase.h:302
TF1 * f1
Definition: legend1.C:11
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TObjArray * GetListOfAxes()
Definition: THnBase.h:133
Multidimensional histogram base.
Definition: THnBase.h:53
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Bool_t kTRUE
Definition: Rtypes.h:91
void SetBinError(Long64_t bin, Double_t e)
Definition: THnBase.h:172
char name[80]
Definition: TGX11.cxx:109
TArrayD fTsumwx
Definition: THnBase.h:61
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:296