source: trunk/MagicSoft/Mars/mjobs/MJMerpp.cc@ 9143

Last change on this file since 9143 was 9141, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 12.0 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, 7/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJMerpp
28//
29// Merging and preprocessing
30//
31/////////////////////////////////////////////////////////////////////////////
32#include "MJMerpp.h"
33
34#include <TFile.h>
35#include <TTree.h>
36
37#include "MLogManip.h"
38
39#include "MParList.h"
40#include "MTaskList.h"
41#include "MEvtLoop.h"
42
43#include "MFDataPhrase.h"
44
45#include "MRawRunHeader.h"
46
47#include "MRawFileRead.h"
48#include "MReportFileReadCC.h"
49#include "MWriteRootFile.h"
50
51ClassImp(MJMerpp);
52
53using namespace std;
54
55MJMerpp::MJMerpp(const char *name, const char *title)
56{
57 fName = name ? name : "MJMerpp";
58 fTitle = title ? title : "Standard program for merging subsystem data";
59}
60
61void MJMerpp::AddTree(MReportFileReadCC &read, const TString &rep, const TString &only) const
62{
63 if (!only.IsNull() && rep!=only)
64 return;
65
66 read.AddToList(Form("MReport%s", rep.Data()));
67}
68
69MFilter *MJMerpp::SetupCurrents(MReportFileReadCC &read, MWriteRootFile &write) const
70{
71 read.SetHasNoHeader();
72 read.AddToList("MReportCurrents");
73
74 write.AddContainer("MTimeCurrents", "Currents");
75 write.AddContainer("MCameraDC", "Currents");
76 write.AddContainer("MReportCurrents", "Currents");
77
78 return 0;
79}
80
81MFilter *MJMerpp::SetupReports(MReportFileReadCC &read, MWriteRootFile &write) const
82{
83 read.SetTelescope(fTelescope);
84
85 read.SetRunNumber(fHeaderRun);
86 read.SetFileNumber(fHeaderFile);
87
88 AddTree(read, "CC", fOnly);
89 AddTree(read, "Rec", fOnly);
90 AddTree(read, "Drive", fOnly);
91 AddTree(read, "Camera", fOnly);
92 AddTree(read, "Trigger", fOnly);
93 AddTree(read, "Starguider", fOnly);
94 AddTree(read, "Pyrometer", fOnly);
95 // AddTree(read, "DAQ", fOnly);
96
97 const Bool_t required = fOnly.IsNull();
98 write.AddContainer("MReportCamera", "Camera", required);
99 write.AddContainer("MTimeCamera", "Camera", required);
100 write.AddContainer("MCameraAUX", "Camera", required);
101 write.AddContainer("MCameraCalibration", "Camera", required);
102 write.AddContainer("MCameraCooling", "Camera", required);
103 write.AddContainer("MCameraActiveLoad", "Camera", required);
104 write.AddContainer("MCameraHV", "Camera", required);
105 write.AddContainer("MCameraDC", "Camera", required);
106 write.AddContainer("MCameraLV", "Camera", required);
107 write.AddContainer("MCameraLids", "Camera", required);
108 write.AddContainer("MReportTrigger", "Trigger", required);
109 write.AddContainer("MTimeTrigger", "Trigger", required);
110 write.AddContainer("MTriggerBit", "Trigger", required);
111 write.AddContainer("MTriggerIPR", "Trigger", required);
112 write.AddContainer("MTriggerCell", "Trigger", required);
113 write.AddContainer("MTriggerPrescFact", "Trigger", required);
114 write.AddContainer("MTriggerLiveTime", "Trigger", required);
115 write.AddContainer("MReportDrive", "Drive", required);
116 write.AddContainer("MTimeDrive", "Drive", required);
117 write.AddContainer("MCameraTH", "Rec", required);
118 write.AddContainer("MCameraTD", "Rec", required);
119 write.AddContainer("MCameraRecTemp", "Rec", required);
120 write.AddContainer("MReportRec", "Rec", required);
121 write.AddContainer("MTimeRec", "Rec", required);
122 write.AddContainer("MReportCC", "CC", required);
123 write.AddContainer("MTimeCC", "CC", required);
124 write.AddContainer("MReportStarguider", "Starguider", required);
125 write.AddContainer("MTimeStarguider", "Starguider", required);
126 write.AddContainer("MReportPyrometer", "Pyrometer", required);
127 write.AddContainer("MTimePyrometer", "Pyrometer", required);
128 // write.AddContainer("MReportDAQ", "DAQ");
129 // write.AddContainer("MTimeDAQ", "DAQ");
130
131 if (fReportRun==(UInt_t)-1)
132 return 0;
133
134 const TString f1 = fReportRun>0 ? Form("MReportRun.fRunNumber==%ud", fReportRun) : "";
135 const TString f2 = fReportFile>=0 ? Form("MReportRun.fFileNumber==%ud", fReportFile) : "";
136
137 const TString f = Form(fReportRun>0 && fReportFile>=0 ? "%s && %s" : "%s%s",
138 f1.Data(), f2.Data());
139
140 read.AddToList("MReportRun");
141
142 MFilter *filter = new MFDataPhrase(f.Data());
143 write.SetFilter(filter);
144 return filter;
145}
146
147void MJMerpp::SetupRaw(MRawFileRead &read, MWriteRootFile &write) const
148{
149 read.SetInterleave(fInterleave);
150 read.SetForceMode(fForceProcessing);
151
152 write.AddContainer("MRawRunHeader", "RunHeaders");
153 write.AddContainer("MTime", "Events");
154 write.AddContainer("MRawEvtHeader", "Events");
155 write.AddContainer("MRawEvtData", "Events");
156 write.AddContainer("MRawEvtData2", "Events");
157 write.AddContainer("MRawCrateArray", "Events");
158}
159
160Bool_t MJMerpp::GetTimeFromFile()
161{
162 if (!fAutoStartTime && !fAutoStopTime)
163 return kTRUE;
164
165 TFile f(fPathOut, "READ");
166 if (f.IsZombie())
167 {
168 *fLog << err << "ERROR - File " << fPathOut << " could not be opened." << endl;
169 return kFALSE;
170 }
171
172 TTree *t = (TTree*)f.Get("RunHeaders");
173 if (t->GetEntries()!=1)
174 {
175 *fLog << err << "ERROR - File " << fPathOut << " contains no or more than one entry in RunHeaders... Times unchanged." << endl;
176 return kFALSE;
177 }
178
179 MRawRunHeader *h = 0;
180 t->SetBranchAddress("MRawRunHeader.", &h);
181 t->GetEntry(0);
182 if (!h)
183 {
184 *fLog << err << "ERROR - File " << fPathOut << " did not contain RunHeaders.MRawRunHeader... Times unchanged." << endl;
185 return kFALSE;
186 }
187
188 if (fAutoStartTime)
189 fTimeStart = h->GetRunStart();
190 if (fAutoStopTime)
191 fTimeStop = h->GetRunEnd();
192
193 return kTRUE;
194}
195
196Bool_t MJMerpp::CheckFilePermissions()
197{
198 //
199 // check whether the given files are OK.
200 //
201 if (gSystem->AccessPathName(fPathIn, kFileExists))
202 {
203 *fLog << err << "ERROR - Input file '" << fPathIn << "' not accessible." << endl;
204 return kFALSE;
205 }
206
207 const Bool_t fileexist = !gSystem->AccessPathName(fPathOut, kFileExists);
208 const Bool_t writeperm = !gSystem->AccessPathName(fPathOut, kWritePermission);
209
210 if (fileexist && !writeperm)
211 {
212 *fLog << err << "ERROR - No write permission for '" << fPathOut << "'." << endl;
213 return kFALSE;
214 }
215
216 if (fileexist && !fUpdate && !fOverwrite)
217 {
218 *fLog << err << "ERROR - File '" << fPathOut << "' already existing." << endl;
219 return kFALSE;
220 }
221
222 if (!fileexist && fUpdate)
223 {
224 *fLog << err << "ERROR - File '" << fPathOut << "' doesn't yet exist... no update possible." << endl;
225 return kFALSE;
226 }
227
228 return kTRUE;
229}
230
231Int_t MJMerpp::Process()
232{
233 *fLog << inf;
234 fLog->Separator(GetDescriptor());
235 *fLog << "In: " << fPathIn << endl;;
236 *fLog << (fUpdate?"Upd":"Out") << ": " << fPathOut << endl;;
237 *fLog << endl;
238
239 if (!CheckFilePermissions())
240 return 1;
241
242 // FIXME: Add a check whether the file has already been merpped!!!
243
244 const Bool_t isreport = fPathIn.EndsWith(".rep");
245 const Bool_t isdc = fPathIn.EndsWith(".txt");
246
247 //
248 // Evaluate possible start-/stop-time
249 //
250 if (fUpdate && (isreport || isdc))
251 if (!GetTimeFromFile())
252 return 2;
253
254 if (fTimeStart)
255 *fLog << inf << "Start Time: " << fTimeStart << endl;
256 if (fTimeStop)
257 *fLog << inf << "Stop Time: " << fTimeStop << endl;
258
259 //
260 // create a (empty) list of parameters which can be used by the tasks
261 // and an (empty) list of tasks which should be executed
262 //
263 MParList plist;
264
265 MTaskList tasks;
266 tasks.SetOwner();
267 plist.AddToList(&tasks);
268
269 //
270 // create the tasks which should be executed and add them to the list
271 // in the case you don't need parameter containers, all of them can
272 // be created by MRawFileRead::PreProcess
273 //
274 const TString option(fUpdate?"UPDATE":(fOverwrite?"RECREATE":"NEW"));
275
276 MTask *read = 0;
277 MFilter *filter = 0;
278 MWriteRootFile *write = new MWriteRootFile(fPathOut, option, "Magic root-file", fCompression);
279
280 if (isreport || isdc)
281 {
282 MReportFileReadCC *r = new MReportFileReadCC(fPathIn);
283 r->SetTimeStart(fTimeStart);
284 r->SetTimeStop(fTimeStop);
285
286 if (isdc)
287 filter = SetupCurrents(*r, *write);
288 else
289 filter = SetupReports(*r, *write);
290
291 read = r;
292 }
293 else
294 {
295 MRawFileRead *r = new MRawFileRead(fPathIn);
296 SetupRaw(*r, *write);
297 read = r;
298 }
299
300 tasks.AddToList(read);
301 if (filter)
302 tasks.AddToList(filter);
303
304 if (!HasNullOut())
305 tasks.AddToList(write);
306
307 // Create and setup the eventloop
308 MEvtLoop evtloop(fName);
309 evtloop.SetParList(&plist);
310 evtloop.SetLogStream(fLog);
311 //evtloop.SetDisplay(fDisplay);
312 //if (!SetupEnv(evtloop))
313 // return kFALSE;
314
315 // Execute first analysis
316 if (!evtloop.Eventloop(fMaxEvents))
317 {
318 *fLog << err << GetDescriptor() << ": Failed." << endl;
319 return 3;
320 }
321
322 *fLog << all << GetDescriptor() << ": Done." << endl << endl << endl;;
323
324 return 0;
325}
326
327Int_t MJMerpp::ProcessSeq(TString fname)
328{
329 if (!MSequence::InflateSeq(fname))
330 return 4;
331
332 MSequence seq(fname);
333 if (!seq.IsValid())
334 return 5;
335
336 const UInt_t num = seq.GetNumEntries(MSequence::kDat);
337
338 const TString reppath = fPathIn;
339 const TString calpath = fPathOut;
340
341 // PreCheck: We don't wantto start merpp if we
342 // know that an error will occur
343 for (UInt_t i=0; i<num; i++)
344 {
345 const TString name1 = fHeaderRun==0 ? reppath : seq.GetFileName(i, MSequence::kReportDat, reppath);
346 const TString name2 = seq.GetFileName(i, MSequence::kCalibrated, calpath);
347
348 // Full qualified name could not be determined or file is not
349 // accessible. For excluded files "0" is returned.
350 if (name1.IsNull() || name2.IsNull())
351 return 6;
352 }
353
354 // Now we can safely start processing
355 TString rc;
356 for (UInt_t i=0; i<num; i++)
357 {
358 UInt_t run, file;
359
360 // Excluded
361 if (seq.GetFile(i, MSequence::kDat, run, file)==0)
362 continue;
363
364 // fHeaderRun==0 means: All day summary file
365 const TString name1 = fHeaderRun==0 ? reppath : seq.GetFileName(i, MSequence::kReportDat, reppath);
366 const TString name2 = seq.GetFileName(i, MSequence::kCalibrated, calpath);
367
368 // FIXME: check runcallisto
369 if (fHeaderRun==0) // extract valid range from summary file
370 SetConstrainRunRep(run, file); // (this is not guranteed to work :-( )
371 else // check for the correct header
372 SetConstrainHeader(seq.GetTelescope(), run, file);
373
374 SetTime(MTime(), MTime()); // Raise error if set?
375 SetPathIn(name1);
376 SetPathOut(name2);
377
378 const Int_t rc = Process();
379 if (rc==0)
380 continue;
381
382 return rc;
383 }
384 return 0;
385}
Note: See TracBrowser for help on using the repository browser.