/[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.51 - (hide annotations) (download)
Wed Nov 6 13:22:09 2013 UTC (11 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.50: +510 -479 lines
New code for retracking + small bug on run reprocessing fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23