source: trunk/MagicSoft/Mars/merpp.cc@ 7008

Last change on this file since 7008 was 7001, checked in by tbretz, 19 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 //
148 // Evaluate arguments
149 //
150 MArgs arg(argc, argv);
151 gLog.Setup(arg);
152
153 StartUpMessage();
154
155 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
156 return 0;
157
158 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
159 {
160 Usage();
161 return 2;
162 }
163
164 const Int_t kComprlvl = arg.GetIntAndRemove("-c", 2);
165 const Bool_t kInterleave = arg.GetIntAndRemove("--interleave=", 1);
166 const Bool_t kForce = arg.HasOnlyAndRemove("-f");
167 const Bool_t kForceProc = arg.HasOnlyAndRemove("-ff");
168 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
169 const Bool_t kAutoTime = arg.HasOnlyAndRemove("--auto-time");
170 Int_t kRunFile = arg.GetIntAndRemove("--runfile=", -1);
171 Bool_t kUpdate = arg.HasOnlyAndRemove("--update") || arg.HasOnlyAndRemove("-u");
172
173 MTime kTimeStart;
174 MTime kTimeStop;
175 if (arg.HasOption("--star="))
176 kTimeStart = AnalyseTime(arg.GetStringAndRemove("--start="));
177 if (arg.HasOption("--stop="))
178 kTimeStop = AnalyseTime(arg.GetStringAndRemove("--stop="));
179
180// const TString kSqlDataBase(arg.GetStringAndRemove("--sql="));
181
182 if (arg.HasOnlyAndRemove("--sumfile"))
183 kRunFile = 0;
184
185 if (arg.GetNumOptions()>0)
186 {
187 gLog << warn << "WARNING - Unknown commandline options..." << endl;
188 arg.Print("options");
189 gLog << endl;
190 }
191
192 //
193 // check for the right usage of the program
194 //
195 if (arg.GetNumArguments()<1 || arg.GetNumArguments()>2)
196 {
197 Usage();
198 return 2;
199 }
200
201 //
202 // This is to make argv[i] more readable insidethe code
203 //
204 TString kNamein = arg.GetArgumentStr(0);
205 TString kNameout = arg.GetArgumentStr(1);
206
207 const Bool_t isreport = kNamein.EndsWith(".rep");
208 const Bool_t isdc = kNamein.EndsWith(".txt");
209 const Bool_t israw = !isreport && !isdc;
210
211 if (!kNamein.EndsWith(".raw") && israw)
212 kNamein += ".raw";
213
214 if (kNameout.IsNull())
215 kNameout = kNamein(0, kNamein.Last('.'));
216
217 if (!kNameout.EndsWith(".root"))
218 kNameout += ".root";
219
220// if (!kSqlDataBase.IsNull() && !israw)
221// gLog << warn << "WARNING - Option '--sql=' only valid for raw-files... ignored." << endl;
222
223 //
224 // Initialize Non-GUI (batch) mode
225 //
226 gROOT->SetBatch();
227
228 //
229 // check whether the given files are OK.
230 //
231 if (gSystem->AccessPathName(kNamein, kFileExists))
232 {
233 gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
234 return 2;
235 }
236
237 const Bool_t fileexist = !gSystem->AccessPathName(kNameout, kFileExists);
238 const Bool_t writeperm = !gSystem->AccessPathName(kNameout, kWritePermission);
239
240 if (fileexist && !writeperm)
241 {
242 gLog << err << "Sorry, you don't have write permission for '" << kNameout << "'." << endl;
243 return 2;
244 }
245
246 if (fileexist && !kUpdate && !kForce)
247 {
248 gLog << err << "Sorry, file '" << kNameout << "' already existing." << endl;
249 return 2;
250 }
251
252 if (!fileexist && kUpdate)
253 {
254 gLog << warn << "File '" << kNameout << "' doesn't yet exist." << endl;
255 kUpdate=kFALSE;
256 }
257
258 //
259 // Evaluate possible start-/stop-time
260 //
261 if (kAutoTime && kUpdate && (isreport || isdc))
262 GetTimeFromFile(kNameout, kTimeStart, kTimeStop);
263
264 if (kTimeStart)
265 gLog << inf << "Start Time: " << kTimeStart << endl;
266 if (kTimeStop)
267 gLog << inf << "Stop Time: " << kTimeStop << endl;
268
269 //
270 // Ignore TObject Streamer (bits, uniqueid) for MArray and MParContainer
271 //
272 MArray::Class()->IgnoreTObjectStreamer();
273 MParContainer::Class()->IgnoreTObjectStreamer();
274
275 //
276 // create a (empty) list of parameters which can be used by the tasks
277 // and an (empty) list of tasks which should be executed
278 //
279 MParList plist;
280
281 MTaskList tasks;
282 tasks.SetOwner();
283 plist.AddToList(&tasks);
284
285 //
286 // ---- The following is only necessary to supress some output ----
287 /*
288 MRawRunHeader runheader;
289 plist.AddToList(&runheader);
290
291 MRawEvtHeader evtheader;
292 plist.AddToList(&evtheader);
293
294 MRawEvtData evtdata;
295 plist.AddToList(&evtdata);
296
297 MRawCrateArray cratearray;
298 plist.AddToList(&cratearray);
299
300 MTime evttime;
301 plist.AddToList(&evttime);
302 */
303
304 //
305 // create the tasks which should be executed and add them to the list
306 // in the case you don't need parameter containers, all of them can
307 // be created by MRawFileRead::PreProcess
308 //
309 MTask *read = 0;
310 MFilter *filter = 0;
311 MTask *write = 0;
312
313 const TString option(kUpdate ? "UPDATE" : "RECREATE");
314 if (isreport || isdc)
315 {
316 MWriteRootFile *w = new MWriteRootFile(kNameout, option, "Magic root-file", kComprlvl);
317 if (isdc)
318 {
319 w->AddContainer("MTimeCurrents", "Currents");
320 w->AddContainer("MCameraDC", "Currents");
321 w->AddContainer("MReportCurrents", "Currents");
322 }
323 else
324 {
325 w->AddContainer("MReportCamera", "Camera");
326 w->AddContainer("MTimeCamera", "Camera");
327 w->AddContainer("MCameraAUX", "Camera");
328 w->AddContainer("MCameraCalibration", "Camera");
329 w->AddContainer("MCameraCooling", "Camera");
330 w->AddContainer("MCameraHV", "Camera");
331 w->AddContainer("MCameraLV", "Camera");
332 w->AddContainer("MCameraLids", "Camera");
333 w->AddContainer("MReportTrigger", "Trigger");
334 w->AddContainer("MTimeTrigger", "Trigger");
335 w->AddContainer("MTriggerBit", "Trigger");
336 w->AddContainer("MTriggerIPR", "Trigger");
337 w->AddContainer("MTriggerCell", "Trigger");
338 w->AddContainer("MTriggerPrescFact", "Trigger");
339 w->AddContainer("MTriggerLiveTime", "Trigger");
340 w->AddContainer("MReportDrive", "Drive");
341 w->AddContainer("MTimeDrive", "Drive");
342 w->AddContainer("MReportCC", "CC");
343 w->AddContainer("MTimeCC", "CC");
344 w->AddContainer("MReportStarguider", "Starguider");
345 w->AddContainer("MTimeStarguider", "Starguider");
346 // w->AddContainer("MReportDAQ", "DAQ");
347 // w->AddContainer("MTimeDAQ", "DAQ");
348 }
349 write = w;
350
351 MReportFileReadCC *r = new MReportFileReadCC(kNamein);
352 r->SetTimeStart(kTimeStart);
353 r->SetTimeStop(kTimeStop);
354 if (isdc)
355 {
356 r->SetHasNoHeader();
357 r->AddToList("MReportCurrents");
358 }
359 else
360 {
361 r->SetRunNumber(kRunFile);
362 r->AddToList("MReportCC");
363 //r->AddToList("MReportDAQ");
364 r->AddToList("MReportDrive");
365 r->AddToList("MReportCamera");
366 r->AddToList("MReportTrigger");
367 r->AddToList("MReportStarguider");
368 if (kRunNumber>0)
369 {
370 r->AddToList("MReportRun");
371 filter = new MFDataMember("MReportRun.fRunNumber", '=', kRunNumber);
372 w->SetFilter(filter);
373 }
374 }
375 read = r;
376 }
377 else
378 {
379 read = new MRawFileRead(kNamein);
380 static_cast<MRawFileRead*>(read)->SetInterleave(kInterleave);
381 static_cast<MRawFileRead*>(read)->SetForceMode(kForceProc);
382 write = new MRawFileWrite(kNameout, option, "Magic root-file", kComprlvl);
383 }
384
385 tasks.AddToList(read);
386 if (filter)
387 tasks.AddToList(filter);
388 /*
389 if (israw && !kSqlDataBase.IsNull())
390 {
391 MSqlInsertRun *ins = new MSqlInsertRun(kSqlDataBase);
392 ins->SetUpdate();
393 tasks.AddToList(ins);
394 }*/
395 tasks.AddToList(write);
396
397 //
398 // create the looping object and tell it about the parameters to use
399 // and the tasks to execute
400 //
401 MEvtLoop magic;
402 magic.SetParList(&plist);
403
404 //
405 // Start the eventloop which reads the raw file (MRawFileRead) and
406 // write all the information into a root file (MRawFileWrite)
407 //
408 // between reading and writing we can do, transformations, checks, etc.
409 // (I'm think of a task like MRawDataCheck)
410 //
411 if (!magic.Eventloop())
412 {
413 gLog << err << "ERROR: Merging and preprocessing failed!" << endl;
414 return 2;
415 }
416
417 tasks.PrintStatistics();
418
419 gLog << all << "Merpp finished successfull!" << endl;
420 return 0;
421}
Note: See TracBrowser for help on using the repository browser.