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

Last change on this file since 4724 was 4723, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 10.7 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 "MSequence.h"
46#include "MDirIter.h"
47#include "MParList.h"
48#include "MTaskList.h"
49#include "MEvtLoop.h"
50
51#include "MJPedestal.h"
52#include "MStatusDisplay.h"
53
54#include "MHCamEvent.h"
55#include "MPedestalCam.h"
56#include "MBadPixelsCam.h"
57#include "MCalibrationQECam.h"
58#include "MCalibrationChargeCam.h"
59#include "MCalibrationRelTimeCam.h"
60
61#include "MReadMarsFile.h"
62#include "MGeomApply.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 : fOverwrite(kFALSE), fMaxEvents(0)
86{
87 fName = name ? name : "MJCalibrateSignal";
88 fTitle = title ? title : "Tool to calibrate data";
89}
90
91MJCalibrateSignal::~MJCalibrateSignal()
92{
93}
94
95Bool_t MJCalibrateSignal::WriteResult(const MSequence &seq)
96{
97 if (fOutputPath.IsNull())
98 return kTRUE;
99
100 const TString oname = Form("%s/calped%06d.root", (const char*)fOutputPath, seq.GetSequence());
101
102 *fLog << inf << "Writing to file: " << oname << endl;
103
104 TFile file(oname, "RECREATE");
105
106 *fLog << inf << " - MStatusDisplay..." << flush;
107 if (fDisplay && fDisplay->Write()<=0)
108 {
109 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
110 return kFALSE;
111 }
112 *fLog << inf << "ok." << endl;
113
114 return kTRUE;
115}
116
117void MJCalibrateSignal::SetOutputPath(const char *path)
118{
119 fOutputPath = path;
120 if (fOutputPath.EndsWith("/"))
121 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
122}
123void MJCalibrateSignal::SetInputPath(const char *path)
124{
125 fInputPath = path;
126 if (fInputPath.EndsWith("/"))
127 fInputPath = fInputPath(0, fInputPath.Length()-1);
128}
129
130void MJCalibrateSignal::CheckEnv(const char *env)
131{
132 TEnv e(env);
133
134 TString e1 = e.GetValue("MJCalibrateSignal.OutputPath", "");
135 if (!e1.IsNull())
136 {
137 e1.ReplaceAll("\015", "");
138 SetOutputPath(e1);
139 }
140
141 fMaxEvents = e.GetValue("MJCalibrateSignal.MaxEvents", fMaxEvents);
142}
143
144Bool_t MJCalibrateSignal::ReadCalibration(const MSequence &seq, const char *path,
145 MCalibrationCam &calcam,
146 MCalibrationCam &qecam,
147 MCalibrationCam &tmcam,
148 MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2) const
149{
150 TString inpath(path);
151 if (inpath.EndsWith("/"))
152 inpath = inpath(0, inpath.Length()-1);
153
154 const TString fname = Form("%s/calib%06d.root", inpath.Data(), seq.GetSequence());
155
156 TFile file(fname, "READ");
157 if (!file.IsOpen())
158 {
159 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
160 return kFALSE;
161 }
162
163 if (calcam.Read("MCalibrationChargeCam")<=0)
164 {
165 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationChargeCam from file " << fname << endl;
166 return kFALSE;
167 }
168 if (qecam.Read("MCalibrationQECam")<=0)
169 {
170 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationQECam from file " << fname << endl;
171 return kFALSE;
172 }
173 if (tmcam.Read("MCalibrationRelTimeCam")<=0)
174 {
175 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationRelTimeCam from file " << fname << endl;
176 return kFALSE;
177 }
178 if (badpix.Read("MBadPixelsCam")<=0)
179 {
180 *fLog << err << dbginf << "ERROR - Unable to read MBadPixelsCam from file " << fname << endl;
181 return kFALSE;
182 }
183
184 TObject *o = file.Get("ExtractSignal");
185 if (!o)
186 {
187 *fLog << err << dbginf << "ERROR - Unable to read ExtractSignal from file " << fname << endl;
188 return kFALSE;
189 }
190 if (!o->InheritsFrom(MExtractor::Class()))
191 {
192 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
193 return kFALSE;
194 }
195 ext1 = (MTask*)o->Clone();
196
197 o = file.Get("ExtractTime");
198 if (!o)
199 {
200 *fLog << err << dbginf << "ERROR - Unable to read ExtractTime from file " << fname << endl;
201 return kFALSE;
202 }
203 if (!o->InheritsFrom(MExtractor::Class()))
204 {
205 *fLog << err << dbginf << "ERROR - ExtractTime read from " << fname << " doesn't inherit from MExtractor!" << endl;
206 return kFALSE;
207 }
208 ext2 = (MTask*)o->Clone();
209
210 return kTRUE;
211}
212
213Bool_t MJCalibrateSignal::ProcessFile(const MSequence &seq, const char *env, const char *path)
214{
215 *fLog << inf;
216 fLog->Separator(GetDescriptor());
217 *fLog << "Calculate calibrated data from runs ";
218 *fLog << seq.GetName() << endl;
219 *fLog << endl;
220
221 CheckEnv(env);
222
223 // -------------------------------------------------------------------------------
224
225 MJPedestal job1;
226 job1.SetSequence(&seq);
227 job1.SetDisplay(fDisplay);;
228 job1.SetEnv(env);
229
230 if (!job1.ProcessFile())
231 return kFALSE;
232
233 // --------------------------------------------------------------------------------
234
235 MDirIter iter;
236 if (seq.SetupDatRuns(iter, fInputPath)==0)
237 {
238 *fLog << err << "ERROR - No input files of sequence found!" << endl;
239 return kFALSE;
240 }
241
242 // Read File
243 MCalibrationChargeCam calcam;
244 MCalibrationQECam qecam;
245 MCalibrationRelTimeCam tmcam;
246 MBadPixelsCam badpix;
247
248 MTask *extractor1=0;
249 MTask *extractor2=0;
250
251 if (!ReadCalibration(seq, path, calcam, qecam, tmcam, badpix, extractor1, extractor2))
252 return kFALSE;
253
254 *fLog << all;
255 *fLog << "Extractors read from file" << endl;
256 *fLog << "=========================" << endl;
257 extractor1->Print();
258 *fLog << endl;
259 extractor2->Print();
260 *fLog << 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(&calcam);
269 plist.AddToList(&qecam);
270 plist.AddToList(&tmcam);
271 plist.AddToList(&badcam);
272 plist.AddToList(&job1.GetPedestalCam());
273
274 // Setup Tasklist
275 MTaskList tlist;
276 plist.AddToList(&tlist);
277
278 MReadMarsFile read("Events");
279 read.DisableAutoScheme();
280 read.AddFiles(iter);
281 //read.AddFiles(fnamein);
282
283 MGeomApply apply; // Only necessary to craete geometry
284 MBadPixelsMerge merge(&badpix);
285 MPedCalcFromLoGain pedlo;
286 //MExtractSlidingWindow extsignal;
287 MTaskEnv taskenv1("ExtractSignal");
288 taskenv1.SetDefault(extractor1);
289 //MExtractTimeFastSpline exttime;
290 MTaskEnv taskenv2("ExtractTime");
291 taskenv2.SetDefault(extractor2);
292 MCalibrateData calib;
293 MCalibrateRelTimes caltm;
294 MBadPixelsCalc bpcal;
295 MBadPixelsTreat treat;
296
297 MHCamEvent evt0("PedestalFLG");
298 MHCamEvent evt1("Extra'd");
299 MHCamEvent evt2("PedPhot");
300 MHCamEvent evt3("Interp'd");
301 MHCamEvent evt4("Unsuitable");
302 MHCamEvent evt5("Times");
303 evt0.SetType(0);
304 evt1.SetType(0);
305 evt2.SetType(0);
306 evt3.SetType(0);
307 evt4.SetType(2);
308 evt5.SetType(0);
309 MFillH fill0(&evt0, "MPedestalCam", "FillPedestalFLG");
310 MFillH fill1(&evt1, "MExtractedSignalCam", "FillExtracted");
311 MFillH fill2(&evt2, "MPedPhotCam", "FillPedPhot");
312 MFillH fill3(&evt3, "MCerPhotEvt", "FillInterpolated");
313 MFillH fill4(&evt4, "MBadPixelsCam", "FillUnsuitable");
314 MFillH fill5(&evt5, "MArrivalTime", "FillTimes");
315
316 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fOutputPath.Data()), fOverwrite);
317 write.AddContainer("MCerPhotEvt", "Events");
318 write.AddContainer("MPedPhotCam", "Events");
319 write.AddContainer("MTime", "Events");
320 write.AddContainer("MRawEvtHeader", "Events");
321 write.AddContainer("MArrivalTime", "Events");
322 write.AddContainer("MRawRunHeader", "RunHeaders");
323 write.AddContainer("MBadPixelsCam", "RunHeaders");
324 write.AddContainer("MGeomCam", "RunHeaders");
325
326 tlist.AddToList(&read);
327 tlist.AddToList(&apply);
328 tlist.AddToList(&merge);
329 tlist.AddToList(&pedlo);
330 tlist.AddToList(&fill0);
331 tlist.AddToList(&taskenv1);
332 tlist.AddToList(&taskenv2);
333 tlist.AddToList(&fill1);
334 tlist.AddToList(&calib);
335 tlist.AddToList(&caltm);
336 tlist.AddToList(&fill2);
337 tlist.AddToList(&bpcal);
338 tlist.AddToList(&treat);
339 tlist.AddToList(&fill3);
340 tlist.AddToList(&fill4);
341 tlist.AddToList(&fill5);
342 tlist.AddToList(&write);
343
344 // Create and setup the eventloop
345 MEvtLoop evtloop(fName);
346 evtloop.SetParList(&plist);
347 evtloop.SetDisplay(fDisplay);
348 evtloop.SetLogStream(fLog);
349 evtloop.ReadEnv(env);
350
351 // Execute first analysis
352 if (!evtloop.Eventloop(fMaxEvents))
353 {
354 *fLog << err << GetDescriptor() << ": Failed." << endl;
355 return kFALSE;
356 }
357
358 tlist.PrintStatistics();
359
360 if (!WriteResult(seq))
361 return kFALSE;
362
363 *fLog << all << GetDescriptor() << ": Done." << endl;
364
365 return kTRUE;
366}
Note: See TracBrowser for help on using the repository browser.