/[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.5 - (hide annotations) (download)
Fri Aug 4 10:31:32 2006 UTC (18 years, 4 months ago) by mocchiut
Branch: MAIN
Changes since 1.4: +6 -0 lines
Small memory leaks fixed

1 mocchiut 1.1 //
2     // C/C++ headers
3     //
4     #include <fstream>
5     #include <string.h>
6     //
7     // ROOT headers
8     //
9     #include <TTree.h>
10     #include <TClassEdit.h>
11     #include <TObject.h>
12     #include <TList.h>
13     #include <TArrayL.h>
14     #include <TSystem.h>
15     #include <TSystemDirectory.h>
16     #include <TString.h>
17     #include <TFile.h>
18     #include <TClass.h>
19     #include <TCanvas.h>
20     #include <TH1.h>
21     #include <TH1F.h>
22     #include <TH2D.h>
23     #include <TLatex.h>
24     #include <TPad.h>
25     #include <TSQLServer.h>
26     #include <TSQLRow.h>
27     #include <TSQLResult.h>
28     #include <TClonesArray.h>
29     //
30     // YODA headers
31     //
32     #include <PamelaRun.h>
33     #include <RegistryEvent.h>
34     #include <physics/trigger/TriggerEvent.h>
35     #include <physics/tof/TofEvent.h>
36     //
37     // RunInfo header
38     //
39     #include <RunInfo.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     extern "C" int rdtofcal(char []);
56    
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     int ToFCore(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t ToFargc, char *ToFargv[]){
69     //
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     TString processFolder = "ToFFolder";
77     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     if ( !strcmp(ToFargv[i],"-v") || !strcmp(ToFargv[i],"--verbose") ) {
88     verbose = true;
89     };
90     if ( !strcmp(ToFargv[i],"-g") || !strcmp(ToFargv[i],"--debug") ) {
91     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     TTree *toft = 0;
106     Long64_t nevents = 0LL;
107     //
108     // variables needed to reprocess data
109     //
110     TString tofversion;
111     ItoRunInfo *runinfo = 0;
112     TArrayL *runlist = 0;
113     TTree *toftclone = 0;
114     Bool_t reproc = false;
115     Bool_t reprocall = false;
116     UInt_t nobefrun = 0;
117     UInt_t noaftrun = 0;
118     UInt_t numbofrun = 0;
119     stringstream ftmpname;
120     TString fname;
121     Long64_t totfileentries = 0ULL;
122     Long64_t idRun = 0LL;
123     //
124     // variables needed to handle error signals
125     //
126     Int_t code = 0;
127     Int_t sgnl;
128     //
129     // tof level2 classes
130     //
131     ToFLevel2 *tof = new ToFLevel2();
132     ToFLevel2 *tofclone = new ToFLevel2();
133     //
134     // tracker level2 variables
135     //
136     TrkLevel2 *trk = new TrkLevel2();
137     Long64_t nevtrkl2 = 0;
138     //
139     // define variables for opening and reading level0 file
140     //
141     TFile *l0File = 0;
142     TTree *l0tr = 0;
143     TBranch *l0head = 0;
144     TBranch *l0registry = 0;
145     TBranch *l0trig = 0;
146     TBranch *l0tof = 0;
147     pamela::RegistryEvent *l0reg=0;
148     pamela::trigger::TriggerEvent *trig = 0;
149     pamela::tof::TofEvent *tofEvent = 0;
150     //
151     // Define other basic variables
152     //
153     UInt_t procev = 0;
154     stringstream file2;
155     stringstream file3;
156     stringstream qy;
157     Int_t itr = -1;
158     Int_t totevent = 0;
159     ULong64_t atime = 0ULL;
160     Int_t ei = 0;
161     Int_t re = 0;
162     //
163     // Working filename
164     //
165     TString outputfile;
166     stringstream name;
167     name.str("");
168     name << outdir << "/";
169     //
170     // temporary file and folder
171     //
172     TFile *tempfile = 0;
173     TTree *temptof = 0;
174     stringstream tempname;
175     stringstream toffolder;
176     tempname.str("");
177     tempname << outdir;
178     tempname << "/" << processFolder.Data();
179     toffolder.str("");
180     toffolder << tempname.str().c_str();
181     gSystem->MakeDirectory(toffolder.str().c_str());
182     tempname << "/toftree_run";
183     tempname << run << ".root";
184     //
185     // variables needed to load magnetic field maps
186     //
187     Int_t ntrkentry = 0;
188 mocchiut 1.4 Int_t npmtentry = 0;
189 mocchiut 1.1 ULong64_t tttrkpar1 = 0ULL;
190     Bool_t trkpar1 = true;
191     ULong64_t tttofpar1 = 0ULL;
192     Bool_t tofpar1 = true;
193     //
194     // DB classes
195     //
196     GL_ROOT *glroot = new GL_ROOT();
197     GL_PARAM *glparam = new GL_PARAM();
198     //
199     // declaring external output and input structures
200     //
201     extern struct ToFInput tofinput_;
202     extern struct ToFOutput tofoutput_;
203     //
204     // Let's start!
205     //
206     //
207     // 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
208     // if run != 0 we must process only that run but first we have to check if the tree ToF already exist in the file
209     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
210     //
211     if ( run == 0ULL ) reproc = true;
212     //
213     //
214     // Output file is "outputfile"
215     //
216     if ( !file->IsOpen() ){
217     if ( verbose ) printf(" ToF - ERROR: cannot open file for writing\n");
218     throw -301;
219     };
220     //
221     // Does it contain the Tracker tree?
222     //
223     tracker = (TTree*)file->Get("Tracker");
224     if ( !tracker ) {
225     if ( verbose ) printf(" TOF - ERROR: no tracker tree\n");
226     code = -302;
227     goto closeandexit;
228     };
229     //
230     // get tracker level2 data pointer
231     //
232     tracker->SetBranchAddress("TrkLevel2",&trk);
233     nevtrkl2 = tracker->GetEntries();
234     //
235     // Retrieve GL_RUN variables from the level2 file
236     //
237     tofversion = ToFInfo(false); // we should decide how to handle versioning system
238     //
239     // create an interface to RunInfo called "runinfo"
240     //
241     // ItoRunInfo= interface with RunInfo and GL_RUN
242     runinfo = new ItoRunInfo(file);
243     //
244     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
245     //
246     sgnl = 0;
247     sgnl = runinfo->Update(run, "TOF",tofversion);
248     if ( sgnl ){
249     if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n");
250     code = sgnl;
251     goto closeandexit;
252     } else {
253     sgnl = 0;
254     };
255     //
256     // number of events in the file BEFORE the first event of our run
257     //
258     nobefrun = runinfo->GetFirstEntry();
259     //
260     // total number of events in the file
261     //
262     totfileentries = runinfo->GetFileEntries();
263     //
264     // first file entry AFTER the last event of our run
265     //
266     noaftrun = runinfo->GetLastEntry() + 1;
267     //
268     // number of run to be processed
269     //
270     numbofrun = runinfo->GetNoRun();
271     //
272     // Try to access the ToF tree in the file, if it exists we are reprocessing data if not we are processing a new run
273     //
274     toftclone = (TTree*)file->Get("ToF");
275     //
276     if ( !toftclone ){
277     //
278     // tree does not exist, we are not reprocessing
279     //
280     reproc = false;
281     if ( run == 0ULL && verbose ) printf(" ToF - WARNING: you are reprocessing data but ToF tree does not exist!\n");
282     if ( runinfo->IsReprocessing() && run != 0ULL && verbose ) printf(" ToF - WARNING: it seems you are not reprocessing data but ToF\n versioning information already exists in RunInfo.\n");
283    
284     } else {
285     //
286     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
287     //
288     reproc = true;
289     //
290     // update versioning information
291     //
292     if ( verbose ) printf("\n Preparing the pre-processing...\n");
293     //
294     if ( run == 0ULL ){
295     //
296     // we are reprocessing all the file
297     // 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
298     //
299     reprocall = true;
300     //
301     if ( verbose ) printf("\n ToF - WARNING: Reprocessing all runs\n");
302     //
303     } else {
304     //
305     // 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
306     //
307     reprocall = false;
308     //
309     if ( verbose ) printf("\n ToF - WARNING: Reprocessing run number %llu \n",run);
310     //
311     // copying old tree to a new file
312     //
313     tempfile = new TFile(tempname.str().c_str(),"RECREATE");
314     temptof = toftclone->CloneTree(-1,"fast");
315     temptof->SetName("ToF-old");
316     tempfile->Write();
317     tempfile->Close();
318     }
319     //
320     // Delete the old tree from old file and memory
321     //
322     toftclone->Delete("all");
323     //
324     if ( verbose ) printf(" ...done!\n");
325     //
326     };
327     //
328     // create ToF detector tree toft
329     //
330     file->cd();
331     toft = new TTree("ToF-new","PAMELA Level2 ToF data");
332     toft->Branch("ToFLevel2","ToFLevel2",&tof);
333     //
334     if ( reproc && !reprocall ){
335     //
336     // open new file and retrieve all tree informations
337     //
338     tempfile = new TFile(tempname.str().c_str(),"READ");
339     toftclone = (TTree*)tempfile->Get("ToF-old");
340     toftclone->SetBranchAddress("ToFLevel2",&tofclone);
341     //
342     if ( nobefrun > 0 ){
343     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
344     if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run);
345     if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
346     for (UInt_t j = 0; j < nobefrun; j++){
347     //
348     toftclone->GetEntry(j);
349     //
350     // copy tofclone to tof
351     //
352 mocchiut 1.3 //tof = new ToFLevel2();
353     tof->Clear();
354 mocchiut 1.1 memcpy(&tof,&tofclone,sizeof(tofclone));
355     //
356     // Fill entry in the new tree
357     //
358     toft->Fill();
359     //
360     };
361     if ( verbose ) printf(" Finished successful copying!\n");
362     };
363     };
364     //
365     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
366     //
367     runlist = runinfo->GetRunList();
368     //
369     // Loop over the run to be processed
370     //
371     for (UInt_t irun=0; irun < numbofrun; irun++){
372     //
373     // retrieve the first run ID to be processed using the RunInfo list
374     //
375     idRun = runlist->At(irun);
376     if ( verbose ) printf("\n\n\n ####################################################################### \n");
377     if ( verbose ) printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
378     if ( verbose ) printf(" ####################################################################### \n\n\n");
379     //
380     runinfo->ID_REG_RUN = 0ULL;
381     //
382     // store in the runinfo class the GL_RUN variables for our run
383     //
384     sgnl = 0;
385     sgnl = runinfo->GetRunInfo(idRun);
386     if ( sgnl ){
387     if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n");
388     code = sgnl;
389     goto closeandexit;
390     } else {
391     sgnl = 0;
392     };
393     //
394     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
395     //
396     if ( runinfo->ID_REG_RUN == 0 ){
397     if ( verbose ) printf("\n TOF - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);
398     code = -5;
399     goto closeandexit;
400     };
401     //
402     // Search in the DB the path and name of the LEVEL0 file to be processed.
403     //
404     glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);
405     //
406     ftmpname.str("");
407     ftmpname << glroot->PATH.Data() << "/";
408     ftmpname << glroot->NAME.Data();
409     fname = ftmpname.str().c_str();
410     //
411     // print out informations
412     //
413     totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;
414     if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
415     if ( verbose ) printf(" RUN HEADER absolute time is: %llu \n",runinfo->RUNHEADER_TIME);
416     if ( verbose ) printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);
417     if ( verbose ) printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,runinfo->EV_REG_PHYS_FROM,runinfo->EV_REG_PHYS_TO);
418     //
419     // Open Level0 file
420     //
421     l0File = new TFile(fname.Data());
422     if ( !l0File ) {
423     if ( verbose ) printf(" TOF - ERROR: problems opening Level0 file\n");
424     code = -6;
425     goto closeandexit;
426     };
427     l0tr = (TTree*)l0File->Get("Physics");
428     if ( !l0tr ) {
429     if ( verbose ) printf(" TOF - ERROR: no Physics tree in Level0 file\n");
430     l0File->Close();
431     code = -7;
432     goto closeandexit;
433     };
434     l0head = l0tr->GetBranch("Header");
435     if ( !l0head ) {
436     if ( verbose ) printf(" TOF - ERROR: no Header branch in Level0 tree\n");
437     l0File->Close();
438     code = -8;
439     goto closeandexit;
440     };
441     l0registry = l0tr->GetBranch("Registry");
442     if ( !l0registry ) {
443     if ( verbose ) printf(" TOF - ERROR: no Registry branch in Level0 tree\n");
444     l0File->Close();
445     code = -9;
446     goto closeandexit;
447     };
448     l0trig = l0tr->GetBranch("Trigger");
449     if ( !l0trig ) {
450     if ( verbose ) printf(" TOF - ERROR: no Trigger branch in Level0 tree\n");
451     l0File->Close();
452     code = -300;
453     goto closeandexit;
454     };
455     l0tof = l0tr->GetBranch("Tof");
456     if ( !l0tof ) {
457     if ( verbose ) printf(" TOF - ERROR: no ToF branch in Level0 tree\n");
458     l0File->Close();
459     code = -303;
460     goto closeandexit;
461     };
462     //
463     l0tr->SetBranchAddress("Trigger", &trig);
464     l0tr->SetBranchAddress("Tof", &tofEvent);
465     l0tr->SetBranchAddress("Registry", &l0reg);
466     //
467     nevents = l0registry->GetEntries();
468     //
469     if ( nevents < 1 ) {
470     if ( verbose ) printf(" TOF - ERROR: Level0 file is empty\n\n");
471     l0File->Close();
472     code = -11;
473     goto closeandexit;
474     };
475     //
476     if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {
477     if ( verbose ) printf(" TOF - ERROR: too few entries in the registry tree\n");
478     l0File->Close();
479     code = -12;
480     goto closeandexit;
481     };
482     //
483     // Check if we have to load parameter files (or calibration associated to runs and not to events)
484     //
485     // for example let's assume that we could have different magnetic field maps for different runs:
486     //
487     if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runinfo->RUNHEADER_TIME ) ){
488     trkpar1 = false;
489     // read from DB infos about Magnetic filed maps
490     glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"field",dbc); // parameters stored in DB in GL_PRAM table
491     tttrkpar1 = glparam->TO_TIME;
492     // ----------------------------
493     // Read the magnetic field
494     // ----------------------------
495     if ( verbose ) printf(" Reading magnetic field maps: \n");
496     trk->LoadField(glparam->PATH+glparam->NAME);
497     if ( verbose ) printf("\n");
498     };
499     // Bug Found: Emiliano 30 May 2006
500     // if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){
501     // tofpar1 = false;
502     // //
503     // glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"tof param",dbc); // parameters stored in DB in GL_PRAM table
504     // //
505     // tttofpar1 = glparam->TO_TIME;
506     // rdtofcal((char *)(glparam->PATH+glparam->NAME).Data());
507     // };
508     // Corrected:
509     if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){
510     tofpar1 = false;
511     //
512     Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"tof param",dbc); // parameters stored in DB in GL_PRAM table
513     if ( error<0 ) {
514     code = error;
515     goto closeandexit;
516     };
517     //
518     tttofpar1 = glparam->TO_TIME;
519     rdtofcal((char *)(glparam->PATH+glparam->NAME).Data());
520     };
521     //
522     // run over all the events of the run
523     //
524     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
525     //
526     for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){
527     //
528     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
529     //
530     l0registry->GetEntry(re);
531     //
532     // absolute time of this event
533     //
534     atime = l0reg->absTime;
535     //
536     // physics events is at entry number ei where
537     //
538     ei = l0reg->event;
539     //
540 mocchiut 1.4 //
541     //
542     tof->Clear();
543     Int_t pmt_id = 0;
544     ToFPMT *t_pmt = new ToFPMT();
545     TClonesArray &tpmt = *tof->PMT;
546     ToFTrkVar *t_tof = new ToFTrkVar();
547     TClonesArray &t = *tof->ToFTrk;
548     //
549 mocchiut 1.1 // paranoid check
550     //
551     if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME ) {
552     if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n");
553     goto jumpev;
554     };
555     //
556     // retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr"
557     //
558     if ( !reprocall ){
559     itr = nobefrun + (re - runinfo->EV_REG_PHYS_FROM);
560     } else {
561     itr = runinfo->GetFirstEntry() + (re - runinfo->EV_REG_PHYS_FROM);
562     };
563     if ( itr > nevtrkl2 ){ // nevtrkl2 tracker entry number
564     if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
565     l0File->Close();
566     code = -313;
567     goto closeandexit;
568     };
569     tracker->GetEntry(itr);
570     ///
571 mocchiut 1.2 //
572 mocchiut 1.1 l0tof->GetEntry(ei);
573     l0trig->GetEntry(ei);
574     ///
575     //
576     procev++;
577     //
578     // start processing
579     //
580     //
581     // Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure
582    
583     for (Int_t gg=0; gg<4;gg++){
584     for (Int_t hh=0; hh<12;hh++){
585     tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];
586     tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh];
587     };
588     };
589    
590     for (Int_t hh=0; hh<5;hh++){
591     tofinput_.patterntrig[hh]=trig->patterntrig[hh];
592     };
593     //
594     // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables.
595     //
596 mocchiut 1.4 npmtentry = 0;
597     //
598     ntrkentry = 0;
599 mocchiut 1.1 //
600     // Calculate tracks informations from ToF alone
601     //
602     tofl2com();
603     //
604 mocchiut 1.4 memcpy(tof->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t));
605     //
606     t_tof->trkseqno = -1;
607     //
608 mocchiut 1.1 // and now we must copy from the output structure to the level2 class:
609     //
610 mocchiut 1.4 t_tof->npmttdc = 0;
611     //
612 mocchiut 1.1 for (Int_t hh=0; hh<12;hh++){
613     for (Int_t kk=0; kk<4;kk++){
614 mocchiut 1.4 if ( tofoutput_.tofmask[hh][kk] != 0 ){
615     pmt_id = tof->GetPMTid(kk,hh);
616     t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
617     t_tof->npmttdc++;
618     };
619     };
620     };
621     for (Int_t kk=0; kk<13;kk++){
622     t_tof->beta[kk] = tofoutput_.betatof_a[kk];
623 mocchiut 1.1 }
624 mocchiut 1.4 //
625     t_tof->npmtadc = 0;
626 mocchiut 1.1 for (Int_t hh=0; hh<12;hh++){
627     for (Int_t kk=0; kk<4;kk++){
628 mocchiut 1.4 if ( tofoutput_.adctof_c[hh][kk] < 1000 ){
629     t_tof->dedx.AddAt(tofoutput_.adctof_c[hh][kk],t_tof->npmtadc);
630     pmt_id = tof->GetPMTid(kk,hh);
631     t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
632     t_tof->npmtadc++;
633     };
634     };
635     };
636     //
637     memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
638     memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
639     //
640     new(t[ntrkentry]) ToFTrkVar(*t_tof);
641     ntrkentry++;
642     t_tof->Clear();
643     //
644 mocchiut 1.1 //
645     //
646 mocchiut 1.4 t_pmt->Clear();
647 mocchiut 1.1 //
648 mocchiut 1.4 for (Int_t hh=0; hh<12;hh++){
649     for (Int_t kk=0; kk<4;kk++){
650     if ( tofoutput_.tdc_c[hh][kk] < 4095 || tofEvent->adc[kk][hh] < 4095 ){
651     //
652     t_pmt->pmt_id = tof->GetPMTid(kk,hh);
653     t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk];
654     t_pmt->adc = tofEvent->adc[kk][hh];
655     //
656     new(tpmt[npmtentry]) ToFPMT(*t_pmt);
657     npmtentry++;
658     t_pmt->Clear();
659     };
660     };
661     };
662 mocchiut 1.1 //
663 mocchiut 1.4 // Calculate track-related variables
664 mocchiut 1.1 //
665 mocchiut 1.2 if ( trk->ntrk() > 0 ){
666 mocchiut 1.1 //
667     // We have at least one track
668     //
669     //
670     // Run over tracks
671     //
672     for(Int_t nt=0; nt < trk->ntrk(); nt++){
673     //
674     TrkTrack *ptt = trk->GetStoredTrack(nt);
675     //
676     // Copy the alpha vector in the input structure
677     //
678     for (Int_t e = 0; e < 5 ; e++){
679     tofinput_.al_pp[e] = ptt->al[e];
680     };
681     //
682     // Get tracker related variables for this track
683     //
684     toftrk();
685     //
686     // Copy values in the class from the structure (we need to use a temporary class to store variables).
687     //
688 mocchiut 1.4 t_tof->npmttdc = 0;
689     for (Int_t hh=0; hh<12;hh++){
690     for (Int_t kk=0; kk<4;kk++){
691     if ( tofoutput_.tofmask[hh][kk] != 0 ){
692     pmt_id = tof->GetPMTid(kk,hh);
693     t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
694     t_tof->npmttdc++;
695     };
696     };
697     };
698 mocchiut 1.1 for (Int_t kk=0; kk<13;kk++){
699 mocchiut 1.4 t_tof->beta[kk] = tofoutput_.beta_a[kk];
700     };
701     //
702     t_tof->npmtadc = 0;
703 mocchiut 1.1 for (Int_t hh=0; hh<12;hh++){
704     for (Int_t kk=0; kk<4;kk++){
705 mocchiut 1.4 if ( tofoutput_.adc_c[hh][kk] < 1000 ){
706     t_tof->dedx.AddAt(tofoutput_.adc_c[hh][kk],t_tof->npmtadc);
707     pmt_id = tof->GetPMTid(kk,hh);
708     t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
709     t_tof->npmtadc++;
710     };
711     };
712     };
713     //
714     memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
715     memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
716 mocchiut 1.1 //
717     // Store the tracker track number in order to be sure to have shyncronized data during analysis
718     //
719     t_tof->trkseqno = nt;
720     //
721     // create a new object for this event with track-related variables
722     //
723     new(t[ntrkentry]) ToFTrkVar(*t_tof);
724     ntrkentry++;
725 mocchiut 1.4 t_tof->Clear();
726 mocchiut 1.1 //
727     }; // loop on all the tracks
728     };
729     //
730     // Fill the rootple
731     //
732     toft->Fill();
733     //
734     //
735 mocchiut 1.4 //
736     delete t_tof;
737     //
738     //
739     //
740 mocchiut 1.1 jumpev:
741     debug = false;
742     //
743     };
744     }; // process all the runs
745     //
746     if ( verbose ) printf("\n Finished processing data \n");
747     //
748     closeandexit:
749     //
750     // 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.
751     //
752     if ( !reprocall && reproc && code >= 0 ){
753     if ( totfileentries > noaftrun ){
754     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
755     if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
756     if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
757     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
758     //
759     // Get entry from old tree
760     //
761     toftclone->GetEntry(j);
762     //
763     // copy tofclone to tof
764     //
765 mocchiut 1.3 // tof = new ToFLevel2();
766     tof->Clear();
767 mocchiut 1.1 memcpy(&tof,&tofclone,sizeof(tofclone));
768     //
769     // Fill entry in the new tree
770     //
771     toft->Fill();
772     };
773     if ( verbose ) printf(" Finished successful copying!\n");
774     };
775     };
776     //
777     // Close files, delete old tree(s), write and close level2 file
778     //
779     if ( l0File ) l0File->Close();
780     if ( tempfile ) tempfile->Close();
781     gSystem->Unlink(tempname.str().c_str());
782     if ( tracker ) tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file!
783     //
784     if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n");
785     if ( verbose ) printf("\n Writing and closing rootple\n");
786     if ( runinfo ) runinfo->Close();
787     if ( toft ) toft->SetName("ToF");
788     if ( file ){
789     file->cd();
790     file->Write("ToF");
791     };
792     //
793     gSystem->Unlink(toffolder.str().c_str());
794     //
795     // the end
796     //
797     if ( verbose ) printf("\n Exiting...\n");
798     if(toft)toft->Delete();
799 mocchiut 1.5 //
800     if ( tof ) delete tof;
801     if ( tofclone ) delete tofclone;
802     if ( glroot ) delete glroot;
803     if ( runinfo ) delete runinfo;
804     //
805 mocchiut 1.1 if ( code < 0 ) throw code;
806     return(code);
807     }

  ViewVC Help
Powered by ViewVC 1.1.23