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 |
|
---|
51 | ClassImp(MJMerpp);
|
---|
52 |
|
---|
53 | using namespace std;
|
---|
54 |
|
---|
55 | MJMerpp::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 |
|
---|
61 | void 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 |
|
---|
69 | MFilter *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 |
|
---|
81 | MFilter *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 |
|
---|
147 | void MJMerpp::SetupRaw(MRawFileRead &read, MWriteRootFile &write) const
|
---|
148 | {
|
---|
149 | read.SetInterleave(fInterleave);
|
---|
150 | read.SetForceMode(fForceProcessing);
|
---|
151 |
|
---|
152 | write.AddContainer("MRawRunHeader", "RunHeader");
|
---|
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 |
|
---|
160 | Bool_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 |
|
---|
196 | Bool_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 |
|
---|
231 | Int_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 | const Bool_t isreport = fPathIn.EndsWith(".rep");
|
---|
243 | const Bool_t isdc = fPathIn.EndsWith(".txt");
|
---|
244 |
|
---|
245 | //
|
---|
246 | // Evaluate possible start-/stop-time
|
---|
247 | //
|
---|
248 | if (fUpdate && (isreport || isdc))
|
---|
249 | if (!GetTimeFromFile())
|
---|
250 | return 2;
|
---|
251 |
|
---|
252 | if (fTimeStart)
|
---|
253 | *fLog << inf << "Start Time: " << fTimeStart << endl;
|
---|
254 | if (fTimeStop)
|
---|
255 | *fLog << inf << "Stop Time: " << fTimeStop << endl;
|
---|
256 |
|
---|
257 | //
|
---|
258 | // create a (empty) list of parameters which can be used by the tasks
|
---|
259 | // and an (empty) list of tasks which should be executed
|
---|
260 | //
|
---|
261 | MParList plist;
|
---|
262 |
|
---|
263 | MTaskList tasks;
|
---|
264 | tasks.SetOwner();
|
---|
265 | plist.AddToList(&tasks);
|
---|
266 |
|
---|
267 | //
|
---|
268 | // create the tasks which should be executed and add them to the list
|
---|
269 | // in the case you don't need parameter containers, all of them can
|
---|
270 | // be created by MRawFileRead::PreProcess
|
---|
271 | //
|
---|
272 | const TString option(fUpdate?"UPDATE":(fOverwrite?"RECREATE":"NEW"));
|
---|
273 |
|
---|
274 | MTask *read = 0;
|
---|
275 | MFilter *filter = 0;
|
---|
276 | MWriteRootFile *write = new MWriteRootFile(fPathOut, option, "Magic root-file", fCompression);
|
---|
277 |
|
---|
278 | if (isreport || isdc)
|
---|
279 | {
|
---|
280 | MReportFileReadCC *r = new MReportFileReadCC(fPathIn);
|
---|
281 | r->SetTimeStart(fTimeStart);
|
---|
282 | r->SetTimeStop(fTimeStop);
|
---|
283 |
|
---|
284 | if (isdc)
|
---|
285 | filter = SetupCurrents(*r, *write);
|
---|
286 | else
|
---|
287 | filter = SetupReports(*r, *write);
|
---|
288 |
|
---|
289 | read = r;
|
---|
290 | }
|
---|
291 | else
|
---|
292 | {
|
---|
293 | MRawFileRead *r = new MRawFileRead(fPathIn);
|
---|
294 | SetupRaw(*r, *write);
|
---|
295 | read = r;
|
---|
296 | }
|
---|
297 |
|
---|
298 | tasks.AddToList(read);
|
---|
299 | if (filter)
|
---|
300 | tasks.AddToList(filter);
|
---|
301 |
|
---|
302 | if (!HasNullOut())
|
---|
303 | tasks.AddToList(write);
|
---|
304 |
|
---|
305 | // Create and setup the eventloop
|
---|
306 | MEvtLoop evtloop(fName);
|
---|
307 | evtloop.SetParList(&plist);
|
---|
308 | evtloop.SetLogStream(fLog);
|
---|
309 | //evtloop.SetDisplay(fDisplay);
|
---|
310 | //if (!SetupEnv(evtloop))
|
---|
311 | // return kFALSE;
|
---|
312 |
|
---|
313 | // Execute first analysis
|
---|
314 | if (!evtloop.Eventloop(fMaxEvents))
|
---|
315 | {
|
---|
316 | *fLog << err << GetDescriptor() << ": Failed." << endl;
|
---|
317 | return 3;
|
---|
318 | }
|
---|
319 |
|
---|
320 | *fLog << all << GetDescriptor() << ": Done." << endl << endl << endl;;
|
---|
321 |
|
---|
322 | return 0;
|
---|
323 | }
|
---|
324 |
|
---|
325 | Int_t MJMerpp::ProcessSeq(TString fname)
|
---|
326 | {
|
---|
327 | if (!MSequence::InflateSeq(fname))
|
---|
328 | return 4;
|
---|
329 |
|
---|
330 | MSequence seq(fname);
|
---|
331 | if (!seq.IsValid())
|
---|
332 | return 5;
|
---|
333 |
|
---|
334 | const UInt_t num = seq.GetNumEntries(MSequence::kDat);
|
---|
335 |
|
---|
336 | const TString reppath = fPathIn;
|
---|
337 | const TString calpath = fPathOut;
|
---|
338 |
|
---|
339 | TString rc;
|
---|
340 | for (UInt_t i=0; i<num; i++)
|
---|
341 | {
|
---|
342 | UInt_t run, file;
|
---|
343 |
|
---|
344 | // Excluded
|
---|
345 | if (seq.GetFile(i, MSequence::kDat, run, file)==0)
|
---|
346 | continue;
|
---|
347 |
|
---|
348 | // fHeaderRun==0 means: All day summary file
|
---|
349 | const TString name1 = fHeaderRun==0 ? reppath : seq.GetFileName(i, MSequence::kReportDat, reppath);
|
---|
350 | const TString name2 = seq.GetFileName(i, MSequence::kCalibrated, calpath);
|
---|
351 |
|
---|
352 | // Full qualified name could not be determined
|
---|
353 | // or file is not accessible
|
---|
354 | if (name1.IsNull() || name2.IsNull())
|
---|
355 | return 6;
|
---|
356 |
|
---|
357 | // FIXME: check runcallisto
|
---|
358 | if (fHeaderRun==0) // extract valid range from summary file
|
---|
359 | SetConstrainRunRep(run, file); // (this is not guranteed to work :-( )
|
---|
360 | else // check for the correct header
|
---|
361 | SetConstrainHeader(seq.GetTelescope(), run, file);
|
---|
362 |
|
---|
363 | SetTime(MTime(), MTime()); // Raise error if set?
|
---|
364 | SetPathIn(name1);
|
---|
365 | SetPathOut(name2);
|
---|
366 |
|
---|
367 | const Int_t rc = Process();
|
---|
368 | if (rc==0)
|
---|
369 | continue;
|
---|
370 |
|
---|
371 | return rc;
|
---|
372 | }
|
---|
373 | return 0;
|
---|
374 | }
|
---|