source: trunk/MagicSoft/Mars/mjobs/MJCalibTest.cc@ 6839

Last change on this file since 6839 was 6777, checked in by gaug, 20 years ago
*** empty log message ***
File size: 16.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-2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJCalibTest
28//
29// If the flag SetDataCheckDisplay() is set, only the most important distributions
30// are displayed.
31// Otherwise, (default: SetNormalDisplay()), a good selection of plots is given
32//
33/////////////////////////////////////////////////////////////////////////////
34#include "MJCalibTest.h"
35
36#include <TFile.h>
37#include <TStyle.h>
38#include <TCanvas.h>
39#include <TSystem.h>
40
41#include "MLog.h"
42#include "MLogManip.h"
43
44#include "MRunIter.h"
45#include "MParList.h"
46#include "MTaskList.h"
47#include "MTaskEnv.h"
48#include "MEvtLoop.h"
49
50#include "MHCamera.h"
51
52#include "MPedestalCam.h"
53#include "MPedPhotCam.h"
54#include "MBadPixelsCam.h"
55#include "MBadPixelsTreat.h"
56#include "MBadPixelsCalc.h"
57#include "MBadPixelsMerge.h"
58#include "MCerPhotEvt.h"
59#include "MArrivalTime.h"
60#include "MCalibrationChargeCam.h"
61#include "MCalibrationRelTimeCam.h"
62#include "MCalibrationQECam.h"
63#include "MCalibrationTestCam.h"
64#include "MCalibrationTestCalc.h"
65#include "MHCamEvent.h"
66#include "MHCalibrationTestCam.h"
67#include "MHCalibrationTestTimeCam.h"
68#include "MHCalibrationPix.h"
69
70#include "MReadMarsFile.h"
71#include "MRawFileRead.h"
72#include "MGeomApply.h"
73#include "MGeomCam.h"
74#include "MExtractTimeAndChargeSlidingWindow.h"
75#include "MExtractor.h"
76#include "MExtractTime.h"
77#include "MExtractTimeFastSpline.h"
78#include "MFCosmics.h"
79#include "MContinue.h"
80#include "MFillH.h"
81#include "MCalibrateData.h"
82#include "MCalibrateRelTimes.h"
83
84#include "MTriggerPattern.h"
85#include "MTriggerPatternDecode.h"
86#include "MFTriggerPattern.h"
87
88#include "MStatusDisplay.h"
89
90ClassImp(MJCalibTest);
91
92using namespace std;
93
94// --------------------------------------------------------------------------
95//
96// Default constructor.
97//
98// Sets fUseCosmicsFilter to kTRUE, fRuns to 0, fExtractor to NULL, fTimeExtractor to NULL
99// fDisplay to kNormalDisplay
100//
101MJCalibTest::MJCalibTest(const char *name, const char *title)
102 : fUseCosmicsFilter(kTRUE), fExtractor(NULL), fTimeExtractor(NULL),
103 fDisplayType(kNormalDisplay), fGeometry("MGeomCamMagic")
104{
105 fName = name ? name : "MJCalibTest";
106 fTitle = title ? title : "Tool to extract, calibrate and test signals from a file";
107}
108
109
110void MJCalibTest::DisplayResult(MParList &plist)
111{
112 if (!fDisplay)
113 return;
114
115 //
116 // Get container from list
117 //
118 MGeomCam &geomcam = *(MGeomCam*) plist.FindObject("MGeomCam");
119 MHCalibrationTestCam &testcam = *(MHCalibrationTestCam*)plist.FindObject("MHCalibrationTestCam");
120
121 // Create histograms to display
122 MHCamera disp1 (geomcam, "Test;PhotoElectrons", "Mean equiv. phes");
123 MHCamera disp2 (geomcam, "Test;SigmaPhes", "Sigma equiv.phes");
124 MHCamera disp3 (geomcam, "Test;PhesPerArea", "Equiv. Phes per Area");
125 MHCamera disp4 (geomcam, "Test;SigmaPhotPerArea", "Sigma equiv. Phes per Area");
126 MHCamera disp5 (geomcam, "Test;Phot", "Calibrated Phes from Fit");
127 MHCamera disp6 (geomcam, "Test;PhotPerArea", "Calibrated Phes per Area from Fit");
128 MHCamera disp7 (geomcam, "Test;NotInterpolate", "Not interpolated pixels");
129 MHCamera disp8 (geomcam, "Test;DeviatingPhots", "Deviating Number Phes");
130 MHCamera disp9 (geomcam, "Test;Arr.Times", "Mean of calibrated Arr.Times");
131 MHCamera disp10(geomcam, "Test;SigmaArr.Times", "Sigma of calibrated Arr.Times");
132
133 // Fitted charge means and sigmas
134 disp1.SetCamContent(testcam, 0);
135 disp1.SetCamError( testcam, 1);
136 disp2.SetCamContent(testcam, 2);
137 disp2.SetCamError( testcam, 3);
138 disp3.SetCamContent(testcam, 7);
139 disp3.SetCamError( testcam, 8);
140 disp4.SetCamContent(testcam, 9);
141 disp4.SetCamError( testcam, 10);
142
143 disp5.SetCamContent(fTestCam, 0);
144 disp5.SetCamError( fTestCam, 1);
145 disp6.SetCamContent(fTestCam, 2);
146 disp6.SetCamError( fTestCam, 3);
147 disp7.SetCamError( fTestCam, 4);
148
149 disp8.SetCamError( fBadPixels, 22);
150
151 disp9.SetCamContent(fTestTimeCam, 0);
152 disp9.SetCamError( fTestTimeCam, 1);
153 disp10.SetCamContent(fTestTimeCam, 2);
154 disp10.SetCamError( fTestTimeCam, 3);
155
156
157 disp1.SetYTitle("Phes");
158 disp2.SetYTitle("\\sigma_{phe}");
159 disp3.SetYTitle("Phes per area [mm^{-2}]");
160 disp4.SetYTitle("\\sigma_{phe} per area [mm^{-2}]");
161
162 disp5.SetYTitle("Phes");
163 disp6.SetYTitle("Phes per area [mm^{-2}]");
164 disp7.SetYTitle("[1]");
165 disp8.SetYTitle("[1]");
166
167 disp9.SetYTitle("Mean Arr.Times [FADC units]");
168 disp10.SetYTitle("\\sigma_{t} [FADC units]");
169
170 TCanvas &c = fDisplay->AddTab("TestPhes");
171 c.Divide(4,4);
172
173 disp1.CamDraw(c, 1, 4, 2, 1);
174 disp2.CamDraw(c, 2, 4, 2, 1);
175 disp3.CamDraw(c, 3, 4, 1, 1);
176 disp4.CamDraw(c, 4, 4, 2, 1);
177
178 /*
179
180 TCanvas &c2 = fDisplay->AddTab("TestResult");
181 c2.Divide(2,4);
182
183 disp5.CamDraw(c2, 1, 2, 2, 1);
184 disp6.CamDraw(c2, 2, 2, 2, 1);
185
186 */
187
188 TCanvas &c3 = fDisplay->AddTab("TestDefects");
189 c3.Divide(2,2);
190
191 disp7.CamDraw(c3, 1, 2, 0);
192 disp8.CamDraw(c3, 2, 2, 0);
193
194 //
195 // Display times
196 //
197 TCanvas &c4 = fDisplay->AddTab("TestTimes");
198 c4.Divide(2,4);
199
200 disp9.CamDraw(c4, 1, 2, 5, 1);
201 disp10.CamDraw(c4, 2, 2, 5, 1);
202
203 return;
204
205}
206
207// --------------------------------------------------------------------------
208//
209// Retrieve the output file written by WriteResult()
210//
211const char* MJCalibTest::GetOutputFile() const
212{
213 const TString name(GetOutputFileName());
214 if (name.IsNull())
215 return "";
216
217 return Form("%s/%s", fPathOut.Data(), name.Data());
218}
219
220
221const char* MJCalibTest::GetOutputFileName() const
222{
223
224 if (fSequence.IsValid())
225 return Form("calib%08d.root", fSequence.GetSequence());
226
227 if (!fRuns)
228 return "";
229
230 return Form("%s-F1.root", (const char*)fRuns->GetRunsAsFileName());
231
232}
233
234Bool_t MJCalibTest::ReadCalibration(TObjArray &l, MBadPixelsCam &cam, MExtractor* &ext1, MExtractor* &ext2, TString &geom) const
235{
236
237 const TString fname = GetOutputFile();
238
239 *fLog << inf << "Reading from file: " << fname << endl;
240
241 TFile file(fname, "READ");
242 if (!file.IsOpen())
243 {
244 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
245 return kFALSE;
246 }
247
248 TObject *o = file.Get("ExtractSignal");
249 if (o && !o->InheritsFrom(MExtractor::Class()))
250 {
251 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
252 return kFALSE;
253 }
254 ext1 = o ? (MExtractor*)o->Clone() : NULL;
255
256 o = file.Get("ExtractTime");
257 if (o && !o->InheritsFrom(MExtractor::Class()))
258 {
259 *fLog << err << dbginf << "ERROR - ExtractTime read from " << fname << " doesn't inherit from MExtractor!" << endl;
260 return kFALSE;
261 }
262 ext2 = o ? (MExtractor*)o->Clone() : NULL;
263 if (!ext1 && !ext2)
264 {
265 *fLog << err << dbginf << "ERROR - Neither ExtractSignal nor ExrtractTime found in " << fname << "!" << endl;
266 return kFALSE;
267 }
268
269 o = file.Get("MGeomCam");
270 if (o && !o->InheritsFrom(MGeomCam::Class()))
271 {
272 *fLog << err << dbginf << "ERROR - MGeomCam read from " << fname << " doesn't inherit from MGeomCam!" << endl;
273 return kFALSE;
274 }
275 geom = o ? o->ClassName() : "";
276
277 TObjArray cont(l);
278 cont.Add(&cam);
279 return ReadContainer(cont);
280}
281
282Bool_t MJCalibTest::ProcessFile(MPedestalCam &pedcam)
283{
284
285 if (!fSequence.IsValid())
286 {
287 if (!fRuns)
288 {
289 *fLog << err << "ERROR - Sequence invalid and no runs chosen!" << endl;
290 return kFALSE;
291 }
292
293 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
294 {
295 *fLog << err << "Number of files found doesn't match number of runs... abort."
296 << fRuns->GetNumRuns() << " vs. " << fRuns->GetNumEntries() << endl;
297 return kFALSE;
298 }
299 *fLog << "Calibrate data from ";
300 *fLog << "Runs " << fRuns->GetRunsAsString() << endl;
301 *fLog << endl;
302 }
303
304 CheckEnv();
305
306 *fLog << inf;
307 fLog->Separator(GetDescriptor());
308
309 *fLog << "Calibrate Calibration data from ";
310 if (fSequence.IsValid())
311 *fLog << "Sequence #" << fSequence.GetSequence() << endl;
312 else
313 *fLog << "Runs " << fRuns->GetRunsAsString() << endl;
314 *fLog << endl;
315
316 MDirIter iter;
317
318 if (fSequence.IsValid())
319 {
320 const Int_t n0 = fSequence.SetupCalRuns(iter, fPathData, "C", IsUseRawData());
321 const Int_t n1 = fSequence.GetNumCalRuns();
322 if (n0==0)
323 {
324 *fLog << err << "ERROR - No input files of sequence found!" << endl;
325 return kFALSE;
326 }
327 if (n0!=n1)
328 {
329 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
330 return kFALSE;
331 }
332 }
333
334 MCalibrationChargeCam calcam;
335 MCalibrationQECam qecam;
336 MCalibrationRelTimeCam tmcam;
337 MBadPixelsCam badpix;
338
339 TObjArray calibcont;
340 calibcont.Add(&calcam);
341 calibcont.Add(&qecam);
342 calibcont.Add(&tmcam);
343
344 MExtractor *extractor1=0;
345 MExtractor *extractor2=0;
346 TString geom;
347
348 if (!ReadCalibration(calibcont, badpix, extractor1, extractor2, geom))
349 {
350 *fLog << err << "Could not read calibration constants " << endl;
351 return kFALSE;
352 }
353
354 *fLog << all;
355 if (extractor1)
356 {
357 *fLog << underline << "Signal Extractor found in calibration file" << endl;
358 extractor1->Print();
359 *fLog << endl;
360 }
361 else
362 *fLog << inf << "No Signal Extractor: ExtractSignal in file." << endl;
363
364 if (extractor2)
365 {
366 *fLog << underline << "Time Extractor found in calibration file" << endl;
367 extractor2->Print();
368 *fLog << endl;
369 }
370 else
371 *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
372
373 if (!geom.IsNull())
374 *fLog << inf << "Camera geometry found in file: " << geom << endl;
375 else
376 *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
377
378 if (fExtractor)
379 extractor1 = fExtractor;
380 if (fTimeExtractor)
381 extractor2 = fTimeExtractor;
382
383 // Setup Lists
384 MParList plist;
385 plist.AddToList(this); // take care of fDisplay!
386 plist.AddToList(&fTestCam);
387 plist.AddToList(&fTestTimeCam);
388 plist.AddToList(&badpix);
389 plist.AddToList(&pedcam);
390 plist.AddToList(&calcam);
391 plist.AddToList(&qecam);
392 plist.AddToList(&tmcam);
393
394 MCerPhotEvt cerphot;
395 MPedPhotCam pedphot;
396 MHCalibrationTestCam testcam;
397
398 plist.AddToList(&cerphot);
399 plist.AddToList(&pedphot);
400 plist.AddToList(&testcam);
401
402 pedcam.SetName("MPedestalFundamental");
403
404 MTaskList tlist;
405 plist.AddToList(&tlist);
406
407 // Setup Task-lists
408 MRawFileRead rawread(NULL);
409 MReadMarsFile read("Events");
410 read.DisableAutoScheme();
411
412 if (IsUseRawData())
413 rawread.AddFiles(fSequence.IsValid() ? iter : *fRuns);
414 else
415 static_cast<MRead&>(read).AddFiles(fSequence.IsValid() ? iter : *fRuns);
416
417 // Check for interleaved events
418 MTriggerPatternDecode decode;
419 MFTriggerPattern fcalib;
420 fcalib.DenyCalibration();
421 MContinue conttp(&fcalib, "ContTrigPattern");
422
423 MGeomApply apply; // Only necessary to craete geometry
424 if (!geom.IsNull())
425 apply.SetGeometry(geom);
426 MBadPixelsMerge merge(&badpix);
427
428 MExtractTimeAndChargeSlidingWindow extrsw;
429 MExtractTimeFastSpline extime;
430 extime.SetPedestals(&pedcam);
431
432 MTaskEnv taskenv1("ExtractSignal");
433 MTaskEnv taskenv2("ExtractTime");
434
435 if (extractor1)
436 {
437 extractor1->SetPedestals(&pedcam);
438 taskenv1.SetDefault(extractor1);
439 }
440
441 if (extractor2)
442 {
443 fTimeExtractor->SetPedestals(&pedcam);
444 taskenv2.SetDefault(fTimeExtractor);
445 }
446 else if (!(extractor1->InheritsFrom("MExtractTimeAndCharge")))
447 {
448 extrsw.SetPedestals(&pedcam);
449 extrsw.SetWindowSize(8,8);
450 taskenv2.SetDefault(&extrsw);
451 *fLog << warn << GetDescriptor()
452 << ": No extractor has been chosen, take default MExtractTimeAndChargeSlidingWindow " << endl;
453 }
454
455
456 MCalibrateData photcalc;
457 photcalc.AddPedestal("Fundamental");
458 photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
459 photcalc.SetPedestalFlag(MCalibrateData::kEvent);
460 photcalc.SetSignalType(MCalibrateData::kPhe);
461
462 MCalibrateRelTimes caltimes;
463 MBadPixelsCalc badcalc;
464 MBadPixelsTreat badtreat;
465 badtreat.SetProcessTimes(kFALSE);
466
467 badcalc.SetNamePedPhotCam("MPedPhotFundamental");
468 badtreat.SetUseInterpolation();
469 badtreat.AddNamePedPhotCam("MPedPhotFundamental");
470
471 MCalibrationTestCalc testcalc;
472
473 if (!fSequence.IsValid())
474 {
475 testcalc.SetOutputPath(fPathOut);
476 testcalc.SetOutputFile(Form("%s-TestCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
477 }
478
479 MHCamEvent evt0(0,"Signal", "Un-Calibrated Signal;;S [FADC cnts]" );
480 MHCamEvent evt1(0,"CalSig", "Cal. and Interp. Sig. by Pixel Size Ratio;;S [phe]");
481 MHCamEvent evt2(0,"Times" , "Arrival Time;;T [slice]");
482
483 MFillH fill0(&evt0, "MExtractedSignalCam", "FillUncalibrated");
484 MFillH fill1(&evt1, "MCerPhotEvt", "FillCalibrated");
485 MFillH fill2(&evt2, "MArrivalTime","FillTimes");
486
487 MFillH fillcam("MHCalibrationTestCam", "MCerPhotEvt" ,"FillTest");
488 MFillH filltme("MHCalibrationTestTimeCam", "MArrivalTime","FillTestTime");
489 fillcam.SetBit(MFillH::kDoNotDisplay);
490 filltme.SetBit(MFillH::kDoNotDisplay);
491
492 MFCosmics cosmics;
493 cosmics.SetNamePedestalCam("MPedestalFundamental");
494 MContinue contcos(&cosmics,"ContCosmics");
495
496 tlist.AddToList(&read);
497 tlist.AddToList(&decode);
498 tlist.AddToList(&apply);
499 tlist.AddToList(&merge);
500 // tlist.AddToList(&conttp);
501 tlist.AddToList(&taskenv1);
502 if (!extractor1->InheritsFrom("MExtractTimeAndCharge"))
503 tlist.AddToList(&taskenv2);
504
505 tlist.AddToList(&contcos);
506 tlist.AddToList(&fill0);
507 tlist.AddToList(&photcalc);
508 tlist.AddToList(&caltimes);
509 tlist.AddToList(&badcalc);
510 tlist.AddToList(&badtreat);
511 tlist.AddToList(&fill1);
512 tlist.AddToList(&fill2);
513 tlist.AddToList(&fillcam);
514 tlist.AddToList(&filltme);
515 tlist.AddToList(&testcalc);
516
517 // Create and setup the eventloop
518 MEvtLoop evtloop(fName);
519 evtloop.SetParList(&plist);
520 evtloop.SetDisplay(fDisplay);
521 evtloop.SetLogStream(fLog);
522
523 // Execute first analysis
524 if (!evtloop.Eventloop())
525 {
526 *fLog << err << GetDescriptor() << ": Failed." << endl;
527 return kFALSE;
528 }
529
530 tlist.PrintStatistics();
531
532 if (fIsPixelCheck)
533 {
534 MHCalibrationTestCam *hcam = (MHCalibrationTestCam*)plist.FindObject("MHCalibrationTestCam");
535 MHCalibrationPix &pix1 = (*hcam)[fCheckedPixId];
536 pix1.DrawClone("");
537
538 MHCalibrationTestTimeCam *hccam = (MHCalibrationTestTimeCam*)plist.FindObject("MHCalibrationTestTimeCam");
539 MHCalibrationPix &pix11 = (*hccam)[fCheckedPixId];
540 pix11.DrawClone("");
541 }
542
543 DisplayResult(plist);
544
545 if (!WriteResult())
546 return kFALSE;
547
548 *fLog << inf << GetDescriptor() << ": Done." << endl;
549
550 return kTRUE;
551}
552
553Bool_t MJCalibTest::WriteResult()
554{
555
556 if (fPathOut.IsNull())
557 return kTRUE;
558
559 const TString oname(GetOutputFile());
560
561 *fLog << inf << "Writing to file: " << oname << endl;
562
563 TFile file(oname, "UPDATE");
564
565 if (fDisplay && fDisplay->Write()<=0)
566 {
567 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
568 return kFALSE;
569 }
570
571 if (fTestCam.Write()<=0)
572 {
573 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
574 return kFALSE;
575 }
576
577 if (fTestTimeCam.Write()<=0)
578 {
579 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
580 return kFALSE;
581 }
582
583 return kTRUE;
584
585}
586
Note: See TracBrowser for help on using the repository browser.