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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (hide annotations) (download)
Tue Nov 29 15:20:16 2011 UTC (13 years, 1 month ago) by pam-fi
Branch: MAIN
Changes since 1.46: +1 -0 lines
Some fixes to prevent memory leaks or double delete.

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

  ViewVC Help
Powered by ViewVC 1.1.23