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

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