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

Last change on this file since 4621 was 4559, checked in by tonello, 20 years ago
*** empty log message ***
File size: 13.1 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 MCalibrationTestCalc testcalc;
265 testcalc.SetOutputPath(fOutputPath);
266 testcalc.SetOutputFile(Form("%s-TestCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
267
268 MHCamEvent evt("ExtSignal");
269 evt.SetType(0);
270 MFillH fill(&evt, "MExtractedSignalCam");
271
272 MFillH fillcam("MHCalibrationTestCam", "MCerPhotEvt");
273 fillcam.SetNameTab("Test");
274
275 MFCosmics cosmics;
276 MContinue cont(&cosmics);
277
278 tlist.AddToList(&read);
279 tlist.AddToList(&apply);
280
281 if (fExtractor)
282 tlist.AddToList(fExtractor);
283 else
284 {
285 *fLog << warn << GetDescriptor()
286 << ": No extractor has been chosen, take default MExtractSlidingWindow " << endl;
287 tlist.AddToList(&extract2);
288 }
289
290
291 if (fUseCosmicsFilter)
292 tlist.AddToList(&cont);
293
294 tlist.AddToList(&fill);
295 tlist.AddToList(&photcalc);
296 tlist.AddToList(&pedphotcalc);
297 tlist.AddToList(&badtreat);
298 tlist.AddToList(&fillcam);
299 tlist.AddToList(&testcalc);
300
301 // Create and setup the eventloop
302 MEvtLoop evtloop(fName);
303 evtloop.SetParList(&plist);
304 evtloop.SetDisplay(fDisplay);
305 evtloop.SetLogStream(fLog);
306
307 // Execute first analysis
308 if (!evtloop.Eventloop())
309 {
310 *fLog << err << GetDescriptor() << ": Failed." << endl;
311 return kFALSE;
312 }
313
314 tlist.PrintStatistics();
315
316 DisplayResult(plist);
317
318 if (!WriteResultD())
319 return kFALSE;
320
321 *fLog << inf << GetDescriptor() << ": Done." << endl;
322
323 return kTRUE;
324}
325
326Bool_t MJExtractCalibTest::ProcessT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
327{
328
329// const TString fname = GetOutputFile();
330
331// if (gSystem->AccessPathName(fname, kFileExists))
332 return ProcessFileT(pedcam,relcam);
333
334// return kTRUE;
335}
336
337Bool_t MJExtractCalibTest::ProcessFileT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
338{
339
340 if (!fRuns)
341 {
342 *fLog << err << "No Runs choosen... abort." << endl;
343 return kFALSE;
344 }
345 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
346 {
347 *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
348 return kFALSE;
349 }
350
351 *fLog << inf;
352 fLog->Separator(GetDescriptor());
353 *fLog << "Calculate MExtractedSignalCam from Runs " << fRuns->GetRunsAsString() << endl;
354 *fLog << endl;
355
356 MArrivalTime arrtime;
357
358 // Setup Lists
359 MParList plist;
360 plist.AddToList(&pedcam);
361 plist.AddToList(&relcam);
362 plist.AddToList(&arrtime);
363 plist.AddToList(&fTestTimeCam);
364 plist.AddToList(&fBadPixels);
365
366 MTaskList tlist;
367 plist.AddToList(&tlist);
368
369 // Setup Task-lists
370 MReadMarsFile read("Events");
371 read.DisableAutoScheme();
372 static_cast<MRead&>(read).AddFiles(*fRuns);
373
374 MGeomApply apply; // Only necessary to craete geometry
375 MExtractTimeFastSpline extract;
376 MExtractSlidingWindow extcharge; // Only for the cosmics filter
377 MCalibrateRelTimes timecalc;
378 MFCosmics cosmics;
379 MContinue cont(&cosmics);
380
381 MHCamEvent evt("ExtTimes");
382 evt.SetType(0);
383 MFillH fill(&evt, "MArrivalTimeCam");
384
385 MFillH fillcam("MHCalibrationTestTimeCam", "MArrivalTime");
386 fillcam.SetNameTab("TestTime");
387
388 tlist.AddToList(&read);
389 tlist.AddToList(&apply);
390
391 if (fTimeExtractor)
392 tlist.AddToList(fTimeExtractor);
393 else
394 {
395 *fLog << warn << GetDescriptor()
396 << ": No extractor has been chosen, take default MExtractTimeFastSpline " << endl;
397 tlist.AddToList(&extract);
398 }
399
400 tlist.AddToList(&extcharge);
401 tlist.AddToList(&cont);
402 tlist.AddToList(&fill);
403 tlist.AddToList(&timecalc);
404 tlist.AddToList(&fillcam);
405
406 // Create and setup the eventloop
407 MEvtLoop evtloop(fName);
408 evtloop.SetParList(&plist);
409 evtloop.SetDisplay(fDisplay);
410 evtloop.SetLogStream(fLog);
411
412 // Execute first analysis
413 if (!evtloop.Eventloop())
414 {
415 *fLog << err << GetDescriptor() << ": Failed." << endl;
416 return kFALSE;
417 }
418
419 tlist.PrintStatistics();
420
421 DisplayResultT(plist);
422
423 if (!WriteResultT())
424 return kFALSE;
425
426 *fLog << inf << GetDescriptor() << ": Done." << endl;
427
428 return kTRUE;
429}
430
431
432Bool_t MJExtractCalibTest::ReadPedPhotCam()
433{
434
435 const TString fname = GetOutputFile();
436
437 if (gSystem->AccessPathName(fname, kFileExists))
438 {
439 *fLog << err << "Input file " << fname << " doesn't exist." << endl;
440 return kFALSE;
441 }
442
443 *fLog << inf << "Reading from file: " << fname << endl;
444
445 TFile file(fname, "READ");
446 if (fPedPhotCam.Read()<=0)
447 {
448 *fLog << "Unable to read MPedPhotCam from " << fname << endl;
449 return kFALSE;
450 }
451
452 if (file.FindKey("MBadPixelsCam"))
453 {
454 MBadPixelsCam bad;
455 if (bad.Read()<=0)
456 {
457 *fLog << "Unable to read MBadPixelsCam from " << fname << endl;
458 return kFALSE;
459 }
460 fBadPixels.Merge(bad);
461 }
462
463 if (fDisplay /*&& !fDisplay->GetCanvas("Pedestals")*/) // FIXME!
464 fDisplay->Read();
465
466 return kTRUE;
467}
468
469Bool_t MJExtractCalibTest::WriteResultD()
470{
471
472 if (fOutputPath.IsNull())
473 return kTRUE;
474
475 const TString oname(GetOutputFile());
476
477 *fLog << inf << "Writing to file: " << oname << endl;
478
479 TFile file(oname, "UPDATE");
480
481 if (fDisplay && fDisplay->Write()<=0)
482 {
483 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
484 return kFALSE;
485 }
486
487 if (fPedPhotCam.Write()<=0)
488 {
489 *fLog << err << "Unable to write MPedPhotCam to " << oname << endl;
490 return kFALSE;
491 }
492
493 if (fTestCam.Write()<=0)
494 {
495 *fLog << err << "Unable to write MHCalibrationTestCam to " << oname << endl;
496 return kFALSE;
497 }
498
499 return kTRUE;
500
501}
502
503Bool_t MJExtractCalibTest::WriteResultT()
504{
505
506 if (fOutputPath.IsNull())
507 return kTRUE;
508
509 const TString oname(GetOutputFile());
510
511 *fLog << inf << "Writing to file: " << oname << endl;
512
513 TFile file(oname, "UPDATE");
514
515 if (fDisplay && fDisplay->Write()<=0)
516 {
517 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
518 return kFALSE;
519 }
520
521 if (fTestTimeCam.Write()<=0)
522 {
523 *fLog << err << "Unable to write MHCalibrationTestTimeCam to " << oname << endl;
524 return kFALSE;
525 }
526
527 return kTRUE;
528
529}
530
531
Note: See TracBrowser for help on using the repository browser.