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

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