/[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.46 - (hide annotations) (download)
Wed Nov 23 21:19:37 2011 UTC (13 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.45: +4 -2 lines
New ToF stuff, new IGRF handling and parameter files

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 mocchiut 1.26 };
704     //
705 mocchiut 1.7 jumped = 0;
706 mocchiut 1.6 //
707     for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
708 mocchiut 1.1 //
709     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
710     //
711 mocchiut 1.42 if ( l0head->GetEntry(re) <= 0 ) throw -36;
712 mocchiut 1.1 //
713     // absolute time of this event
714     //
715 mocchiut 1.6 ph = eh->GetPscuHeader();
716     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
717 mocchiut 1.4 //
718     tof->Clear();
719     Int_t pmt_id = 0;
720     ToFPMT *t_pmt = new ToFPMT();
721 mocchiut 1.15 if(!(tof->PMT))tof->PMT = new TClonesArray("ToFPMT",12); //ELENA
722 mocchiut 1.4 TClonesArray &tpmt = *tof->PMT;
723     ToFTrkVar *t_tof = new ToFTrkVar();
724 mocchiut 1.15 if(!(tof->ToFTrk))tof->ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
725 mocchiut 1.4 TClonesArray &t = *tof->ToFTrk;
726     //
727 mocchiut 1.1 // paranoid check
728     //
729 mocchiut 1.32 if ( atime > (runinfo->RUNTRAILER_TIME+1) || atime < (runinfo->RUNHEADER_TIME-1) ) {
730 mocchiut 1.1 if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n");
731 mocchiut 1.7 jumped++;
732 mocchiut 1.1 goto jumpev;
733     };
734     //
735     // retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr"
736     //
737     if ( !reprocall ){
738 mocchiut 1.7 itr = nobefrun + (re - runinfo->EV_FROM -jumped);
739 mocchiut 1.1 } else {
740 mocchiut 1.7 itr = runinfo->GetFirstEntry() + (re - runinfo->EV_FROM -jumped);
741 mocchiut 1.1 };
742     if ( itr > nevtrkl2 ){ // nevtrkl2 tracker entry number
743     if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
744     l0File->Close();
745     code = -313;
746     goto closeandexit;
747     };
748 mocchiut 1.8 //
749     trk->Clear();
750     //
751 mocchiut 1.42 if ( tracker->GetEntry(itr) <= 0 ) throw -36;
752 mocchiut 1.1 ///
753 mocchiut 1.2 //
754 mocchiut 1.42 if ( l0tof->GetEntry(re) <= 0 ) throw -36;
755     if ( l0trig->GetEntry(re) <= 0 ) throw -36;
756 mocchiut 1.1 ///
757     //
758     procev++;
759     //
760     // start processing
761     //
762 mocchiut 1.32 // dE/dx II order correction: check time limits and interpolate time correction
763     //==================================================================
764     //== if time is outside time limits:
765     //==================================================================
766     if ( atime<mtime[ical] || atime>mtime[ical+1] ){
767     if ( verbose ) cout<<"Checking Time Limits!"<<endl;
768     ical=0;
769 mocchiut 1.35 //Bool_t fg = false;
770     while ( ( mtime[ical] > atime || atime > mtime[ical+1]) && ical < 99 ){
771 mocchiut 1.32 ical = ical+1;
772 mocchiut 1.35 //fg = true;
773 mocchiut 1.32 }
774 mocchiut 1.35 // if ( fg ) ical = ical-1;
775 mocchiut 1.32 if ( ical < 0 || ical >= 98 ){
776     code = -317;
777     goto closeandexit;
778     };
779     if ( verbose ) cout<<"abs time "<<atime<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<<endl;
780     };
781     //==================================================================
782     //== interpolate betwen time limits
783     //==================================================================
784     thelp1 = mtime[ical];
785     thelp2 = mtime[ical+1];
786     for (ii=0; ii<48;ii++) {
787 mocchiut 1.39 yhelp1 = fabs(dedx_corr_m[ical][ii]);
788 mocchiut 1.43 if ( yhelp1 < 0.1 ) yhelp1 = 4.;
789 mocchiut 1.39 yhelp2 = fabs(dedx_corr_m[ical+1][ii]);
790 mocchiut 1.43 if ( yhelp2 < 0.1 ) yhelp2 = 4.;
791 mocchiut 1.32 slope = (yhelp2-yhelp1)/(thelp2-thelp1);
792     inter = yhelp1 - slope*thelp1;
793     dedx_corr[ii] = slope*atime + inter;
794 mocchiut 1.34 if ( fabs(dedx_corr[ii]) <= 1e-15 ){
795     if ( verbose ) printf("ii %i slope %f atime %u inter %f dedx_corr %f \n",ii,slope,atime,inter,dedx_corr[ii]);
796     if ( verbose ) printf("ical %i yhelp2 %f yhelp1 %f thelp2 %f thelp1 %f \n",ical,yhelp2,yhelp1,thelp2,thelp1);
797 mocchiut 1.32 code = -316;
798     goto closeandexit;
799     };
800     };
801     //================================================================
802     //================================================================
803 mocchiut 1.1 //
804     // Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure
805 mocchiut 1.32 //
806 mocchiut 1.1 for (Int_t gg=0; gg<4;gg++){
807     for (Int_t hh=0; hh<12;hh++){
808     tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];
809     tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh];
810     };
811     };
812 mocchiut 1.32 //
813 mocchiut 1.38 tofdedx->Init(tofEvent);
814     //
815 mocchiut 1.1 for (Int_t hh=0; hh<5;hh++){
816     tofinput_.patterntrig[hh]=trig->patterntrig[hh];
817     };
818     //
819     // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables.
820     //
821 mocchiut 1.4 npmtentry = 0;
822     //
823     ntrkentry = 0;
824 mocchiut 1.1 //
825     // Calculate tracks informations from ToF alone
826     //
827     tofl2com();
828     //
829 mocchiut 1.4 memcpy(tof->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t));
830     //
831     t_tof->trkseqno = -1;
832     //
833 mocchiut 1.1 // and now we must copy from the output structure to the level2 class:
834     //
835 mocchiut 1.4 t_tof->npmttdc = 0;
836     //
837 mocchiut 1.1 for (Int_t hh=0; hh<12;hh++){
838     for (Int_t kk=0; kk<4;kk++){
839 mocchiut 1.4 if ( tofoutput_.tofmask[hh][kk] != 0 ){
840     pmt_id = tof->GetPMTid(kk,hh);
841     t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
842 mocchiut 1.14 t_tof->tdcflag.AddAt(tofoutput_.tdcflagtof[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
843 mocchiut 1.4 t_tof->npmttdc++;
844     };
845     };
846     };
847     for (Int_t kk=0; kk<13;kk++){
848     t_tof->beta[kk] = tofoutput_.betatof_a[kk];
849 mocchiut 1.1 }
850 mocchiut 1.4 //
851 carbone 1.37
852     //
853     memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
854     memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
855     memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
856     memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
857     //
858     {
859    
860 mocchiut 1.40 Float_t xtof_temp[6]={100.,100.,100.,100.,100.,100.};
861     Float_t ytof_temp[6]={100.,100.,100.,100.,100.,100.};
862    
863     if(t_tof->xtofpos[0]<100. && t_tof->ytofpos[0]<100.){
864     xtof_temp[1]=t_tof->xtofpos[0];
865     ytof_temp[0]=t_tof->ytofpos[0];
866     }else if(t_tof->xtofpos[0]>=100. && t_tof->ytofpos[0]<100.){
867     ytof_temp[0]=t_tof->ytofpos[0];
868     tof->GetPaddleGeometry(0,(Int_t)log2(tof->tof_j_flag[0]),xleft, xright, yleft, yright);
869 mocchiut 1.41 xtof_temp[1]=xleft+2.55;
870 mocchiut 1.40 }else if(t_tof->ytofpos[0]>=100. && t_tof->xtofpos[0]<100.){
871     xtof_temp[1]=t_tof->xtofpos[0];
872     tof->GetPaddleGeometry(1,(Int_t)log2(tof->tof_j_flag[1]),xleft, xright, yleft, yright);
873 mocchiut 1.41 ytof_temp[0]=yleft+2.75;
874 mocchiut 1.40 }
875    
876     if(t_tof->xtofpos[1]<100. && t_tof->ytofpos[1]<100.){
877     xtof_temp[2]=t_tof->xtofpos[1];
878     ytof_temp[3]=t_tof->ytofpos[1];
879     }else if(t_tof->xtofpos[1]>=100. && t_tof->ytofpos[1]<100.){
880     ytof_temp[3]=t_tof->ytofpos[1];
881     tof->GetPaddleGeometry(3,(Int_t)log2(tof->tof_j_flag[3]),xleft, xright, yleft, yright);
882 mocchiut 1.41 xtof_temp[2]=xleft+4.5;
883 mocchiut 1.40 }else if(t_tof->ytofpos[1]>=100. && t_tof->xtofpos[1]<100.){
884     xtof_temp[2]=t_tof->xtofpos[1];
885     tof->GetPaddleGeometry(2,(Int_t)log2(tof->tof_j_flag[2]),xleft, xright, yleft, yright);
886 mocchiut 1.41 ytof_temp[3]=yleft+3.75;
887 mocchiut 1.40 }
888    
889     if(t_tof->xtofpos[2]<100. && t_tof->ytofpos[2]<100.){
890     xtof_temp[5]=t_tof->xtofpos[2];
891     ytof_temp[4]=t_tof->ytofpos[2];
892     }else if(t_tof->xtofpos[2]>=100. && t_tof->ytofpos[2]<100.){
893     ytof_temp[4]=t_tof->ytofpos[2];
894     tof->GetPaddleGeometry(4,(Int_t)log2(tof->tof_j_flag[4]),xleft, xright, yleft, yright);
895 mocchiut 1.41 xtof_temp[5]=xleft+3;
896 mocchiut 1.40 }else if(t_tof->ytofpos[2]>=100. && t_tof->xtofpos[2]<100.){
897     xtof_temp[5]=t_tof->xtofpos[2];
898     tof->GetPaddleGeometry(5,(Int_t)log2(tof->tof_j_flag[5]),xleft, xright, yleft, yright);
899 mocchiut 1.41 ytof_temp[4]=yleft+2.5;
900 mocchiut 1.40 }
901     // Float_t xtof_temp[6]={0.,t_tof->xtofpos[0],t_tof->xtofpos[1],0.,0.,t_tof->xtofpos[2]};
902     // Float_t ytof_temp[6]={t_tof->ytofpos[0],0.,0.,t_tof->ytofpos[1],t_tof->ytofpos[2],0.};
903     tofdedx->Process(atime,t_tof->beta[12], (Float_t *)xtof_temp,(Float_t *)ytof_temp);
904     t_tof->npmtadc = 0;
905     for (Int_t hh=0; hh<12;hh++){
906     for (Int_t kk=0; kk<4;kk++){
907 mocchiut 1.4 pmt_id = tof->GetPMTid(kk,hh);
908 mocchiut 1.40 Int_t Iplane=-1;
909     Int_t Ipaddle=-1;
910     // Int_t IpaddleT=-1;
911     tof->GetPMTPaddle(pmt_id, Iplane, Ipaddle);
912     tof->GetPaddleGeometry(Iplane,Ipaddle,xleft,xright,yleft,yright);
913     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 !!!!
914 mocchiut 1.43 t_tof->dedx.AddAt((tofdedx->GetdEdx_pmt(pmt_id)*4./dedx_corr[pmt_id]),t_tof->npmtadc);
915 mocchiut 1.40 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
916     t_tof->adcflag.AddAt(0,t_tof->npmtadc); // gf: Jan 09/07
917     t_tof->npmtadc++;
918     };
919 mocchiut 1.4 };
920     };
921     };
922 mocchiut 1.40
923 mocchiut 1.20 /*
924     cout<<"ToFCore tofl2com"<<endl;
925     cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
926     cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
927     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;
928     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;
929     */
930    
931    
932 mocchiut 1.4 new(t[ntrkentry]) ToFTrkVar(*t_tof);
933     ntrkentry++;
934     t_tof->Clear();
935     //
936 mocchiut 1.1 //
937     //
938 mocchiut 1.4 t_pmt->Clear();
939 mocchiut 1.1 //
940 mocchiut 1.4 for (Int_t hh=0; hh<12;hh++){
941     for (Int_t kk=0; kk<4;kk++){
942 mocchiut 1.29 // new WM
943     if ( tofoutput_.tdc_c[hh][kk] < 4095 || tofEvent->adc[kk][hh] < 4095 || tofEvent->tdc[kk][hh] < 4095 ){
944 mocchiut 1.4 //
945     t_pmt->pmt_id = tof->GetPMTid(kk,hh);
946     t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk];
947 mocchiut 1.28 t_pmt->adc = (Float_t)tofEvent->adc[kk][hh];
948     t_pmt->tdc = (Float_t)tofEvent->tdc[kk][hh];
949 mocchiut 1.29 //
950 mocchiut 1.4 new(tpmt[npmtentry]) ToFPMT(*t_pmt);
951     npmtentry++;
952     t_pmt->Clear();
953     };
954     };
955     };
956 mocchiut 1.1 //
957 mocchiut 1.38 if ( debug ) printf(" ATIME %u re %u \n",atime,(UInt_t)re);
958 mocchiut 1.4 // Calculate track-related variables
959 mocchiut 1.1 //
960 mocchiut 1.2 if ( trk->ntrk() > 0 ){
961 mocchiut 1.1 //
962     // We have at least one track
963     //
964     //
965     // Run over tracks
966     //
967     for(Int_t nt=0; nt < trk->ntrk(); nt++){
968     //
969     TrkTrack *ptt = trk->GetStoredTrack(nt);
970     //
971     // Copy the alpha vector in the input structure
972     //
973     for (Int_t e = 0; e < 5 ; e++){
974     tofinput_.al_pp[e] = ptt->al[e];
975     };
976 mocchiut 1.45
977     // new input for 9th reduction: tracker dEdx
978     tofinput_.trkmip = ptt->GetDEDX();
979    
980 mocchiut 1.1 //
981     // Get tracker related variables for this track
982     //
983     toftrk();
984     //
985     // Copy values in the class from the structure (we need to use a temporary class to store variables).
986     //
987 mocchiut 1.4 t_tof->npmttdc = 0;
988     for (Int_t hh=0; hh<12;hh++){
989     for (Int_t kk=0; kk<4;kk++){
990     if ( tofoutput_.tofmask[hh][kk] != 0 ){
991     pmt_id = tof->GetPMTid(kk,hh);
992     t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
993 mocchiut 1.14 t_tof->tdcflag.AddAt(tofoutput_.tdcflag[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
994 mocchiut 1.4 t_tof->npmttdc++;
995     };
996     };
997     };
998 mocchiut 1.1 for (Int_t kk=0; kk<13;kk++){
999 mocchiut 1.4 t_tof->beta[kk] = tofoutput_.beta_a[kk];
1000     };
1001 carbone 1.37 memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
1002     memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
1003     memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
1004     memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
1005 mocchiut 1.4 //
1006 mocchiut 1.38 tofdedx->Process(atime,t_tof->beta[12], (Float_t *)t_tof->xtr_tof,(Float_t *)t_tof->ytr_tof);
1007 mocchiut 1.4 t_tof->npmtadc = 0;
1008 mocchiut 1.44 // for (Int_t rrtt = 0; rrtt<48; rrtt++){
1009     // printf(" ===>>> %i ===> %f \n",rrtt,tofdedx->GetdEdx_pmt(rrtt));
1010     // };
1011 mocchiut 1.1 for (Int_t hh=0; hh<12;hh++){
1012     for (Int_t kk=0; kk<4;kk++){
1013 carbone 1.37 pmt_id = tof->GetPMTid(kk,hh);
1014 mocchiut 1.40 Int_t Iplane=-1;
1015     Int_t Ipaddle=-1;
1016     Int_t IpaddleT=-1;
1017     tof->GetPMTPaddle(pmt_id, Iplane, Ipaddle);
1018 mocchiut 1.44 IpaddleT=tof->GetPaddleIdOfTrack(t_tof->xtr_tof[Iplane],t_tof->ytr_tof[Iplane], Iplane,0.0);
1019     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]);
1020 mocchiut 1.40 if ( tofdedx->GetdEdx_pmt(pmt_id) > -1. && Ipaddle==IpaddleT ){
1021 mocchiut 1.43 t_tof->dedx.AddAt((tofdedx->GetdEdx_pmt(pmt_id)*4./dedx_corr[pmt_id]),t_tof->npmtadc);
1022 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]);
1023 mocchiut 1.4 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
1024 carbone 1.37 t_tof->adcflag.AddAt(0,t_tof->npmtadc); // gf: Jan 09/07
1025 mocchiut 1.4 t_tof->npmtadc++;
1026     };
1027 carbone 1.37
1028 mocchiut 1.4 };
1029     };
1030     //
1031 mocchiut 1.20
1032     /*
1033     cout<<"ToFCore toftrk"<<endl;
1034     cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
1035     cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
1036     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;
1037     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;
1038     */
1039    
1040 mocchiut 1.1 //
1041     // Store the tracker track number in order to be sure to have shyncronized data during analysis
1042     //
1043     t_tof->trkseqno = nt;
1044     //
1045     // create a new object for this event with track-related variables
1046     //
1047     new(t[ntrkentry]) ToFTrkVar(*t_tof);
1048     ntrkentry++;
1049 mocchiut 1.4 t_tof->Clear();
1050 mocchiut 1.1 //
1051     }; // loop on all the tracks
1052     };
1053     //
1054 mocchiut 1.9 tof->unpackError = tofEvent->unpackError;
1055 mocchiut 1.22 if ( defcal ){
1056     tof->default_calib = 1;
1057     } else {
1058     tof->default_calib = 0;
1059     };
1060 mocchiut 1.9 //
1061 mocchiut 1.1 // Fill the rootple
1062     //
1063     toft->Fill();
1064     //
1065     //
1066 mocchiut 1.4 //
1067     delete t_tof;
1068     //
1069     //
1070     //
1071 mocchiut 1.1 jumpev:
1072 mocchiut 1.38 if ( !debug ) debug = false;
1073 mocchiut 1.1 //
1074     };
1075 mocchiut 1.6 //
1076     // Here you may want to clear some variables before processing another run
1077     //
1078     delete dbtime;
1079 mocchiut 1.1 }; // process all the runs
1080     //
1081     if ( verbose ) printf("\n Finished processing data \n");
1082     //
1083     closeandexit:
1084     //
1085     // 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.
1086     //
1087     if ( !reprocall && reproc && code >= 0 ){
1088     if ( totfileentries > noaftrun ){
1089     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1090     if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
1091     if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
1092     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1093     //
1094     // Get entry from old tree
1095     //
1096 mocchiut 1.42 if ( toftclone->GetEntry(j) <= 0 ) throw -36;
1097 mocchiut 1.1 //
1098     // copy tofclone to tof
1099     //
1100 mocchiut 1.3 tof->Clear();
1101 mocchiut 1.1 memcpy(&tof,&tofclone,sizeof(tofclone));
1102     //
1103     // Fill entry in the new tree
1104     //
1105     toft->Fill();
1106     };
1107     if ( verbose ) printf(" Finished successful copying!\n");
1108     };
1109     };
1110     //
1111     // Close files, delete old tree(s), write and close level2 file
1112     //
1113     if ( l0File ) l0File->Close();
1114     if ( tempfile ) tempfile->Close();
1115 mocchiut 1.25 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1116 mocchiut 1.1 if ( tracker ) tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file!
1117     //
1118     if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n");
1119     if ( verbose ) printf("\n Writing and closing rootple\n");
1120     if ( runinfo ) runinfo->Close();
1121     if ( toft ) toft->SetName("ToF");
1122     if ( file ){
1123     file->cd();
1124     file->Write("ToF");
1125     };
1126     //
1127 mocchiut 1.25 if ( myfold ) gSystem->Unlink(toffolder.str().c_str());
1128 mocchiut 1.1 //
1129     // the end
1130     //
1131     if ( verbose ) printf("\n Exiting...\n");
1132     if(toft)toft->Delete();
1133 mocchiut 1.5 //
1134     if ( tof ) delete tof;
1135     if ( tofclone ) delete tofclone;
1136 carbone 1.37 if ( tofdedx ) delete tofdedx;
1137 mocchiut 1.5 if ( glroot ) delete glroot;
1138     if ( runinfo ) delete runinfo;
1139     //
1140 mocchiut 1.1 if ( code < 0 ) throw code;
1141     return(code);
1142     }

  ViewVC Help
Powered by ViewVC 1.1.23