/[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.48 - (hide annotations) (download)
Thu Apr 12 12:27:09 2012 UTC (12 years, 8 months ago) by mocchiut
Branch: MAIN
Changes since 1.47: +156 -43 lines
New TrigLevel2, ToFLevel2, OrbitalInfo classes, new order in DV processing (trigger first), ToF bugs fixed, new tof calibration, new variables in ToF and OrbitalInfo

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

  ViewVC Help
Powered by ViewVC 1.1.23