/[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.53 - (hide annotations) (download)
Tue Nov 5 14:02:07 2013 UTC (11 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.52: +236 -180 lines
New code for retracking

1 mocchiut 1.53 /**
2     * \file CaloCore.cpp
3     * \author Emiliano Mocchiutti
4     */
5 mocchiut 1.1 //
6     // Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti
7     //
8 mocchiut 1.3 // CaloCore.cxx version 3.05 (2006-05-30)
9 mocchiut 1.1 //
10     // The only input needed is the path to the directory created by YODA for the data file you want to analyze.
11     //
12     // Changelog:
13 mocchiut 1.3 //
14 mocchiut 1.53 // 7.00 (2013-11): Code to call this routine for level1 data only added.
15     //
16 mocchiut 1.11 // 3.08 (2006-11-13): Added high energy nuclei capability and "process all events" capability.
17     //
18 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.
19 mocchiut 1.2 //
20     // 3.03 - 3.04 (2006-05-23): Forgot to put impx and impy in the PAMELA reference system, fixed.
21 mocchiut 1.1 //
22     // 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.
23     //
24     // 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.
25     // Added variable planemax[2], plane of maximum energy release (x and y) in final output. Use ItoRunInfo instead of RunInfo.
26     //
27     // 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
28     // small bug in fortran routines
29     //
30     // 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
31     // working class "CaloProcessing", linked to the preliminary tracker flight software v0r00, reads YODA unique output files,
32     // reduced the number of installed libraries, F77 programs splitted depending on the function contained, introduced the readout and
33     // processing of self-trigger events, if the tracker provides more than one track all calorimeter track-related variables are saved
34     // as many times as the number of tracks (via the TClonesArray object in the level2 rootple) and many other small changes.
35     //
36     // 2.00 - 2.01 (2006-01-26): bug: wrong calculation of baselines in some cases, fixed.
37     //
38     // 1.00 - 2.00 (2006-01-11): use TSQL ROOT classes instead of directly calling MySQL.
39     //
40     // 0.00 - 1.00 (2005-09-14): seems working.
41     //
42     // 0.00 (2005-09-09): clone of CaloLEVEL2.c .
43     //
44     // C/C++ headers
45     //
46     #include <fstream>
47     #include <string.h>
48     //
49     // ROOT headers
50     //
51     #include <TTree.h>
52     #include <TClassEdit.h>
53     #include <TObject.h>
54     #include <TList.h>
55 mocchiut 1.8 #include <TArrayI.h>
56 mocchiut 1.1 #include <TSystem.h>
57     #include <TSystemDirectory.h>
58     #include <TString.h>
59     #include <TFile.h>
60     #include <TClass.h>
61     #include <TCanvas.h>
62     #include <TH1.h>
63     #include <TH1F.h>
64     #include <TH2D.h>
65     #include <TLatex.h>
66     #include <TPad.h>
67     #include <TSQLServer.h>
68     #include <TSQLRow.h>
69     #include <TSQLResult.h>
70     #include <TClonesArray.h>
71     #include <TStreamerInfo.h>
72     //
73 mocchiut 1.20 // This program headers
74     //
75     #include <RunInfo.h>
76     //
77 mocchiut 1.1 // YODA headers
78     //
79     #include <PamelaRun.h>
80     #include <physics/trigger/TriggerEvent.h>
81     //
82     // This program headers
83     //
84     #include <CaloCore.h>
85 mocchiut 1.13 #include <CaloLevel1.h>
86 mocchiut 1.1 #include <CaloLevel2.h>
87 mocchiut 1.22 #include <CaloLevel0.h>
88 mocchiut 1.1 #include <CaloVerl2.h>
89     //
90     // Tracker classes headers and definitions
91     //
92     #include <TrkLevel2.h>
93     //
94     using namespace std;
95     //
96 mocchiut 1.47 // IsCaloSelfTrigger method from Nicola (adapted)
97     //
98     //===============================================================================
99     // The IsCaloSelfTrigger functions checks in the trigger pattern register
100     // whether the event has been triggered by the calorimeter or not.
101     // Since the calorimeter self trigger coincidences are slower than both
102     // TOF and S4 triggers the triggering condition is checked for these detectors
103     // before looking for a calorimeter coincidence
104     //===============================================================================
105     //Int_t IsCaloSelfTrigger(TrigLevel2* event)
106     Int_t IsCaloSelfTrigger(UInt_t tc, UInt_t *pt)
107     {
108     // UInt_t tc, *pt;
109     bool S1or, S2or, S3or;
110     bool S1and, S2and, S3and;
111    
112     // tc = event->trigconf;
113     // pt = event->patterntrig;
114    
115     if (tc & CCALIB_ON) return false; // calibration trigger
116    
117     S1or = S11(pt) || S12(pt);
118     S2or = S21(pt) || S22(pt);
119     S3or = S31(pt) || S32(pt);
120     S1and = S11(pt) && S12(pt);
121     S2and = S21(pt) && S22(pt);
122     S3and = S31(pt) && S32(pt);
123    
124     switch (tc & ~(CCALO | CS4 | CCALIB_ON)) {
125     case CTOF1:
126     if (S1or && S2or && S3or) return 0; // TOF1 trigger
127     break;
128     case CTOF2:
129     if (S1and && S2and && S3and) return 0; // TOF2 trigger
130     break;
131     case CTOF3:
132     if (S2or && S3or) return 0; // TOF3 trigger
133     break;
134     case CTOF4:
135     if (S2and && S3and) return 0; // TOF4 trigger
136     break;
137     case CTOF5:
138     if (S12(pt) && S2and) return 0; // TOF5 trigger
139     break;
140     case CTOF6:
141     if (S1or && S3or) return 0; // TOF6 trigger
142     break;
143     case CTOF7:
144     if (S1and && S3and) return 0; // TOF7 trigger
145     break;
146     default: // other trigger configuration
147     break;
148     }
149    
150     if ((tc & CS4) && PS4(pt)) return 2; // S4 trigger
151    
152     if (PCaloST(pt)) return 2; // calorimeter trigger
153    
154     return 2; // nobody gernerated the trigger?
155     }
156    
157     //
158 mocchiut 1.1 // CORE ROUTINE
159     //
160 mocchiut 1.32 int CaloCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t calargc, char *calargv[]){
161 mocchiut 1.1 //
162     // Set these to true to have a very verbose output.
163     //
164     Bool_t verbose = false;
165     Bool_t debug = false;
166     //
167 mocchiut 1.21 Bool_t crosst = true;
168 mocchiut 1.36 Bool_t ctground = false;
169 mocchiut 1.41 Bool_t usetable = true;
170     Bool_t noselfct = false;
171 mocchiut 1.21 //
172 mocchiut 1.11 Bool_t trackanyway = true;
173     //
174     Float_t rigdefault = 50.;
175 mocchiut 1.35 //
176 mocchiut 1.11 Bool_t hZn = true;
177     //
178     Bool_t withtrk = true;
179     //
180     Bool_t st = true;
181     //
182 mocchiut 1.13 Bool_t getl1 = true;
183     //
184 mocchiut 1.23 Bool_t mechal = false;
185     //
186 mocchiut 1.30 Bool_t checksimu = true;
187     //
188 mocchiut 1.47 Bool_t mask18 = false;
189     //
190 mocchiut 1.53 Bool_t cl1only = false;
191     //
192     Bool_t froml1 = false;
193     //
194     Bool_t delct = false;
195     //
196 mocchiut 1.1 // Output directory is the working directoy.
197     //
198     const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
199     //
200     Int_t ri = 0;
201 mocchiut 1.26 TString processFolder = Form("calorimeterFolder_%u",run);
202 mocchiut 1.1 if ( calargc > 0 ){
203     ri = 0;
204     while ( ri < calargc ){
205     if ( !strcmp(calargv[ri],"-processFolder") ) {
206     if ( calargc < ri+1 ){
207     throw -3;
208     };
209     processFolder = (TString)calargv[ri+1];
210     ri++;
211     };
212     if ( !strcmp(calargv[ri],"-v") || !strcmp(calargv[ri],"--verbose") ) {
213     verbose = true;
214     };
215     if ( !strcmp(calargv[ri],"-g") || !strcmp(calargv[ri],"--debug") ) {
216 mocchiut 1.13 verbose = true;
217 mocchiut 1.1 debug = true;
218     };
219 mocchiut 1.11 if ( !strcmp(calargv[ri],"--alltracks") ) {
220     trackanyway = true;
221     };
222 mocchiut 1.23 if ( !strcmp(calargv[ri],"--use-default-alig") ) {
223     mechal = true;
224     };
225 mocchiut 1.21 if ( !strcmp(calargv[ri],"--no-crosstalk") ) {
226     crosst = false;
227     };
228 mocchiut 1.47 if ( !strcmp(calargv[ri],"--mask-plane18X") ) {
229     mask18 = true;
230     };
231     if ( !strcmp(calargv[ri],"--unmask-plane18X") ) {
232     mask18 = false;
233     };
234 mocchiut 1.34 if ( !strcmp(calargv[ri],"--flight-crosstalk") ) {
235     ctground = false;
236     };
237 mocchiut 1.41 if ( !strcmp(calargv[ri],"--ct-use-pulse") ) {
238     usetable = false;
239     };
240     if ( !strcmp(calargv[ri],"--ct-use-table") ) {
241     usetable = true;
242     };
243 mocchiut 1.34 if ( !strcmp(calargv[ri],"--ground-crosstalk") ) {
244     ctground = true;
245     };
246 mocchiut 1.41 if ( !strcmp(calargv[ri],"--no-self-crosstalk") ) {
247     noselfct = true;
248     };
249 mocchiut 1.11 if ( !strcmp(calargv[ri],"--no-tracker") ) {
250     withtrk = false;
251     };
252     if ( !strcmp(calargv[ri],"--with-tracker") ) {
253     withtrk = true;
254     };
255     if ( !strcmp(calargv[ri],"--defrig") ) {
256     if ( calargc < ri+1 ){
257     throw -3;
258     };
259     rigdefault = atof(calargv[ri+1]);
260     ri++;
261     };
262     if ( !strcmp(calargv[ri],"--no-alltracks") ) {
263     trackanyway = false;
264     };
265     if ( !strcmp(calargv[ri],"--highZnuclei") ) {
266     hZn = true;
267     };
268     if ( !strcmp(calargv[ri],"--no-highZnuclei") ) {
269     hZn = false;
270     };
271     if ( !strcmp(calargv[ri],"--selftrigger") ) {
272     st = true;
273     };
274     if ( !strcmp(calargv[ri],"--no-selftrigger") ) {
275     st = false;
276     };
277 mocchiut 1.13 if ( !strcmp(calargv[ri],"--no-level1") ) {
278     getl1 = false;
279 mocchiut 1.53 cl1only = false;
280     };
281     if ( !strcmp(calargv[ri],"--delete-calo-tree") ) {
282     delct = true;
283 mocchiut 1.13 };
284 mocchiut 1.41 if ( !strcmp(calargv[ri],"--level1") ) {
285     getl1 = true;
286     };
287 mocchiut 1.30 if ( !strcmp(calargv[ri],"--ignore-h20") ) {
288     checksimu = false;
289     };
290 mocchiut 1.53 if ( !strcmp(calargv[ri],"--level1-only") ) {
291     cl1only = true;
292     withtrk = false;
293     getl1 = true;
294     }
295     if ( !strcmp(calargv[ri],"--from-level1") ) {
296     froml1 = true;
297     }
298 mocchiut 1.11 if ( !strcmp(calargv[ri],"--help") ) {
299     printf("\n\n CALORIMETER HELP CALLED\n\n");
300     printf(" CaloCore options: \n");
301     printf(" -v | --verbose be verbose\n");
302     printf(" -g | --debug be really verbose\n");
303     printf(" --defrig rig rig is the default rigidity in GV to be used to\n");
304     printf(" obtain calorimeter variables in the routines\n");
305     printf(" \"alltracks\" and \"higZnuclei\" [default = 50]\n");
306     printf(" --alltracks fill the track related variables even in the case\n");
307     printf(" of no tracks from tracker and no selftrigger event\n");
308 mocchiut 1.14 printf(" when we have a calorimeter fit for both views [default]\n");
309     printf(" --no-alltracks fill the track related variables only in the case\n");
310     printf(" of a good track from tracker or selftrigger\n");
311 mocchiut 1.11 printf(" --highZnuclei call the routine to analyze high Z nuclei\n");
312     printf(" selftrigger events [default]\n");
313     printf(" --no-highZnuclei do not call the routine to analyze high Z nuclei\n");
314     printf(" selftrigger events\n");
315     printf(" --no-tracker do not use tracker level2\n");
316     printf(" --with-tracker use tracker level2 [default]\n");
317 mocchiut 1.21 printf(" --no-crosstalk do not apply crosstalk corrections\n");
318 mocchiut 1.37 printf(" --ground-crosstalk apply ground crosstalk corrections\n");
319     printf(" --flight-crosstalk apply flight crosstalk corrections [default]\n");
320 mocchiut 1.41 printf(" --no-self-crosstalk do not apply preamplifiers crosstalk corrections to the strip itself\n");
321     printf(" --ct--use-pulse flight crosstalk corrections from pulse calibrations \n");
322     printf(" --ct--use-table flight crosstalk corrections from calibration table [default] \n");
323 mocchiut 1.11 printf(" --selftrigger process selftrigger events [default]\n");
324     printf(" --no-selftrigger skip selftrigger events\n");
325 mocchiut 1.53 printf(" --delete-calo-tree remove the ''Calorimeter'' TTree if found in the file - could cause fatal problems when reprocessing, use it with care!\n");
326     printf(" --level1 save Level1 TBranch [default] \n");
327 mocchiut 1.13 printf(" --no-level1 do not save Level1 TBranch\n");
328 mocchiut 1.53 printf(" --level1-only process level1 only (force --level1 flag)\n");
329     printf(" --from-level1 process level2 data starting from level1 already saved in the file\n");
330 mocchiut 1.30 printf(" --ignore-h20 do not check if it is a file from simulations\n");
331 mocchiut 1.53 printf(" --unmask-plane18X process plane 18X as well [default]\n");
332     printf(" --mask-plane18X do not process plane 18X\n");
333 mocchiut 1.27 printf(" --use-default-alig use default mechanical alignement (defined in CaloLevel1.h)\n");
334 mocchiut 1.11 throw -114;
335     };
336 mocchiut 1.1 ri++;
337     };
338     };
339     //
340 mocchiut 1.13 if ( verbose ){
341     printf("\n");
342     if ( getl1 ) printf(" Saving calorimeter level1 data \n");
343 mocchiut 1.53 if ( cl1only ) printf(" Saving calorimeter level1 data ONLY \n");
344     if ( froml1 ) printf(" Using level1 data saved in the file\n");
345 mocchiut 1.13 if ( st ) printf(" Calling selftrigger subroutine \n");
346     if ( hZn ) printf(" Calling high energy nuclei subroutine \n");
347     if ( trackanyway ) printf(" Filling track related variables for all the possible tracks \n");
348     if ( hZn || trackanyway ) printf(" => default assumed rigidity %f \n",rigdefault);
349     if ( withtrk ) printf(" Using tracker level2 data \n");
350 mocchiut 1.27 if ( mechal ) printf(" Using default mechanical alignement (defined in CaloLevel1.h)\n");
351 mocchiut 1.34 if ( crosst ){
352     printf(" Applying cross-talk corrections \n");
353     if ( ctground ){
354     printf(" => Using ground cross-talk coefficients \n");
355     } else {
356 mocchiut 1.41 printf(" => Using flight cross-talk coefficients... \n");
357     };
358     if ( usetable ){
359     printf(" ... coming from tables \n");
360     } else {
361     printf(" ... coming from pulse calibrations \n");
362 mocchiut 1.34 };
363     } else {
364     printf(" Do not applying cross-talk corrections \n");
365     };
366     if ( !checksimu ) printf(" Check on h20 TTree in level2 file skipped \n");
367 mocchiut 1.13 printf("\n");
368     };
369 mocchiut 1.11 //
370 mocchiut 1.1 // Working filename
371     //
372     TString outputfile;
373     stringstream name;
374     name.str("");
375     name << outdir << "/";
376     //
377     // Variables.
378     //
379     TTree *tracker = 0;
380     TTree *calo = 0;
381     TTree *caloclone = 0;
382     Bool_t reproc = false;
383     Bool_t reprocall = false;
384 mocchiut 1.8 UInt_t nevents = 0;
385 mocchiut 1.1 UInt_t nobefrun = 0;
386     UInt_t noaftrun = 0;
387     UInt_t numbofrun = 0;
388 mocchiut 1.8 UInt_t totnorun = 0;
389 mocchiut 1.1 //
390     Int_t code = 0;
391     Int_t sgnl;
392     //
393     // calorimeter level2 classes
394     //
395 mocchiut 1.13 CaloLevel1 *c1 = 0;
396     CaloLevel1 *c1clone = 0;
397     if ( getl1 ){
398     c1 = new CaloLevel1();
399     c1clone = new CaloLevel1();
400     };
401     //
402     // calorimeter level2 classes
403     //
404 mocchiut 1.1 CaloLevel2 *ca = new CaloLevel2();
405     CaloLevel2 *caclone = new CaloLevel2();
406     //
407     TrkLevel2 *trk = new TrkLevel2();
408 mocchiut 1.8 Int_t nevtrkl2 = 0;
409 mocchiut 1.1 //
410     UInt_t procev = 0;
411     //
412 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).
413 mocchiut 1.1 //
414 mocchiut 1.8 UInt_t runheadtime = 0;
415     UInt_t runtrailtime = 0;
416 mocchiut 1.1 UInt_t evfrom = 0;
417     UInt_t evto = 0;
418 mocchiut 1.8 UInt_t totfileentries = 0;
419     UInt_t idRun = 0;
420 mocchiut 1.1 Int_t id_reg_run=-1;
421     stringstream ftmpname;
422     TString fname;
423     //
424     // define variables for opening and reading level0 file
425     //
426     TFile *l0File = 0;
427     TTree *l0tr = 0;
428 mocchiut 1.24 TTree *softinfo = 0;
429 mocchiut 1.1 TBranch *l0head = 0;
430     TBranch *l0calo = 0;
431     TBranch *l0trig = 0;
432 mocchiut 1.8 pamela::EventHeader *eh = 0;
433     pamela::PscuHeader *ph = 0;
434 mocchiut 1.1 pamela::trigger::TriggerEvent *trig = 0;
435 mocchiut 1.24 Int_t yodaver = 0;
436 mocchiut 1.1 //
437     // Define some basic variables
438     //
439 mocchiut 1.22 CaloLevel0 *event = new CaloLevel0(); // NOTICE: very important to call here the constructor!
440 mocchiut 1.21 event->SetCrossTalk(crosst);
441 mocchiut 1.41 // event->SetCrossTalkType(ctground);
442     Int_t ict = -1;
443     if ( ctground ) ict = 0;
444     if ( !ctground ){
445     ict = 1;
446     };
447     if ( !ctground && noselfct ){
448     ict = 2;
449     };
450     event->SetCrossTalkType(ict);
451 mocchiut 1.1 stringstream file2;
452     stringstream file3;
453     stringstream qy;
454     // Bool_t imtrack = false;
455     Bool_t filled = false;
456     //
457 mocchiut 1.8 UInt_t caloevents = 0;
458 mocchiut 1.1 stringstream calfile;
459     stringstream aligfile;
460     //
461     Int_t i = -1;
462     Int_t itr = -1;
463     Int_t badevent = 0;
464     Int_t totevent = 0;
465     //
466 mocchiut 1.8 UInt_t atime = 0;
467 mocchiut 1.1 //
468 mocchiut 1.47 // Int_t S3 = 0;
469     // Int_t S2 = 0;
470     // Int_t S12 = 0;
471     // Int_t S11 = 0;
472 mocchiut 1.1 UInt_t re = 0;
473 mocchiut 1.9 UInt_t jumped = 0;
474 mocchiut 1.1 //
475     TString caloversion;
476     ItoRunInfo *runinfo = 0;
477 mocchiut 1.8 TArrayI *runlist = 0;
478 mocchiut 1.1 //
479 mocchiut 1.47 // Float_t tmptrigty = -1.;
480 mocchiut 1.1 Int_t ntrkentry = 0;
481     GL_PARAM *q4 = new GL_PARAM();
482 mocchiut 1.8 UInt_t tttrkpar1 = 0;
483 mocchiut 1.1 Bool_t trkpar1 = true;
484     GL_ROOT *glroot = new GL_ROOT();
485 mocchiut 1.8 GL_TIMESYNC *dbtime = 0;
486 mocchiut 1.1 //
487 mocchiut 1.15 Long64_t maxsize = 10000000000LL;
488     TTree::SetMaxTreeSize(maxsize);
489 mocchiut 1.1 //
490     //
491     TFile *tempfile = 0;
492     TTree *tempcalo = 0;
493 mocchiut 1.30 Bool_t myfold = false;
494 mocchiut 1.1 stringstream tempname;
495     stringstream calofolder;
496     tempname.str("");
497     tempname << outdir;
498     tempname << "/" << processFolder.Data();
499     calofolder.str("");
500     calofolder << tempname.str().c_str();
501     tempname << "/calotree_run";
502     tempname << run << ".root";
503     //
504     // 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
505     // if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file
506     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
507     //
508 mocchiut 1.8 if ( run == 0 ) reproc = true;
509 mocchiut 1.1 //
510     //
511     //
512     if ( !file->IsOpen() ){
513     if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n");
514     throw -101;
515     };
516     //
517 mocchiut 1.53 if ( delct ){
518     TTree *T = (TTree*)file->Get("Calorimeter");
519     if ( T ){
520     if ( verbose ) printf(" REMOVING Calorimeter TTree \n");
521     T->Delete("all");
522     }
523     }
524     //
525 mocchiut 1.11 if ( withtrk ){
526     //
527     // Does it contain the Tracker tree?
528     //
529     tracker = (TTree*)file->Get("Tracker");
530     if ( !tracker ) {
531     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n");
532     code = -102;
533     goto closeandexit;
534     };
535     tracker->SetBranchAddress("TrkLevel2",&trk);
536     nevtrkl2 = tracker->GetEntries();
537 mocchiut 1.1 };
538     //
539 mocchiut 1.30 // Is it a file from simulations?
540     //
541     if ( checksimu ){
542     TTree *h20 = (TTree*)file->Get("h20");
543     if ( h20 ){
544     //
545     // yes!
546     //
547     crosst = false;
548 mocchiut 1.34 ctground = true;
549 mocchiut 1.30 mechal = true;
550 mocchiut 1.47 mask18 = true;
551     if ( verbose ) printf("\n\n SIMULATION!!! Setting --use-default-alig and --no-crosstalk --mask-plane18X flags!\n\n");
552 mocchiut 1.30 h20->Delete();
553     };
554     };
555     //
556 mocchiut 1.1 // Call runinfo
557     //
558     sgnl = 0;
559     runinfo = new ItoRunInfo(file);
560     //
561     // update versioning information and retrieve informations about the run to be processed
562     //
563     caloversion = CaloInfo(false);
564     sgnl = runinfo->Update(run,"CALO",caloversion);
565     //
566     if ( sgnl ){
567     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
568     code = sgnl;
569     goto closeandexit;
570     } else {
571     sgnl = 0;
572     };
573     //
574     // number of events in the file BEFORE the first event of our run
575     //
576     nobefrun = runinfo->GetFirstEntry();
577     //
578     // total number of events in the file
579     //
580     totfileentries = runinfo->GetFileEntries();
581     //
582     // first file entry AFTER the last event of our run
583     //
584     noaftrun = runinfo->GetLastEntry() + 1;
585     //
586     // number of run to be processed
587     //
588     numbofrun = runinfo->GetNoRun();
589     //
590     // number of runs in the file
591     //
592     totnorun = runinfo->GetRunEntries();
593     //
594     // Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it.
595     //
596     caloclone = (TTree*)file->Get("Calorimeter");
597     //
598     if ( !caloclone ){
599     reproc = false;
600 mocchiut 1.8 if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n");
601     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");
602 mocchiut 1.1 //
603     } else {
604     //
605 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
606 mocchiut 1.1 reproc = true;
607     //
608     if ( verbose ) printf("\n Preparing the pre-processing...\n");
609     //
610 mocchiut 1.28 if ( run == 0 || totnorun == 1 ){
611 mocchiut 1.1 //
612     // 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.
613     //
614     if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n");
615     reprocall = true;
616     //
617     } else {
618     //
619     // we are reprocessing a single run
620     //
621 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run);
622 mocchiut 1.1 reprocall = false;
623     //
624     //
625     //
626 mocchiut 1.30 gSystem->MakeDirectory(calofolder.str().c_str());
627     myfold = true;
628 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
629     tempcalo = caloclone->CloneTree(-1,"fast");
630     tempcalo->SetName("Calorimeter-old");
631     tempfile->Write();
632     tempfile->Close();
633     };
634     //
635     // delete old tree
636     //
637 mocchiut 1.53 if ( debug ) printf(" Delete caloclone \n");
638     if ( caloclone ) caloclone->Delete("all");
639 mocchiut 1.1 //
640     if ( verbose ) printf("\n ...done!\n");
641     //
642     };
643     //
644     // create calorimeter tree calo
645     //
646     file->cd();
647     calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data");
648 mocchiut 1.15 calo->SetAutoSave(900000000000000LL);
649 pam-fi 1.16 ca->Set();
650 mocchiut 1.53 if ( !cl1only ) calo->Branch("CaloLevel2","CaloLevel2",&ca);
651 mocchiut 1.13 if ( getl1 ) calo->Branch("CaloLevel1","CaloLevel1",&c1);
652 mocchiut 1.1 //
653     if ( reproc && !reprocall ){
654     //
655     //
656     //
657     tempfile = new TFile(tempname.str().c_str(),"READ");
658     caloclone = (TTree*)tempfile->Get("Calorimeter-old");
659 mocchiut 1.15 caloclone->SetAutoSave(900000000000000LL);
660 mocchiut 1.53 if ( !cl1only ) caloclone->SetBranchAddress("CaloLevel2",&caclone);
661 mocchiut 1.13 TBranch *havel1 = caloclone->GetBranch("CaloLevel1");
662     if ( !havel1 && getl1 ) throw -117;
663     if ( havel1 && !getl1 ) throw -118;
664     if ( getl1 ) caloclone->SetBranchAddress("CaloLevel1",&c1clone);
665 mocchiut 1.1 //
666 mocchiut 1.53 if ( nobefrun > 0 ){
667 mocchiut 1.1 if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
668 mocchiut 1.8 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
669 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
670     for (UInt_t j = 0; j < nobefrun; j++){
671     //
672 mocchiut 1.48 if ( caloclone->GetEntry(j) <= 0 ) throw -36;
673 mocchiut 1.1 //
674     // copy caclone to ca
675     //
676 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
677 mocchiut 1.53 if ( !cl1only ) memcpy(&ca,&caclone,sizeof(caclone));
678 mocchiut 1.1 //
679     // Fill entry in the new tree
680     //
681 mocchiut 1.53 if ( getl1 || !cl1only ) calo->Fill();
682 mocchiut 1.1 //
683 mocchiut 1.13 if ( getl1 ) c1->Clear();
684 mocchiut 1.6 ca->Clear();
685     //
686 mocchiut 1.1 };
687     if ( verbose ) printf(" Finished successful copying!\n");
688 mocchiut 1.53 }
689     }
690 mocchiut 1.1 //
691     // Get the list of run to be processed
692     //
693     runlist = runinfo->GetRunList();
694     //
695     // Loop over the run to be processed
696     //
697     for (UInt_t irun=0; irun < numbofrun; irun++){
698     //
699     badevent = 0;
700     //
701     idRun = runlist->At(irun);
702     if ( verbose ) printf("\n\n\n ####################################################################### \n");
703 mocchiut 1.8 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
704 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
705     //
706     sgnl = runinfo->GetRunInfo(idRun);
707     if ( sgnl ){
708     if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
709     code = sgnl;
710     goto closeandexit;
711     } else {
712     sgnl = 0;
713     };
714 mocchiut 1.8 id_reg_run = runinfo->ID_ROOT_L0;
715 mocchiut 1.1 runheadtime = runinfo->RUNHEADER_TIME;
716     runtrailtime = runinfo->RUNTRAILER_TIME;
717 mocchiut 1.8 evfrom = runinfo->EV_FROM;
718     evto = runinfo->EV_TO;
719 mocchiut 1.1 //
720     if ( id_reg_run == -1 ){
721 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
722 mocchiut 1.1 code = -5;
723     goto closeandexit;
724     };
725     //
726 mocchiut 1.8 // prepare the timesync for the db
727     //
728 mocchiut 1.32 TString host = glt->CGetHost();
729     TString user = glt->CGetUser();
730     TString psw = glt->CGetPsw();
731     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
732     if ( !dbc->IsConnected() ) throw -116;
733 mocchiut 1.33 stringstream myquery;
734     myquery.str("");
735     myquery << "SET time_zone='+0:00'";
736 mocchiut 1.52 delete dbc->Query(myquery.str().c_str());
737 mocchiut 1.32 //
738 mocchiut 1.31 // if ( !dbc->IsConnected() ) throw -116;
739 mocchiut 1.8 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
740     //
741 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
742     //
743 mocchiut 1.31 // if ( !dbc->IsConnected() ) throw -116;
744 mocchiut 1.8 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
745 mocchiut 1.1 //
746     ftmpname.str("");
747     ftmpname << glroot->PATH.Data() << "/";
748     ftmpname << glroot->NAME.Data();
749     fname = ftmpname.str().c_str();
750     //
751     // print out informations
752     //
753 mocchiut 1.8 totevent = runinfo->NEVENTS;
754 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
755 mocchiut 1.8 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime);
756     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime);
757     if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent);
758 mocchiut 1.1 //
759 mocchiut 1.43 // if ( !totevent ) goto closeandexit;
760 mocchiut 1.42 //
761 mocchiut 1.1 // Open Level0 file
762     //
763 mocchiut 1.52 if ( l0File ) l0File->Close();
764 mocchiut 1.1 l0File = new TFile(fname.Data());
765     if ( !l0File ) {
766     if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n");
767     code = -6;
768     goto closeandexit;
769     };
770     l0tr = (TTree*)l0File->Get("Physics");
771     if ( !l0tr ) {
772     if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n");
773     l0File->Close();
774     code = -7;
775     goto closeandexit;
776     };
777     l0head = l0tr->GetBranch("Header");
778     if ( !l0head ) {
779     if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n");
780     l0File->Close();
781     code = -8;
782     goto closeandexit;
783     };
784     l0calo = l0tr->GetBranch("Calorimeter");
785     if ( !l0calo ) {
786     if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n");
787     l0File->Close();
788     code = -103;
789     goto closeandexit;
790     };
791     l0trig = l0tr->GetBranch("Trigger");
792     if ( !l0trig ) {
793     if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n");
794     l0File->Close();
795     code = -104;
796     goto closeandexit;
797     };
798     //
799     l0tr->SetBranchAddress("Trigger", &trig);
800 mocchiut 1.8 l0tr->SetBranchAddress("Header", &eh);
801 mocchiut 1.1 //
802 mocchiut 1.24 softinfo = (TTree*)l0File->Get("SoftInfo");
803     if ( softinfo ){
804     softinfo->SetBranchAddress("SoftInfo",&yodaver);
805 mocchiut 1.48 if ( softinfo->GetEntry(0) <= 0 ) throw -36;
806 mocchiut 1.24 };
807     if ( debug ) printf(" LEVEL0 FILE GENERATED WITH YODA VERSION %i \n",yodaver);
808     //
809 mocchiut 1.38 if ( withtrk ){
810     if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){
811     trkpar1 = false;
812     Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc);
813     if ( glpar < 0 ){
814     code = glpar;
815     goto closeandexit;
816     };
817     tttrkpar1 = q4->TO_TIME;
818     // ----------------------------
819     // Read the magnetic field
820     // ----------------------------
821     if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
822     trk->LoadField(q4->PATH+q4->NAME);
823     if ( verbose ) printf("\n");
824     };
825     };
826     //
827     // Close DB connection
828     //
829     if ( dbc ){
830     dbc->Close();
831     delete dbc;
832 pam-fi 1.50 dbc = 0;
833 mocchiut 1.38 };
834     //
835     //
836 mocchiut 1.1 // Construct the event object, look for the calibration which include the first header
837     //
838     sgnl = 0;
839     if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n");
840 mocchiut 1.31 // if ( !dbc->IsConnected() ) throw -116;
841 mocchiut 1.32 event->ProcessingInit(glt,runheadtime,sgnl,l0tr,debug,verbose);
842 mocchiut 1.47 if ( mask18 ){
843     event->MaskPlane18X();
844     } else {
845     event->UnMaskPlane18X();
846     };
847 mocchiut 1.1 if ( verbose ) printf("\n");
848     if ( sgnl == 100 ) {
849     code = sgnl;
850     if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n");
851     sgnl = 0;
852     };
853     if ( sgnl ){
854     l0File->Close();
855     code = sgnl;
856     goto closeandexit;
857     };
858     //
859     qy.str("");
860     //
861 mocchiut 1.8 nevents = l0head->GetEntries();
862 mocchiut 1.1 caloevents = l0calo->GetEntries();
863     //
864 mocchiut 1.43 if ( nevents < 1 && totevent ) {
865 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n");
866     l0File->Close();
867     code = -11;
868     goto closeandexit;
869     };
870     //
871 mocchiut 1.43 if ( evto > nevents-1 && totevent ) {
872 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n");
873     l0File->Close();
874     code = -12;
875     goto closeandexit;
876     };
877     //
878     // Check if we have to load parameter files
879     //
880     sgnl = 0;
881 mocchiut 1.32 sgnl = event->ChkParam(glt,runheadtime,mechal); // calorimeter parameter files
882 mocchiut 1.1 if ( sgnl < 0 ){
883     code = sgnl;
884     l0File->Close();
885     goto closeandexit;
886     };
887     //
888 mocchiut 1.34 // Calculate the cross talk corrections needed for this run using calibration informations
889     //
890     if ( crosst && !ctground ){
891     sgnl = 0;
892 mocchiut 1.41 sgnl = event->CalcCrossTalkCorr(glt,runheadtime,usetable);
893 mocchiut 1.34 if ( sgnl < 0 ){
894     code = sgnl;
895     l0File->Close();
896     goto closeandexit;
897     };
898     };
899     //
900 mocchiut 1.1 // run over all the events of the run
901     //
902     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
903     //
904 mocchiut 1.19 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
905 mocchiut 1.44 // for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+5000); re++){
906 mocchiut 1.49 //for ( re = 92012+3400; re < 92012+3700; re++){
907     //for ( re = 4451+2833; re < 4451+2836 ; re++){
908 mocchiut 1.18 // printf(" i = %i \n",re-200241);
909 mocchiut 1.1 //
910     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
911     //
912 mocchiut 1.39 if ( debug ) printf("\n\n\n EVENT NUMBER %i \n",procev);
913     //
914 mocchiut 1.48 if ( l0head->GetEntry(re) <= 0 ) throw -36;
915 mocchiut 1.1 //
916     // absolute time of this event
917     //
918 mocchiut 1.8 ph = eh->GetPscuHeader();
919     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
920 mocchiut 1.1 //
921     //
922     //
923 mocchiut 1.8 if ( re > caloevents-1 ){
924 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i);
925     l0File->Close();
926     code = -112;
927     goto closeandexit;
928     };
929     //
930 mocchiut 1.45 if ( atime > (runtrailtime+1) || atime < (runheadtime-1) ) {
931 mocchiut 1.1 if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n");
932 mocchiut 1.9 jumped++;
933 mocchiut 1.1 goto jumpev;
934     };
935     //
936     // retrieve tracker informations
937     //
938     if ( !reprocall ){
939 mocchiut 1.19 itr = nobefrun + (re - evfrom - jumped);
940     //itr = re-(46438+200241);
941 mocchiut 1.1 } else {
942 mocchiut 1.9 itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
943 mocchiut 1.1 };
944 mocchiut 1.11 //
945     if ( withtrk ){
946     if ( itr > nevtrkl2 ){
947     if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
948     l0File->Close();
949     code = -113;
950     goto closeandexit;
951     };
952     //
953     trk->Clear();
954     //
955 mocchiut 1.48 if ( tracker->GetEntry(itr) <= 0 ) throw -36;
956 mocchiut 1.11 //
957 mocchiut 1.1 };
958 mocchiut 1.10 //
959 mocchiut 1.1 procev++;
960     //
961     // start processing
962     //
963 mocchiut 1.13 if ( getl1 ) c1->Clear();
964 mocchiut 1.6 ca->Clear();
965 mocchiut 1.1 //
966     // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
967     //
968 mocchiut 1.48 if ( l0trig->GetEntry(re) <= 0 ) throw -36;
969 mocchiut 1.47 // S3 = 0;
970     // S2 = 0;
971     // S12 = 0;
972     // S11 = 0;
973     // S3 = trig->patterntrig[2];
974     // S2 = trig->patterntrig[3];
975     // S12 = trig->patterntrig[4];
976     // S11 = trig->patterntrig[5];
977     // if ( trig->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
978     // if ( trig->patterntrig[0] ) tmptrigty = 2.;
979     // if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
980     // if ( !(trig->patterntrig[1] & (1<<0)) && !trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
981     // event->clevel2->trigty = tmptrigty;
982     event->clevel2->trigty = (Float_t)IsCaloSelfTrigger(trig->trigconf,(UInt_t*)trig->patterntrig);
983     if ( debug ) printf(" starting with trigty %f \n",event->clevel2->trigty);
984 mocchiut 1.1 //
985     // check if the calibration we are using is still good, if not load another calibration
986     //
987     sgnl = 0;
988 mocchiut 1.32 sgnl = event->ChkCalib(glt,atime);
989 mocchiut 1.1 if ( sgnl < 0 ){
990     code = sgnl;
991     goto closeandexit;
992     };
993     if ( sgnl == 100 ){
994     code = sgnl;
995     if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n");
996     badevent++;
997     sgnl = 0;
998     };
999     //
1000 mocchiut 1.11 // do we have at least one track from the tracker? this check has been disabled
1001 mocchiut 1.1 //
1002     event->clevel1->good2 = 1;
1003 mocchiut 1.8 //
1004 mocchiut 1.29 // use the whole calorimeter, 22 W planes
1005     //
1006     event->clevel1->npla = 22;
1007     //
1008     // Use the standard silicon planes shifting
1009     //
1010     event->clevel1->reverse = 0;
1011     //
1012 mocchiut 1.1 //
1013 mocchiut 1.8 // Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine
1014 mocchiut 1.1 //
1015 mocchiut 1.11 event->Calibrate(re);
1016 mocchiut 1.1 //
1017     // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables.
1018     //
1019     //
1020     // Calculate variables common to all tracks (qtot, nstrip, etc.)
1021     //
1022 mocchiut 1.53 if ( !cl1only ) event->GetCommonVar();
1023 mocchiut 1.1 //
1024     // Fill common variables
1025     //
1026 mocchiut 1.53 if ( cl1only ){
1027     event->FillCommonVar(c1);
1028     } else {
1029     event->FillCommonVar(c1,ca);
1030     }
1031 mocchiut 1.1 //
1032     // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
1033     //
1034     ntrkentry = 0;
1035     //
1036     filled = false;
1037 mocchiut 1.11 //
1038 mocchiut 1.53 // skip track-related if processing level1 only
1039 mocchiut 1.1 //
1040 mocchiut 1.53 if ( !cl1only ){
1041     //
1042     // Run over tracks (tracker or calorimeter )
1043     //
1044     if ( withtrk ){
1045     for(Int_t nt=0; nt < trk->ntrk(); nt++){
1046     //
1047     event->clevel1->good2 = 1;
1048     //
1049     TrkTrack *ptt = trk->GetStoredTrack(nt);
1050     //
1051     event->clevel1->trkchi2 = 0;
1052     //
1053     // Copy the alpha vector in the input structure
1054     //
1055     for (Int_t e = 0; e < 5 ; e++){
1056     event->clevel1->al_p[e][0] = ptt->al[e];
1057     };
1058     //
1059     // Get tracker related variables for this track
1060     //
1061     event->GetTrkVar();
1062     //
1063     // Save tracker track sequence number
1064     //
1065     event->trkseqno = nt;
1066     //
1067     // Copy values in the class ca from the structure clevel2
1068     //
1069     event->FillTrkVar(ca,ntrkentry);
1070     ntrkentry++;
1071     filled = true;
1072     //
1073     }; // loop on all the tracks
1074     };
1075     //
1076     // 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
1077     // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
1078     // here we make an event selection so it must be done very carefully...
1079     //
1080     // 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
1081     //
1082     if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
1083     if ( debug ) printf(" Event with a track not fitted by the tracker at entry %i \n",itr);
1084     //
1085     // Disable "track mode" in the fortran routine
1086     //
1087     event->clevel1->good2 = 0;
1088     event->clevel1->riginput = rigdefault;
1089     if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
1090     //
1091     // We have a selftrigger event to analyze.
1092     //
1093     for (Int_t e = 0; e < 5 ; e++){
1094     event->clevel1->al_p[e][0] = 0.;
1095     event->clevel1->al_p[e][1] = 0.;
1096     }
1097     event->clevel1->trkchi2 = 0;
1098     //
1099     event->GetTrkVar();
1100     //
1101     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
1102     //
1103     if ( event->clevel1->good2 == 0 ) {
1104     //
1105     // In selftrigger mode the trkentry variable is set to -1
1106     //
1107     event->trkseqno = -3;
1108     //
1109     // Copy values in the class ca from the structure clevel2
1110     //
1111     if ( debug ) printf(" -3 begin \n");
1112     event->FillTrkVar(ca,ntrkentry);
1113     if ( debug ) printf(" -3 end \n");
1114     ntrkentry++;
1115     filled = true;
1116     //
1117     } else {
1118     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
1119     }
1120     //
1121     }
1122     //
1123     // Call high energy nuclei routine
1124     //
1125     if ( hZn && event->clevel2->trigty >= 2. ){
1126     if ( debug ) printf(" Calling selftrigger high energy nuclei routine for entry %i \n",itr);
1127     //
1128     // Disable "track mode" in the fortran routine
1129     //
1130     event->clevel1->good2 = 0;
1131     //
1132     // Set high energy nuclei flag to one
1133     //
1134     event->clevel1->hzn = 1;
1135     event->clevel1->riginput = rigdefault;
1136     //
1137     // We have a selftrigger event to analyze.
1138     //
1139     for (Int_t e = 0; e < 5 ; e++){
1140     event->clevel1->al_p[e][0] = 0.;
1141     event->clevel1->al_p[e][1] = 0.;
1142     }
1143     event->clevel1->trkchi2 = 0;
1144     //
1145     event->GetTrkVar();
1146     //
1147     // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
1148     //
1149     if ( event->clevel1->good2 == 0 ) {
1150     //
1151     // In selftrigger mode the trkentry variable is set to -1
1152     //
1153     event->trkseqno = -2;
1154     //
1155     // Copy values in the class ca from the structure clevel2
1156     //
1157     event->FillTrkVar(ca,ntrkentry);
1158     ntrkentry++;
1159     filled = true;
1160     //
1161     } else {
1162     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
1163     }
1164     //
1165     }
1166     //
1167     // self trigger event
1168     //
1169     if ( st && event->clevel2->trigty >= 2. ){
1170     if ( verbose ) printf(" Selftrigger event at entry %i \n",itr);
1171     //
1172     // Disable "track mode" in the fortran routine
1173     //
1174     event->clevel1->good2 = 0;
1175     //
1176     // disable high enery nuclei flag;
1177     //
1178     event->clevel1->hzn = 0;
1179     //
1180     // We have a selftrigger event to analyze.
1181     //
1182     for (Int_t e = 0; e < 5 ; e++){
1183     event->clevel1->al_p[e][0] = 0.;
1184     event->clevel1->al_p[e][1] = 0.;
1185     };
1186     event->clevel1->trkchi2 = 0;
1187     //
1188     event->GetTrkVar();
1189     //
1190     // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
1191     //
1192     if ( event->clevel1->good2 == 0 ) {
1193     //
1194     // In selftrigger mode the trkentry variable is set to -1
1195     //
1196     event->trkseqno = -1;
1197     //
1198     // Copy values in the class ca from the structure clevel2
1199     //
1200     event->FillTrkVar(ca,ntrkentry);
1201     ntrkentry++;
1202     filled = true;
1203     //
1204     } else {
1205     if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
1206     }
1207     }
1208     //
1209     if ( !filled ) badevent++;
1210     }
1211 mocchiut 1.1 //
1212     // Clear structures used to communicate with fortran
1213     //
1214     event->ClearStructs();
1215     //
1216     // Fill the rootple
1217     //
1218     calo->Fill();
1219     //
1220 mocchiut 1.6 // delete ca;
1221 mocchiut 1.1 //
1222     jumpev:
1223 mocchiut 1.23 if ( !debug ) debug = false;
1224 mocchiut 1.1 //
1225     };
1226     //
1227     if ( verbose ) printf("\n SUMMARY:\n");
1228     if ( verbose ) printf(" Total number of events: %i \n",totevent);
1229     if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent);
1230     if ( verbose ) printf(" Events without tracks: %i \n",badevent);
1231     //
1232     if ( badevent == totevent ){
1233 mocchiut 1.8 if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun);
1234 mocchiut 1.1 code = 101;
1235     };
1236     //
1237 mocchiut 1.8 delete dbtime;
1238 mocchiut 1.1 //
1239     // Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software).
1240     //
1241     event->RunClose();
1242     if ( l0File ) l0File->Close();
1243     //
1244     }; // process all the runs
1245     //
1246     if ( verbose ) printf("\n Finished processing data \n");
1247     //
1248     closeandexit:
1249     //
1250     if ( !reprocall && reproc && code >= 0 ){
1251     if ( totfileentries > noaftrun ){
1252     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1253 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);
1254     if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries);
1255 mocchiut 1.1 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1256     //
1257     // Get entry from old tree
1258     //
1259 mocchiut 1.48 if ( caloclone->GetEntry(j) <= 0 ) throw -36;
1260 mocchiut 1.1 //
1261     // copy caclone to ca
1262     //
1263 mocchiut 1.13 if ( getl1 ) c1->Clear();
1264 mocchiut 1.6 ca->Clear();
1265 mocchiut 1.8 //
1266 mocchiut 1.13 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
1267 mocchiut 1.1 memcpy(&ca,&caclone,sizeof(caclone));
1268     //
1269     // Fill entry in the new tree
1270     //
1271 mocchiut 1.53 if ( getl1 || !cl1only ) calo->Fill();
1272 mocchiut 1.1 //
1273     };
1274     if ( verbose ) printf(" Finished successful copying!\n");
1275     };
1276     };
1277     //
1278     // Case of no errors: close files, delete old tree(s), write and close level2 file
1279     //
1280     if ( l0File ) l0File->Close();
1281     if ( tempfile ) tempfile->Close();
1282 mocchiut 1.30 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1283 mocchiut 1.1 //
1284     if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n");
1285     if ( verbose ) printf("\n Writing and closing rootple\n");
1286 mocchiut 1.53 if ( debug ) printf("\n Setting name\n");
1287 mocchiut 1.1 if ( calo ) calo->SetName("Calorimeter");
1288 mocchiut 1.53 if ( debug ) printf("\n Writing calo tree\n");
1289 mocchiut 1.1 if ( file ){
1290     file->cd();
1291 mocchiut 1.53 if ( calo ) calo->Write("Calorimeter", TObject::kOverwrite); // 10RED CRASH when exiting with no tracker, calo is not defined
1292 mocchiut 1.1 };
1293     //
1294 mocchiut 1.53 if ( debug ) printf("\n Unlink folder\n");
1295 mocchiut 1.30 if ( myfold ) gSystem->Unlink(calofolder.str().c_str());
1296 mocchiut 1.1 //
1297 mocchiut 1.51 // if ( ca ) delete ca;
1298     // if ( caclone ) delete caclone;
1299     // if ( c1 ) delete c1;
1300     // if ( c1clone ) delete c1clone;
1301     // if ( trk ) delete trk; // cannot delete anything since now all these objects are owned by TFile file!
1302    
1303 mocchiut 1.53 if ( debug ) printf("\n Delete q4\n");
1304 mocchiut 1.7 if ( q4 ) delete q4;
1305 mocchiut 1.53 if ( debug ) printf("\n Delete glroot\n");
1306 mocchiut 1.7 if ( glroot ) delete glroot;
1307 mocchiut 1.53 if ( debug ) printf("\n Close runinfo\n");
1308 mocchiut 1.51 if ( runinfo ) runinfo->Close();
1309 mocchiut 1.53 if ( debug ) printf("\n Delete runinfo\n");
1310 mocchiut 1.7 if ( runinfo ) delete runinfo;
1311     //
1312 mocchiut 1.1 // the end
1313     //
1314     if ( verbose ) printf("\n Exiting...\n");
1315     if ( code < 0 ) throw code;
1316     return(code);
1317     }
1318    
1319    
1320    

  ViewVC Help
Powered by ViewVC 1.1.23