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

Last change on this file since 4753 was 4752, checked in by tbretz, 20 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 "MDirIter.h"
46#include "MParList.h"
47#include "MTaskList.h"
48#include "MEvtLoop.h"
49
50#include "MJPedestal.h"
51#include "MStatusDisplay.h"
52
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 "MReadMarsFile.h"
61#include "MGeomApply.h"
62#include "MPedCalcFromLoGain.h"
63#include "MExtractor.h"
64#include "MTaskEnv.h"
65#include "MCalibrateData.h"
66#include "MCalibrateRelTimes.h"
67#include "MBadPixelsMerge.h"
68#include "MBadPixelsCalc.h"
69#include "MBadPixelsTreat.h"
70#include "MFillH.h"
71#include "MWriteRootFile.h"
72
73ClassImp(MJCalibrateSignal);
74
75using namespace std;
76
77// --------------------------------------------------------------------------
78//
79// Default constructor.
80//
81// Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE
82//
83MJCalibrateSignal::MJCalibrateSignal(const char *name, const char *title)
84{
85 fName = name ? name : "MJCalibrateSignal";
86 fTitle = title ? title : "Tool to calibrate data";
87}
88
89Bool_t MJCalibrateSignal::WriteResult()
90{
91 if (fPathOut.IsNull())
92 {
93 *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
94 return kTRUE;
95 }
96
97 const TString oname = Form("%s/signal%06d.root", (const char*)fPathOut, fSequence.GetSequence());
98
99 *fLog << inf << "Writing to file: " << oname << endl;
100
101 TFile file(oname, "RECREATE");
102
103 *fLog << inf << " - MStatusDisplay..." << flush;
104 if (fDisplay && fDisplay->Write()<=0)
105 {
106 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
107 return kFALSE;
108 }
109 *fLog << inf << "ok." << endl;
110
111 return kTRUE;
112}
113
114Bool_t MJCalibrateSignal::ReadCalibration(MCalibrationCam &calcam,
115 MCalibrationCam &qecam,
116 MCalibrationCam &tmcam,
117 MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2) const
118{
119 const TString fname = Form("%s/calib%06d.root", fPathIn.Data(), fSequence.GetSequence());
120
121 TFile file(fname, "READ");
122 if (!file.IsOpen())
123 {
124 *fLog << err << dbginf << "ERROR - Could not open file " << fname << endl;
125 return kFALSE;
126 }
127
128 if (calcam.Read("MCalibrationChargeCam")<=0)
129 {
130 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationChargeCam from file " << fname << endl;
131 return kFALSE;
132 }
133 if (qecam.Read("MCalibrationQECam")<=0)
134 {
135 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationQECam from file " << fname << endl;
136 return kFALSE;
137 }
138 if (tmcam.Read("MCalibrationRelTimeCam")<=0)
139 {
140 *fLog << err << dbginf << "ERROR - Unable to read MCalibrationRelTimeCam from file " << fname << endl;
141 return kFALSE;
142 }
143 if (badpix.Read("MBadPixelsCam")<=0)
144 {
145 *fLog << err << dbginf << "ERROR - Unable to read MBadPixelsCam from file " << fname << endl;
146 return kFALSE;
147 }
148
149 TObject *o = file.Get("ExtractSignal");
150// if (!o)
151// {
152// *fLog << err << dbginf << "ERROR - Unable to read ExtractSignal from file " << fname << endl;
153// return kFALSE;
154// }
155 if (o && !o->InheritsFrom(MExtractor::Class()))
156 {
157 *fLog << err << dbginf << "ERROR - ExtractSignal read from " << fname << " doesn't inherit from MExtractor!" << endl;
158 return kFALSE;
159 }
160 ext1 = o ? (MTask*)o->Clone() : NULL;
161
162 o = file.Get("ExtractTime");
163// if (!o)
164// {
165// *fLog << err << dbginf << "ERROR - Unable to read ExtractTime from file " << fname << endl;
166// return kFALSE;
167// }
168 if (o && !o->InheritsFrom(MExtractor::Class()))
169 {
170 *fLog << err << dbginf << "ERROR - ExtractTime read from " << fname << " doesn't inherit from MExtractor!" << endl;
171 return kFALSE;
172 }
173 ext2 = o ? (MTask*)o->Clone() : NULL;
174
175 if (!ext1 && !ext2)
176 {
177 *fLog << err << dbginf << "ERROR - Neither ExtractSignal nor ExrtractTime found in " << fname << "!" << endl;
178 return kFALSE;
179 }
180
181 return kTRUE;
182}
183
184Bool_t MJCalibrateSignal::ProcessFile(MPedestalCam &pedcam)
185{
186 if (!fSequence.IsValid())
187 {
188 *fLog << err << "ERROR - Sequence invalid!" << endl;
189 return kFALSE;
190 }
191
192 //if (!CheckEnv())
193 // return kFALSE;
194
195 CheckEnv();
196
197 // --------------------------------------------------------------------------------
198
199 *fLog << inf;
200 fLog->Separator(GetDescriptor());
201 *fLog << "Calculate calibrated data from runs ";
202 *fLog << fSequence.GetName() << endl;
203 *fLog << endl;
204
205 // --------------------------------------------------------------------------------
206
207 MDirIter iter;
208 const Int_t n0 = fSequence.SetupDatRuns(iter, fPathData);
209 const Int_t n1 = fSequence.GetNumDatRuns();
210 if (n0==0)
211 {
212 *fLog << err << "ERROR - No input files of sequence found!" << endl;
213 return kFALSE;
214 }
215 if (n0!=n1)
216 {
217 *fLog << err << "ERROR - Number of files found (" << n0 << ") doesn't match number of files in sequence (" << n1 << ")" << endl;
218 return kFALSE;
219 }
220
221 // Read File
222 MCalibrationChargeCam calcam;
223 MCalibrationQECam qecam;
224 MCalibrationRelTimeCam tmcam;
225 MBadPixelsCam badpix;
226
227 MTask *extractor1=0;
228 MTask *extractor2=0;
229
230 if (!ReadCalibration(calcam, qecam, tmcam, badpix, extractor1, extractor2))
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 // This is necessary for the case in which it is not in the files
253 MBadPixelsCam badcam;
254
255 // Setup Parlist
256 MParList plist;
257 plist.AddToList(this); // take care of fDisplay!
258 plist.AddToList(&calcam);
259 plist.AddToList(&qecam);
260 plist.AddToList(&tmcam);
261 plist.AddToList(&badcam);
262 plist.AddToList(&pedcam);
263
264 // Setup Tasklist
265 MTaskList tlist;
266 plist.AddToList(&tlist);
267
268 MReadMarsFile read("Events");
269 read.DisableAutoScheme();
270 read.AddFiles(iter);
271 //read.AddFiles(fnamein);
272
273 MGeomApply apply; // Only necessary to craete geometry
274 MBadPixelsMerge merge(&badpix);
275 MPedCalcFromLoGain pedlo;
276 //MExtractSlidingWindow extsignal;
277 MTaskEnv taskenv1("ExtractSignal");
278 taskenv1.SetDefault(extractor1);
279 //MExtractTimeFastSpline exttime;
280 MTaskEnv taskenv2("ExtractTime");
281 taskenv2.SetDefault(extractor2);
282 MCalibrateData calib;
283 MCalibrateRelTimes caltm;
284 MBadPixelsCalc bpcal;
285 MBadPixelsTreat treat;
286
287 MHCamEvent evt0("PedestalFLG");
288 MHCamEvent evt1("Extra'd");
289 MHCamEvent evt2("PedPhot");
290 MHCamEvent evt3("Interp'd");
291 MHCamEvent evt4("Unsuitable");
292 MHCamEvent evt5("Times");
293 evt0.SetType(0);
294 evt1.SetType(0);
295 evt2.SetType(0);
296 evt3.SetType(0);
297 evt4.SetType(2);
298 evt5.SetType(0);
299 MFillH fill0(&evt0, "MPedestalCam", "FillPedestalFLG");
300 MFillH fill1(&evt1, "MExtractedSignalCam", "FillExtracted");
301 MFillH fill2(&evt2, "MPedPhotCam", "FillPedPhot");
302 MFillH fill3(&evt3, "MCerPhotEvt", "FillInterpolated");
303 MFillH fill4(&evt4, "MBadPixelsCam", "FillUnsuitable");
304 MFillH fill5(&evt5, "MArrivalTime", "FillTimes");
305
306 MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fPathOut.Data()), fOverwrite);
307 write.AddContainer("MCerPhotEvt", "Events");
308 write.AddContainer("MPedPhotCam", "Events");
309 write.AddContainer("MTime", "Events");
310 write.AddContainer("MRawEvtHeader", "Events");
311 write.AddContainer("MArrivalTime", "Events");
312 write.AddContainer("MRawRunHeader", "RunHeaders");
313 write.AddContainer("MBadPixelsCam", "RunHeaders");
314 write.AddContainer("MGeomCam", "RunHeaders");
315
316 tlist.AddToList(&read);
317 tlist.AddToList(&apply);
318 tlist.AddToList(&merge);
319 tlist.AddToList(&pedlo);
320 tlist.AddToList(&fill0);
321 if (extractor1)
322 tlist.AddToList(&taskenv1);
323 if (extractor2)
324 tlist.AddToList(&taskenv2);
325 tlist.AddToList(&fill1);
326 tlist.AddToList(&calib);
327 tlist.AddToList(&caltm);
328 tlist.AddToList(&fill2);
329 tlist.AddToList(&bpcal);
330 tlist.AddToList(&treat);
331 tlist.AddToList(&fill3);
332 tlist.AddToList(&fill4);
333 tlist.AddToList(&fill5);
334 tlist.AddToList(&write);
335
336 // Create and setup the eventloop
337 MEvtLoop evtloop(fName);
338 evtloop.SetParList(&plist);
339 evtloop.SetDisplay(fDisplay);
340 evtloop.SetLogStream(fLog);
341 if (!SetupEnv(evtloop))
342 return kFALSE;
343
344 // Execute first analysis
345 if (!evtloop.Eventloop(fMaxEvents))
346 {
347 *fLog << err << GetDescriptor() << ": Failed." << endl;
348 return kFALSE;
349 }
350
351 tlist.PrintStatistics();
352
353 if (!WriteResult())
354 return kFALSE;
355
356 *fLog << all << GetDescriptor() << ": Done." << endl;
357 *fLog << endl << endl;
358
359 return kTRUE;
360}
Note: See TracBrowser for help on using the repository browser.