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

Last change on this file since 3370 was 3364, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 11.3 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(AnalyseTime(arg.GetStringAndRemove("--start=")));
128 MTime kTimeStop(AnalyseTime(arg.GetStringAndRemove("--stop=")));
129
130 const Int_t kRunNumber = arg.HasOption("--run=") ? arg.GetIntAndRemove("--run=") : -1;
131 const TString kSqlDataBase(arg.GetStringAndRemove("--sql="));
132
133 if (kTimeStart)
134 gLog << inf << "Start Time: " << kTimeStart << endl;
135 if (kTimeStop)
136 gLog << inf << "Stop Time: " << kTimeStop << endl;
137
138 //
139 // check for the right usage of the program
140 //
141 if (arg.GetNumArguments()<1 || arg.GetNumArguments()>2)
142 {
143 Usage();
144 return -1;
145 }
146
147 //
148 // This is to make argv[i] more readable insidethe code
149 //
150 TString kNamein = arg.GetArgumentStr(0);
151 TString kNameout = arg.GetArgumentStr(1);
152
153 const Bool_t isreport = kNamein.EndsWith(".rep");
154 const Bool_t isdc = kNamein.EndsWith(".txt");
155 const Bool_t israw = !isreport && !isdc;
156
157 if (!kNamein.EndsWith(".raw") && israw)
158 kNamein += ".raw";
159
160 if (kNameout.IsNull())
161 kNameout = kNamein(0, kNamein.Last('.'));
162
163 if (!kNameout.EndsWith(".root"))
164 kNameout += ".root";
165
166 if (!kSqlDataBase.IsNull() && !israw)
167 gLog << warn << "WARNING - Option '--sql=' only valid for raw-files... ignored." << endl;
168
169 //
170 // Initialize Non-GUI (batch) mode
171 //
172 gROOT->SetBatch();
173
174 //
175 // check whether the given files are OK.
176 //
177 if (gSystem->AccessPathName(kNamein, kFileExists))
178 {
179 gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
180 return -1;
181 }
182
183 const Bool_t fileexist = !gSystem->AccessPathName(kNameout, kFileExists);
184 const Bool_t writeperm = !gSystem->AccessPathName(kNameout, kWritePermission);
185
186 if (fileexist && !writeperm)
187 {
188 gLog << err << "Sorry, you don't have write permission for '" << kNameout << "'." << endl;
189 return -1;
190 }
191
192 if (fileexist && !kUpdate)
193 {
194 gLog << err << "Sorry, file '" << kNameout << "' already existing." << endl;
195 return -1;
196 }
197
198 if (!fileexist && kUpdate)
199 {
200 gLog << warn << "File '" << kNameout << "' doesn't yet exist." << endl;
201 kUpdate=kFALSE;
202 }
203
204 MArray::Class()->IgnoreTObjectStreamer();
205 MParContainer::Class()->IgnoreTObjectStreamer();
206
207 //
208 // create a (empty) list of parameters which can be used by the tasks
209 // and an (empty) list of tasks which should be executed
210 //
211 MParList plist;
212
213 MTaskList tasks;
214 tasks.SetOwner();
215 plist.AddToList(&tasks);
216
217 //
218 // ---- The following is only necessary to supress some output ----
219 /*
220 MRawRunHeader runheader;
221 plist.AddToList(&runheader);
222
223 MRawEvtHeader evtheader;
224 plist.AddToList(&evtheader);
225
226 MRawEvtData evtdata;
227 plist.AddToList(&evtdata);
228
229 MRawCrateArray cratearray;
230 plist.AddToList(&cratearray);
231
232 MTime evttime;
233 plist.AddToList(&evttime);
234 */
235
236 //
237 // create the tasks which should be executed and add them to the list
238 // in the case you don't need parameter containers, all of them can
239 // be created by MRawFileRead::PreProcess
240 //
241 MTask *read = 0;
242 MFilter *filter = 0;
243 MTask *write = 0;
244
245 const TString option(kUpdate ? "UPDATE" : "RECREATE");
246 if (isreport || isdc)
247 {
248 MWriteRootFile *w = new MWriteRootFile(kNameout, option, "Magic root-file", kComprlvl);
249 if (isdc)
250 {
251 w->AddContainer("MTimeCurrents", "Currents");
252 w->AddContainer("MCameraDC", "Currents");
253 w->AddContainer("MReportCurrents", "Currents");
254 }
255 else
256 {
257 w->AddContainer("MReportCamera", "Camera");
258 w->AddContainer("MTimeCamera", "Camera");
259 w->AddContainer("MCameraAUX", "Camera");
260 w->AddContainer("MCameraCalibration", "Camera");
261 w->AddContainer("MCameraCooling", "Camera");
262 w->AddContainer("MCameraHV", "Camera");
263 w->AddContainer("MCameraLV", "Camera");
264 w->AddContainer("MCameraLids", "Camera");
265 w->AddContainer("MReportTrigger", "Trigger");
266 w->AddContainer("MTimeTrigger", "Trigger");
267 w->AddContainer("MReportDrive", "Drive");
268 w->AddContainer("MTimeDrive", "Drive");
269 w->AddContainer("MReportCC", "CC");
270 w->AddContainer("MTimeCC", "CC");
271 // w->AddContainer("MReportDAQ", "DAQ");
272 // w->AddContainer("MTimeDAQ", "DAQ");
273 }
274 write = w;
275
276 MReportFileRead *r = new MReportFileRead(kNamein);
277 r->SetTimeStart(kTimeStart);
278 r->SetTimeStop(kTimeStop);
279 if (isdc)
280 {
281 r->SetHasNoHeader();
282 r->AddToList("MReportCurrents");
283 }
284 else
285 {
286 r->AddToList("MReportCC");
287 //r->AddToList("MReportDAQ");
288 r->AddToList("MReportDrive");
289 r->AddToList("MReportCamera");
290 r->AddToList("MReportTrigger");
291 if (kRunNumber>0)
292 {
293 r->AddToList("MReportRun");
294 filter = new MFDataMember("MReportRun.fRunNumber", '=', kRunNumber);
295 w->SetFilter(filter);
296 }
297 }
298 read = r;
299 }
300 else
301 {
302 read = new MRawFileRead(kNamein);
303 write = new MRawFileWrite(kNameout, option, "Magic root-file", kComprlvl);
304 }
305
306 tasks.AddToList(read);
307 if (filter)
308 tasks.AddToList(filter);
309 if (israw && !kSqlDataBase.IsNull())
310 {
311 MSqlInsertRun *ins = new MSqlInsertRun(kSqlDataBase);
312 ins->SetUpdate();
313 tasks.AddToList(ins);
314 }
315 tasks.AddToList(write);
316
317 //
318 // create the looping object and tell it about the parameters to use
319 // and the tasks to execute
320 //
321 MEvtLoop magic;
322 magic.SetParList(&plist);
323
324 //
325 // Start the eventloop which reads the raw file (MRawFileRead) and
326 // write all the information into a root file (MRawFileWrite)
327 //
328 // between reading and writing we can do, transformations, checks, etc.
329 // (I'm think of a task like MRawDataCheck)
330 //
331 if (!magic.Eventloop())
332 {
333 gLog << err << "ERROR: Merging and preprocessing failed!" << endl;
334 return -1;
335 }
336
337 tasks.PrintStatistics();
338
339 gLog << all << "Merpp finished successfull!" << endl;
340 return 0;
341}
Note: See TracBrowser for help on using the repository browser.