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

  ViewVC Help
Powered by ViewVC 1.1.23