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

Last change on this file since 8672 was 8498, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.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-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 "MExtractTimeAndChargeSpline.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, 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 return Form("%s/calib%08d.root", fPathOut.Data(), fSequence.GetSequence());
213}
214
215Bool_t MJCalibTest::ReadCalibration(TObjArray &l, MBadPixelsCam &cam, MExtractor* &ext1, MExtractor* &ext2, TString &geom) const
216{
217
218 const TString fname = GetOutputFile();
219
220 *fLog << inf << "Reading from file: " << fname << endl;
221
222 TFile file(fname, "READ");
223 if (!file.IsOpen())
224 {
225 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
226 return kFALSE;
227 }
228
229 TObject *o = file.Get("ExtractSignal");
230 if (o && !o->InheritsFrom(MExtractor::Class()))
231 {
232 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
233 return kFALSE;
234 }
235 ext1 = o ? (MExtractor*)o->Clone() : NULL;
236
237 o = file.Get("ExtractTime");
238 if (o && !o->InheritsFrom(MExtractor::Class()))
239 {
240 *fLog << err << dbginf << "ERROR - ExtractTime read from " << fname << " doesn't inherit from MExtractor!" << endl;
241 return kFALSE;
242 }
243 ext2 = o ? (MExtractor*)o->Clone() : NULL;
244 if (!ext1 && !ext2)
245 {
246 *fLog << err << dbginf << "ERROR - Neither ExtractSignal nor ExrtractTime found in " << fname << "!" << endl;
247 return kFALSE;
248 }
249
250 o = file.Get("MGeomCam");
251 if (o && !o->InheritsFrom(MGeomCam::Class()))
252 {
253 *fLog << err << dbginf << "ERROR - MGeomCam read from " << fname << " doesn't inherit from MGeomCam!" << endl;
254 return kFALSE;
255 }
256 geom = o ? o->ClassName() : "";
257
258 TObjArray cont(l);
259 cont.Add(&cam);
260 return ReadContainer(cont);
261}
262
263Bool_t MJCalibTest::Process(MPedestalCam &pedcam)
264{
265 if (!fSequence.IsValid())
266 {
267 *fLog << err << "ERROR - Sequence invalid..." << endl;
268 return kFALSE;
269 }
270
271 *fLog << inf;
272 fLog->Separator(GetDescriptor());
273 *fLog << "Calculate calibration test from Sequence #";
274 *fLog << fSequence.GetSequence() << endl << endl;
275
276 CheckEnv();
277
278 *fLog << inf;
279 fLog->Separator(GetDescriptor());
280
281 *fLog << "Calibrate Calibration data from ";
282 *fLog << "Sequence #" << fSequence.GetSequence() << endl;
283 *fLog << endl;
284
285 MDirIter iter;
286
287 if (fSequence.IsValid())
288 {
289 if (fSequence.SetupCalRuns(iter, 0, !fSequence.IsMonteCarlo())<=0)
290 return kFALSE;
291 }
292
293 MCalibrationChargeCam calcam;
294 MCalibrationQECam qecam;
295 MCalibrationRelTimeCam tmcam;
296 MBadPixelsCam badpix;
297
298 TObjArray calibcont;
299 calibcont.Add(&calcam);
300 calibcont.Add(&qecam);
301 calibcont.Add(&tmcam);
302
303 MExtractor *extractor1=0;
304 MExtractor *extractor2=0;
305 TString geom;
306
307 if (!ReadCalibration(calibcont, badpix, extractor1, extractor2, geom))
308 {
309 *fLog << err << "Could not read calibration constants " << endl;
310 return kFALSE;
311 }
312
313 *fLog << all;
314 if (extractor1)
315 {
316 *fLog << underline << "Signal Extractor found in calibration file" << endl;
317 extractor1->Print();
318 *fLog << endl;
319 }
320 else
321 *fLog << inf << "No Signal Extractor: ExtractSignal in file." << endl;
322
323 if (extractor2)
324 {
325 *fLog << underline << "Time Extractor found in calibration file" << endl;
326 extractor2->Print();
327 *fLog << endl;
328 }
329 else
330 *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
331
332 if (!geom.IsNull())
333 *fLog << inf << "Camera geometry found in file: " << geom << endl;
334 else
335 *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
336
337 if (fExtractor)
338 extractor1 = fExtractor;
339 if (fTimeExtractor)
340 extractor2 = fTimeExtractor;
341
342 // Setup Lists
343 MParList plist;
344 plist.AddToList(this); // take care of fDisplay!
345 plist.AddToList(&fTestCam);
346 plist.AddToList(&fTestTimeCam);
347 plist.AddToList(&badpix);
348 plist.AddToList(&pedcam);
349 plist.AddToList(&calcam);
350 plist.AddToList(&qecam);
351 plist.AddToList(&tmcam);
352
353 MSignalCam cerphot;
354 MPedPhotCam pedphot;
355 MHCalibrationTestCam testcam;
356
357 plist.AddToList(&cerphot);
358 plist.AddToList(&pedphot);
359 plist.AddToList(&testcam);
360
361 pedcam.SetName("MPedestalFundamental");
362
363 MTaskList tlist;
364 plist.AddToList(&tlist);
365
366 // Setup Task-lists
367 MRawFileRead rawread(NULL);
368 MReadMarsFile read("Events");
369 read.DisableAutoScheme();
370
371 if (!fSequence.IsMonteCarlo())
372 rawread.AddFiles(iter);
373 else
374 static_cast<MRead&>(read).AddFiles(iter);
375
376 // Check for interleaved events
377 // This will make that for data with version less than 5, where trigger
378 // patterns were not yet correct, all the events in the file will be
379 // processed. For those data there are no interleaved calibration events,
380 // so it makes no sense to run this test on a _D_ file. So we assume it
381 // will be a _C_ file, and accept all events.
382 MTriggerPatternDecode decode;
383 MFTriggerPattern fcalib;
384 fcalib.DenyCalibration();
385 fcalib.SetDefault(kFALSE);
386 MContinue conttp(&fcalib, "ContTrigPattern");
387
388 MGeomApply apply; // Only necessary to craete geometry
389 if (!geom.IsNull())
390 apply.SetGeometry(geom);
391 MBadPixelsMerge merge(&badpix);
392
393// MExtractTimeAndChargeSlidingWindow extrsw;
394// MExtractTimeFastSpline extime;
395
396 MExtractTimeAndChargeSpline spline;
397
398 MTaskEnv taskenv1("ExtractSignal");
399 MTaskEnv taskenv2("ExtractTime");
400
401 if (extractor1)
402 taskenv1.SetDefault(extractor1);
403
404 if (extractor2)
405 taskenv2.SetDefault(fTimeExtractor);
406 else if (!(extractor1->InheritsFrom("MExtractTimeAndCharge")))
407 {
408 spline.SetWindowSize(8,8);
409 taskenv2.SetDefault(&spline);
410 *fLog << warn << GetDescriptor()
411 << ": No extractor has been chosen, take default MExtractTimeAndChargeSlidingWindow " << endl;
412 }
413
414
415 MCalibrateData photcalc;
416 photcalc.AddPedestal("Fundamental");
417 photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
418 photcalc.SetPedestalFlag(MCalibrateData::kEvent);
419 photcalc.SetSignalType(MCalibrateData::kPhe);
420
421 MCalibrateRelTimes caltimes;
422 MBadPixelsCalc badcalc;
423 MBadPixelsTreat badtreat;
424 badtreat.SetProcessTimes(kFALSE);
425
426 badcalc.SetNamePedPhotCam("MPedPhotFundamental");
427 badtreat.SetUseInterpolation();
428 badtreat.AddNamePedPhotCam("MPedPhotFundamental");
429
430 MCalibrationTestCalc testcalc;
431
432 MHCamEvent evt0(0,"Signal", "Un-Calibrated Signal;;S [FADC cnts]" );
433 MHCamEvent evt1(0,"CalSig", "Cal. and Interp. Sig. by Pixel Size Ratio;;S [phe]");
434 MHCamEvent evt2(6,"Times" , "Arrival Time;;T [slice]");
435
436 MFillH fill0(&evt0, "MExtractedSignalCam", "FillUncalibrated");
437 MFillH fill1(&evt1, "MSignalCam", "FillCalibrated");
438 MFillH fill2(&evt2, "MSignalCam", "FillTimes");
439
440 MFillH fillcam("MHCalibrationTestCam", "MSignalCam" ,"FillTest");
441 MFillH filltme("MHCalibrationTestTimeCam", "MSignalCam", "FillTestTime");
442 fillcam.SetBit(MFillH::kDoNotDisplay);
443 filltme.SetBit(MFillH::kDoNotDisplay);
444
445 MFCosmics cosmics;
446 cosmics.SetNamePedestalCam("MPedestalFundamental");
447 MContinue contcos(&cosmics,"ContCosmics");
448
449 tlist.AddToList(&read);
450 tlist.AddToList(&decode);
451 tlist.AddToList(&apply);
452 tlist.AddToList(&merge);
453 // tlist.AddToList(&conttp);
454 tlist.AddToList(&taskenv1);
455 if (!extractor1->InheritsFrom("MExtractTimeAndCharge"))
456 tlist.AddToList(&taskenv2);
457
458 tlist.AddToList(&contcos);
459 tlist.AddToList(&fill0);
460 tlist.AddToList(&photcalc);
461 tlist.AddToList(&caltimes);
462 tlist.AddToList(&badcalc);
463 tlist.AddToList(&badtreat);
464 tlist.AddToList(&fill1);
465 tlist.AddToList(&fill2);
466 tlist.AddToList(&fillcam);
467 tlist.AddToList(&filltme);
468 tlist.AddToList(&testcalc);
469
470 // Create and setup the eventloop
471 MEvtLoop evtloop(fName);
472 evtloop.SetParList(&plist);
473 evtloop.SetDisplay(fDisplay);
474 evtloop.SetLogStream(fLog);
475
476 // Execute first analysis
477 if (!evtloop.Eventloop())
478 {
479 *fLog << err << GetDescriptor() << ": Failed." << endl;
480 return kFALSE;
481 }
482
483 if (fIsPixelCheck)
484 {
485 MHCalibrationTestCam *hcam = (MHCalibrationTestCam*)plist.FindObject("MHCalibrationTestCam");
486 MHCalibrationPix &pix1 = (*hcam)[fCheckedPixId];
487 pix1.DrawClone("");
488
489 MHCalibrationTestTimeCam *hccam = (MHCalibrationTestTimeCam*)plist.FindObject("MHCalibrationTestTimeCam");
490 MHCalibrationPix &pix11 = (*hccam)[fCheckedPixId];
491 pix11.DrawClone("");
492 }
493
494 DisplayResult(plist);
495
496 if (!WriteResult())
497 return kFALSE;
498
499 *fLog << inf << GetDescriptor() << ": Done." << endl;
500
501 return kTRUE;
502}
503
504Bool_t MJCalibTest::WriteResult()
505{
506
507 if (fPathOut.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 (fTestCam.Write()<=0)
523 {
524 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
525 return kFALSE;
526 }
527
528 if (fTestTimeCam.Write()<=0)
529 {
530 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
531 return kFALSE;
532 }
533
534 return kTRUE;
535
536}
Note: See TracBrowser for help on using the repository browser.