1 |
// |
2 |
// C/C++ headers |
3 |
// |
4 |
#include <fstream> |
5 |
#include <string.h> |
6 |
#include <iomanip> |
7 |
// |
8 |
// ROOT headers |
9 |
// |
10 |
#include <TTree.h> |
11 |
#include <TClassEdit.h> |
12 |
#include <TObject.h> |
13 |
#include <TList.h> |
14 |
#include <TArrayI.h> |
15 |
#include <TSystem.h> |
16 |
#include <TSystemDirectory.h> |
17 |
#include <TString.h> |
18 |
#include <TFile.h> |
19 |
#include <TClass.h> |
20 |
#include <TCanvas.h> |
21 |
#include <TH1.h> |
22 |
#include <TH1F.h> |
23 |
#include <TH2D.h> |
24 |
#include <TLatex.h> |
25 |
#include <TPad.h> |
26 |
#include <TSQLServer.h> |
27 |
#include <TSQLRow.h> |
28 |
#include <TSQLResult.h> |
29 |
#include <TClonesArray.h> |
30 |
// |
31 |
// RunInfo header |
32 |
// |
33 |
#include <RunInfo.h> |
34 |
// |
35 |
// YODA headers |
36 |
// |
37 |
#include <PamelaRun.h> |
38 |
#include <physics/trigger/TriggerEvent.h> |
39 |
#include <physics/tof/TofEvent.h> |
40 |
// |
41 |
// This program headers |
42 |
// |
43 |
#include <ToFCore.h> |
44 |
#include <ToFLevel2.h> |
45 |
#include <ToFVerl2.h> |
46 |
// |
47 |
// |
48 |
// Declaration of the core fortran routines |
49 |
// |
50 |
#define tofl2com tofl2com_ |
51 |
extern "C" int tofl2com(); |
52 |
#define toftrk toftrk_ |
53 |
extern "C" int toftrk(); |
54 |
#define rdtofcal rdtofcal_ |
55 |
extern "C" int rdtofcal(char [], int *); |
56 |
|
57 |
// |
58 |
// Tracker classes headers and definitions |
59 |
// |
60 |
#include <TrkLevel2.h> |
61 |
// |
62 |
using namespace std; |
63 |
// |
64 |
// |
65 |
// CORE ROUTINE |
66 |
// |
67 |
// |
68 |
int ToFCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t ToFargc, char *ToFargv[]){ |
69 |
// |
70 |
// |
71 |
// Set these to true to have a very verbose output. |
72 |
// |
73 |
Bool_t verbose = false; |
74 |
Bool_t debug = false; |
75 |
// |
76 |
TString processFolder = Form("ToFFolder_%u",run); |
77 |
if ( ToFargc > 0 ){ |
78 |
Int_t i = 0; |
79 |
while ( i < ToFargc ){ |
80 |
if ( !strcmp(ToFargv[i],"-processFolder") ) { |
81 |
if ( ToFargc < i+1 ){ |
82 |
throw -3; |
83 |
}; |
84 |
processFolder = (TString)ToFargv[i+1]; |
85 |
i++; |
86 |
}; |
87 |
if ( !strcmp(ToFargv[i],"-v") || !strcmp(ToFargv[i],"--verbose") ) { |
88 |
verbose = true; |
89 |
}; |
90 |
if ( !strcmp(ToFargv[i],"-g") || !strcmp(ToFargv[i],"--debug") ) { |
91 |
verbose = true; |
92 |
debug = true; |
93 |
}; |
94 |
i++; |
95 |
}; |
96 |
}; |
97 |
// |
98 |
// |
99 |
// Output directory is the working directoy. |
100 |
// |
101 |
const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath())); |
102 |
// |
103 |
// Variables for level2 |
104 |
// |
105 |
TTree *tracker = 0; |
106 |
TTree *toft = 0; |
107 |
UInt_t nevents = 0; |
108 |
Long64_t maxsize = 10000000000LL; |
109 |
TTree::SetMaxTreeSize(maxsize); |
110 |
// |
111 |
// variables needed to reprocess data |
112 |
// |
113 |
TString tofversion; |
114 |
ItoRunInfo *runinfo = 0; |
115 |
TArrayI *runlist = 0; |
116 |
TTree *toftclone = 0; |
117 |
Bool_t reproc = false; |
118 |
Bool_t reprocall = false; |
119 |
UInt_t nobefrun = 0; |
120 |
UInt_t noaftrun = 0; |
121 |
UInt_t numbofrun = 0; |
122 |
stringstream ftmpname; |
123 |
TString fname; |
124 |
UInt_t totfileentries = 0; |
125 |
UInt_t idRun = 0; |
126 |
// |
127 |
// variables needed to handle error signals |
128 |
// |
129 |
Int_t code = 0; |
130 |
Int_t sgnl; |
131 |
// |
132 |
// tof level2 classes |
133 |
// |
134 |
ToFLevel2 *tof = new ToFLevel2(); |
135 |
ToFLevel2 *tofclone = new ToFLevel2(); |
136 |
// |
137 |
// tracker level2 variables |
138 |
// |
139 |
TrkLevel2 *trk = new TrkLevel2(); |
140 |
Int_t nevtrkl2 = 0; |
141 |
// |
142 |
// define variables for opening and reading level0 file |
143 |
// |
144 |
TFile *l0File = 0; |
145 |
TTree *l0tr = 0; |
146 |
TBranch *l0head = 0; |
147 |
TBranch *l0trig = 0; |
148 |
TBranch *l0tof = 0; |
149 |
pamela::EventHeader *eh = 0; |
150 |
pamela::PscuHeader *ph = 0; |
151 |
pamela::trigger::TriggerEvent *trig = 0; |
152 |
pamela::tof::TofEvent *tofEvent = 0; |
153 |
// |
154 |
// Define other basic variables |
155 |
// |
156 |
UInt_t procev = 0; |
157 |
stringstream file2; |
158 |
stringstream file3; |
159 |
stringstream qy; |
160 |
Int_t itr = -1; |
161 |
Int_t totevent = 0; |
162 |
UInt_t atime = 0; |
163 |
UInt_t re = 0; |
164 |
UInt_t jumped = 0; |
165 |
// |
166 |
// Working filename |
167 |
// |
168 |
TString outputfile; |
169 |
stringstream name; |
170 |
name.str(""); |
171 |
name << outdir << "/"; |
172 |
// |
173 |
// temporary file and folder |
174 |
// |
175 |
TFile *tempfile = 0; |
176 |
TTree *temptof = 0; |
177 |
stringstream tempname; |
178 |
stringstream toffolder; |
179 |
Bool_t myfold = false; |
180 |
tempname.str(""); |
181 |
tempname << outdir; |
182 |
tempname << "/" << processFolder.Data(); |
183 |
toffolder.str(""); |
184 |
toffolder << tempname.str().c_str(); |
185 |
tempname << "/toftree_run"; |
186 |
tempname << run << ".root"; |
187 |
// |
188 |
// variables needed to load magnetic field maps |
189 |
// |
190 |
Int_t ntrkentry = 0; |
191 |
Int_t npmtentry = 0; |
192 |
UInt_t tttrkpar1 = 0; |
193 |
Bool_t trkpar1 = true; |
194 |
UInt_t tttofpar1 = 0; |
195 |
Bool_t tofpar1 = true; |
196 |
// |
197 |
// DB classes |
198 |
// |
199 |
GL_ROOT *glroot = new GL_ROOT(); |
200 |
GL_PARAM *glparam = new GL_PARAM(); |
201 |
GL_TIMESYNC *dbtime = 0; |
202 |
// |
203 |
// declaring external output and input structures |
204 |
// |
205 |
extern struct ToFInput tofinput_; |
206 |
extern struct ToFOutput tofoutput_; |
207 |
// |
208 |
// WM variables perform dE/dx II order corrections |
209 |
// |
210 |
Float_t dedx_corr_m[100][48],dedx_corr[48]; |
211 |
Double_t mtime[100],t1,t2,tm; |
212 |
Float_t yhelp1,yhelp2,slope,inter,thelp1,thelp2; |
213 |
Float_t xmean1,xwidth1; |
214 |
Int_t ical,ii,wj,jj; |
215 |
// |
216 |
// Let's start! |
217 |
// |
218 |
// |
219 |
// 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 |
220 |
// if run != 0 we must process only that run but first we have to check if the tree ToF already exist in the file |
221 |
// if it exists we are reprocessing data and we must delete that entries, if not we must create it. |
222 |
// |
223 |
if ( run == 0 ) reproc = true; |
224 |
// |
225 |
// |
226 |
// Output file is "outputfile" |
227 |
// |
228 |
if ( !file->IsOpen() ){ |
229 |
if ( verbose ) printf(" ToF - ERROR: cannot open file for writing\n"); |
230 |
throw -301; |
231 |
}; |
232 |
// |
233 |
// Does it contain the Tracker tree? |
234 |
// |
235 |
tracker = (TTree*)file->Get("Tracker"); |
236 |
if ( !tracker ) { |
237 |
if ( verbose ) printf(" TOF - ERROR: no tracker tree\n"); |
238 |
code = -302; |
239 |
goto closeandexit; |
240 |
}; |
241 |
// |
242 |
// get tracker level2 data pointer |
243 |
// |
244 |
tracker->SetBranchAddress("TrkLevel2",&trk); |
245 |
nevtrkl2 = tracker->GetEntries(); |
246 |
// |
247 |
// Retrieve GL_RUN variables from the level2 file |
248 |
// |
249 |
tofversion = ToFInfo(false); // we should decide how to handle versioning system |
250 |
// |
251 |
// create an interface to RunInfo called "runinfo" |
252 |
// |
253 |
// ItoRunInfo= interface with RunInfo and GL_RUN |
254 |
runinfo = new ItoRunInfo(file); |
255 |
// |
256 |
// open "Run" tree in level2 file, if not existing return an error (sngl != 0) |
257 |
// |
258 |
sgnl = 0; |
259 |
sgnl = runinfo->Update(run, "TOF",tofversion); |
260 |
if ( sgnl ){ |
261 |
if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n"); |
262 |
code = sgnl; |
263 |
goto closeandexit; |
264 |
} else { |
265 |
sgnl = 0; |
266 |
}; |
267 |
// |
268 |
// number of events in the file BEFORE the first event of our run |
269 |
// |
270 |
nobefrun = runinfo->GetFirstEntry(); |
271 |
// |
272 |
// total number of events in the file |
273 |
// |
274 |
totfileentries = runinfo->GetFileEntries(); |
275 |
// |
276 |
// first file entry AFTER the last event of our run |
277 |
// |
278 |
noaftrun = runinfo->GetLastEntry() + 1; |
279 |
// |
280 |
// number of run to be processed |
281 |
// |
282 |
numbofrun = runinfo->GetNoRun(); |
283 |
UInt_t totnorun = runinfo->GetRunEntries(); |
284 |
// |
285 |
// Try to access the ToF tree in the file, if it exists we are reprocessing data if not we are processing a new run |
286 |
// |
287 |
toftclone = (TTree*)file->Get("ToF"); |
288 |
// |
289 |
if ( !toftclone ){ |
290 |
// |
291 |
// tree does not exist, we are not reprocessing |
292 |
// |
293 |
reproc = false; |
294 |
if ( run == 0 && verbose ) printf(" ToF - WARNING: you are reprocessing data but ToF tree does not exist!\n"); |
295 |
if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" ToF - WARNING: it seems you are not reprocessing data but ToF\n versioning information already exists in RunInfo.\n"); |
296 |
|
297 |
} else { |
298 |
// |
299 |
// tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? |
300 |
// |
301 |
toftclone->SetAutoSave(900000000000000LL); |
302 |
reproc = true; |
303 |
// |
304 |
// update versioning information |
305 |
// |
306 |
if ( verbose ) printf("\n Preparing the pre-processing...\n"); |
307 |
// |
308 |
if ( run == 0 || totnorun == 1 ){ |
309 |
// |
310 |
// we are reprocessing all the file |
311 |
// 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 |
312 |
// |
313 |
reprocall = true; |
314 |
// |
315 |
if ( verbose ) printf("\n ToF - WARNING: Reprocessing all runs\n"); |
316 |
// |
317 |
} else { |
318 |
// |
319 |
// 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 |
320 |
// |
321 |
reprocall = false; |
322 |
// |
323 |
if ( verbose ) printf("\n ToF - WARNING: Reprocessing run number %u \n",run); |
324 |
// |
325 |
// copying old tree to a new file |
326 |
// |
327 |
gSystem->MakeDirectory(toffolder.str().c_str()); |
328 |
myfold = true; |
329 |
tempfile = new TFile(tempname.str().c_str(),"RECREATE"); |
330 |
temptof = toftclone->CloneTree(-1,"fast"); |
331 |
temptof->SetName("ToF-old"); |
332 |
tempfile->Write(); |
333 |
tempfile->Close(); |
334 |
} |
335 |
// |
336 |
// Delete the old tree from old file and memory |
337 |
// |
338 |
toftclone->Delete("all"); |
339 |
// |
340 |
if ( verbose ) printf(" ...done!\n"); |
341 |
// |
342 |
}; |
343 |
// |
344 |
// create ToF detector tree toft |
345 |
// |
346 |
file->cd(); |
347 |
toft = new TTree("ToF-new","PAMELA Level2 ToF data"); |
348 |
toft->SetAutoSave(900000000000000LL); |
349 |
tof->Set();//ELENA **TEMPORANEO?** |
350 |
toft->Branch("ToFLevel2","ToFLevel2",&tof); |
351 |
// |
352 |
if ( reproc && !reprocall ){ |
353 |
// |
354 |
// open new file and retrieve all tree informations |
355 |
// |
356 |
tempfile = new TFile(tempname.str().c_str(),"READ"); |
357 |
toftclone = (TTree*)tempfile->Get("ToF-old"); |
358 |
toftclone->SetAutoSave(900000000000000LL); |
359 |
toftclone->SetBranchAddress("ToFLevel2",&tofclone); |
360 |
// |
361 |
if ( nobefrun > 0 ){ |
362 |
if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n"); |
363 |
if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run); |
364 |
if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); |
365 |
for (UInt_t j = 0; j < nobefrun; j++){ |
366 |
// |
367 |
toftclone->GetEntry(j); |
368 |
// |
369 |
// copy tofclone to tof |
370 |
// |
371 |
tof->Clear(); |
372 |
memcpy(&tof,&tofclone,sizeof(tofclone)); |
373 |
// |
374 |
// Fill entry in the new tree |
375 |
// |
376 |
toft->Fill(); |
377 |
// |
378 |
}; |
379 |
if ( verbose ) printf(" Finished successful copying!\n"); |
380 |
}; |
381 |
}; |
382 |
// |
383 |
// Get the list of run to be processed, if only one run has to be processed the list will contain one entry only. |
384 |
// |
385 |
runlist = runinfo->GetRunList(); |
386 |
// |
387 |
// Loop over the run to be processed |
388 |
// |
389 |
for (UInt_t irun=0; irun < numbofrun; irun++){ |
390 |
// |
391 |
// retrieve the first run ID to be processed using the RunInfo list |
392 |
// |
393 |
idRun = runlist->At(irun); |
394 |
if ( verbose ) printf("\n\n\n ####################################################################### \n"); |
395 |
if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun); |
396 |
if ( verbose ) printf(" ####################################################################### \n\n\n"); |
397 |
// |
398 |
runinfo->ID_ROOT_L0 = 0; |
399 |
// |
400 |
// store in the runinfo class the GL_RUN variables for our run |
401 |
// |
402 |
sgnl = 0; |
403 |
sgnl = runinfo->GetRunInfo(idRun); |
404 |
if ( sgnl ){ |
405 |
if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n"); |
406 |
code = sgnl; |
407 |
goto closeandexit; |
408 |
} else { |
409 |
sgnl = 0; |
410 |
}; |
411 |
// |
412 |
// now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0 |
413 |
// |
414 |
if ( runinfo->ID_ROOT_L0 == 0 ){ |
415 |
if ( verbose ) printf("\n TOF - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun); |
416 |
code = -5; |
417 |
goto closeandexit; |
418 |
}; |
419 |
// |
420 |
// prepare the timesync for the db |
421 |
// |
422 |
TString host = glt->CGetHost(); |
423 |
TString user = glt->CGetUser(); |
424 |
TString psw = glt->CGetPsw(); |
425 |
TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
426 |
if ( !dbc->IsConnected() ) throw -314; |
427 |
stringstream myquery; |
428 |
myquery.str(""); |
429 |
myquery << "SET time_zone='+0:00'"; |
430 |
dbc->Query(myquery.str().c_str()); |
431 |
dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc); |
432 |
// |
433 |
// Search in the DB the path and name of the LEVEL0 file to be processed. |
434 |
// |
435 |
// if ( !dbc->IsConnected() ) throw -314; |
436 |
glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc); |
437 |
// |
438 |
ftmpname.str(""); |
439 |
ftmpname << glroot->PATH.Data() << "/"; |
440 |
ftmpname << glroot->NAME.Data(); |
441 |
fname = ftmpname.str().c_str(); |
442 |
// |
443 |
// print out informations |
444 |
// |
445 |
totevent = runinfo->NEVENTS; |
446 |
if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data()); |
447 |
if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME); |
448 |
if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME); |
449 |
if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM,runinfo->EV_FROM+totevent); |
450 |
// |
451 |
// if ( !totevent ) goto closeandexit; |
452 |
// |
453 |
// Open Level0 file |
454 |
// |
455 |
l0File = new TFile(fname.Data()); |
456 |
if ( !l0File ) { |
457 |
if ( verbose ) printf(" TOF - ERROR: problems opening Level0 file\n"); |
458 |
code = -6; |
459 |
goto closeandexit; |
460 |
}; |
461 |
l0tr = (TTree*)l0File->Get("Physics"); |
462 |
if ( !l0tr ) { |
463 |
if ( verbose ) printf(" TOF - ERROR: no Physics tree in Level0 file\n"); |
464 |
l0File->Close(); |
465 |
code = -7; |
466 |
goto closeandexit; |
467 |
}; |
468 |
l0head = l0tr->GetBranch("Header"); |
469 |
if ( !l0head ) { |
470 |
if ( verbose ) printf(" TOF - ERROR: no Header branch in Level0 tree\n"); |
471 |
l0File->Close(); |
472 |
code = -8; |
473 |
goto closeandexit; |
474 |
}; |
475 |
l0trig = l0tr->GetBranch("Trigger"); |
476 |
if ( !l0trig ) { |
477 |
if ( verbose ) printf(" TOF - ERROR: no Trigger branch in Level0 tree\n"); |
478 |
l0File->Close(); |
479 |
code = -300; |
480 |
goto closeandexit; |
481 |
}; |
482 |
l0tof = l0tr->GetBranch("Tof"); |
483 |
if ( !l0tof ) { |
484 |
if ( verbose ) printf(" TOF - ERROR: no ToF branch in Level0 tree\n"); |
485 |
l0File->Close(); |
486 |
code = -303; |
487 |
goto closeandexit; |
488 |
}; |
489 |
// |
490 |
l0tr->SetBranchAddress("Trigger", &trig); |
491 |
l0tr->SetBranchAddress("Tof", &tofEvent); |
492 |
l0tr->SetBranchAddress("Header", &eh); |
493 |
// |
494 |
nevents = l0tof->GetEntries(); |
495 |
// |
496 |
if ( nevents < 1 && totevent ) { |
497 |
if ( verbose ) printf(" TOF - ERROR: Level0 file is empty\n\n"); |
498 |
l0File->Close(); |
499 |
code = -11; |
500 |
goto closeandexit; |
501 |
}; |
502 |
// |
503 |
if ( runinfo->EV_TO > nevents-1 && totevent ) { |
504 |
if ( verbose ) printf(" TOF - ERROR: too few entries in the registry tree\n"); |
505 |
l0File->Close(); |
506 |
code = -12; |
507 |
goto closeandexit; |
508 |
}; |
509 |
// |
510 |
// Check if we have to load parameter files (or calibration associated to runs and not to events) |
511 |
// |
512 |
// for example let's assume that we could have different magnetic field maps for different runs: |
513 |
// |
514 |
if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runinfo->RUNHEADER_TIME ) ){ |
515 |
trkpar1 = false; |
516 |
// read from DB infos about Magnetic filed maps |
517 |
// if ( !dbc->IsConnected() ) throw -314; |
518 |
glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc); // parameters stored in DB in GL_PRAM table |
519 |
tttrkpar1 = glparam->TO_TIME; |
520 |
// ---------------------------- |
521 |
// Read the magnetic field |
522 |
// ---------------------------- |
523 |
if ( verbose ) printf(" Reading magnetic field maps: \n"); |
524 |
trk->LoadField(glparam->PATH+glparam->NAME); |
525 |
if ( verbose ) printf("\n"); |
526 |
}; |
527 |
// |
528 |
// variable to save information about the tof calibration used |
529 |
// |
530 |
Bool_t defcal = true; |
531 |
// |
532 |
if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){ |
533 |
tofpar1 = false; |
534 |
// |
535 |
// if ( !dbc->IsConnected() ) throw -314; |
536 |
Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,201,dbc); // parameters stored in DB in GL_PRAM table |
537 |
if ( error<0 ) { |
538 |
code = error; |
539 |
goto closeandexit; |
540 |
}; |
541 |
// |
542 |
if ( verbose ) printf(" Reading ToF parameter file: %s \n",(glparam->PATH+glparam->NAME).Data()); |
543 |
// |
544 |
if ( (UInt_t)glparam->TO_TIME != (UInt_t)4294967295UL ) defcal = false; |
545 |
// |
546 |
tttofpar1 = glparam->TO_TIME; |
547 |
Int_t nlen = (Int_t)(glparam->PATH+glparam->NAME).Length(); |
548 |
rdtofcal((char *)(glparam->PATH+glparam->NAME).Data(),&nlen); |
549 |
// |
550 |
}; |
551 |
// |
552 |
// WM reading parameter file for dE/dx II order corrections |
553 |
// |
554 |
memset(dedx_corr_m,0,100*48*sizeof(Float_t)); |
555 |
memset(dedx_corr,0,48*sizeof(Float_t)); |
556 |
memset(mtime,0,100*sizeof(Double_t)); |
557 |
// |
558 |
// Query the DB to get the file |
559 |
// |
560 |
Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,203,dbc); // parameters stored in DB in GL_PRAM table |
561 |
if ( error<0 ) { |
562 |
code = error; |
563 |
goto closeandexit; |
564 |
}; |
565 |
// |
566 |
if ( verbose ) printf(" Reading ToF dE/dx II order correction parameter file: %s \n",(glparam->PATH+glparam->NAME).Data()); |
567 |
// |
568 |
ical=0; // counter set to zero if first-time reading |
569 |
//----------------------------------------------------------- |
570 |
// Here I read the dEdx_korr parameters |
571 |
//----------------------------------------------------------- |
572 |
jj=0; |
573 |
ifstream fin((glparam->PATH+glparam->NAME).Data()); |
574 |
UInt_t window = 200000; |
575 |
Bool_t first = true; |
576 |
Bool_t last = true; |
577 |
Float_t sdedx_corr_m[48]; |
578 |
memset(sdedx_corr_m,0,48*sizeof(Float_t)); |
579 |
Double_t stm = 0; |
580 |
while ( !fin.eof() ){ |
581 |
stm = tm; |
582 |
if ( jj > 0 ) memcpy(sdedx_corr_m,dedx_corr_m[jj-1],48*sizeof(Float_t)); |
583 |
fin>>t1>>tm>>t2; |
584 |
if ( verbose ) cout << setiosflags(ios::fixed) << setw(10) << setprecision(3) << tm << endl; |
585 |
if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last) ){ |
586 |
if ( first ){ |
587 |
mtime[jj]=stm; |
588 |
jj++; |
589 |
if ( jj >= 100 ){ |
590 |
code = -318; |
591 |
goto closeandexit; |
592 |
}; |
593 |
}; |
594 |
mtime[jj]=tm; |
595 |
}; |
596 |
for (ii=0; ii<48;ii++){ |
597 |
fin>>wj>>xmean1>>xwidth1; |
598 |
if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last) ){ |
599 |
if ( first ){ |
600 |
dedx_corr_m[jj-1][ii]=sdedx_corr_m[ii]; |
601 |
}; |
602 |
dedx_corr_m[jj][ii]=xmean1; |
603 |
}; |
604 |
}; |
605 |
if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last)){ |
606 |
if ( first ) first = false; |
607 |
if ( tm > (runinfo->RUNTRAILER_TIME+window) ) last = false; |
608 |
jj++; |
609 |
}; |
610 |
if ( jj >= 100 ){ |
611 |
code = -318; |
612 |
goto closeandexit; |
613 |
}; |
614 |
}; |
615 |
// |
616 |
fin.close(); |
617 |
Bool_t ff = false; |
618 |
while ( runinfo->RUNHEADER_TIME > mtime[ical] && ical < 100 ) { |
619 |
ical = ical+1; |
620 |
ff = true; |
621 |
}; |
622 |
if ( ff ) ical = ical-1; |
623 |
if ( verbose ) cout<<"rh time "<<runinfo->RUNHEADER_TIME<<" rt time "<<runinfo->RUNTRAILER_TIME<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<< " jj " << jj<< endl; |
624 |
if ( ical < 0 || ical >= 98 ){ |
625 |
code = -315; |
626 |
goto closeandexit; |
627 |
}; |
628 |
// |
629 |
// run over all the events of the run |
630 |
// |
631 |
if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n"); |
632 |
// |
633 |
if ( dbc ){ |
634 |
dbc->Close(); |
635 |
delete dbc; |
636 |
}; |
637 |
// |
638 |
jumped = 0; |
639 |
// |
640 |
for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){ |
641 |
// |
642 |
if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000); |
643 |
// |
644 |
l0head->GetEntry(re); |
645 |
// |
646 |
// absolute time of this event |
647 |
// |
648 |
ph = eh->GetPscuHeader(); |
649 |
atime = dbtime->DBabsTime(ph->GetOrbitalTime()); |
650 |
// |
651 |
tof->Clear(); |
652 |
Int_t pmt_id = 0; |
653 |
ToFPMT *t_pmt = new ToFPMT(); |
654 |
if(!(tof->PMT))tof->PMT = new TClonesArray("ToFPMT",12); //ELENA |
655 |
TClonesArray &tpmt = *tof->PMT; |
656 |
ToFTrkVar *t_tof = new ToFTrkVar(); |
657 |
if(!(tof->ToFTrk))tof->ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA |
658 |
TClonesArray &t = *tof->ToFTrk; |
659 |
// |
660 |
// paranoid check |
661 |
// |
662 |
if ( atime > (runinfo->RUNTRAILER_TIME+1) || atime < (runinfo->RUNHEADER_TIME-1) ) { |
663 |
if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n"); |
664 |
jumped++; |
665 |
goto jumpev; |
666 |
}; |
667 |
// |
668 |
// retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr" |
669 |
// |
670 |
if ( !reprocall ){ |
671 |
itr = nobefrun + (re - runinfo->EV_FROM -jumped); |
672 |
} else { |
673 |
itr = runinfo->GetFirstEntry() + (re - runinfo->EV_FROM -jumped); |
674 |
}; |
675 |
if ( itr > nevtrkl2 ){ // nevtrkl2 tracker entry number |
676 |
if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr); |
677 |
l0File->Close(); |
678 |
code = -313; |
679 |
goto closeandexit; |
680 |
}; |
681 |
// |
682 |
trk->Clear(); |
683 |
// |
684 |
tracker->GetEntry(itr); |
685 |
/// |
686 |
// |
687 |
l0tof->GetEntry(re); |
688 |
l0trig->GetEntry(re); |
689 |
/// |
690 |
// |
691 |
procev++; |
692 |
// |
693 |
// start processing |
694 |
// |
695 |
// dE/dx II order correction: check time limits and interpolate time correction |
696 |
//================================================================== |
697 |
//== if time is outside time limits: |
698 |
//================================================================== |
699 |
if ( atime<mtime[ical] || atime>mtime[ical+1] ){ |
700 |
if ( verbose ) cout<<"Checking Time Limits!"<<endl; |
701 |
ical=0; |
702 |
while ( atime > mtime[ical] && ical < 100 ){ |
703 |
ical = ical+1; |
704 |
} |
705 |
ical = ical-1; |
706 |
if ( ical < 0 || ical >= 98 ){ |
707 |
code = -317; |
708 |
goto closeandexit; |
709 |
}; |
710 |
if ( verbose ) cout<<"abs time "<<atime<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<<endl; |
711 |
}; |
712 |
//================================================================== |
713 |
//== interpolate betwen time limits |
714 |
//================================================================== |
715 |
thelp1 = mtime[ical]; |
716 |
thelp2 = mtime[ical+1]; |
717 |
for (ii=0; ii<48;ii++) { |
718 |
yhelp1 = dedx_corr_m[ical][ii]; |
719 |
yhelp2 = dedx_corr_m[ical+1][ii]; |
720 |
slope = (yhelp2-yhelp1)/(thelp2-thelp1); |
721 |
inter = yhelp1 - slope*thelp1; |
722 |
dedx_corr[ii] = slope*atime + inter; |
723 |
if ( fabs(dedx_corr[ii]) <= 1e-15 ){ |
724 |
if ( verbose ) printf("ii %i slope %f atime %u inter %f dedx_corr %f \n",ii,slope,atime,inter,dedx_corr[ii]); |
725 |
if ( verbose ) printf("ical %i yhelp2 %f yhelp1 %f thelp2 %f thelp1 %f \n",ical,yhelp2,yhelp1,thelp2,thelp1); |
726 |
code = -316; |
727 |
goto closeandexit; |
728 |
}; |
729 |
}; |
730 |
//================================================================ |
731 |
//================================================================ |
732 |
// |
733 |
// Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure |
734 |
// |
735 |
for (Int_t gg=0; gg<4;gg++){ |
736 |
for (Int_t hh=0; hh<12;hh++){ |
737 |
tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh]; |
738 |
tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh]; |
739 |
}; |
740 |
}; |
741 |
// |
742 |
for (Int_t hh=0; hh<5;hh++){ |
743 |
tofinput_.patterntrig[hh]=trig->patterntrig[hh]; |
744 |
}; |
745 |
// |
746 |
// Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables. |
747 |
// |
748 |
npmtentry = 0; |
749 |
// |
750 |
ntrkentry = 0; |
751 |
// |
752 |
// Calculate tracks informations from ToF alone |
753 |
// |
754 |
tofl2com(); |
755 |
// |
756 |
memcpy(tof->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t)); |
757 |
// |
758 |
t_tof->trkseqno = -1; |
759 |
// |
760 |
// and now we must copy from the output structure to the level2 class: |
761 |
// |
762 |
t_tof->npmttdc = 0; |
763 |
// |
764 |
for (Int_t hh=0; hh<12;hh++){ |
765 |
for (Int_t kk=0; kk<4;kk++){ |
766 |
if ( tofoutput_.tofmask[hh][kk] != 0 ){ |
767 |
pmt_id = tof->GetPMTid(kk,hh); |
768 |
t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc); |
769 |
t_tof->tdcflag.AddAt(tofoutput_.tdcflagtof[hh][kk],t_tof->npmttdc); // gf: Jan 09/07 |
770 |
t_tof->npmttdc++; |
771 |
}; |
772 |
}; |
773 |
}; |
774 |
for (Int_t kk=0; kk<13;kk++){ |
775 |
t_tof->beta[kk] = tofoutput_.betatof_a[kk]; |
776 |
} |
777 |
// |
778 |
t_tof->npmtadc = 0; |
779 |
for (Int_t hh=0; hh<12;hh++){ |
780 |
for (Int_t kk=0; kk<4;kk++){ |
781 |
if ( tofoutput_.adctof_c[hh][kk] < 1000 ){ |
782 |
pmt_id = tof->GetPMTid(kk,hh); |
783 |
t_tof->dedx.AddAt((tofoutput_.adctof_c[hh][kk]*4./dedx_corr[pmt_id]),t_tof->npmtadc); |
784 |
t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc); |
785 |
t_tof->adcflag.AddAt(tofoutput_.adcflagtof[hh][kk],t_tof->npmtadc); // gf: Jan 09/07 |
786 |
t_tof->npmtadc++; |
787 |
}; |
788 |
}; |
789 |
}; |
790 |
// |
791 |
memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos)); |
792 |
memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos)); |
793 |
memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof)); |
794 |
memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof)); |
795 |
// |
796 |
|
797 |
/* |
798 |
cout<<"ToFCore tofl2com"<<endl; |
799 |
cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl; |
800 |
cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl; |
801 |
cout<<tofoutput_.xtr_tof[0]<<" "<<tofoutput_.xtr_tof[1]<<" "<<tofoutput_.xtr_tof[2]<<" "<<tofoutput_.xtr_tof[3]<<" "<<tofoutput_.xtr_tof[4]<<" "<<tofoutput_.xtr_tof[5]<<endl; |
802 |
cout<<tofoutput_.ytr_tof[0]<<" "<<tofoutput_.ytr_tof[1]<<" "<<tofoutput_.ytr_tof[2]<<" "<<tofoutput_.ytr_tof[3]<<" "<<tofoutput_.ytr_tof[4]<<" "<<tofoutput_.ytr_tof[5]<<endl; |
803 |
*/ |
804 |
|
805 |
|
806 |
new(t[ntrkentry]) ToFTrkVar(*t_tof); |
807 |
ntrkentry++; |
808 |
t_tof->Clear(); |
809 |
// |
810 |
// |
811 |
// |
812 |
t_pmt->Clear(); |
813 |
// |
814 |
for (Int_t hh=0; hh<12;hh++){ |
815 |
for (Int_t kk=0; kk<4;kk++){ |
816 |
// new WM |
817 |
if ( tofoutput_.tdc_c[hh][kk] < 4095 || tofEvent->adc[kk][hh] < 4095 || tofEvent->tdc[kk][hh] < 4095 ){ |
818 |
// |
819 |
t_pmt->pmt_id = tof->GetPMTid(kk,hh); |
820 |
t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk]; |
821 |
t_pmt->adc = (Float_t)tofEvent->adc[kk][hh]; |
822 |
t_pmt->tdc = (Float_t)tofEvent->tdc[kk][hh]; |
823 |
// |
824 |
new(tpmt[npmtentry]) ToFPMT(*t_pmt); |
825 |
npmtentry++; |
826 |
t_pmt->Clear(); |
827 |
}; |
828 |
}; |
829 |
}; |
830 |
// |
831 |
|
832 |
// Calculate track-related variables |
833 |
// |
834 |
if ( trk->ntrk() > 0 ){ |
835 |
// |
836 |
// We have at least one track |
837 |
// |
838 |
// |
839 |
// Run over tracks |
840 |
// |
841 |
for(Int_t nt=0; nt < trk->ntrk(); nt++){ |
842 |
// |
843 |
TrkTrack *ptt = trk->GetStoredTrack(nt); |
844 |
// |
845 |
// Copy the alpha vector in the input structure |
846 |
// |
847 |
for (Int_t e = 0; e < 5 ; e++){ |
848 |
tofinput_.al_pp[e] = ptt->al[e]; |
849 |
}; |
850 |
// |
851 |
// Get tracker related variables for this track |
852 |
// |
853 |
toftrk(); |
854 |
// |
855 |
// Copy values in the class from the structure (we need to use a temporary class to store variables). |
856 |
// |
857 |
t_tof->npmttdc = 0; |
858 |
for (Int_t hh=0; hh<12;hh++){ |
859 |
for (Int_t kk=0; kk<4;kk++){ |
860 |
if ( tofoutput_.tofmask[hh][kk] != 0 ){ |
861 |
pmt_id = tof->GetPMTid(kk,hh); |
862 |
t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc); |
863 |
t_tof->tdcflag.AddAt(tofoutput_.tdcflag[hh][kk],t_tof->npmttdc); // gf: Jan 09/07 |
864 |
t_tof->npmttdc++; |
865 |
}; |
866 |
}; |
867 |
}; |
868 |
for (Int_t kk=0; kk<13;kk++){ |
869 |
t_tof->beta[kk] = tofoutput_.beta_a[kk]; |
870 |
}; |
871 |
// |
872 |
t_tof->npmtadc = 0; |
873 |
for (Int_t hh=0; hh<12;hh++){ |
874 |
for (Int_t kk=0; kk<4;kk++){ |
875 |
if ( tofoutput_.adc_c[hh][kk] < 1000 ){ |
876 |
pmt_id = tof->GetPMTid(kk,hh); |
877 |
t_tof->dedx.AddAt((tofoutput_.adc_c[hh][kk]*4./dedx_corr[pmt_id]),t_tof->npmtadc); |
878 |
t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc); |
879 |
t_tof->adcflag.AddAt(tofoutput_.adcflag[hh][kk],t_tof->npmtadc); // gf: Jan 09/07 |
880 |
t_tof->npmtadc++; |
881 |
}; |
882 |
}; |
883 |
}; |
884 |
// |
885 |
memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos)); |
886 |
memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos)); |
887 |
memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof)); |
888 |
memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof)); |
889 |
|
890 |
/* |
891 |
cout<<"ToFCore toftrk"<<endl; |
892 |
cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl; |
893 |
cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl; |
894 |
cout<<tofoutput_.xtr_tof[0]<<" "<<tofoutput_.xtr_tof[1]<<" "<<tofoutput_.xtr_tof[2]<<" "<<tofoutput_.xtr_tof[3]<<" "<<tofoutput_.xtr_tof[4]<<" "<<tofoutput_.xtr_tof[5]<<endl; |
895 |
cout<<tofoutput_.ytr_tof[0]<<" "<<tofoutput_.ytr_tof[1]<<" "<<tofoutput_.ytr_tof[2]<<" "<<tofoutput_.ytr_tof[3]<<" "<<tofoutput_.ytr_tof[4]<<" "<<tofoutput_.ytr_tof[5]<<endl; |
896 |
*/ |
897 |
|
898 |
// |
899 |
// Store the tracker track number in order to be sure to have shyncronized data during analysis |
900 |
// |
901 |
t_tof->trkseqno = nt; |
902 |
// |
903 |
// create a new object for this event with track-related variables |
904 |
// |
905 |
new(t[ntrkentry]) ToFTrkVar(*t_tof); |
906 |
ntrkentry++; |
907 |
t_tof->Clear(); |
908 |
// |
909 |
}; // loop on all the tracks |
910 |
}; |
911 |
// |
912 |
tof->unpackError = tofEvent->unpackError; |
913 |
if ( defcal ){ |
914 |
tof->default_calib = 1; |
915 |
} else { |
916 |
tof->default_calib = 0; |
917 |
}; |
918 |
// |
919 |
// Fill the rootple |
920 |
// |
921 |
toft->Fill(); |
922 |
// |
923 |
// |
924 |
// |
925 |
delete t_tof; |
926 |
// |
927 |
// |
928 |
// |
929 |
jumpev: |
930 |
debug = false; |
931 |
// |
932 |
}; |
933 |
// |
934 |
// Here you may want to clear some variables before processing another run |
935 |
// |
936 |
delete dbtime; |
937 |
}; // process all the runs |
938 |
// |
939 |
if ( verbose ) printf("\n Finished processing data \n"); |
940 |
// |
941 |
closeandexit: |
942 |
// |
943 |
// 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. |
944 |
// |
945 |
if ( !reprocall && reproc && code >= 0 ){ |
946 |
if ( totfileentries > noaftrun ){ |
947 |
if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n"); |
948 |
if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run); |
949 |
if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries); |
950 |
for (UInt_t j = noaftrun; j < totfileentries; j++ ){ |
951 |
// |
952 |
// Get entry from old tree |
953 |
// |
954 |
toftclone->GetEntry(j); |
955 |
// |
956 |
// copy tofclone to tof |
957 |
// |
958 |
tof->Clear(); |
959 |
memcpy(&tof,&tofclone,sizeof(tofclone)); |
960 |
// |
961 |
// Fill entry in the new tree |
962 |
// |
963 |
toft->Fill(); |
964 |
}; |
965 |
if ( verbose ) printf(" Finished successful copying!\n"); |
966 |
}; |
967 |
}; |
968 |
// |
969 |
// Close files, delete old tree(s), write and close level2 file |
970 |
// |
971 |
if ( l0File ) l0File->Close(); |
972 |
if ( tempfile ) tempfile->Close(); |
973 |
if ( myfold ) gSystem->Unlink(tempname.str().c_str()); |
974 |
if ( tracker ) tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file! |
975 |
// |
976 |
if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n"); |
977 |
if ( verbose ) printf("\n Writing and closing rootple\n"); |
978 |
if ( runinfo ) runinfo->Close(); |
979 |
if ( toft ) toft->SetName("ToF"); |
980 |
if ( file ){ |
981 |
file->cd(); |
982 |
file->Write("ToF"); |
983 |
}; |
984 |
// |
985 |
if ( myfold ) gSystem->Unlink(toffolder.str().c_str()); |
986 |
// |
987 |
// the end |
988 |
// |
989 |
if ( verbose ) printf("\n Exiting...\n"); |
990 |
if(toft)toft->Delete(); |
991 |
// |
992 |
if ( tof ) delete tof; |
993 |
if ( tofclone ) delete tofclone; |
994 |
if ( glroot ) delete glroot; |
995 |
if ( runinfo ) delete runinfo; |
996 |
// |
997 |
if ( code < 0 ) throw code; |
998 |
return(code); |
999 |
} |