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

Last change on this file since 5157 was 5157, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 13.5 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 "MGeomApply.h"
72#include "MMcPedestalCopy.h"
73#include "MPointingPosCalc.h"
74#include "MPedCalcFromLoGain.h"
75#include "MExtractor.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{
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%06d.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, MTask* &ext1, MTask* &ext2, TString &geom) const
127{
128 const TString fname = Form("%s/calib%06d.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 ? (MTask*)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 ? (MTask*)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
173Bool_t MJCalibrateSignal::ProcessFile(MPedestalCam &pedcam, Bool_t ismc)
174{
175 if (!fSequence.IsValid())
176 {
177 *fLog << err << "ERROR - Sequence invalid!" << endl;
178 return kFALSE;
179 }
180
181 //if (!CheckEnv())
182 // return kFALSE;
183
184 CheckEnv();
185
186 // --------------------------------------------------------------------------------
187
188 *fLog << inf;
189 fLog->Separator(GetDescriptor());
190 *fLog << "Calculate calibrated data from runs ";
191 *fLog << fSequence.GetName() << endl;
192 *fLog << endl;
193
194 // --------------------------------------------------------------------------------
195
196 MDirIter iter;
197 const Int_t n0 = fSequence.SetupDatRuns(iter, fPathData);
198 const Int_t n1 = fSequence.GetNumDatRuns();
199 if (n0==0)
200 {
201 *fLog << err << "ERROR - No input files of sequence found!" << endl;
202 return kFALSE;
203 }
204 if (n0!=n1)
205 {
206 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
207 return kFALSE;
208 }
209
210 // Read File
211 /*
212 MCalibrationIntensityChargeCam calcam;
213 MCalibrationIntensityQECam qecam;
214 MCalibrationIntensityBlindCam bndcam;
215 MCalibrationIntensityRelTimeCam tmcam;
216 */
217 MCalibrationChargeCam calcam;
218 MCalibrationQECam qecam;
219 MCalibrationBlindCam bndcam;
220 MCalibrationChargePINDiode pind;
221 MCalibrationRelTimeCam tmcam;
222 MBadPixelsCam badpix;
223
224 MTask *extractor1=0;
225 MTask *extractor2=0;
226 TString geom;
227
228 TObjArray calibcont;
229 calibcont.Add(&calcam);
230 calibcont.Add(&qecam);
231 calibcont.Add(&bndcam);
232 calibcont.Add(&pind);
233 calibcont.Add(&tmcam);
234
235 if (!ReadCalibration(calibcont, badpix, extractor1, extractor2, geom))
236 return kFALSE;
237
238 *fLog << all;
239 if (extractor1)
240 {
241 *fLog << underline << "Signal Extractor found in calibration file" << endl;
242 extractor1->Print();
243 *fLog << endl;
244 }
245 else
246 *fLog << inf << "No Signal Extractor: ExtractSignal in file." << endl;
247
248 if (extractor2)
249 {
250 *fLog << underline << "Time Extractor found in calibration file" << endl;
251 extractor2->Print();
252 *fLog << endl;
253 }
254 else
255 *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
256
257 if (!geom.IsNull())
258 *fLog << inf << "Camera geometry found in file: " << geom << endl;
259 else
260 *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
261
262 // This is necessary for the case in which it is not in the files
263 MBadPixelsCam badcam;
264
265 // Setup Parlist
266 MParList plist;
267 plist.AddToList(this); // take care of fDisplay!
268 plist.AddToList(&badcam);
269 plist.AddToList(&pedcam);
270 plist.AddToList(&calibcont);
271
272 // Setup Tasklist
273 MTaskList tlist;
274 plist.AddToList(&tlist);
275
276 MReadReports readreal;
277 readreal.AddTree("Events", "MTime.", kTRUE);
278 readreal.AddTree("Trigger");
279 readreal.AddTree("Camera");
280 readreal.AddTree("Drive");
281 readreal.AddTree("CC");
282 readreal.AddTree("Currents");
283 readreal.AddFiles(iter);
284
285 MReadMarsFile readmc("Events");
286 readmc.DisableAutoScheme();
287 readmc.AddFiles(iter);
288
289 MGeomApply apply; // Only necessary to create geometry
290 if (!geom.IsNull())
291 apply.SetGeometry(geom);
292 MBadPixelsMerge merge(&badpix);
293 MPedCalcFromLoGain pedlo;
294 MMcPedestalCopy pcopy;
295 MTaskEnv taskenv1("ExtractSignal");
296 MTaskEnv taskenv2("ExtractTime");
297 taskenv1.SetDefault(extractor1);
298 taskenv2.SetDefault(extractor2);
299 MCalibrateData calib;
300 if (ismc)
301 calib.SetCalibrationMode(MCalibrateData::kFfactor);
302 MCalibrateRelTimes caltm;
303 MBadPixelsCalc bpcal;
304 MBadPixelsTreat treat;
305
306 MHCamEvent evt0(0, "PedestalFLG", "Pedestal from Lo Gain;;P [fadc/sl]");
307 MHCamEvent evt1(0, "Extra'd", "Extracted Signal;;S [fadc/sl]");
308 MHCamEvent evt2(0, "PedPhot", "Calibrated Pedestal;;P [\\gamma]");
309 MHCamEvent evt3(1, "PedRMS", "Calibrated Pedestal RMS;;\\sigma_{p} [\\gamma]");
310 MHCamEvent evt4(0, "Interp'd", "Interpolated Signal;;S [\\gamma]");
311 MHCamEvent evt5(2, "Unsuitable", "Unsuitable event ratio;;%");
312 MHCamEvent evt6(0, "Times", "Arrival Time;;T [slice]");
313 evt0.EnableVariance();
314 evt2.EnableVariance();
315 evt3.EnableVariance();
316 evt6.EnableVariance();
317
318 MFillH fill0(&evt0, "MPedestalCam", "FillPedestalFLG");
319 MFillH fill1(&evt1, "MExtractedSignalCam", "FillExtracted");
320 MFillH fill2(&evt2, "MPedPhotCam", "FillPedPhot");
321 MFillH fill3(&evt3, "MPedPhotCam", "FillPedRMS");
322 MFillH fill4(&evt4, "MCerPhotEvt", "FillInterpolated");
323 MFillH fill5(&evt5, "MBadPixelsCam", "FillUnsuitable");
324 MFillH fill6(&evt6, "MArrivalTime", "FillTimes");
325
326 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fPathOut.Data()), fOverwrite);
327 // Run Header
328 write.AddContainer("MRawRunHeader", "RunHeaders");
329 write.AddContainer("MBadPixelsCam", "RunHeaders");
330 write.AddContainer("MGeomCam", "RunHeaders");
331 // Monte Carlo Headers
332 write.AddContainer("MMcTrigHeader", "RunHeaders", kFALSE);
333 write.AddContainer("MMcConfigRunHeader", "RunHeaders", kFALSE);
334 write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
335 // Monte Carlo
336 write.AddContainer("MMcEvt", "Events", kFALSE);
337 write.AddContainer("MMcTrig", "Events", kFALSE);
338 // Data
339 write.AddContainer("MCerPhotEvt", "Events");
340 write.AddContainer("MPedPhotCam", "Events");
341 write.AddContainer("MTime", "Events", kFALSE);
342 write.AddContainer("MRawEvtHeader", "Events");
343 write.AddContainer("MArrivalTime", "Events");
344 // Slow-Control: Current
345 write.AddContainer("MTimeCurrents", "Currents", kFALSE);
346 write.AddContainer("MCameraDC", "Currents", kFALSE);
347 write.AddContainer("MReportCurrents", "Currents", kFALSE);
348 // Slow-Control: Camera
349 write.AddContainer("MReportCamera", "Camera", kFALSE);
350 write.AddContainer("MTimeCamera", "Camera", kFALSE);
351 write.AddContainer("MCameraAUX", "Camera", kFALSE);
352 write.AddContainer("MCameraCalibration", "Camera", kFALSE);
353 write.AddContainer("MCameraCooling", "Camera", kFALSE);
354 write.AddContainer("MCameraHV", "Camera", kFALSE);
355 write.AddContainer("MCameraLV", "Camera", kFALSE);
356 write.AddContainer("MCameraLids", "Camera", kFALSE);
357 // Slow-Control: Trigger
358 write.AddContainer("MReportTrigger", "Trigger", kFALSE);
359 write.AddContainer("MTimeTrigger", "Trigger", kFALSE);
360 // Slow-Control: Drive
361 write.AddContainer("MPointingPos", "Drive", kFALSE);
362 write.AddContainer("MReportDrive", "Drive", kFALSE);
363 write.AddContainer("MTimeDrive", "Drive", kFALSE);
364 // Slow-Control: Central Control
365 write.AddContainer("MReportCC", "CC", kFALSE);
366 write.AddContainer("MTimeCC", "CC", kFALSE);
367
368 // Now setup tasklist for events
369 MTaskList tlist2;
370 tlist2.AddToList(&apply);
371 tlist2.AddToList(&merge);
372 tlist2.AddToList(ismc ? (MTask*)&pcopy : (MTask*)&pedlo);
373 tlist2.AddToList(&fill0);
374 if (extractor1)
375 tlist2.AddToList(&taskenv1);
376 if (extractor2)
377 tlist2.AddToList(&taskenv2);
378 tlist2.AddToList(&fill1);
379 tlist2.AddToList(&calib);
380 tlist2.AddToList(&caltm);
381 tlist2.AddToList(&fill2);
382 tlist2.AddToList(&bpcal);
383 tlist2.AddToList(&treat);
384 tlist2.AddToList(&fill3);
385 tlist2.AddToList(&fill4);
386 tlist2.AddToList(&fill5);
387 tlist2.AddToList(&fill6);
388
389 // Setup List for Drive-tree
390 MPointingPosCalc pcalc;
391
392 // Now setup main tasklist
393 tlist.AddToList(ismc ? (MTask*)&readmc : (MTask*)&readreal);
394 tlist.AddToList(&tlist2, "Events");
395 tlist.AddToList(&pcalc, "Drive");
396 tlist.AddToList(&write);
397
398 // Create and setup the eventloop
399 MEvtLoop evtloop(fName);
400 evtloop.SetParList(&plist);
401 evtloop.SetDisplay(fDisplay);
402 evtloop.SetLogStream(fLog);
403 if (!SetupEnv(evtloop))
404 return kFALSE;
405
406 // Execute first analysis
407 if (!evtloop.Eventloop(fMaxEvents))
408 {
409 *fLog << err << GetDescriptor() << ": Failed." << endl;
410 return kFALSE;
411 }
412
413 tlist.PrintStatistics();
414
415 if (!WriteResult())
416 return kFALSE;
417
418 *fLog << all << GetDescriptor() << ": Done." << endl;
419 *fLog << endl << endl;
420
421 return kTRUE;
422}
Note: See TracBrowser for help on using the repository browser.