source: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc@ 6031

Last change on this file since 6031 was 6031, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 19.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): Thomas Bretz, 1/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJCalibrateSignal
28//
29// This class is reading the output written by callisto. It calibrates
30// signal and time.
31//
32// The signal and time extractors are read from the callisto-output. In
33// pricipal you could overwrite these default using the resource file,
34// but this is NOT recommended!
35//
36// Resource file entries are case sensitive!
37//
38/////////////////////////////////////////////////////////////////////////////
39#include "MJCalibrateSignal.h"
40
41#include <TEnv.h>
42#include <TFile.h>
43
44#include "MLog.h"
45#include "MLogManip.h"
46
47#include "MDirIter.h"
48#include "MParList.h"
49#include "MTaskList.h"
50#include "MEvtLoop.h"
51
52#include "MStatusDisplay.h"
53
54#include "MGeomCam.h"
55#include "MHCamEvent.h"
56#include "MPedestalCam.h"
57#include "MBadPixelsCam.h"
58
59#include "MCalibrationQECam.h"
60#include "MCalibrationBlindCam.h"
61#include "MCalibrationChargeCam.h"
62#include "MCalibrationRelTimeCam.h"
63#include "MCalibrationChargePINDiode.h"
64
65#include "MReadReports.h"
66#include "MReadMarsFile.h"
67#include "MRawFileRead.h"
68#include "MContinue.h"
69#include "MTriggerPatternDecode.h"
70#include "MFTriggerPattern.h"
71#include "MGeomApply.h"
72#include "MMcPedestalCopy.h"
73#include "MPointingPosCalc.h"
74#include "MPedCalcFromLoGain.h"
75#include "MExtractor.h"
76#include "MExtractTimeAndCharge.h"
77#include "MFCosmics.h"
78#include "MTaskEnv.h"
79#include "MCalibrateData.h"
80#include "MCalibrateRelTimes.h"
81#include "MBadPixelsMerge.h"
82#include "MBadPixelsCalc.h"
83#include "MBadPixelsTreat.h"
84#include "MFillH.h"
85#include "MWriteRootFile.h"
86
87ClassImp(MJCalibrateSignal);
88
89using namespace std;
90
91// --------------------------------------------------------------------------
92//
93// Default constructor.
94//
95// Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE
96//
97MJCalibrateSignal::MJCalibrateSignal(const char *name, const char *title)
98 : fIsDataCheck(kFALSE)
99{
100 fName = name ? name : "MJCalibrateSignal";
101 fTitle = title ? title : "Tool to calibrate data";
102}
103
104Bool_t MJCalibrateSignal::WriteResult()
105{
106 if (fPathOut.IsNull())
107 {
108 *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
109 return kTRUE;
110 }
111
112 const TString oname = Form("%s/signal%08d.root", (const char*)fPathOut, fSequence.GetSequence());
113
114 *fLog << inf << "Writing to file: " << oname << endl;
115
116 TFile file(oname, "RECREATE");
117 if (!file.IsOpen())
118 {
119 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
120 return kFALSE;
121 }
122
123 *fLog << inf << " - MStatusDisplay..." << flush;
124 if (fDisplay && fDisplay->Write()<=0)
125 {
126 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
127 return kFALSE;
128 }
129 *fLog << inf << "ok." << endl;
130
131 return kTRUE;
132}
133
134Bool_t MJCalibrateSignal::ReadCalibration(TObjArray &l, MBadPixelsCam &cam, MExtractor* &ext1, MExtractor* &ext2, TString &geom) const
135{
136 const TString fname = Form("%s/calib%08d.root", fPathIn.Data(), fSequence.GetSequence());
137
138 *fLog << inf << "Reading from file: " << fname << endl;
139
140 TFile file(fname, "READ");
141 if (!file.IsOpen())
142 {
143 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
144 return kFALSE;
145 }
146
147 TObject *o = file.Get("ExtractSignal");
148 if (o && !o->InheritsFrom(MExtractor::Class()))
149 {
150 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
151 return kFALSE;
152 }
153 ext1 = o ? (MExtractor*)o->Clone() : NULL;
154
155 o = file.Get("ExtractTime");
156 if (o && !o->InheritsFrom(MExtractor::Class()))
157 {
158 *fLog << err << dbginf << "ERROR - ExtractTime read from " << fname << " doesn't inherit from MExtractor!" << endl;
159 return kFALSE;
160 }
161 ext2 = o ? (MExtractor*)o->Clone() : NULL;
162 if (!ext1 && !ext2)
163 {
164 *fLog << err << dbginf << "ERROR - Neither ExtractSignal nor ExrtractTime found in " << fname << "!" << endl;
165 return kFALSE;
166 }
167
168 o = file.Get("MGeomCam");
169 if (o && !o->InheritsFrom(MGeomCam::Class()))
170 {
171 *fLog << err << dbginf << "ERROR - MGeomCam read from " << fname << " doesn't inherit from MGeomCam!" << endl;
172 return kFALSE;
173 }
174 geom = o ? o->ClassName() : "";
175
176 TObjArray cont(l);
177 cont.Add(&cam);
178 return ReadContainer(cont);
179}
180
181// --------------------------------------------------------------------------
182//
183// MJCalibration allows to setup several option by a resource file:
184// MJCalibrateSignal.DataCheck: yes,no
185//
186// For more details see the class description and the corresponding Getters
187//
188Bool_t MJCalibrateSignal::CheckEnvLocal()
189{
190 SetDataCheck(GetEnv("DataCheck", IsDataCheck()));
191 return kTRUE;
192}
193
194Bool_t MJCalibrateSignal::ProcessFile(MPedestalCam &pedcamab, MPedestalCam &pedcam2, MPedestalCam &pedcam)
195{
196 if (!fSequence.IsValid())
197 {
198 *fLog << err << "ERROR - Sequence invalid!" << endl;
199 return kFALSE;
200 }
201
202 //if (!CheckEnv())
203 // return kFALSE;
204
205 CheckEnv();
206
207 // --------------------------------------------------------------------------------
208
209 *fLog << inf;
210 fLog->Separator(GetDescriptor());
211 *fLog << "Calculate calibrated data from runs ";
212 *fLog << fSequence.GetName() << endl;
213 *fLog << endl;
214
215 // --------------------------------------------------------------------------------
216
217 MDirIter iter;
218 const Int_t n0 = fSequence.SetupDatRuns(iter, fPathData, "D", IsDataCheck());
219 const Int_t n1 = fSequence.GetNumDatRuns();
220 if (n0==0)
221 {
222 *fLog << err << "ERROR - No input files of sequence found!" << endl;
223 return kFALSE;
224 }
225 if (n0!=n1)
226 {
227 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
228 return kFALSE;
229 }
230
231 // Read File
232 /*
233 MCalibrationIntensityChargeCam calcam;
234 MCalibrationIntensityQECam qecam;
235 MCalibrationIntensityBlindCam bndcam;
236 MCalibrationIntensityRelTimeCam tmcam;
237 */
238 MCalibrationChargeCam calcam;
239 MCalibrationQECam qecam;
240 MCalibrationBlindCam bndcam;
241 MCalibrationChargePINDiode pind;
242 MCalibrationRelTimeCam tmcam;
243 MBadPixelsCam badpix;
244
245 MExtractor *extractor1=0;
246 MExtractor *extractor2=0;
247 TString geom;
248
249 TObjArray calibcont;
250 calibcont.Add(&calcam);
251 calibcont.Add(&qecam);
252 calibcont.Add(&bndcam);
253 calibcont.Add(&pind);
254 calibcont.Add(&tmcam);
255
256 if (!ReadCalibration(calibcont, badpix, extractor1, extractor2, geom))
257 return kFALSE;
258
259 *fLog << all;
260 if (extractor1)
261 {
262 *fLog << underline << "Signal Extractor found in calibration file" << endl;
263 extractor1->Print();
264 *fLog << endl;
265 }
266 else
267 *fLog << inf << "No Signal Extractor: ExtractSignal in file." << endl;
268
269 if (extractor2)
270 {
271 *fLog << underline << "Time Extractor found in calibration file" << endl;
272 extractor2->Print();
273 *fLog << endl;
274 }
275 else
276 *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
277
278 if (!geom.IsNull())
279 *fLog << inf << "Camera geometry found in file: " << geom << endl;
280 else
281 *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
282
283 // This is necessary for the case in which it is not in the files
284 MBadPixelsCam badcam;
285
286 // Setup Parlist
287 MParList plist;
288 plist.AddToList(this); // take care of fDisplay!
289 plist.AddToList(&badcam);
290 plist.AddToList(&calibcont);
291
292 // Setup Tasklist
293 MTaskList tlist;
294 plist.AddToList(&tlist);
295
296 // FIXME: Move this to an intermediate class MJMagic
297 Byte_t filetype = 2;
298 /*
299 TString name = iter.Next();
300 Byte_t filetype = MRawFileRead::IsFileValid(name);
301 if (!filetype)
302 filetype = MReadMarsFile::IsFileValid(name)+1;
303 if (filetype<1||filetype>3)
304 {
305 gLog << err << "ERROR - FileType #" << (int)filetype << " of first file " << name << " unknown..." << endl;
306 return kFALSE;
307 } */
308 // 1 = raw-file
309 // 2 = raw-root file
310 // 3 = mc-raw file
311
312 MReadReports readreal;
313 readreal.AddTree("Events", "MTime.", kTRUE);
314 readreal.AddTree("Trigger");
315 readreal.AddTree("Camera");
316 readreal.AddTree("Drive");
317 readreal.AddTree("CC");
318 readreal.AddTree("Currents");
319
320 //MReadMarsFile read("Events");
321 //read.DisableAutoScheme();
322 MRawFileRead rawread(NULL);
323 if (IsDataCheck())
324 rawread.AddFiles(iter);
325 else
326 readreal.AddFiles(iter);
327
328 MGeomApply apply; // Only necessary to create geometry
329 if (!geom.IsNull())
330 apply.SetGeometry(geom);
331 MBadPixelsMerge merge(&badpix);
332
333 // Make sure that pedcamab has the correct name
334 pedcamab.SetName("MPedestalFundamental");
335 pedcam.SetName("MPedestalFromExtractorRndm");
336 pedcam2.SetName("MPedestalFromExtractor");
337 plist.AddToList(&pedcam);
338 plist.AddToList(&pedcam2);
339 plist.AddToList(&pedcamab);
340
341 // Check for interleaved events
342 MTriggerPatternDecode decode;
343
344 MFTriggerPattern ftp;
345 ftp.RequireCalibration();
346
347 MContinue conttp(&ftp, "ContTrigPattern");
348 // --> tlist2
349
350 // Do signal and pedestal calculation
351 MPedCalcFromLoGain pedlo1("MPedCalcFundamental");
352 pedlo1.SetPedestalUpdate(kTRUE);
353 pedlo1.SetNamePedestalCamOut("MPedestalFundamental");
354
355 MPedCalcFromLoGain pedlo2("MPedCalcWithExtractorRndm");
356 pedlo2.SetPedestalUpdate(kTRUE);
357 pedlo2.SetRandomCalculation(kTRUE);
358 pedlo2.SetNamePedestalCamIn("MPedestalFundamental");
359 pedlo2.SetNamePedestalCamOut("MPedestalFromExtractorRndm");
360
361 MPedCalcFromLoGain pedlo3("MPedCalcWithExtractor");
362 pedlo3.SetPedestalUpdate(kTRUE);
363 pedlo3.SetRandomCalculation(kFALSE);
364 pedlo3.SetNamePedestalCamIn("MPedestalFundamental");
365 pedlo3.SetNamePedestalCamOut("MPedestalFromExtractor");
366
367 if (extractor1)
368 {
369 extractor1->SetPedestals(&pedcamab);
370
371 if (extractor1->InheritsFrom("MExtractTimeAndCharge"))
372 {
373 pedlo2.SetExtractor((MExtractTimeAndCharge*)extractor1);
374 pedlo3.SetExtractor((MExtractTimeAndCharge*)extractor1);
375 const Int_t win = ((MExtractTimeAndCharge*)extractor1)->GetWindowSizeHiGain();
376 pedlo1.SetExtractWindow(15, win);
377 pedlo2.SetExtractWindow(15, win/*obsolete*/);
378 pedlo3.SetExtractWindow(15, win/*obsolete*/);
379 }
380 else
381 {
382 // FIXME: How to get the fixed value 15 automatically?
383 const Int_t f = (Int_t)(15.5+extractor1->GetHiGainFirst());
384 const Int_t n = (Int_t)(15.5+extractor1->GetNumHiGainSamples());
385 pedlo1.SetExtractWindow(f, n);
386 pedlo2.SetExtractWindow(f, n);
387 pedlo3.SetExtractWindow(f, n);
388 }
389 }
390 if (extractor2)
391 extractor2->SetPedestals(&pedcamab);
392
393 MFCosmics fcosmics;
394 fcosmics.SetNamePedestalCam("MPedestalFundamental");
395 MContinue contcos(&fcosmics, "ContTrigEvts");
396 contcos.SetInverted();
397
398 MMcPedestalCopy pcopy;
399 MTaskEnv taskenv1("ExtractSignal");
400 MTaskEnv taskenv2("ExtractTime");
401 taskenv1.SetDefault(extractor1);
402 taskenv2.SetDefault(extractor2);
403 MCalibrateData calib;
404 if (filetype==3) // MC file
405 {
406 calib.SetCalibrationMode(MCalibrateData::kFfactor);
407 calib.SetPedestalFlag(MCalibrateData::kRun);
408 // FIXME: What to do for MC files???
409 calib.AddPedestal("MPedestalCam", "MPedPhotFundamental");
410 calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractor");
411 calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractorRndm");
412 }
413 else
414 {
415 calib.AddPedestal("Fundamental");
416 calib.AddPedestal("FromExtractor");
417 calib.AddPedestal("FromExtractorRndm");
418 calib.SetPedestalFlag(MCalibrateData::kEvent);
419 }
420
421 MCalibrateRelTimes caltm;
422 MBadPixelsCalc bpcal;
423 MBadPixelsTreat treat;
424
425 bpcal.SetNamePedPhotCam("MPedPhotFromExtractor");
426 treat.AddNamePedPhotCam("MPedPhotFundamental");
427 treat.AddNamePedPhotCam("MPedPhotFromExtractor");
428 treat.AddNamePedPhotCam("MPedPhotFromExtractorRndm");
429 if (!extractor2 && !extractor1->InheritsFrom("MExtractTimeAndCharge"))
430 treat.SetProcessTimes(kFALSE);
431
432
433 MHCamEvent evt0(0, "PedFLG", "Pedestal from Lo Gain;;P [fadc/sl]");
434 MHCamEvent evt1(2, "PedRmsFLG", "Pedestal RMS from Lo Gain;;\\sigma_{p} [fadc/sl]");
435 MHCamEvent evt2(0, "Extra'd", "Extracted Signal;;S [fadc/sl]");
436 //MHCamEvent evt3(4, "PedPhot", "Calibrated Pedestal;;P [\\gamma]");
437 MHCamEvent evt4(5, "PedRMS", "Calibrated Pedestal RMS;;\\sigma_{p} [\\gamma]");
438 MHCamEvent evt5(0, "Interp'd", "Interpolated Signal;;S [\\gamma]");
439 MHCamEvent evt6(2, "Unsuitable", "Fraction of unsuitable events per Pixel;;[1]");
440 MHCamEvent evt7(0, "Times", "Arrival Time;;T [slice]");
441 evt0.EnableVariance();
442 evt1.EnableVariance();
443 evt2.EnableVariance();
444 //evt3.EnableVariance();
445 evt4.EnableVariance();
446 evt5.EnableVariance();
447 evt7.EnableVariance();
448
449 MFillH fill0(&evt0, "MPedestalFundamental", "FillPedFLG");
450 MFillH fill1(&evt1, "MPedestalFromExtractor", "FillPedRmsFLG");
451 MFillH fill2(&evt2, "MExtractedSignalCam", "FillExtracted");
452 //MFillH fill3(&evt3, "MPedPhotFundamental", "FillPedPhot");
453 MFillH fill4(&evt4, "MPedPhotFromExtractor", "FillPedRMS");
454 MFillH fill5(&evt5, "MCerPhotEvt", "FillInterpolated");
455 MFillH fill6(&evt6, "MBadPixelsCam", "FillUnsuitable");
456 MFillH fill7(&evt7, "MArrivalTime", "FillTimes");
457
458 // The second rule is for the case reading raw-files!
459 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}{s/.raw$/.root}", fPathOut.Data()), fOverwrite);
460 // Run Header
461 write.AddContainer("MRawRunHeader", "RunHeaders");
462 write.AddContainer("MBadPixelsCam", "RunHeaders");
463 write.AddContainer("MGeomCam", "RunHeaders");
464 // Monte Carlo Headers
465 write.AddContainer("MMcTrigHeader", "RunHeaders", kFALSE);
466 write.AddContainer("MMcConfigRunHeader", "RunHeaders", kFALSE);
467 write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
468 // Monte Carlo
469 write.AddContainer("MMcEvt", "Events", kFALSE);
470 write.AddContainer("MMcTrig", "Events", kFALSE);
471 // Data
472 write.AddContainer("MCerPhotEvt", "Events");
473 write.AddContainer("MPedPhotFundamental", "Events");
474 write.AddContainer("MPedPhotFromExtractor", "Events");
475 write.AddContainer("MPedPhotFromExtractorRndm", "Events");
476 write.AddContainer("MTime", "Events", kFALSE);
477 write.AddContainer("MRawEvtHeader", "Events");
478 write.AddContainer("MArrivalTime", "Events", kFALSE);
479 // Slow-Control: Current
480 write.AddContainer("MTimeCurrents", "Currents", kFALSE);
481 write.AddContainer("MCameraDC", "Currents", kFALSE);
482 write.AddContainer("MReportCurrents", "Currents", kFALSE);
483 // Slow-Control: Camera
484 write.AddContainer("MReportCamera", "Camera", kFALSE);
485 write.AddContainer("MTimeCamera", "Camera", kFALSE);
486 write.AddContainer("MCameraAUX", "Camera", kFALSE);
487 write.AddContainer("MCameraCalibration", "Camera", kFALSE);
488 write.AddContainer("MCameraCooling", "Camera", kFALSE);
489 write.AddContainer("MCameraHV", "Camera", kFALSE);
490 write.AddContainer("MCameraLV", "Camera", kFALSE);
491 write.AddContainer("MCameraLids", "Camera", kFALSE);
492 // Slow-Control: Trigger
493 write.AddContainer("MReportTrigger", "Trigger", kFALSE);
494 write.AddContainer("MTimeTrigger", "Trigger", kFALSE);
495 // Slow-Control: Drive
496 write.AddContainer("MPointingPos", "Drive", kFALSE);
497 write.AddContainer("MReportDrive", "Drive", kFALSE);
498 write.AddContainer("MTimeDrive", "Drive", kFALSE);
499 // Slow-Control: Central Control
500 write.AddContainer("MReportCC", "CC", kFALSE);
501 write.AddContainer("MTimeCC", "CC", kFALSE);
502
503 // Now setup tasklist for events
504 MTaskList tlist2;
505 tlist2.AddToList(&decode);
506 tlist2.AddToList(&conttp);
507 tlist2.AddToList(&apply);
508 tlist2.AddToList(&merge);
509 if (filetype==3)
510 tlist2.AddToList(&pcopy);
511 else
512 {
513 tlist2.AddToList(&pedlo1);
514 tlist2.AddToList(&pedlo2);
515 tlist2.AddToList(&pedlo3);
516 }
517 tlist2.AddToList(&fill0);
518 tlist2.AddToList(&fill1);
519 if (extractor1)
520 tlist2.AddToList(&taskenv1);
521 if (extractor2)
522 tlist2.AddToList(&taskenv2);
523 tlist2.AddToList(&contcos);
524 tlist2.AddToList(&fill2);
525 tlist2.AddToList(&calib);
526 if (extractor2 || extractor1->InheritsFrom("MExtractTimeAndCharge"))
527 tlist2.AddToList(&caltm);
528 tlist2.AddToList(&bpcal);
529 tlist2.AddToList(&treat);
530 tlist2.AddToList(&fill6);
531 //tlist2.AddToList(&fill3);
532 tlist2.AddToList(&fill4);
533 tlist2.AddToList(&fill5);
534 if (extractor2 || extractor1->InheritsFrom("MExtractTimeAndCharge"))
535 tlist2.AddToList(&fill7);
536
537 // Setup List for Drive-tree
538 MPointingPosCalc pcalc;
539
540 // Now setup main tasklist
541 tlist.AddToList(IsDataCheck() ? (MTask*)&rawread : (MTask*)&readreal);
542 tlist.AddToList(&tlist2, IsDataCheck()?"All":"Events");
543 if (!IsDataCheck())
544 tlist.AddToList(&pcalc, "Drive");
545 tlist.AddToList(&write);
546
547 // Create and setup the eventloop
548 MEvtLoop evtloop(fName);
549 evtloop.SetParList(&plist);
550 evtloop.SetDisplay(fDisplay);
551 evtloop.SetLogStream(fLog);
552 if (!SetupEnv(evtloop))
553 return kFALSE;
554
555 // Execute first analysis
556 if (!evtloop.Eventloop(fMaxEvents))
557 {
558 *fLog << err << GetDescriptor() << ": Failed." << endl;
559 return kFALSE;
560 }
561
562 tlist.PrintStatistics();
563
564 if (!WriteResult())
565 return kFALSE;
566
567 *fLog << all << GetDescriptor() << ": Done." << endl;
568 *fLog << endl << endl;
569
570 return kTRUE;
571}
Note: See TracBrowser for help on using the repository browser.