Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 {
55  kNoInt,
56  kValidInt,
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:
65  THnBase():
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 
83  /// Increment the statistics due to filled weight "w",
84  void FillBinBase(Double_t 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 
138  /// Calculate errors (or not if "calc" == kFALSE)
139  void CalculateErrors(Bool_t calc = kTRUE) {
140  if (calc) Sumw2();
141  else fTsumw2 = -1.;
142  }
143 
144  Long64_t Fill(const Double_t *x, Double_t w = 1.) {
145  UpdateXStat(x, w);
146  Long64_t bin = GetBin(x, kTRUE /*alloc*/);
147  FillBin(bin, w);
148  return bin;
149  }
150  Long64_t Fill(const char* name[], Double_t w = 1.) {
151  Long64_t bin = GetBin(name, kTRUE /*alloc*/);
152  FillBin(bin, w);
153  return bin;
154  }
155 
156  virtual void FillBin(Long64_t bin, Double_t w) = 0;
157 
158  void SetBinEdges(Int_t idim, const Double_t* bins);
159  Bool_t IsInRange(Int_t *coord) const;
160  Double_t GetBinError(const Int_t *idx) const { return GetBinError(GetBin(idx)); }
161  Double_t GetBinError(Long64_t linidx) const { return TMath::Sqrt(GetBinError2(linidx)); }
162  void SetBinError(const Int_t* idx, Double_t e) { SetBinError(GetBin(idx), e); }
163  void SetBinError(Long64_t bin, Double_t e) { SetBinError2(bin, e*e); }
164  void AddBinContent(const Int_t* x, Double_t v = 1.) { AddBinContent(GetBin(x), v); }
165  void SetEntries(Double_t entries) { fEntries = entries; }
166  void SetTitle(const char *title);
167 
168  Double_t GetBinContent(const Int_t *idx) const { return GetBinContent(GetBin(idx)); } // intentionally non-virtual
169  virtual Double_t GetBinContent(Long64_t bin, Int_t* idx = 0) const = 0;
170  virtual Double_t GetBinError2(Long64_t linidx) const = 0;
171  virtual Long64_t GetBin(const Int_t* idx) const = 0;
172  virtual Long64_t GetBin(const Double_t* x) const = 0;
173  virtual Long64_t GetBin(const char* name[]) const = 0;
174  virtual Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) = 0;
175  virtual Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) = 0;
176  virtual Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) = 0;
177 
178  void SetBinContent(const Int_t* idx, Double_t v) { SetBinContent(GetBin(idx), v); } // intentionally non-virtual
179  virtual void SetBinContent(Long64_t bin, Double_t v) = 0;
180  virtual void SetBinError2(Long64_t bin, Double_t e2) = 0;
181  virtual void AddBinError2(Long64_t bin, Double_t e2) = 0;
182  virtual void AddBinContent(Long64_t bin, Double_t v = 1.) = 0;
183 
184  Double_t GetSumw() const { return fTsumw; }
185  Double_t GetSumw2() const { return fTsumw2; }
186  Double_t GetSumwx(Int_t dim) const { return fTsumwx[dim]; }
187  Double_t GetSumwx2(Int_t dim) const { return fTsumwx2[dim]; }
188 
189  /// Project all bins into a 1-dimensional histogram,
190  /// keeping only axis "xDim".
191  /// If "option" contains:
192  /// - "E" errors will be calculated.
193  /// - "A" ranges of the taget axes will be ignored.
194  /// - "O" original axis range of the taget axes will be
195  /// kept, but only bins inside the selected range
196  /// will be filled.
197  TH1D* Projection(Int_t xDim, Option_t* option = "") const {
198  return (TH1D*) ProjectionAny(1, &xDim, false, option);
199  }
200 
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  TH2D* Projection(Int_t yDim, Int_t xDim, Option_t* option = "") const {
211  const Int_t dim[2] = {xDim, yDim};
212  return (TH2D*) ProjectionAny(2, dim, false, option);
213  }
214 
215  /// Project all bins into a 3-dimensional histogram,
216  /// keeping only axes "xDim", "yDim", and "zDim".
217  /// If "option" contains:
218  /// - "E" errors will be calculated.
219  /// - "A" ranges of the taget axes will be ignored.
220  /// - "O" original axis range of the taget axes will be
221  /// kept, but only bins inside the selected range
222  /// will be filled.
223  TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t* option = "") const {
224  const Int_t dim[3] = {xDim, yDim, zDim};
225  return (TH3D*) ProjectionAny(3, dim, false, option);
226  }
227 
228  THnBase* ProjectionND(Int_t ndim, const Int_t* dim,
229  Option_t* option = "") const {
230  return (THnBase*)ProjectionAny(ndim, dim, kTRUE /*wantNDim*/, option);
231  }
232 
233  Long64_t Merge(TCollection* list);
234 
235  void Scale(Double_t c);
236  void Add(const THnBase* h, Double_t c=1.);
237  void Add(const TH1* hist, Double_t c=1.);
238  void Multiply(const THnBase* h);
239  void Multiply(TF1* f, Double_t c = 1.);
240  void Divide(const THnBase* h);
241  void Divide(const THnBase* h1, const THnBase* h2, Double_t c1 = 1., Double_t c2 = 1., Option_t* option="");
242  void RebinnedAdd(const THnBase* h, Double_t c=1.);
243 
244  virtual void Reset(Option_t* option = "") = 0;
245  virtual void Sumw2() = 0;
246 
247  Double_t ComputeIntegral();
248  void GetRandom(Double_t *rand, Bool_t subBinRandom = kTRUE);
249 
250  void Print(Option_t* option = "") const;
251  void PrintEntries(Long64_t from = 0, Long64_t howmany = -1, Option_t* options = 0) const;
252  void PrintBin(Int_t* coord, Option_t* options) const {
253  PrintBin(-1, coord, options);
254  }
255  void PrintBin(Long64_t idx, Option_t* options) const;
256 
257  void Browse(TBrowser *b);
258  Bool_t IsFolder() const { return kTRUE; }
259 
260  //void Draw(Option_t* option = "");
261 
262  ClassDef(THnBase, 1); // Common base for n-dimensional histogram
263 
264  friend class THnIter;
265 };
266 
267 namespace ROOT {
268 namespace Internal {
269  // Helper class for browsing THnBase objects
270  class THnBaseBrowsable: public TNamed {
271  public:
272  THnBaseBrowsable(THnBase* hist, Int_t axis);
273  ~THnBaseBrowsable();
274  void Browse(TBrowser *b);
275  Bool_t IsFolder() const { return kFALSE; }
276 
277  private:
278  THnBase* fHist; // Original histogram
279  Int_t fAxis; // Axis to visualize
280  TH1* fProj; // Projection result
281  ClassDef(THnBaseBrowsable, 0); // Browser-helper for THnBase
282  };
283 
284  // Base class for iterating over THnBase bins
285  class THnBaseBinIter {
286  public:
287  THnBaseBinIter(Bool_t respectAxisRange):
288  fRespectAxisRange(respectAxisRange), fHaveSkippedBin(kFALSE) {}
289  virtual ~THnBaseBinIter();
290  Bool_t HaveSkippedBin() const { return fHaveSkippedBin; }
291  Bool_t RespectsAxisRange() const { return fRespectAxisRange; }
292 
293  virtual Int_t GetCoord(Int_t dim) const = 0;
294  virtual Long64_t Next(Int_t* coord = 0) = 0;
295 
296  protected:
297  Bool_t fRespectAxisRange;
298  Bool_t fHaveSkippedBin;
299  };
300 }
301 }
302 
303 class THnIter: public TObject {
304 public:
305  THnIter(const THnBase* hist, Bool_t respectAxisRange = kFALSE):
306  fIter(hist->CreateIter(respectAxisRange)) {}
307  virtual ~THnIter();
308 
309  /// Return the next bin's index.
310  /// If provided, set coord to that bin's coordinates (bin indexes).
311  /// I.e. coord must point to Int_t[hist->GetNdimensions()]
312  /// Returns -1 when all bins have been visited.
313  Long64_t Next(Int_t* coord = 0) {
314  return fIter->Next(coord);
315  }
316 
317  Int_t GetCoord(Int_t dim) const { return fIter->GetCoord(dim); }
318  Bool_t HaveSkippedBin() const { return fIter->HaveSkippedBin(); }
319  Bool_t RespectsAxisRange() const { return fIter->RespectsAxisRange(); }
320 
321 private:
322  ROOT::Internal::THnBaseBinIter* fIter;
323  ClassDef(THnIter, 0); //Iterator over bins of a THnBase.
324 };
325 
326 #endif // ROOT_THnBase