1 |
mocchiut |
1.1 |
// |
2 |
|
|
// C/C++ headers |
3 |
|
|
// |
4 |
|
|
#include <fstream> |
5 |
|
|
#include <string.h> |
6 |
|
|
// |
7 |
|
|
// ROOT headers |
8 |
|
|
// |
9 |
|
|
#include <TTree.h> |
10 |
|
|
#include <TClassEdit.h> |
11 |
|
|
#include <TObject.h> |
12 |
|
|
#include <TList.h> |
13 |
|
|
#include <TArrayL.h> |
14 |
|
|
#include <TSystem.h> |
15 |
|
|
#include <TSystemDirectory.h> |
16 |
|
|
#include <TString.h> |
17 |
|
|
#include <TFile.h> |
18 |
|
|
#include <TClass.h> |
19 |
|
|
#include <TCanvas.h> |
20 |
|
|
#include <TH1.h> |
21 |
|
|
#include <TH1F.h> |
22 |
|
|
#include <TH2D.h> |
23 |
|
|
#include <TLatex.h> |
24 |
|
|
#include <TPad.h> |
25 |
|
|
#include <TSQLServer.h> |
26 |
|
|
#include <TSQLRow.h> |
27 |
|
|
#include <TSQLResult.h> |
28 |
|
|
#include <TClonesArray.h> |
29 |
|
|
// |
30 |
|
|
// YODA headers |
31 |
|
|
// |
32 |
|
|
#include <PamelaRun.h> |
33 |
|
|
#include <RegistryEvent.h> |
34 |
|
|
#include <physics/trigger/TriggerEvent.h> |
35 |
|
|
// |
36 |
|
|
// RunInfo header |
37 |
|
|
// |
38 |
|
|
#include <RunInfo.h> |
39 |
|
|
// |
40 |
|
|
// This program headers |
41 |
|
|
// |
42 |
|
|
#include <MyDect2Core.h> |
43 |
|
|
#include <MyDect2Level2.h> |
44 |
|
|
#include <MyDect2Verl2.h> |
45 |
|
|
// |
46 |
|
|
// |
47 |
|
|
// Declaration of the core fortran routines |
48 |
|
|
// |
49 |
|
|
#define mydeccom mydeccom_ |
50 |
|
|
extern "C" int mydeccom(); |
51 |
|
|
#define mydectrk mydectrk_ |
52 |
|
|
extern "C" int mydectrk(); |
53 |
|
|
// |
54 |
|
|
// Tracker classes headers and definitions |
55 |
|
|
// |
56 |
|
|
#include <TrkLevel2.h> |
57 |
|
|
#define readB readb_ |
58 |
|
|
extern "C" int readB(); |
59 |
|
|
// |
60 |
|
|
using namespace std; |
61 |
|
|
// |
62 |
|
|
Bool_t existfile(TString filename){ |
63 |
|
|
ifstream myfile; |
64 |
|
|
myfile.open(filename.Data()); |
65 |
|
|
if ( !myfile ){ |
66 |
|
|
return(false); |
67 |
|
|
}; |
68 |
|
|
myfile.close(); |
69 |
|
|
return(true); |
70 |
|
|
} |
71 |
|
|
// |
72 |
|
|
// |
73 |
|
|
// CORE ROUTINE |
74 |
|
|
// |
75 |
|
|
// |
76 |
|
|
int MyDect2Core(ULong64_t run, TString filename, TSQLServer *dbc){ |
77 |
|
|
// |
78 |
|
|
// Set these to true to have a very verbose output. |
79 |
|
|
// |
80 |
|
|
// Bool_t debug = true; |
81 |
|
|
Bool_t debug = false; |
82 |
|
|
// |
83 |
|
|
// Output directory is the working directoy. |
84 |
|
|
// |
85 |
|
|
const char* outdir = gSystem->WorkingDirectory(); |
86 |
|
|
// |
87 |
|
|
// Working filename |
88 |
|
|
// |
89 |
|
|
TString outputfile; |
90 |
|
|
stringstream name; |
91 |
|
|
name.str(""); |
92 |
|
|
name << outdir << "/"; |
93 |
|
|
// |
94 |
|
|
// Variables. |
95 |
|
|
// |
96 |
|
|
TFile *file = 0; |
97 |
|
|
TTree *tracker = 0; |
98 |
|
|
TTree *mydect = 0; |
99 |
|
|
TTree *mydectclone = 0; |
100 |
|
|
Bool_t reproc = false; |
101 |
|
|
Bool_t reprocall = false; |
102 |
|
|
Long64_t nevents = 0LL; |
103 |
|
|
UInt_t nobefrun = 0; |
104 |
|
|
UInt_t noaftrun = 0; |
105 |
|
|
UInt_t numbofrun = 0; |
106 |
|
|
// |
107 |
|
|
Int_t code = 0; |
108 |
|
|
Int_t sgnl; |
109 |
|
|
// |
110 |
|
|
// mydetector2 level2 classes |
111 |
|
|
// |
112 |
|
|
MyDect2Level2 *mydec = new MyDect2Level2(); |
113 |
|
|
MyDect2Level2 *mydecclone = new MyDect2Level2(); |
114 |
|
|
// |
115 |
|
|
TrkLevel2 *trk = new TrkLevel2(); |
116 |
|
|
Long64_t nevtrkl2 = 0; |
117 |
|
|
// |
118 |
|
|
// MySQL variables |
119 |
|
|
// |
120 |
|
|
TSQLResult *pResult; |
121 |
|
|
TSQLRow *Row; |
122 |
|
|
Int_t t; |
123 |
|
|
Int_t r; |
124 |
|
|
UInt_t procev = 0; |
125 |
|
|
stringstream myquery; |
126 |
|
|
// |
127 |
|
|
// define variables where to store the absolute run header and run trailer times (unsigned long long integers, when set to a number use ULL to store the correct number). |
128 |
|
|
// |
129 |
|
|
ULong64_t runheadtime = 0ULL; |
130 |
|
|
ULong64_t runtrailtime = 0ULL; |
131 |
|
|
UInt_t evfrom = 0; |
132 |
|
|
UInt_t evto = 0; |
133 |
|
|
Long64_t totfileentries = 0ULL; |
134 |
|
|
Long64_t idRun = 0LL; |
135 |
|
|
Int_t id_reg_run=-1; |
136 |
|
|
stringstream ftmpname; |
137 |
|
|
TString fname; |
138 |
|
|
// |
139 |
|
|
// define variables for opening and reading level0 file |
140 |
|
|
// |
141 |
|
|
TFile *l0File = 0; |
142 |
|
|
TTree *l0tr = 0; |
143 |
|
|
TBranch *l0head = 0; |
144 |
|
|
TBranch *l0registry = 0; |
145 |
|
|
TBranch *l0trig = 0; |
146 |
|
|
pamela::RegistryEvent *l0reg=0; |
147 |
|
|
pamela::trigger::TriggerEvent *trig = 0; |
148 |
|
|
// |
149 |
|
|
// Define some basic variables |
150 |
|
|
// |
151 |
|
|
stringstream file2; |
152 |
|
|
stringstream file3; |
153 |
|
|
stringstream qy; |
154 |
|
|
Bool_t imtrack = false; |
155 |
|
|
// |
156 |
|
|
Int_t itr = -1; |
157 |
|
|
Int_t totevent = 0; |
158 |
|
|
// |
159 |
|
|
ULong64_t atime = 0ULL; |
160 |
|
|
// |
161 |
|
|
Int_t ei = 0; |
162 |
|
|
UInt_t re = 0; |
163 |
|
|
// |
164 |
|
|
TString mydectversion; |
165 |
|
|
RunInfo *runinfo = 0; |
166 |
|
|
TArrayL *runlist = 0; |
167 |
|
|
// |
168 |
|
|
Int_t ntrkentry = 0; |
169 |
|
|
stringstream Bpath; |
170 |
|
|
stringstream Bname; |
171 |
|
|
extern struct cParamPath parampath_; |
172 |
|
|
ULong64_t fttrkpar1 = 0ULL; |
173 |
|
|
ULong64_t tttrkpar1 = 0ULL; |
174 |
|
|
Bool_t trkpar1 = true; |
175 |
|
|
// |
176 |
|
|
// declaring external output and input structures |
177 |
|
|
// |
178 |
|
|
extern struct InputStruct cinput_; |
179 |
|
|
extern struct OutputStruct coutput_; |
180 |
|
|
// |
181 |
|
|
// As a first thing we must check what we have to do: if run = 0 we must process all events in the file has been passed |
182 |
|
|
// if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file |
183 |
|
|
// if it exists we are reprocessing data and we must delete that entries, if not we must create it. |
184 |
|
|
// |
185 |
|
|
if ( run != 0ULL ){ |
186 |
|
|
// |
187 |
|
|
// Run is not 0, we must process only one run . |
188 |
|
|
// |
189 |
|
|
// |
190 |
|
|
// If no filename is given, use the deafult one run_id.Level2.root. |
191 |
|
|
// |
192 |
|
|
if ( !strcmp(filename.Data(),"") ){ |
193 |
|
|
name << run << ".Level2.root"; |
194 |
|
|
} else { |
195 |
|
|
name << filename.Data(); |
196 |
|
|
}; |
197 |
|
|
// |
198 |
|
|
} else { |
199 |
|
|
// |
200 |
|
|
// Run is 0, we must process all entries in the given file. |
201 |
|
|
// |
202 |
|
|
// |
203 |
|
|
// OUCH! no filename has been given, run out! |
204 |
|
|
// |
205 |
|
|
if ( !strcmp(filename.Data(),"") ) { |
206 |
|
|
printf(" MYDETECTOR2 - ERROR: processing all runs but no filename is given\n"); |
207 |
|
|
return(-4); |
208 |
|
|
}; |
209 |
|
|
name << filename.Data(); |
210 |
|
|
reproc = true; |
211 |
|
|
// |
212 |
|
|
}; |
213 |
|
|
// |
214 |
|
|
// Output file is "outputfile" |
215 |
|
|
// |
216 |
|
|
outputfile = name.str().c_str(); |
217 |
|
|
printf("\n Output filename is: \n %s \n\n",outputfile.Data()); |
218 |
|
|
// |
219 |
|
|
// Check if file exists, if not exit with error (we need tracker data). |
220 |
|
|
// |
221 |
|
|
if ( !existfile(outputfile.Data()) ) { |
222 |
|
|
printf(" MYDETECTOR2 - ERROR: no Level2 file\n"); |
223 |
|
|
return(-100); |
224 |
|
|
}; |
225 |
|
|
// |
226 |
|
|
// OK, file exists, open it in "update" mode. |
227 |
|
|
// |
228 |
|
|
file = new TFile(outputfile.Data(),"UPDATE"); |
229 |
|
|
if ( !file->IsOpen() ){ |
230 |
|
|
printf(" MYDETECTOR2 - ERROR: cannot open file for writing\n"); |
231 |
|
|
return(-101); |
232 |
|
|
}; |
233 |
|
|
// |
234 |
|
|
// Does it contain the Tracker tree? |
235 |
|
|
// |
236 |
|
|
tracker = (TTree*)file->Get("Tracker"); |
237 |
|
|
if ( !tracker ) { |
238 |
|
|
printf(" MYDETECTOR2 - ERROR: no tracker tree\n"); |
239 |
|
|
code = -102; |
240 |
|
|
goto closeandexit; |
241 |
|
|
}; |
242 |
|
|
// |
243 |
|
|
// get tracker level2 data pointer |
244 |
|
|
// |
245 |
|
|
tracker->SetBranchAddress("TrkLevel2",&trk); |
246 |
|
|
nevtrkl2 = tracker->GetEntries(); |
247 |
|
|
// |
248 |
|
|
// update (or create) versioning information using RunInfo |
249 |
|
|
// |
250 |
|
|
mydectversion = MyDect2Info(false); |
251 |
|
|
sgnl = 0; |
252 |
|
|
runinfo = new RunInfo(dbc,file,run,"MYDECT2",mydectversion,sgnl); |
253 |
|
|
if ( sgnl ){ |
254 |
|
|
printf(" MYDETECTOR2 - ERROR: RunInfo exited with non-zero status\n"); |
255 |
|
|
code = sgnl; |
256 |
|
|
goto closeandexit; |
257 |
|
|
} else { |
258 |
|
|
sgnl = 0; |
259 |
|
|
}; |
260 |
|
|
// |
261 |
|
|
// number of events in the file BEFORE the first event of our run |
262 |
|
|
// |
263 |
|
|
nobefrun = runinfo->GetFirstFileEntry(); |
264 |
|
|
// |
265 |
|
|
// total number of events in the file |
266 |
|
|
// |
267 |
|
|
totfileentries = runinfo->GetFileEntries(); |
268 |
|
|
// |
269 |
|
|
// first file entry AFTER the last event of our run |
270 |
|
|
// |
271 |
|
|
noaftrun = runinfo->GetLastFileEntry(); |
272 |
|
|
// |
273 |
|
|
// number of run to be processed |
274 |
|
|
// |
275 |
|
|
numbofrun = runinfo->GetNoRun(); |
276 |
|
|
// |
277 |
|
|
// Try to access the MyDetector2 tree in the file, if it exists we are reprocessing data if not we are processing a new run |
278 |
|
|
// |
279 |
|
|
mydectclone = (TTree*)file->Get("MyDetector2"); |
280 |
|
|
// |
281 |
|
|
// create mydetector tree mydect |
282 |
|
|
// |
283 |
|
|
mydect = new TTree("MyDetector2","PAMELA Level2 MyDetector2 data"); |
284 |
|
|
mydect->Branch("MyDect2Level2","MyDect2Level2",&mydec); |
285 |
|
|
// |
286 |
|
|
if ( !mydectclone ){ |
287 |
|
|
// |
288 |
|
|
// tree does not exist, we are not reprocessing |
289 |
|
|
// |
290 |
|
|
reproc = false; |
291 |
|
|
if ( run == 0ULL ) printf(" MYDETECTOR2 - WARNING: you are reprocessing data but MyDetector2 tree does not exist!\n"); |
292 |
|
|
if ( runinfo->IsReprocessing() && run != 0ULL ) printf(" MYDETECTOR2 - WARNING: it seems you are not reprocessing data but mydetector2\n versioning information already exists in RunInfo.\n"); |
293 |
|
|
|
294 |
|
|
} else { |
295 |
|
|
// |
296 |
|
|
// tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? |
297 |
|
|
// |
298 |
|
|
reproc = true; |
299 |
|
|
// |
300 |
|
|
if ( run == 0ULL ){ |
301 |
|
|
// |
302 |
|
|
// we are reprocessing all the file |
303 |
|
|
// |
304 |
|
|
// |
305 |
|
|
// if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately |
306 |
|
|
// |
307 |
|
|
printf("\n MYDETECTOR2 - WARNING: Reprocessing all runs\n"); |
308 |
|
|
reprocall = true; |
309 |
|
|
mydectclone->Delete("all"); |
310 |
|
|
} else { |
311 |
|
|
reprocall = false; |
312 |
|
|
// |
313 |
|
|
// we are reprocessing a single run, we must copy to the new tree the events in the file which preceed the first event of the run |
314 |
|
|
// |
315 |
|
|
printf("\n MYDETECTOR2 - WARNING: Reprocessing run number %i \n",(int)run); |
316 |
|
|
// |
317 |
|
|
// access old branch in old tree |
318 |
|
|
// |
319 |
|
|
mydectclone->SetBranchAddress("MyDect2Level2",&mydecclone); |
320 |
|
|
// |
321 |
|
|
if ( nobefrun > 0 ){ |
322 |
|
|
printf("\n Pre-processing: copying events from the old tree before the processed run\n"); |
323 |
|
|
printf(" Copying %i events in the file which are before the beginning of the run %i \n",(int)(nobefrun),(int)run); |
324 |
|
|
printf(" Start copying at event number 0, end copying at event number %i \n",(int)nobefrun); |
325 |
|
|
for (UInt_t j = 0; j < nobefrun; j++){ |
326 |
|
|
// |
327 |
|
|
mydectclone->GetEntry(j); |
328 |
|
|
// |
329 |
|
|
// copy caclone to ca |
330 |
|
|
// |
331 |
|
|
memcpy(&mydec,&mydecclone,sizeof(mydec)); |
332 |
|
|
// |
333 |
|
|
// Fill entry in the new tree |
334 |
|
|
// |
335 |
|
|
mydect->Fill(); |
336 |
|
|
// |
337 |
|
|
}; |
338 |
|
|
printf(" Finished successful copying!\n"); |
339 |
|
|
}; |
340 |
|
|
}; |
341 |
|
|
}; |
342 |
|
|
// |
343 |
|
|
// Get the list of run to be processed, if only one run has to be processed the list will contain one entry only. |
344 |
|
|
// |
345 |
|
|
runlist = runinfo->GetRunList(); |
346 |
|
|
// |
347 |
|
|
// Loop over the run to be processed |
348 |
|
|
// |
349 |
|
|
for (UInt_t irun=0; irun < numbofrun; irun++){ |
350 |
|
|
// |
351 |
|
|
// retrieve the first run ID to be processed using the RunInfo list |
352 |
|
|
// |
353 |
|
|
idRun = runlist->At(irun); |
354 |
|
|
printf("\n\n\n ####################################################################### \n"); |
355 |
|
|
printf(" PROCESSING RUN NUMBER %i \n",(int)idRun); |
356 |
|
|
printf(" ####################################################################### \n\n\n"); |
357 |
|
|
// |
358 |
|
|
// store in the runinfo class the GL_RUN variables for our run |
359 |
|
|
// |
360 |
|
|
sgnl = runinfo->GetRunInfo(idRun); |
361 |
|
|
if ( sgnl ){ |
362 |
|
|
printf(" MYDETECTOR2 - ERROR: RunInfo exited with non-zero status\n"); |
363 |
|
|
code = sgnl; |
364 |
|
|
goto closeandexit; |
365 |
|
|
} else { |
366 |
|
|
sgnl = 0; |
367 |
|
|
}; |
368 |
|
|
// |
369 |
|
|
// now you can access that variables using the RunInfo class this way |
370 |
|
|
// |
371 |
|
|
id_reg_run = runinfo->ID_REG_RUN; |
372 |
|
|
runheadtime = runinfo->RUNHEADER_TIME; |
373 |
|
|
runtrailtime = runinfo->RUNTRAILER_TIME; |
374 |
|
|
evfrom = runinfo->EV_REG_PHYS_FROM; |
375 |
|
|
evto = runinfo->EV_REG_PHYS_TO; |
376 |
|
|
// |
377 |
|
|
if ( id_reg_run == -1 ){ |
378 |
|
|
printf("\n MYDETECTOR2 - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun); |
379 |
|
|
code = -5; |
380 |
|
|
goto closeandexit; |
381 |
|
|
}; |
382 |
|
|
// |
383 |
|
|
// Search in the DB the path and name of the LEVEL0 file to be processed. |
384 |
|
|
// |
385 |
|
|
printf("\n Querying DB for data files informations...\n"); |
386 |
|
|
// |
387 |
|
|
// where can I find the file with the id I have just recovered? |
388 |
|
|
// |
389 |
|
|
myquery.str(""); |
390 |
|
|
myquery << "select PATH,NAME from GL_ROOT where ID=" << id_reg_run; |
391 |
|
|
myquery << ";"; |
392 |
|
|
// |
393 |
|
|
if ( debug ) printf("The query is:\n \"%s\" \n",myquery.str().c_str()); |
394 |
|
|
// |
395 |
|
|
pResult = dbc->Query(myquery.str().c_str()); |
396 |
|
|
if ( !pResult ) { |
397 |
|
|
printf(" MYDETECTOR2 - ERROR: error querying the DB [6]\n"); |
398 |
|
|
code = -14; |
399 |
|
|
goto closeandexit; |
400 |
|
|
}; |
401 |
|
|
ftmpname.str(""); |
402 |
|
|
// |
403 |
|
|
for( r=0; r < 1000; r++){ |
404 |
|
|
Row = pResult->Next(); |
405 |
|
|
if( Row == NULL ) break; |
406 |
|
|
for( t = 0; t < pResult->GetFieldCount(); t++){ |
407 |
|
|
// |
408 |
|
|
// put in fpath the path to that file |
409 |
|
|
// |
410 |
|
|
if ( t == 0 ) ftmpname << Row->GetField(t) << "/"; |
411 |
|
|
if ( t == 1 ) ftmpname << Row->GetField(t); |
412 |
|
|
if ( debug ) printf(" Row %i value = %s \n",t,Row->GetField(t)); |
413 |
|
|
}; |
414 |
|
|
}; |
415 |
|
|
delete pResult; |
416 |
|
|
// |
417 |
|
|
fname = ftmpname.str().c_str(); |
418 |
|
|
// |
419 |
|
|
// print out informations |
420 |
|
|
// |
421 |
|
|
totevent = evto - evfrom + 1; |
422 |
|
|
printf("\n LEVEL0 data file: %s \n",fname.Data()); |
423 |
|
|
printf(" RUN HEADER absolute time is: %llu \n",runheadtime); |
424 |
|
|
printf(" RUN TRAILER absolute time is: %llu \n",runtrailtime); |
425 |
|
|
printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evto); |
426 |
|
|
// |
427 |
|
|
// Open Level0 file |
428 |
|
|
// |
429 |
|
|
l0File = new TFile(fname.Data()); |
430 |
|
|
if ( !l0File ) { |
431 |
|
|
printf(" MYDETECTOR2 - ERROR: problems opening Level0 file\n"); |
432 |
|
|
code = -6; |
433 |
|
|
goto closeandexit; |
434 |
|
|
}; |
435 |
|
|
l0tr = (TTree*)l0File->Get("Physics"); |
436 |
|
|
if ( !l0tr ) { |
437 |
|
|
printf(" MYDETECTOR2 - ERROR: no Physics tree in Level0 file\n"); |
438 |
|
|
l0File->Close(); |
439 |
|
|
code = -7; |
440 |
|
|
goto closeandexit; |
441 |
|
|
}; |
442 |
|
|
l0head = l0tr->GetBranch("Header"); |
443 |
|
|
if ( !l0head ) { |
444 |
|
|
printf(" MYDETECTOR2 - ERROR: no Header branch in Level0 tree\n"); |
445 |
|
|
l0File->Close(); |
446 |
|
|
code = -8; |
447 |
|
|
goto closeandexit; |
448 |
|
|
}; |
449 |
|
|
l0registry = l0tr->GetBranch("Registry"); |
450 |
|
|
if ( !l0registry ) { |
451 |
|
|
printf(" MYDETECTOR2 - ERROR: no Registry branch in Level0 tree\n"); |
452 |
|
|
l0File->Close(); |
453 |
|
|
code = -9; |
454 |
|
|
goto closeandexit; |
455 |
|
|
}; |
456 |
|
|
l0trig = l0tr->GetBranch("Trigger"); |
457 |
|
|
if ( !l0trig ) { |
458 |
|
|
printf(" MYDETECTOR2 - ERROR: no Trigger branch in Level0 tree\n"); |
459 |
|
|
l0File->Close(); |
460 |
|
|
code = -104; |
461 |
|
|
goto closeandexit; |
462 |
|
|
}; |
463 |
|
|
// |
464 |
|
|
l0tr->SetBranchAddress("Trigger", &trig); |
465 |
|
|
l0tr->SetBranchAddress("Registry", &l0reg); |
466 |
|
|
// |
467 |
|
|
nevents = l0registry->GetEntries(); |
468 |
|
|
// |
469 |
|
|
if ( nevents < 1 ) { |
470 |
|
|
printf(" MYDETECTOR2 - ERROR: Level0 file is empty\n\n"); |
471 |
|
|
l0File->Close(); |
472 |
|
|
code = -11; |
473 |
|
|
goto closeandexit; |
474 |
|
|
}; |
475 |
|
|
// |
476 |
|
|
if ( evto > nevents-1 ) { |
477 |
|
|
printf(" MYDETECTOR2 - ERROR: too few entries in the registry tree\n"); |
478 |
|
|
l0File->Close(); |
479 |
|
|
code = -12; |
480 |
|
|
goto closeandexit; |
481 |
|
|
}; |
482 |
|
|
// |
483 |
|
|
// Check if we have to load parameter files (or calibration associated to runs and not to events) |
484 |
|
|
// |
485 |
|
|
// for example let's assume that we could have different magnetic field maps for different runs: |
486 |
|
|
// |
487 |
|
|
if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){ |
488 |
|
|
trkpar1 = false; |
489 |
|
|
// |
490 |
|
|
// Load magnetic field maps. |
491 |
|
|
// |
492 |
|
|
// ============================================================= |
493 |
|
|
// Now retrieve information about parameters to process LEVEL2 |
494 |
|
|
// ============================================================= |
495 |
|
|
if ( debug ) printf("\n --------------------------------------"); |
496 |
|
|
if ( debug ) printf("\n Querying DB for parameter file info..."); |
497 |
|
|
if ( debug ) printf("\n --------------------------------------\n"); |
498 |
|
|
myquery.str(""); |
499 |
|
|
Bpath.str(""); |
500 |
|
|
Bname.str(""); |
501 |
|
|
myquery << "select PATH,NAME,FROM_TIME,TO_TIME from GL_PARAM where DESCR=\"field\""; |
502 |
|
|
if ( debug ) printf("Query:\n \"%s\" \n",myquery.str().c_str()); |
503 |
|
|
pResult = dbc->Query(myquery.str().c_str()); |
504 |
|
|
if ( !pResult ) { |
505 |
|
|
printf(" MYDETECTOR2 - ERROR: error querying the DB [9]\n"); |
506 |
|
|
code = -14; |
507 |
|
|
goto closeandexit; |
508 |
|
|
}; |
509 |
|
|
for( r=0; r < 1000; r++){ |
510 |
|
|
Row = pResult->Next(); |
511 |
|
|
if( Row == NULL ) break; |
512 |
|
|
for( t = 0; t < pResult->GetFieldCount(); t++){ |
513 |
|
|
if(r==0){ |
514 |
|
|
if(t==0) Bpath << Row->GetField(t); |
515 |
|
|
if(t==1) Bname << Row->GetField(t); |
516 |
|
|
char *pEnd; |
517 |
|
|
if ( t == 2 ) fttrkpar1 = strtoull(Row->GetField(t),&pEnd,0); |
518 |
|
|
if ( t == 3 ) tttrkpar1 = strtoull(Row->GetField(t),&pEnd,0); |
519 |
|
|
if ( debug ) printf("r=%i--> Row %i value = %s \n",r,t,Row->GetField(t)); |
520 |
|
|
} |
521 |
|
|
}; |
522 |
|
|
}; |
523 |
|
|
delete pResult; |
524 |
|
|
// |
525 |
|
|
Bpath << "/" << Bname.str().c_str(); |
526 |
|
|
strcpy(parampath_.B_parampath ,Bpath.str().c_str()); |
527 |
|
|
parampath_.B_pathlen = Bpath.str().size(); |
528 |
|
|
// ---------------------------- |
529 |
|
|
// Read the magnetic field |
530 |
|
|
// ---------------------------- |
531 |
|
|
printf(" Reading magnetic field maps: \n"); |
532 |
|
|
readB(); |
533 |
|
|
printf("\n"); |
534 |
|
|
}; |
535 |
|
|
// |
536 |
|
|
// run over all the events of the run |
537 |
|
|
// |
538 |
|
|
printf("\n Ready to start! \n\n Processed events: \n\n"); |
539 |
|
|
// |
540 |
|
|
for ( re = evfrom; re <= evto; re++){ |
541 |
|
|
// |
542 |
|
|
if ( procev%1000 == 0 && procev > 0 ) printf(" %iK \n",procev/1000); |
543 |
|
|
// |
544 |
|
|
l0registry->GetEntry(re); |
545 |
|
|
// |
546 |
|
|
// absolute time of this event |
547 |
|
|
// |
548 |
|
|
atime = l0reg->absTime; |
549 |
|
|
// |
550 |
|
|
// physics events is at entry number ei where |
551 |
|
|
// |
552 |
|
|
ei = l0reg->event; |
553 |
|
|
// |
554 |
|
|
if ( atime > runtrailtime || atime < runheadtime ) { |
555 |
|
|
printf(" MYDETECTOR2 - WARNING: event at time outside the run time window, skipping it\n"); |
556 |
|
|
goto jumpev; |
557 |
|
|
}; |
558 |
|
|
// |
559 |
|
|
// retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr" |
560 |
|
|
// |
561 |
|
|
if ( !reprocall ){ |
562 |
|
|
itr = nobefrun + (re - evfrom); |
563 |
|
|
} else { |
564 |
|
|
itr = runinfo->GetFirstFileEntry() + (re - evfrom); |
565 |
|
|
}; |
566 |
|
|
if ( itr > nevtrkl2 ){ |
567 |
|
|
printf(" MYDETECTOR2 - ERROR: no tracker events with entry = %i in Level2 file\n",itr); |
568 |
|
|
l0File->Close(); |
569 |
|
|
code = -113; |
570 |
|
|
goto closeandexit; |
571 |
|
|
}; |
572 |
|
|
tracker->GetEntry(itr); |
573 |
|
|
// |
574 |
|
|
procev++; |
575 |
|
|
// |
576 |
|
|
// start processing |
577 |
|
|
// |
578 |
|
|
mydec = new MyDect2Level2(); |
579 |
|
|
|
580 |
|
|
// |
581 |
|
|
// Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure |
582 |
|
|
// |
583 |
|
|
cinput_.myinputvar = (int)ei; |
584 |
|
|
|
585 |
|
|
// |
586 |
|
|
// Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables. |
587 |
|
|
// |
588 |
|
|
// |
589 |
|
|
// Calculate variables common to all tracks |
590 |
|
|
// |
591 |
|
|
mydeccom(); |
592 |
|
|
// |
593 |
|
|
// and now we must copy from the output structure to the level2 class: |
594 |
|
|
// |
595 |
|
|
mydec->myvar1 = coutput_.myvar1; |
596 |
|
|
mydec->myvar2 = coutput_.myvar2; |
597 |
|
|
mydec->myvar3 = coutput_.myvar3; |
598 |
|
|
// |
599 |
|
|
// Calculate track-related variables |
600 |
|
|
// |
601 |
|
|
// |
602 |
|
|
// Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker) |
603 |
|
|
// |
604 |
|
|
ntrkentry = 0; |
605 |
|
|
// |
606 |
|
|
if ( trk->GetNTracks() > 0 ){ |
607 |
|
|
// |
608 |
|
|
// We have at least one track |
609 |
|
|
// |
610 |
|
|
// |
611 |
|
|
// Run over tracks |
612 |
|
|
// |
613 |
|
|
for(Int_t nt=0; nt < trk->GetNTracks(); nt++){ |
614 |
|
|
// |
615 |
|
|
TrkTrack *pti = 0; |
616 |
|
|
TrkTrack *ptt = trk->GetTrack(nt); |
617 |
|
|
// |
618 |
|
|
imtrack = false; |
619 |
|
|
cinput_.trkchi2 = 0; |
620 |
|
|
coutput_.good = 1; |
621 |
|
|
// |
622 |
|
|
// If we have only one track, check if we have a couple of "mirrored" tracks or a unique track |
623 |
|
|
// |
624 |
|
|
if ( trk->GetNTracks() == 1 ){ |
625 |
|
|
if( ptt->HasImage() ){ |
626 |
|
|
imtrack = true; |
627 |
|
|
cinput_.trkchi2 = 1; |
628 |
|
|
pti = trk->GetTrackImage(nt); |
629 |
|
|
}; |
630 |
|
|
}; |
631 |
|
|
// |
632 |
|
|
// If we have a couple of tracks pass both to the fortran routine, it will decide which one is the good one |
633 |
|
|
// |
634 |
|
|
for (Int_t e = 0; e < 5 ; e++){ |
635 |
|
|
cinput_.al_pp[e][0] = ptt->al[e]; |
636 |
|
|
if ( imtrack ){ |
637 |
|
|
cinput_.al_pp[e][1] = pti->al[e]; |
638 |
|
|
} else { |
639 |
|
|
cinput_.al_pp[e][1] = 0.; |
640 |
|
|
}; |
641 |
|
|
}; |
642 |
|
|
// |
643 |
|
|
// Get tracker related variables for this track |
644 |
|
|
// |
645 |
|
|
mydectrk(); |
646 |
|
|
// |
647 |
|
|
// if we had no problem (coutput_.good = 1), fill and go on |
648 |
|
|
// |
649 |
|
|
if ( coutput_.good == 1 ) { |
650 |
|
|
// |
651 |
|
|
// Copy values in the class from the structure (we need to use a temporary class to store variables). |
652 |
|
|
// |
653 |
|
|
MyDect2TrkVar *t_mydec = new MyDect2TrkVar(); |
654 |
|
|
t_mydec->mytrkvar = coutput_.mytrkvar; |
655 |
|
|
// |
656 |
|
|
// Determine if we used the image track instead of the physics track |
657 |
|
|
// |
658 |
|
|
t_mydec->trkseqno = nt; |
659 |
|
|
if ( imtrack && cinput_.trkchi2 == 2 ) t_mydec->trkseqno = nt+100; // we have image track which seems to be better than the main track, used that one. |
660 |
|
|
if ( imtrack && cinput_.trkchi2 == -1 ) t_mydec->trkseqno = -nt-100; // we have image track but nor the main nor the image seems to be good, processed anyway |
661 |
|
|
// |
662 |
|
|
|
663 |
|
|
// |
664 |
|
|
// create a new object for this event with track-related variables |
665 |
|
|
// |
666 |
|
|
TClonesArray &t = *mydec->myTrk; |
667 |
|
|
new(t[ntrkentry]) MyDect2TrkVar(*t_mydec); |
668 |
|
|
// |
669 |
|
|
ntrkentry++; |
670 |
|
|
} else { |
671 |
|
|
printf(" Problems with event at entry %i (track related variables not saved)\n",itr); |
672 |
|
|
}; |
673 |
|
|
}; // loop on all the tracks |
674 |
|
|
}; |
675 |
|
|
// |
676 |
|
|
// Here you may want to clear structures used to communicate with fortran |
677 |
|
|
// |
678 |
|
|
cinput_.myinputvar = 0; |
679 |
|
|
// |
680 |
|
|
// Fill the rootple |
681 |
|
|
// |
682 |
|
|
mydect->Fill(); |
683 |
|
|
// |
684 |
|
|
// |
685 |
|
|
jumpev: |
686 |
|
|
debug = false; |
687 |
|
|
// |
688 |
|
|
}; |
689 |
|
|
// |
690 |
|
|
// Here you may want to clear some variables before processing another run |
691 |
|
|
// |
692 |
|
|
ei = 0; |
693 |
|
|
}; // process all the runs |
694 |
|
|
// |
695 |
|
|
printf("\n Finished processing data \n"); |
696 |
|
|
// |
697 |
|
|
closeandexit: |
698 |
|
|
// |
699 |
|
|
// we have finished processing the run(s). If we processed a single run now we must copy all the events after our run from the old tree to the new one and delete the old tree. |
700 |
|
|
// |
701 |
|
|
if ( !reprocall && reproc && code >= 0 ){ |
702 |
|
|
if ( totfileentries > noaftrun ){ |
703 |
|
|
printf("\n Post-processing: copying events from the old tree after the processed run\n"); |
704 |
|
|
printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run); |
705 |
|
|
printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries); |
706 |
|
|
for (UInt_t j = noaftrun; j < totfileentries; j++ ){ |
707 |
|
|
// |
708 |
|
|
// Get entry from old tree |
709 |
|
|
// |
710 |
|
|
mydectclone->GetEntry(j); |
711 |
|
|
// |
712 |
|
|
// copy caclone to ca |
713 |
|
|
// |
714 |
|
|
memcpy(&mydec,&mydecclone,sizeof(mydec)); |
715 |
|
|
// |
716 |
|
|
// Fill entry in the new tree |
717 |
|
|
// |
718 |
|
|
mydect->Fill(); |
719 |
|
|
}; |
720 |
|
|
printf(" Finished successful copying!\n"); |
721 |
|
|
}; |
722 |
|
|
}; |
723 |
|
|
// |
724 |
|
|
// Case of no errors: close files, delete old tree(s), write and close level2 file |
725 |
|
|
// |
726 |
|
|
if ( code >= 0 ){ |
727 |
|
|
printf("\n Writing and closing rootple\n"); |
728 |
|
|
if ( mydecclone && !reprocall ) mydecclone->Delete("all"); |
729 |
|
|
if ( l0File ) l0File->Close(); |
730 |
|
|
tracker->Delete(); |
731 |
|
|
runinfo->Close(); |
732 |
|
|
file->Write(); |
733 |
|
|
file->Close(); |
734 |
|
|
} else { |
735 |
|
|
printf("\n MYDETECTOR2 - ERROR: an error occurred, exiting without saving\n"); |
736 |
|
|
}; |
737 |
|
|
// |
738 |
|
|
// the end |
739 |
|
|
// |
740 |
|
|
printf("\n Exiting...\n"); |
741 |
|
|
return(code); |
742 |
|
|
} |
743 |
|
|
|
744 |
|
|
|
745 |
|
|
|