/[PAMELA software]/DarthVader/ToFLevel2/src/ToFCore.cpp
ViewVC logotype

Contents of /DarthVader/ToFLevel2/src/ToFCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.32 - (show annotations) (download)
Fri Nov 28 09:05:24 2008 UTC (16 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.31: +105 -5 lines
Event loss due to timesync bug fixed + ToF dE/dx II order correction implemented

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 while ( !fin.eof() ){
575 fin>>t1>>tm>>t2;
576 if ( verbose ) cout << setiosflags(ios::fixed) << setw(10) << setprecision(3) << tm << endl;
577 if ( tm >= (runinfo->RUNHEADER_TIME-100000) && tm <= (runinfo->RUNTRAILER_TIME+100000) ){
578 mtime[jj]=tm;
579 };
580 for (ii=0; ii<48;ii++){
581 fin>>wj>>xmean1>>xwidth1;
582 if ( tm >= (runinfo->RUNHEADER_TIME-100000) && tm <= (runinfo->RUNTRAILER_TIME+100000) ){
583 dedx_corr_m[jj][ii]=xmean1;
584 };
585 };
586 if ( tm >= (runinfo->RUNHEADER_TIME-100000) && tm <= (runinfo->RUNTRAILER_TIME+100000) ){
587 jj++;
588 };
589 if ( jj >= 100 ){
590 code = -318;
591 goto closeandexit;
592 };
593 };
594 //
595 fin.close();
596 Bool_t ff = false;
597 while ( runinfo->RUNHEADER_TIME > mtime[ical] && ical < 100 ) {
598 ical = ical+1;
599 ff = true;
600 };
601 if ( ff ) ical = ical-1;
602 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;
603 if ( ical < 0 || ical >= 98 ){
604 code = -315;
605 goto closeandexit;
606 };
607 //
608 // run over all the events of the run
609 //
610 if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
611 //
612 if ( dbc ){
613 dbc->Close();
614 delete dbc;
615 };
616 //
617 jumped = 0;
618 //
619 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
620 //
621 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
622 //
623 l0head->GetEntry(re);
624 //
625 // absolute time of this event
626 //
627 ph = eh->GetPscuHeader();
628 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
629 //
630 tof->Clear();
631 Int_t pmt_id = 0;
632 ToFPMT *t_pmt = new ToFPMT();
633 if(!(tof->PMT))tof->PMT = new TClonesArray("ToFPMT",12); //ELENA
634 TClonesArray &tpmt = *tof->PMT;
635 ToFTrkVar *t_tof = new ToFTrkVar();
636 if(!(tof->ToFTrk))tof->ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
637 TClonesArray &t = *tof->ToFTrk;
638 //
639 // paranoid check
640 //
641 if ( atime > (runinfo->RUNTRAILER_TIME+1) || atime < (runinfo->RUNHEADER_TIME-1) ) {
642 if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n");
643 jumped++;
644 goto jumpev;
645 };
646 //
647 // retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr"
648 //
649 if ( !reprocall ){
650 itr = nobefrun + (re - runinfo->EV_FROM -jumped);
651 } else {
652 itr = runinfo->GetFirstEntry() + (re - runinfo->EV_FROM -jumped);
653 };
654 if ( itr > nevtrkl2 ){ // nevtrkl2 tracker entry number
655 if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
656 l0File->Close();
657 code = -313;
658 goto closeandexit;
659 };
660 //
661 trk->Clear();
662 //
663 tracker->GetEntry(itr);
664 ///
665 //
666 l0tof->GetEntry(re);
667 l0trig->GetEntry(re);
668 ///
669 //
670 procev++;
671 //
672 // start processing
673 //
674 // dE/dx II order correction: check time limits and interpolate time correction
675 //==================================================================
676 //== if time is outside time limits:
677 //==================================================================
678 if ( atime<mtime[ical] || atime>mtime[ical+1] ){
679 if ( verbose ) cout<<"Checking Time Limits!"<<endl;
680 ical=0;
681 while ( atime > mtime[ical] && ical < 100 ){
682 ical = ical+1;
683 }
684 ical = ical-1;
685 if ( ical < 0 || ical >= 98 ){
686 code = -317;
687 goto closeandexit;
688 };
689 if ( verbose ) cout<<"abs time "<<atime<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<<endl;
690 };
691 //==================================================================
692 //== interpolate betwen time limits
693 //==================================================================
694 thelp1 = mtime[ical];
695 thelp2 = mtime[ical+1];
696 for (ii=0; ii<48;ii++) {
697 yhelp1 = dedx_corr_m[ical][ii];
698 yhelp2 = dedx_corr_m[ical+1][ii];
699 slope = (yhelp2-yhelp1)/(thelp2-thelp1);
700 inter = yhelp1 - slope*thelp1;
701 dedx_corr[ii] = slope*atime + inter;
702 if ( fabs(dedx_corr[ii]) <= 0.0000001 ){
703 code = -316;
704 goto closeandexit;
705 };
706 };
707 //================================================================
708 //================================================================
709 //
710 // Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure
711 //
712 for (Int_t gg=0; gg<4;gg++){
713 for (Int_t hh=0; hh<12;hh++){
714 tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];
715 tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh];
716 };
717 };
718 //
719 for (Int_t hh=0; hh<5;hh++){
720 tofinput_.patterntrig[hh]=trig->patterntrig[hh];
721 };
722 //
723 // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables.
724 //
725 npmtentry = 0;
726 //
727 ntrkentry = 0;
728 //
729 // Calculate tracks informations from ToF alone
730 //
731 tofl2com();
732 //
733 memcpy(tof->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t));
734 //
735 t_tof->trkseqno = -1;
736 //
737 // and now we must copy from the output structure to the level2 class:
738 //
739 t_tof->npmttdc = 0;
740 //
741 for (Int_t hh=0; hh<12;hh++){
742 for (Int_t kk=0; kk<4;kk++){
743 if ( tofoutput_.tofmask[hh][kk] != 0 ){
744 pmt_id = tof->GetPMTid(kk,hh);
745 t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
746 t_tof->tdcflag.AddAt(tofoutput_.tdcflagtof[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
747 t_tof->npmttdc++;
748 };
749 };
750 };
751 for (Int_t kk=0; kk<13;kk++){
752 t_tof->beta[kk] = tofoutput_.betatof_a[kk];
753 }
754 //
755 t_tof->npmtadc = 0;
756 for (Int_t hh=0; hh<12;hh++){
757 for (Int_t kk=0; kk<4;kk++){
758 if ( tofoutput_.adctof_c[hh][kk] < 1000 ){
759 pmt_id = tof->GetPMTid(kk,hh);
760 t_tof->dedx.AddAt((tofoutput_.adctof_c[hh][kk]*4./dedx_corr[pmt_id]),t_tof->npmtadc);
761 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
762 t_tof->adcflag.AddAt(tofoutput_.adcflagtof[hh][kk],t_tof->npmtadc); // gf: Jan 09/07
763 t_tof->npmtadc++;
764 };
765 };
766 };
767 //
768 memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
769 memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
770 memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
771 memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
772 //
773
774 /*
775 cout<<"ToFCore tofl2com"<<endl;
776 cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
777 cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
778 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;
779 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;
780 */
781
782
783 new(t[ntrkentry]) ToFTrkVar(*t_tof);
784 ntrkentry++;
785 t_tof->Clear();
786 //
787 //
788 //
789 t_pmt->Clear();
790 //
791 for (Int_t hh=0; hh<12;hh++){
792 for (Int_t kk=0; kk<4;kk++){
793 // new WM
794 if ( tofoutput_.tdc_c[hh][kk] < 4095 || tofEvent->adc[kk][hh] < 4095 || tofEvent->tdc[kk][hh] < 4095 ){
795 //
796 t_pmt->pmt_id = tof->GetPMTid(kk,hh);
797 t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk];
798 t_pmt->adc = (Float_t)tofEvent->adc[kk][hh];
799 t_pmt->tdc = (Float_t)tofEvent->tdc[kk][hh];
800 //
801 new(tpmt[npmtentry]) ToFPMT(*t_pmt);
802 npmtentry++;
803 t_pmt->Clear();
804 };
805 };
806 };
807 //
808
809 // Calculate track-related variables
810 //
811 if ( trk->ntrk() > 0 ){
812 //
813 // We have at least one track
814 //
815 //
816 // Run over tracks
817 //
818 for(Int_t nt=0; nt < trk->ntrk(); nt++){
819 //
820 TrkTrack *ptt = trk->GetStoredTrack(nt);
821 //
822 // Copy the alpha vector in the input structure
823 //
824 for (Int_t e = 0; e < 5 ; e++){
825 tofinput_.al_pp[e] = ptt->al[e];
826 };
827 //
828 // Get tracker related variables for this track
829 //
830 toftrk();
831 //
832 // Copy values in the class from the structure (we need to use a temporary class to store variables).
833 //
834 t_tof->npmttdc = 0;
835 for (Int_t hh=0; hh<12;hh++){
836 for (Int_t kk=0; kk<4;kk++){
837 if ( tofoutput_.tofmask[hh][kk] != 0 ){
838 pmt_id = tof->GetPMTid(kk,hh);
839 t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
840 t_tof->tdcflag.AddAt(tofoutput_.tdcflag[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
841 t_tof->npmttdc++;
842 };
843 };
844 };
845 for (Int_t kk=0; kk<13;kk++){
846 t_tof->beta[kk] = tofoutput_.beta_a[kk];
847 };
848 //
849 t_tof->npmtadc = 0;
850 for (Int_t hh=0; hh<12;hh++){
851 for (Int_t kk=0; kk<4;kk++){
852 if ( tofoutput_.adc_c[hh][kk] < 1000 ){
853 pmt_id = tof->GetPMTid(kk,hh);
854 t_tof->dedx.AddAt((tofoutput_.adc_c[hh][kk]*4./dedx_corr[pmt_id]),t_tof->npmtadc);
855 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
856 t_tof->adcflag.AddAt(tofoutput_.adcflag[hh][kk],t_tof->npmtadc); // gf: Jan 09/07
857 t_tof->npmtadc++;
858 };
859 };
860 };
861 //
862 memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
863 memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
864 memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
865 memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
866
867 /*
868 cout<<"ToFCore toftrk"<<endl;
869 cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
870 cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
871 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;
872 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;
873 */
874
875 //
876 // Store the tracker track number in order to be sure to have shyncronized data during analysis
877 //
878 t_tof->trkseqno = nt;
879 //
880 // create a new object for this event with track-related variables
881 //
882 new(t[ntrkentry]) ToFTrkVar(*t_tof);
883 ntrkentry++;
884 t_tof->Clear();
885 //
886 }; // loop on all the tracks
887 };
888 //
889 tof->unpackError = tofEvent->unpackError;
890 if ( defcal ){
891 tof->default_calib = 1;
892 } else {
893 tof->default_calib = 0;
894 };
895 //
896 // Fill the rootple
897 //
898 toft->Fill();
899 //
900 //
901 //
902 delete t_tof;
903 //
904 //
905 //
906 jumpev:
907 debug = false;
908 //
909 };
910 //
911 // Here you may want to clear some variables before processing another run
912 //
913 delete dbtime;
914 }; // process all the runs
915 //
916 if ( verbose ) printf("\n Finished processing data \n");
917 //
918 closeandexit:
919 //
920 // 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.
921 //
922 if ( !reprocall && reproc && code >= 0 ){
923 if ( totfileentries > noaftrun ){
924 if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
925 if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
926 if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
927 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
928 //
929 // Get entry from old tree
930 //
931 toftclone->GetEntry(j);
932 //
933 // copy tofclone to tof
934 //
935 tof->Clear();
936 memcpy(&tof,&tofclone,sizeof(tofclone));
937 //
938 // Fill entry in the new tree
939 //
940 toft->Fill();
941 };
942 if ( verbose ) printf(" Finished successful copying!\n");
943 };
944 };
945 //
946 // Close files, delete old tree(s), write and close level2 file
947 //
948 if ( l0File ) l0File->Close();
949 if ( tempfile ) tempfile->Close();
950 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
951 if ( tracker ) tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file!
952 //
953 if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n");
954 if ( verbose ) printf("\n Writing and closing rootple\n");
955 if ( runinfo ) runinfo->Close();
956 if ( toft ) toft->SetName("ToF");
957 if ( file ){
958 file->cd();
959 file->Write("ToF");
960 };
961 //
962 if ( myfold ) gSystem->Unlink(toffolder.str().c_str());
963 //
964 // the end
965 //
966 if ( verbose ) printf("\n Exiting...\n");
967 if(toft)toft->Delete();
968 //
969 if ( tof ) delete tof;
970 if ( tofclone ) delete tofclone;
971 if ( glroot ) delete glroot;
972 if ( runinfo ) delete runinfo;
973 //
974 if ( code < 0 ) throw code;
975 return(code);
976 }

  ViewVC Help
Powered by ViewVC 1.1.23