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

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