Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUnfoldIterativeEM.cxx
Go to the documentation of this file.
2#include "TUnfoldBinning.h"
3#include <TVectorD.h>
4
6
8 f_inputBins=nullptr;
9 f_outputBins=nullptr;
10 f_constInputBins=nullptr;
11 f_constOutputBins=nullptr;
12 fA=nullptr;
13 fEpsilon=nullptr;
14 fX0=nullptr;
15 fY=nullptr;
16 fBgr=nullptr;
17 fX=nullptr;
18 fDXDY=nullptr;
19}
20
22(const TH2 *hist_A, TUnfold::EHistMap histmap,
23 const TUnfoldBinning *outputBins,const TUnfoldBinning *inputBins) {
24 // copied from TUnfoldDensity
25 TAxis const *genAxis,*detAxis;
26 if(histmap==TUnfold::kHistMapOutputHoriz) {
27 genAxis=hist_A->GetXaxis();
28 detAxis=hist_A->GetYaxis();
29 } else {
30 genAxis=hist_A->GetYaxis();
31 detAxis=hist_A->GetXaxis();
32 }
33 if(!inputBins) {
34 f_inputBins=new TUnfoldBinning(*detAxis,0,0);
36 } else {
37 f_inputBins=nullptr;
38 f_constInputBins=inputBins;
39 }
40 if(!outputBins) {
41 f_outputBins=new TUnfoldBinning(*genAxis,1,1);
43 } else {
44 f_outputBins=nullptr;
45 f_constOutputBins=outputBins;
46 }
47 int nGen=f_constOutputBins->GetEndBin();
48 int nRec=f_constInputBins->GetEndBin();
49 fA=new TMatrixD(nRec-1,nGen);
50 fEpsilon=new TVectorD(nGen);
51 fX0=new TVectorD(nGen);
52 for(int iGen=0;iGen<nGen;iGen++) {
53 double sum=0.;
54 for(int iRec=0;iRec<=nRec;iRec++) {
55 double c;
56 if(histmap==TUnfold::kHistMapOutputHoriz) {
57 c= hist_A->GetBinContent(iGen,iRec);
58 } else {
59 c= hist_A->GetBinContent(iRec,iGen);
60 }
61 if((iRec>0)&&(iRec<=fA->GetNrows())) {
62 (*fA)(iRec-1,iGen)=c;
63 }
64 sum +=c;
65 }
66 double epsilon=0.;
67 if(sum!=0.) {
68 for(int iRec=0;iRec<fA->GetNrows();iRec++) {
69 (*fA)(iRec,iGen) /=sum;
70 epsilon += (*fA)(iRec,iGen);
71 }
72 }
73 (*fEpsilon)(iGen)=epsilon;
74 (*fX0)(iGen)=sum;
75 }
76 fStep=-1;
77 fScaleBias=1.;
78 fY=new TVectorD(nRec-1);
79 fBgr=new TVectorD(nRec-1);
80 fX=new TVectorD(*fX0);
81 fDXDY=new TMatrixD(nGen,nRec-1);
82}
83
85 if(f_inputBins) delete f_inputBins;
86 if(f_outputBins) delete f_outputBins;
87 if(fA) delete fA;
88 if(fEpsilon) delete fEpsilon;
89 if(fX0) delete fX0;
90 if(fY) delete fY;
91 if(fBgr) delete fBgr;
92 if(fX) delete fX;
93 if(fDXDY) delete fDXDY;
94}
95
97 if(numIterations<fStep) {
98 Reset();
99 }
100 while(fStep<numIterations) {
101 IterateOnce();
102 }
103}
104
106 int nRec=f_constInputBins->GetEndBin();
107 for(int iRec=1;iRec<nRec;iRec++) {
108 (*fY)(iRec-1)=hist_y->GetBinContent(iRec);
109 }
110 // reset start value
111 fScaleBias=scaleBias;
112 Reset();
113 return 0;
114}
115
117 for(int iGen=0;iGen<fX->GetNrows();iGen++) {
118 (*fX)=fScaleBias*(*fX0);
119 }
120 for(int i=0;i<fDXDY->GetNrows();i++) {
121 for(int j=0;j<fDXDY->GetNcols();j++) {
122 (*fDXDY)(i,j)=0.;
123 }
124 }
125 fStep=-1;
126}
127
128void TUnfoldIterativeEM::SubtractBackground(const TH1 *hist_bgr,const char * /*name*/,Double_t scale) {
129 int nRec=f_constInputBins->GetEndBin();
130 for(int iRec=1;iRec<nRec;iRec++) {
131 (*fBgr)(iRec-1)+=hist_bgr->GetBinContent(iRec)*scale;
132 }
133}
134
136(Int_t nIter,const TH1 *hist_y, Double_t scaleBias) {
137 SetInput(hist_y,scaleBias);
138 DoUnfold(nIter);
139}
140
142 TVectorD Ax_plus_bgr=(*fA)*(*fX)+(*fBgr);
143 TMatrixD f(fY->GetNrows(),1);
144 TMatrixD dfdy(fY->GetNrows(),fY->GetNrows());
146 for(int i=0;i<f.GetNrows();i++) {
147 f(i,0)=(*fY)(i)/Ax_plus_bgr(i);
148 // dfdx(i,j)=-f(i,0)/Ax(i)*(*fA)(i,j);
149 dfdy(i,i)=1./Ax_plus_bgr(i);
150 for(int j=0;j<fY->GetNrows();j++) {
151 dfdy(i,j) -= f(i,0)/Ax_plus_bgr(i)*ADXDY(i,j);
152 }
153 }
154 //
156 TMatrixD At_dfdy(*fA,TMatrixD::kTransposeMult,dfdy);
157 for(int i=0;i<fX->GetNrows();i++) {
158 if((*fEpsilon)(i)<=0.) continue;
159 double factor=At_f(i,0)/(*fEpsilon)(i);
160 for(int j=0;j<fY->GetNrows();j++) {
161 (*fDXDY)(i,j) *= factor;
162 (*fDXDY)(i,j) += (*fX)(i)/(*fEpsilon)(i)*At_dfdy(i,j);
163 }
164 (*fX)(i) *= factor;
165 }
166 fStep++;
167}
168
170 TGraph **df_deviance) {
171 Reset();
172 double minSURE=GetSURE();
173 int stepSURE=fStep;
174 TVectorD X_SURE(*fX);
175 TMatrixD DXDY_SURE(*fDXDY);
176 std::vector<double> nIter,scanSURE,scanDeviance,scanDF;
177 nIter.push_back(fStep);
178 scanSURE.push_back(minSURE);
179 scanDeviance.push_back(GetDeviance());
180 scanDF.push_back(GetDF());
181 Info("TUnfoldIterativeEM::ScanSURE",
182 "step=%d SURE=%lf DF=%lf deviance=%lf",
183 fStep,*scanSURE.rbegin(),*scanDF.rbegin(),*scanDeviance.rbegin());
184 while(fStep<nIterMax) {
185 DoUnfold(fStep+1);
186 double SURE=GetSURE();
187 nIter.push_back(fStep);
188 scanSURE.push_back(SURE);
189 scanDeviance.push_back(GetDeviance());
190 scanDF.push_back(GetDF());
191 Info("TUnfoldIterativeEM::ScanSURE",
192 "step=%d SURE=%lf DF=%lf deviance=%lf",
193 fStep,*scanSURE.rbegin(),*scanDF.rbegin(),*scanDeviance.rbegin());
194 if(SURE<minSURE) {
195 minSURE=SURE;
196 X_SURE=*fX;
197 DXDY_SURE=*fDXDY;
198 stepSURE=fStep;
199 }
200 }
201 if(graphSURE) {
202 *graphSURE=new TGraph(nIter.size(),nIter.data(),scanSURE.data());
203 }
204 if(df_deviance) {
205 *df_deviance=new TGraph
206 (scanDeviance.size(),scanDF.data(),scanDeviance.data());
207 }
208
209 *fX=X_SURE;
210 *fDXDY=DXDY_SURE;
211 fStep=stepSURE;
212
213 return fStep;
214}
215
217(const char *histogramName,const char *histogramTitle,
218 const char *distributionName,const char *projectionMode,
219 Bool_t useAxisBinning) const {
220 TUnfoldBinning const *binning=f_constOutputBins->FindNode(distributionName);
221 Int_t *binMap=nullptr;
222 TH1 *r=binning->CreateHistogram
223 (histogramName,useAxisBinning,&binMap,histogramTitle,projectionMode);
224 if(r) {
225 for(Int_t i=0;i<binning->GetEndBin();i++) {
226 Int_t destBin=binMap[i];
227 if(destBin<0.) continue;
228 r->SetBinContent(destBin,r->GetBinContent(destBin)+(*fX)(i));
229 Double_t Vii=0.;
230 for(Int_t k=0;k<binning->GetEndBin();k++) {
231 if(binMap[k]!=destBin) continue;
232 for(int j=0;j<fDXDY->GetNcols();j++) {
233 // add up Poisson errors squared
234 Vii += (*fDXDY)(i,j)*(*fY)(j)*(*fDXDY)(k,j);
235 }
236 }
237 r->SetBinError(destBin,TMath::Sqrt(r->GetBinError(destBin)+Vii));
238 }
239 }
240 if(binMap) {
241 delete [] binMap;
242 }
243 return r;
244}
245
247(const char *histogramName,const char *histogramTitle,
248 const char *distributionName,const char *projectionMode,
249 Bool_t useAxisBinning,Bool_t addBgr) const {
250 TUnfoldBinning const *binning=f_constInputBins->FindNode(distributionName);
251 Int_t *binMap=nullptr;
252 TH1 *r=binning->CreateHistogram
253 (histogramName,useAxisBinning,&binMap,histogramTitle,projectionMode);
254 if(r) {
255 TVectorD folded((*fA)*(*fX));
256 if(addBgr) folded+= *fBgr;
257 TMatrixD dFoldedDY((*fA)*(*fDXDY));
258 for(Int_t i=1;i<binning->GetEndBin();i++) {
259 Int_t destBin=binMap[i];
260 if(destBin<0.) continue;
261 r->SetBinContent(destBin,r->GetBinContent(destBin)+folded(i-1));
262 Double_t Vii=0.;
263 for(Int_t k=1;k<binning->GetEndBin();k++) {
264 if(binMap[k]!=destBin) continue;
265 for(int j=0;j<dFoldedDY.GetNcols();j++) {
266 // add up Poisson errors squared
267 Vii += dFoldedDY(i-1,j)*(*fY)(j)*dFoldedDY(k-1,j);
268 }
269 }
270 r->SetBinError(destBin,TMath::Sqrt(r->GetBinError(destBin)+Vii));
271 }
272 }
273 if(binMap) {
274 delete [] binMap;
275 }
276 return r;
277}
278
279
281 // fold data with matrix
282 TVectorD Ax_plus_bgr=(*fA)*(*fX)+(*fBgr);
283 double r=0.;
284 for(int i=0;i<Ax_plus_bgr.GetNrows();i++) {
285 double n=(*fY)(i);
286 double mu=Ax_plus_bgr(i);
287 if(n>0.) {
288 r += 2.* (mu-n-n*TMath::Log(mu/n));
289 } else if(mu>0.) {
290 r += 2.*mu;
291 }
292 }
293 return r;
294}
295
297 double r=0.;
298 for(int i=0;i<fA->GetNrows();i++) {
299 for(int j=0;j<fA->GetNcols();j++) {
300 r += (*fA)(i,j)*(*fDXDY)(j,i);
301 }
302 }
303 return r;
304}
305
307 return GetDeviance()+2.*GetDF();
308}
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
TMatrixT< Double_t > TMatrixD
Definition TMatrixDfwd.h:23
TVectorT< Double_t > TVectorD
Definition TVectorDfwd.h:23
Class to manage histogram axis.
Definition TAxis.h:32
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
TAxis * GetXaxis()
Definition TH1.h:325
TAxis * GetYaxis()
Definition TH1.h:326
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5090
Service class for 2-D histogram classes.
Definition TH2.h:30
Double_t GetBinContent(Int_t binx, Int_t biny) const override
Definition TH2.h:94
Int_t GetNrows() const
Int_t GetNcols() const
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:979
Binning schemes for use with the unfolding algorithm TUnfoldDensity.
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 GetEndBin(void) const
last+1 bin of this node (includes children)
TUnfoldBinning const * FindNode(char const *name) const
traverse the tree and return the first node which matches the given name
const TUnfoldBinning * f_constInputBins
virtual void Reset(void)
Double_t GetDeviance(void) const
const TUnfoldBinning * f_constOutputBins
TUnfoldBinning * f_outputBins
Double_t GetSURE(void) const
virtual void DoUnfold(Int_t numIterations)
Double_t GetDF(void) const
TH1 * GetFoldedOutput(const char *histogramName, const char *histogramTitle=nullptr, const char *distributionName=nullptr, const char *projectionMode=nullptr, Bool_t useAxisBinning=kTRUE, Bool_t addBgr=kFALSE) const
void SubtractBackground(const TH1 *hist_bgr, const char *name, Double_t scale=1.0)
virtual void IterateOnce(void)
TH1 * GetOutput(const char *histogramName, const char *histogramTitle=nullptr, const char *distributionName=nullptr, const char *projectionMode=nullptr, Bool_t useAxisBinning=kTRUE) const
virtual Int_t SetInput(const TH1 *hist_y, Double_t scaleBias=1.0)
TUnfoldBinning * f_inputBins
virtual Int_t ScanSURE(Int_t nIterMax, TGraph **SURE=nullptr, TGraph **df_deviance=nullptr)
EHistMap
arrangement of axes for the response matrix (TH2 histogram)
Definition TUnfold.h:143
@ kHistMapOutputHoriz
truth level on x-axis of the response matrix
Definition TUnfold.h:146
Int_t GetNrows() const
Definition TVectorT.h:73
const Int_t n
Definition legend1.C:16
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:760
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345