source: trunk/MagicSoft/Mars/macros/unfoldCoeff.C@ 6662

Last change on this file since 6662 was 6549, checked in by rico, 20 years ago
*** empty log message ***
File size: 6.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Javier Rico, 12/2005 <mailto:jrico@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25//
26// Example macro on how to calculate coefficients for unfolding and
27// effective areas.
28//.The input is a file of the MC events surviving a certain kind
29// of analysis. It must contain the branch MEnergyEst with energy
30// estimator.
31// It must also contain a tree called "OriginalMC" with a branch
32// "MMcEvtBasic" and one entry per original simulated shower used to produce
33// the events in that input file.
34//
35// The macro performs 2 loops.
36// In the first one the histograms of the original MC events are filled,
37// needed by the coefficient (MMcUnfoldCoeffCalc) and effective area
38// (MMcCollectionAreaCalc) computation
39// In the second one we deal with the survivors and compute effective areas
40// and coefficients
41
42void unfoldCoeff(TString filename="/data/19990101_10002_Q_MCGamTestLZA_E_10_5.root", TString outname="coeff.root")
43{
44
45 ///////////////////////////////////////////////////////////////////
46 // First loop: over all events processed by corsika
47 ///////////////////////////////////////////////////////////////////
48 MParList parlist;
49 MTaskList tasklist;
50 parlist.AddToList(&tasklist);
51
52 // theta binning
53 Int_t zbins = 2;
54 TArrayD edges(zbins+1);
55 edges[0] = 0;
56 edges[1] = 10;
57 edges[2] = 20;
58
59 MBinning binstheta("binsTheta");
60 binstheta.SetEdges(edges); // Theta [deg]
61
62 // energy binning
63 const Int_t ebins=10;
64 const Float_t emin=10;
65 const Float_t emax=1000;
66
67 MBinning binsenergy("binsEnergy");
68 binsenergy.SetEdgesLog(ebins,emin,emax); // Energy [GeV]
69
70 parlist.AddToList(&binsenergy);
71 parlist.AddToList(&binstheta);
72
73 // Tentative spectrum
74 TF1 spectrum("func","pow(x,-1.6)",2.,20000.);
75
76 // Setup out tasks:
77 MReadMarsFile reader("OriginalMC", filename);
78 reader.DisableAutoScheme();
79
80 // Create unfolding coefficients object and add it to the parameter list
81 MHMcCollectionArea aeff;
82 MHMcUnfoldCoeff coeff;
83 parlist.AddToList(&coeff);
84 parlist.AddToList(&aeff);
85
86 // Task which fills the necessary histograms in MHMcCollectionArea
87 MMcUnfoldCoeffCalc coeffcalc;
88 MMcCollectionAreaCalc aeffcalc;
89
90 coeffcalc.SetSpectrum(&spectrum);
91 aeffcalc.SetSpectrum(&spectrum);
92
93 tasklist.AddToList(&reader);
94 tasklist.AddToList(&coeffcalc);
95 tasklist.AddToList(&aeffcalc);
96
97 // set up the loop for the processing
98 MEvtLoop magic;
99 magic.SetParList(&parlist);
100
101 if (!magic.Eventloop())
102 return;
103
104 tasklist.PrintStatistics();
105
106 ///////////////////////////////////////////////////////////////////
107 // Second loop: now fill the histograms of the survivors
108 ///////////////////////////////////////////////////////////////////
109
110 MParList parlist2;
111 MTaskList tasklist2;
112 parlist2.AddToList(&tasklist2);
113 parlist2.AddToList(&coeff);
114 parlist2.AddToList(&aeff);
115
116 MReadMarsFile reader2("Events", filename);
117 reader2.DisableAutoScheme();
118
119 tasklist2.AddToList(&reader2);
120 tasklist2.AddToList(&aeffcalc);
121 tasklist2.AddToList(&coeffcalc);
122
123 //
124 // set up the loop for the processing
125 //
126 MEvtLoop magic2;
127 magic2.SetParList(&parlist2);
128
129 if (!magic2.Eventloop())
130 return;
131
132 tasklist2.PrintStatistics();
133
134
135
136 // Dummy cross-check, see if we recover the original spectrum
137 const UInt_t ncutfiles=1;
138 Char_t* cutName[ncutfiles]={"/data/19990101_10002_Q_MCGamTestLZA_E_10_5.root"};
139
140 TChain* ccut = new TChain("Events");
141 for(Int_t i = 0; i < ncutfiles; i++)
142 ccut->Add(cutName[i]);
143 // ccut->SetAlias("logestenergy","log10(MHillas.fSize/0.18/15.)");
144 ccut->SetAlias("logestenergy","log10(MEnergyEst.fEnergy)");
145 ccut->SetAlias("theta","MMcEvt.fTelescopeTheta*180./3.14159");
146
147 const Double_t logemin = TMath::Log10(emin);
148 const Double_t logemax = TMath::Log10(emax);
149 TH1D* hspec = new TH1D("hspec","Spectrum",ebins,logemin,logemax);
150 hspec->Sumw2();
151 ccut->Draw("logestenergy>>hspec","theta<10","goff");
152
153 for(Int_t i=0;i<ebins;i++)
154 {
155 const Float_t uncorrval = hspec->GetBinContent(i+1);
156 const Float_t effa = ((TH2D*) aeff.GetHistCoarse())->GetBinContent(i+1,1);
157 const Float_t unfold = ((TH2D*)coeff.GetHistCoeff())->GetBinContent(i+1,1);
158
159 const Float_t euncorrval = hspec->GetBinError(i+1);
160 const Float_t eeffa = ((TH2D*) aeff.GetHistCoarse())->GetBinError(i+1,1);
161 const Float_t eunfold = ((TH2D*)coeff.GetHistCoeff())->GetBinError(i+1,1);
162
163 Float_t corrval,ecorrval;
164 if(effa>0)
165 {
166 corrval = uncorrval*unfold/effa;
167 if(uncorrval>0 && effa>0 & unfold>0)
168 ecorrval = corrval*TMath::Sqrt(euncorrval/uncorrval*euncorrval/uncorrval+
169 eeffa/effa*eeffa/effa+
170 eunfold/unfold*eunfold/unfold);
171 else
172 ecorrval = 0;
173 }
174 else
175 {
176 corrval = 0;
177 ecorrval = 0;
178 }
179 hspec->SetBinContent(i+1,corrval);
180 hspec->SetBinError(i+1,ecorrval);
181 }
182
183 // Write histogram to a file in case an output
184 // filename has been supplie
185 if (outname.IsNull())
186 return;
187
188 TFile f(outname, "recreate");
189 if (!f)
190 return;
191
192 coeff.GetHistAll()->Write();
193 coeff.GetHistWeight()->Write();
194 coeff.GetHistMcE()->Write();
195 coeff.GetHistEstE()->Write();
196 coeff.GetHistCoeff()->Write();
197 aeff.GetHistCoarse()->Write();
198 aeff.Write();
199 hspec->Write();
200}
Note: See TracBrowser for help on using the repository browser.