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

Last change on this file since 5832 was 5810, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 18.3 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 pedcamab.SetName("MPedestalFundamental");
285 plist.AddToList(&pedcamab);
286
287 // Setup Tasklist
288 MTaskList tlist;
289 plist.AddToList(&tlist);
290
291 // FIXME: Move this to an intermediate class MJMagic
292 Byte_t filetype = 2;
293 /*
294 TString name = iter.Next();
295 Byte_t filetype = MRawFileRead::IsFileValid(name);
296 if (!filetype)
297 filetype = MReadMarsFile::IsFileValid(name)+1;
298 if (filetype<1||filetype>3)
299 {
300 gLog << err << "ERROR - FileType #" << (int)filetype << " of first file " << name << " unknown..." << endl;
301 return kFALSE;
302 } */
303 // 1 = raw-file
304 // 2 = raw-root file
305 // 3 = mc-raw file
306
307 MReadReports readreal;
308 readreal.AddTree("Events", "MTime.", kTRUE);
309 readreal.AddTree("Trigger");
310 readreal.AddTree("Camera");
311 readreal.AddTree("Drive");
312 readreal.AddTree("CC");
313 readreal.AddTree("Currents");
314
315 //MReadMarsFile read("Events");
316 //read.DisableAutoScheme();
317 MRawFileRead rawread(NULL);
318 if (IsDataCheck())
319 rawread.AddFiles(iter);
320 else
321 readreal.AddFiles(iter);
322
323 MGeomApply apply; // Only necessary to create geometry
324 if (!geom.IsNull())
325 apply.SetGeometry(geom);
326 MBadPixelsMerge merge(&badpix);
327
328 // Make sure that pedcamab has the correct name
329 pedcamab.SetName("MPedestalFundamental");
330 pedcam.SetName("MPedestalFromExtractorRndm");
331 pedcam2.SetName("MPedestalFromExtractor");
332 plist.AddToList(&pedcam);
333 plist.AddToList(&pedcam2);
334 plist.AddToList(&pedcamab);
335
336 // Check for interleaved events
337 MTriggerPatternDecode decode;
338
339 MFTriggerPattern ftp;
340 ftp.DenyCalibration();
341 ftp.DenyPedestal();
342 ftp.DenyPinDiode();
343
344 MContinue conttp(&ftp, "ContTrigPattern");
345 // --> tlist2
346
347 // Do signal and pedestal calculation
348 MPedCalcFromLoGain pedlo1("MPedCalcFundamental");
349 pedlo1.SetPedestalUpdate(kTRUE);
350 pedlo1.SetNamePedestalCamOut("MPedestalFundamental");
351
352 MPedCalcFromLoGain pedlo2("MPedCalcWithExtractorRndm");
353 pedlo2.SetPedestalUpdate(kTRUE);
354 pedlo2.SetRandomCalculation(kTRUE);
355 pedlo2.SetNamePedestalCamIn("MPedestalFundamental");
356 pedlo2.SetNamePedestalCamOut("MPedestalFromExtractorRndm");
357
358 MPedCalcFromLoGain pedlo3("MPedCalcWithExtractor");
359 pedlo3.SetPedestalUpdate(kTRUE);
360 pedlo3.SetRandomCalculation(kFALSE);
361 pedlo3.SetNamePedestalCamIn("MPedestalFundamental");
362 pedlo3.SetNamePedestalCamOut("MPedestalFromExtractor");
363
364 if (extractor1)
365 {
366 // FIXME: How to get the fixed value 15 automatically?
367 const Float_t win = extractor1->GetNumHiGainSamples();
368 pedlo1.SetExtractWindow(15, (UShort_t)TMath::Nint(win));
369 pedlo2.SetExtractWindow(15, (UShort_t)TMath::Nint(win));
370 pedlo3.SetExtractWindow(15, (UShort_t)TMath::Nint(win));
371
372 if (extractor1->InheritsFrom("MExtractTimeAndCharge"))
373 {
374 pedlo2.SetExtractor((MExtractTimeAndCharge*)extractor1);
375 pedlo3.SetExtractor((MExtractTimeAndCharge*)extractor1);
376 extractor1->SetPedestals(&pedcamab);
377 }
378 }
379
380 MFCosmics fcosmics;
381 fcosmics.SetNamePedestalCam("MPedestalFundamental");
382 MContinue contcos(&fcosmics, "ContTrigEvts");
383 contcos.SetInverted();
384
385 MMcPedestalCopy pcopy;
386 MTaskEnv taskenv1("ExtractSignal");
387 MTaskEnv taskenv2("ExtractTime");
388 taskenv1.SetDefault(extractor1);
389 taskenv2.SetDefault(extractor2);
390 MCalibrateData calib;
391 if (filetype==3) // MC file
392 {
393 calib.SetCalibrationMode(MCalibrateData::kFfactor);
394 calib.SetPedestalFlag(MCalibrateData::kRun);
395 // FIXME: What to do for MC files???
396 calib.AddPedestal("MPedestalCam", "MPedPhotFundamental");
397 calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractor");
398 calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractorRndm");
399 }
400 else
401 {
402 calib.AddPedestal("Fundamental");
403 calib.AddPedestal("FromExtractor");
404 calib.AddPedestal("FromExtractorRndm");
405 calib.SetPedestalFlag(MCalibrateData::kEvent);
406 }
407
408 MCalibrateRelTimes caltm;
409 MBadPixelsCalc bpcal;
410 MBadPixelsTreat treat;
411
412 bpcal.SetNamePedPhotCam("MPedPhotFromExtractor");
413 treat.AddNamePedPhotCam("MPedPhotFundamental");
414 treat.AddNamePedPhotCam("MPedPhotFromExtractor");
415 treat.AddNamePedPhotCam("MPedPhotFromExtractorRndm");
416
417
418 MHCamEvent evt0(0, "PedFLG", "Pedestal from Lo Gain;;P [fadc/sl]");
419 MHCamEvent evt1(2, "PedRmsFLG", "Pedestal RMS from Lo Gain;;\\sigma_{p} [fadc/sl]");
420 MHCamEvent evt2(0, "Extra'd", "Extracted Signal;;S [fadc/sl]");
421 MHCamEvent evt3(4, "PedPhot", "Calibrated Pedestal;;P [\\gamma]");
422 MHCamEvent evt4(5, "PedRMS", "Calibrated Pedestal RMS;;\\sigma_{p} [\\gamma]");
423 MHCamEvent evt5(0, "Interp'd", "Interpolated Signal;;S [\\gamma]");
424 MHCamEvent evt6(2, "Unsuitable", "Unsuitable event ratio;;%");
425 MHCamEvent evt7(0, "Times", "Arrival Time;;T [slice]");
426 evt0.EnableVariance();
427 evt1.EnableVariance();
428 evt2.EnableVariance();
429 evt3.EnableVariance();
430 evt4.EnableVariance();
431 evt5.EnableVariance();
432 evt7.EnableVariance();
433
434 MFillH fill0(&evt0, "MPedestalFundamental", "FillPedFLG");
435 MFillH fill1(&evt1, "MPedestalFromExtractor", "FillPedRmsFLG");
436 MFillH fill2(&evt2, "MExtractedSignalCam", "FillExtracted");
437 MFillH fill3(&evt3, "MPedPhotFundamental", "FillPedPhot");
438 MFillH fill4(&evt4, "MPedPhotFromExtractor", "FillPedRMS");
439 MFillH fill5(&evt5, "MCerPhotEvt", "FillInterpolated");
440 MFillH fill6(&evt6, "MBadPixelsCam", "FillUnsuitable");
441 MFillH fill7(&evt7, "MArrivalTime", "FillTimes");
442
443 // The second rule is for the case reading raw-files!
444 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}{s/.raw$/.root}", fPathOut.Data()), fOverwrite);
445 // Run Header
446 write.AddContainer("MRawRunHeader", "RunHeaders");
447 write.AddContainer("MBadPixelsCam", "RunHeaders");
448 write.AddContainer("MGeomCam", "RunHeaders");
449 // Monte Carlo Headers
450 write.AddContainer("MMcTrigHeader", "RunHeaders", kFALSE);
451 write.AddContainer("MMcConfigRunHeader", "RunHeaders", kFALSE);
452 write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
453 // Monte Carlo
454 write.AddContainer("MMcEvt", "Events", kFALSE);
455 write.AddContainer("MMcTrig", "Events", kFALSE);
456 // Data
457 write.AddContainer("MCerPhotEvt", "Events");
458 write.AddContainer("MPedPhotFundamental", "Events");
459 write.AddContainer("MPedPhotFromExtractor", "Events");
460 write.AddContainer("MPedPhotFromExtractorRndm", "Events");
461 write.AddContainer("MTime", "Events", kFALSE);
462 write.AddContainer("MRawEvtHeader", "Events");
463 write.AddContainer("MArrivalTime", "Events");
464 // Slow-Control: Current
465 write.AddContainer("MTimeCurrents", "Currents", kFALSE);
466 write.AddContainer("MCameraDC", "Currents", kFALSE);
467 write.AddContainer("MReportCurrents", "Currents", kFALSE);
468 // Slow-Control: Camera
469 write.AddContainer("MReportCamera", "Camera", kFALSE);
470 write.AddContainer("MTimeCamera", "Camera", kFALSE);
471 write.AddContainer("MCameraAUX", "Camera", kFALSE);
472 write.AddContainer("MCameraCalibration", "Camera", kFALSE);
473 write.AddContainer("MCameraCooling", "Camera", kFALSE);
474 write.AddContainer("MCameraHV", "Camera", kFALSE);
475 write.AddContainer("MCameraLV", "Camera", kFALSE);
476 write.AddContainer("MCameraLids", "Camera", kFALSE);
477 // Slow-Control: Trigger
478 write.AddContainer("MReportTrigger", "Trigger", kFALSE);
479 write.AddContainer("MTimeTrigger", "Trigger", kFALSE);
480 // Slow-Control: Drive
481 write.AddContainer("MPointingPos", "Drive", kFALSE);
482 write.AddContainer("MReportDrive", "Drive", kFALSE);
483 write.AddContainer("MTimeDrive", "Drive", kFALSE);
484 // Slow-Control: Central Control
485 write.AddContainer("MReportCC", "CC", kFALSE);
486 write.AddContainer("MTimeCC", "CC", kFALSE);
487
488 // Now setup tasklist for events
489 MTaskList tlist2;
490 tlist2.AddToList(&decode);
491 tlist2.AddToList(&conttp);
492 tlist2.AddToList(&apply);
493 tlist2.AddToList(&merge);
494 if (filetype==3)
495 tlist2.AddToList(&pcopy);
496 else
497 {
498 tlist2.AddToList(&pedlo1);
499 tlist2.AddToList(&pedlo2);
500 tlist2.AddToList(&pedlo3);
501 }
502 tlist2.AddToList(&fill0);
503 tlist2.AddToList(&fill1);
504 if (extractor1)
505 tlist2.AddToList(&taskenv1);
506 if (extractor2)
507 tlist2.AddToList(&taskenv2);
508 tlist2.AddToList(&contcos);
509 tlist2.AddToList(&fill2);
510 tlist2.AddToList(&calib);
511 tlist2.AddToList(&caltm);
512 tlist2.AddToList(&bpcal);
513 tlist2.AddToList(&treat);
514 tlist2.AddToList(&fill6);
515 tlist2.AddToList(&fill3);
516 tlist2.AddToList(&fill4);
517 tlist2.AddToList(&fill5);
518 tlist2.AddToList(&fill7);
519
520 // Setup List for Drive-tree
521 MPointingPosCalc pcalc;
522
523 // Now setup main tasklist
524 tlist.AddToList(IsDataCheck() ? (MTask*)&rawread : (MTask*)&readreal);
525 tlist.AddToList(&tlist2, IsDataCheck()?"All":"Events");
526 if (!IsDataCheck())
527 tlist.AddToList(&pcalc, "Drive");
528 tlist.AddToList(&write);
529
530 // Create and setup the eventloop
531 MEvtLoop evtloop(fName);
532 evtloop.SetParList(&plist);
533 evtloop.SetDisplay(fDisplay);
534 evtloop.SetLogStream(fLog);
535 if (!SetupEnv(evtloop))
536 return kFALSE;
537
538 // Execute first analysis
539 if (!evtloop.Eventloop(fMaxEvents))
540 {
541 *fLog << err << GetDescriptor() << ": Failed." << endl;
542 return kFALSE;
543 }
544
545 tlist.PrintStatistics();
546
547 if (!WriteResult())
548 return kFALSE;
549
550 *fLog << all << GetDescriptor() << ": Done." << endl;
551 *fLog << endl << endl;
552
553 return kTRUE;
554}
Note: See TracBrowser for help on using the repository browser.