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

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