source: tags/Mars-V0.8.5/merpp.cc@ 19868

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