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

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