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

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