source: tags/Mars-V0.8.6/merpp.cc@ 10108

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