Logo ROOT   6.08/07
Reference Guide
TUnfoldBinning.h
Go to the documentation of this file.
1 // Author: Stefan Schmitt
2 // DESY, 10/08/11
3 
4 // Version 17.1, in parallel to TUnfold
5 //
6 // History:
7 // Version 17.0, initial version, numbered in parallel to TUnfold
8 
9 #ifndef ROOT_TUnfoldBinning
10 #define ROOT_TUnfoldBinning
11 
12 
13 //////////////////////////////////////////////////////////////////////////
14 // //
15 // //
16 // TUnfoldBinning, an auxillary class to provide //
17 // complex binning schemes as input to TUnfoldDensity //
18 // //
19 // Citation: S.Schmitt, JINST 7 (2012) T10003 [arXiv:1205.6201] //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 
23 /*
24  This file is part of TUnfold.
25 
26  TUnfold is free software: you can redistribute it and/or modify
27  it under the terms of the GNU General Public License as published by
28  the Free Software Foundation, either version 3 of the License, or
29  (at your option) any later version.
30 
31  TUnfold is distributed in the hope that it will be useful,
32  but WITHOUT ANY WARRANTY; without even the implied warranty of
33  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34  GNU General Public License for more details.
35 
36  You should have received a copy of the GNU General Public License
37  along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
38 */
39 
40 #include "TUnfold.h"
41 #include <iostream>
42 #include <TNamed.h>
43 #include <TObjArray.h>
44 #include <TObjString.h>
45 
46 class TAxis;
47 class TF1;
48 
49 
50 class TUnfoldBinning : public TNamed {
51  protected:
52  TUnfoldBinning *parentNode; // mother node
53  TUnfoldBinning *childNode; // first daughter node
54  TUnfoldBinning *nextNode; // next sister
55  TUnfoldBinning *prevNode; // previous sister
56  TObjArray *fAxisList; // for each axis the bin borders (TVectorD)
57  TObjArray *fAxisLabelList; // for each axis its name (TObjString)
58  Int_t fHasUnderflow,fHasOverflow; // bit fields indicating whether there are underflow/overflow bins on the axes
59  Int_t fDistributionSize; // number of bins in this node's distribution
60  Int_t fFirstBin; // global bin number of the first bin
61  Int_t fLastBin; // global bin number of the last(+1) bin
62  TF1 *fBinFactorFunction; // function to calculate user factor from bin centres (default function is a constant)
63  Double_t fBinFactorConstant; // scale factor on user factor
64  public:
65  /********************* setup **************************/
66 
67  TUnfoldBinning(const char *name=0,Int_t nBins=0,const char *binNames=0); // create a new root node with a given number of unconnected bins
68  TUnfoldBinning(const TAxis &axis,Int_t includeUnderflow,Int_t includeOverflow); // create a binning scheme with one axis
69  TUnfoldBinning *AddBinning(TUnfoldBinning *binning); // add a new node to the TUnfoldBinning tree
70  TUnfoldBinning *AddBinning(const char *name,Int_t nBins=0,const char *binNames=0); // add a new node to the TUnfoldBinning tree
71  Bool_t AddAxis(const char *name,Int_t nBins,const Double_t *binBorders,
72  Bool_t hasUnderflow,Bool_t hasOverflow); // add an axis (variable bins) to the distribution associated with this node
73  Bool_t AddAxis(const char *name,Int_t nBins,Double_t xMin,Double_t xMax,
74  Bool_t hasUnderflow,Bool_t hasOverflow); // add an axis (equidistant bins) to the distribution associated with this node
75  Bool_t AddAxis(const TAxis &axis,Bool_t includeUnderflow,Bool_t includeOverflow); // add an axis (from TAxis instance) to the distribution associated with this node
76  virtual ~TUnfoldBinning(void);
77  void PrintStream(std::ostream &out,Int_t indent=0) const;
78  inline void SetBinFactorFunction(Double_t normalisation,TF1 *userFunc=0) {
79  fBinFactorConstant=normalisation; fBinFactorFunction=userFunc; }// define function to calculate bin factor
80 
81  /********************* Navigation **********************/
82  inline TUnfoldBinning const *GetChildNode(void) const { return childNode; } // first daughter
83  inline TUnfoldBinning const *GetPrevNode(void) const { return prevNode; } // previoous sister
84  inline TUnfoldBinning const *GetNextNode(void) const { return nextNode; } // next sister
85  inline TUnfoldBinning const *GetParentNode(void) const { return parentNode; } // mother
86  TUnfoldBinning const *FindNode(char const *name) const; // find node by name
87 
88  /********************* Create THxx histograms **********/
89  Int_t GetTH1xNumberOfBins(Bool_t originalAxisBinning=kTRUE,const char *axisSteering=0) const; // get number of bins of a one-dimensional histogram TH1
90  TH1 *CreateHistogram(const char *histogramName,Bool_t originalAxisBinning=kFALSE,Int_t **binMap=0,const char *histogramTitle=0,const char *axisSteering=0) const; // create histogram and bin map for this node
91  TH2D *CreateErrorMatrixHistogram(const char *histogramName,Bool_t originalAxisBinning,Int_t **binMap=0,const char *histogramTitle=0,const char *axisSteering=0) const; // create histogram and bin map for this node
92  static TH2D *CreateHistogramOfMigrations(TUnfoldBinning const *xAxis,
93  TUnfoldBinning const *yAxis,
94  char const *histogramName,
95  Bool_t originalXAxisBinning=kFALSE,
96  Bool_t originalYAxisBinning=kFALSE,
97  char const *histogramTitle=0); // create 2D histogram with one binning on the x axis and the other binning on the y axis
98  TH1 *ExtractHistogram(const char *histogramName,const TH1 *globalBins,const TH2 *globalBinsEmatrix=0,Bool_t originalAxisBinning=kTRUE,const char *axisSteering=0) const; // extract a distribution from the given set of global bins
99  /********************* Calculate global bin number ******/
100  Int_t GetGlobalBinNumber(Double_t x) const; // get bin number 1-dim distribution
101  Int_t GetGlobalBinNumber(Double_t x,Double_t y) const; // get bin number 2-dim distribution
102  Int_t GetGlobalBinNumber(Double_t x,Double_t y,Double_t z) const; // get bin number 3-dim distribution
103  Int_t GetGlobalBinNumber(Double_t x0,Double_t x1,Double_t x2,Double_t x3) const; // get bin number for given variables, up to four-dimensional binning
104  Int_t GetGlobalBinNumber(const Double_t *x) const; // get bin number, up to 32 dimenstional binning
105  inline Int_t GetStartBin(void) const { return fFirstBin; } // first bin of this node
106  inline Int_t GetEndBin(void) const { return fLastBin; } // last+1 bin of this node (includes children)
107 
108  /********************* access by global bin number ******/
109  TString GetBinName(Int_t iBin) const; // return bin name
110  Double_t GetBinSize(Int_t iBin) const; // return bin size (in N dimensions)
111  virtual Double_t GetBinFactor(Int_t iBin) const; // return user factor
112  void GetBinUnderflowOverflowStatus(Int_t iBin,Int_t *uStatus,Int_t *oStatus) const; // return bit map indicating underflow and overflow status
113  void GetBinNeighbours(Int_t globalBin,Int_t axis,
114  Int_t *prev,Double_t *distPrev,
115  Int_t *next,Double_t *distNext) const; // get neighbour bins along an axis
116  /********************* access by bin number, given an axis steering ******/
117  enum { MAXDIM=32 };
118  const TUnfoldBinning *GetBinLocation(Int_t binTHxx,const char *axisSteering,
119  Int_t axisBins[MAXDIM]) const; // locate a given THxx bin for a given axis steering
120  void DecodeAxisSteering(const char *axisSteering,const char *options,
121  Int_t *isOptionGiven) const; // decode axis steering options
122  /********************** access distribution properties *************/
123  inline Int_t GetDistributionNumberOfBins(void) const { return fDistributionSize; } // number of bins in the distribution possibly including under/overflow
124  inline Int_t GetDistributionDimension(void) const { return fAxisList->GetEntriesFast(); } // query dimension of this node's distribution
125  virtual Double_t GetDistributionAverageBinSize(Int_t axis,Bool_t includeUnderflow, Bool_t includeOverflow) const; // get average bin size
126  inline TVectorD const *GetDistributionBinning(Int_t axis) const {
127  return (TVectorD const *)fAxisList->At(axis); } // get bin borders for some axis
129  return ((TObjString * const)fAxisLabelList->At(axis))->GetString(); }// get name of this axis
130 
131  virtual Double_t GetDistributionUnderflowBinWidth(Int_t axis) const; // width of underflow bin on the given axis
132  virtual Double_t GetDistributionOverflowBinWidth(Int_t axis) const; // width of overflow bin on the given axis
133  virtual Double_t GetDistributionBinCenter(Int_t axis,Int_t bin) const; // position of bin center on the given axis
134  protected:
135  TUnfoldBinning *GetRootNode(void); // return root node
136  TUnfoldBinning const *GetRootNode(void) const; // return root node
137  void Initialize(Int_t nBins);
138  Int_t UpdateFirstLastBin(Bool_t startWithRootNode=kTRUE); // update fFirstBin and fLastBin
139  Bool_t HasUnconnectedBins(void) const; // check whether this node has bins without axis
140  TUnfoldBinning const *ToAxisBins(Int_t globalBin,Int_t *axisBins) const; // return distribution in which the bin is located
141  Int_t ToGlobalBin(Int_t const *axisBins) const; // return -1 if not inside distribution
142  TString BuildHistogramTitle(const char *histogramName,const char *histogramTitle,
143  Int_t const *axisList) const; // construct histogram title
144  TString BuildHistogramTitle2D(const char *histogramName,const char *histogramTitle,
145  Int_t xAxis,const TUnfoldBinning *yAxisBinning,Int_t yAxis) const; // construct histogram title
146  Int_t GetTHxxBinning(Int_t maxDim,Int_t *axisBins,Int_t *axisList,const char *axisSteering) const; // get binning information for creating a THxx
147  Int_t GetTHxxBinningSingleNode(Int_t maxDim,Int_t *axisBins,Int_t *axisList,const char *axisSteering) const; // get binning information for creating a THxx
148  Int_t GetTHxxBinsRecursive(const char *axisSteering) const; // get binning information for creating a THxx
149  const TUnfoldBinning *GetBinLocationRecursive(Int_t &offset,const char *axisSteering,
150  Int_t axisBins[MAXDIM]) const; // locate a THxx bin offset for a given axis steering
151  const TUnfoldBinning *GetNonemptyNode(void) const; // get the only nodes with non-empty distributions if there are multiple nodes, return 0
152  Int_t *CreateBinMap(const TH1 *hist,Int_t nDim,const Int_t *axisList,const char *axisSteering) const; // create mapping from global bins to a histogram
153  Int_t FillBinMapRecursive(Int_t startBin,const char *axisSteering,
154  Int_t *binMap) const; // fill bin map recursively
155  Int_t FillBinMapSingleNode(const TH1 *hist,Int_t startBin,Int_t nDim,const Int_t *axisList,const char *axisSteering,Int_t *binMap) const; // fill bin map for a single node
156 
157  ClassDef(TUnfoldBinning, TUnfold_CLASS_VERSION) //Complex binning schemes for TUnfoldDensity
158 };
159 
160 #endif
const TUnfoldBinning * GetBinLocation(Int_t binTHxx, const char *axisSteering, Int_t axisBins[MAXDIM]) const
TUnfoldBinning const * GetChildNode(void) const
An array of TObjects.
Definition: TObjArray.h:39
TUnfoldBinning * parentNode
TH1 * CreateHistogram(const char *histogramName, Bool_t originalAxisBinning=kFALSE, Int_t **binMap=0, const char *histogramTitle=0, const char *axisSteering=0) const
TH2D * CreateErrorMatrixHistogram(const char *histogramName, Bool_t originalAxisBinning, Int_t **binMap=0, const char *histogramTitle=0, const char *axisSteering=0) const
virtual Double_t GetDistributionBinCenter(Int_t axis, Int_t bin) const
const TUnfoldBinning * GetNonemptyNode(void) const
Collectable string class.
Definition: TObjString.h:32
Int_t fDistributionSize
TUnfoldBinning * childNode
virtual Double_t GetDistributionAverageBinSize(Int_t axis, Bool_t includeUnderflow, Bool_t includeOverflow) const
Int_t * CreateBinMap(const TH1 *hist, Int_t nDim, const Int_t *axisList, const char *axisSteering) const
TString BuildHistogramTitle2D(const char *histogramName, const char *histogramTitle, Int_t xAxis, const TUnfoldBinning *yAxisBinning, Int_t yAxis) const
TUnfoldBinning const * ToAxisBins(Int_t globalBin, Int_t *axisBins) const
Double_t GetBinSize(Int_t iBin) const
TUnfoldBinning * nextNode
void GetBinNeighbours(Int_t globalBin, Int_t axis, Int_t *prev, Double_t *distPrev, Int_t *next, Double_t *distNext) const
void GetBinUnderflowOverflowStatus(Int_t iBin, Int_t *uStatus, Int_t *oStatus) const
void PrintStream(std::ostream &out, Int_t indent=0) const
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
Int_t GetTHxxBinning(Int_t maxDim, Int_t *axisBins, Int_t *axisList, const char *axisSteering) const
virtual ~TUnfoldBinning(void)
TUnfoldBinning const * GetNextNode(void) const
Int_t UpdateFirstLastBin(Bool_t startWithRootNode=kTRUE)
Double_t fBinFactorConstant
const TUnfoldBinning * GetBinLocationRecursive(Int_t &offset, const char *axisSteering, Int_t axisBins[MAXDIM]) const
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
Int_t GetEndBin(void) const
#define ClassDef(name, id)
Definition: Rtypes.h:254
Int_t ToGlobalBin(Int_t const *axisBins) const
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TObjArray * fAxisLabelList
void Initialize(Int_t nBins)
TF1 * fBinFactorFunction
Int_t GetStartBin(void) const
TVectorD const * GetDistributionBinning(Int_t axis) const
Int_t GetDistributionDimension(void) const
TUnfoldBinning const * GetParentNode(void) const
TH1 * ExtractHistogram(const char *histogramName, const TH1 *globalBins, const TH2 *globalBinsEmatrix=0, Bool_t originalAxisBinning=kTRUE, const char *axisSteering=0) const
Bool_t AddAxis(const char *name, Int_t nBins, const Double_t *binBorders, Bool_t hasUnderflow, Bool_t hasOverflow)
void DecodeAxisSteering(const char *axisSteering, const char *options, Int_t *isOptionGiven) const
void SetBinFactorFunction(Double_t normalisation, TF1 *userFunc=0)
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
Class to manage histogram axis.
Definition: TAxis.h:36
virtual Double_t GetDistributionUnderflowBinWidth(Int_t axis) const
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
TUnfoldBinning const * GetPrevNode(void) const
static void indent(ostringstream &buf, int indent_level)
static TH2D * CreateHistogramOfMigrations(TUnfoldBinning const *xAxis, TUnfoldBinning const *yAxis, char const *histogramName, Bool_t originalXAxisBinning=kFALSE, Bool_t originalYAxisBinning=kFALSE, char const *histogramTitle=0)
This class serves as a container of analysis bins analysis bins are specified by defining the axes of...
TString GetDistributionAxisLabel(Int_t axis) const
static const double x1[5]
double Double_t
Definition: RtypesCore.h:55
TUnfoldBinning * prevNode
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
Int_t GetDistributionNumberOfBins(void) const
Int_t FillBinMapSingleNode(const TH1 *hist, Int_t startBin, Int_t nDim, const Int_t *axisList, const char *axisSteering, Int_t *binMap) const
#define TUnfold_CLASS_VERSION
Definition: TUnfold.h:96
Int_t GetGlobalBinNumber(Double_t x) const
TString GetBinName(Int_t iBin) const
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Int_t GetTH1xNumberOfBins(Bool_t originalAxisBinning=kTRUE, const char *axisSteering=0) const
Int_t FillBinMapRecursive(Int_t startBin, const char *axisSteering, Int_t *binMap) const
Bool_t HasUnconnectedBins(void) const
TObjArray * fAxisList
virtual Double_t GetDistributionOverflowBinWidth(Int_t axis) const
1-Dim function class
Definition: TF1.h:149
TUnfoldBinning(const char *name=0, Int_t nBins=0, const char *binNames=0)
Int_t GetTHxxBinningSingleNode(Int_t maxDim, Int_t *axisBins, Int_t *axisList, const char *axisSteering) const
TString BuildHistogramTitle(const char *histogramName, const char *histogramTitle, Int_t const *axisList) const
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t GetTHxxBinsRecursive(const char *axisSteering) const
TUnfoldBinning * GetRootNode(void)
TUnfoldBinning * AddBinning(TUnfoldBinning *binning)
TUnfoldBinning const * FindNode(char const *name) const
char name[80]
Definition: TGX11.cxx:109
virtual Double_t GetBinFactor(Int_t iBin) const
static const double x3[11]
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:296