Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUnfoldBinning.h
Go to the documentation of this file.
1// Author: Stefan Schmitt
2// DESY, 10/08/11
3
4// Version 17.9, parallel to changes in TUnfold
5//
6// History:
7// Version 17.8, bug fix in GetNonemptyNode() and non-const access of tree
8// Version 17.7, bug fix in ExtractHistogram
9// Version 17.6, bug fix to avoid possible crash in method
10// CreateHistogramOfMigrations(). Bug fix with NaN in GetGlobalBinNumber()
11// Version 17.5, in parallel to changes in TUnfold
12// Version 17.4, bug fix with error handling
13// Version 17.3, bug fix with underflow/overflow bins
14// Version 17.2, new option isPeriodic
15// Version 17.1, in parallel to TUnfold
16// Version 17.0, initial version, numbered in parallel to TUnfold
17
18#ifndef ROOT_TUnfoldBinning
19#define ROOT_TUnfoldBinning
20
21
22//////////////////////////////////////////////////////////////////////////
23// //
24// //
25// TUnfoldBinning, an auxillary class to provide //
26// complex binning schemes as input to TUnfoldDensity //
27// //
28// Citation: S.Schmitt, JINST 7 (2012) T10003 [arXiv:1205.6201] //
29// //
30//////////////////////////////////////////////////////////////////////////
31
32/*
33 This file is part of TUnfold.
34
35 TUnfold is free software: you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation, either version 3 of the License, or
38 (at your option) any later version.
39
40 TUnfold is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44
45 You should have received a copy of the GNU General Public License
46 along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
47*/
48
49#include "TUnfold.h"
50#include <iostream>
51#include <TNamed.h>
52#include <TObjArray.h>
53#include <TObjString.h>
54#include <TAxis.h>
55#include <TF1.h>
56
57
58class TUnfoldBinning : public TNamed {
59 protected:
60 /// mother node
62 /// first daughter node
64 /// next sister
66 /// previous sister
68 /// for each axis the bin borders (TVectorD)
70 /// for each axis its name (TObjString), or names of unconnected bins
72 /// bit fields indicating whether there are underflow bins on the axes
74 /// bit fields indicating whether there are overflow bins on the axes
76 /// number of bins in this node's distribution
78 /// global bin number of the first bin
80 /// global bin number of the last(+1) bin, including daughters
82 /// function to calculate a scale factor from bin centres (may be a TF1 or a TVectorD
84 /// common scale factor for all bins of this node
86 public:
87 /********************* setup **************************/
88 enum {
89 /// maximum numner of axes per distribution
90 MAXDIM=32
91 };
92 TUnfoldBinning(const char *name=nullptr,Int_t nBins=0,const char *binNames=nullptr); // create a new root node with a given number of unconnected bins
93 TUnfoldBinning(const TAxis &axis,Int_t includeUnderflow,Int_t includeOverflow); // create a binning scheme with one axis
95 (TUnfoldBinning *binning); // add a new node to the TUnfoldBinning tree
96 TUnfoldBinning *AddBinning(const char *name,Int_t nBins=0,const char *binNames=nullptr); // add a new node to the TUnfoldBinning tree
97 Bool_t AddAxis(const char *name,Int_t nBins,const Double_t *binBorders,
98 Bool_t hasUnderflow,Bool_t hasOverflow); // add an axis (variable bins) to the distribution associated with this node
99 Bool_t AddAxis(const char *name,Int_t nBins,Double_t xMin,Double_t xMax,
100 Bool_t hasUnderflow,Bool_t hasOverflow); // add an axis (equidistant bins) to the distribution associated with this node
101 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
102 ~TUnfoldBinning(void) override;
103 void PrintStream(std::ostream &out,Int_t indent=0,int debug=0) const;
104 void SetBinFactorFunction(Double_t normalisation,TF1 *userFunc=nullptr); // define function to calculate bin factor. Note: the function is not owned by this class
105
106 /********************* Navigation **********************/
107 /// first daughter node
108 inline TUnfoldBinning const *GetChildNode(void) const { return childNode; }
109 inline TUnfoldBinning *GetChildNode(void) { return childNode; }
110 /// previous sister node
111 inline TUnfoldBinning const *GetPrevNode(void) const { return prevNode; }
112 /// next sister node
113 inline TUnfoldBinning const *GetNextNode(void) const { return nextNode; }
114 inline TUnfoldBinning *GetNextNode(void) { return nextNode; }
115 /// mother node
116 inline TUnfoldBinning const *GetParentNode(void) const { return parentNode; }
117 inline TUnfoldBinning *GetParentNode(void) { return parentNode; }
118 TUnfoldBinning const *FindNode(char const *name) const; // find node by name
119 /// return root node of the binnig scheme
120 TUnfoldBinning const *GetRootNode(void) const;
121
122 /********************* Create THxx histograms **********/
123 Int_t GetTH1xNumberOfBins(Bool_t originalAxisBinning=kTRUE,const char *axisSteering=nullptr) const; // get number of bins of a one-dimensional histogram TH1
124 TH1 *CreateHistogram(const char *histogramName,Bool_t originalAxisBinning=kFALSE,Int_t **binMap=nullptr,const char *histogramTitle=nullptr,const char *axisSteering=nullptr) const; // create histogram and bin map for this node
125 TH2D *CreateErrorMatrixHistogram(const char *histogramName,Bool_t originalAxisBinning,Int_t **binMap=nullptr,const char *histogramTitle=nullptr,const char *axisSteering=nullptr) const; // create histogram and bin map for this node
127 TUnfoldBinning const *yAxis,
128 char const *histogramName,
129 Bool_t originalXAxisBinning=kFALSE,
130 Bool_t originalYAxisBinning=kFALSE,
131 char const *histogramTitle=nullptr); // create 2D histogram with one binning on the x axis and the other binning on the y axis
132 TH1 *ExtractHistogram(const char *histogramName,const TH1 *globalBins,const TH2 *globalBinsEmatrix=nullptr,Bool_t originalAxisBinning=kTRUE,const char *axisSteering=nullptr) const; // extract a distribution from the given set of global bins
133 /********************* Create and manipulate bin map ****/
134 Int_t *CreateEmptyBinMap(void) const; // create empty bin map
135 void SetBinMapEntry(Int_t *binMap,Int_t globalBin,Int_t destBin) const; // change mapping for a given global bin
136 Int_t FillBinMap1D(Int_t *binMap,const char *axisSteering,
137 Int_t firstBinX) const; // map bins from this distribution to destHist
138 /********************* Calculate global bin number ******/
139 Int_t GetGlobalBinNumber(Double_t x) const; // get bin number 1-dim distribution
140 Int_t GetGlobalBinNumber(Double_t x,Double_t y) const; // get bin number 2-dim distribution
141 Int_t GetGlobalBinNumber(Double_t x,Double_t y,Double_t z) const; // get bin number 3-dim distribution
142 Int_t GetGlobalBinNumber(Double_t x0,Double_t x1,Double_t x2,Double_t x3) const; // get bin number four-dimensional binning
143 Int_t GetGlobalBinNumber(Double_t x0,Double_t x1,Double_t x2,Double_t x3,Double_t x4) const; // get bin number five-dimensional binning
144 Int_t GetGlobalBinNumber(Double_t x0,Double_t x1,Double_t x2,Double_t x3,Double_t x4,Double_t x5) const; // get bin number six-dimensional binning
145 Int_t GetGlobalBinNumber(const Double_t *x,Int_t *isBelow=nullptr,Int_t *isAbove=nullptr) const; // get bin number, up to 32 dimensional binning
146 /// first bin of this node
147 inline Int_t GetStartBin(void) const { return fFirstBin; }
148 /// last+1 bin of this node (includes children)
149 inline Int_t GetEndBin(void) const { return fLastBin; }
150 virtual Bool_t IsBinFactorGlobal(void) const; // check whether global or local bin factor must be used
151 Double_t GetGlobalFactor(void) const; // return global factor
152
153 /********************* access by global bin number ******/
154 TString GetBinName(Int_t iBin) const; // return bin name
155 Double_t GetBinSize(Int_t iBin) const; // return bin size (in N dimensions)
156 virtual Double_t GetBinFactor(Int_t iBin) const; // return user factor
157 void GetBinUnderflowOverflowStatus(Int_t iBin,Int_t *uStatus,Int_t *oStatus) const; // return bit map indicating underflow and overflow status
158 Int_t GetBinNeighbours(Int_t globalBin,Int_t axis,
159 Int_t *prev,Double_t *distPrev,
160 Int_t *next,Double_t *distNext,
161 Bool_t isPeriodic=kFALSE) const; // get neighbour bins along an axis
162 /********************** access distribution properties *************/
163 /// number of bins in the distribution possibly including under/overflow
165 /// query dimension of this node's distribution
166 inline Int_t GetDistributionDimension(void) const { return fAxisList->GetEntriesFast(); }
167 virtual Double_t GetDistributionAverageBinSize(Int_t axis,Bool_t includeUnderflow, Bool_t includeOverflow) const; // get average bin size
168 /// get vector of bin borders for one axis
169 inline TVectorD const *GetDistributionBinning(Int_t axis) const {
170 return (TVectorD const *)fAxisList->At(axis); }
171 /// get name of an axis
173 return ((TObjString const *)fAxisLabelList->At(axis))->GetString(); }
174
175 virtual Double_t GetDistributionUnderflowBinWidth(Int_t axis) const; // width of underflow bin on the given axis
176 virtual Double_t GetDistributionOverflowBinWidth(Int_t axis) const; // width of overflow bin on the given axis
177 virtual Double_t GetDistributionBinCenter(Int_t axis,Int_t bin) const; // position of bin center on the given axis
178 Bool_t HasUnconnectedBins(void) const; // check whether this node has bins without axis
179 const TObjString *GetUnconnectedBinName(Int_t bin) const; // return bin name (if any)
180 /// check whether an axis has an underflow bin
181 Bool_t HasUnderflow(int axis) const { return fHasUnderflow & (1<<axis); }
182 /// check whether the axis has an overflow bin
183 Bool_t HasOverflow(int axis) const { return fHasOverflow & (1<<axis); }
184 void DecodeAxisSteering(const char *axisSteering,const char *options,
185 Int_t *isOptionGiven) const; // decode axis steering options
186 protected:
187 /// return root node
189 void Initialize(Int_t nBins);
190 Int_t UpdateFirstLastBin(Bool_t startWithRootNode=kTRUE); // update fFirstBin and fLastBin
191 TUnfoldBinning const *ToAxisBins(Int_t globalBin,Int_t *axisBins) const; // return distribution in which the bin is located
192 Int_t ToGlobalBin(Int_t const *axisBins,Int_t *isBelow=nullptr,Int_t *isAbove=nullptr) const; // return -1 if not inside distribution
193 TString BuildHistogramTitle(const char *histogramName,const char *histogramTitle,
194 Int_t const *axisList) const; // construct histogram title
195 TString BuildHistogramTitle2D(const char *histogramName,const char *histogramTitle,
196 Int_t xAxis,const TUnfoldBinning *yAxisBinning,Int_t yAxis) const; // construct histogram title
197 Int_t GetTHxxBinning(Int_t maxDim,Int_t *axisBins,Int_t *axisList,const char *axisSteering) const; // get binning information for creating a THxx
198 Int_t GetTHxxBinningSingleNode(Int_t maxDim,Int_t *axisBins,Int_t *axisList,const char *axisSteering) const; // get binning information for creating a THxx
199 Int_t GetTHxxBinsRecursive(const char *axisSteering) const; // get binning information for creating a THxx
200 const TUnfoldBinning *GetNonemptyNode(void) const; // get the single node with non-empty distribution, if there are multiple nodes, return 0
201 const TUnfoldBinning *GetNonemptyNode_r(int &count) const; // get a node with non-empty distributions, count how many exit
202 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
203 Int_t FillBinMapRecursive(Int_t startBin,const char *axisSteering,
204 Int_t *binMap) const; // fill bin map recursively
205 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
206 void SetBinFactor(Double_t normalisation,TObject *factors); // define function to calculate bin factor. Note: the object is owned by this class, unless it is a function
207
208 ClassDefOverride(TUnfoldBinning, TUnfold_CLASS_VERSION) //Complex binning schemes for TUnfoldDensity
209};
210
211#endif
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
static void indent(ostringstream &buf, int indent_level)
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
char name[80]
Definition TGX11.cxx:110
#define TUnfold_CLASS_VERSION
Definition TUnfold.h:105
Class to manage histogram axis.
Definition TAxis.h:31
1-Dim function class
Definition TF1.h:233
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:358
Service class for 2-D histogram classes.
Definition TH2.h:30
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntriesFast() const
Definition TObjArray.h:58
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
Binning schemes for use with the unfolding algorithm TUnfoldDensity.
void PrintStream(std::ostream &out, Int_t indent=0, int debug=0) const
print some information about this binning tree
Int_t ToGlobalBin(Int_t const *axisBins, Int_t *isBelow=nullptr, Int_t *isAbove=nullptr) const
get global bin number, given axis bin numbers
Bool_t HasOverflow(int axis) const
check whether the axis has an overflow bin
~TUnfoldBinning(void) override
TH1 * CreateHistogram(const char *histogramName, Bool_t originalAxisBinning=kFALSE, Int_t **binMap=nullptr, const char *histogramTitle=nullptr, const char *axisSteering=nullptr) const
create a THxx histogram capable to hold the bins of this binning node and its children
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
virtual Double_t GetDistributionOverflowBinWidth(Int_t axis) const
return bin width assigned to the overflow bin
Int_t fFirstBin
global bin number of the first bin
void SetBinFactorFunction(Double_t normalisation, TF1 *userFunc=nullptr)
set normalisation factor and function which are used in calls to GetBinFactor()
TH1 * ExtractHistogram(const char *histogramName, const TH1 *globalBins, const TH2 *globalBinsEmatrix=nullptr, Bool_t originalAxisBinning=kTRUE, const char *axisSteering=nullptr) const
Int_t UpdateFirstLastBin(Bool_t startWithRootNode=kTRUE)
update fFirstBin and fLastBin members of this node and its children
TString BuildHistogramTitle(const char *histogramName, const char *histogramTitle, Int_t const *axisList) const
construct a title
Int_t FillBinMapRecursive(Int_t startBin, const char *axisSteering, Int_t *binMap) const
recursively fill bin map
Int_t FillBinMap1D(Int_t *binMap, const char *axisSteering, Int_t firstBinX) const
map all global bins referenced by this node to the one-dimensional histogram destHist,...
TUnfoldBinning * parentNode
mother node
Int_t GetDistributionDimension(void) const
query dimension of this node's distribution
TUnfoldBinning const * GetPrevNode(void) const
previous sister node
void SetBinFactor(Double_t normalisation, TObject *factors)
set normalisation factors which are used in calls to GetBinFactor()
TUnfoldBinning * childNode
first daughter node
TString GetBinName(Int_t iBin) const
get the name of a bin
Int_t GetTH1xNumberOfBins(Bool_t originalAxisBinning=kTRUE, const char *axisSteering=nullptr) const
return the number of histogram bins required when storing this binning in a one-dimensional histogram
virtual Double_t GetDistributionUnderflowBinWidth(Int_t axis) const
return bin width assigned to the underflow bin
Double_t GetBinSize(Int_t iBin) const
get N-dimensional bin size
Int_t GetDistributionNumberOfBins(void) const
number of bins in the distribution possibly including under/overflow
Double_t GetGlobalFactor(void) const
return global scaling factor for this node
Int_t GetGlobalBinNumber(Double_t x) const
locate a bin in a one-dimensional distribution
Bool_t HasUnconnectedBins(void) const
check whether there are bins but no axis
virtual Double_t GetBinFactor(Int_t iBin) const
return scaling factor for the given global bin number
TUnfoldBinning * nextNode
next sister
TObject * fBinFactorFunction
function to calculate a scale factor from bin centres (may be a TF1 or a TVectorD
Int_t GetTHxxBinning(Int_t maxDim, Int_t *axisBins, Int_t *axisList, const char *axisSteering) const
calculate properties of a THxx histogram to store this binning
const TUnfoldBinning * GetNonemptyNode_r(int &count) const
Int_t fHasUnderflow
bit fields indicating whether there are underflow bins on the axes
Int_t GetEndBin(void) const
last+1 bin of this node (includes children)
TObjArray * fAxisLabelList
for each axis its name (TObjString), or names of unconnected bins
Double_t fBinFactorConstant
common scale factor for all bins of this node
TUnfoldBinning const * GetNextNode(void) const
next sister node
Bool_t HasUnderflow(int axis) const
check whether an axis has an underflow bin
Int_t * CreateEmptyBinMap(void) const
create an empty bin map, useful together with the getter methods of class TUnfold and TUnfoldSys
TUnfoldBinning const * GetParentNode(void) const
mother node
@ MAXDIM
maximum numner of axes per distribution
static TH2D * CreateHistogramOfMigrations(TUnfoldBinning const *xAxis, TUnfoldBinning const *yAxis, char const *histogramName, Bool_t originalXAxisBinning=kFALSE, Bool_t originalYAxisBinning=kFALSE, char const *histogramTitle=nullptr)
create a TH2D histogram capable to hold the bins of the two input binning schemes on the x and y axes...
TObjArray * fAxisList
for each axis the bin borders (TVectorD)
Int_t fLastBin
global bin number of the last(+1) bin, including daughters
void GetBinUnderflowOverflowStatus(Int_t iBin, Int_t *uStatus, Int_t *oStatus) const
return bit maps indicating underflow and overflow status
virtual Double_t GetDistributionBinCenter(Int_t axis, Int_t bin) const
return bin center for a given axis and bin number
Int_t fDistributionSize
number of bins in this node's distribution
Int_t fHasOverflow
bit fields indicating whether there are overflow bins on the axes
const TUnfoldBinning * GetNonemptyNode(void) const
find a node which has non-empty distributions if there is none or if there are many,...
void Initialize(Int_t nBins)
initialize variables for a given number of bins
void SetBinMapEntry(Int_t *binMap, Int_t globalBin, Int_t destBin) const
set one entry in a bin map
virtual Double_t GetDistributionAverageBinSize(Int_t axis, Bool_t includeUnderflow, Bool_t includeOverflow) const
get average bin size on the specified axis
TUnfoldBinning * GetNextNode(void)
TUnfoldBinning * GetChildNode(void)
TUnfoldBinning * GetParentNode(void)
void DecodeAxisSteering(const char *axisSteering, const char *options, Int_t *isOptionGiven) const
decode axis steering
virtual Bool_t IsBinFactorGlobal(void) const
check whether there is only a global scaling factor for this node
TString GetDistributionAxisLabel(Int_t axis) const
get name of an axis
TUnfoldBinning * AddBinning(TUnfoldBinning *binning)
add a TUnfoldBinning as the last child of this node
const TObjString * GetUnconnectedBinName(Int_t bin) const
return the bin names of unconnected bins
Int_t GetBinNeighbours(Int_t globalBin, Int_t axis, Int_t *prev, Double_t *distPrev, Int_t *next, Double_t *distNext, Bool_t isPeriodic=kFALSE) const
get neighbour bins along the specified axis
TVectorD const * GetDistributionBinning(Int_t axis) const
get vector of bin borders for one axis
Int_t * CreateBinMap(const TH1 *hist, Int_t nDim, const Int_t *axisList, const char *axisSteering) const
create mapping from global bin number to a histogram for this node
TH2D * CreateErrorMatrixHistogram(const char *histogramName, Bool_t originalAxisBinning, Int_t **binMap=nullptr, const char *histogramTitle=nullptr, const char *axisSteering=nullptr) const
create a TH2D histogram capable to hold a covariance matrix
Bool_t AddAxis(const char *name, Int_t nBins, const Double_t *binBorders, Bool_t hasUnderflow, Bool_t hasOverflow)
add an axis with the specified bin borders
TUnfoldBinning * prevNode
previous sister
TUnfoldBinning const * GetRootNode(void) const
return root node of the binnig scheme
Int_t GetStartBin(void) const
first bin of this node
TUnfoldBinning const * ToAxisBins(Int_t globalBin, Int_t *axisBins) const
return distribution in which the bin is located and bin numbers on the corresponding axes
TUnfoldBinning const * FindNode(char const *name) const
traverse the tree and return the first node which matches the given name
Int_t GetTHxxBinsRecursive(const char *axisSteering) const
calculate number of bins required to store this binning with teh given axisSteering
TUnfoldBinning const * GetChildNode(void) const
first daughter node
Int_t GetTHxxBinningSingleNode(Int_t maxDim, Int_t *axisBins, Int_t *axisList, const char *axisSteering) const
get the properties of a histogram capable to hold the distribution attached to this node
TString BuildHistogramTitle2D(const char *histogramName, const char *histogramTitle, Int_t xAxis, const TUnfoldBinning *yAxisBinning, Int_t yAxis) const
construct a histogram title for a 2D histogram with different binning schemes on x and y axis
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17