source: trunk/MagicSoft/Mars/macros/CT1EEst.C@ 2037

Last change on this file since 2037 was 1963, checked in by wittek, 22 years ago
*** empty log message ***
File size: 16.9 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, 09/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Abelardo Moralejo, 03/2003 <mailto:moralejo@pd.infn.it>
20! Wolfgang Wittek, 04/2003 <mailto:wittek@mppmu.mpg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2003
23!
24!
25\* ======================================================================== */
26//
27//------------------------------------------------------------------------
28//
29// fcn calculates the function to be minimized (using TMinuit::Migrad)
30//
31void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
32{
33 MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit();
34
35 MTaskList *tlist = (MTaskList*)evtloop->GetParList()->FindObject("MTaskList"); // GetTaskList();
36
37 MChisqEval *eval = (MChisqEval*) tlist->FindObject("MChisqEval");
38 MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam");
39
40 eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par));
41
42 evtloop->Eventloop();
43
44 f = eval->GetChisq();
45}
46//------------------------------------------------------------------------
47
48void CT1EgyEst()
49{
50 TString inPath = "~magican/ct1test/wittek/marsoutput/optionA/";
51 TString fileNameIn = "MC2.root";
52
53 TString outPath = "~magican/ct1test/wittek/marsoutput/optionA/";
54 TString energyParName = "energyest.root";
55
56 TString hilName = "MHillas";
57 TString hilSrcName = "MHillasSrc";
58
59 TString hadronnessName = "MHadronness";
60
61 Int_t howMany = 2200;
62
63 Float_t maxhadronness = 0.4;
64 Float_t maxalpha = 20.0;
65
66 CT1EEst(inPath, fileNameIn,
67 outPath, energyParName,
68 hilName, hilSrcName, hadronnessName,
69 howMany, maxhadronness, maxalpha);
70}
71
72
73
74//------------------------------------------------------------------------
75//
76// Arguments of CT1EEst :
77// ------------------------
78//
79// inPath, fileNameIn path and name of input file (MC gamma events)
80// outPath, energyParName path and name of file containing the parameters
81// of the energy estimator
82// hilName, hilSrcName names of "MHillas" and "MHillasSrc" containers
83// hadronnessName name of container holding the hadronness
84// howMany no.of events to be read from input file
85// maxhadronness maximum value of hadronness to be accepted
86// maxalpha maximum value of |ALPHA| to be accepted
87//
88void CT1EEst(TString inPath, TString fileNameIn,
89 TString outPath, TString energyParName,
90 TString hilName, TString hilSrcName, TString hadronnessName,
91 Int_t howMany,
92 Float_t maxhadronness, Float_t maxalpha)
93{
94 cout << "================================================================"
95 << endl;
96 cout << "Start of energy estimation part" << endl;
97 cout << "" << endl;
98 cout << "CT1EEst input values : " << endl;
99 cout << "---------------------- " << endl;
100 cout << "inPath, fileNameIn = "
101 << inPath << ", " << fileNameIn << endl;
102 cout << "outPath, energyParName = "
103 << outPath << ", " << energyParName << endl;
104 cout << "hilName, hilSrcName, hadronnessName = " << hilName << ", "
105 << hilSrcName << ", " << hadronnessName << endl;
106 cout << "howMany, maxhadronness, maxalpha = " << howMany << ", "
107 << maxhadronness << ", " << maxalpha << endl;
108 cout << "" << endl;
109
110
111 // convert from [deg] to [mm]
112 //
113 MGeomCamCT1 gct1;
114 //maxdist /= gct1.GetConvMm2Deg();
115
116
117 //==========================================================================
118 // Start of Part 1 (determination of the parameters of the energy estimator)
119 //
120
121 //-----------------------------------------------------------------------
122 // Fill events into a MHMatrix,
123 // to be used later in the minimization by MINUIT
124 //
125
126 MParList parlist;
127
128 TString inputfile(outPath);
129 inputfile += fileNameIn;
130
131 MFEventSelector selector;
132 selector.SetNumSelectEvts(howMany);
133
134 cout << "Read events from file '" << inputfile << "'" << endl;
135 MReadTree read("Events");
136 read.AddFile(inputfile);
137 read.DisableAutoScheme();
138 read.SetSelector(&selector);
139
140 MFCT1SelFinal filterhadrons;
141 filterhadrons.SetCuts(maxhadronness, maxalpha);
142 filterhadrons.SetHadronnessName(hadronnessName);
143 filterhadrons.SetInverted();
144
145 cout << "Define columns of matrix" << endl;
146 MHMatrix matrix;
147 Int_t colenergy = matrix.AddColumn("MMcEvt.fEnergy");
148 Int_t colimpact = matrix.AddColumn("MMcEvt.fImpact");
149
150 if (colenergy < 0 || colimpact < 0)
151 {
152 cout << "colenergy, colimpact = " << colenergy << ", "
153 << colimpact << endl;
154 return;
155 }
156
157 MEnergyEstParam eest(hilName);
158 eest.Add(hilSrcName);
159 eest.InitMapping(&matrix);
160
161 cout << "--------------------------------------" << endl;
162 cout << "Fill events into the matrix" << endl;
163 if ( !matrix.Fill(&parlist, &read, &filterhadrons) )
164 return;
165 cout << "Matrix was filled with " << matrix.GetNumRows()
166 << " events" << endl;
167
168 //-----------------------------------------------------------------------
169 //
170 // Setup the eventloop which will be executed in the fcn of MINUIT
171 //
172 cout << "--------------------------------------" << endl;
173 cout << "Setup event loop to be used in 'fcn'" << endl;
174
175 MParList parlist;
176 MTaskList tasklist;
177
178 MMatrixLoop loop(&matrix);
179
180
181 MChisqEval eval;
182 eval.SetY1(new MDataElement(&matrix, colenergy));
183 eval.SetY2(new MDataMember("MEnergyEst.fEnergy"));
184 eval.SetOwner();
185
186 //********************************
187 // Entries in MParList
188
189 parlist.AddToList(&tasklist);
190
191 //********************************
192 // Entries in MTaskList
193
194 tasklist.AddToList(&loop);
195 tasklist.AddToList(&eest);
196 tasklist.AddToList(&eval);
197
198 //********************************
199
200 cout << "Event loop was setup" << endl;
201 MEvtLoop evtloop;
202 evtloop.SetParList(&parlist);
203
204
205
206 //..........................................................................
207
208 //---------- Start of minimization part --------------------
209 //
210 // Do the minimization with MINUIT
211 //
212 // Be careful: This is not thread safe
213 //
214 cout << "--------------------------------------" << endl;
215 cout << "Start minimization" << endl;
216
217 TMinuit minuit(12);
218 minuit.SetPrintLevel(-1);
219 minuit.SetFCN(fcn);
220
221 // Ready for: minuit.mnexcm("SET ERR", arglist, 1, ierflg)
222
223 if (minuit.SetErrorDef(1))
224 {
225 cout << "SetErrorDef failed." << endl;
226 return;
227 }
228
229 //
230 // Set initial values of the parameters (close enough to the final ones, taken
231 // from previous runs of the procedure). Parameter fA[4] is not used in the default
232 // energy estimation model (from D. Kranich).
233 //
234 TArrayD fA(5);
235 TArrayD fB(7);
236
237 fA[0] = 21006.2;
238 fA[1] = -43.2648;
239 fA[2] = -690.403;
240 fA[3] = -0.0428544;
241 fA[4] = 1.;
242 fB[0] = -3391.05;
243 fB[1] = 136.58;
244 fB[2] = 0.253807;
245 fB[3] = 254.363;
246 fB[4] = 61.0386;
247 fB[5] = -0.0190984;
248 fB[6] = -421695;
249
250 //
251 // Set starting values and step sizes for parameters
252 //
253 for (Int_t i=0; i<fA.GetSize(); i++)
254 {
255 TString name = "fA[";
256 name += i;
257 name += "]";
258 Double_t vinit = fA[i];
259 Double_t step = fabs(fA[i]/3);
260
261 Double_t limlo = 0; // limlo=limup=0: no limits
262 Double_t limup = 0;
263
264 Bool_t rc = minuit.DefineParameter(i, name, vinit, step, limlo, limup);
265 if (!rc)
266 continue;
267
268 cout << "Error in defining parameter #" << i << endl;
269 return;
270 }
271
272 for (Int_t i=0; i<fB.GetSize(); i++)
273 {
274 TString name = "fB[";
275 name += i;
276 name += "]";
277 Double_t vinit = fB[i];
278 Double_t step = fabs(fB[i]/3);
279
280 Double_t limlo = 0; // limlo=limup=0: no limits
281 Double_t limup = 0;
282
283 Bool_t rc = minuit.DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
284 if (!rc)
285 continue;
286
287 cout << "Error in defining parameter #" << i+fA.GetSize() << endl;
288 return;
289 }
290
291 //
292 // Setup globals used in FCN
293 //
294 minuit.SetObjectFit(&evtloop);
295
296 TStopwatch clock;
297 clock.Start();
298
299 // Now ready for minimization step:
300
301 gLog.SetNullOutput(kTRUE);
302 Bool_t rc = minuit.Migrad();
303 gLog.SetNullOutput(kFALSE);
304
305 if (rc)
306 {
307 cout << "Migrad failed." << endl;
308 return;
309 }
310
311 cout << endl;
312 clock.Stop();
313 clock.Print();
314 cout << endl;
315
316 cout << "Resulting Chisq: " << minuit.fAmin << endl;
317
318 for (Int_t i=0; i<fA.GetSize()+fB.GetSize(); i++)
319 {
320 Double_t val;
321 Double_t er;
322
323 if (!minuit.GetParameter(i, val, er))
324 {
325 cout << "Error getting parameter #" << i << endl;
326 return;
327 }
328
329 cout << i << ": " << val << endl;
330 // " +- " << er << endl;
331 }
332
333 /*
334 // Print results
335 Double_t amin, edm, errdef;
336 Int_t nvpar, nparx, icstat;
337 minuit.mnstat(amin, edm, errdef, nvpar, nparx, icstat);
338 minuit.mnprin(3, amin);
339 eest.Print();
340 */
341
342 eest.StopMapping();
343 cout << "Minimization finished" << endl;
344
345 //---------- End of minimization part --------------------
346
347 //..........................................................................
348
349
350 //
351 // Now write the parameters of the energy estimator to a file
352 //
353 TVector* EnergyParams = new TVector(12);
354
355 for (Int_t i=0; i<eest.GetNumCoeff(); i++)
356 EnergyParams(i) = eest.GetCoeff(i);
357
358 TString paramout(outPath);
359 paramout += energyParName;
360
361 TFile outfile(paramout, "RECREATE");
362 EnergyParams->Write("EnergyParams");
363 outfile.Close();
364
365 cout << "--------------------------------------" << endl;
366 cout << "Write parameters of energy estimator onto file '"
367 << paramout << endl;
368 cout << "--------------------------------------" << endl;
369 //
370 // End of Part 1 (determination of the parameters of the energy estimator)
371 //=========================================================================
372
373
374 ////////////////////////////////////////////////////////////////////////////
375 ////////////////////////////////////////////////////////////////////////////
376 ////////////////////////////////////////////////////////////////////////////
377 ////////////////////////////////////////////////////////////////////////////
378
379
380 //==========================================================================
381 // Start of Part 2 (test quality of energy estimation)
382 //
383 //
384
385 //--------------------------------------------
386 // Read the parameters of the energy estimator
387 //
388
389 TString paramin(outPath);
390 paramin += energyParName;
391
392 cout << "--------------------------------------" << endl;
393 cout << "Read parameters of energy estimator from file '"
394 << paramin << endl;
395 TFile enparam(paramin);
396
397 //=======================================================================
398 // Setup the event loop
399 //
400 cout << "--------------------------------------" << endl;
401 cout << "Setup event loop for checking quality of energy estimation" << endl;
402
403
404 MTaskList tlist2;
405 MParList parlist2;
406
407 //-----------------------------------------------
408 // Read events
409 //
410
411 TString inputfile(inPath);
412 inputfile += fileNameIn;
413
414 cout << "Read events from file '" << inputfile << "'" << endl;
415 MReadTree read2("Events");
416 read2.AddFile(inputfile);
417 read2.DisableAutoScheme();
418
419
420 //-----------------------------------------------
421 // Select events
422 //
423 cout << "Select events with hadronness < " << maxhadronness
424 << " and |alpha| < " << maxalpha << endl;
425 MFCT1SelFinal hcut2;
426 hcut2.SetCuts(maxhadronness, maxalpha);
427 hcut2.SetHadronnessName(hadronnessName);
428
429 MContinue cont(&hcut2);
430
431
432 //-----------------------------------------------
433 // Create some histograms to display the results
434 //
435
436 MH3 mh3ed ("log10(MMcEvt.fEnergy)", "MEnergyEst.fEnergy/MMcEvt.fEnergy-1.0");
437 MH3 mh3ed2("log10(MEnergyEst.fEnergy)", "MEnergyEst.fEnergy/MMcEvt.fEnergy-1.0");
438 MH3 mhe ("log10(MMcEvt.fEnergy)", "log10(MEnergyEst.fEnergy)");
439
440 mhe.SetName("HistEE");
441 mh3ed.SetName("HistEnergyDelta");
442 mh3ed2.SetName("HistEnergyDelta");
443
444 MBinning binsedx("BinningHistEnergyDeltaX");
445 MBinning binsedy("BinningHistEnergyDeltaY");
446 MBinning binseex("BinningHistEEX");
447 MBinning binseey("BinningHistEEY");
448
449 binsedx.SetEdges(35, 2.0, 5.5);
450 binsedy.SetEdges(40,-1.5, 2.5);
451
452 binseex.SetEdges(35, 2.0, 5.5);
453 binseey.SetEdges(35, 2.0, 5.5);
454
455 MFillH hfilled(&mh3ed);
456 MFillH hfilled2(&mh3ed2);
457 MFillH hfillee(&mhe);
458
459
460 //-----------------------------------------------
461 // Create energy estimator task, add necessary containers and
462 // initialize parameters read from file:
463 //
464
465 MEnergyEstParam eest2(hilName);
466 eest2.Add(hilSrcName);
467
468 TArrayD parA(5);
469 TArrayD parB(7);
470
471 for (Int_t i=0; i<parA.GetSize(); i++)
472 parA[i] = EnergyParams(i);
473 for (Int_t i=0; i<parB.GetSize(); i++)
474 parB[i] = EnergyParams(i+parA.GetSize());
475
476 eest2.SetCoeffA(parA);
477 eest2.SetCoeffB(parB);
478
479 enparam.Close();
480
481
482
483 //********************************
484 // Entries in MParList
485
486 parlist2.AddToList(&tlist2);
487
488 parlist2.AddToList(&binsedx);
489 parlist2.AddToList(&binsedy);
490 parlist2.AddToList(&binseex);
491 parlist2.AddToList(&binseey);
492
493
494 //********************************
495 // Entries in MTaskList
496
497 tlist2.AddToList(&read2);
498
499 tlist2.AddToList(&cont);
500 tlist2.AddToList(&eest2);
501
502 // tlist2.AddToList(new MPrint("MMcEvt"));
503 // tlist2.AddToList(new MPrint("MEnergyEst"));
504
505 tlist2.AddToList(&hfilled);
506 tlist2.AddToList(&hfilled2);
507 tlist2.AddToList(&hfillee);
508
509 //********************************
510
511 cout << "Event loop was setup" << endl;
512 MProgressBar bar;
513
514 MEvtLoop evtloop2;
515 evtloop2.SetProgressBar(&bar);
516 evtloop2.SetParList(&parlist2);
517
518 if (!evtloop2.Eventloop())
519 return;
520
521 TH1& histed = mh3ed.GetHist();
522 Float_t meany = histed.GetMean(2);
523 Float_t RMSy = histed.GetRMS(2);
524 Float_t resolution = sqrt( meany*meany + RMSy*RMSy );
525 cout << "" << endl;
526 cout << "With y = (E_EST -E_MC)/E_MC one obtains :" << endl;
527 cout << " Average y = " << histed.GetMean(2)
528 << ", RMS = " << histed.GetRMS(2)
529 << ", Average resolution = " << resolution << endl;
530 cout << "" << endl;
531
532 //=======================================================================
533
534 //
535 // Plot results:
536 //
537 cout << "--------------------------------------" << endl;
538 cout << "Plot the results" << endl;
539
540 mhe.GetHist()->GetXaxis()->SetTitle("log_{10} E_{MC} (GeV)");
541 mhe.GetHist()->GetYaxis()->SetTitle("log_{10} E_{EST} (GeV)");
542 mhe.GetHist()->GetXaxis()->SetTitleOffset(1.2);
543
544 TCanvas *c = new TCanvas("energy","CT1 Energy estimation");
545 c->Divide(2,2);
546 c->cd(1);
547 energy_1->SetBottomMargin(0.12);
548 mhe.DrawClone("PROFXnonew");
549
550 mh3ed.GetHist()->GetXaxis()->SetTitle("log_{10} E_{MC} (GeV)");
551 mh3ed.GetHist()->GetYaxis()->SetTitle("\\frac{E_{EST} - E_{MC}}{E_{MC}}");
552 mh3ed.GetHist()->GetXaxis()->SetTitleOffset(1.2);
553 mh3ed.GetHist()->GetYaxis()->SetTitleOffset(1.5);
554 c->cd(2);
555 energy_2->SetLeftMargin(0.15);
556 energy_2->SetBottomMargin(0.12);
557 mh3ed.DrawClone("PROFXnonew");
558
559 mh3ed2.GetHist()->GetXaxis()->SetTitle("log_{10} E_{EST} (GeV)");
560 mh3ed2.GetHist()->GetYaxis()->SetTitle("\\frac{E_{EST} - E_{MC}}{E_{MC}}");
561 mh3ed2.GetHist()->GetXaxis()->SetTitleOffset(1.2);
562 mh3ed2.GetHist()->GetYaxis()->SetTitleOffset(1.5);
563 c->cd(3);
564 energy_3->SetLeftMargin(0.15);
565 energy_3->SetBottomMargin(0.12);
566 mh3ed2.DrawClone("PROFXnonew");
567
568 ((TH2*)mh3ed2.GetHist())->ProjectionY("deltae");
569
570 c->cd(4);
571 energy_4->SetLeftMargin(0.1);
572 energy_4->SetRightMargin(0.05);
573 energy_4->SetBottomMargin(0.12);
574 deltae.GetXaxis()->SetTitleOffset(1.2);
575 deltae.GetXaxis()->SetTitle("(E_{EST} - E_{MC}) / E_{MC}");
576 deltae.Draw("e");
577
578 //-------------------------------------------
579 //
580 // include the histograms
581 // into the root file containing the parameters of the energy estimator
582 //
583
584
585 TString paramout(outPath);
586 paramout += energyParName;
587
588 TFile out2(paramout, "UPDATE");
589 ((TH2*)mh3ed.GetHist())->Write("mh3ed");
590 ((TH2*)mh3ed2.GetHist())->Write("mh3ed2");
591 ((TH2*)mhe.GetHist())->Write("mhe");
592 deltae.Write();
593 out2.Close();
594
595 cout << "Quality histograms were added onto the file '" << paramout << endl;
596
597 cout << "" << endl;
598 cout << "End of energy estimation part" << endl;
599 cout << "================================================================"
600 << endl;
601 //
602 // End of Part 2 (test quality of energy estimation)
603 //==========================================================================
604
605
606 return;
607
608}
609//============================================================================
610
611
612
613
614
615
616
617
618
Note: See TracBrowser for help on using the repository browser.