Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
testUnfold2.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_unfold
3/// \notebook
4/// Test program as an example for a user specific regularisation scheme.
5///
6/// 1. Generate Monte Carlo and Data events
7/// The events consist of:
8/// - signal
9/// - background
10///
11/// The signal is a resonance. It is generated with a Breit-Wigner,
12/// smeared by a Gaussian
13///
14/// 2. Unfold the data. The result is:
15/// - The background level
16/// - The shape of the resonance, corrected for detector effects
17///
18/// The regularisation is done on the curvature, excluding the bins
19/// near the peak.
20///
21/// 3. produce some plots
22///
23/// \macro_output
24/// \macro_code
25///
26/// **Version 17.6, in parallel to changes in TUnfold**
27///
28/// #### History:
29/// - Version 17.5, in parallel to changes in TUnfold
30/// - Version 17.4, in parallel to changes in TUnfold
31/// - Version 17.3, in parallel to changes in TUnfold
32/// - Version 17.2, in parallel to changes in TUnfold
33/// - Version 17.1, in parallel to changes in TUnfold
34/// - Version 17.0, updated for changed methods in TUnfold
35/// - Version 16.1, parallel to changes in TUnfold
36/// - Version 16.0, parallel to changes in TUnfold
37/// - Version 15, with automatic L-curve scan, simplified example
38/// - Version 14, with changes in TUnfoldSys.cxx
39/// - Version 13, with changes to TUnfold.C
40/// - Version 12, with improvements to TUnfold.cxx
41/// - Version 11, print chi**2 and number of degrees of freedom
42/// - Version 10, with bug-fix in TUnfold.cxx
43/// - Version 9, with bug-fix in TUnfold.cxx, TUnfold.h
44/// - Version 8, with bug-fix in TUnfold.cxx, TUnfold.h
45/// - Version 7, with bug-fix in TUnfold.cxx, TUnfold.h
46/// - Version 6a, fix problem with dynamic array allocation under windows
47/// - Version 6, re-include class MyUnfold in the example
48/// - Version 5, move class MyUnfold to separate files
49/// - Version 4, with bug-fix in TUnfold.C
50/// - Version 3, with bug-fix in TUnfold.C
51/// - Version 2, with changed ScanLcurve() arguments
52/// - Version 1, remove L curve analysis, use ScanLcurve() method instead
53/// - Version 0, L curve analysis included here
54///
55/// This file is part of TUnfold.
56///
57/// TUnfold is free software: you can redistribute it and/or modify
58/// it under the terms of the GNU General Public License as published by
59/// the Free Software Foundation, either version 3 of the License, or
60/// (at your option) any later version.
61///
62/// TUnfold is distributed in the hope that it will be useful,
63/// but WITHOUT ANY WARRANTY; without even the implied warranty of
64/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65/// GNU General Public License for more details.
66///
67/// You should have received a copy of the GNU General Public License
68/// along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
69///
70/// \author Stefan Schmitt DESY, 14.10.2008
71
72#include <TMath.h>
73#include <TCanvas.h>
74#include <TRandom3.h>
75#include <TF1.h>
76#include <TStyle.h>
77#include <TVector.h>
78#include <TGraph.h>
79#include "TUnfold.h"
80
81#include <iostream>
82
83
84TRandom *rnd=nullptr;
85
86// generate an event
87// output:
88// negative mass: background event
89// positive mass: signal event
90Double_t GenerateEvent(Double_t bgr, // relative fraction of background
91 Double_t mass, // peak position
92 Double_t gamma) // peak width
93{
94 Double_t t;
95 if(rnd->Rndm()>bgr) {
96 // generate signal event
97 // with positive mass
98 do {
99 do {
100 t=rnd->Rndm();
101 } while(t>=1.0);
102 t=TMath::Tan((t-0.5)*TMath::Pi())*gamma+mass;
103 } while(t<=0.0);
104 return t;
105 } else {
106 // generate background event
107 // generate events following a power-law distribution
108 // f(E) = K * TMath::power((E0+E),N0)
109 static Double_t const E0=2.4;
110 static Double_t const N0=2.9;
111 do {
112 do {
113 t=rnd->Rndm();
114 } while(t>=1.0);
115 // the mass is returned negative
116 // In our example a convenient way to indicate it is a background event.
117 t= -(TMath::Power(1.-t,1./(1.-N0))-1.0)*E0;
118 } while(t>=0.0);
119 return t;
120 }
121}
122
123// smear the event to detector level
124// input:
125// mass on generator level (mTrue>0 !)
126// output:
127// mass on detector level
129 // smear by double-gaussian
130 static Double_t frac=0.1;
131 static Double_t wideBias=0.03;
132 static Double_t wideSigma=0.5;
133 static Double_t smallBias=0.0;
134 static Double_t smallSigma=0.1;
135 if(rnd->Rndm()>frac) {
136 return rnd->Gaus(mTrue+smallBias,smallSigma);
137 } else {
138 return rnd->Gaus(mTrue+wideBias,wideSigma);
139 }
140}
141
142int testUnfold2()
143{
144 // switch on histogram errors
146
147 // random generator
148 rnd=new TRandom3();
149
150 // data and MC luminosity, cross-section
151 Double_t const luminosityData=100000;
152 Double_t const luminosityMC=1000000;
153 Double_t const crossSection=1.0;
154
155 Int_t const nDet=250;
156 Int_t const nGen=100;
157 Double_t const xminDet=0.0;
158 Double_t const xmaxDet=10.0;
159 Double_t const xminGen=0.0;
160 Double_t const xmaxGen=10.0;
161
162 //============================================
163 // generate MC distribution
164 //
165 TH1D *histMgenMC=new TH1D("MgenMC",";mass(gen)",nGen,xminGen,xmaxGen);
166 TH1D *histMdetMC=new TH1D("MdetMC",";mass(det)",nDet,xminDet,xmaxDet);
167 TH2D *histMdetGenMC=new TH2D("MdetgenMC",";mass(det);mass(gen)",nDet,xminDet,xmaxDet,
170 for(Int_t i=0;i<neventMC;i++) {
171 Double_t mGen=GenerateEvent(0.3, // relative fraction of background
172 4.0, // peak position in MC
173 0.2); // peak width in MC
175 // the generated mass is negative for background
176 // and positive for signal
177 // so it will be filled in the underflow bin
178 // this is very convenient for the unfolding:
179 // the unfolded result will contain the number of background
180 // events in the underflow bin
181
182 // generated MC distribution (for comparison only)
184 // reconstructed MC distribution (for comparison only)
186
187 // matrix describing how the generator input migrates to the
188 // reconstructed level. Unfolding input.
189 // NOTE on underflow/overflow bins:
190 // (1) the detector level under/overflow bins are used for
191 // normalisation ("efficiency" correction)
192 // in our toy example, these bins are populated from tails
193 // of the initial MC distribution.
194 // (2) the generator level underflow/overflow bins are
195 // unfolded. In this example:
196 // underflow bin: background events reconstructed in the detector
197 // overflow bin: signal events generated at masses > xmaxDet
198 // for the unfolded result these bins will be filled
199 // -> the background normalisation will be contained in the underflow bin
201 }
202
203 //============================================
204 // generate data distribution
205 //
206 TH1D *histMgenData=new TH1D("MgenData",";mass(gen)",nGen,xminGen,xmaxGen);
207 TH1D *histMdetData=new TH1D("MdetData",";mass(det)",nDet,xminDet,xmaxDet);
209 for(Int_t i=0;i<neventData;i++) {
210 Double_t mGen=GenerateEvent(0.4, // relative fraction of background
211 3.8, // peak position
212 0.15); // peak width
214 // generated data mass for comparison plots
215 // for real data, we do not have this histogram
216 histMgenData->Fill(mGen);
217
218 // reconstructed mass, unfolding input
219 histMdetData->Fill(mDet);
220 }
221
222 //=========================================================================
223 // set up the unfolding
226 // regularisation
227 //----------------
228 // the regularisation is done on the curvature (2nd derivative) of
229 // the output distribution
230 //
231 // One has to exclude the bins near the peak of the Breit-Wigner,
232 // because there the curvature is high
233 // (and the regularisation eventually could enforce a small
234 // curvature, thus biasing result)
235 //
236 // in real life, the parameters below would have to be optimized,
237 // depending on the data peak position and width
238 // Or maybe one finds a different regularisation scheme... this is
239 // just an example...
241 Int_t nPeek=3;
243 // calculate bin number corresponding to estimated peak position
245 // offset 1.5
246 // accounts for start bin 1
247 // and rounding errors +0.5
248 +1.5);
249 // regularize output bins 1..iPeek-nPeek
250 unfold.RegularizeBins(1,1,iPeek-nPeek,regMode);
251 // regularize output bins iPeek+nPeek..nGen
252 unfold.RegularizeBins(iPeek+nPeek,1,nGen-(iPeek+nPeek),regMode);
253
254 // unfolding
255 //-----------
256
257 // set input distribution and bias scale (=0)
258 if(unfold.SetInput(histMdetData,0.0)>=10000) {
259 std::cout<<"Unfolding result may be wrong\n";
260 }
261
262 // do the unfolding here
263 Double_t tauMin=0.0;
264 Double_t tauMax=0.0;
265 Int_t nScan=30;
266 Int_t iBest;
268 TGraph *lCurve;
269 // this method scans the parameter tau and finds the kink in the L curve
270 // finally, the unfolding is done for the "best" choice of tau
272 std::cout<<"tau="<<unfold.GetTau()<<"\n";
273 std::cout<<"chi**2="<<unfold.GetChi2A()<<"+"<<unfold.GetChi2L()
274 <<" / "<<unfold.GetNdf()<<"\n";
275
276 // save point corresponding to the kink in the L curve as TGraph
277 Double_t t[1],x[1],y[1];
278 logTauX->GetKnot(iBest,t[0],x[0]);
279 logTauY->GetKnot(iBest,t[0],y[0]);
280 TGraph *bestLcurve=new TGraph(1,x,y);
281 TGraph *bestLogTauX=new TGraph(1,t,x);
282
283 //============================================================
284 // extract unfolding results into histograms
285
286 // set up a bin map, excluding underflow and overflow bins
287 // the binMap relates the output of the unfolding to the final
288 // histogram bins
289 Int_t *binMap=new Int_t[nGen+2];
290 for(Int_t i=1;i<=nGen;i++) binMap[i]=i;
291 binMap[0]=-1;
292 binMap[nGen+1]=-1;
293
294 TH1D *histMunfold=new TH1D("Unfolded",";mass(gen)",nGen,xminGen,xmaxGen);
295 unfold.GetOutput(histMunfold,binMap);
296 TH1D *histMdetFold=new TH1D("FoldedBack","mass(det)",nDet,xminDet,xmaxDet);
297 unfold.GetFoldedOutput(histMdetFold);
298
299 // store global correlation coefficients
300 TH1D *histRhoi=new TH1D("rho_I","mass",nGen,xminGen,xmaxGen);
301 unfold.GetRhoI(histRhoi,binMap);
302
303 delete[] binMap;
304 binMap=nullptr;
305
306 //=====================================================================
307 // plot some histograms
308 TCanvas output;
309
310 // produce some plots
311 output.Divide(3,2);
312
313 // Show the matrix which connects input and output
314 // There are overflow bins at the bottom, not shown in the plot
315 // These contain the background shape.
316 // The overflow bins to the left and right contain
317 // events which are not reconstructed. These are necessary for proper MC
318 // normalisation
319 output.cd(1);
320 histMdetGenMC->Draw("BOX");
321
322 // draw generator-level distribution:
323 // data (red) [for real data this is not available]
324 // MC input (black) [with completely wrong peak position and shape]
325 // unfolded data (blue)
326 output.cd(2);
327 histMunfold->SetLineColor(kBlue);
328 histMunfold->Draw();
329 histMgenData->SetLineColor(kRed);
330 histMgenData->Draw("SAME");
331 histMgenMC->Draw("SAME HIST");
332
333 // show detector level distributions
334 // data (red)
335 // MC (black)
336 // unfolded data (blue)
337 output.cd(3);
338 histMdetFold->SetLineColor(kBlue);
339 histMdetFold->Draw();
340 histMdetData->SetLineColor(kRed);
341 histMdetData->Draw("SAME");
342 histMdetMC->Draw("SAME HIST");
343
344 // show correlation coefficients
345 // all bins outside the peak are found to be highly correlated
346 // But they are compatible with zero anyway
347 // If the peak shape is fitted,
348 // these correlations have to be taken into account, see example
349 output.cd(4);
350 histRhoi->Draw();
351
352 // show rhoi_max(tau) distribution
353 output.cd(5);
354 logTauX->Draw();
355 bestLogTauX->SetMarkerColor(kRed);
356 bestLogTauX->Draw("*");
357
358 output.cd(6);
359 lCurve->Draw("AL");
360 bestLcurve->SetMarkerColor(kRed);
361 bestLcurve->Draw("*");
362
363 output.SaveAs("testUnfold2.ps");
364 return 0;
365}
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:728
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
static void SetDefaultSumw2(Bool_t sumw2=kTRUE)
When this static function is called with sumw2=kTRUE, all new histograms will automatically activate ...
Definition TH1.cxx:6914
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:400
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1294
void SaveAs(const char *filename="", Option_t *option="") const override
Save the pad content in a file.
Definition TPad.cxx:5806
Random number generator class based on M.
Definition TRandom3.h:27
This is the base class for the ROOT Random number generators.
Definition TRandom.h:28
Base class for spline implementation containing the Draw/Paint methods.
Definition TSpline.h:31
An algorithm to unfold distributions from detector to truth level.
Definition TUnfold.h:107
ERegMode
choice of regularisation scheme
Definition TUnfold.h:123
@ kRegModeNone
no regularisation, or defined later by RegularizeXXX() methods
Definition TUnfold.h:126
@ kRegModeCurvature
regularize the 2nd derivative of the output distribution
Definition TUnfold.h:135
@ kHistMapOutputVert
truth level on y-axis of the response matrix
Definition TUnfold.h:149
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
double gamma(double x)
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:734
constexpr Double_t Pi()
Definition TMath.h:40
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:613
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122