source: trunk/MagicSoft/Mars/mtemp/mifae/macros/makeHillasMC.C@ 6723

Last change on this file since 6723 was 6196, checked in by blanch, 20 years ago
*** empty log message ***
File size: 14.6 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): Thomas Bretz 5/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19 ! Abelardo Moralejo 1/2004 <mailto:moralejo@pd.infn.it>
20 ! Eva Domingo 1/2005 <mailto:domingo@ifae.es>
21 !
22 ! Copyright: MAGIC Software Development, 2000-2004
23 !
24 !
25 \* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// STARMC - STandard Analysis and Reconstruction (MC example)
30//
31// This macro is a version of the standard converter to convert raw data
32// into image parameters, made to show how to run analysis on MC files.
33//
34// (1/2005):: Spline extractor added and additional containers written out
35//
36/////////////////////////////////////////////////////////////////////////////
37
38#include "MImgCleanStd.h"
39
40void makeHillasMC()
41{
42 //
43 // This is a demonstration program which calculates the image
44 // parameters from Magic Monte Carlo files (output of camera).
45
46
47 TString* CalibrationFilename;
48 TString* AnalysisFilename;
49 TString* OutFilename1;
50 TString* OutFilename2;
51
52 TString rffilename;
53 TString dispfilename;
54
55 // ------------- USER CHANGE -----------------
56 // Comment line starting "CalibrationFileName" to disable calibration.
57 // In that case the units of the MHillas.fSize parameter will be ADC counts
58 // (rather, equivalent ADC counts in inner pixels, since we correct for the
59 // possible differences in gain of outer pixels)
60
61 // File to be used in the calibration (must be a camera file without added noise)
62 CalibrationFilename = new TString("/mnt/local_wdjrico/jrico/mc/Gammas/Gamma_zbin3_90_7_1290to1299_w0_nonoise.root");
63 // File to be analyzed
64 AnalysisFilename = new TString("/mnt/users/blanch/magic/TestSample/file*.root");
65
66 // Change output file names as desired.
67 // If you want only one output (not dividing the events in Train and Test samples),
68 // comment the initialization of OutFilename2.
69 OutFilename1 = new TString("/mnt/users/blanch/Temp/Prova.root");
70 // OutFilename2 = new TString("star_test.root"); // Output file name 2 (train)
71
72 // File to read RandomForest
73 rffilename="/mnt/users/blanch/magic/Mars-All/Mars_Standard06/mtemp/mifae/programs/RFstd.root";
74 // File to read Disp
75 dispfilename="/mnt/users/blanch/magic/Mars-All/Mars_Standard06/mtemp/mifae/programs/DISPstd.root";
76
77 // Fraction of events (taken at random) which one wants to process from the
78 // file to be analyzed (useful to make smaller files if starting sample is too large).
79 Float_t accepted_fraction = 1.;
80
81
82 // ------------- USER CHANGE -----------------
83 // Cleaning levels
84 Float_t CleanLev[2] = {4.0, 3.5}; // Tail cuts for image analysis
85
86 // Signal extractor
87 // (default=Spline, other extraction methods can be used)
88 const Int_t wsize=2;
89 MExtractor* sigextract = new MExtractTimeAndChargeSpline();
90 ((MExtractTimeAndChargeSpline*)sigextract)->SetTimeType(MExtractTimeAndChargeSpline::kHalfMaximum);
91 ((MExtractTimeAndChargeSpline*)sigextract)->SetChargeType(MExtractTimeAndChargeSpline::kIntegral);
92 ((MExtractTimeAndChargeSpline*)sigextract)->SetRiseTime((Float_t)wsize*0.25);
93 ((MExtractTimeAndChargeSpline*)sigextract)->SetFallTime((Float_t)wsize*0.75);
94 // Define FADC slices to be integrated in high and low gain:
95 sigextract->SetRange(1, 11, 2, 12);
96 // Defines when to switch to low gain
97 sigextract->SetSaturationLimit(240);
98
99
100 // -------------------------------------------
101 //
102 // Create a empty Parameter List and an empty Task List
103 // The tasklist is identified in the eventloop by its name
104 //
105 MParList plist;
106 MTaskList tlist;
107 plist.AddToList(&tlist);
108
109 // MGeomCamMagic geomcam;
110
111 // MSrcPosCam src;
112 // WOBBLE MODE!!
113 // src.SetX(120.); // units: mm
114 // src.SetXY(0.,0.4/geomcam.GetConvMm2Deg());
115 // src.SetXY(0.,0.);
116 // src.SetReadyToSave();
117
118 MBadPixelsCam badpix;
119
120 // plist.AddToList(&geomcam);
121 // plist.AddToList(&src);
122 plist.AddToList(&badpix);
123
124 MCerPhotEvt nphot;
125 // Stores number of phe
126 plist.AddToList(&nphot);
127
128 // Now setup the tasks and tasklist:
129 // ---------------------------------
130 //
131 MReadMarsFile read("Events");
132 if (CalibrationFilename)
133 read.AddFile(CalibrationFilename->Data());
134 read.DisableAutoScheme();
135
136 MGeomApply geom;
137 // Reads in geometry from MC file and sets the right sizes for
138 // several parameter containers.
139
140 MMcPedestalCopy pcopy;
141 // Copies pedestal data from the MC file run fadc header to the MPedestalCam container.
142
143 MPointingPosCalc pointcalc;
144 // Creates MPointingPos object and fill it with the telescope orientation
145 // information taken from MMcEvt.
146
147 MMcCalibrationUpdate mccalibupdate;
148
149 MCalibrateData calib;
150 // Transforms signals from ADC counts into photons. In the first
151 // loop it applies a "dummy" calibration supplied by MMcCalibrationUpdate, just
152 // to equalize inner and outer pixels. At the end of the first loop, in the
153 // PostProcess of MMcCalibrationCalc (see below) the true calibration constants
154 // are calculated.
155 calib.SetCalibrationMode(MCalibrateData::kFfactor);
156
157 // MBlindPixelCalc blind;
158 // blind.SetUseInterpolation();
159
160 MMcCalibrationCalc mccalibcalc;
161 // Calculates calibration constants to convert from ADC counts to photons.
162
163 MAddGainFluctuation gainfluc;
164 //gainfluc.FillHist(1,0.5);
165 gainfluc.FillHist(0,0.1); // defaul line to not add fluctuations
166 // Adds Gain fluctuations
167
168 MImgCleanStd clean(CleanLev[0], CleanLev[1]);
169 // Applies tail cuts to image. Since the calibration is performed on
170 // noiseless camera files, the precise values of the cleaning levels
171 // are unimportant for the calibration file processing (in any case,
172 // only pixels without any C-photon will be rejected).
173 clean.SetCleanRings(1);
174 // clean.SetRemoveSingle(kFALSE);
175 // clean.SetMethod(MImgCleanStd::kDemocratic);
176
177 MHillasCalc hcalc;
178 // Calculates Hillas parameters.
179
180 tlist.AddToList(&read);
181 tlist.AddToList(&geom);
182 tlist.AddToList(&pcopy);
183 tlist.AddToList(&pointcalc);
184 tlist.AddToList(sigextract);
185 tlist.AddToList(&mccalibupdate);
186 tlist.AddToList(&calib);
187 tlist.AddToList(&clean);
188 // tlist.AddToList(&blind);
189 tlist.AddToList(&hcalc);
190 tlist.AddToList(&mccalibcalc);
191
192
193 //
194 // Open output files:
195 //
196
197 MWriteRootFile write1(OutFilename1->Data()); // Writes output1
198 write1.AddContainer("MRawRunHeader", "RunHeaders");
199 write1.AddContainer("MMcRunHeader", "RunHeaders");
200 write1.AddContainer("MSrcPosCam", "RunHeaders");
201 write1.AddContainer("MGeomCam", "RunHeaders");
202 write1.AddContainer("MMcConfigRunHeader", "RunHeaders");
203 write1.AddContainer("MMcCorsikaRunHeader", "RunHeaders");
204 write1.AddContainer("MMcFadcHeader", "RunHeaders");
205 write1.AddContainer("MMcTrigHeader", "RunHeaders");
206 write1.AddContainer("MGainFluctuationCam", "RunHeaders");
207
208 write1.AddContainer("MRawEvtHeader", "Events");
209 write1.AddContainer("MMcEvt", "Events");
210 write1.AddContainer("MHillas", "Events");
211 write1.AddContainer("MHillasExt", "Events");
212 write1.AddContainer("MHillasSrc", "Events");
213 write1.AddContainer("MImagePar", "Events");
214 write1.AddContainer("MNewImagePar", "Events");
215 write1.AddContainer("MConcentration", "Events");
216 write1.AddContainer("MIslands", "Events");
217 write1.AddContainer("MTopology", "Events");
218 write1.AddContainer("MPointingPos", "Events");
219 write1.AddContainer("MHadronness", "Events");
220 write1.AddContainer("MImageParDisp" , "Events");
221 //write1.AddContainer("MArrivalTimeCam", "Events");
222 //write1.AddContainer("MCerPhotEvt", "Events");
223
224 if (OutFilename2)
225 {
226 MWriteRootFile write2(OutFilename2->Data()); // Writes output2
227 write2.AddContainer("MRawRunHeader", "RunHeaders");
228 write2.AddContainer("MMcRunHeader", "RunHeaders");
229 write2.AddContainer("MSrcPosCam", "RunHeaders");
230 write2.AddContainer("MGeomCam", "RunHeaders");
231 write2.AddContainer("MMcConfigRunHeader", "RunHeaders");
232 write2.AddContainer("MMcCorsikaRunHeader", "RunHeaders");
233 write2.AddContainer("MMcFadcHeader", "RunHeaders");
234 write2.AddContainer("MMcTrigHeader", "RunHeaders");
235 write2.AddContainer("MGainFluctuationCam", "RunHeaders");
236
237 write2.AddContainer("MRawEvtHeader", "Events");
238 write2.AddContainer("MMcEvt", "Events");
239 write2.AddContainer("MHillas", "Events");
240 write2.AddContainer("MHillasExt", "Events");
241 write2.AddContainer("MHillasSrc", "Events");
242 write2.AddContainer("MImagePar", "Events");
243 write2.AddContainer("MNewImagePar", "Events");
244 write2.AddContainer("MConcentration", "Events");
245 write2.AddContainer("MIslands", "Events");
246 write2.AddContainer("MTopology", "Events");
247 write2.AddContainer("MPointingPos", "Events");
248 write2.AddContainer("MHadronness", "Events");
249 write2.AddContainer("MImageParDisp" , "Events");
250 //write2.AddContainer("MArrivalTimeCam", "Events");
251 //write2.AddContainer("MCerPhotEvt", "Events");
252
253
254 // Divide output in train and test samples, using the event number
255 // (odd/even) to achieve otherwise unbiased event samples:
256 MF filter1("{MMcEvt.fEvtNumber%2}>0.5");
257 MF filter2("{MMcEvt.fEvtNumber%2}<0.5");
258
259 write1.SetFilter(&filter1);
260 write2.SetFilter(&filter2);
261 }
262
263
264 //
265 // FIRST LOOP: Calibration loop (with no noise file)
266 //
267 MProgressBar bar;
268 bar.SetWindowName("Calibrating...");
269
270 MEvtLoop evtloop;
271 evtloop.SetProgressBar(&bar);
272 evtloop.SetParList(&plist);
273
274 if (CalibrationFilename)
275 {
276 if (!evtloop.Eventloop())
277 return;
278 mccalibcalc.GetHistADC2PhotEl()->Write();
279 mccalibcalc.GetHistPhot2PhotEl()->Write();
280 // Writes out the histograms used for calibration.
281 }
282
283 //
284 // SECOND LOOP: TO READ THE RANDOM FOREST FILE(S)
285 //
286
287 MParList plistrf;
288 MTaskList tlistrf;
289 MRanForest ranforest;
290 MRanTree rantree;
291
292 if(rffilename.Length())
293 {
294 plistrf.AddToList(&tlistrf);
295 plistrf.AddToList(&ranforest);
296 plistrf.AddToList(&rantree);
297
298 MReadTree readrf("Tree",rffilename);
299 readrf.DisableAutoScheme();
300
301 MRanForestFill rffill;
302 rffill.SetNumTrees(100);
303
304 tlistrf.AddToList(&readrf);
305 tlistrf.AddToList(&rffill);
306
307 MEvtLoop evtlooprf;
308 evtlooprf.SetParList(&plistrf);
309 if (!evtlooprf.Eventloop())
310 return;
311
312 tlistrf.PrintStatistics();
313 }
314
315
316 //
317 // THIRD LOOP: Analysis loop (process file with noise)
318 //
319 bar.SetWindowName("Analyzing...");
320
321 MIslands isl;
322 MArrivalTimeCam timecam;
323 MTopology topology;
324 plist.AddToList(&isl);
325 plist.AddToList(&timecam);
326 plist.AddToList(&topology);
327 plist.AddToList(&rantree);
328 plist.AddToList(&ranforest);
329
330 MArrivalTimeCalc2 timecalc;
331 // Calculates the arrival time as the mean time of the fWindowSize
332 // time slices which have the highest integral content.
333 // MArrivalTimeCalc timecalc;
334 // Calculates the arrival times of photons. Returns the absolute
335 // maximum of the spline that interpolates the FADC slices.
336
337 MIslandsCalc islcalc;
338 islcalc.SetOutputName("MIslands");
339 islcalc.SetAlgorithm(1);
340 // MIslandsClean islclean(40);
341 // islclean.SetInputName("MIslands");
342 // islclean.SetMethod(1);
343
344 MTopologyCalc topcalc;
345
346 // Change the read task by another one which reads the file we want to analyze:
347 MReadMarsFile read2("Events","/mnt/users/blanch/magic/TestSample/file*.root");
348 // MReadMarsFile read2("Events","/mnt/users/blanch/magic/TestSample/file53.root");
349 // read2.AddFile(AnalysisFilename->Data());
350 read2.DisableAutoScheme();
351 tlist.AddToListBefore(&read2, &read);
352 tlist.RemoveFromList(&read);
353
354
355 // Add new tasks (Islands and Topology calculations)
356 tlist.AddToListBefore(&timecalc,&mccalibupdate);
357 tlist.AddToListBefore(&islcalc,&hcalc);
358 // tlist.AddToListBefore(&islclean,&hcalc);
359 tlist.AddToListBefore(&topcalc,&hcalc);
360
361 // Add Task to Add Gain Fluctuations
362 tlist.AddToListBefore(&gainfluc, &hcalc);
363
364 MGeomCamMagic geomcam;// = new MGeomCam();
365 plist.AddToList(&geomcam);
366
367
368 MHillasDisplay* disphillas=NULL;
369 disphillas = new MHillasDisplay(&nphot,&geomcam);
370
371 // Analyze only the desired fraction of events, skip the rest:
372 MFEventSelector eventselector;
373 Float_t rejected_fraction = 1. - accepted_fraction;
374 eventselector.SetSelectionRatio(rejected_fraction);
375 MContinue skip(&eventselector);
376 tlist.AddToListBefore(&skip, sigextract);
377
378 // Remove calibration task from list.
379 tlist.RemoveFromList(&mccalibcalc);
380
381 // disp
382 // (read in optimum Disp parameter values)
383 MDispParameters dispin;
384 TArrayD dispPar;
385 if(dispfilename.Length())
386 {
387 TFile inparam(dispfilename);
388 dispin.Read("MDispParameters");
389 cout << "Optimum parameter values taken for calculating Disp : " << endl;
390
391 dispPar = dispin.GetParameters();
392 for (Int_t i=0; i<dispPar.GetSize(); i++)
393 cout << dispPar[i] << ", ";
394 cout << endl;
395
396 inparam.Close();
397 }
398 // Disp results container
399 MImageParDisp imagepardisp;
400 // Disp histograms
401 MHDisp hdisp;
402 hdisp.SetName("MHDispAsym");
403 hdisp.SetSelectedPos(4);
404 MFillH filldisp("MHDispAsym[MHDisp]", "");
405
406 // Add RandomForest and Disp task computation
407 MRanForestCalc hadrcalc;
408 MDispCalc dispcalc;
409
410 tlist.AddToList(&dispcalc);
411 tlist.AddToList(&filldisp);
412 if(rffilename.Length())
413 tlist.AddToList(&hadrcalc);
414
415 // Add tasks to write output:
416 if (OutFilename2)
417 {
418 tlist.AddToList(&filter1);
419 tlist.AddToList(&filter2);
420 tlist.AddToList(&write2);
421 }
422 tlist.AddToList(&write1);
423
424 // Add display image with hillas
425 disphillas->SetPSFile();
426 disphillas->SetPSFileName("provaGF.ps");
427 disphillas->SetPause(kFALSE);
428 //tlist.AddToList(disphillas);
429
430
431 if (!evtloop.Eventloop())
432 return;
433
434 tlist.PrintStatistics();
435}
Note: See TracBrowser for help on using the repository browser.