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

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