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

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