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

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