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

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