/[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.35 - (hide annotations) (download)
Wed Feb 4 16:50:23 2009 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.34: +17 -9 lines
II order dE/dx correction bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23