/[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.12 - (hide annotations) (download)
Fri Nov 17 10:08:07 2006 UTC (18 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.11: +5 -0 lines
Added some checks on MySQL connection

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

  ViewVC Help
Powered by ViewVC 1.1.23