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

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