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

Last change on this file since 7888 was 7071, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 15.7 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, 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 const Int_t n0 = fSequence.SetupCalRuns(iter, fPathData, IsUseRawData());
290 const Int_t n1 = fSequence.GetNumCalRuns();
291 if (n0==0)
292 {
293 *fLog << err << "ERROR - No input files of sequence found!" << endl;
294 return kFALSE;
295 }
296 if (n0!=n1)
297 {
298 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
299 if (fLog->GetDebugLevel()>4)
300 {
301 *fLog << dbg << "Files which are searched:" << endl;
302 iter.Print();
303 }
304 return kFALSE;
305 }
306 }
307
308 MCalibrationChargeCam calcam;
309 MCalibrationQECam qecam;
310 MCalibrationRelTimeCam tmcam;
311 MBadPixelsCam badpix;
312
313 TObjArray calibcont;
314 calibcont.Add(&calcam);
315 calibcont.Add(&qecam);
316 calibcont.Add(&tmcam);
317
318 MExtractor *extractor1=0;
319 MExtractor *extractor2=0;
320 TString geom;
321
322 if (!ReadCalibration(calibcont, badpix, extractor1, extractor2, geom))
323 {
324 *fLog << err << "Could not read calibration constants " << endl;
325 return kFALSE;
326 }
327
328 *fLog << all;
329 if (extractor1)
330 {
331 *fLog << underline << "Signal Extractor found in calibration file" << endl;
332 extractor1->Print();
333 *fLog << endl;
334 }
335 else
336 *fLog << inf << "No Signal Extractor: ExtractSignal in file." << endl;
337
338 if (extractor2)
339 {
340 *fLog << underline << "Time Extractor found in calibration file" << endl;
341 extractor2->Print();
342 *fLog << endl;
343 }
344 else
345 *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
346
347 if (!geom.IsNull())
348 *fLog << inf << "Camera geometry found in file: " << geom << endl;
349 else
350 *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
351
352 if (fExtractor)
353 extractor1 = fExtractor;
354 if (fTimeExtractor)
355 extractor2 = fTimeExtractor;
356
357 // Setup Lists
358 MParList plist;
359 plist.AddToList(this); // take care of fDisplay!
360 plist.AddToList(&fTestCam);
361 plist.AddToList(&fTestTimeCam);
362 plist.AddToList(&badpix);
363 plist.AddToList(&pedcam);
364 plist.AddToList(&calcam);
365 plist.AddToList(&qecam);
366 plist.AddToList(&tmcam);
367
368 MSignalCam cerphot;
369 MPedPhotCam pedphot;
370 MHCalibrationTestCam testcam;
371
372 plist.AddToList(&cerphot);
373 plist.AddToList(&pedphot);
374 plist.AddToList(&testcam);
375
376 pedcam.SetName("MPedestalFundamental");
377
378 MTaskList tlist;
379 plist.AddToList(&tlist);
380
381 // Setup Task-lists
382 MRawFileRead rawread(NULL);
383 MReadMarsFile read("Events");
384 read.DisableAutoScheme();
385
386 if (IsUseRawData())
387 rawread.AddFiles(iter);
388 else
389 static_cast<MRead&>(read).AddFiles(iter);
390
391 // Check for interleaved events
392 // This will make that for data with version less than 5, where trigger
393 // patterns were not yet correct, all the events in the file will be
394 // processed. For those data there are no interleaved calibration events,
395 // so it makes no sense to run this test on a _D_ file. So we assume it
396 // will be a _C_ file, and accept all events.
397 MTriggerPatternDecode decode;
398 MFTriggerPattern fcalib;
399 fcalib.DenyCalibration();
400 fcalib.SetDefault(kFALSE);
401 MContinue conttp(&fcalib, "ContTrigPattern");
402
403 MGeomApply apply; // Only necessary to craete geometry
404 if (!geom.IsNull())
405 apply.SetGeometry(geom);
406 MBadPixelsMerge merge(&badpix);
407
408 MExtractTimeAndChargeSlidingWindow extrsw;
409 MExtractTimeFastSpline extime;
410 extime.SetPedestals(&pedcam);
411
412 MTaskEnv taskenv1("ExtractSignal");
413 MTaskEnv taskenv2("ExtractTime");
414
415 if (extractor1)
416 {
417 extractor1->SetPedestals(&pedcam);
418 taskenv1.SetDefault(extractor1);
419 }
420
421 if (extractor2)
422 {
423 fTimeExtractor->SetPedestals(&pedcam);
424 taskenv2.SetDefault(fTimeExtractor);
425 }
426 else if (!(extractor1->InheritsFrom("MExtractTimeAndCharge")))
427 {
428 extrsw.SetPedestals(&pedcam);
429 extrsw.SetWindowSize(8,8);
430 taskenv2.SetDefault(&extrsw);
431 *fLog << warn << GetDescriptor()
432 << ": No extractor has been chosen, take default MExtractTimeAndChargeSlidingWindow " << endl;
433 }
434
435
436 MCalibrateData photcalc;
437 photcalc.AddPedestal("Fundamental");
438 photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
439 photcalc.SetPedestalFlag(MCalibrateData::kEvent);
440 photcalc.SetSignalType(MCalibrateData::kPhe);
441
442 MCalibrateRelTimes caltimes;
443 MBadPixelsCalc badcalc;
444 MBadPixelsTreat badtreat;
445 badtreat.SetProcessTimes(kFALSE);
446
447 badcalc.SetNamePedPhotCam("MPedPhotFundamental");
448 badtreat.SetUseInterpolation();
449 badtreat.AddNamePedPhotCam("MPedPhotFundamental");
450
451 MCalibrationTestCalc testcalc;
452
453 MHCamEvent evt0(0,"Signal", "Un-Calibrated Signal;;S [FADC cnts]" );
454 MHCamEvent evt1(0,"CalSig", "Cal. and Interp. Sig. by Pixel Size Ratio;;S [phe]");
455 MHCamEvent evt2(6,"Times" , "Arrival Time;;T [slice]");
456
457 MFillH fill0(&evt0, "MExtractedSignalCam", "FillUncalibrated");
458 MFillH fill1(&evt1, "MSignalCam", "FillCalibrated");
459 MFillH fill2(&evt2, "MSignalCam", "FillTimes");
460
461 MFillH fillcam("MHCalibrationTestCam", "MSignalCam" ,"FillTest");
462 MFillH filltme("MHCalibrationTestTimeCam", "MSignalCam", "FillTestTime");
463 fillcam.SetBit(MFillH::kDoNotDisplay);
464 filltme.SetBit(MFillH::kDoNotDisplay);
465
466 MFCosmics cosmics;
467 cosmics.SetNamePedestalCam("MPedestalFundamental");
468 MContinue contcos(&cosmics,"ContCosmics");
469
470 tlist.AddToList(&read);
471 tlist.AddToList(&decode);
472 tlist.AddToList(&apply);
473 tlist.AddToList(&merge);
474 // tlist.AddToList(&conttp);
475 tlist.AddToList(&taskenv1);
476 if (!extractor1->InheritsFrom("MExtractTimeAndCharge"))
477 tlist.AddToList(&taskenv2);
478
479 tlist.AddToList(&contcos);
480 tlist.AddToList(&fill0);
481 tlist.AddToList(&photcalc);
482 tlist.AddToList(&caltimes);
483 tlist.AddToList(&badcalc);
484 tlist.AddToList(&badtreat);
485 tlist.AddToList(&fill1);
486 tlist.AddToList(&fill2);
487 tlist.AddToList(&fillcam);
488 tlist.AddToList(&filltme);
489 tlist.AddToList(&testcalc);
490
491 // Create and setup the eventloop
492 MEvtLoop evtloop(fName);
493 evtloop.SetParList(&plist);
494 evtloop.SetDisplay(fDisplay);
495 evtloop.SetLogStream(fLog);
496
497 // Execute first analysis
498 if (!evtloop.Eventloop())
499 {
500 *fLog << err << GetDescriptor() << ": Failed." << endl;
501 return kFALSE;
502 }
503
504 if (fIsPixelCheck)
505 {
506 MHCalibrationTestCam *hcam = (MHCalibrationTestCam*)plist.FindObject("MHCalibrationTestCam");
507 MHCalibrationPix &pix1 = (*hcam)[fCheckedPixId];
508 pix1.DrawClone("");
509
510 MHCalibrationTestTimeCam *hccam = (MHCalibrationTestTimeCam*)plist.FindObject("MHCalibrationTestTimeCam");
511 MHCalibrationPix &pix11 = (*hccam)[fCheckedPixId];
512 pix11.DrawClone("");
513 }
514
515 DisplayResult(plist);
516
517 if (!WriteResult())
518 return kFALSE;
519
520 *fLog << inf << GetDescriptor() << ": Done." << endl;
521
522 return kTRUE;
523}
524
525Bool_t MJCalibTest::WriteResult()
526{
527
528 if (fPathOut.IsNull())
529 return kTRUE;
530
531 const TString oname(GetOutputFile());
532
533 *fLog << inf << "Writing to file: " << oname << endl;
534
535 TFile file(oname, "UPDATE");
536
537 if (fDisplay && fDisplay->Write()<=0)
538 {
539 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
540 return kFALSE;
541 }
542
543 if (fTestCam.Write()<=0)
544 {
545 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
546 return kFALSE;
547 }
548
549 if (fTestTimeCam.Write()<=0)
550 {
551 *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
552 return kFALSE;
553 }
554
555 return kTRUE;
556
557}
558
Note: See TracBrowser for help on using the repository browser.