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

Last change on this file since 4723 was 4723, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 12.5 KB
Line 
1#include <TSystem.h>
2
3#include "MParList.h"
4#include "MTaskList.h"
5#include "MEvtLoop.h"
6
7#include "MRawFileRead.h"
8#include "MSqlInsertRun.h"
9#include "MRawFileWrite.h"
10#include "MReportFileReadCC.h"
11#include "MWriteRootFile.h"
12
13#include "MLog.h"
14#include "MLogManip.h"
15
16#include "MArgs.h"
17#include "MTime.h"
18#include "MArray.h"
19#include "MRawEvtData.h"
20#include "MRawRunHeader.h"
21#include "MRawEvtHeader.h"
22#include "MRawCrateArray.h"
23
24#include "MFDataMember.h"
25
26using namespace std;
27
28//////////////////////////////////////////////////////////////////////////////
29// //
30// This is an easy implementation of the Merging process //
31// (as compilable prog) //
32// //
33// at the moment it reads a binary file ("rawtest.bin") which was written //
34// in the DAQ raw format. //
35// //
36// The data are stored in root container objects (classes derived from //
37// TObject like MRawRunHeader) //
38// //
39// This containers are written to a root file ("rawtest.root") //
40// //
41//////////////////////////////////////////////////////////////////////////////
42
43static void StartUpMessage()
44{
45 gLog << all << endl;
46
47 // 1 2 3 4 5
48 // 12345678901234567890123456789012345678901234567890
49 gLog << "==================================================" << endl;
50 gLog << " MERPP - MARS V" << MARSVER << endl;
51 gLog << " MARS - Merging and Preprocessing Program" << endl;
52 gLog << " Compiled on <" << __DATE__ << ">" << endl;
53 gLog << " Using ROOT v" << ROOTVER << endl;
54 gLog << "==================================================" << endl;
55 gLog << endl;
56}
57
58static void Usage()
59{
60 // 1 2 3 4 5 6 7 8
61 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
62 gLog << all << endl;
63 gLog << "Sorry the usage is:" << endl;
64 gLog << " merpp [options] inputfile[.rep,[.raw],[.txt]] [outputfile[.root]]" << endl << endl;
65 gLog << " Arguments:" << endl;
66 gLog << " inputfile.raw Magic DAQ binary file." << endl;
67 gLog << " inputfile.rep Magic Central Control report file." << endl;
68 gLog << " inputfile.txt Magic DC currents file." << endl;
69 gLog << " ouputfile.root Merpped root file." << endl << endl;
70 gLog << " Options:" << endl;
71 gLog.Usage();
72 gLog << " -?, -h, --help This help" << endl << endl;
73 gLog << " File Options:" << endl;
74 gLog << " -c# Compression level #=1..9 [default=2]" << endl;
75 gLog << " -f Force overwrite of an existing file" << endl << endl;
76 gLog << " Raw Data Options:" << endl;
77 gLog << " -ff Force merpp to ignore broken events" << endl;
78 gLog << " --interleave=# Process only each i-th event [default=1]" << endl << endl;
79// gLog << " --sql=mysql://user:password@url Insert run into database" << endl << endl;
80 gLog << " Report File Options:" << endl;
81 gLog << " --start=date/time Start event time" << endl;
82 gLog << " --stop=date/time Stop event time" << endl;
83 gLog << " --run=# Only data corresponding to this run number" << endl;
84 gLog << " (from RUN-REPORT)" << endl;
85 gLog << " --runfile=# Allow only run-control files" << endl;
86 gLog << " (from .rep header)" << endl;
87 gLog << " --sumfile Check for an all night summary file" << endl;
88 gLog << " (from .rep header)" << endl;
89 gLog << " --allfiles Don't check file type <default>" << endl << endl;
90 gLog << " REMARK: - At the moment you can process a .raw _or_ a .rep file, only!" << endl;
91 gLog << " - 'date/time' has the format 'yyyy-mm-dd/hh:mm:ss.mmm'" << endl << endl;
92}
93
94// FIXME: Move to MTime (maybe 'InterpreteCmdline')
95MTime AnalyseTime(TString str)
96{
97 Int_t y=0, ms=0, mon=0, d=0, h=0, m=0, s=0;
98
99 const Int_t n = sscanf(str.Data(), "%d-%d-%d/%d:%d:%d.%d", &y, &mon, &d, &h, &m, &s, &ms);
100
101 if (n<6 || n>7)
102 {
103 gLog << warn << "'" << str << "' no valid Time... ignored." << endl;
104 return MTime();
105 }
106
107 MTime t;
108 t.Set(y, mon, d, h, m, s, ms);
109 return t;
110}
111
112int main(const int argc, char **argv)
113{
114 StartUpMessage();
115
116 //
117 // Evaluate arguments
118 //
119 MArgs arg(argc, argv);
120
121 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
122 {
123 Usage();
124 return -1;
125 }
126
127 gLog.Setup(arg);
128
129 const Int_t kComprlvl = arg.GetIntAndRemove("-c", 2);
130 const Bool_t kInterleave = arg.GetIntAndRemove("--interleave=", 1);
131 const Bool_t kForce = arg.HasOnlyAndRemove("-f");
132 const Bool_t kForceProc = arg.HasOnlyAndRemove("-ff");
133 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
134 Int_t kRunFile = arg.GetIntAndRemove("--runfile=", -1);
135 Bool_t kUpdate = arg.HasOnlyAndRemove("--update") || arg.HasOnlyAndRemove("-u");
136
137 MTime kTimeStart;
138 MTime kTimeStop;
139 if (arg.HasOption("--star="))
140 kTimeStart = AnalyseTime(arg.GetStringAndRemove("--start="));
141 if (arg.HasOption("--stop="))
142 kTimeStop = AnalyseTime(arg.GetStringAndRemove("--stop="));
143
144// const TString kSqlDataBase(arg.GetStringAndRemove("--sql="));
145
146 if (arg.HasOnlyAndRemove("--sumfile"))
147 kRunFile = 0;
148
149 if (kTimeStart)
150 gLog << inf << "Start Time: " << kTimeStart << endl;
151 if (kTimeStop)
152 gLog << inf << "Stop Time: " << kTimeStop << endl;
153
154 if (arg.GetNumOptions()>0)
155 {
156 gLog << warn << "WARNING - Unknown commandline options..." << endl;
157 arg.Print("options");
158 gLog << endl;
159 }
160
161 //
162 // check for the right usage of the program
163 //
164 if (arg.GetNumArguments()<1 || arg.GetNumArguments()>2)
165 {
166 Usage();
167 return -1;
168 }
169
170 //
171 // This is to make argv[i] more readable insidethe code
172 //
173 TString kNamein = arg.GetArgumentStr(0);
174 TString kNameout = arg.GetArgumentStr(1);
175
176 const Bool_t isreport = kNamein.EndsWith(".rep");
177 const Bool_t isdc = kNamein.EndsWith(".txt");
178 const Bool_t israw = !isreport && !isdc;
179
180 if (!kNamein.EndsWith(".raw") && israw)
181 kNamein += ".raw";
182
183 if (kNameout.IsNull())
184 kNameout = kNamein(0, kNamein.Last('.'));
185
186 if (!kNameout.EndsWith(".root"))
187 kNameout += ".root";
188
189// if (!kSqlDataBase.IsNull() && !israw)
190// gLog << warn << "WARNING - Option '--sql=' only valid for raw-files... ignored." << endl;
191
192 //
193 // Initialize Non-GUI (batch) mode
194 //
195 gROOT->SetBatch();
196
197 //
198 // check whether the given files are OK.
199 //
200 if (gSystem->AccessPathName(kNamein, kFileExists))
201 {
202 gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
203 return -1;
204 }
205
206 const Bool_t fileexist = !gSystem->AccessPathName(kNameout, kFileExists);
207 const Bool_t writeperm = !gSystem->AccessPathName(kNameout, kWritePermission);
208
209 if (fileexist && !writeperm)
210 {
211 gLog << err << "Sorry, you don't have write permission for '" << kNameout << "'." << endl;
212 return -1;
213 }
214
215 if (fileexist && !kUpdate && !kForce)
216 {
217 gLog << err << "Sorry, file '" << kNameout << "' already existing." << endl;
218 return -1;
219 }
220
221 if (!fileexist && kUpdate)
222 {
223 gLog << warn << "File '" << kNameout << "' doesn't yet exist." << endl;
224 kUpdate=kFALSE;
225 }
226
227 MArray::Class()->IgnoreTObjectStreamer();
228 MParContainer::Class()->IgnoreTObjectStreamer();
229
230 //
231 // create a (empty) list of parameters which can be used by the tasks
232 // and an (empty) list of tasks which should be executed
233 //
234 MParList plist;
235
236 MTaskList tasks;
237 tasks.SetOwner();
238 plist.AddToList(&tasks);
239
240 //
241 // ---- The following is only necessary to supress some output ----
242 /*
243 MRawRunHeader runheader;
244 plist.AddToList(&runheader);
245
246 MRawEvtHeader evtheader;
247 plist.AddToList(&evtheader);
248
249 MRawEvtData evtdata;
250 plist.AddToList(&evtdata);
251
252 MRawCrateArray cratearray;
253 plist.AddToList(&cratearray);
254
255 MTime evttime;
256 plist.AddToList(&evttime);
257 */
258
259 //
260 // create the tasks which should be executed and add them to the list
261 // in the case you don't need parameter containers, all of them can
262 // be created by MRawFileRead::PreProcess
263 //
264 MTask *read = 0;
265 MFilter *filter = 0;
266 MTask *write = 0;
267
268 const TString option(kUpdate ? "UPDATE" : "RECREATE");
269 if (isreport || isdc)
270 {
271 MWriteRootFile *w = new MWriteRootFile(kNameout, option, "Magic root-file", kComprlvl);
272 if (isdc)
273 {
274 w->AddContainer("MTimeCurrents", "Currents");
275 w->AddContainer("MCameraDC", "Currents");
276 w->AddContainer("MReportCurrents", "Currents");
277 }
278 else
279 {
280 w->AddContainer("MReportCamera", "Camera");
281 w->AddContainer("MTimeCamera", "Camera");
282 w->AddContainer("MCameraAUX", "Camera");
283 w->AddContainer("MCameraCalibration", "Camera");
284 w->AddContainer("MCameraCooling", "Camera");
285 w->AddContainer("MCameraHV", "Camera");
286 w->AddContainer("MCameraLV", "Camera");
287 w->AddContainer("MCameraLids", "Camera");
288 w->AddContainer("MReportTrigger", "Trigger");
289 w->AddContainer("MTimeTrigger", "Trigger");
290 w->AddContainer("MReportDrive", "Drive");
291 w->AddContainer("MTimeDrive", "Drive");
292 w->AddContainer("MReportCC", "CC");
293 w->AddContainer("MTimeCC", "CC");
294 // w->AddContainer("MReportDAQ", "DAQ");
295 // w->AddContainer("MTimeDAQ", "DAQ");
296 }
297 write = w;
298
299 MReportFileReadCC *r = new MReportFileReadCC(kNamein);
300 r->SetTimeStart(kTimeStart);
301 r->SetTimeStop(kTimeStop);
302 if (isdc)
303 {
304 r->SetHasNoHeader();
305 r->AddToList("MReportCurrents");
306 }
307 else
308 {
309 r->SetRunNumber(kRunFile);
310 r->AddToList("MReportCC");
311 //r->AddToList("MReportDAQ");
312 r->AddToList("MReportDrive");
313 r->AddToList("MReportCamera");
314 r->AddToList("MReportTrigger");
315 if (kRunNumber>0)
316 {
317 r->AddToList("MReportRun");
318 filter = new MFDataMember("MReportRun.fRunNumber", '=', kRunNumber);
319 w->SetFilter(filter);
320 }
321 }
322 read = r;
323 }
324 else
325 {
326 read = new MRawFileRead(kNamein);
327 static_cast<MRawFileRead*>(read)->SetInterleave(kInterleave);
328 static_cast<MRawFileRead*>(read)->SetForceMode(kForceProc);
329 write = new MRawFileWrite(kNameout, option, "Magic root-file", kComprlvl);
330 }
331
332 tasks.AddToList(read);
333 if (filter)
334 tasks.AddToList(filter);
335 /*
336 if (israw && !kSqlDataBase.IsNull())
337 {
338 MSqlInsertRun *ins = new MSqlInsertRun(kSqlDataBase);
339 ins->SetUpdate();
340 tasks.AddToList(ins);
341 }*/
342 tasks.AddToList(write);
343
344 //
345 // create the looping object and tell it about the parameters to use
346 // and the tasks to execute
347 //
348 MEvtLoop magic;
349 magic.SetParList(&plist);
350
351 //
352 // Start the eventloop which reads the raw file (MRawFileRead) and
353 // write all the information into a root file (MRawFileWrite)
354 //
355 // between reading and writing we can do, transformations, checks, etc.
356 // (I'm think of a task like MRawDataCheck)
357 //
358 if (!magic.Eventloop())
359 {
360 gLog << err << "ERROR: Merging and preprocessing failed!" << endl;
361 return -1;
362 }
363
364 tasks.PrintStatistics();
365
366 gLog << all << "Merpp finished successfull!" << endl;
367 return 0;
368}
Note: See TracBrowser for help on using the repository browser.