/[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.36 - (hide annotations) (download)
Tue Aug 4 14:01:31 2009 UTC (15 years, 4 months ago) by mocchiut
Branch: MAIN
Changes since 1.35: +2 -1 lines
Changed to work with GCC 4.x (gfortran) + ROOT >= 5.24

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

  ViewVC Help
Powered by ViewVC 1.1.23