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

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