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

Last change on this file since 5601 was 5601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 15.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 &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(&pedcam);
279 plist.AddToList(&calibcont);
280
281 // Setup Tasklist
282 MTaskList tlist;
283 plist.AddToList(&tlist);
284
285 // FIXME: Move this to an intermediate class MJMagic
286 Byte_t filetype = 2;
287 /*
288 TString name = iter.Next();
289 Byte_t filetype = MRawFileRead::IsFileValid(name);
290 if (!filetype)
291 filetype = MReadMarsFile::IsFileValid(name)+1;
292 if (filetype<1||filetype>3)
293 {
294 gLog << err << "ERROR - FileType #" << (int)filetype << " of first file " << name << " unknown..." << endl;
295 return kFALSE;
296 } */
297 // 1 = raw-file
298 // 2 = raw-root file
299 // 3 = mc-raw file
300
301 MReadReports readreal;
302 readreal.AddTree("Events", "MTime.", kTRUE);
303 readreal.AddTree("Trigger");
304 readreal.AddTree("Camera");
305 readreal.AddTree("Drive");
306 readreal.AddTree("CC");
307 readreal.AddTree("Currents");
308
309 //MReadMarsFile read("Events");
310 //read.DisableAutoScheme();
311 MRawFileRead rawread(NULL);
312 if (IsDataCheck())
313 rawread.AddFiles(iter);
314 else
315 readreal.AddFiles(iter);
316
317 MGeomApply apply; // Only necessary to create geometry
318 if (!geom.IsNull())
319 apply.SetGeometry(geom);
320 MBadPixelsMerge merge(&badpix);
321
322 MPedCalcFromLoGain pedlo1("MPedCalcMeanFromLoGain");
323 pedlo1.SetPedestalUpdate(kTRUE);
324 pedlo1.SetPedestalsOut(&pedcamab);
325
326 MPedCalcFromLoGain pedlo2("MPedCalcRmsFromLoGain");
327 pedlo2.SetPedestalUpdate(kTRUE);
328 pedlo2.SetPedestalsIn(&pedcamab);
329
330 if (extractor1)
331 {
332 // FIXME: How to get the fixed value 15 automatically?
333 const Float_t win = extractor1->GetNumHiGainSamples();
334 pedlo1.SetExtractWindow(15, (UShort_t)TMath::Nint(win));
335 pedlo2.SetExtractWindow(15, (UShort_t)TMath::Nint(win));
336
337 if (extractor1->InheritsFrom("MExtractTimeAndCharge"))
338 {
339 pedlo2.SetExtractor((MExtractTimeAndCharge*)extractor1);
340 extractor1->SetPedestals(&pedcamab);
341 }
342 }
343
344 MMcPedestalCopy pcopy;
345 MTaskEnv taskenv1("ExtractSignal");
346 MTaskEnv taskenv2("ExtractTime");
347 taskenv1.SetDefault(extractor1);
348 taskenv2.SetDefault(extractor2);
349 MCalibrateData calib;
350 if (filetype==3) // MC file
351 {
352 calib.SetCalibrationMode(MCalibrateData::kFfactor);
353 calib.SetPedestalFlag(MCalibrateData::kRun);
354 }
355 else
356 {
357 calib.SetPedestalCamMean(&pedcamab);
358 calib.SetPedestalFlag(MCalibrateData::kEvent);
359 }
360
361 MCalibrateRelTimes caltm;
362 MBadPixelsCalc bpcal;
363 MBadPixelsTreat treat;
364
365 MHCamEvent evt0(0, "PedFLG", "Pedestal from Lo Gain;;P [fadc/sl]");
366 MHCamEvent evt1(2, "PedRmsFLG", "Pedestal RMS from Lo Gain;;\\sigma_{p} [fadc/sl]");
367 MHCamEvent evt2(0, "Extra'd", "Extracted Signal;;S [fadc/sl]");
368 MHCamEvent evt3(0, "PedPhot", "Calibrated Pedestal;;P [\\gamma]");
369 MHCamEvent evt4(1, "PedRMS", "Calibrated Pedestal RMS;;\\sigma_{p} [\\gamma]");
370 MHCamEvent evt5(0, "Interp'd", "Interpolated Signal;;S [\\gamma]");
371 MHCamEvent evt6(2, "Unsuitable", "Unsuitable event ratio;;%");
372 MHCamEvent evt7(0, "Times", "Arrival Time;;T [slice]");
373 evt0.EnableVariance();
374 evt1.EnableVariance();
375 evt2.EnableVariance();
376 evt3.EnableVariance();
377 evt4.EnableVariance();
378 evt5.EnableVariance();
379 evt7.EnableVariance();
380
381 MFillH fill0(&evt0, &pedcamab, "FillPedFLG");
382 MFillH fill1(&evt1, "MPedestalCam", "FillPedRmsFLG");
383 MFillH fill2(&evt2, "MExtractedSignalCam", "FillExtracted");
384 MFillH fill3(&evt3, "MPedPhotCam", "FillPedPhot");
385 MFillH fill4(&evt4, "MPedPhotCam", "FillPedRMS");
386 MFillH fill5(&evt5, "MCerPhotEvt", "FillInterpolated");
387 MFillH fill6(&evt6, "MBadPixelsCam", "FillUnsuitable");
388 MFillH fill7(&evt7, "MArrivalTime", "FillTimes");
389
390 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fPathOut.Data()), fOverwrite);
391 // Run Header
392 write.AddContainer("MRawRunHeader", "RunHeaders");
393 write.AddContainer("MBadPixelsCam", "RunHeaders");
394 write.AddContainer("MGeomCam", "RunHeaders");
395 // Monte Carlo Headers
396 write.AddContainer("MMcTrigHeader", "RunHeaders", kFALSE);
397 write.AddContainer("MMcConfigRunHeader", "RunHeaders", kFALSE);
398 write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
399 // Monte Carlo
400 write.AddContainer("MMcEvt", "Events", kFALSE);
401 write.AddContainer("MMcTrig", "Events", kFALSE);
402 // Data
403 write.AddContainer("MCerPhotEvt", "Events");
404 write.AddContainer("MPedPhotCam", "Events");
405 write.AddContainer("MTime", "Events", kFALSE);
406 write.AddContainer("MRawEvtHeader", "Events");
407 write.AddContainer("MArrivalTime", "Events");
408 // Slow-Control: Current
409 write.AddContainer("MTimeCurrents", "Currents", kFALSE);
410 write.AddContainer("MCameraDC", "Currents", kFALSE);
411 write.AddContainer("MReportCurrents", "Currents", kFALSE);
412 // Slow-Control: Camera
413 write.AddContainer("MReportCamera", "Camera", kFALSE);
414 write.AddContainer("MTimeCamera", "Camera", kFALSE);
415 write.AddContainer("MCameraAUX", "Camera", kFALSE);
416 write.AddContainer("MCameraCalibration", "Camera", kFALSE);
417 write.AddContainer("MCameraCooling", "Camera", kFALSE);
418 write.AddContainer("MCameraHV", "Camera", kFALSE);
419 write.AddContainer("MCameraLV", "Camera", kFALSE);
420 write.AddContainer("MCameraLids", "Camera", kFALSE);
421 // Slow-Control: Trigger
422 write.AddContainer("MReportTrigger", "Trigger", kFALSE);
423 write.AddContainer("MTimeTrigger", "Trigger", kFALSE);
424 // Slow-Control: Drive
425 write.AddContainer("MPointingPos", "Drive", kFALSE);
426 write.AddContainer("MReportDrive", "Drive", kFALSE);
427 write.AddContainer("MTimeDrive", "Drive", kFALSE);
428 // Slow-Control: Central Control
429 write.AddContainer("MReportCC", "CC", kFALSE);
430 write.AddContainer("MTimeCC", "CC", kFALSE);
431
432 // Now setup tasklist for events
433 MTaskList tlist2;
434 tlist2.AddToList(&apply);
435 tlist2.AddToList(&merge);
436 if (filetype==3)
437 tlist2.AddToList(&pcopy);
438 else
439 {
440 tlist2.AddToList(&pedlo1);
441 tlist2.AddToList(&pedlo2);
442 }
443 tlist2.AddToList(&fill0);
444 tlist2.AddToList(&fill1);
445 if (extractor1)
446 tlist2.AddToList(&taskenv1);
447 if (extractor2)
448 tlist2.AddToList(&taskenv2);
449 tlist2.AddToList(&fill2);
450 tlist2.AddToList(&calib);
451 tlist2.AddToList(&caltm);
452 tlist2.AddToList(&fill3);
453 tlist2.AddToList(&bpcal);
454 tlist2.AddToList(&treat);
455 tlist2.AddToList(&fill4);
456 tlist2.AddToList(&fill5);
457 tlist2.AddToList(&fill6);
458 tlist2.AddToList(&fill7);
459
460 // Setup List for Drive-tree
461 MPointingPosCalc pcalc;
462
463 // Now setup main tasklist
464 tlist.AddToList(IsDataCheck() ? (MTask*)&rawread : (MTask*)&readreal);
465 tlist.AddToList(&tlist2, IsDataCheck()?"All":"Events");
466 if (!IsDataCheck())
467 tlist.AddToList(&pcalc, "Drive");
468 tlist.AddToList(&write);
469
470 // Create and setup the eventloop
471 MEvtLoop evtloop(fName);
472 evtloop.SetParList(&plist);
473 evtloop.SetDisplay(fDisplay);
474 evtloop.SetLogStream(fLog);
475 if (!SetupEnv(evtloop))
476 return kFALSE;
477
478 // Execute first analysis
479 if (!evtloop.Eventloop(fMaxEvents))
480 {
481 *fLog << err << GetDescriptor() << ": Failed." << endl;
482 return kFALSE;
483 }
484
485 tlist.PrintStatistics();
486
487 if (!WriteResult())
488 return kFALSE;
489
490 *fLog << all << GetDescriptor() << ": Done." << endl;
491 *fLog << endl << endl;
492
493 return kTRUE;
494}
Note: See TracBrowser for help on using the repository browser.