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

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