source: trunk/MagicSoft/Mars/macros/starmcstereo.C@ 9620

Last change on this file since 9620 was 7188, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 12.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): Abelardo Moralejo 1/2004 <mailto:moralejo@pd.infn.it>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// STARMCSTEREO - STandard Analysis and Reconstruction (for MC stereo files)
28//
29// This macro is the standard converter to convert raw data from stereo
30// camera simulation into image parameters
31//
32/////////////////////////////////////////////////////////////////////////////
33
34void starmcstereo(Int_t ct1 = 1, Int_t ct2 = 2)
35{
36 // ------------- user change -----------------
37
38 TString* CalibrationFilename = 0;
39
40 // Calibration file: a file with no added noise. Comment out next line if you
41 // do not want to calibrate the data (means SIZE will be in ADC counts)
42
43 CalibrationFilename = new TString("nonoise/Gamma_20_0_7_200000to200009_XX_w0.root");
44
45 Char_t* AnalysisFilename = "Gamma_20_0_7_*_XX_w0.root"; // File to be analyzed
46 Char_t* OutFileTag = "gammas"; // Output file tag
47
48 // First open input files to check that the required telescopes
49 // are in the file, and get telescope coordinates.
50
51 TChain *rh = new TChain("RunHeaders");
52 rh->Add(AnalysisFilename);
53 MMcCorsikaRunHeader *corsrh = new MMcCorsikaRunHeader();
54 rh->SetBranchAddress("MMcCorsikaRunHeader.", &corsrh);
55 rh->GetEvent(0);
56 // We assume that all the read files will have the same telescopes inside,
57 // so we look only into the first runheader.
58 Int_t allcts = corsrh->GetNumCT();
59 if (ct1 > allcts || ct2 > allcts)
60 {
61 cout << endl << "Wrong CT id number, not contained in input file!" << endl;
62 return;
63 }
64 // Set telescope coordinates as read from first runheader:
65 Float_t ctx[2];
66 Float_t cty[2];
67 ctx[0] = ((*corsrh)[ct1-1])->GetCTx();
68 cty[0] = ((*corsrh)[ct1-1])->GetCTy();
69 ctx[1] = ((*corsrh)[ct2-1])->GetCTx();
70 cty[1] = ((*corsrh)[ct2-1])->GetCTy();
71
72 // Now find out number of pixels in each camera:
73 MMcConfigRunHeader* confrh1 = new MMcConfigRunHeader();
74 MMcConfigRunHeader* confrh2 = new MMcConfigRunHeader();
75 rh->SetBranchAddress("MMcConfigRunHeader;1.", &confrh1);
76 rh->SetBranchAddress("MMcConfigRunHeader;2.", &confrh2);
77 rh->GetEvent(0);
78 Int_t npix[2];
79 npix[0] = confrh1->GetNumPMTs();
80 npix[1] = confrh2->GetNumPMTs();
81
82 rh->Delete();
83
84
85 Int_t CT[2] = {ct1, ct2}; // Only 2-telescope analysis for the moment
86 Int_t NCTs = 2;
87
88
89 // ------------- user change -----------------
90
91 Float_t BinsHigh[2] = {0, 79};
92 Float_t BinsLow[2] = {0, 79}; // FADC slices (2GHz sampling)
93 Float_t CleanLev[2] = {7., 5.}; // Units: phes (absolute cleaning will be used later!)
94 // Tail cuts for the analysis loop. In the first (calibration) loop they will not
95 // be used; we run over a noiseless file and we want to accept all pixels with any
96 // number of phes.
97
98
99 MImgCleanStd** clean = new MImgCleanStd*[NCTs];
100
101 MImgCleanStd* clean[0] = new MImgCleanStd(1.,1.);
102 MImgCleanStd* clean[1] = new MImgCleanStd(1.,1.);
103 // Just dummy levels. Since the calibration file will be a noiseless file, RMS of
104 // pedestal will be 0, and all events will be accepted, regardless of the cleaning level.
105 // For some reason the above lines do not work if made on a loop! (???)
106 clean[0]->SetSerialNumber(CT[0]);
107 clean[1]->SetSerialNumber(CT[1]);
108
109 MExtractTimeAndChargeSpline* sigextract = new MExtractTimeAndChargeSpline[NCTs];
110 MMcCalibrationUpdate* mccalibupdate = new MMcCalibrationUpdate[NCTs];
111 MCalibrateData* calib = new MCalibrateData[NCTs];
112 MMcCalibrationCalc* mccalibcalc = new MMcCalibrationCalc[NCTs];
113
114 // -------------------------------------------
115 // Create a empty Parameter List and an empty Task List
116 // The tasklist is identified in the eventloop by its name
117 //
118 MParList plist;
119 MTaskList tlist;
120 plist.AddToList(&tlist);
121
122 MSrcPosCam src[NCTs];
123 MBadPixelsCam badpix[NCTs];
124
125 Float_t hi2lowratio = 10.0;
126
127 for (Int_t i = 0; i < NCTs; i++)
128 {
129 TString s = "MSrcPosCam;";
130 s += CT[i];
131 src[i].SetName(s);
132 src[i].SetReadyToSave();
133 plist.AddToList(&(src[i]));
134
135 TString b = "MBadPixelsCam;";
136 b += CT[i];
137 badpix[i].SetName(b);
138 badpix[i].InitSize(npix[i]);
139 badpix[i].SetReadyToSave();
140 plist.AddToList(&(badpix[i]));
141
142 sigextract[i].SetSerialNumber(CT[i]);
143 sigextract[i].SetRange(BinsHigh[0], BinsHigh[1], BinsLow[0], BinsLow[1]);
144 sigextract[i].SetRiseTimeHiGain(0.5);
145 sigextract[i].SetFallTimeHiGain(0.5);
146 sigextract[i].SetLoGainStretch(1.);
147
148 mccalibupdate[i].SetSerialNumber(CT[i]);
149 mccalibupdate[i].SetUserLow2HiGainFactor(hi2lowratio);
150 mccalibupdate[i].SetSignalType(MCalibrateData::kPhe);
151
152 calib[i].SetSerialNumber(CT[i]);
153 calib[i].SetCalibConvMinLimit(0.);
154 calib[i].SetCalibConvMaxLimit(100.); // Override limits for real data
155 calib[i].SetCalibrationMode(MCalibrateData::kFfactor);
156 // Do not change CalibrationMode (just indicates where the cal. constants will be stored)
157 calib[i].SetSignalType(mccalibupdate[i].GetSignalType());
158
159 mccalibcalc[i].SetSerialNumber(CT[i]);
160 mccalibcalc[i].SetMinSize(200);
161 // Minimum SIZE for an event to be used in the calculation of calibration constants.
162 // Units are ADC counts, and value depends on signal extractor!
163 }
164
165
166 //
167 // Now setup the tasks and tasklist:
168 // ---------------------------------
169 //
170 MReadMarsFile read("Events");
171 read.DisableAutoScheme();
172
173 if (CalibrationFilename)
174 read.AddFile(CalibrationFilename->Data());
175
176
177 MGeomApply* apply = new MGeomApply[NCTs];
178 MMcPedestalCopy* pcopy = new MMcPedestalCopy[NCTs];
179 MHillasCalc* hcalc = new MHillasCalc[NCTs];
180
181 TString outfile = "star_";
182 outfile += CT[0];
183 outfile += "_";
184 outfile += CT[1];
185
186 //
187 // We have two output files (will be later train and test sampls for random forest)
188 //
189 outfile += "_";
190 outfile += OutFileTag;
191 outfile += "_train.root";
192 MWriteRootFile write1(outfile);
193
194 outfile = "star_";
195 outfile += CT[0];
196 outfile += "_";
197 outfile += CT[1];
198
199 outfile += "_";
200 outfile += OutFileTag;
201 outfile += "_test.root";
202
203 MWriteRootFile write2(outfile);
204
205 for (Int_t i = 0; i < NCTs; i++)
206 {
207 apply[i]->SetSerialNumber(CT[i]);
208 pcopy[i]->SetSerialNumber(CT[i]);
209
210 hcalc[i]->SetSerialNumber(CT[i]);
211 hcalc[i].Disable(MHillasCalc::kCalcHillasSrc);
212 // Source-dependent parameters not needed in the first loop (calibration)
213
214 write1.SetSerialNumber(CT[i]);
215 write2.SetSerialNumber(CT[i]);
216
217 write1.AddContainer("MMcEvt", "Events");
218 write1.AddContainer("MHillas", "Events");
219 write1.AddContainer("MHillasExt", "Events");
220 write1.AddContainer("MHillasSrc", "Events");
221 write1.AddContainer("MNewImagePar", "Events");
222 write1.AddContainer("MSrcPosCam", "Events");
223 write2.AddContainer("MMcEvt", "Events");
224 write2.AddContainer("MHillas", "Events");
225 write2.AddContainer("MHillasExt", "Events");
226 write2.AddContainer("MHillasSrc", "Events");
227 write2.AddContainer("MNewImagePar", "Events");
228 write2.AddContainer("MSrcPosCam", "Events");
229 }
230
231 MStereoPar* mstereo = new MStereoPar;
232 plist.AddToList(mstereo);
233
234 write1.AddContainer(mstereo, "Events");
235 write2.AddContainer(mstereo, "Events");
236 // We use MWriteRootFile::AddContainer(MParContainer* ,...) instead
237 // of using the container name as above, because in the former case the
238 // serial number tag (indicating the telescope id) is added to the
239 // container name, which is fine for containers of which there is one
240 // per telescope. However, the container MStereoPar is unique, since it
241 // is filled with information coming from both telescopes.
242
243 write1.AddContainer("MRawRunHeader", "RunHeaders");
244 write1.AddContainer("MMcRunHeader", "RunHeaders");
245
246 write2.AddContainer("MRawRunHeader", "RunHeaders");
247 write2.AddContainer("MMcRunHeader", "RunHeaders");
248
249 tlist.AddToList(&read);
250
251 // Skip untriggered events (now camera simulation output contains by default all simulated events)
252 MContinue* trigger = new MContinue("(MMcTrig;1.fNumFirstLevel<1) && (MMcTrig;2.fNumFirstLevel<1)","Events");
253 tlist.AddToList(trigger);
254
255 for (i = 0; i < NCTs; i++)
256 {
257 tlist.AddToList(&(apply[i]));
258 tlist.AddToList(&(pcopy[i]));
259 tlist.AddToList(&(sigextract[i]));
260 tlist.AddToList(&(mccalibupdate[i]));
261 tlist.AddToList(&(calib[i]));
262 tlist.AddToList(clean[i]);
263 tlist.AddToList(&(hcalc[i]));
264 tlist.AddToList(&(mccalibcalc[i]));
265 }
266
267
268 MF filter1("{MMcEvt;1.fEvtNumber%2}<0.5");
269 MF filter2("{MMcEvt;1.fEvtNumber%2}>0.5");
270 //
271 // ^^^^ Filters to divide output in two: test and train samples.
272 //
273 write1.SetFilter (&filter1);
274 write2.SetFilter (&filter2);
275
276 //
277 // Create and set up the eventloop
278 //
279 MProgressBar bar;
280 bar.SetWindowName("Calibrating");
281
282 MEvtLoop evtloop;
283 evtloop.SetProgressBar(&bar);
284 evtloop.SetParList(&plist);
285
286 //
287 // First loop: calibration loop. Go over MC events simulated with
288 // no noise, to correlate SIZE with the number of phes and get the
289 // conversion factor (this is done by MMcCalibrationCalc).
290 //
291 if (CalibrationFilename)
292 {
293 if (!evtloop.Eventloop())
294 return;
295 }
296
297 tlist.PrintStatistics();
298
299 ///////////////////////////////////////////////////////////////////////
300
301
302 // Now prepare the second loop: go over the events you want to analyze.
303 // This time the MMcCalibrationUpdate tasks will apply the previously
304 // calculated calibration factors.
305
306 // First substitute the reading task:
307 MReadMarsFile read2("Events");
308 read2.AddFile(AnalysisFilename);
309 read2.DisableAutoScheme();
310 tlist.AddToListBefore(&read2, &read);
311 tlist.RemoveFromList(&read);
312
313 // Delete cleaning tasks and create new ones with absolute cleaning method:
314 for (Int_t i= 0; i < NCTs; i++ )
315 {
316 tlist.RemoveFromList(clean[i]);
317 delete clean[i];
318 }
319
320 // New cleaning tasks:
321 clean[0] = new MImgCleanStd(CleanLev[0], CleanLev[1]);
322 clean[1] = new MImgCleanStd(CleanLev[0], CleanLev[1]);
323 clean[0]->SetMethod(MImgCleanStd::kAbsolute);
324 clean[1]->SetMethod(MImgCleanStd::kAbsolute);
325 clean[0]->SetSerialNumber(CT[0]);
326 clean[1]->SetSerialNumber(CT[1]);
327 tlist.AddToListBefore(clean[0],&(hcalc[0]));
328 tlist.AddToListBefore(clean[1],&(hcalc[1]));
329
330 tlist.RemoveFromList(&(mccalibcalc[0]));
331 tlist.RemoveFromList(&(mccalibcalc[1])); // Remove calibration tasks from list.
332
333 // Now calculate also source-dependent Hillas parameters:
334 for (i = 0; i < NCTs; i++)
335 hcalc[i].Enable(MHillasCalc::kCalcHillasSrc);
336
337 // Add task to calculate stereo parameters:
338 MStereoCalc stereocalc;
339 stereocalc.SetCTids(CT[0],CT[1]);
340 stereocalc.SetCT1coor(ctx[0],cty[0]);
341 stereocalc.SetCT2coor(ctx[1],cty[1]);
342 tlist.AddToList(&stereocalc);
343
344 // Add writing tasks:
345 tlist.AddToList(&filter1);
346 tlist.AddToList(&write1);
347 tlist.AddToList(&filter2);
348 tlist.AddToList(&write2);
349
350 bar.SetWindowName("Analyzing");
351
352 if (!evtloop.Eventloop())
353 return;
354
355 tlist.PrintStatistics();
356
357 for (Int_t i= 0; i < NCTs; i++ )
358 delete clean[i];
359
360 plist.FindObject("MCalibrationChargeCam;1")->Write();
361 plist.FindObject("MCalibrationChargeCam;2")->Write();
362
363 plist.FindObject("MCalibrationQECam;1")->Write();
364 plist.FindObject("MCalibrationQECam;2")->Write();
365
366 return;
367}
Note: See TracBrowser for help on using the repository browser.