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

Last change on this file since 5117 was 5035, 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
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 //MExtractSlidingWindow extsignal;
296 MTaskEnv taskenv1("ExtractSignal");
297 taskenv1.SetDefault(extractor1);
298 //MExtractTimeFastSpline exttime;
299 MTaskEnv taskenv2("ExtractTime");
300 taskenv2.SetDefault(extractor2);
301 MCalibrateData calib;
302 if (ismc)
303 calib.SetCalibrationMode(MCalibrateData::kFfactor);
304 MCalibrateRelTimes caltm;
305 MBadPixelsCalc bpcal;
306 MBadPixelsTreat treat;
307
308 MHCamEvent evt0("PedestalFLG");
309 MHCamEvent evt1("Extra'd");
310 MHCamEvent evt2("PedPhot");
311 MHCamEvent evt3("Interp'd");
312 MHCamEvent evt4("Unsuitable");
313 MHCamEvent evt5("Times");
314 evt0.SetType(0);
315 evt1.SetType(0);
316 evt2.SetType(0);
317 evt3.SetType(0);
318 evt4.SetType(2);
319 evt5.SetType(0);
320 MFillH fill0(&evt0, "MPedestalCam", "FillPedestalFLG");
321 MFillH fill1(&evt1, "MExtractedSignalCam", "FillExtracted");
322 MFillH fill2(&evt2, "MPedPhotCam", "FillPedPhot");
323 MFillH fill3(&evt3, "MCerPhotEvt", "FillInterpolated");
324 MFillH fill4(&evt4, "MBadPixelsCam", "FillUnsuitable");
325 MFillH fill5(&evt5, "MArrivalTime", "FillTimes");
326
327 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fPathOut.Data()), fOverwrite);
328 // Run Header
329 write.AddContainer("MRawRunHeader", "RunHeaders");
330 write.AddContainer("MBadPixelsCam", "RunHeaders");
331 write.AddContainer("MGeomCam", "RunHeaders");
332 // Monte Carlo Headers
333 write.AddContainer("MMcTrigHeader", "RunHeaders", kFALSE);
334 write.AddContainer("MMcConfigRunHeader", "RunHeaders", kFALSE);
335 write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
336 // Monte Carlo
337 write.AddContainer("MMcEvt", "Events", kFALSE);
338 write.AddContainer("MMcTrig", "Events", kFALSE);
339 // Data
340 write.AddContainer("MCerPhotEvt", "Events");
341 write.AddContainer("MPedPhotCam", "Events");
342 write.AddContainer("MTime", "Events", kFALSE);
343 write.AddContainer("MRawEvtHeader", "Events");
344 write.AddContainer("MArrivalTime", "Events");
345 // Slow-Control: Current
346 write.AddContainer("MTimeCurrents", "Currents", kFALSE);
347 write.AddContainer("MCameraDC", "Currents", kFALSE);
348 write.AddContainer("MReportCurrents", "Currents", kFALSE);
349 // Slow-Control: Camera
350 write.AddContainer("MReportCamera", "Camera", kFALSE);
351 write.AddContainer("MTimeCamera", "Camera", kFALSE);
352 write.AddContainer("MCameraAUX", "Camera", kFALSE);
353 write.AddContainer("MCameraCalibration", "Camera", kFALSE);
354 write.AddContainer("MCameraCooling", "Camera", kFALSE);
355 write.AddContainer("MCameraHV", "Camera", kFALSE);
356 write.AddContainer("MCameraLV", "Camera", kFALSE);
357 write.AddContainer("MCameraLids", "Camera", kFALSE);
358 // Slow-Control: Trigger
359 write.AddContainer("MReportTrigger", "Trigger", kFALSE);
360 write.AddContainer("MTimeTrigger", "Trigger", kFALSE);
361 // Slow-Control: Drive
362 write.AddContainer("MPointingPos", "Drive", kFALSE);
363 write.AddContainer("MReportDrive", "Drive", kFALSE);
364 write.AddContainer("MTimeDrive", "Drive", kFALSE);
365 // Slow-Control: Central Control
366 write.AddContainer("MReportCC", "CC", kFALSE);
367 write.AddContainer("MTimeCC", "CC", kFALSE);
368
369 // Now setup tasklist for events
370 MTaskList tlist2;
371 tlist2.AddToList(&apply);
372 tlist2.AddToList(&merge);
373 tlist2.AddToList(ismc ? (MTask*)&pcopy : (MTask*)&pedlo);
374 tlist2.AddToList(&fill0);
375 if (extractor1)
376 tlist2.AddToList(&taskenv1);
377 if (extractor2)
378 tlist2.AddToList(&taskenv2);
379 tlist2.AddToList(&fill1);
380 tlist2.AddToList(&calib);
381 tlist2.AddToList(&caltm);
382 tlist2.AddToList(&fill2);
383 tlist2.AddToList(&bpcal);
384 tlist2.AddToList(&treat);
385 tlist2.AddToList(&fill3);
386 tlist2.AddToList(&fill4);
387 tlist2.AddToList(&fill5);
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.