source: tags/Mars-V0.10.2/merpp.cc@ 17873

Last change on this file since 17873 was 8011, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.2 KB
Line 
1#include <TSystem.h>
2
3#include <TFile.h>
4#include <TTree.h>
5
6#include "MParList.h"
7#include "MTaskList.h"
8#include "MEvtLoop.h"
9
10#include "MRawFileRead.h"
11#include "MSqlInsertRun.h"
12#include "MRawFileWrite.h"
13#include "MReportFileReadCC.h"
14#include "MWriteRootFile.h"
15
16#include "MLog.h"
17#include "MLogManip.h"
18
19#include "MArgs.h"
20#include "MTime.h"
21#include "MArray.h"
22#include "MRawEvtData.h"
23#include "MRawRunHeader.h"
24#include "MRawEvtHeader.h"
25#include "MRawCrateArray.h"
26
27#include "MFDataMember.h"
28
29using namespace std;
30
31//////////////////////////////////////////////////////////////////////////////
32// //
33// This is an easy implementation of the Merging process //
34// (as compilable prog) //
35// //
36// at the moment it reads a binary file ("rawtest.bin") which was written //
37// in the DAQ raw format. //
38// //
39// The data are stored in root container objects (classes derived from //
40// TObject like MRawRunHeader) //
41// //
42// This containers are written to a root file ("rawtest.root") //
43// //
44//////////////////////////////////////////////////////////////////////////////
45
46static void StartUpMessage()
47{
48 gLog << all << endl;
49
50 // 1 2 3 4 5
51 // 12345678901234567890123456789012345678901234567890
52 gLog << "==================================================" << endl;
53 gLog << " MERPP - MARS V" << MARSVER << endl;
54 gLog << " MARS - Merging and Preprocessing Program" << endl;
55 gLog << " Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
56 gLog << "==================================================" << endl;
57 gLog << endl;
58}
59
60static void Usage()
61{
62 // 1 2 3 4 5 6 7 8
63 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
64 gLog << all << endl;
65 gLog << "Sorry the usage is:" << endl;
66 gLog << " merpp [options] inputfile[.rep,[.raw.gz],[.txt]] [outputfile[.root]]" << endl << endl;
67 gLog << " Arguments:" << endl;
68 gLog << " inputfile.raw[.gz] Magic DAQ binary file." << endl;
69 gLog << " inputfile.rep Magic Central Control report file." << endl;
70 gLog << " inputfile.txt Magic DC currents file." << endl;
71 gLog << " ouputfile.root Merpped root file." << endl << endl;
72 gLog << " Options:" << endl;
73 gLog.Usage();
74 gLog << " --version, -V Show startup message with version number" << endl;
75 gLog << " -?, -h, --help This help" << endl << endl;
76 gLog << " File Options:" << endl;
77 gLog << " -c# Compression level #=1..9 [default=2]" << endl;
78 gLog << " -f Force overwrite of an existing file" << endl;
79 gLog << " -u, --update Update an existing file." << endl << endl;
80 gLog << " Raw Data Options:" << endl;
81 gLog << " -ff Force merpp to ignore broken events" << endl;
82 gLog << " --interleave=# Process only each i-th event [default=1]" << endl << endl;
83// gLog << " --sql=mysql://user:password@url Insert run into database" << endl << endl;
84 gLog << " Report File Options:" << endl;
85 gLog << " --auto-time-start Take time automatically from MRawRunHeader" << endl;
86 gLog << " (overwrites --start=)" << endl;
87 gLog << " --auto-time-stop Take time automatically from MRawRunHeader" << endl;
88 gLog << " (overwrites --stop=)" << endl;
89 gLog << " --auto-time Abbreviation for --auto-time-start and auto-time-stop" << endl;
90 gLog << " --start=date/time Start event time" << endl;
91 gLog << " --stop=date/time Stop event time" << endl;
92 gLog << " --run=# Only data corresponding to this run number" << endl;
93 gLog << " (from RUN-REPORT)" << endl;
94 gLog << " --runfile=# Allow only run-control files" << endl;
95 gLog << " (from .rep header)" << endl;
96 gLog << " --sumfile Check for an all night summary file" << endl;
97 gLog << " (from .rep header)" << endl;
98 gLog << " --allfiles Don't check file type <default>" << endl << endl;
99 gLog << " REMARK: - At the moment you can process a .raw _or_ a .rep file, only!" << endl;
100 gLog << " - 'date/time' has the format 'yyyy-mm-dd/hh:mm:ss.mmm'" << endl << endl;
101}
102
103// FIXME: Move to MTime (maybe 'InterpreteCmdline')
104MTime AnalyseTime(TString str)
105{
106 Int_t y=0, ms=0, mon=0, d=0, h=0, m=0, s=0;
107
108 const Int_t n = sscanf(str.Data(), "%d-%d-%d/%d:%d:%d.%d", &y, &mon, &d, &h, &m, &s, &ms);
109
110 if (n<6 || n>7)
111 {
112 gLog << warn << "'" << str << "' no valid Time... ignored." << endl;
113 return MTime();
114 }
115
116 MTime t;
117 t.Set(y, mon, d, h, m, s, ms);
118 return t;
119}
120
121void GetTimeFromFile(const char *fname, MTime *start, MTime *stop)
122{
123 TFile f(fname, "READ");
124
125 TTree *t = (TTree*)f.Get("RunHeaders");
126 if (t->GetEntries()!=1)
127 {
128 gLog << warn << "WARNING - File " << fname << " contains no or more than one entry in RunHeaders... Times unchanged." << endl;
129 return;
130 }
131
132 MRawRunHeader *h = 0;
133 t->SetBranchAddress("MRawRunHeader.", &h);
134 t->GetEntry(0);
135 if (!h)
136 {
137 gLog << warn << "WARNING - File " << fname << " did not contain RunHeaders.MRawRunHeader... Times unchanged." << endl;
138 return;
139 }
140
141 if (start && !*start)
142 *start = h->GetRunStart();
143 if (stop && !*stop)
144 *stop = h->GetRunEnd();
145}
146
147int main(const int argc, char **argv)
148{
149 if (!MARS::CheckRootVer())
150 return 0xff;
151
152 //
153 // Evaluate arguments
154 //
155 MArgs arg(argc, argv);
156 gLog.Setup(arg);
157
158 StartUpMessage();
159
160 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
161 return 0;
162
163 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
164 {
165 Usage();
166 return 2;
167 }
168
169 const Int_t kComprlvl = arg.GetIntAndRemove("-c", 2);
170 const Bool_t kInterleave = arg.GetIntAndRemove("--interleave=", 1);
171 const Bool_t kForce = arg.HasOnlyAndRemove("-f");
172 const Bool_t kForceProc = arg.HasOnlyAndRemove("-ff");
173 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
174 const Bool_t kAutoTime = arg.HasOnlyAndRemove("--auto-time");
175 const Bool_t kAutoTimeStart = arg.HasOnlyAndRemove("--auto-time-start") || kAutoTime;
176 const Bool_t kAutoTimeStop = arg.HasOnlyAndRemove("--auto-time-stop") || kAutoTime;
177 Int_t kRunFile = arg.GetIntAndRemove("--runfile=", -1);
178 Bool_t kUpdate = arg.HasOnlyAndRemove("--update") || arg.HasOnlyAndRemove("-u");
179
180 MTime kTimeStart;
181 MTime kTimeStop;
182 if (arg.HasOption("--star="))
183 kTimeStart = AnalyseTime(arg.GetStringAndRemove("--start="));
184 if (arg.HasOption("--stop="))
185 kTimeStop = AnalyseTime(arg.GetStringAndRemove("--stop="));
186
187// const TString kSqlDataBase(arg.GetStringAndRemove("--sql="));
188
189 if (arg.HasOnlyAndRemove("--sumfile"))
190 kRunFile = 0;
191
192 if (arg.GetNumOptions()>0)
193 {
194 gLog << warn << "WARNING - Unknown commandline options..." << endl;
195 arg.Print("options");
196 gLog << endl;
197 }
198
199 //
200 // check for the right usage of the program
201 //
202 if (arg.GetNumArguments()<1 || arg.GetNumArguments()>2)
203 {
204 Usage();
205 return 2;
206 }
207
208 //
209 // This is to make argv[i] more readable insidethe code
210 //
211 TString kNamein = arg.GetArgumentStr(0);
212 TString kNameout = arg.GetArgumentStr(1);
213
214 const Bool_t isreport = kNamein.EndsWith(".rep");
215 const Bool_t isdc = kNamein.EndsWith(".txt");
216 const Bool_t israw = !isreport && !isdc;
217
218 if (!kNamein.EndsWith(".raw") && !kNamein.EndsWith(".raw.gz") && israw)
219 kNamein += ".raw.gz";
220
221 if (kNameout.IsNull())
222 kNameout = kNamein(0, kNamein.Last('.'));
223
224 if (!kNameout.EndsWith(".root"))
225 kNameout += ".root";
226
227// if (!kSqlDataBase.IsNull() && !israw)
228// gLog << warn << "WARNING - Option '--sql=' only valid for raw-files... ignored." << endl;
229
230 //
231 // Initialize Non-GUI (batch) mode
232 //
233 gROOT->SetBatch();
234
235 //
236 // check whether the given files are OK.
237 //
238 if (gSystem->AccessPathName(kNamein, kFileExists))
239 {
240 gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
241 return 2;
242 }
243
244 const Bool_t fileexist = !gSystem->AccessPathName(kNameout, kFileExists);
245 const Bool_t writeperm = !gSystem->AccessPathName(kNameout, kWritePermission);
246
247 if (fileexist && !writeperm)
248 {
249 gLog << err << "Sorry, you don't have write permission for '" << kNameout << "'." << endl;
250 return 2;
251 }
252
253 if (fileexist && !kUpdate && !kForce)
254 {
255 gLog << err << "Sorry, file '" << kNameout << "' already existing." << endl;
256 return 2;
257 }
258
259 if (!fileexist && kUpdate)
260 {
261 gLog << warn << "File '" << kNameout << "' doesn't yet exist." << endl;
262 kUpdate=kFALSE;
263 }
264
265 //
266 // Evaluate possible start-/stop-time
267 //
268 if ((kAutoTimeStart || kAutoTimeStop) && kUpdate && (isreport || isdc))
269 GetTimeFromFile(kNameout, kAutoTimeStart?&kTimeStart:0, kAutoTimeStop?&kTimeStop:0);
270
271 if (kTimeStart)
272 gLog << inf << "Start Time: " << kTimeStart << endl;
273 if (kTimeStop)
274 gLog << inf << "Stop Time: " << kTimeStop << endl;
275
276 //
277 // Ignore TObject Streamer (bits, uniqueid) for MArray and MParContainer
278 //
279 MArray::Class()->IgnoreTObjectStreamer();
280 MParContainer::Class()->IgnoreTObjectStreamer();
281
282 //
283 // create a (empty) list of parameters which can be used by the tasks
284 // and an (empty) list of tasks which should be executed
285 //
286 MParList plist;
287
288 MTaskList tasks;
289 tasks.SetOwner();
290 plist.AddToList(&tasks);
291
292 //
293 // ---- The following is only necessary to supress some output ----
294 /*
295 MRawRunHeader runheader;
296 plist.AddToList(&runheader);
297
298 MRawEvtHeader evtheader;
299 plist.AddToList(&evtheader);
300
301 MRawEvtData evtdata;
302 plist.AddToList(&evtdata);
303
304 MRawCrateArray cratearray;
305 plist.AddToList(&cratearray);
306
307 MTime evttime;
308 plist.AddToList(&evttime);
309 */
310
311 //
312 // create the tasks which should be executed and add them to the list
313 // in the case you don't need parameter containers, all of them can
314 // be created by MRawFileRead::PreProcess
315 //
316 MTask *read = 0;
317 MFilter *filter = 0;
318 MTask *write = 0;
319
320 const TString option(kUpdate ? "UPDATE" : "RECREATE");
321 if (isreport || isdc)
322 {
323 MWriteRootFile *w = new MWriteRootFile(kNameout, option, "Magic root-file", kComprlvl);
324 if (isdc)
325 {
326 w->AddContainer("MTimeCurrents", "Currents");
327 w->AddContainer("MCameraDC", "Currents");
328 w->AddContainer("MReportCurrents", "Currents");
329 }
330 else
331 {
332 w->AddContainer("MReportCamera", "Camera");
333 w->AddContainer("MTimeCamera", "Camera");
334 w->AddContainer("MCameraAUX", "Camera");
335 w->AddContainer("MCameraCalibration", "Camera");
336 w->AddContainer("MCameraCooling", "Camera");
337 w->AddContainer("MCameraActiveLoad", "Camera");
338 w->AddContainer("MCameraHV", "Camera");
339 w->AddContainer("MCameraLV", "Camera");
340 w->AddContainer("MCameraLids", "Camera");
341 w->AddContainer("MReportTrigger", "Trigger");
342 w->AddContainer("MTimeTrigger", "Trigger");
343 w->AddContainer("MTriggerBit", "Trigger");
344 w->AddContainer("MTriggerIPR", "Trigger");
345 w->AddContainer("MTriggerCell", "Trigger");
346 w->AddContainer("MTriggerPrescFact", "Trigger");
347 w->AddContainer("MTriggerLiveTime", "Trigger");
348 w->AddContainer("MReportDrive", "Drive");
349 w->AddContainer("MTimeDrive", "Drive");
350 w->AddContainer("MReportCC", "CC");
351 w->AddContainer("MCameraTH", "CC");
352 w->AddContainer("MCameraTD", "CC");
353 w->AddContainer("MCameraRecTemp", "CC");
354 w->AddContainer("MTimeCC", "CC");
355 w->AddContainer("MReportStarguider", "Starguider");
356 w->AddContainer("MTimeStarguider", "Starguider");
357 // w->AddContainer("MReportDAQ", "DAQ");
358 // w->AddContainer("MTimeDAQ", "DAQ");
359 }
360 write = w;
361
362 MReportFileReadCC *r = new MReportFileReadCC(kNamein);
363 r->SetTimeStart(kTimeStart);
364 r->SetTimeStop(kTimeStop);
365 if (isdc)
366 {
367 r->SetHasNoHeader();
368 r->AddToList("MReportCurrents");
369 }
370 else
371 {
372 r->SetRunNumber(kRunFile);
373 r->AddToList("MReportCC");
374 //r->AddToList("MReportDAQ");
375 r->AddToList("MReportDrive");
376 r->AddToList("MReportCamera");
377 r->AddToList("MReportTrigger");
378 r->AddToList("MReportStarguider");
379 if (kRunNumber>0)
380 {
381 r->AddToList("MReportRun");
382 filter = new MFDataMember("MReportRun.fRunNumber", '=', kRunNumber);
383 w->SetFilter(filter);
384 }
385 }
386 read = r;
387 }
388 else
389 {
390 read = new MRawFileRead(kNamein);
391 static_cast<MRawFileRead*>(read)->SetInterleave(kInterleave);
392 static_cast<MRawFileRead*>(read)->SetForceMode(kForceProc);
393 write = new MRawFileWrite(kNameout, option, "Magic root-file", kComprlvl);
394 }
395
396 tasks.AddToList(read);
397 if (filter)
398 tasks.AddToList(filter);
399 /*
400 if (israw && !kSqlDataBase.IsNull())
401 {
402 MSqlInsertRun *ins = new MSqlInsertRun(kSqlDataBase);
403 ins->SetUpdate();
404 tasks.AddToList(ins);
405 }*/
406 tasks.AddToList(write);
407
408 //
409 // create the looping object and tell it about the parameters to use
410 // and the tasks to execute
411 //
412 MEvtLoop magic;
413 magic.SetParList(&plist);
414
415 //
416 // Start the eventloop which reads the raw file (MRawFileRead) and
417 // write all the information into a root file (MRawFileWrite)
418 //
419 // between reading and writing we can do, transformations, checks, etc.
420 // (I'm think of a task like MRawDataCheck)
421 //
422 if (!magic.Eventloop())
423 {
424 gLog << err << "ERROR: Merging and preprocessing failed!" << endl;
425 return 2;
426 }
427
428 tasks.PrintStatistics();
429
430 gLog << all << "Merpp finished successfull!" << endl;
431 return 0;
432}
Note: See TracBrowser for help on using the repository browser.