source: trunk/MagicSoft/Mars/mjobs/MJExtractCalibTest.cc@ 4542

Last change on this file since 4542 was 4542, checked in by gaug, 20 years ago
*** empty log message ***
File size: 13.2 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): Markus Gaug, 04/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJExtractCalibTest
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MJExtractCalibTest.h"
31
32#include <TFile.h>
33#include <TStyle.h>
34#include <TCanvas.h>
35#include <TSystem.h>
36
37#include "MLog.h"
38#include "MLogManip.h"
39
40#include "MRunIter.h"
41#include "MParList.h"
42#include "MTaskList.h"
43#include "MEvtLoop.h"
44
45#include "MHCamera.h"
46
47#include "MPedestalCam.h"
48#include "MBadPixelsCam.h"
49#include "MBadPixelsTreat.h"
50#include "MCerPhotEvt.h"
51#include "MArrivalTime.h"
52#include "MCalibrationChargeCam.h"
53#include "MCalibrationRelTimeCam.h"
54#include "MCalibrationQECam.h"
55#include "MCalibrationTestCalc.h"
56#include "MHCamEvent.h"
57
58#include "MReadMarsFile.h"
59#include "MGeomApply.h"
60#include "MExtractSlidingWindow.h"
61#include "MExtractor.h"
62#include "MExtractTime.h"
63#include "MExtractTimeFastSpline.h"
64#include "MFCosmics.h"
65#include "MContinue.h"
66#include "MFillH.h"
67#include "MCalibrate.h"
68#include "MCalibrateRelTimes.h"
69#include "MPedPhotCalc.h"
70
71#include "MStatusDisplay.h"
72
73ClassImp(MJExtractCalibTest);
74
75using namespace std;
76// --------------------------------------------------------------------------
77//
78// Default constructor.
79//
80// Sets fUseCosmicsFilter to kTRUE, fRuns to 0, fExtractor to NULL, fTimeExtractor to NULL
81//
82MJExtractCalibTest::MJExtractCalibTest(const char *name, const char *title)
83 : fUseCosmicsFilter(kTRUE), fRuns(NULL), fExtractor(NULL), fTimeExtractor(NULL)
84{
85 fName = name ? name : "MJExtractCalibTest";
86 fTitle = title ? title : "Tool to extract, calibrate and test signals from a file";
87}
88
89
90void MJExtractCalibTest::DisplayResult(MParList &plist)
91{
92 if (!fDisplay)
93 return;
94
95 //
96 // Update display
97 //
98 TString title = fDisplay->GetTitle();
99 title += "-- Extraction-Calibration-Test ";
100 title += fRuns->GetRunsAsString();
101 title += " --";
102 fDisplay->SetTitle(title);
103
104 //
105 // Get container from list
106 //
107 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
108
109 // Create histograms to display
110 MHCamera disp1 (geomcam, "Test;Photons", "Mean of calibrated Photons");
111 MHCamera disp2 (geomcam, "Test;SigmaPhotons", "Sigma of calibrated photons");
112 MHCamera disp3 (geomcam, "Test;PhotonsPerArea", "Equiv. Cherenkov Photons per Area");
113 MHCamera disp4 (geomcam, "Test;SigmaPhotPerArea", "Sigma equiv. Cher. Photons per Area");
114
115 // Fitted charge means and sigmas
116 disp1.SetCamContent(fTestCam, 0);
117 disp1.SetCamError( fTestCam, 1);
118 disp2.SetCamContent(fTestCam, 2);
119 disp2.SetCamError( fTestCam, 3);
120 disp3.SetCamContent(fTestCam, 7);
121 disp3.SetCamError( fTestCam, 8);
122 disp4.SetCamContent(fTestCam, 9);
123 disp4.SetCamError( fTestCam, 10);
124
125 disp1.SetYTitle("Photons");
126 disp2.SetYTitle("\\sigma_{phot}");
127 disp3.SetYTitle("Photons per Area [mm^{-2}]");
128 disp4.SetYTitle("\\sigma_{phot} per Area [mm^{-2}]");
129
130 gStyle->SetOptStat(1111);
131 gStyle->SetOptFit();
132
133 TCanvas &c = fDisplay->AddTab("TestCharges");
134 c.Divide(4,4);
135
136 disp1.CamDraw(c, 1, 4, 2, 1);
137 disp2.CamDraw(c, 2, 4, 2, 1);
138 disp3.CamDraw(c, 3, 4, 1, 1);
139 disp4.CamDraw(c, 4, 4, 2, 1);
140
141 return;
142
143}
144
145
146void MJExtractCalibTest::DisplayResultT(MParList &plist)
147{
148 if (!fDisplay)
149 return;
150
151 //
152 // Update display
153 //
154 TString title = fDisplay->GetTitle();
155 title += "-- Extraction-Calibration-Test-Time";
156 title += fRuns->GetRunsAsString();
157 title += " --";
158 fDisplay->SetTitle(title);
159
160 //
161 // Get container from list
162 //
163 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
164
165 // Create histograms to display
166 MHCamera disp1 (geomcam, "Test;Arr.Times", "Mean of calibrated Arr.Times");
167 MHCamera disp2 (geomcam, "Test;SigmaArr.Times", "Sigma of calibrated Arr.Times");
168
169 // Fitted charge means and sigmas
170 disp1.SetCamContent(fTestTimeCam, 0);
171 disp1.SetCamError( fTestTimeCam, 1);
172 disp2.SetCamContent(fTestTimeCam, 2);
173 disp2.SetCamError( fTestTimeCam, 3);
174
175 disp1.SetYTitle("Mean Arr.Times [FADC units]");
176 disp2.SetYTitle("\\sigma_{t} [FADC units]");
177
178 gStyle->SetOptStat(1111);
179 gStyle->SetOptFit();
180
181 TCanvas &c = fDisplay->AddTab("TestTimes");
182 c.Divide(2,4);
183
184 disp1.CamDraw(c, 1, 2, 5, 1);
185 disp2.CamDraw(c, 2, 2, 5, 1);
186
187 return;
188
189}
190
191
192void MJExtractCalibTest::SetOutputPath(const char *path)
193{
194 fOutputPath = path;
195 if (fOutputPath.EndsWith("/"))
196 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
197}
198
199const char* MJExtractCalibTest::GetOutputFile() const
200{
201 if (!fRuns)
202 return "";
203
204 return Form("%s/%s-Test.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
205}
206
207
208Bool_t MJExtractCalibTest::ProcessD(MPedestalCam &pedcam, MCalibrationChargeCam &calcam, MCalibrationQECam &qecam)
209{
210 // const TString fname = GetOutputFile();
211
212// if (gSystem->AccessPathName(fname, kFileExists))
213 return ProcessFileD(pedcam,calcam,qecam);
214
215 // return kTRUE;
216}
217
218Bool_t MJExtractCalibTest::ProcessFileD(MPedestalCam &pedcam, MCalibrationChargeCam &calcam, MCalibrationQECam &qecam)
219{
220 if (!fRuns)
221 {
222 *fLog << err << "No Runs choosen... abort." << endl;
223 return kFALSE;
224 }
225 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
226 {
227 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
228 return kFALSE;
229 }
230
231 *fLog << inf;
232 fLog->Separator(GetDescriptor());
233 *fLog << "Calculate MExtractedSignalCam from Runs " << fRuns->GetRunsAsString() << endl;
234 *fLog << endl;
235
236 MCerPhotEvt cerphot;
237 MPedPhotCam pedphot;
238
239 // Setup Lists
240 MParList plist;
241 plist.AddToList(&pedcam);
242 plist.AddToList(&calcam);
243 plist.AddToList(&qecam);
244 plist.AddToList(&cerphot);
245 plist.AddToList(&pedphot);
246 plist.AddToList(&fTestCam);
247 plist.AddToList(&fBadPixels);
248
249 MTaskList tlist;
250 plist.AddToList(&tlist);
251
252 // Setup Task-lists
253 MReadMarsFile read("Events");
254 read.DisableAutoScheme();
255 static_cast<MRead&>(read).AddFiles(*fRuns);
256
257 MGeomApply apply; // Only necessary to craete geometry
258 MExtractSlidingWindow extract2;
259 MCalibrate photcalc;
260 photcalc.SetCalibrationMode(MCalibrate::kFfactor);
261 MPedPhotCalc pedphotcalc;
262 MBadPixelsTreat badtreat;
263 badtreat.SetUseInterpolation();
264 badtreat.SetSloppyTreatment();
265 MCalibrationTestCalc testcalc;
266 testcalc.SetOutputPath(fOutputPath);
267 testcalc.SetOutputFile(Form("%s-TestCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
268
269 MHCamEvent evt("ExtSignal");
270 evt.SetType(0);
271 MFillH fill(&evt, "MExtractedSignalCam");
272
273 MFillH fillcam("MHCalibrationTestCam", "MCerPhotEvt");
274 fillcam.SetNameTab("Test");
275
276 MFCosmics cosmics;
277 MContinue cont(&cosmics);
278
279 tlist.AddToList(&read);
280 tlist.AddToList(&apply);
281
282 if (fExtractor)
283 tlist.AddToList(fExtractor);
284 else
285 {
286 *fLog << warn << GetDescriptor()
287 << ": No extractor has been chosen, take default MExtractSlidingWindow " << endl;
288 tlist.AddToList(&extract2);
289 }
290
291
292 if (fUseCosmicsFilter)
293 tlist.AddToList(&cont);
294
295 tlist.AddToList(&fill);
296 tlist.AddToList(&photcalc);
297 tlist.AddToList(&pedphotcalc);
298 tlist.AddToList(&badtreat);
299 tlist.AddToList(&fillcam);
300 tlist.AddToList(&testcalc);
301
302 // Create and setup the eventloop
303 MEvtLoop evtloop(fName);
304 evtloop.SetParList(&plist);
305 evtloop.SetDisplay(fDisplay);
306 evtloop.SetLogStream(fLog);
307
308 // Execute first analysis
309 if (!evtloop.Eventloop())
310 {
311 *fLog << err << GetDescriptor() << ": Failed." << endl;
312 return kFALSE;
313 }
314
315 tlist.PrintStatistics();
316
317 DisplayResult(plist);
318
319 if (!WriteResultD())
320 return kFALSE;
321
322 *fLog << inf << GetDescriptor() << ": Done." << endl;
323
324 return kTRUE;
325}
326
327Bool_t MJExtractCalibTest::ProcessT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
328{
329
330// const TString fname = GetOutputFile();
331
332// if (gSystem->AccessPathName(fname, kFileExists))
333 return ProcessFileT(pedcam,relcam);
334
335// return kTRUE;
336}
337
338Bool_t MJExtractCalibTest::ProcessFileT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
339{
340
341 if (!fRuns)
342 {
343 *fLog << err << "No Runs choosen... abort." << endl;
344 return kFALSE;
345 }
346 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
347 {
348 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
349 return kFALSE;
350 }
351
352 *fLog << inf;
353 fLog->Separator(GetDescriptor());
354 *fLog << "Calculate MExtractedSignalCam from Runs " << fRuns->GetRunsAsString() << endl;
355 *fLog << endl;
356
357 MArrivalTime arrtime;
358
359 // Setup Lists
360 MParList plist;
361 plist.AddToList(&pedcam);
362 plist.AddToList(&relcam);
363 plist.AddToList(&arrtime);
364 plist.AddToList(&fTestTimeCam);
365 plist.AddToList(&fBadPixels);
366
367 MTaskList tlist;
368 plist.AddToList(&tlist);
369
370 // Setup Task-lists
371 MReadMarsFile read("Events");
372 read.DisableAutoScheme();
373 static_cast<MRead&>(read).AddFiles(*fRuns);
374
375 MGeomApply apply; // Only necessary to craete geometry
376 MExtractTimeFastSpline extract;
377 MExtractSlidingWindow extcharge; // Only for the cosmics filter
378 MCalibrateRelTimes timecalc;
379 MFCosmics cosmics;
380 MContinue cont(&cosmics);
381
382 MHCamEvent evt("ExtTimes");
383 evt.SetType(0);
384 MFillH fill(&evt, "MArrivalTimeCam");
385
386 MFillH fillcam("MHCalibrationTestTimeCam", "MArrivalTime");
387 fillcam.SetNameTab("TestTime");
388
389 tlist.AddToList(&read);
390 tlist.AddToList(&apply);
391
392 if (fTimeExtractor)
393 tlist.AddToList(fTimeExtractor);
394 else
395 {
396 *fLog << warn << GetDescriptor()
397 << ": No extractor has been chosen, take default MExtractTimeFastSpline " << endl;
398 tlist.AddToList(&extract);
399 }
400
401 tlist.AddToList(&extcharge);
402 tlist.AddToList(&cont);
403 tlist.AddToList(&fill);
404 tlist.AddToList(&timecalc);
405 tlist.AddToList(&fillcam);
406
407 // Create and setup the eventloop
408 MEvtLoop evtloop(fName);
409 evtloop.SetParList(&plist);
410 evtloop.SetDisplay(fDisplay);
411 evtloop.SetLogStream(fLog);
412
413 // Execute first analysis
414 if (!evtloop.Eventloop())
415 {
416 *fLog << err << GetDescriptor() << ": Failed." << endl;
417 return kFALSE;
418 }
419
420 tlist.PrintStatistics();
421
422 DisplayResultT(plist);
423
424 if (!WriteResultT())
425 return kFALSE;
426
427 *fLog << inf << GetDescriptor() << ": Done." << endl;
428
429 return kTRUE;
430}
431
432
433Bool_t MJExtractCalibTest::ReadPedPhotCam()
434{
435
436 const TString fname = GetOutputFile();
437
438 if (gSystem->AccessPathName(fname, kFileExists))
439 {
440 *fLog << err << "Input file " << fname << " doesn't exist." << endl;
441 return kFALSE;
442 }
443
444 *fLog << inf << "Reading from file: " << fname << endl;
445
446 TFile file(fname, "READ");
447 if (fPedPhotCam.Read()<=0)
448 {
449 *fLog << "Unable to read MPedPhotCam from " << fname << endl;
450 return kFALSE;
451 }
452
453 if (file.FindKey("MBadPixelsCam"))
454 {
455 MBadPixelsCam bad;
456 if (bad.Read()<=0)
457 {
458 *fLog << "Unable to read MBadPixelsCam from " << fname << endl;
459 return kFALSE;
460 }
461 fBadPixels.Merge(bad);
462 }
463
464 if (fDisplay /*&& !fDisplay->GetCanvas("Pedestals")*/) // FIXME!
465 fDisplay->Read();
466
467 return kTRUE;
468}
469
470Bool_t MJExtractCalibTest::WriteResultD()
471{
472
473 if (fOutputPath.IsNull())
474 return kTRUE;
475
476 const TString oname(GetOutputFile());
477
478 *fLog << inf << "Writing to file: " << oname << endl;
479
480 TFile file(oname, "UPDATE");
481
482 if (fDisplay && fDisplay->Write()<=0)
483 {
484 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
485 return kFALSE;
486 }
487
488 if (fPedPhotCam.Write()<=0)
489 {
490 *fLog << err << "Unable to write MPedPhotCam to " << oname << endl;
491 return kFALSE;
492 }
493
494 if (fTestCam.Write()<=0)
495 {
496 *fLog << err << "Unable to write MHCalibrationTestCam to " << oname << endl;
497 return kFALSE;
498 }
499
500 return kTRUE;
501
502}
503
504Bool_t MJExtractCalibTest::WriteResultT()
505{
506
507 if (fOutputPath.IsNull())
508 return kTRUE;
509
510 const TString oname(GetOutputFile());
511
512 *fLog << inf << "Writing to file: " << oname << endl;
513
514 TFile file(oname, "UPDATE");
515
516 if (fDisplay && fDisplay->Write()<=0)
517 {
518 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
519 return kFALSE;
520 }
521
522 if (fTestTimeCam.Write()<=0)
523 {
524 *fLog << err << "Unable to write MHCalibrationTestTimeCam to " << oname << endl;
525 return kFALSE;
526 }
527
528 return kTRUE;
529
530}
531
532
Note: See TracBrowser for help on using the repository browser.