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

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