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

  ViewVC Help
Powered by ViewVC 1.1.23