/[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.23 - (hide annotations) (download)
Mon Mar 26 14:02:05 2007 UTC (17 years, 8 months ago) by mocchiut
Branch: MAIN
Changes since 1.22: +8 -2 lines
Bug in CaloStrip/Level0 fixed + new alignement parameters added

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     TString processFolder = "calorimeterFolder";
121     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     TBranch *l0head = 0;
278     TBranch *l0calo = 0;
279     TBranch *l0trig = 0;
280 mocchiut 1.8 pamela::EventHeader *eh = 0;
281     pamela::PscuHeader *ph = 0;
282 mocchiut 1.1 pamela::trigger::TriggerEvent *trig = 0;
283     //
284     // Define some basic variables
285     //
286 mocchiut 1.22 CaloLevel0 *event = new CaloLevel0(); // NOTICE: very important to call here the constructor!
287 mocchiut 1.21 event->SetCrossTalk(crosst);
288 mocchiut 1.1 stringstream file2;
289     stringstream file3;
290     stringstream qy;
291     // Bool_t imtrack = false;
292     Bool_t filled = false;
293     //
294 mocchiut 1.8 UInt_t caloevents = 0;
295 mocchiut 1.1 stringstream calfile;
296     stringstream aligfile;
297     //
298     Int_t i = -1;
299     Int_t itr = -1;
300     Int_t badevent = 0;
301     Int_t totevent = 0;
302     //
303 mocchiut 1.8 UInt_t atime = 0;
304 mocchiut 1.1 //
305     Int_t S3 = 0;
306     Int_t S2 = 0;
307     Int_t S12 = 0;
308     Int_t S11 = 0;
309     UInt_t re = 0;
310 mocchiut 1.9 UInt_t jumped = 0;
311 mocchiut 1.1 //
312     TString caloversion;
313     ItoRunInfo *runinfo = 0;
314 mocchiut 1.8 TArrayI *runlist = 0;
315 mocchiut 1.1 //
316     Float_t tmptrigty = -1.;
317     Int_t ntrkentry = 0;
318     GL_PARAM *q4 = new GL_PARAM();
319 mocchiut 1.8 UInt_t tttrkpar1 = 0;
320 mocchiut 1.1 Bool_t trkpar1 = true;
321     GL_ROOT *glroot = new GL_ROOT();
322 mocchiut 1.8 GL_TIMESYNC *dbtime = 0;
323 mocchiut 1.1 //
324 mocchiut 1.15 Long64_t maxsize = 10000000000LL;
325     TTree::SetMaxTreeSize(maxsize);
326 mocchiut 1.1 //
327     //
328     TFile *tempfile = 0;
329     TTree *tempcalo = 0;
330     stringstream tempname;
331     stringstream calofolder;
332     tempname.str("");
333     tempname << outdir;
334     tempname << "/" << processFolder.Data();
335     calofolder.str("");
336     calofolder << tempname.str().c_str();
337     gSystem->MakeDirectory(calofolder.str().c_str());
338     tempname << "/calotree_run";
339     tempname << run << ".root";
340     //
341     // 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
342     // if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file
343     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
344     //
345 mocchiut 1.8 if ( run == 0 ) reproc = true;
346 mocchiut 1.1 //
347     //
348     //
349     if ( !file->IsOpen() ){
350     if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n");
351     throw -101;
352     };
353     //
354 mocchiut 1.11 if ( withtrk ){
355     //
356     // Does it contain the Tracker tree?
357     //
358     tracker = (TTree*)file->Get("Tracker");
359     if ( !tracker ) {
360     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n");
361     code = -102;
362     goto closeandexit;
363     };
364     tracker->SetBranchAddress("TrkLevel2",&trk);
365     nevtrkl2 = tracker->GetEntries();
366 mocchiut 1.1 };
367     //
368     // Call runinfo
369     //
370     sgnl = 0;
371     runinfo = new ItoRunInfo(file);
372     //
373     // update versioning information and retrieve informations about the run to be processed
374     //
375     caloversion = CaloInfo(false);
376     sgnl = runinfo->Update(run,"CALO",caloversion);
377     //
378     if ( sgnl ){
379     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
380     code = sgnl;
381     goto closeandexit;
382     } else {
383     sgnl = 0;
384     };
385     //
386     // number of events in the file BEFORE the first event of our run
387     //
388     nobefrun = runinfo->GetFirstEntry();
389     //
390     // total number of events in the file
391     //
392     totfileentries = runinfo->GetFileEntries();
393     //
394     // first file entry AFTER the last event of our run
395     //
396     noaftrun = runinfo->GetLastEntry() + 1;
397     //
398     // number of run to be processed
399     //
400     numbofrun = runinfo->GetNoRun();
401     //
402     // number of runs in the file
403     //
404     totnorun = runinfo->GetRunEntries();
405     //
406     // Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it.
407     //
408     caloclone = (TTree*)file->Get("Calorimeter");
409     //
410     if ( !caloclone ){
411     reproc = false;
412 mocchiut 1.8 if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n");
413     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");
414 mocchiut 1.1 //
415     } else {
416     //
417 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
418 mocchiut 1.1 reproc = true;
419     //
420     if ( verbose ) printf("\n Preparing the pre-processing...\n");
421     //
422 mocchiut 1.8 if ( run == 0 ){
423 mocchiut 1.1 //
424     // 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.
425     //
426     if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n");
427     reprocall = true;
428     //
429     } else {
430     //
431     // we are reprocessing a single run
432     //
433 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run);
434 mocchiut 1.1 reprocall = false;
435     //
436     //
437     //
438     tempfile = new TFile(tempname.str().c_str(),"RECREATE");
439     tempcalo = caloclone->CloneTree(-1,"fast");
440     tempcalo->SetName("Calorimeter-old");
441     tempfile->Write();
442     tempfile->Close();
443     };
444     //
445     // delete old tree
446     //
447     caloclone->Delete("all");
448     //
449     if ( verbose ) printf("\n ...done!\n");
450     //
451     };
452     //
453     // create calorimeter tree calo
454     //
455     file->cd();
456     calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data");
457 mocchiut 1.15 calo->SetAutoSave(900000000000000LL);
458 pam-fi 1.16 ca->Set();
459 mocchiut 1.1 calo->Branch("CaloLevel2","CaloLevel2",&ca);
460 mocchiut 1.13 if ( getl1 ) calo->Branch("CaloLevel1","CaloLevel1",&c1);
461 mocchiut 1.1 //
462     if ( reproc && !reprocall ){
463     //
464     //
465     //
466     tempfile = new TFile(tempname.str().c_str(),"READ");
467     caloclone = (TTree*)tempfile->Get("Calorimeter-old");
468 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
469 mocchiut 1.1 caloclone->SetBranchAddress("CaloLevel2",&caclone);
470 mocchiut 1.13 TBranch *havel1 = caloclone->GetBranch("CaloLevel1");
471     if ( !havel1 && getl1 ) throw -117;
472     if ( havel1 && !getl1 ) throw -118;
473     if ( getl1 ) caloclone->SetBranchAddress("CaloLevel1",&c1clone);
474 mocchiut 1.1 //
475     if ( nobefrun > 0 ){
476     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
477 mocchiut 1.8 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
478 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
479     for (UInt_t j = 0; j < nobefrun; j++){
480     //
481     caloclone->GetEntry(j);
482     //
483     // copy caclone to ca
484     //
485 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
486 mocchiut 1.1 memcpy(&ca,&caclone,sizeof(caclone));
487     //
488     // Fill entry in the new tree
489     //
490     calo->Fill();
491     //
492 mocchiut 1.13 if ( getl1 ) c1->Clear();
493 mocchiut 1.6 ca->Clear();
494     //
495 mocchiut 1.1 };
496     if ( verbose ) printf(" Finished successful copying!\n");
497     };
498     };
499     //
500     // Get the list of run to be processed
501     //
502     runlist = runinfo->GetRunList();
503     //
504     // Loop over the run to be processed
505     //
506     for (UInt_t irun=0; irun < numbofrun; irun++){
507     //
508     badevent = 0;
509     //
510     idRun = runlist->At(irun);
511     if ( verbose ) printf("\n\n\n ####################################################################### \n");
512 mocchiut 1.8 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
513 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
514     //
515     sgnl = runinfo->GetRunInfo(idRun);
516     if ( sgnl ){
517     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
518     code = sgnl;
519     goto closeandexit;
520     } else {
521     sgnl = 0;
522     };
523 mocchiut 1.8 id_reg_run = runinfo->ID_ROOT_L0;
524 mocchiut 1.1 runheadtime = runinfo->RUNHEADER_TIME;
525     runtrailtime = runinfo->RUNTRAILER_TIME;
526 mocchiut 1.8 evfrom = runinfo->EV_FROM;
527     evto = runinfo->EV_TO;
528 mocchiut 1.1 //
529     if ( id_reg_run == -1 ){
530 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
531 mocchiut 1.1 code = -5;
532     goto closeandexit;
533     };
534     //
535 mocchiut 1.8 // prepare the timesync for the db
536     //
537 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
538 mocchiut 1.8 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
539     //
540 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
541     //
542 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
543 mocchiut 1.8 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
544 mocchiut 1.1 //
545     ftmpname.str("");
546     ftmpname << glroot->PATH.Data() << "/";
547     ftmpname << glroot->NAME.Data();
548     fname = ftmpname.str().c_str();
549     //
550     // print out informations
551     //
552 mocchiut 1.8 totevent = runinfo->NEVENTS;
553 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
554 mocchiut 1.8 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime);
555     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime);
556     if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent);
557 mocchiut 1.1 //
558     // Open Level0 file
559     //
560     l0File = new TFile(fname.Data());
561     if ( !l0File ) {
562     if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n");
563     code = -6;
564     goto closeandexit;
565     };
566     l0tr = (TTree*)l0File->Get("Physics");
567     if ( !l0tr ) {
568     if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n");
569     l0File->Close();
570     code = -7;
571     goto closeandexit;
572     };
573     l0head = l0tr->GetBranch("Header");
574     if ( !l0head ) {
575     if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n");
576     l0File->Close();
577     code = -8;
578     goto closeandexit;
579     };
580     l0calo = l0tr->GetBranch("Calorimeter");
581     if ( !l0calo ) {
582     if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n");
583     l0File->Close();
584     code = -103;
585     goto closeandexit;
586     };
587     l0trig = l0tr->GetBranch("Trigger");
588     if ( !l0trig ) {
589     if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n");
590     l0File->Close();
591     code = -104;
592     goto closeandexit;
593     };
594     //
595     l0tr->SetBranchAddress("Trigger", &trig);
596 mocchiut 1.8 l0tr->SetBranchAddress("Header", &eh);
597 mocchiut 1.1 //
598     // Construct the event object, look for the calibration which include the first header
599     //
600     sgnl = 0;
601     if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n");
602 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
603 mocchiut 1.1 event->ProcessingInit(dbc,runheadtime,sgnl,l0tr,debug,verbose);
604     if ( verbose ) printf("\n");
605     if ( sgnl == 100 ) {
606     code = sgnl;
607     if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n");
608     sgnl = 0;
609     };
610     if ( sgnl ){
611     l0File->Close();
612     code = sgnl;
613     goto closeandexit;
614     };
615     //
616     qy.str("");
617     //
618 mocchiut 1.8 nevents = l0head->GetEntries();
619 mocchiut 1.1 caloevents = l0calo->GetEntries();
620     //
621     if ( nevents < 1 ) {
622     if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n");
623     l0File->Close();
624     code = -11;
625     goto closeandexit;
626     };
627     //
628     if ( evto > nevents-1 ) {
629     if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n");
630     l0File->Close();
631     code = -12;
632     goto closeandexit;
633     };
634     //
635     // Check if we have to load parameter files
636     //
637     sgnl = 0;
638 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
639 mocchiut 1.23 sgnl = event->ChkParam(dbc,runheadtime,mechal); // calorimeter parameter files
640 mocchiut 1.1 if ( sgnl < 0 ){
641     code = sgnl;
642     l0File->Close();
643     goto closeandexit;
644     };
645     //
646 mocchiut 1.11 if ( withtrk ){
647     if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){
648     trkpar1 = false;
649 mocchiut 1.12 if ( !dbc->IsConnected() ) throw -116;
650 mocchiut 1.11 Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc);
651     if ( glpar < 0 ){
652     code = glpar;
653     goto closeandexit;
654     };
655     tttrkpar1 = q4->TO_TIME;
656     // ----------------------------
657     // Read the magnetic field
658     // ----------------------------
659     if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
660     trk->LoadField(q4->PATH+q4->NAME);
661     if ( verbose ) printf("\n");
662 mocchiut 1.4 };
663 mocchiut 1.1 };
664     //
665     // run over all the events of the run
666     //
667     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
668     //
669 mocchiut 1.8 //
670 mocchiut 1.19 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
671     //for ( re = 46438+200241; re < 46441+200241; re++){
672 mocchiut 1.18 // printf(" i = %i \n",re-200241);
673 mocchiut 1.1 //
674     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
675     //
676 mocchiut 1.8 l0head->GetEntry(re);
677 mocchiut 1.1 //
678     // absolute time of this event
679     //
680 mocchiut 1.8 ph = eh->GetPscuHeader();
681     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
682 mocchiut 1.1 //
683     //
684     //
685 mocchiut 1.8 if ( re > caloevents-1 ){
686 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i);
687     l0File->Close();
688     code = -112;
689     goto closeandexit;
690     };
691     //
692     if ( atime > runtrailtime || atime < runheadtime ) {
693     if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n");
694 mocchiut 1.9 jumped++;
695 mocchiut 1.1 goto jumpev;
696     };
697     //
698     // retrieve tracker informations
699     //
700     if ( !reprocall ){
701 mocchiut 1.19 itr = nobefrun + (re - evfrom - jumped);
702     //itr = re-(46438+200241);
703 mocchiut 1.1 } else {
704 mocchiut 1.9 itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
705 mocchiut 1.1 };
706 mocchiut 1.11 //
707     if ( withtrk ){
708     if ( itr > nevtrkl2 ){
709     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
710     l0File->Close();
711     code = -113;
712     goto closeandexit;
713     };
714     //
715     trk->Clear();
716     //
717     tracker->GetEntry(itr);
718     //
719 mocchiut 1.1 };
720 mocchiut 1.10 //
721 mocchiut 1.1 procev++;
722     //
723     // start processing
724     //
725 mocchiut 1.13 if ( getl1 ) c1->Clear();
726 mocchiut 1.6 ca->Clear();
727 mocchiut 1.1 //
728     // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
729     //
730 mocchiut 1.8 l0trig->GetEntry(re);
731 mocchiut 1.1 S3 = 0;
732     S2 = 0;
733     S12 = 0;
734     S11 = 0;
735     S3 = trig->patterntrig[2];
736     S2 = trig->patterntrig[3];
737     S12 = trig->patterntrig[4];
738     S11 = trig->patterntrig[5];
739 mocchiut 1.17 if ( trig->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
740 mocchiut 1.1 if ( trig->patterntrig[0] ) tmptrigty = 2.;
741     if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
742 mocchiut 1.17 if ( !(trig->patterntrig[1] & (1<<0)) && !trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
743 mocchiut 1.1 event->clevel2->trigty = tmptrigty;
744     //
745     // check if the calibration we are using is still good, if not load another calibration
746     //
747     sgnl = 0;
748     sgnl = event->ChkCalib(dbc,atime);
749     if ( sgnl < 0 ){
750     code = sgnl;
751     goto closeandexit;
752     };
753     if ( sgnl == 100 ){
754     code = sgnl;
755     if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n");
756     badevent++;
757     sgnl = 0;
758     };
759     //
760 mocchiut 1.11 // do we have at least one track from the tracker? this check has been disabled
761 mocchiut 1.1 //
762     event->clevel1->good2 = 1;
763 mocchiut 1.8 //
764 mocchiut 1.1 //
765 mocchiut 1.8 // Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine
766 mocchiut 1.1 //
767 mocchiut 1.11 event->Calibrate(re);
768 mocchiut 1.1 //
769     // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables.
770     //
771     //
772     // Calculate variables common to all tracks (qtot, nstrip, etc.)
773     //
774     event->GetCommonVar();
775     //
776     // Fill common variables
777     //
778 mocchiut 1.13 event->FillCommonVar(c1,ca);
779 mocchiut 1.1 //
780     // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
781     //
782     ntrkentry = 0;
783     //
784     filled = false;
785     //
786 mocchiut 1.11 // Run over tracks (tracker or calorimeter )
787     //
788     if ( withtrk ){
789     for(Int_t nt=0; nt < trk->ntrk(); nt++){
790     //
791     event->clevel1->good2 = 1;
792     //
793     TrkTrack *ptt = trk->GetStoredTrack(nt);
794     //
795     event->clevel1->trkchi2 = 0;
796     //
797     // Copy the alpha vector in the input structure
798     //
799     for (Int_t e = 0; e < 5 ; e++){
800     event->clevel1->al_p[e][0] = ptt->al[e];
801     };
802     //
803     // Get tracker related variables for this track
804     //
805     event->GetTrkVar();
806     //
807     // Save tracker track sequence number
808     //
809     event->trkseqno = nt;
810     //
811     // Copy values in the class ca from the structure clevel2
812     //
813     event->FillTrkVar(ca,ntrkentry);
814     ntrkentry++;
815     filled = true;
816     //
817     }; // loop on all the tracks
818     };
819     //
820     // 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
821     // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
822     // here we make an event selection so it must be done very carefully...
823     //
824     // 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
825     //
826     if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
827     if ( debug ) printf(" Event with a track not fitted by the tracker at entry %i \n",itr);
828     //
829     // Disable "track mode" in the fortran routine
830     //
831     event->clevel1->good2 = 0;
832     event->clevel1->riginput = rigdefault;
833     if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
834     //
835     // We have a selftrigger event to analyze.
836     //
837     for (Int_t e = 0; e < 5 ; e++){
838     event->clevel1->al_p[e][0] = 0.;
839     event->clevel1->al_p[e][1] = 0.;
840     };
841     event->clevel1->trkchi2 = 0;
842     //
843     event->GetTrkVar();
844     //
845     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
846 mocchiut 1.1 //
847 mocchiut 1.11 if ( event->clevel1->good2 == 0 ) {
848 mocchiut 1.1 //
849 mocchiut 1.11 // In selftrigger mode the trkentry variable is set to -1
850 mocchiut 1.1 //
851 mocchiut 1.11 event->trkseqno = -3;
852 mocchiut 1.1 //
853 mocchiut 1.11 // Copy values in the class ca from the structure clevel2
854 mocchiut 1.1 //
855 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
856     ntrkentry++;
857     filled = true;
858     //
859     } else {
860     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
861     };
862     //
863     };
864     //
865     // Call high energy nuclei routine
866     //
867     if ( hZn && event->clevel2->trigty >= 2. ){
868     if ( debug ) printf(" Calling selftrigger high energy nuclei routine for entry %i \n",itr);
869     //
870     // Disable "track mode" in the fortran routine
871     //
872     event->clevel1->good2 = 0;
873     //
874     // Set high energy nuclei flag to one
875     //
876     event->clevel1->hzn = 1;
877     event->clevel1->riginput = rigdefault;
878     //
879     // We have a selftrigger event to analyze.
880     //
881     for (Int_t e = 0; e < 5 ; e++){
882     event->clevel1->al_p[e][0] = 0.;
883     event->clevel1->al_p[e][1] = 0.;
884 mocchiut 1.1 };
885 mocchiut 1.11 event->clevel1->trkchi2 = 0;
886     //
887     event->GetTrkVar();
888     //
889     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
890     //
891     if ( event->clevel1->good2 == 0 ) {
892     //
893     // In selftrigger mode the trkentry variable is set to -1
894     //
895     event->trkseqno = -2;
896     //
897     // Copy values in the class ca from the structure clevel2
898 mocchiut 1.1 //
899 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
900     ntrkentry++;
901     filled = true;
902 mocchiut 1.1 //
903 mocchiut 1.11 } else {
904     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
905     };
906     //
907     };
908     //
909     // self trigger event
910     //
911     if ( st && event->clevel2->trigty >= 2. ){
912     if ( verbose ) printf(" Selftrigger event at entry %i \n",itr);
913     //
914     // Disable "track mode" in the fortran routine
915     //
916     event->clevel1->good2 = 0;
917     //
918     // disable high enery nuclei flag;
919     //
920     event->clevel1->hzn = 0;
921     //
922     // We have a selftrigger event to analyze.
923     //
924     for (Int_t e = 0; e < 5 ; e++){
925     event->clevel1->al_p[e][0] = 0.;
926     event->clevel1->al_p[e][1] = 0.;
927     };
928     event->clevel1->trkchi2 = 0;
929     //
930     event->GetTrkVar();
931     //
932     // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
933     //
934     if ( event->clevel1->good2 == 0 ) {
935 mocchiut 1.1 //
936 mocchiut 1.11 // In selftrigger mode the trkentry variable is set to -1
937 mocchiut 1.1 //
938 mocchiut 1.11 event->trkseqno = -1;
939 mocchiut 1.1 //
940 mocchiut 1.11 // Copy values in the class ca from the structure clevel2
941 mocchiut 1.1 //
942 mocchiut 1.11 event->FillTrkVar(ca,ntrkentry);
943     ntrkentry++;
944     filled = true;
945 mocchiut 1.1 //
946 mocchiut 1.11 } else {
947     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
948 mocchiut 1.1 };
949     };
950     //
951     if ( !filled ) badevent++;
952     //
953     // Clear structures used to communicate with fortran
954     //
955     event->ClearStructs();
956     //
957     // Fill the rootple
958     //
959     calo->Fill();
960     //
961 mocchiut 1.6 // delete ca;
962 mocchiut 1.1 //
963     jumpev:
964 mocchiut 1.23 if ( !debug ) debug = false;
965 mocchiut 1.1 //
966     };
967     //
968     if ( verbose ) printf("\n SUMMARY:\n");
969     if ( verbose ) printf(" Total number of events: %i \n",totevent);
970     if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent);
971     if ( verbose ) printf(" Events without tracks: %i \n",badevent);
972     //
973     if ( badevent == totevent ){
974 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun);
975 mocchiut 1.1 code = 101;
976     };
977     //
978 mocchiut 1.8 delete dbtime;
979 mocchiut 1.1 //
980     // Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software).
981     //
982     event->RunClose();
983     if ( l0File ) l0File->Close();
984     //
985     }; // process all the runs
986     //
987     if ( verbose ) printf("\n Finished processing data \n");
988     //
989     closeandexit:
990     //
991     if ( !reprocall && reproc && code >= 0 ){
992     if ( totfileentries > noaftrun ){
993     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
994 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);
995     if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries);
996 mocchiut 1.1 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
997     //
998     // Get entry from old tree
999     //
1000     caloclone->GetEntry(j);
1001     //
1002     // copy caclone to ca
1003     //
1004 mocchiut 1.13 if ( getl1 ) c1->Clear();
1005 mocchiut 1.6 ca->Clear();
1006 mocchiut 1.8 //
1007 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
1008 mocchiut 1.1 memcpy(&ca,&caclone,sizeof(caclone));
1009     //
1010     // Fill entry in the new tree
1011     //
1012     calo->Fill();
1013     //
1014     };
1015     if ( verbose ) printf(" Finished successful copying!\n");
1016     };
1017     };
1018     //
1019     // Case of no errors: close files, delete old tree(s), write and close level2 file
1020     //
1021     if ( l0File ) l0File->Close();
1022     if ( tempfile ) tempfile->Close();
1023     gSystem->Unlink(tempname.str().c_str());
1024     if ( tracker ) tracker->Delete();
1025     //
1026     if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n");
1027     if ( verbose ) printf("\n Writing and closing rootple\n");
1028     if ( runinfo ) runinfo->Close();
1029     if ( calo ) calo->SetName("Calorimeter");
1030     if ( file ){
1031     file->cd();
1032     file->Write();
1033     };
1034     if ( calo ) calo->Delete();
1035     //
1036     gSystem->Unlink(calofolder.str().c_str());
1037     //
1038 mocchiut 1.7 if ( ca ) delete ca;
1039     if ( caclone ) delete caclone;
1040 mocchiut 1.13 if ( c1 ) delete c1;
1041     if ( c1clone ) delete c1clone;
1042 mocchiut 1.7 if ( trk ) delete trk;
1043     if ( q4 ) delete q4;
1044     if ( glroot ) delete glroot;
1045     if ( runinfo ) delete runinfo;
1046     //
1047 mocchiut 1.1 // the end
1048     //
1049     if ( verbose ) printf("\n Exiting...\n");
1050     if ( code < 0 ) throw code;
1051     return(code);
1052     }
1053    
1054    
1055    

  ViewVC Help
Powered by ViewVC 1.1.23