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

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