source: trunk/MagicSoft/Mars/mtemp/mifae/macros/OptimizeDisp.C@ 5674

Last change on this file since 5674 was 5674, checked in by domingo, 20 years ago
*** empty log message ***
File size: 18.8 KB
Line 
1//************************************************************************
2//
3// Authors : Eva Domingo, 12/2004 <mailto:domingo@ifae.es>
4// Wolfgang Wittek, 12/2004 <mailto:wittek@mpppmu.mpg.de>
5//
6//
7// Macro for estimating the DISP parameter
8// ---------------------------------------
9//
10// DISP is the distance between the center of gravity of the shower image
11// and the estimated source position, assumed to lie on the major
12// axis of the shower
13//
14// In order to get an estimate of DISP
15// - one assumes a parametrization DISP = f(p[0],p[1],p[2],... ; s1,s2,s3,...)
16// where s1,s2,s3,... are measurable quantities like
17// image parameters, (theta, phi), etc.
18// and p[0],p[1],p[2], ... are free parameters
19//
20// - and determines the free parameters a,b,c ... from MC gamma data
21// by minimizing a chi2 which measures the deviation of the estimated
22// source position (using DISP) and the true source position
23//
24// The following classes are used :
25//
26// MDisp container holding the parameters p[0],p[1],p[2],...
27//
28// ::Calc member function calculating DISP for a given event;
29// it is called by MDispCalc::Process()
30//
31// MDispCalc task calculating DISP with the parameters stored in MDisp
32//
33// MFindDisp class with several functions :
34//
35// ::DefineTrainMatrix \ member functions which generate the training
36// ::DefineTestMatrix | and test samples (in the form of matrices)
37// ::DefineTrainTestMatrix / from the MC gamma data
38//
39// ::FindParams member function steering the minimization
40// (definition of the fcn function for Minuit, setting up the
41// event loop to be executed in each minimization step,
42// call to Minuit);
43// for the minimization the training matrix is used
44//
45// ::TestParams member function testing the quality of the DISP estimate;
46// for the test the test matrix is used
47//
48// MHDisp container for histograms which are useful for judging
49// the quality of the DISP estimate
50// Also task calculating the chi2 contribution of an event
51// (in Fill())
52// and computing the final chi2 of the whole event sample
53// (in Finalize())
54//
55// MFDisp filter to select sample for the optimization
56//
57//
58// The classes are stored in the CVS directory Mars/mtemp/mifae/library
59// and Mars/mtemp/mifae/macros
60//
61//
62//************************************************************************
63
64void OptimizeDisp()
65{
66 //************************************************************************
67
68 Bool_t CMatrix = kFALSE; // Create training and test matrices
69 Bool_t WOptimize = kFALSE; // Do optimization using the training sample
70 // and write Disp parameter values
71 // onto the file parDispfile
72 // Optimize Disp with :
73 //TString typeOpt = "Data";
74 TString typeOpt = "MC";
75 Bool_t RTest = kFALSE; // Test the quality of the Disp estimate
76 // using the test matrix
77 Bool_t WDisp = kTRUE; // Make Disp plots for the data of type :
78 //TString typeInput = "ON";
79 //TString typeInput = "OFF";
80 TString typeInput = "MC";
81
82 //************************************************************************
83
84 gLog.SetNoColors();
85
86 if (gRandom)
87 delete gRandom;
88 gRandom = new TRandom3(0);
89
90 //-----------------------------------------------
91 //names of files to be read for generating the training and test matrices
92 const char *filetrain = "...";
93 const char *filetest = "...";
94
95 //-----------------------------------------------
96 // path for input for Mars
97 TString inPathON =
98 "/home/pcmagic14/wittek/CalibData/CrabLow42/2004_10_16/";
99 TString inPathOFF =
100 "/home/pcmagic14/wittek/CalibData/CrabLow42/2004_10_17/";
101 TString inPathMC =
102 "~domingo/MC/";
103
104 // path for output from Mars
105 TString outPath = "/mnt/home/pcmagic04/domingo/DispOptimization/";
106
107 //-----------------------------------------------
108 // names of files for which Disp plots should be made
109 const char *offfile = "*OffCrabLow42*";
110 const char *onfile = "*CrabLowEn42*";
111 const char *mcfile = "starmc_Gammas_30-20_Period21.27ReducedMC.Zbins0-12";
112 //-----------------------------------------------
113
114
115 //---------------------------------------------------------------------
116 gLog << "=====================================================" << endl;
117 gLog << "Macro OptimizeDisp : Start " << endl;
118
119 gLog << "" << endl;
120 gLog << "Macro OptimizeDisp : CMatrix, WOptimize, RTest, WDisp = "
121 << (CMatrix ? "kTRUE" : "kFALSE") << ", "
122 << (WOptimize ? "kTRUE" : "kFALSE") << ", "
123 << (RTest ? "kTRUE" : "kFALSE") << ", "
124 << (WDisp ? "kTRUE" : "kFALSE") << endl;
125
126
127 //--------------------------------------------
128 // files to contain the matrices (generated from filenameTrain and
129 // filenameTest)
130 //
131 // for the training
132 TString fileMatrixTrain = outPath;
133 fileMatrixTrain += "MatrixTrainDisp";
134 fileMatrixTrain += ".root";
135 gLog << "" << endl;
136 gLog << "Files containing the Training and Test matrices :" << endl;
137 gLog << " fileMatrixTrain = " << fileMatrixTrain << endl;
138
139 // for testing
140 TString fileMatrixTest = outPath;
141 fileMatrixTest += "MatrixTestDisp";
142 fileMatrixTest += ".root";
143 gLog << " fileMatrixTest = " << fileMatrixTest << endl;
144 gLog << "" << endl;
145
146
147 //---------------
148 // file to contain the optimum Disp parameter values
149 TString parDispFile = outPath;
150 // parDispFile += "parDispMC.root";
151 parDispFile += "parDispMC_withcuts.root";
152
153 gLog << "" << endl;
154 gLog << "File containing the optimum Disp parameter values : parDispFile = "
155 << parDispFile << endl;
156
157
158 // Set the filter cuts to select a sample of events for the Disp optimization
159 // (islandsmin,islandsmax,usedpixelsmin,usedpixelsmax,corepixelsmin,
160 // corepixelsmax,sizemin,sizemax,leakage1min,leakage1max,leakage2min,
161 // leakage2max,lengthmin,widthmin);
162 MFDisp *fdisp = NULL;
163 fdisp = new MFDisp;
164 fdisp->SetCuts(0,2,7,600,0,600,0.,3000.,0.,0.,0.,0.,0.,0.);
165 fdisp->SetName("FilterSelector2");
166
167
168 // Create the MFindDisp object and set the file to store optimium Disp parameters
169 MFindDisp finddisp(fdisp);
170 finddisp.SetFilenameParam(parDispFile);
171
172
173
174 //======================================================================
175 // Create matrices and write them onto files
176 //======================================================================
177
178 if (CMatrix)
179 {
180 gLog << "-----------------------------------------------------" << endl;
181 gLog << "Generate the training and test samples" << endl;
182
183 //--------------------------------------------
184 // files to be read for optimizing the Disp parameters
185 //
186 // for the training
187 TString filenameTrain = inPathMC;
188 filenameTrain += mcfile;
189 filenameTrain += ".root";
190 Int_t howManyTrain = 30000;
191 gLog << "filenameTrain = " << filenameTrain << ", howManyTrain = "
192 << howManyTrain << endl;
193
194 // for testing
195 TString filenameTest = inPathMC;
196 filenameTest += mcfile;
197 filenameTest += ".root";
198 Int_t howManyTest = 30000;
199 gLog << "filenameTest = " << filenameTest << ", howManyTest = "
200 << howManyTest << endl;
201
202
203 //--------------------------------------------
204 // For the events in the matrices define requested distribution
205 // in some quantities; this may be a 1-dim, 2-dim or 3-dim distribution
206
207 /*
208 // Define the cos(theta) distribution
209 TString mname("costheta");
210 MBinning bincost("Binning"+mname);
211 bincost.SetEdges(10, 0., 1.0);
212
213 //MH3 mh3("cos((MPointingPos.fZd)/kRad2Deg)");
214 MH3 mh3("cos(MMcEvt.fTelescopeTheta)");
215 mh3.SetName(mname);
216 MH::SetBinning(&mh3.GetHist(), &bincost);
217
218 for (Int_t i=1; i<=mh3.GetNbins(); i++)
219 mh3.GetHist().SetBinContent(i, 1.0);
220 */
221
222 // Define the log10(Etrue) distribution
223 TString mh3name("log10Etrue");
224 MBinning binslogE("Binning"+mh3name);
225 binslogE.SetEdges(50, 1., 3.);
226
227 MH3 mh3("log10(MMcEvt.fEnergy)");
228 mh3.SetName(mh3name);
229 MH::SetBinning(&mh3.GetHist(), &binslogE);
230
231 // Flat energy distribution
232 // for (Int_t i=1; i<=mh3.GetNbins(); i++)
233 // mh3.GetHist().SetBinContent(i, 1.0);
234
235 // Power law energy distribution
236 // for (Int_t i=1; i<=mh3.GetNbins(); i++)
237 // {
238 // Double_t weight = pow((Double_t)i, -1.7);
239 // mh3.GetHist().SetBinContent(i, weight);
240 // }
241
242 /*
243 // define the 'cos(Theta) vs. log10(Etrue)' distribution
244 TString mh3name("cosThetaVslog10Etrue");
245 MBinning binslogE("Binning"+mh3name+"X");
246 binslogE.SetEdges(18, 1.5, 2.2);
247 MBinning binscost("Binning"+mh3name+"Y");
248 binscost.SetEdges(10, 0., 1.0);
249
250 // MH3 mh3("log10(MMcEvt.fEnergy)", "cos((MPointingPos.fZd)/kRad2Deg)");
251 MH3 mh3("log10(MMcEvt.fEnergy)", "cos(MMcEvt.fTelescopeTheta)");
252 mh3.SetName(mh3name);
253 MH::SetBinning((TH2*)&mh3.GetHist(), &binslogE, &binscost);
254
255 for (Int_t i=1; i<=((TH2*)mh3.GetHist()).GetNbinsX(); i++)
256 {
257 // Double_t weight = pow((Double_t)i, -1.7);
258 for (Int_t j=1; j<=((TH2*)mh3.GetHist()).GetNbinsY(); j++)
259 {
260 // mh3.GetHist().SetBinContent(i, j, weight);
261 mh3.GetHist().SetBinContent(i, j, 1.);
262 }
263 }
264 */
265
266 //--------------------------
267 // Training and test samples are generated from the same input files
268 if (filenameTrain == filenameTest)
269 {
270 if ( !finddisp.DefineTrainTestMatrix(
271 filenameTrain, mh3,
272 howManyTrain, howManyTest,
273 fileMatrixTrain, fileMatrixTest, 0) )
274 {
275 *fLog << "OptimizeDisp.C : DefineTrainTestMatrix failed" << endl;
276 return;
277 }
278 }
279
280 //--------------------------
281 // Training and test samples are generated from different input files
282 else
283 {
284 if ( !finddisp.DefineTrainMatrix(filenameTrain, mh3,
285 howManyTrain, fileMatrixTrain, 0) )
286 {
287 *fLog << "OptimizeDisp.C : DefineTrainMatrix failed" << endl;
288 return;
289 }
290
291 if ( !finddisp.DefineTestMatrix( filenameTest, mh3,
292 howManyTest, fileMatrixTest, 0) )
293 {
294 *fLog << "OptimizeDisp.C : DefineTestMatrix failed" << endl;
295 return;
296 }
297 }
298
299 gLog << "Generation of training and test samples finished" << endl;
300 gLog << "-----------------------------------------------------" << endl;
301 }
302
303
304
305 //======================================================================
306 // Optimize Disp parameters using the training sample
307 //
308 // the initial values of the parameters are taken
309 // - from the file parDispInit (if != "")
310 // - or from the arrays params and steps (if their sizes are != 0)
311 // - or from the MDisp constructor
312 //======================================================================
313
314 if (WOptimize)
315 {
316 gLog << "-----------------------------------------------------" << endl;
317 gLog << "Optimize the Disp parameters over a "
318 << typeOpt << " sample, using the training matrix" << endl;
319
320 // Read training matrix from file
321 finddisp.ReadMatrix(fileMatrixTrain, fileMatrixTest);
322
323 //--------------------------------------------
324 // file which contains the initial values for the Disp parameters;
325 // if parDispInit =""
326 // - the initial values are taken from the arrays params and steps
327 // if their sizes are different from 0
328 // - otherwise they are taken from the MDisp constructor
329
330 TString parDispInit = outPath;
331 //parDispInit += "parDispInit.root";
332 parDispInit = "";
333
334 //--------------------------------------------
335
336 TArrayD params(0);
337 TArrayD steps(0);
338
339 if (parDispInit == "")
340 {
341 Double_t vparams[5] = {
342 // p[0], p[1], p[2], p[3], p[4], p[5]
343 1.0, 0.6, -0.8, -0.8, -1.2/*, 0.5*/};
344 // 0.5, 0., 0.03, 0., 0./*, 0.5*/};
345 // 0.8, 0., 0., 0., 0./*, 0.5*/};
346
347 Double_t vsteps[5] = {
348 // dp[0], dp[1], dp[2], dp[3], dp[4], dp[5]
349 0.01, 0.005, 0.005, 0.005, 0.01/*, 0.001*/};
350 // 0.01, 0.01, 0.01, 0.01, 0.01/*, 0.001*/};
351 // 0.01, 0.01, 0.01, 0.01, 0.01/*, 0.001*/};
352
353 params.Set(5, vparams);
354 steps.Set (5, vsteps );
355 }
356
357
358 Bool_t rf;
359 rf = finddisp.FindParams(parDispInit, params, steps);
360
361 if (!rf)
362 {
363 gLog << "OptimizeDisp.C : optimization of the Disp parameters failed" << endl;
364 return;
365 }
366
367 gLog << "Optimization of Disp parameters finished" << endl;
368 gLog << "-----------------------------------------------------" << endl;
369 }
370
371
372
373 //======================================================================
374 // Test the Disp parameters on the test sample
375 //======================================================================
376
377 if (RTest)
378 {
379 // Read test matrix from file
380 finddisp.ReadMatrix(fileMatrixTrain, fileMatrixTest);
381
382 gLog << "-----------------------------------------------------" << endl;
383 gLog << "Test the Disp parameters using on the test matrix" << endl;
384 Bool_t rt = finddisp.TestParams();
385 if (!rt)
386 {
387 gLog << "Test of the Disp parameters on the test matrix failed"
388 << endl;
389 }
390 gLog << "Test of the Disp parameters finished" << endl;
391 gLog << "-----------------------------------------------------" << endl;
392 }
393
394
395
396
397 //======================================================================
398 // Make Disp plots
399 //======================================================================
400
401 if (WDisp)
402 {
403 gLog << "" << endl;
404 gLog << "-----------------------------------------------------" << endl;
405 gLog << "Make plots for Disp for data of the type " << typeInput << endl;
406
407 //--------------------------------------------
408 // type of data to be read (ON, OFF or MC)
409 if (typeInput == "ON")
410 TString file(onfile);
411 else if (typeInput == "OFF")
412 TString file(offfile);
413 else if (typeInput == "MC")
414 TString file(mcfile);
415
416 // name of input root file
417 TString filenameData = inPathMC;
418 filenameData += mcfile;
419 filenameData += ".root";
420 gLog << "Input file '" << filenameData << endl;
421
422 //----------------------------------------------------
423 // read in optimum Disp parameter values
424
425 TFile inparam(parDispFile);
426 MDisp dispin;
427 dispin.Read("MDisp");
428 inparam.Close();
429
430 gLog << "Disp parameter values were read in from file '"
431 << parDispFile << "'" << endl;
432
433 TArrayD dispPar;
434 dispPar = dispin.GetParameters();
435
436 TArrayD dispStep;
437 dispStep = dispin.GetStepsizes();
438
439 gLog << "optimum parameter values for calculating Disp : " << endl;
440 for (Int_t i=0; i<dispPar.GetSize(); i++)
441 {
442 gLog << dispPar[i] << ", ";
443 }
444 gLog << endl;
445
446
447 //----------------------------------------------------
448 MTaskList tliston;
449 MParList pliston;
450
451 MReadMarsFile read("Events", filenameData);
452 read.DisableAutoScheme();
453
454 // set cuts to select an event sample to apply Disp
455 MContinue contdisp(fdisp);
456
457 // calculate Disp
458 MDispCalc dispcalc;
459
460 // make Disp plots
461 // SelectedPos = 1 means choose the right source position
462 // 2 wrong
463 // 3 the position according to M3Long
464 // 4 the position according to Asym
465 MHDisp hdisp;
466 hdisp.SetSelectedPos(1);
467 MFillH filldisp("MHDisp", "");
468 filldisp.SetName("FillDispPlots");
469
470 MHDisp hdisp1;
471 hdisp1.SetName("MHDispCorr");
472 hdisp1.SetSelectedPos(1);
473 MFillH filldisp1("MHDispCorr[MHDisp]", "");
474
475 MHDisp hdisp2;
476 hdisp2.SetName("MHDispWrong");
477 hdisp2.SetSelectedPos(2);
478 MFillH filldisp2("MHDispWrong[MHDisp]", "");
479
480 MHDisp hdisp3;
481 hdisp3.SetName("MHDispM3Long");
482 hdisp3.SetSelectedPos(3);
483 MFillH filldisp3("MHDispM3Long[MHDisp]", "");
484
485 MHDisp hdisp4;
486 hdisp4.SetName("MHDispAsym");
487 hdisp4.SetSelectedPos(4);
488 MFillH filldisp4("MHDispAsym[MHDisp]", "");
489
490
491 //*****************************
492 // entries in MParList
493
494 pliston.AddToList(&tliston);
495 pliston.AddToList(&dispin);
496 pliston.AddToList(&hdisp);
497 pliston.AddToList(&hdisp1);
498 pliston.AddToList(&hdisp2);
499 pliston.AddToList(&hdisp3);
500 pliston.AddToList(&hdisp4);
501
502 //*****************************
503 // entries in MTaskList
504
505 tliston.AddToList(&read);
506 if (fdisp != NULL)
507 tliston.AddToList(&contdisp);
508 tliston.AddToList(&dispcalc);
509 tliston.AddToList(&filldisp);
510 tliston.AddToList(&filldisp1);
511 tliston.AddToList(&filldisp2);
512 tliston.AddToList(&filldisp3);
513 tliston.AddToList(&filldisp4);
514
515 //*****************************
516
517 //-------------------------------------------
518 // Execute event loop
519 //
520 MProgressBar bar;
521 MEvtLoop evtloop;
522 evtloop.SetParList(&pliston);
523 evtloop.SetProgressBar(&bar);
524
525 Int_t maxevents = -1;
526 if ( !evtloop.Eventloop(maxevents) )
527 return;
528
529 tliston.PrintStatistics(0, kTRUE);
530
531 //-------------------------------------------
532 // Display the histograms
533 //
534 // hdisp.Draw();
535 hdisp1.Draw();
536 hdisp2.Draw();
537 hdisp3.Draw();
538 hdisp4.Draw();
539
540 //-------------------------------------------
541
542 gLog << "Disp plots were made for file '" << filenameData << endl;
543 gLog << "-----------------------------------------------------" << endl;
544 }
545
546 delete fdisp;
547
548 gLog << "Macro OptimizeDisp.C : End " << endl;
549 gLog << "=======================================================" << endl;
550
551}
552
553
554
555
556
557
558
559
560
Note: See TracBrowser for help on using the repository browser.