/[PAMELA software]/DarthVader/CalorimeterLevel2/src/CaloCore.cpp
ViewVC logotype

Annotation of /DarthVader/CalorimeterLevel2/src/CaloCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (hide annotations) (download)
Tue Apr 17 11:58:17 2007 UTC (17 years, 10 months ago) by mocchiut
Branch: MAIN
Changes since 1.24: +1 -1 lines
XXXFolder bug fixed

1 mocchiut 1.1 //
2     // Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti
3     //
4 mocchiut 1.3 // CaloCore.cxx version 3.05 (2006-05-30)
5 mocchiut 1.1 //
6     // The only input needed is the path to the directory created by YODA for the data file you want to analyze.
7     //
8     // Changelog:
9 mocchiut 1.3 //
10 mocchiut 1.11 // 3.08 (2006-11-13): Added high energy nuclei capability and "process all events" capability.
11     //
12 mocchiut 1.4 // 3.04 - 3.05 (2006-05-30): Qlast and nlast are now calculated using 4 (not 8) strips aournd the shower axis. Small bug fixed.
13 mocchiut 1.2 //
14     // 3.03 - 3.04 (2006-05-23): Forgot to put impx and impy in the PAMELA reference system, fixed.
15 mocchiut 1.1 //
16     // 3.02 - 3.03 (2006-05-18): updated to be called in DarthVader. Output dimension are now in cm and in the PAMELA reference system.
17     //
18     // 3.01 - 3.02 (2006-04-21): when copying entries get size of caclone and not of ca... (shouldn't matter). Fixed increasing file dimension bug when reprocessing.
19     // Added variable planemax[2], plane of maximum energy release (x and y) in final output. Use ItoRunInfo instead of RunInfo.
20     //
21     // 3.00 - 3.01 (2006-04-14): fixed small bug in tagging the track used to determine track-related variables, put in caloprocessing the opening of parameters files, fixed
22     // small bug in fortran routines
23     //
24     // 2.01 - 3.00 (2006-04-14): almost everything has changed. Now it can process one, all or some runs, introduced the final CaloLevel2 class+methods and the
25     // working class "CaloProcessing", linked to the preliminary tracker flight software v0r00, reads YODA unique output files,
26     // reduced the number of installed libraries, F77 programs splitted depending on the function contained, introduced the readout and
27     // processing of self-trigger events, if the tracker provides more than one track all calorimeter track-related variables are saved
28     // as many times as the number of tracks (via the TClonesArray object in the level2 rootple) and many other small changes.
29     //
30     // 2.00 - 2.01 (2006-01-26): bug: wrong calculation of baselines in some cases, fixed.
31     //
32     // 1.00 - 2.00 (2006-01-11): use TSQL ROOT classes instead of directly calling MySQL.
33     //
34     // 0.00 - 1.00 (2005-09-14): seems working.
35     //
36     // 0.00 (2005-09-09): clone of CaloLEVEL2.c .
37     //
38     // C/C++ headers
39     //
40     #include <fstream>
41     #include <string.h>
42     //
43     // ROOT headers
44     //
45     #include <TTree.h>
46     #include <TClassEdit.h>
47     #include <TObject.h>
48     #include <TList.h>
49 mocchiut 1.8 #include <TArrayI.h>
50 mocchiut 1.1 #include <TSystem.h>
51     #include <TSystemDirectory.h>
52     #include <TString.h>
53     #include <TFile.h>
54     #include <TClass.h>
55     #include <TCanvas.h>
56     #include <TH1.h>
57     #include <TH1F.h>
58     #include <TH2D.h>
59     #include <TLatex.h>
60     #include <TPad.h>
61     #include <TSQLServer.h>
62     #include <TSQLRow.h>
63     #include <TSQLResult.h>
64     #include <TClonesArray.h>
65     #include <TStreamerInfo.h>
66     //
67 mocchiut 1.20 // This program headers
68     //
69     #include <RunInfo.h>
70     //
71 mocchiut 1.1 // YODA headers
72     //
73     #include <PamelaRun.h>
74     #include <physics/trigger/TriggerEvent.h>
75     //
76     // This program headers
77     //
78     #include <CaloCore.h>
79 mocchiut 1.13 #include <CaloLevel1.h>
80 mocchiut 1.1 #include <CaloLevel2.h>
81 mocchiut 1.22 #include <CaloLevel0.h>
82 mocchiut 1.1 #include <CaloVerl2.h>
83     //
84     // Tracker classes headers and definitions
85     //
86     #include <TrkLevel2.h>
87     //
88     using namespace std;
89     //
90     // CORE ROUTINE
91     //
92 mocchiut 1.8 int CaloCore(UInt_t run, TFile *file, TSQLServer *dbc, Int_t calargc, char *calargv[]){
93 mocchiut 1.1 //
94     // Set these to true to have a very verbose output.
95     //
96     Bool_t verbose = false;
97     Bool_t debug = false;
98     //
99 mocchiut 1.21 Bool_t crosst = true;
100     //
101 mocchiut 1.11 Bool_t trackanyway = true;
102     //
103     Float_t rigdefault = 50.;
104     //
105     Bool_t hZn = true;
106     //
107     Bool_t withtrk = true;
108     //
109     Bool_t st = true;
110     //
111 mocchiut 1.13 Bool_t getl1 = true;
112     //
113 mocchiut 1.23 Bool_t mechal = false;
114     //
115 mocchiut 1.1 // Output directory is the working directoy.
116     //
117     const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
118     //
119     Int_t ri = 0;
120 mocchiut 1.25 TString processFolder = "calorimeterFolder_"+run;
121 mocchiut 1.1 if ( calargc > 0 ){
122     ri = 0;
123     while ( ri < calargc ){
124     if ( !strcmp(calargv[ri],"-processFolder") ) {
125     if ( calargc < ri+1 ){
126     throw -3;
127     };
128     processFolder = (TString)calargv[ri+1];
129     ri++;
130     };
131     if ( !strcmp(calargv[ri],"-v") || !strcmp(calargv[ri],"--verbose") ) {
132     verbose = true;
133     };
134     if ( !strcmp(calargv[ri],"-g") || !strcmp(calargv[ri],"--debug") ) {
135 mocchiut 1.13 verbose = true;
136 mocchiut 1.1 debug = true;
137     };
138 mocchiut 1.11 if ( !strcmp(calargv[ri],"--alltracks") ) {
139     trackanyway = true;
140     };
141 mocchiut 1.23 if ( !strcmp(calargv[ri],"--use-default-alig") ) {
142     mechal = true;
143     };
144 mocchiut 1.21 if ( !strcmp(calargv[ri],"--no-crosstalk") ) {
145     crosst = false;
146     };
147 mocchiut 1.11 if ( !strcmp(calargv[ri],"--no-tracker") ) {
148     withtrk = false;
149     };
150     if ( !strcmp(calargv[ri],"--with-tracker") ) {
151     withtrk = true;
152     };
153     if ( !strcmp(calargv[ri],"--defrig") ) {
154     if ( calargc < ri+1 ){
155     throw -3;
156     };
157     rigdefault = atof(calargv[ri+1]);
158     ri++;
159     };
160     if ( !strcmp(calargv[ri],"--no-alltracks") ) {
161     trackanyway = false;
162     };
163     if ( !strcmp(calargv[ri],"--highZnuclei") ) {
164     hZn = true;
165     };
166     if ( !strcmp(calargv[ri],"--no-highZnuclei") ) {
167     hZn = false;
168     };
169     if ( !strcmp(calargv[ri],"--selftrigger") ) {
170     st = true;
171     };
172     if ( !strcmp(calargv[ri],"--no-selftrigger") ) {
173     st = false;
174     };
175 mocchiut 1.13 if ( !strcmp(calargv[ri],"--no-level1") ) {
176     getl1 = false;
177     };
178 mocchiut 1.11 if ( !strcmp(calargv[ri],"--help") ) {
179     printf("\n\n CALORIMETER HELP CALLED\n\n");
180     printf(" CaloCore options: \n");
181     printf(" -v | --verbose be verbose\n");
182     printf(" -g | --debug be really verbose\n");
183     printf(" --defrig rig rig is the default rigidity in GV to be used to\n");
184     printf(" obtain calorimeter variables in the routines\n");
185     printf(" \"alltracks\" and \"higZnuclei\" [default = 50]\n");
186     printf(" --alltracks fill the track related variables even in the case\n");
187     printf(" of no tracks from tracker and no selftrigger event\n");
188 mocchiut 1.14 printf(" when we have a calorimeter fit for both views [default]\n");
189     printf(" --no-alltracks fill the track related variables only in the case\n");
190     printf(" of a good track from tracker or selftrigger\n");
191 mocchiut 1.11 printf(" --highZnuclei call the routine to analyze high Z nuclei\n");
192     printf(" selftrigger events [default]\n");
193     printf(" --no-highZnuclei do not call the routine to analyze high Z nuclei\n");
194     printf(" selftrigger events\n");
195     printf(" --no-tracker do not use tracker level2\n");
196     printf(" --with-tracker use tracker level2 [default]\n");
197 mocchiut 1.21 printf(" --no-crosstalk do not apply crosstalk corrections\n");
198 mocchiut 1.11 printf(" --selftrigger process selftrigger events [default]\n");
199     printf(" --no-selftrigger skip selftrigger events\n");
200 mocchiut 1.13 printf(" --no-level1 do not save Level1 TBranch\n");
201 mocchiut 1.23 printf(" --use-default-alig use default mechanical alignement (defined in CaloLevel1.h\n");
202 mocchiut 1.11 throw -114;
203     };
204 mocchiut 1.1 ri++;
205     };
206     };
207     //
208 mocchiut 1.13 if ( verbose ){
209     printf("\n");
210     if ( getl1 ) printf(" Saving calorimeter level1 data \n");
211     if ( st ) printf(" Calling selftrigger subroutine \n");
212     if ( hZn ) printf(" Calling high energy nuclei subroutine \n");
213     if ( trackanyway ) printf(" Filling track related variables for all the possible tracks \n");
214     if ( hZn || trackanyway ) printf(" => default assumed rigidity %f \n",rigdefault);
215     if ( withtrk ) printf(" Using tracker level2 data \n");
216     printf("\n");
217     };
218 mocchiut 1.11 //
219 mocchiut 1.1 // Working filename
220     //
221     TString outputfile;
222     stringstream name;
223     name.str("");
224     name << outdir << "/";
225     //
226     // Variables.
227     //
228     TTree *tracker = 0;
229     TTree *calo = 0;
230     TTree *caloclone = 0;
231     Bool_t reproc = false;
232     Bool_t reprocall = false;
233 mocchiut 1.8 UInt_t nevents = 0;
234 mocchiut 1.1 UInt_t nobefrun = 0;
235     UInt_t noaftrun = 0;
236     UInt_t numbofrun = 0;
237 mocchiut 1.8 UInt_t totnorun = 0;
238 mocchiut 1.1 //
239     Int_t code = 0;
240     Int_t sgnl;
241     //
242     // calorimeter level2 classes
243     //
244 mocchiut 1.13 CaloLevel1 *c1 = 0;
245     CaloLevel1 *c1clone = 0;
246     if ( getl1 ){
247     c1 = new CaloLevel1();
248     c1clone = new CaloLevel1();
249     };
250     //
251     // calorimeter level2 classes
252     //
253 mocchiut 1.1 CaloLevel2 *ca = new CaloLevel2();
254     CaloLevel2 *caclone = new CaloLevel2();
255     //
256     TrkLevel2 *trk = new TrkLevel2();
257 mocchiut 1.8 Int_t nevtrkl2 = 0;
258 mocchiut 1.1 //
259     UInt_t procev = 0;
260     //
261 mocchiut 1.8 // define variables where to store the absolute run header and run trailer times (unsigned long long integers, when set to a number use to store the correct number).
262 mocchiut 1.1 //
263 mocchiut 1.8 UInt_t runheadtime = 0;
264     UInt_t runtrailtime = 0;
265 mocchiut 1.1 UInt_t evfrom = 0;
266     UInt_t evto = 0;
267 mocchiut 1.8 UInt_t totfileentries = 0;
268     UInt_t idRun = 0;
269 mocchiut 1.1 Int_t id_reg_run=-1;
270     stringstream ftmpname;
271     TString fname;
272     //
273     // define variables for opening and reading level0 file
274     //
275     TFile *l0File = 0;
276     TTree *l0tr = 0;
277 mocchiut 1.24 TTree *softinfo = 0;
278 mocchiut 1.1 TBranch *l0head = 0;
279     TBranch *l0calo = 0;
280     TBranch *l0trig = 0;
281 mocchiut 1.8 pamela::EventHeader *eh = 0;
282     pamela::PscuHeader *ph = 0;
283 mocchiut 1.1 pamela::trigger::TriggerEvent *trig = 0;
284 mocchiut 1.24 Int_t yodaver = 0;
285 mocchiut 1.1 //
286     // Define some basic variables
287     //
288 mocchiut 1.22 CaloLevel0 *event = new CaloLevel0(); // NOTICE: very important to call here the constructor!
289 mocchiut 1.21 event->SetCrossTalk(crosst);
290 mocchiut 1.1 stringstream file2;
291     stringstream file3;
292     stringstream qy;
293     // Bool_t imtrack = false;
294     Bool_t filled = false;
295     //
296 mocchiut 1.8 UInt_t caloevents = 0;
297 mocchiut 1.1 stringstream calfile;
298     stringstream aligfile;
299     //
300     Int_t i = -1;
301     Int_t itr = -1;
302     Int_t badevent = 0;
303     Int_t totevent = 0;
304     //
305 mocchiut 1.8 UInt_t atime = 0;
306 mocchiut 1.1 //
307     Int_t S3 = 0;
308     Int_t S2 = 0;
309     Int_t S12 = 0;
310     Int_t S11 = 0;
311     UInt_t re = 0;
312 mocchiut 1.9 UInt_t jumped = 0;
313 mocchiut 1.1 //
314     TString caloversion;
315     ItoRunInfo *runinfo = 0;
316 mocchiut 1.8 TArrayI *runlist = 0;
317 mocchiut 1.1 //
318     Float_t tmptrigty = -1.;
319     Int_t ntrkentry = 0;
320     GL_PARAM *q4 = new GL_PARAM();
321 mocchiut 1.8 UInt_t tttrkpar1 = 0;
322 mocchiut 1.1 Bool_t trkpar1 = true;
323     GL_ROOT *glroot = new GL_ROOT();
324 mocchiut 1.8 GL_TIMESYNC *dbtime = 0;
325 mocchiut 1.1 //
326 mocchiut 1.15 Long64_t maxsize = 10000000000LL;
327     TTree::SetMaxTreeSize(maxsize);
328 mocchiut 1.1 //
329     //
330     TFile *tempfile = 0;
331     TTree *tempcalo = 0;
332     stringstream tempname;
333     stringstream calofolder;
334     tempname.str("");
335     tempname << outdir;
336     tempname << "/" << processFolder.Data();
337     calofolder.str("");
338     calofolder << tempname.str().c_str();
339     gSystem->MakeDirectory(calofolder.str().c_str());
340     tempname << "/calotree_run";
341     tempname << run << ".root";
342     //
343     // As a first thing we must check what we have to do: if run = -1 we must process all events in the file has been passed
344     // if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file
345     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
346     //
347 mocchiut 1.8 if ( run == 0 ) reproc = true;
348 mocchiut 1.1 //
349     //
350     //
351     if ( !file->IsOpen() ){
352     if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n");
353     throw -101;
354     };
355     //
356 mocchiut 1.11 if ( withtrk ){
357     //
358     // Does it contain the Tracker tree?
359     //
360     tracker = (TTree*)file->Get("Tracker");
361     if ( !tracker ) {
362     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n");
363     code = -102;
364     goto closeandexit;
365     };
366     tracker->SetBranchAddress("TrkLevel2",&trk);
367     nevtrkl2 = tracker->GetEntries();
368 mocchiut 1.1 };
369     //
370     // Call runinfo
371     //
372     sgnl = 0;
373     runinfo = new ItoRunInfo(file);
374     //
375     // update versioning information and retrieve informations about the run to be processed
376     //
377     caloversion = CaloInfo(false);
378     sgnl = runinfo->Update(run,"CALO",caloversion);
379     //
380     if ( sgnl ){
381     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
382     code = sgnl;
383     goto closeandexit;
384     } else {
385     sgnl = 0;
386     };
387     //
388     // number of events in the file BEFORE the first event of our run
389     //
390     nobefrun = runinfo->GetFirstEntry();
391     //
392     // total number of events in the file
393     //
394     totfileentries = runinfo->GetFileEntries();
395     //
396     // first file entry AFTER the last event of our run
397     //
398     noaftrun = runinfo->GetLastEntry() + 1;
399     //
400     // number of run to be processed
401     //
402     numbofrun = runinfo->GetNoRun();
403     //
404     // number of runs in the file
405     //
406     totnorun = runinfo->GetRunEntries();
407     //
408     // Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it.
409     //
410     caloclone = (TTree*)file->Get("Calorimeter");
411     //
412     if ( !caloclone ){
413     reproc = false;
414 mocchiut 1.8 if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n");
415     if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" CALORIMETER - WARNING: it seems you are not reprocessing data but calorimeter\n versioning information already exists in RunInfo.\n");
416 mocchiut 1.1 //
417     } else {
418     //
419 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
420 mocchiut 1.1 reproc = true;
421     //
422     if ( verbose ) printf("\n Preparing the pre-processing...\n");
423     //
424 mocchiut 1.8 if ( run == 0 ){
425 mocchiut 1.1 //
426     // if we are reprocessing everything we don't need to copy any old event and we can just create a new branch in the clone tree and jump steps 4/5/7.
427     //
428     if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n");
429     reprocall = true;
430     //
431     } else {
432     //
433     // we are reprocessing a single run
434     //
435 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run);
436 mocchiut 1.1 reprocall = false;
437     //
438     //
439     //
440     tempfile = new TFile(tempname.str().c_str(),"RECREATE");
441     tempcalo = caloclone->CloneTree(-1,"fast");
442     tempcalo->SetName("Calorimeter-old");
443     tempfile->Write();
444     tempfile->Close();
445     };
446     //
447     // delete old tree
448     //
449     caloclone->Delete("all");
450     //
451     if ( verbose ) printf("\n ...done!\n");
452     //
453     };
454     //
455     // create calorimeter tree calo
456     //
457     file->cd();
458     calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data");
459 mocchiut 1.15 calo->SetAutoSave(900000000000000LL);
460 pam-fi 1.16 ca->Set();
461 mocchiut 1.1 calo->Branch("CaloLevel2","CaloLevel2",&ca);
462 mocchiut 1.13 if ( getl1 ) calo->Branch("CaloLevel1","CaloLevel1",&c1);
463 mocchiut 1.1 //
464     if ( reproc && !reprocall ){
465     //
466     //
467     //
468     tempfile = new TFile(tempname.str().c_str(),"READ");
469     caloclone = (TTree*)tempfile->Get("Calorimeter-old");
470 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
471 mocchiut 1.1 caloclone->SetBranchAddress("CaloLevel2",&caclone);
472 mocchiut 1.13 TBranch *havel1 = caloclone->GetBranch("CaloLevel1");
473     if ( !havel1 && getl1 ) throw -117;
474     if ( havel1 && !getl1 ) throw -118;
475     if ( getl1 ) caloclone->SetBranchAddress("CaloLevel1",&c1clone);
476 mocchiut 1.1 //
477     if ( nobefrun > 0 ){
478     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
479 mocchiut 1.8 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
480 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
481     for (UInt_t j = 0; j < nobefrun; j++){
482     //
483     caloclone->GetEntry(j);
484     //
485     // copy caclone to ca
486     //
487 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
488 mocchiut 1.1 memcpy(&ca,&caclone,sizeof(caclone));
489     //
490     // Fill entry in the new tree
491     //
492     calo->Fill();
493     //
494 mocchiut 1.13 if ( getl1 ) c1->Clear();
495 mocchiut 1.6 ca->Clear();
496     //
497 mocchiut 1.1 };
498     if ( verbose ) printf(" Finished successful copying!\n");
499     };
500     };
501     //
502     // Get the list of run to be processed
503     //
504     runlist = runinfo->GetRunList();
505     //
506     // Loop over the run to be processed
507     //
508     for (UInt_t irun=0; irun < numbofrun; irun++){
509     //
510     badevent = 0;
511     //
512     idRun = runlist->At(irun);
513     if ( verbose ) printf("\n\n\n ####################################################################### \n");
514 mocchiut 1.8 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
515 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
516     //
517     sgnl = runinfo->GetRunInfo(idRun);
518     if ( sgnl ){
519     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
520     code = sgnl;
521     goto closeandexit;
522     } else {
523     sgnl = 0;
524     };
525 mocchiut 1.8 id_reg_run = runinfo->ID_ROOT_L0;
526 mocchiut 1.1 runheadtime = runinfo->RUNHEADER_TIME;
527     runtrailtime = runinfo->RUNTRAILER_TIME;
528 mocchiut 1.8 evfrom = runinfo->EV_FROM;
529     evto = runinfo->EV_TO;
530 mocchiut 1.1 //
531     if ( id_reg_run == -1 ){
532 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
533 mocchiut 1.1 code = -5;
534     goto closeandexit;
535     };
536     //
537 mocchiut 1.8 // prepare the timesync for the db
538     //
539 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
540 mocchiut 1.8 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
541     //
542 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
543     //
544 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
545 mocchiut 1.8 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
546 mocchiut 1.1 //
547     ftmpname.str("");
548     ftmpname << glroot->PATH.Data() << "/";
549     ftmpname << glroot->NAME.Data();
550     fname = ftmpname.str().c_str();
551     //
552     // print out informations
553     //
554 mocchiut 1.8 totevent = runinfo->NEVENTS;
555 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
556 mocchiut 1.8 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime);
557     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime);
558     if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent);
559 mocchiut 1.1 //
560     // Open Level0 file
561     //
562     l0File = new TFile(fname.Data());
563     if ( !l0File ) {
564     if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n");
565     code = -6;
566     goto closeandexit;
567     };
568     l0tr = (TTree*)l0File->Get("Physics");
569     if ( !l0tr ) {
570     if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n");
571     l0File->Close();
572     code = -7;
573     goto closeandexit;
574     };
575     l0head = l0tr->GetBranch("Header");
576     if ( !l0head ) {
577     if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n");
578     l0File->Close();
579     code = -8;
580     goto closeandexit;
581     };
582     l0calo = l0tr->GetBranch("Calorimeter");
583     if ( !l0calo ) {
584     if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n");
585     l0File->Close();
586     code = -103;
587     goto closeandexit;
588     };
589     l0trig = l0tr->GetBranch("Trigger");
590     if ( !l0trig ) {
591     if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n");
592     l0File->Close();
593     code = -104;
594     goto closeandexit;
595     };
596     //
597     l0tr->SetBranchAddress("Trigger", &trig);
598 mocchiut 1.8 l0tr->SetBranchAddress("Header", &eh);
599 mocchiut 1.1 //
600 mocchiut 1.24 softinfo = (TTree*)l0File->Get("SoftInfo");
601     if ( softinfo ){
602     softinfo->SetBranchAddress("SoftInfo",&yodaver);
603     softinfo->GetEntry(0);
604     };
605     if ( debug ) printf(" LEVEL0 FILE GENERATED WITH YODA VERSION %i \n",yodaver);
606     //
607 mocchiut 1.1 // Construct the event object, look for the calibration which include the first header
608     //
609     sgnl = 0;
610     if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n");
611 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
612 mocchiut 1.1 event->ProcessingInit(dbc,runheadtime,sgnl,l0tr,debug,verbose);
613     if ( verbose ) printf("\n");
614     if ( sgnl == 100 ) {
615     code = sgnl;
616     if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n");
617     sgnl = 0;
618     };
619     if ( sgnl ){
620     l0File->Close();
621     code = sgnl;
622     goto closeandexit;
623     };
624     //
625     qy.str("");
626     //
627 mocchiut 1.8 nevents = l0head->GetEntries();
628 mocchiut 1.1 caloevents = l0calo->GetEntries();
629     //
630     if ( nevents < 1 ) {
631     if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n");
632     l0File->Close();
633     code = -11;
634     goto closeandexit;
635     };
636     //
637     if ( evto > nevents-1 ) {
638     if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n");
639     l0File->Close();
640     code = -12;
641     goto closeandexit;
642     };
643     //
644     // Check if we have to load parameter files
645     //
646     sgnl = 0;
647 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
648 mocchiut 1.23 sgnl = event->ChkParam(dbc,runheadtime,mechal); // calorimeter parameter files
649 mocchiut 1.1 if ( sgnl < 0 ){
650     code = sgnl;
651     l0File->Close();
652     goto closeandexit;
653     };
654     //
655 mocchiut 1.11 if ( withtrk ){
656     if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){
657     trkpar1 = false;
658 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
659 mocchiut 1.11 Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc);
660     if ( glpar < 0 ){
661     code = glpar;
662     goto closeandexit;
663     };
664     tttrkpar1 = q4->TO_TIME;
665     // ----------------------------
666     // Read the magnetic field
667     // ----------------------------
668     if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
669     trk->LoadField(q4->PATH+q4->NAME);
670     if ( verbose ) printf("\n");
671 mocchiut 1.4 };
672 mocchiut 1.1 };
673     //
674     // run over all the events of the run
675     //
676     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
677     //
678 mocchiut 1.8 //
679 mocchiut 1.19 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
680     //for ( re = 46438+200241; re < 46441+200241; re++){
681 mocchiut 1.18 // printf(" i = %i \n",re-200241);
682 mocchiut 1.1 //
683     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
684     //
685 mocchiut 1.8 l0head->GetEntry(re);
686 mocchiut 1.1 //
687     // absolute time of this event
688     //
689 mocchiut 1.8 ph = eh->GetPscuHeader();
690     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
691 mocchiut 1.1 //
692     //
693     //
694 mocchiut 1.8 if ( re > caloevents-1 ){
695 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i);
696     l0File->Close();
697     code = -112;
698     goto closeandexit;
699     };
700     //
701     if ( atime > runtrailtime || atime < runheadtime ) {
702     if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n");
703 mocchiut 1.9 jumped++;
704 mocchiut 1.1 goto jumpev;
705     };
706     //
707     // retrieve tracker informations
708     //
709     if ( !reprocall ){
710 mocchiut 1.19 itr = nobefrun + (re - evfrom - jumped);
711     //itr = re-(46438+200241);
712 mocchiut 1.1 } else {
713 mocchiut 1.9 itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
714 mocchiut 1.1 };
715 mocchiut 1.11 //
716     if ( withtrk ){
717     if ( itr > nevtrkl2 ){
718     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
719     l0File->Close();
720     code = -113;
721     goto closeandexit;
722     };
723     //
724     trk->Clear();
725     //
726     tracker->GetEntry(itr);
727     //
728 mocchiut 1.1 };
729 mocchiut 1.10 //
730 mocchiut 1.1 procev++;
731     //
732     // start processing
733     //
734 mocchiut 1.13 if ( getl1 ) c1->Clear();
735 mocchiut 1.6 ca->Clear();
736 mocchiut 1.1 //
737     // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
738     //
739 mocchiut 1.8 l0trig->GetEntry(re);
740 mocchiut 1.1 S3 = 0;
741     S2 = 0;
742     S12 = 0;
743     S11 = 0;
744     S3 = trig->patterntrig[2];
745     S2 = trig->patterntrig[3];
746     S12 = trig->patterntrig[4];
747     S11 = trig->patterntrig[5];
748 mocchiut 1.17 if ( trig->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
749 mocchiut 1.1 if ( trig->patterntrig[0] ) tmptrigty = 2.;
750     if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
751 mocchiut 1.17 if ( !(trig->patterntrig[1] & (1<<0)) && !trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
752 mocchiut 1.1 event->clevel2->trigty = tmptrigty;
753     //
754     // check if the calibration we are using is still good, if not load another calibration
755     //
756     sgnl = 0;
757     sgnl = event->ChkCalib(dbc,atime);
758     if ( sgnl < 0 ){
759     code = sgnl;
760     goto closeandexit;
761     };
762     if ( sgnl == 100 ){
763     code = sgnl;
764     if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n");
765     badevent++;
766     sgnl = 0;
767     };
768     //
769 mocchiut 1.11 // do we have at least one track from the tracker? this check has been disabled
770 mocchiut 1.1 //
771     event->clevel1->good2 = 1;
772 mocchiut 1.8 //
773 mocchiut 1.1 //
774 mocchiut 1.8 // Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine
775 mocchiut 1.1 //
776 mocchiut 1.11 event->Calibrate(re);
777 mocchiut 1.1 //
778     // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables.
779     //
780     //
781     // Calculate variables common to all tracks (qtot, nstrip, etc.)
782     //
783     event->GetCommonVar();
784     //
785     // Fill common variables
786     //
787 mocchiut 1.13 event->FillCommonVar(c1,ca);
788 mocchiut 1.1 //
789     // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
790     //
791     ntrkentry = 0;
792     //
793     filled = false;
794     //
795 mocchiut 1.11 // Run over tracks (tracker or calorimeter )
796     //
797     if ( withtrk ){
798     for(Int_t nt=0; nt < trk->ntrk(); nt++){
799     //
800     event->clevel1->good2 = 1;
801     //
802     TrkTrack *ptt = trk->GetStoredTrack(nt);
803     //
804     event->clevel1->trkchi2 = 0;
805     //
806     // Copy the alpha vector in the input structure
807     //
808     for (Int_t e = 0; e < 5 ; e++){
809     event->clevel1->al_p[e][0] = ptt->al[e];
810     };
811     //
812     // Get tracker related variables for this track
813     //
814     event->GetTrkVar();
815     //
816     // Save tracker track sequence number
817     //
818     event->trkseqno = nt;
819     //
820     // Copy values in the class ca from the structure clevel2
821     //
822     event->FillTrkVar(ca,ntrkentry);
823     ntrkentry++;
824     filled = true;
825     //
826     }; // loop on all the tracks
827     };
828     //
829     // if no tracks found but there is the possibility to have a good track we should try to calculate anyway the track related variables using the calorimeter
830     // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
831     // here we make an event selection so it must be done very carefully...
832     //
833     // conditions are: 0) no track from the tracker 1) we have a track fit both in x and y 2) no problems with calo for this event 3) no selftrigger event
834     //
835     if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
836     if ( debug ) printf(" Event with a track not fitted by the tracker at entry %i \n",itr);
837     //
838     // Disable "track mode" in the fortran routine
839     //
840     event->clevel1->good2 = 0;
841     event->clevel1->riginput = rigdefault;
842     if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
843     //
844     // We have a selftrigger event to analyze.
845     //
846     for (Int_t e = 0; e < 5 ; e++){
847     event->clevel1->al_p[e][0] = 0.;
848     event->clevel1->al_p[e][1] = 0.;
849     };
850     event->clevel1->trkchi2 = 0;
851     //
852     event->GetTrkVar();
853     //
854     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
855 mocchiut 1.1 //
856 mocchiut 1.11 if ( event->clevel1->good2 == 0 ) {
857 mocchiut 1.1 //
858 mocchiut 1.11 // In selftrigger mode the trkentry variable is set to -1
859 mocchiut 1.1 //
860 mocchiut 1.11 event->trkseqno = -3;
861 mocchiut 1.1 //
862 mocchiut 1.11 // Copy values in the class ca from the structure clevel2
863 mocchiut 1.1 //
864 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
865     ntrkentry++;
866     filled = true;
867     //
868     } else {
869     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
870     };
871     //
872     };
873     //
874     // Call high energy nuclei routine
875     //
876     if ( hZn && event->clevel2->trigty >= 2. ){
877     if ( debug ) printf(" Calling selftrigger high energy nuclei routine for entry %i \n",itr);
878     //
879     // Disable "track mode" in the fortran routine
880     //
881     event->clevel1->good2 = 0;
882     //
883     // Set high energy nuclei flag to one
884     //
885     event->clevel1->hzn = 1;
886     event->clevel1->riginput = rigdefault;
887     //
888     // We have a selftrigger event to analyze.
889     //
890     for (Int_t e = 0; e < 5 ; e++){
891     event->clevel1->al_p[e][0] = 0.;
892     event->clevel1->al_p[e][1] = 0.;
893 mocchiut 1.1 };
894 mocchiut 1.11 event->clevel1->trkchi2 = 0;
895     //
896     event->GetTrkVar();
897     //
898     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
899     //
900     if ( event->clevel1->good2 == 0 ) {
901     //
902     // In selftrigger mode the trkentry variable is set to -1
903     //
904     event->trkseqno = -2;
905     //
906     // Copy values in the class ca from the structure clevel2
907 mocchiut 1.1 //
908 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
909     ntrkentry++;
910     filled = true;
911 mocchiut 1.1 //
912 mocchiut 1.11 } else {
913     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
914     };
915     //
916     };
917     //
918     // self trigger event
919     //
920     if ( st && event->clevel2->trigty >= 2. ){
921     if ( verbose ) printf(" Selftrigger event at entry %i \n",itr);
922     //
923     // Disable "track mode" in the fortran routine
924     //
925     event->clevel1->good2 = 0;
926     //
927     // disable high enery nuclei flag;
928     //
929     event->clevel1->hzn = 0;
930     //
931     // We have a selftrigger event to analyze.
932     //
933     for (Int_t e = 0; e < 5 ; e++){
934     event->clevel1->al_p[e][0] = 0.;
935     event->clevel1->al_p[e][1] = 0.;
936     };
937     event->clevel1->trkchi2 = 0;
938     //
939     event->GetTrkVar();
940     //
941     // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
942     //
943     if ( event->clevel1->good2 == 0 ) {
944 mocchiut 1.1 //
945 mocchiut 1.11 // In selftrigger mode the trkentry variable is set to -1
946 mocchiut 1.1 //
947 mocchiut 1.11 event->trkseqno = -1;
948 mocchiut 1.1 //
949 mocchiut 1.11 // Copy values in the class ca from the structure clevel2
950 mocchiut 1.1 //
951 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
952     ntrkentry++;
953     filled = true;
954 mocchiut 1.1 //
955 mocchiut 1.11 } else {
956     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
957 mocchiut 1.1 };
958     };
959     //
960     if ( !filled ) badevent++;
961     //
962     // Clear structures used to communicate with fortran
963     //
964     event->ClearStructs();
965     //
966     // Fill the rootple
967     //
968     calo->Fill();
969     //
970 mocchiut 1.6 // delete ca;
971 mocchiut 1.1 //
972     jumpev:
973 mocchiut 1.23 if ( !debug ) debug = false;
974 mocchiut 1.1 //
975     };
976     //
977     if ( verbose ) printf("\n SUMMARY:\n");
978     if ( verbose ) printf(" Total number of events: %i \n",totevent);
979     if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent);
980     if ( verbose ) printf(" Events without tracks: %i \n",badevent);
981     //
982     if ( badevent == totevent ){
983 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun);
984 mocchiut 1.1 code = 101;
985     };
986     //
987 mocchiut 1.8 delete dbtime;
988 mocchiut 1.1 //
989     // Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software).
990     //
991     event->RunClose();
992     if ( l0File ) l0File->Close();
993     //
994     }; // process all the runs
995     //
996     if ( verbose ) printf("\n Finished processing data \n");
997     //
998     closeandexit:
999     //
1000     if ( !reprocall && reproc && code >= 0 ){
1001     if ( totfileentries > noaftrun ){
1002     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1003 mocchiut 1.8 if ( verbose ) printf(" Copying %u events in the file which are after the end of the run %u \n",(totfileentries-noaftrun),run);
1004     if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries);
1005 mocchiut 1.1 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1006     //
1007     // Get entry from old tree
1008     //
1009     caloclone->GetEntry(j);
1010     //
1011     // copy caclone to ca
1012     //
1013 mocchiut 1.13 if ( getl1 ) c1->Clear();
1014 mocchiut 1.6 ca->Clear();
1015 mocchiut 1.8 //
1016 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
1017 mocchiut 1.1 memcpy(&ca,&caclone,sizeof(caclone));
1018     //
1019     // Fill entry in the new tree
1020     //
1021     calo->Fill();
1022     //
1023     };
1024     if ( verbose ) printf(" Finished successful copying!\n");
1025     };
1026     };
1027     //
1028     // Case of no errors: close files, delete old tree(s), write and close level2 file
1029     //
1030     if ( l0File ) l0File->Close();
1031     if ( tempfile ) tempfile->Close();
1032     gSystem->Unlink(tempname.str().c_str());
1033     if ( tracker ) tracker->Delete();
1034     //
1035     if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n");
1036     if ( verbose ) printf("\n Writing and closing rootple\n");
1037     if ( runinfo ) runinfo->Close();
1038     if ( calo ) calo->SetName("Calorimeter");
1039     if ( file ){
1040     file->cd();
1041     file->Write();
1042     };
1043     if ( calo ) calo->Delete();
1044     //
1045     gSystem->Unlink(calofolder.str().c_str());
1046     //
1047 mocchiut 1.7 if ( ca ) delete ca;
1048     if ( caclone ) delete caclone;
1049 mocchiut 1.13 if ( c1 ) delete c1;
1050     if ( c1clone ) delete c1clone;
1051 mocchiut 1.7 if ( trk ) delete trk;
1052     if ( q4 ) delete q4;
1053     if ( glroot ) delete glroot;
1054     if ( runinfo ) delete runinfo;
1055     //
1056 mocchiut 1.1 // the end
1057     //
1058     if ( verbose ) printf("\n Exiting...\n");
1059     if ( code < 0 ) throw code;
1060     return(code);
1061     }
1062    
1063    
1064    

  ViewVC Help
Powered by ViewVC 1.1.23