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

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