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