/[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.16 - (hide annotations) (download)
Tue Jan 16 10:09:45 2007 UTC (17 years, 10 months ago) by pam-fi
Branch: MAIN
Changes since 1.15: +1 -0 lines
Set()

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

  ViewVC Help
Powered by ViewVC 1.1.23