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

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