/[PAMELA software]/PamelaLevel2/src/PamLevel2.cpp
ViewVC logotype

Annotation of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.93 - (hide annotations) (download)
Thu Jan 16 15:36:32 2014 UTC (11 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.92: +5 -5 lines
Compilation warnings using GCC4.7 fixed

1 pam-fi 1.1 #include <PamLevel2.h>
2 pam-fi 1.52
3     /////////////////////////////////////////////////////////////////////////////
4     /////////////////////////////////////////////////////////////////////////////
5     /////////////////////////////////////////////////////////////////////////////
6     /////////////////////////////////////////////////////////////////////////////
7    
8 pam-fi 1.81 void GPamela::Clear() {
9     Irun = 0;
10     Ievnt = 0;
11     Ipa = 0;
12     X0 = 0.;
13     Y0 = 0.;
14     Z0 = 0.;
15     Theta = 0.;
16     Phi = 0.;
17     P0 = 0.;
18     Nthtof = 0;
19     Nthcat = 0;
20     Nthcas = 0;
21     Nthspe = 0;
22     Nstrpx = 0;
23     Nstrpy = 0;
24     Nthcali = 0;
25     Nthcal = 0;
26     Nthnd = 0;
27     Nthcard = 0;
28 pam-fi 1.52 }
29    
30 pam-fi 1.81 void GPamela::Delete() {
31    
32     Clear();
33 mocchiut 1.93 /* EM all these are in the stack not in the heap, so no need to delete by hand...
34 pam-fi 1.81 delete[] Ipltof;
35     delete[] Ipaddle;
36     delete[] Ipartof;
37     delete[] Xintof;
38     delete[] Yintof;
39     delete[] Zintof;
40     delete[] Xouttof;
41     delete[] Youttof;
42     delete[] Zouttof;
43     delete[] Ereltof;
44     delete[] Timetof;
45     delete[] Pathtof;
46     delete[] P0tof;
47     delete[] Iparcat;
48     delete[] Icat;
49     delete[] Xincat;
50     delete[] Yincat;
51     delete[] Zincat;
52     delete[] Xoutcat;
53     delete[] Youtcat;
54     delete[] Zoutcat;
55     delete[] Erelcat;
56     delete[] Timecat;
57     delete[] Pathcat;
58     delete[] P0cat;
59     delete[] Iparcas;
60     delete[] Icas;
61     delete[] Xincas;
62     delete[] Yincas;
63     delete[] Zincas;
64     delete[] Xoutcas;
65     delete[] Youtcas;
66     delete[] Zoutcas;
67     delete[] Erelcas;
68     delete[] Timecas;
69     delete[] Pathcas;
70     delete[] P0cas;
71     delete[] Iparspe;
72     delete[] Itrpb;
73     delete[] Itrsl;
74     delete[] Itspa;
75     delete[] Xinspe;
76     delete[] Yinspe;
77     delete[] Zinspe;
78     delete[] Xoutspe;
79     delete[] Youtspe;
80     delete[] Zoutspe;
81     delete[] Xavspe;
82     delete[] Yavspe;
83     delete[] Zavspe;
84     delete[] Erelspe;
85     delete[] Pathspe;
86     delete[] P0spe;
87     delete[] Nxmult;
88     delete[] Nymult;
89     delete[] Npstripx;
90     delete[] Ntstripx;
91     delete[] Istripx;
92     delete[] Qstripx;
93     delete[] Xstripx;
94     delete[] Npstripy;
95     delete[] Ntstripy;
96     delete[] Istripy;
97     delete[] Qstripy;
98     delete[] Ystripy;
99     delete[] Icaplane;
100     delete[] Icastrip;
101     delete[] Icamod;
102     delete[] Enestrip;
103     delete[] Icapl;
104     delete[] Icasi;
105     delete[] Icast;
106     delete[] Xincal;
107     delete[] Yincal;
108     delete[] Zincal;
109     delete[] Erelcal;
110     delete[] Itubend;
111     delete[] Iparnd;
112     delete[] Xinnd;
113     delete[] Yinnd;
114     delete[] Zinnd;
115     delete[] Xoutnd;
116     delete[] Youtnd;
117     delete[] Zoutnd;
118     delete[] Erelnd;
119     delete[] Timend;
120     delete[] Pathnd;
121     delete[] P0nd;
122     delete[] Iparcard;
123     delete[] Icard;
124     delete[] Xincard;
125     delete[] Yincard;
126     delete[] Zincard;
127     delete[] Xoutcard;
128     delete[] Youtcard;
129     delete[] Zoutcard;
130     delete[] Erelcard;
131     delete[] Timecard;
132     delete[] Pathcard;
133     delete[] P0card;
134 mocchiut 1.93 */
135     }
136 pam-fi 1.52
137 pam-fi 1.81
138     void GPamela::SetBranchAddress(TChain* fhBookTree) {
139 pam-fi 1.52
140 pam-fi 1.81 // cout << "fhBookTree "<<fhBookTree << endl;
141     // prepare tree
142     fhBookTree->SetBranchAddress("Irun", &Irun);
143     fhBookTree->SetBranchAddress("Ievnt", &Ievnt);
144     fhBookTree->SetBranchAddress("Ipa", &Ipa);
145     fhBookTree->SetBranchAddress("X0", &X0);
146     fhBookTree->SetBranchAddress("Y0", &Y0);
147     fhBookTree->SetBranchAddress("Z0", &Z0);
148     fhBookTree->SetBranchAddress("Theta", &Theta);
149     fhBookTree->SetBranchAddress("Phi", &Phi);
150     fhBookTree->SetBranchAddress("P0", &P0);
151     fhBookTree->SetBranchAddress("Nthtof", &Nthtof);
152     fhBookTree->SetBranchAddress("Ipltof", Ipltof);
153     fhBookTree->SetBranchAddress("Ipaddle", Ipaddle);
154     fhBookTree->SetBranchAddress("Ipartof", Ipartof);
155     fhBookTree->SetBranchAddress("Xintof", Xintof);
156     fhBookTree->SetBranchAddress("Yintof", Yintof);
157     fhBookTree->SetBranchAddress("Zintof", Zintof);
158     fhBookTree->SetBranchAddress("Xouttof", Xouttof);
159     fhBookTree->SetBranchAddress("Youttof", Youttof);
160     fhBookTree->SetBranchAddress("Zouttof", Zouttof);
161     fhBookTree->SetBranchAddress("Ereltof", Ereltof);
162     fhBookTree->SetBranchAddress("Timetof", Timetof);
163     fhBookTree->SetBranchAddress("Pathtof", Pathtof);
164     fhBookTree->SetBranchAddress("P0tof", P0tof);
165     fhBookTree->SetBranchAddress("Nthcat", &Nthcat);
166     fhBookTree->SetBranchAddress("Iparcat", Iparcat);
167     fhBookTree->SetBranchAddress("Icat", Icat);
168     fhBookTree->SetBranchAddress("Xincat", Xincat);
169     fhBookTree->SetBranchAddress("Yincat", Yincat);
170     fhBookTree->SetBranchAddress("Zincat", Zincat);
171     fhBookTree->SetBranchAddress("Xoutcat", Xoutcat);
172     fhBookTree->SetBranchAddress("Youtcat", Youtcat);
173     fhBookTree->SetBranchAddress("Zoutcat", Zoutcat);
174     fhBookTree->SetBranchAddress("Erelcat", Erelcat);
175     fhBookTree->SetBranchAddress("Timecat", Timecat);
176     fhBookTree->SetBranchAddress("Pathcat", Pathcat);
177     fhBookTree->SetBranchAddress("P0cat", P0cat);
178     fhBookTree->SetBranchAddress("Nthcas", &Nthcas);
179     fhBookTree->SetBranchAddress("Iparcas", Iparcas);
180     fhBookTree->SetBranchAddress("Icas", Icas);
181     fhBookTree->SetBranchAddress("Xincas", Xincas);
182     fhBookTree->SetBranchAddress("Yincas", Yincas);
183     fhBookTree->SetBranchAddress("Zincas", Zincas);
184     fhBookTree->SetBranchAddress("Xoutcas", Xoutcas);
185     fhBookTree->SetBranchAddress("Youtcas", Youtcas);
186     fhBookTree->SetBranchAddress("Zoutcas", Zoutcas);
187     fhBookTree->SetBranchAddress("Erelcas", Erelcas);
188     fhBookTree->SetBranchAddress("Timecas", Timecas);
189     fhBookTree->SetBranchAddress("Pathcas", Pathcas);
190     fhBookTree->SetBranchAddress("P0cas", P0cas);
191     fhBookTree->SetBranchAddress("Nthspe", &Nthspe);
192     fhBookTree->SetBranchAddress("Iparspe", Iparspe);
193     fhBookTree->SetBranchAddress("Itrpb", Itrpb);
194     fhBookTree->SetBranchAddress("Itrsl", Itrsl);
195     fhBookTree->SetBranchAddress("Itspa", Itspa);
196     fhBookTree->SetBranchAddress("Xinspe", Xinspe);
197     fhBookTree->SetBranchAddress("Yinspe", Yinspe);
198     fhBookTree->SetBranchAddress("Zinspe", Zinspe);
199     fhBookTree->SetBranchAddress("Xoutspe", Xoutspe);
200     fhBookTree->SetBranchAddress("Youtspe", Youtspe);
201     fhBookTree->SetBranchAddress("Zoutspe", Zoutspe);
202     fhBookTree->SetBranchAddress("Xavspe", Xavspe);
203     fhBookTree->SetBranchAddress("Yavspe", Yavspe);
204     fhBookTree->SetBranchAddress("Zavspe", Zavspe);
205     fhBookTree->SetBranchAddress("Erelspe", Erelspe);
206     fhBookTree->SetBranchAddress("Pathspe", Pathspe);
207     fhBookTree->SetBranchAddress("P0spe", P0spe);
208     fhBookTree->SetBranchAddress("Nxmult", Nxmult);
209     fhBookTree->SetBranchAddress("Nymult", Nymult);
210     fhBookTree->SetBranchAddress("Nstrpx", &Nstrpx);
211     fhBookTree->SetBranchAddress("Npstripx", Npstripx);
212     fhBookTree->SetBranchAddress("Ntstripx", Ntstripx);
213     fhBookTree->SetBranchAddress("Istripx", Istripx);
214     fhBookTree->SetBranchAddress("Qstripx", Qstripx);
215     fhBookTree->SetBranchAddress("Xstripx", Xstripx);
216     fhBookTree->SetBranchAddress("Nstrpy", &Nstrpy);
217     fhBookTree->SetBranchAddress("Npstripy", Npstripy);
218     fhBookTree->SetBranchAddress("Ntstripy", Ntstripy);
219     fhBookTree->SetBranchAddress("Istripy", Istripy);
220     fhBookTree->SetBranchAddress("Qstripy", Qstripy);
221     fhBookTree->SetBranchAddress("Ystripy", Ystripy);
222     fhBookTree->SetBranchAddress("Nthcali", &Nthcali);
223     fhBookTree->SetBranchAddress("Icaplane", Icaplane);
224     fhBookTree->SetBranchAddress("Icastrip", Icastrip);
225     fhBookTree->SetBranchAddress("Icamod", Icamod);
226     fhBookTree->SetBranchAddress("Enestrip", Enestrip);
227     fhBookTree->SetBranchAddress("Nthcal", &Nthcal);
228     fhBookTree->SetBranchAddress("Icapl", Icapl);
229     fhBookTree->SetBranchAddress("Icasi", Icasi);
230     fhBookTree->SetBranchAddress("Icast", Icast);
231     fhBookTree->SetBranchAddress("Xincal", Xincal);
232     fhBookTree->SetBranchAddress("Yincal", Yincal);
233     fhBookTree->SetBranchAddress("Zincal", Zincal);
234     fhBookTree->SetBranchAddress("Erelcal", Erelcal);
235     fhBookTree->SetBranchAddress("Nthnd", &Nthnd);
236     fhBookTree->SetBranchAddress("Itubend", Itubend);
237     fhBookTree->SetBranchAddress("Iparnd", Iparnd);
238     fhBookTree->SetBranchAddress("Xinnd", Xinnd);
239     fhBookTree->SetBranchAddress("Yinnd", Yinnd);
240     fhBookTree->SetBranchAddress("Zinnd", Zinnd);
241     fhBookTree->SetBranchAddress("Xoutnd", Xoutnd);
242     fhBookTree->SetBranchAddress("Youtnd", Youtnd);
243     fhBookTree->SetBranchAddress("Zoutnd", Zoutnd);
244     fhBookTree->SetBranchAddress("Erelnd", Erelnd);
245     fhBookTree->SetBranchAddress("Timend", Timend);
246     fhBookTree->SetBranchAddress("Pathnd", Pathnd);
247     fhBookTree->SetBranchAddress("P0nd", P0nd);
248     fhBookTree->SetBranchAddress("Nthcard", &Nthcard);
249     fhBookTree->SetBranchAddress("Iparcard", Iparcard);
250     fhBookTree->SetBranchAddress("Icard", Icard);
251     fhBookTree->SetBranchAddress("Xincard", Xincard);
252     fhBookTree->SetBranchAddress("Yincard", Yincard);
253     fhBookTree->SetBranchAddress("Zincard", Zincard);
254     fhBookTree->SetBranchAddress("Xoutcard", Xoutcard);
255     fhBookTree->SetBranchAddress("Youtcard", Youtcard);
256     fhBookTree->SetBranchAddress("Zoutcard", Zoutcard);
257     fhBookTree->SetBranchAddress("Erelcard", Erelcard);
258     fhBookTree->SetBranchAddress("Timecard", Timecard);
259     fhBookTree->SetBranchAddress("Pathcard", Pathcard);
260     fhBookTree->SetBranchAddress("P0card", P0card);
261 pam-fi 1.52
262 pam-fi 1.81 // fhBookTree->SetBranchStatus("*",0);
263 pam-fi 1.52
264     }
265    
266 pam-fi 1.81 ClassImp( GPamela);
267 pam-fi 1.52
268     /////////////////////////////////////////////////////////////////////////////
269     /////////////////////////////////////////////////////////////////////////////
270     /////////////////////////////////////////////////////////////////////////////
271     /////////////////////////////////////////////////////////////////////////////
272 pam-fi 1.1 //--------------------------------------
273     //
274     //
275     //--------------------------------------
276     /**
277 pam-fi 1.81 * Default constructor
278 pam-fi 1.1 */
279 pam-fi 1.81 PamTrack::PamTrack() {
280     trk_track = 0;
281     calo_track = 0;
282     tof_track = 0;
283     orb_track = 0;
284     candeleteobj = 0;
285     pscore = 0;
286     iscore = 0;
287     }
288     ;
289 pam-fi 1.1 //--------------------------------------
290     //
291     //
292     //--------------------------------------
293     /**
294 pam-fi 1.81 * Constructor
295 pam-fi 1.1 */
296 pam-fi 1.81 PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
297    
298     trk_track = 0;
299     calo_track = 0;
300     tof_track = 0;
301     orb_track = 0;
302     // if(t)trk_track = new TrkTrack(*t);
303     // if(c)calo_track = new CaloTrkVar(*c);
304     // if(o)tof_track = new ToFTrkVar(*o);
305     if (t)
306     trk_track = t;
307     if (c)
308     calo_track = c;
309     if (o)
310     tof_track = o;
311     if (r)
312     orb_track = r;
313    
314     candeleteobj = 0;
315    
316     }
317     ;
318     PamTrack::PamTrack(const PamTrack& track) {
319    
320     TrkTrack *t = track.trk_track;
321     CaloTrkVar *c = track.calo_track;
322     ToFTrkVar *o = track.tof_track;
323     OrbitalInfoTrkVar *r = track.orb_track;
324    
325     trk_track = 0;
326     calo_track = 0;
327     tof_track = 0;
328     orb_track = 0;
329     if (t)
330     trk_track = new TrkTrack(*t);
331     if (c)
332     calo_track = new CaloTrkVar(*c);
333     if (o)
334     tof_track = new ToFTrkVar(*o);
335     if (r)
336     orb_track = new OrbitalInfoTrkVar(*r);
337     candeleteobj = 1;
338    
339     }
340     void PamTrack::Clear() {
341 pam-fi 1.13
342 pam-fi 1.81 // cout << "PamTrack::Clear() "<<candeleteobj<<endl;
343     if (candeleteobj) {
344     if (trk_track)
345     trk_track->TrkTrack::Clear();
346     if (calo_track)
347     calo_track->CaloTrkVar::Clear();//???
348     if (tof_track)
349     tof_track->ToFTrkVar::Clear();//???
350     if (orb_track)
351     orb_track->OrbitalInfoTrkVar::Clear();//???
352     }
353     else {
354     trk_track = 0;
355 pam-fi 1.13 calo_track = 0;
356 pam-fi 1.81 tof_track = 0;
357     orb_track = 0;
358     }
359     pscore = 0;
360     iscore = 0;
361 pam-fi 1.13
362 pam-fi 1.81 }
363     void PamTrack::Delete() {
364     // cout << "PamTrack::Delete() "<<candeleteobj<<endl;
365     if (candeleteobj) {
366     if (trk_track) {
367     trk_track->TrkTrack::Clear();
368     delete trk_track;
369     }
370     if (calo_track) {
371     calo_track->CaloTrkVar::Clear();//???
372     delete calo_track;
373     }
374     if (tof_track) {
375     tof_track->ToFTrkVar::Clear();//???
376     delete tof_track;
377     }
378     if (orb_track) {
379     orb_track->OrbitalInfoTrkVar::Clear();//???
380     delete orb_track;
381 pam-fi 1.13 }
382 pam-fi 1.81 }
383     else {
384     Clear();
385     }
386 pam-fi 1.13 }
387 pam-fi 1.1 //--------------------------------------
388     //
389     //
390     //--------------------------------------
391     /**
392 pam-fi 1.81 * Default Constructor
393 pam-fi 1.1 */
394 pam-fi 1.81 PamLevel2::PamLevel2() {
395     Initialize();
396     }
397 mocchiut 1.90
398 mocchiut 1.35
399     /**
400 pam-fi 1.52 * Constructor
401     * @param ddir Name of directory where level2 files are stored.
402     * @param list Name of an ascii file containing the list of file names
403     * @param detlist Options to chose what to load.
404 pam-fi 1.81 * Possible options are:
405 pam-fi 1.52 * +AUTO --> load all trees/branches in the input files
406     * +(-)ALL --> inlcude(exclude) all trees/branches
407     * +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
408 pam-fi 1.81 * +(-)TRK0 --> include(exclude) tracker level0 tree
409 pam-fi 1.52 * +(-)GP --> include exclude GPAMELA output tree
410     * If no options are specified, the default is assumed. Default is:
411     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
412 mocchiut 1.35 */
413 pam-fi 1.81 PamLevel2::PamLevel2(TString ddir, TString llist, TString detlist) {
414     Initialize();
415     TList* listf = GetListOfLevel2Files(ddir, llist);
416     if (listf)
417     GetPamTree(listf, detlist);
418     if (listf)
419     GetRunTree(listf);
420 mocchiut 1.90 this->SetMaxShift(-1);
421 pam-fi 1.81 }
422 mocchiut 1.90
423 pam-fi 1.81
424     PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
425     Initialize();
426     GetPamTree(llist, detlist);
427     GetRunTree(llist);
428 mocchiut 1.90 this->SetMaxShift(-1);
429 pam-fi 1.81 }
430 mocchiut 1.90
431 mocchiut 1.35 /**
432 pam-fi 1.81 * Constructor
433 pam-fi 1.52 * @param ddir Name of directory where level2 files are stored.
434     * @param list Name of an ascii file containing the list of file names
435     * Default trees/branches are loaded. Default is:
436     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
437 mocchiut 1.35 */
438 pam-fi 1.81 PamLevel2::PamLevel2(TString ddir, TString llist) {
439     Initialize();
440     TList* listf = GetListOfLevel2Files(ddir, llist);
441     GetPamTree(listf, "");
442     GetRunTree(listf);
443 mocchiut 1.90 this->SetMaxShift(-1);
444 pam-fi 1.81 }
445 mocchiut 1.90
446 pam-fi 1.81
447     void PamLevel2::Initialize() {
448 mocchiut 1.35
449 pam-fi 1.81 h0_obj = 0;
450     trk0_obj = 0;
451     calo0_obj = 0;
452    
453     trk2_obj = 0;
454     trk1_obj = 0;
455     trkh_obj = 0;
456     calo1_obj = 0;
457     calo2_obj = 0;
458     tof_obj = 0;
459     trig_obj = 0;
460     s4_obj = 0;
461     nd_obj = 0;
462     ac_obj = 0;
463     orb_obj = 0;
464     gp_obj = 0;
465    
466     run_obj = 0;//new GL_RUN();
467     soft_obj = 0;// Emiliano
468     irun = -1LL;
469     irunt = -1LL;
470 mocchiut 1.89 totrunentry = 0LL;
471     totrunentrymax = 0LL;
472     totrunentrymin = 0LL;
473 pam-fi 1.81 runfirstentry = 0LL;
474     runlastentry = 0LL;
475     gltsync = 0; // Emiliano
476 mocchiut 1.85 fUpdateRunInfo = true; // Emiliano
477 mocchiut 1.90 isSync = false; // by default assume that the level2 file(s) is(are) not sinchronized between L0/DB and L2, that is we miss some packets in L2 due to nested/DV-skipped events
478     il0entry = 0LL;
479 mocchiut 1.91 // hasL0EE = true;
480 pam-fi 1.81
481     l0_file = NULL;
482     l0_tree = NULL;
483     iroot = -1;
484     dbc = 0;
485    
486     prevshift = 0;
487 mocchiut 1.90 yprevshift = 0;
488     maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
489 pam-fi 1.81
490     run_tree = NULL;
491     run_tree_clone = NULL;
492     sel_tree = NULL;
493     sel_tree_clone = NULL;
494    
495     irunentry = -1LL;
496     pam_tree = NULL;
497     for (Int_t i = 0; i < NCLONES; i++)
498     pam_tree_clone[i] = NULL;
499    
500     totdltime[0] = 0LL;
501     totdltime[1] = 0LL;
502     totdltime[2] = 0LL;
503    
504     host = "mysql://localhost/pamelaprod";
505     user = "anonymous";
506     psw = "";
507     const char *pamdbhost = gSystem->Getenv("PAM_DBHOST");
508     const char *pamdbuser = gSystem->Getenv("PAM_DBUSER");
509     const char *pamdbpsw = gSystem->Getenv("PAM_DBPSW");
510     if (!pamdbhost)
511     pamdbhost = "";
512     if (!pamdbuser)
513     pamdbuser = "";
514     if (!pamdbpsw)
515     pamdbpsw = "";
516     if (strcmp(pamdbhost, ""))
517     host = pamdbhost;
518     if (strcmp(pamdbuser, ""))
519     user = pamdbuser;
520     if (strcmp(pamdbpsw, ""))
521     psw = pamdbpsw;
522    
523     // sorted_tracks = 0;//new TRefArray();
524    
525     CAL0 = false;
526     CAL1 = true;
527     CAL2 = true;
528     TRK2 = true;
529     TRK1 = false;
530     TRK0 = false;
531     TRKh = false;
532     TRG = true;
533     TOF = true;
534     TOF0 = false;
535     S4 = true;
536     ND = true;
537     AC = true;
538     ORB = true;
539     GP = false;
540    
541     RUN = true;
542    
543     SELLI = -1;
544    
545     ISGP = false;
546    
547     DBG = false;
548    
549     tsorted = 0;
550     timage = 0;
551    
552     howtosort = "+CAL+TOF";
553     //howtosort = "+TOF";
554     sortthr = 100.;
555 mocchiut 1.35
556 pam-fi 1.81 issorted = false;
557     lastsorted = -1;
558 mocchiut 1.30
559 pam-fi 1.81 }
560     ;
561     /**
562     * Delete the event (act as Dtor)
563     */
564     void PamLevel2::Delete() {
565 pam-fi 1.20
566 pam-fi 1.81 if (run_obj)
567     delete run_obj;
568     if (soft_obj)
569     delete soft_obj; //Emiliano
570    
571     // cout << "void PamLevel2::Clear()"<<endl;
572     if (h0_obj)
573     delete h0_obj;
574     if (trk0_obj)
575     delete trk0_obj;
576     if (calo0_obj)
577     delete calo0_obj;
578     if (trk1_obj)
579     delete trk1_obj;
580     if (trk2_obj)
581     delete trk2_obj;
582     if (trkh_obj)
583     delete trkh_obj;
584     if (calo1_obj)
585     delete calo1_obj;
586     if (calo2_obj)
587     delete calo2_obj;
588     if (tof_obj)
589     delete tof_obj;
590     if (trig_obj)
591     delete trig_obj;
592     if (s4_obj)
593     delete s4_obj;
594     if (nd_obj)
595     delete nd_obj;
596     if (ac_obj)
597     delete ac_obj;
598     if (orb_obj)
599     delete orb_obj;
600     if (gp_obj)
601     delete gp_obj;
602 pam-fi 1.9
603 pam-fi 1.81 if (tsorted) {
604     tsorted->Delete();
605     delete tsorted;
606     }
607     if (timage) {
608     timage->Delete();
609     delete timage;
610     }
611 pam-fi 1.52
612 pam-fi 1.81 if (dbc) {
613     dbc->Close();
614     delete dbc;
615 mocchiut 1.82 dbc=0;
616 pam-fi 1.81 }
617    
618     if (gltsync)
619     delete gltsync;
620    
621     if (l0_file)
622     l0_file->Close();
623     // if(pam_tree)pam_tree->Delete();;
624 pam-fi 1.20
625 pam-fi 1.81 if (pam_tree) {
626     //
627     // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
628     //
629     TList *temp = pam_tree->GetListOfFriends();
630     TList *contents = new TList; // create chain friend list
631     contents->SetOwner();
632     TIter next(temp);
633     TChain *questo = 0;
634     while ((questo = (TChain*) next())) {
635     TString name = questo->GetName();
636     contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
637     };
638     //
639     // deleting the main chain
640     //
641     pam_tree->Delete();
642     //
643     // deleting the friends...
644     //
645     TIter next2(contents);
646     TChain *questa = 0;
647 mocchiut 1.83 while ( (questa = (TChain*)next2()) ) {
648 pam-fi 1.81 TString name = questa->GetName();
649     questa->Delete();
650     questa = NULL;
651     };
652     //
653     };
654     pam_tree = NULL;
655 pam-fi 1.8
656 pam-fi 1.81 if (run_tree)
657     run_tree->Delete();;
658     if (sel_tree)
659     sel_tree->Delete();;
660 pam-fi 1.88
661     // The following lines are commented out since they may generate a double delete error
662     // if the file containing the clone trees is closed. This is because the file owns the
663     // clone trees which are written into it, so it will delete them when it is closed; if
664     // also PamLevel2 will try to delete these trees, a double delete error will be generated
665     // when exiting from analysis program. (Nicola 28/11/2011)
666    
667     /*for (Int_t i = 0; i < NCLONES; i++)
668 pam-fi 1.81 if (pam_tree_clone[i])
669     pam_tree_clone[i]->Delete();;
670     if (run_tree_clone)
671     run_tree_clone->Delete();;
672     if (sel_tree_clone)
673 pam-fi 1.88 sel_tree_clone->Delete();;*/
674 pam-fi 1.43
675 pam-fi 1.81 if (irunoffset)
676     delete[] irunoffset;
677 pam-fi 1.59
678 pam-fi 1.81 }
679     ;
680 pam-fi 1.13
681 pam-fi 1.2 /**
682 pam-fi 1.81 * Clear the event (NB! does not deallocate objects)
683 pam-fi 1.2 */
684 pam-fi 1.81 void PamLevel2::Clear() {
685    
686     // cout << "void PamLevel2::Clear()"<<endl;
687 mocchiut 1.35
688 pam-fi 1.81 //
689     // This method is called once for every entry but RunInfo and SoftInfo do not change until the next run so we cannot clear them here unless we don't
690     // want to load them for each event even if they are the same...
691     //
692     // if(run_obj)delete run_obj;
693     // if(run_obj) run_obj->Clear(); // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
694     // if(soft_obj) soft_obj->Clear();
695    
696     if (h0_obj)
697     h0_obj->Clear();
698     // if(trk0_obj) trk0_obj->Clear();
699     if (trk1_obj)
700     trk1_obj->Clear();
701     if (trk2_obj)
702     trk2_obj->Clear();
703     if (trkh_obj)
704     trkh_obj->Clear();
705     if (calo0_obj)
706     calo0_obj->Clear();
707     if (calo1_obj)
708     calo1_obj->Clear();
709     if (calo2_obj)
710     calo2_obj->Clear();
711     if (tof_obj)
712     tof_obj->Clear();
713     if (trig_obj)
714     trig_obj->Clear();
715     if (s4_obj)
716     s4_obj->Clear();
717     if (nd_obj)
718     nd_obj->Clear();
719     if (ac_obj)
720     ac_obj->Clear();
721     if (orb_obj)
722     orb_obj->Clear();
723     if (gp_obj)
724     gp_obj->Clear();
725 pam-fi 1.20
726 pam-fi 1.81 // if(sorted_tracks)sorted_tracks->Clear();
727     // sorted_tracks.Clear();
728 pam-fi 1.13
729 pam-fi 1.81 if (tsorted) {
730     tsorted->Delete();
731     }
732     if (timage) {
733     timage->Delete();
734     }
735     }
736     ;
737 mocchiut 1.35
738 pam-fi 1.81 void PamLevel2::Reset() {
739     //
740     // First of all clear everything
741     //
742     Clear();
743     //
744     // close and reset chains and pointers
745     //
746     if (pam_tree) {
747 mocchiut 1.35 //
748 pam-fi 1.81 // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
749 pam-fi 1.55 //
750 pam-fi 1.81 TList *temp = pam_tree->GetListOfFriends();
751     TList *contents = new TList; // create chain friend list
752     contents->SetOwner();
753     TIter next(temp);
754     TChain *questo = 0;
755     while ((questo = (TChain*) next())) {
756     TString name = questo->GetName();
757     contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
758 pam-fi 1.55 };
759     //
760 pam-fi 1.81 // deleting the main chain
761 pam-fi 1.55 //
762 pam-fi 1.81 pam_tree->Delete();
763 pam-fi 1.55 //
764 pam-fi 1.81 // deleting the friends...
765 pam-fi 1.55 //
766 pam-fi 1.81 TIter next2(contents);
767     TChain *questa = 0;
768 mocchiut 1.83 while ( (questa = (TChain*) next2()) ) {
769 pam-fi 1.81 TString name = questa->GetName();
770     questa->Delete();
771     questa = NULL;
772     };
773 pam-fi 1.55 //
774 pam-fi 1.81 };
775     pam_tree = NULL;
776     //
777     if (run_tree)
778     run_tree->Delete();;
779     run_tree = NULL;
780     if (sel_tree)
781     sel_tree->Delete();;
782     sel_tree = NULL;
783     //
784     // Close file
785     //
786     if (l0_file)
787     l0_file->Close("R");
788     l0_file = NULL;
789     //
790     h0_obj = 0;
791     trk0_obj = 0;
792     calo0_obj = 0;
793     //
794     trk2_obj = 0;
795     trk1_obj = 0;
796     trkh_obj = 0;
797     calo1_obj = 0;
798     calo2_obj = 0;
799     tof_obj = 0;
800     trig_obj = 0;
801     s4_obj = 0;
802     nd_obj = 0;
803     ac_obj = 0;
804     orb_obj = 0;
805     gp_obj = 0;
806     //
807     // Reset run pointers
808     //
809     run_obj = 0;//new GL_RUN();
810     soft_obj = 0;// Emiliano
811     irun = -1;
812     irunt = -1;
813 mocchiut 1.89 totrunentry = 0LL;
814     totrunentrymax = 0LL;
815     totrunentrymin = 0LL;
816 pam-fi 1.81 runfirstentry = 0ULL;
817     runlastentry = 0ULL;
818 mocchiut 1.89 prevabstime = 0ULL;
819     prevpktnum = 0;
820     abstime = 0ULL;
821     pktnum = 0;
822     isFragment = false;
823 pam-fi 1.81 //
824     totdltime[0] = 0LL;
825     totdltime[1] = 0LL;
826     totdltime[2] = 0LL;
827     //
828     }
829     ;
830 pam-fi 1.2
831 pam-fi 1.81 Bool_t PamLevel2::IsGood(Bool_t strict) {
832     Bool_t goodev = true;
833 mocchiut 1.69 //
834 pam-fi 1.81 if (calo2_obj && !calo2_obj->IsGood(strict))
835     goodev = false;
836 mocchiut 1.69 //
837 pam-fi 1.81 if (strict) {
838     if (trk2_obj && trk2_obj->UnpackError() != 0)
839     goodev = false;
840     if (tof_obj && tof_obj->unpackError != 0)
841     goodev = false;
842     if (trig_obj && trig_obj->unpackError != 0)
843     goodev = false;
844     if (s4_obj && s4_obj->unpackError != 0)
845     goodev = false;
846     if (nd_obj && nd_obj->unpackError != 0)
847     goodev = false;
848     if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
849     goodev = false;
850     // if(orb_obj)
851     }
852     else {
853     if (nd_obj && nd_obj->unpackError != 0)
854     goodev = false;
855     if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
856     goodev = false;
857 mocchiut 1.69 };
858 pam-fi 1.81 return (goodev);
859     }
860     ;
861 pam-fi 1.2
862 mocchiut 1.86 void PamLevel2::SkipRunInfoUpdate(){
863     printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
864     fUpdateRunInfo = false;
865     this->SetSELLI(2);
866     printf(" ===============> W A R N I N G <================ \n");
867     printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
868     printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
869     if ( run_tree_clone ){
870     printf(" ===============> W A R N I N G <================ \n");
871     printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
872     printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");
873     };
874     }
875    
876 mocchiut 1.90 void PamLevel2::SetMaxShift(Int_t sh){
877     if ( sh >= 0 ){
878     printf("PamLevel2::SetMaxShift(Int_t) --WARNING-- the default is optimized by checking the level2 file\n it is strongly suggested to let PamLevel2 choose the max shift!\n");
879     maxshift = sh;
880     } else {
881     ULong64_t nev = this->GetEntries();
882     ULong64_t runnev = 0ULL;
883     for (Int_t r=0; r< run_tree->GetEntries();r++){
884     run_tree->GetEntry(r);//update runinfo
885     runnev += GetRunInfo()->NEVENTS;
886     }
887     maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
888     if ( (runnev-nev) == 0 ) isSync = true;
889     if (DBG) printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift);
890     // printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift); // TOGLITOGLI
891     }
892     }
893    
894 pam-fi 1.1 //--------------------------------------
895     //
896     //
897     //--------------------------------------
898 pam-fi 1.81 void *PamLevel2::GetPointerTo(const char* c) {
899 pam-fi 1.9
900 pam-fi 1.81 TString objname = c;
901 pam-fi 1.13
902 pam-fi 1.81 if (!objname.CompareTo("TrkLevel1")) {
903     if (!trk1_obj) {
904     trk1_obj = new TrkLevel1();
905     trk1_obj->Set();
906     }
907     return &trk1_obj;
908     };
909     if (!objname.CompareTo("TrkLevel2")) {
910     if (!trk2_obj) {
911     trk2_obj = new TrkLevel2();
912     trk2_obj->Set();
913     }
914     return &trk2_obj;
915     };
916     if (!objname.CompareTo("TrkHough")) {
917     if (!trkh_obj) {
918     trkh_obj = new TrkHough();
919     trkh_obj->Set();
920     }
921     return &trkh_obj;
922     };
923     if (!objname.CompareTo("CaloLevel1")) {
924     if (!calo1_obj)
925     calo1_obj = new CaloLevel1();
926     return &calo1_obj;
927     };
928     if (!objname.CompareTo("CaloLevel2")) {
929     if (!calo2_obj) {
930     calo2_obj = new CaloLevel2();
931     calo2_obj->Set();
932 pam-fi 1.13 };
933 pam-fi 1.81 return &calo2_obj;
934     };
935     if (!objname.CompareTo("ToFLevel2")) {
936     if (!tof_obj) {
937     tof_obj = new ToFLevel2();
938     tof_obj->Set();
939     }
940     return &tof_obj;
941     };
942     if (!objname.CompareTo("TrigLevel2")) {
943     if (!trig_obj)
944     trig_obj = new TrigLevel2();
945     return &trig_obj;
946     };
947     if (!objname.CompareTo("S4Level2")) {
948     if (!s4_obj)
949     s4_obj = new S4Level2();
950     return &s4_obj;
951     };
952     if (!objname.CompareTo("NDLevel2")) {
953     if (!nd_obj)
954     nd_obj = new NDLevel2();
955     return &nd_obj;
956     };
957     if (!objname.CompareTo("AcLevel2")) {
958     if (!ac_obj)
959     ac_obj = new AcLevel2();
960     return &ac_obj;
961     };
962     if (!objname.CompareTo("OrbitalInfo")) {
963     if (!orb_obj) {
964     orb_obj = new OrbitalInfo();
965     orb_obj->Set();
966     }
967     return &orb_obj;
968     };
969     // if(!objname.CompareTo("OrbitalInfo")){
970     // if(!orb_obj) orb_obj = new OrbitalInfo();
971     // return &orb_obj;
972     // };
973     if (!objname.CompareTo("GPamela")) {
974     if (!gp_obj)
975     gp_obj = new GPamela();
976     return &gp_obj;
977     };
978    
979     if (!objname.CompareTo("RunInfo"))
980     return &run_obj;
981 mocchiut 1.15
982 pam-fi 1.81 if (!objname.CompareTo("SoftInfo"))
983     return &soft_obj; // Emiliano
984 mocchiut 1.15
985 pam-fi 1.81 return NULL;
986     }
987     ;
988 pam-fi 1.9 //--------------------------------------
989     //
990     //
991     //--------------------------------------
992 pam-fi 1.1 /**
993 pam-fi 1.81 * Retrieves the calorimeter track matching the seqno-th tracker stored track.
994 pam-fi 1.3 * (If seqno = -1 retrieves the self-trigger calorimeter track)
995     */
996 pam-fi 1.81 CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno) {
997    
998     if (!calo2_obj)
999     return 0;
1000    
1001     if (calo2_obj->CaloLevel2::ntrk() == 0) {
1002     cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1003     << " but no Calorimeter tracks are stored" << endl;
1004     return NULL;
1005     };
1006    
1007     CaloTrkVar *c = 0;
1008     Int_t it_calo = 0;
1009    
1010     do {
1011     c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
1012     it_calo++;
1013     } while (c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
1014    
1015     if (!c || seqno != c->trkseqno) {
1016     c = 0;
1017     if (seqno != -1)
1018     cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1019     << " does not match Calorimeter stored tracks" << endl;
1020     };
1021     return c;
1022    
1023     }
1024     ;
1025 pam-fi 1.3 //--------------------------------------
1026 pam-fi 1.55 //
1027     //
1028 pam-fi 1.3 //--------------------------------------
1029     /**
1030 pam-fi 1.81 * Retrieves the ToF track matching the seqno-th tracker stored track.
1031 pam-fi 1.55 * (If seqno = -1 retrieves the tracker-independent tof track)
1032 pam-fi 1.3 */
1033 pam-fi 1.81 ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
1034    
1035     if (!tof_obj)
1036     return 0;
1037    
1038     if (tof_obj->ToFLevel2::ntrk() == 0) {
1039     cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
1040     << endl;
1041     return NULL;
1042     };
1043    
1044     ToFTrkVar *c = 0;
1045     Int_t it_tof = 0;
1046    
1047     do {
1048     c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
1049     it_tof++;
1050     } while (c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
1051    
1052     if (!c || seqno != c->trkseqno) {
1053     c = 0;
1054     if (seqno != -1)
1055     cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno
1056     << " does not match ToF stored tracks" << endl;
1057     };
1058     return c;
1059    
1060     }
1061     ;
1062 pam-fi 1.3
1063     //--------------------------------------
1064 pam-fi 1.55 //
1065     //
1066 pam-fi 1.3 //--------------------------------------
1067     /**
1068 pam-fi 1.81 * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
1069 mocchiut 1.64 * (If seqno = -1 retrieves the tracker-independent tof related track)
1070     */
1071 pam-fi 1.81 OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
1072    
1073     if (!orb_obj)
1074     return 0;
1075    
1076     if (orb_obj->OrbitalInfo::ntrk() == 0) {
1077     // // TRICK BEGIN
1078     // OrbitalInfoTrkVar *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1079     // Int_t nn = 0;
1080     // TClonesArray &tor = *orb_obj->OrbitalInfoTrk;
1081     // for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){
1082     // //
1083     // ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt);
1084     // if ( ptt->trkseqno != -1 ){
1085     // //
1086     // r->trkseqno = ptt->trkseqno;
1087     // //
1088     // r->Eij = 0;
1089     // //
1090     // r->Sij = 0;
1091     // //
1092     // r->pitch = -1000.;
1093     // //
1094     // r->cutoff = -1000.;
1095     // //
1096     // new(tor[nn]) OrbitalInfoTrkVar(*r);
1097     // nn++;
1098     // //
1099     // r->Clear();
1100     // //
1101     // };
1102     // };
1103     // delete r;
1104     // OrbitalInfoTrkVar *c = 0;
1105     // c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1106     // return c;
1107     // //TRICK END
1108     cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1109     << " but no OrbitalInfo tracks are stored" << endl;
1110     return NULL;
1111     };
1112    
1113     OrbitalInfoTrkVar *c = 0;
1114     Int_t it_tof = 0;
1115    
1116     do {
1117     c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1118     it_tof++;
1119     } while (c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());
1120    
1121     if (!c || seqno != c->trkseqno) {
1122     c = 0;
1123     if (seqno != -1)
1124     cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1125     << " does not match OrbitalInfo stored tracks" << endl;
1126     };
1127     return c;
1128    
1129     }
1130     ;
1131 mocchiut 1.64
1132     //--------------------------------------
1133     //
1134     //
1135     //--------------------------------------
1136     /**
1137 pam-fi 1.81 * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
1138 pam-fi 1.3 */
1139 pam-fi 1.81 PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t) {
1140    
1141     cout << "PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** " << endl;
1142     cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1143    
1144     CaloTrkVar *c = 0;
1145     ToFTrkVar *o = 0;
1146     OrbitalInfoTrkVar *r = 0;
1147    
1148     if (CAL2)
1149     c = GetCaloStoredTrack(t->GetSeqNo());
1150     if (TOF)
1151     o = GetToFStoredTrack(t->GetSeqNo());
1152     if (ORB)
1153     r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
1154    
1155     // if(t && c && o)track = new PamTrack(t,c,o);
1156     PamTrack *track = new PamTrack(t, c, o, r);
1157    
1158     return track;
1159    
1160     }
1161     ;
1162 pam-fi 1.3 //--------------------------------------
1163     //
1164     //
1165     //--------------------------------------
1166     /**
1167 pam-fi 1.81 * Retrieves the it-th stored track.
1168     * It override TrkLevel2::GetTrack(int it).
1169 pam-fi 1.1 * @param itrk Track number, ranging from 0 to GetNTracks().
1170     */
1171    
1172 pam-fi 1.81 PamTrack* PamLevel2::GetStoredTrack(Int_t itrk) {
1173    
1174     cout << "PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** " << endl;
1175     cout
1176     << "for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack) OrbitalInfo::GetOrbitalInfoTrkVar(Int_t notrack)"
1177     << endl;
1178     cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1179     PamTrack *track = 0;
1180    
1181     if (itrk >= 0 && itrk < trk2_obj->TrkLevel2::ntrk()) {
1182    
1183     TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
1184     track = GetPamTrackAlong(t);
1185    
1186     }
1187     else {
1188     cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo " << itrk << " does not exist (GetNTracks() = "
1189     << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1190     };
1191    
1192     return track;
1193 pam-fi 1.13
1194 pam-fi 1.1 }
1195     //--------------------------------------
1196     //
1197 mocchiut 1.37
1198     /**
1199 pam-fi 1.55 * Sort physical (tracker) tracks. Left here as backward compatibility method.
1200 mocchiut 1.37 **/
1201 pam-fi 1.81 void PamLevel2::SortTracks(TString how) {
1202     printf(
1203     " WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",
1204     how.Data());
1205     howtosort = how;
1206     SortTracks();
1207     }
1208     ;
1209 mocchiut 1.37
1210 pam-fi 1.1 //
1211     //--------------------------------------
1212     /**
1213 pam-fi 1.55 * Sort physical (tracker) tracks.
1214 pam-fi 1.3 * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1215     * Sorting cryteria:
1216 pam-fi 1.81 * TRK: lower chi**2
1217 pam-fi 1.3 * CAL: lower Y spatial residual on the first calorimeter plane
1218     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
1219 pam-fi 1.55 * S1: (ask Emiliano)
1220     * S2: (ask Emiliano)
1221     * S3: (ask Emiliano)
1222 pam-fi 1.81 * GP: more GP hits
1223 pam-fi 1.3 * The default sorting cryterium is "TOF+CAL".
1224 pam-fi 1.81 *
1225 pam-fi 1.3 * The total number of physical tracks is always given by GetNTracks() and the it-th physical track can be retrieved by means of the methods GetTrack(int it) and GetTrack(int it, TString how).
1226 pam-fi 1.1 */
1227 pam-fi 1.81 void PamLevel2::SortTracks() {
1228 pam-fi 1.55
1229 pam-fi 1.81 //Check if the current event has already been sorted
1230     if (issorted && lastsorted == GetReadEntry()) {
1231     return;
1232     }
1233     //Reset the sort flags, just in case something will go wrong...
1234     issorted = false;
1235     lastsorted = -1;
1236    
1237     TString how = howtosort;
1238    
1239     // cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1240     if (!trk2_obj) {
1241     cout << "void PamLevel2::SortTracks(): TrkLevel2 not loaded !!!";
1242     return;
1243     };
1244     //Save current Object count
1245     Int_t ObjectNumber = TProcessID::GetObjectCount();
1246 pam-fi 1.1
1247 pam-fi 1.81 // create TCloneArrays to store tracks and its images
1248     if (!tsorted)
1249     tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1250     tsorted->Delete();
1251     TClonesArray &ttsorted = *tsorted;
1252    
1253     if (!timage)
1254     timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1255     timage->Delete();
1256     TClonesArray &ttimage = *timage;
1257    
1258     //--------------------------------------------------
1259     // retrieve sorting method
1260     //--------------------------------------------------
1261     Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1262     Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1263     Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1264     Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1265     Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1266     Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1267     Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1268    
1269     if (use_TOF) {
1270     use_S1 = true;
1271     use_S2 = true;
1272     use_S3 = true;
1273     };
1274     if (!CAL2 && use_CAL)
1275     use_CAL = false;
1276     if (!TOF) {
1277     use_TOF = false;
1278     use_S1 = false;
1279     use_S2 = false;
1280     use_S3 = false;
1281     }
1282     if (!GP) {
1283     use_GP = false;
1284     }
1285    
1286     if (!TRK2) {
1287     cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1288     return;
1289     };
1290 pam-fi 1.55
1291 pam-fi 1.81 // cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
1292 pam-fi 1.13
1293    
1294 pam-fi 1.81 //--------------------------------------------------
1295     // loop over "physical" tracks sorted by the tracker
1296     //--------------------------------------------------
1297     for (Int_t i = 0; i < trk2_obj->TrkLevel2::GetNTracks(); i++) {
1298    
1299     TrkTrack *ts = 0;
1300     CaloTrkVar *cs = 0;
1301     ToFTrkVar *os = 0;
1302     OrbitalInfoTrkVar *rs = 0;
1303    
1304     // get tracker tracks
1305     TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1306     CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1307     ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
1308     OrbitalInfoTrkVar *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1309    
1310     TrkTrack *ti = 0; //tracker (image)
1311     CaloTrkVar *ci = 0;
1312     ToFTrkVar *oi = 0;
1313     OrbitalInfoTrkVar *ri = 0;
1314     // cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1315     // if track has an image, check image selection
1316    
1317     Int_t tp_score = 0; //main track sorted by the tracker
1318     Int_t ti_score = 0; //image track
1319     Int_t totp_score = 0; //main track sorted by the tracker
1320     Int_t toti_score = 0; //image track
1321    
1322     if (tp->HasImage()) {
1323    
1324     ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
1325     ci = GetCaloStoredTrack(ti->GetSeqNo());
1326     oi = GetToFStoredTrack(ti->GetSeqNo());
1327     ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1328    
1329     // cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1330    
1331     //assign starting scores
1332     tp_score = 0; //main track sorted by the tracker
1333     ti_score = 0; //image track
1334    
1335     // -----------------------------------------------------------------------------------------
1336     // *****************************************************************************************
1337     // -----------------------------------------------------------------------------------------
1338     // calorimeter check
1339     // -----------------------------------------------------------------------------------------
1340     if (use_CAL && !calo2_obj) {
1341     cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
1342     return;
1343     };
1344     if (use_CAL && !cp && ci) {
1345     ti_score++;
1346     toti_score++;
1347     };
1348     if (use_CAL && cp && !ci) {
1349     tp_score++;
1350     totp_score++;
1351     };
1352     if (use_CAL && cp && ci && true) {
1353    
1354     if (cp->npresh > ci->npresh && true) {
1355     tp_score++;
1356     totp_score++;
1357     };
1358     if (cp->npresh < ci->npresh && true) {
1359     ti_score++;
1360     toti_score++;
1361     };
1362    
1363     // cout << "CALO "<<tp_score<<ti_score<<endl;
1364    
1365     };
1366     // -----------------------------------------------------------------------------------------
1367     // *****************************************************************************************
1368     // -----------------------------------------------------------------------------------------
1369     // TOF check
1370     // -----------------------------------------------------------------------------------------
1371     // check the number of hit pmts along the track
1372     // on S12 S21 and S32, where paddles are parallel to Y axis
1373     if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof_obj) {
1374     cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1375     return;
1376     };
1377     //
1378     if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
1379     ti_score++;
1380     toti_score++;
1381     };
1382     if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
1383     tp_score++;
1384     totp_score++;
1385     };
1386     if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
1387     //
1388     Float_t sen = 0.;
1389     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1390     Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1391     if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1392     sen += (op->dedx).At(ih);
1393     };
1394     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1395     Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1396     if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1397     sen += (oi->dedx).At(ih);
1398     };
1399     //
1400     if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1401     //printf(" IS A NUCLEUS! en = %f \n",sen);
1402     //
1403     // is a nucleus use a different algorithm
1404     //
1405     Int_t nz = 6;
1406     Float_t zin[6]; // << define TOF z-coordinates
1407     for (Int_t ip = 0; ip < nz; ip++)
1408     zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1409     Trajectory *tr = new Trajectory(nz, zin);
1410     //
1411     Int_t nphit_p = 0;
1412     Int_t nphit_i = 0;
1413     Float_t enhit_p = 0.;
1414     Float_t enhit_i = 0.;
1415     //
1416     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1417     Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1418     if (pl == 1 || pl == 2 || pl == 5) {
1419     nphit_p++;
1420     enhit_p += (op->dedx).At(ih);
1421     };
1422     };
1423     //
1424     tp->DoTrack2(tr);
1425     //
1426     if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1427     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1428     Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1429     if (pl == 0) {
1430     nphit_p++;
1431     enhit_p += (op->dedx).At(ih);
1432     };
1433     };
1434     };
1435     if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1436     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1437     Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1438     if (pl == 3) {
1439     nphit_p++;
1440     enhit_p += (op->dedx).At(ih);
1441     };
1442     };
1443     };
1444     if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1445     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1446     Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1447     if (pl == 4) {
1448     nphit_p++;
1449     enhit_p += (op->dedx).At(ih);
1450     };
1451     };
1452     };
1453    
1454     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1455     Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1456     if (pl == 1 || pl == 2 || pl == 5) {
1457     nphit_i++;
1458     enhit_i += (op->dedx).At(ih);
1459     };
1460     };
1461     //
1462     ti->DoTrack2(tr);
1463     //
1464     if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1465     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1466     Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1467     if (pl == 0) {
1468     nphit_i++;
1469     enhit_i += (op->dedx).At(ih);
1470     };
1471     };
1472     };
1473     if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1474     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1475     Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1476     if (pl == 3) {
1477     nphit_i++;
1478     enhit_i += (op->dedx).At(ih);
1479     };
1480     };
1481     };
1482     if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1483     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1484     Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1485     if (pl == 4) {
1486     nphit_i++;
1487     enhit_i += (op->dedx).At(ih);
1488     };
1489     };
1490     };
1491    
1492     if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
1493    
1494     // printf(" seqno %i nphit_p %i nphit_i %i enhit_p %f enhit_i %f \n",trk2_obj->TrkLevel2::GetSeqNo(i),nphit_p,nphit_i,enhit_p,enhit_i);
1495     // printf(" score p %i score i %i \n",tp_score,ti_score);
1496     // if( enhit_p > enhit_i ) tp_score++;
1497     // if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1498     if (nphit_p > nphit_i)
1499     tp_score++;
1500     if (nphit_p < nphit_i)
1501     ti_score++;
1502     if (nphit_p == nphit_i) {
1503     if (enhit_p > enhit_i)
1504     tp_score++;
1505     else
1506     ti_score++;
1507     };
1508     // printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1509     };
1510     delete tr;
1511     //
1512     }
1513     else {
1514     // -------------
1515     // NOT a NUCLEUS
1516     // -------------
1517     //printf(" NOT a NUCLEUS! en = %f \n",sen);
1518    
1519     Int_t nphit_p = 0;
1520     Int_t nphit_i = 0;
1521    
1522     /* cout << "track: npmtadc "<< op->npmtadc << endl;
1523     cout << "track: npmttdc "<< op->npmttdc << endl;
1524     cout << "image: npmtadc "<< oi->npmtadc << endl;
1525     cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1526    
1527     // for (Int_t ih=0; ih < op->npmtadc; ih++){
1528     // Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1529     // if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1530     // };
1531    
1532     // for (Int_t ih=0; ih < oi->npmtadc; ih++){
1533     // Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1534     // if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1535     // };
1536     // --- modified to count tdc signals (more efficient?)
1537     // --- and to implement check on tdcflag
1538     for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1539     Int_t pl = tof_obj->GetPlaneIndex((op->pmttdc).At(ih));
1540     // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1541     if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1542     || (use_S3 && (pl == 4 || pl == 5))) {
1543     if ((op->tdcflag).At(ih) == 0)
1544     nphit_p++;
1545     };
1546     };
1547    
1548     for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1549     Int_t pl = tof_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1550     // if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
1551     if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1552     || (use_S3 && (pl == 4 || pl == 5))) {
1553     if ((oi->tdcflag).At(ih) == 0)
1554     nphit_i++;
1555     };
1556     };
1557    
1558     if ((nphit_p + nphit_i) != 0 && true) {
1559    
1560     if (nphit_p != nphit_i) {
1561     totp_score += nphit_p;
1562     toti_score += nphit_i;
1563     tp_score += nphit_p;
1564     ti_score += nphit_i;
1565     };
1566     // if ( nphit_p > nphit_i) tp_score+=nphit_p;
1567     // else if( nphit_p < nphit_i) ti_score+=nphit_i;
1568     // else ;//niente
1569     };
1570     };
1571     // cout << "TOF "<<tp_score<<ti_score<<endl;
1572     };
1573    
1574     // if(tp_score == ti_score) use_TRK = true;
1575    
1576    
1577     // -----------------------------------------------------------------------------------------
1578     // *****************************************************************************************
1579     // -----------------------------------------------------------------------------------------
1580     // tracker check
1581     // -----------------------------------------------------------------------------------------
1582     // chi**2 difference is not always large enough to distinguish among
1583     // the real track and its image.
1584     // Tracker check will be applied always when calorimeter and tof information is ambiguous.
1585     // *** MODIFIED ON AUGUST 2007 ***
1586     if (use_TRK) {
1587     // if( tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1588     // else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1589    
1590     // CHECK 1 : number of points along X
1591     if (tp->GetNX() >= ti->GetNX()) {
1592     tp_score++;
1593     totp_score++;
1594     };
1595     if (tp->GetNX() <= ti->GetNX()) {
1596     ti_score++;
1597     toti_score++;
1598     };
1599     // CHECK 2 : number of points along Y
1600     if (tp->GetNY() >= ti->GetNY()) {
1601     tp_score++;
1602     totp_score++;
1603     };
1604     if (tp->GetNY() <= ti->GetNY()) {
1605     ti_score++;
1606     toti_score++;
1607     };
1608    
1609     // cout << "TRK "<<tp_score<<ti_score<<endl;
1610     };
1611    
1612     // -----------------------------------------------------------------------------------------
1613     // *****************************************************************************************
1614     // -----------------------------------------------------------------------------------------
1615     // GPamela check
1616     // -----------------------------------------------------------------------------------------
1617    
1618     //---------------------------------------------------
1619     // count the number of GP hits
1620     //---------------------------------------------------
1621     if (use_GP) {
1622     int ngphits_p = 0;
1623     int ngphits_i = 0;
1624     float toll = 0.02; //200 micron
1625     for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
1626     int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1627     if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1628     ngphits_p++;
1629     if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1630     ngphits_i++;
1631     }
1632     if (ngphits_p > ngphits_i && true) {
1633     tp_score++;
1634     totp_score++;
1635     }
1636     if (ngphits_p < ngphits_i && true) {
1637     ti_score++;
1638     toti_score++;
1639     }
1640     }
1641    
1642     // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1643     // the winner is....
1644     // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1645     if (tp_score > ti_score) {
1646    
1647     }
1648     else if (tp_score < ti_score) {
1649    
1650     ts = ti;//its image!!
1651     cs = ci;
1652     os = oi;
1653     rs = ri;
1654     Int_t totis = toti_score;
1655    
1656     ti = tp;//its image!!
1657     ci = cp;
1658     oi = op;
1659     ri = rp;
1660    
1661     tp = ts;//its image!!
1662     cp = cs;
1663     op = os;
1664     rp = rs;
1665    
1666     toti_score = totp_score;
1667     totp_score = totis;
1668    
1669     }
1670     else {
1671    
1672     // cout << "Warning - track image ambiguity not solved" << endl;
1673    
1674     };
1675    
1676     }
1677     else {
1678     totp_score = 1;
1679     toti_score = 0;
1680    
1681     // ts = tp;
1682     // cs = cp;
1683     // os = op;
1684     };
1685    
1686     // cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1687     // sorted_tracks->Add(ts);//save the track in the sorted array
1688     // sorted_tracks.Add(ts);//save the track in the sorted array
1689     // sorted_tracks.Add(tp);//save the track in the sorted array
1690     // cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1691     // cout<<"o "<<tp<<endl;
1692     // cout<<"o "<<cp<<endl;
1693     // cout<<"o "<<op<<endl;
1694    
1695     new (ttsorted[i]) PamTrack(tp, cp, op, rp);
1696     new (ttimage[i]) PamTrack(ti, ci, oi, ri);
1697    
1698     ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
1699     ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
1700     ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
1701     ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
1702     };
1703 pam-fi 1.41
1704 pam-fi 1.81 if (tsorted->GetEntries() != trk2_obj->GetNTracks()) {
1705     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
1706     << " != trk2_obj->GetNTracks() = " << trk2_obj->GetNTracks() << endl;
1707     tsorted->Delete();
1708     tsorted = 0;
1709     timage->Delete();
1710     timage = 0;
1711     }
1712 pam-fi 1.55
1713 pam-fi 1.81 //Restore Object count
1714     //To save space in the table keeping track of all referenced objects
1715     //We reset the object count to what it was at the beginning of the event.
1716     TProcessID::SetObjectCount(ObjectNumber);
1717    
1718     //Everything went fine so the current event can be tagged as sorted
1719     issorted = true;
1720     lastsorted = GetReadEntry();
1721 pam-fi 1.9
1722 pam-fi 1.81 }
1723     ;
1724 pam-fi 1.1 //--------------------------------------
1725     //
1726     //
1727     //--------------------------------------
1728     /**
1729 pam-fi 1.2 * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1730     * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1731     */
1732 pam-fi 1.13 // TRefArray *PamLevel2::GetTracks(){
1733    
1734     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1735     // SortTracks("+CAL+TOF");
1736     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1737    
1738     // // return sorted_tracks;
1739     // return &sorted_tracks;
1740 pam-fi 1.81
1741 pam-fi 1.13 // };
1742 pam-fi 1.81 TClonesArray *PamLevel2::GetTracks() {
1743    
1744     // *-*-*-*-*-*-*-*-*-*-*-*-*
1745     SortTracks();
1746     // *-*-*-*-*-*-*-*-*-*-*-*-*
1747    
1748     return tsorted;
1749 pam-fi 1.8
1750 pam-fi 1.81 }
1751     ;
1752 pam-fi 1.2 //--------------------------------------
1753 pam-fi 1.55 //
1754     //
1755 pam-fi 1.2 //--------------------------------------
1756     /**
1757 pam-fi 1.81 * Retrieves the it-th Pamela "physical" track.
1758     * It override TrkLevel2::GetTrack(int it).
1759 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
1760     */
1761 pam-fi 1.81 PamTrack *PamLevel2::GetTrack(int it) {
1762    
1763     // if(!trk2_obj) return 0;
1764    
1765     // // *-*-*-*+-*-*-*-*-*-*-*-*-*
1766     // SortTracks("+CAL+TOF");
1767     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1768     // // if(!sorted_tracks)return 0;
1769     // if(sorted_tracks.IsEmpty())return 0;
1770    
1771     // PamTrack *track = 0;
1772    
1773     // if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){
1774     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1775     // track = GetPamTrackAlong(t);
1776     // }else{
1777     // cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1778     // };
1779    
1780     // return track;
1781    
1782     // cout << "PamLevel2::GetTrack(int it) "<<endl;
1783     // *-*-*-*-*-*-*-*-*-*-*-*-*
1784     SortTracks();
1785     // *-*-*-*-*-*-*-*-*-*-*-*-*
1786     if (!tsorted)
1787     return 0;
1788     if (!tsorted->GetEntries())
1789     return 0;
1790    
1791     PamTrack *track = 0;
1792    
1793     if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
1794     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1795     // track = GetPamTrackAlong(t);
1796     //TClonesArray &t = *(tsorted);
1797     //track = (PamTrack*)t[it];
1798     track = (PamTrack*)((*tsorted)[it]);
1799     }
1800     else {
1801     cout << "PamLevel2::GetTrack(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
1802     << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1803     };
1804 pam-fi 1.13
1805 pam-fi 1.81 return track;
1806 pam-fi 1.13
1807 pam-fi 1.81 }
1808     ;
1809 pam-fi 1.3
1810 pam-fi 1.1 //--------------------------------------
1811     //
1812     //
1813     //--------------------------------------
1814     /**
1815 pam-fi 1.81 * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
1816 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
1817     */
1818 pam-fi 1.81 PamTrack *PamLevel2::GetTrackImage(int it) {
1819 pam-fi 1.1
1820 pam-fi 1.81 // *-*-*-*-*-*-*-*-*-*-*-*-*
1821     SortTracks();
1822     // *-*-*-*-*-*-*-*-*-*-*-*-*
1823     if (!timage)
1824     return 0;
1825     if (!timage->GetEntries())
1826     return 0;
1827    
1828     PamTrack *image = 0;
1829    
1830     if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
1831     TClonesArray &t = *(tsorted);
1832     PamTrack *temp = (PamTrack*) t[it];
1833     if (temp->GetTrkTrack()->HasImage()) {
1834     TClonesArray & t = *(timage);
1835     image = (PamTrack*) t[it];
1836     }
1837     else {
1838     // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1839     };
1840     }
1841     else {
1842     cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
1843     << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1844     };
1845    
1846     return image;
1847 pam-fi 1.1 }
1848    
1849     //--------------------------------------
1850     //
1851     //
1852     //--------------------------------------
1853     /**
1854 pam-fi 1.2 * Get the Pamela detector trees in a single file and make them friends.
1855     * @param f TFile pointer
1856     * @param detlist String to select trees to be included
1857     * @return Pointer to a TTree
1858 pam-fi 1.1 */
1859 pam-fi 1.81 TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
1860    
1861     if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
1862     cout << "+AUTO" << endl;
1863     GetWhichTrees(f);
1864     };
1865     SetWhichTrees(detlist);
1866    
1867     if (pam_tree) {
1868     printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1869     return pam_tree;
1870     };
1871     //
1872 pam-fi 1.4
1873 pam-fi 1.81 cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte " << endl;
1874    
1875     // SetWhichTrees(detlist);
1876 pam-fi 1.52
1877 pam-fi 1.81 TTree *Trout = 0;
1878 mocchiut 1.35
1879 pam-fi 1.81 TString fname = f->GetName();
1880     if (!CheckLevel2File(fname))
1881     return NULL;
1882 pam-fi 1.20
1883 pam-fi 1.81 // UInt_t *found=0;
1884 pam-fi 1.9
1885 pam-fi 1.81 cout << "GetPamTree(TFile*,TString): detector list --> ";
1886     if (TRK1)
1887     cout << "TRK1 ";
1888     if (TRK2)
1889     cout << "TRK2 ";
1890     if (TRKh)
1891     cout << "TRKH ";
1892     if (CAL1)
1893     cout << "CAL1 ";
1894     if (CAL2)
1895     cout << "CAL2 ";
1896     if (TOF)
1897     cout << "TOF ";
1898     if (TRG)
1899     cout << "TRG ";
1900     if (AC)
1901     cout << "AC ";
1902     if (ND)
1903     cout << "ND ";
1904     if (S4)
1905     cout << "S4 ";
1906     if (ORB)
1907     cout << "ORB ";
1908     if (GP)
1909     cout << "GP ";
1910     cout << endl;
1911     if (SELLI && SELLI != 2)
1912     cout << ">>> Found selection-list <<<" << endl; //EMILIANO
1913    
1914     f->cd();
1915    
1916     // Tracker
1917     TTree *T = (TTree*) f->Get("Tracker");
1918     if (T && (TRK2 || TRK1 || TRKh)) {
1919     if (TRK2)
1920     T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1921     // else T->SetBranchStatus("TrkLevel2",0,found);
1922     if (TRK2)
1923     cout << "Tracker : set branch address TrkLevel2" << endl;
1924     if (TRK1)
1925     T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1926     // else T->SetBranchStatus("TrkLevel1",0,found);
1927     if (TRK1)
1928     cout << "Tracker : set branch address TrkLevel1" << endl;
1929     if (TRKh)
1930     T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1931     // else T->SetBranchStatus("TrkHough",0,found);
1932     if (TRKh)
1933     cout << "Tracker : set branch address TrkHough" << endl;
1934     if (!Trout)
1935     Trout = T;
1936     else
1937     Trout->AddFriend(T);
1938     }
1939     else {
1940     cout << "Tracker : missing tree" << endl;
1941     };
1942     // Calorimeter
1943     TTree *C = (TTree*) f->Get("Calorimeter");
1944     if (C && (CAL2 || CAL1)) {
1945     if (CAL2)
1946     C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1947     // else C->SetBranchStatus("CaloLevel2",0,found);
1948     if (CAL2)
1949     cout << "Calorimeter : set branch address CaloLevel2" << endl;
1950     if (CAL1)
1951     C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1952     // else C->SetBranchStatus("CaloLevel1",0,found);
1953     if (CAL1)
1954     cout << "Calorimeter : set branch address CaloLevel1" << endl;
1955     if (!Trout)
1956     Trout = C;
1957     else
1958     Trout->AddFriend(C);
1959     }
1960     else {
1961     cout << "Calorimeter : missing tree" << endl;
1962     };
1963 pam-fi 1.9
1964 pam-fi 1.81 // ToF
1965     TTree *O = (TTree*) f->Get("ToF");
1966     if (O && TOF) {
1967     O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1968     cout << "ToF : set branch address ToFLevel2" << endl;
1969     if (!Trout)
1970     Trout = O;
1971     else
1972     Trout->AddFriend(O);
1973     }
1974     else {
1975     cout << "ToF : missing tree" << endl;
1976     };
1977     // Trigger
1978     TTree *R = (TTree*) f->Get("Trigger");
1979     if (R && TRG) {
1980     R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1981     cout << "Trigger : set branch address TrigLevel2" << endl;
1982     if (!Trout)
1983     Trout = R;
1984     else
1985     Trout->AddFriend(R);
1986     }
1987     else {
1988     cout << "Trigger : missing tree" << endl;
1989     };
1990     // S4
1991     TTree *S = (TTree*) f->Get("S4");
1992     if (S && S4) {
1993     S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1994     cout << "S4 : set branch address S4Level2" << endl;
1995     if (!Trout)
1996     Trout = S;
1997     else
1998     Trout->AddFriend(S);
1999     }
2000     else {
2001     cout << "S4 : missing tree" << endl;
2002     };
2003     // Neutron Detector
2004     TTree *N = (TTree*) f->Get("NeutronD");
2005     if (N && ND) {
2006     N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
2007     cout << "NeutronD : set branch address NDLevel2" << endl;
2008     if (!Trout)
2009     Trout = N;
2010     else
2011     Trout->AddFriend(N);
2012     }
2013     else {
2014     cout << "NeutronD : missing tree" << endl;
2015     };
2016     // Anticounters
2017     TTree *A = (TTree*) f->Get("Anticounter");
2018     if (A && AC) {
2019     A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
2020     cout << "Anticounter : set branch address AcLevel2" << endl;
2021     if (!Trout)
2022     Trout = A;
2023     else
2024     Trout->AddFriend(A);
2025     }
2026     else {
2027     cout << "Anticounter : missing tree" << endl;
2028     };
2029     // Orbital Info
2030     TTree *B = (TTree*) f->Get("OrbitalInfo");
2031     if (B && ORB) {
2032     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2033     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
2034     if (!Trout)
2035     Trout = B;
2036     else
2037     Trout->AddFriend(B);
2038     }
2039     else {
2040     cout << "OrbitalInfo : missing tree" << endl;
2041     };
2042 pam-fi 1.9
2043 pam-fi 1.81 // GPamela
2044     TTree *G = (TTree*) f->Get("h20");
2045     if (G && GP) {
2046     if (!gp_obj)
2047     gp_obj = new GPamela();
2048     // ------------------------------------
2049     // ATTENZIONE!!!
2050     // non so per quale arcano motivo,
2051     // per l'albero di gpamela il branch address lo devo settare
2052     // DOPO aver fatto friend
2053     // FGRRRVZZZZUTSALKJMSLKJ!!!
2054     // ------------------------------------
2055     // gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
2056     // cout << "h20 : set branch address GPamela "<<endl;
2057     if (!Trout)
2058     Trout = G;
2059     else
2060     Trout->AddFriend(G);
2061     }
2062     else {
2063     // cout << "h20 : missing tree"<<endl;
2064     };
2065 pam-fi 1.12
2066 pam-fi 1.81 TTree *L = (TTree*) f->Get("SelectionList");
2067     if (L && SELLI == 1) {
2068     cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!" << endl;
2069     sel_tree = 0;
2070     }
2071     else {
2072     cout << "SelectionList : missing tree" << endl;
2073     };
2074 pam-fi 1.20
2075 pam-fi 1.81 cout << endl << " Number of entries: " << Trout->GetEntries() << endl << endl;
2076 pam-fi 1.52
2077 pam-fi 1.81 // ------------------------------------
2078     // ATTENZIONE!!!
2079     // non so per quale arcano motivo,
2080     // per l'albero di gpamela il branch address lo devo settare
2081     // DOPO aver fatto friend
2082     // FGRRRVZZZZUTSALKJMSLKJ!!!
2083     // ------------------------------------
2084     if (G && GP) {
2085     gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
2086     cout << "h20 : set branch address GPamela " << endl;
2087     }
2088     else {
2089     cout << "h20 : missing tree" << endl;
2090     };
2091 pam-fi 1.52
2092 pam-fi 1.81 pam_tree = (TChain*) Trout;
2093 mocchiut 1.28
2094 pam-fi 1.81 return Trout;
2095 pam-fi 1.52
2096 pam-fi 1.1 }
2097 pam-fi 1.2 //--------------------------------------
2098     //
2099     //
2100     //--------------------------------------
2101     /**
2102     * Get list of Level2 files.
2103     * @param ddir Level2 data directory.
2104     * @param flisttxt Name of txt file containing file list.
2105     * @return Pointer to a TList of TSystemFiles
2106     * If no input file list is given , all the Level2 files inside the directory are processed.
2107     */
2108 pam-fi 1.81 TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = "") {
2109    
2110     TString wdir = gSystem->WorkingDirectory();
2111    
2112     if (ddir != "") {
2113     cout << "Level2 data directory : " << ddir << endl;
2114     }
2115     else {
2116     cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory "
2117     << endl;
2118     };
2119     TList *contents = new TList; // create output list
2120     contents->SetOwner();
2121    
2122     // --------------------------------------
2123     // case 1 : input file/file-list provided
2124     // --------------------------------------
2125     if (flisttxt != "") {
2126    
2127 pam-fi 1.79 // --------------------------------------
2128 pam-fi 1.81 // a list of files given as input
2129 pam-fi 1.79 // --------------------------------------
2130 pam-fi 1.81 if (!flisttxt.EndsWith(".root")) {
2131    
2132     TString dir = gSystem->DirName(flisttxt);
2133     // cout << " List directory "<<dir<<endl;
2134     if (dir == "." || dir == "")
2135     flisttxt = gSystem->ConcatFileName(wdir.Data(), gSystem->BaseName(flisttxt));
2136     // flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
2137    
2138     if (!gSystem->ChangeDirectory(ddir)) {
2139     cout << "Cannot change directory : " << ddir << endl;
2140     return 0;
2141     }
2142    
2143     cout << "Input file list : " << flisttxt << endl;
2144     ifstream in;
2145     in.open(flisttxt, ios::in); //open input file list
2146     if (!in.good()) {
2147     cout << " TList* PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file " << endl;
2148     gSystem->ChangeDirectory(wdir); // back to the working directory
2149     return 0;
2150     }
2151     int line = 0;
2152     // ...........................
2153     // loop over file-list records
2154     // ...........................
2155     while (1) {
2156     TString file;
2157     in >> file;
2158     if (!in.good())
2159     break;
2160     line++;
2161     cout << "(*) " << file << endl;
2162     if (file.IsNull()) {
2163     cout << "-- list interrupted at line " << line << endl;
2164     break;
2165     }
2166     if (file.Contains("#"))
2167     file = file(0, file.First("#"));
2168     //
2169     // take only root files
2170     //
2171     if (file.EndsWith(".root")) {
2172     TString filedir;
2173     cout << ddir << endl;
2174 mocchiut 1.84 if ( ddir != "" ) {
2175 pam-fi 1.81 filedir = ddir; // take the input dir
2176     }
2177     else {
2178     gSystem->ChangeDirectory(wdir); // back to the working directory
2179     filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
2180     };
2181     filedir.Append("/");
2182 mocchiut 1.84 // char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
2183     char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
2184 pam-fi 1.81 contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
2185     cout << fullpath << endl;
2186     delete fullpath;
2187     }
2188     // }else{
2189     // if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
2190     // };
2191     };
2192     in.close();
2193     // --------------------------------------
2194     // a single root file given as input
2195     // --------------------------------------
2196     }
2197     else {
2198     if (flisttxt.Contains("#"))
2199     flisttxt = flisttxt(0, flisttxt.First("#"));
2200     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt), gSystem->BaseName(flisttxt));
2201     contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
2202     delete fullpath;
2203     };
2204 pam-fi 1.79 // ---------------------------------------------------------------------------------
2205     // case 2 : no input file/file-list provided --> read all files insede the directory
2206     // ---------------------------------------------------------------------------------
2207 pam-fi 1.81 }
2208     else {
2209    
2210     cout << "No input file list given." << endl;
2211     cout << "Check for existing root files." << endl;
2212     // cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
2213     if (ddir == "") {
2214     ddir = wdir;
2215     cout << "Level2 data directory : " << ddir << endl;
2216 pam-fi 1.6 };
2217 pam-fi 1.81
2218     TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir), ddir);
2219     TList *temp = datadir->GetListOfFiles();
2220     if (!temp)
2221     return 0;
2222     // temp->Print();
2223     // cout << "*************************************" << endl;
2224    
2225     TIter next(temp);
2226     TSystemFile *questo = 0;
2227    
2228     while ((questo = (TSystemFile*) next())) {
2229     TString name = questo-> GetName();
2230     if (name.EndsWith(".root")) {
2231     // const char *fullpath = gSystem->FindFile(ddir,name);
2232     // char *fullpath;
2233     // gSystem->IsFileInIncludePath(name,&fullpath);
2234     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir), gSystem->BaseName(name));
2235     contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));
2236     delete fullpath;
2237     };
2238     }
2239     delete temp;
2240     delete datadir;
2241    
2242     };
2243     gSystem->ChangeDirectory(wdir); // back to the working directory
2244     // cout << endl << "Selected files:" << endl;
2245     // contents->Print();
2246     cout << contents->GetEntries() << " files \n";
2247     // cout << endl;
2248     // cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
2249     return contents;
2250     }
2251     ;
2252 pam-fi 1.2 //--------------------------------------
2253     //
2254     //
2255     //--------------------------------------
2256     /**
2257     * Get the Pamela detector chains from a list of files and make them friends.
2258 pam-fi 1.81 * @param fl Pointer to a TList of TSystemFiles
2259 pam-fi 1.2 * @param detlist String to select trees to be included
2260     * @return Pointer to a TChain
2261     */
2262 pam-fi 1.81 TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) {
2263     //
2264     //
2265     //
2266     if (pam_tree) {
2267     printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
2268     return pam_tree;
2269     };
2270     //
2271 pam-fi 1.2
2272 pam-fi 1.81 TChain *Trout = 0;
2273 pam-fi 1.52
2274 pam-fi 1.81 // -------------------------------------------
2275     // set flags to include/exclude trees/branches
2276     // -------------------------------------------
2277     if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
2278     if (fl->GetEntries() > 0) {
2279     cout << "+AUTO" << endl;
2280     TFile *fprimo = new TFile(fl->At(0)->GetName());
2281     GetWhichTrees(fprimo);
2282     fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
2283     fprimo->Delete();
2284     }
2285     };
2286     SetWhichTrees(detlist);
2287 pam-fi 1.52
2288 pam-fi 1.81 // -------------------------------------------
2289     // build chains
2290     // -------------------------------------------
2291     TChain *T = 0;
2292     TChain *C = 0;
2293     TChain *O = 0;
2294     TChain *R = 0;
2295     TChain *S = 0;
2296     TChain *N = 0;
2297     TChain *A = 0;
2298     TChain *B = 0;
2299     TChain *G = 0;
2300    
2301     TChain *L = 0;
2302    
2303     if (TRK2 || TRK1 || TRKh)
2304     T = new TChain("Tracker");
2305     if (CAL2 || CAL1)
2306     C = new TChain("Calorimeter");
2307     if (TOF)
2308     O = new TChain("ToF");
2309     if (TRG)
2310     R = new TChain("Trigger");
2311     if (S4)
2312     S = new TChain("S4");
2313     if (ND)
2314     N = new TChain("NeutronD");
2315     if (AC)
2316     A = new TChain("Anticounter");
2317     if (ORB)
2318     B = new TChain("OrbitalInfo");
2319     if (GP)
2320     G = new TChain("h20");
2321     L = new TChain("SelectionList");
2322    
2323     // loop over files and create chains
2324     TIter next(fl);
2325     TSystemFile *questo = 0;
2326     while ((questo = (TSystemFile*) next())) {
2327     TString name = questo->GetName();
2328     cout << "File: " << name << endl;
2329     if (CheckLevel2File(name)) {
2330     if (TRK2 || TRK1 || TRKh)
2331     T->Add(name);
2332     if (CAL1 || CAL2)
2333     C->Add(name);
2334     if (TOF)
2335     O->Add(name);
2336     if (TRG)
2337     R->Add(name);
2338     if (S4)
2339     S->Add(name);
2340     if (ND)
2341     N->Add(name);
2342     if (AC)
2343     A->Add(name);
2344     if (ORB)
2345     B->Add(name);
2346     if (GP)
2347     G->Add(name);
2348     if (SELLI == 1)
2349     L->Add(name);
2350 mocchiut 1.35 };
2351 pam-fi 1.81 };
2352 pam-fi 1.11
2353 pam-fi 1.81 cout << "done chains\n";
2354     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2355 pam-fi 1.56
2356 pam-fi 1.81 // -------------------------------------------
2357     // make friends
2358     // -------------------------------------------
2359    
2360     // Tracker
2361     cout << "Friends: " << endl;
2362     if (T && (TRK2 || TRK1 || TRKh)) {
2363     if (!Trout)
2364     Trout = T;
2365     else
2366     Trout->AddFriend("Tracker");
2367     // cout << "+Tacker"<<endl;
2368     };
2369     // Calorimeter
2370     if (C && (CAL2 || CAL1)) {
2371     if (!Trout)
2372     Trout = C;
2373     else
2374     Trout->AddFriend("Calorimeter");
2375     // cout << "+Calorimeter"<<endl;
2376     };
2377     // ToF
2378     if (O && TOF) {
2379     if (!Trout)
2380     Trout = O;
2381     else
2382     Trout->AddFriend("ToF");
2383     // cout << "+ToF"<<endl;
2384     };
2385     // Trigger
2386     if (R && TRG) {
2387     if (!Trout)
2388     Trout = O;
2389     else
2390     Trout->AddFriend("Trigger");
2391     // cout << "+Trigger"<<endl;
2392     };
2393     // S4
2394     if (S && S4) {
2395     if (!Trout)
2396     Trout = O;
2397     else
2398     Trout->AddFriend("S4");
2399     // cout << "+S4"<<endl;
2400     };
2401     // Neutron Detector
2402     if (N && ND) {
2403     if (!Trout)
2404     Trout = O;
2405     else
2406     Trout->AddFriend("NeutronD");
2407     // cout << "+NeutronD"<<endl;
2408     };
2409     // Anticounters
2410     if (A && AC) {
2411     if (!Trout)
2412     Trout = O;
2413     else
2414     Trout->AddFriend("Anticounter");
2415     // cout << "+Anticounter"<<endl;
2416     };
2417     // Orbital Info
2418     if (B && ORB) {
2419     if (!Trout)
2420     Trout = O;
2421     else
2422     Trout->AddFriend("OrbitalInfo");
2423     // cout << "+OrbitalInfo"<<endl;
2424     };
2425     // GPamela
2426     if (G && GP) {
2427     if (!Trout)
2428     Trout = G;
2429     else
2430     Trout->AddFriend("h20");
2431     // cout << "+h20"<<endl;
2432     };
2433 pam-fi 1.52
2434 pam-fi 1.81 // =====================================
2435     // SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
2436     // =====================================
2437     SetBranchAddress(Trout);
2438    
2439     // ------------------------------------
2440     // finally handle selection trees...
2441     // (it is not friend of pamela tree)
2442     // ------------------------------------
2443    
2444     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2445    
2446     // Selection List
2447     if (L && SELLI == 1) {
2448     cout << ">>> Found selection-list <<<" << endl;
2449     // L->SetBranchAddress("RunEntry",&irun);
2450     L->SetBranchAddress("RunEntry", &irunt);//NEWNEW
2451     cout << "SelectionList: set branch address RunEntry" << endl;
2452     L->SetBranchAddress("EventEntry", &irunentry);
2453     cout << "SelectionList: set branch address EventEntry" << endl;
2454 mocchiut 1.91 /* if ( L->GetBranch("L0EventEntry") ){
2455 mocchiut 1.90 hasL0EE = true;
2456     L->SetBranchAddress("L0EventEntry", &il0entry);
2457     cout << "SelectionList: set branch address L0EventEntry" << endl;
2458     } else {
2459     hasL0EE = false; // backward compatibility with old preselected files...
2460 mocchiut 1.91 }*/
2461 pam-fi 1.81 sel_tree = L;
2462     // if(!Trout)Trout=O;
2463     // else Trout->AddFriend("SelectionList");
2464     cout << "+SelectionList" << endl;
2465     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2466     }
2467     else {
2468     // cout << "SelectionList : missing tree"<<endl;
2469     if (L)
2470     L->Delete();
2471     };
2472 pam-fi 1.52
2473 pam-fi 1.81 // --------------------------------------------
2474     // return the pamela chain with all the friends
2475     // --------------------------------------------
2476 pam-fi 1.8
2477 pam-fi 1.81 pam_tree = Trout;
2478 pam-fi 1.8
2479 pam-fi 1.81 return Trout;
2480     }
2481 pam-fi 1.8
2482 pam-fi 1.4 //--------------------------------------
2483     //
2484     //
2485     //--------------------------------------
2486     /**
2487     * Set branch addresses for Pamela friend trees
2488     */
2489 pam-fi 1.81 void PamLevel2::SetBranchAddress(TTree *t) {
2490    
2491     TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
2492     TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
2493     TRKh = TRKh & t->GetBranchStatus("TrkHough");
2494     CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
2495     CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
2496     TOF = TOF & t->GetBranchStatus("ToFLevel2");
2497     TRG = TRG & t->GetBranchStatus("TrigLevel2");
2498     S4 = S4 & t->GetBranchStatus("S4Level2");
2499     ND = ND & t->GetBranchStatus("NDLevel2");
2500     AC = AC & t->GetBranchStatus("AcLevel2");
2501     ORB = ORB & t->GetBranchStatus("OrbitalInfo");
2502     GP = GP & t->GetBranchStatus("h20");
2503    
2504     // Tracker
2505     if (TRK1) {
2506     t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
2507     cout << "Tracker : set branch address TrkLevel1" << endl;
2508     };
2509     if (TRK2) {
2510     t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
2511     cout << "Tracker : set branch address TrkLevel2" << endl;
2512     };
2513     if (TRKh) {
2514     t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2515     cout << "Tracker : set branch address TrkHough" << endl;
2516     };
2517    
2518     // Calorimeter
2519     if (CAL1) {
2520     t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
2521     cout << "Calorimeter : set branch address CaloLevel1" << endl;
2522     };
2523     if (CAL2) {
2524     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
2525     cout << "Calorimeter : set branch address CaloLevel2" << endl;
2526     };
2527 pam-fi 1.8
2528 pam-fi 1.81 // ToF
2529     if (TOF) {
2530     t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
2531     cout << "ToF : set branch address ToFLevel2" << endl;
2532     };
2533     // Trigger
2534     if (TRG) {
2535     t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
2536     cout << "Trigger : set branch address TrigLevel2" << endl;
2537     };
2538     // S4
2539     if (S4) {
2540     t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
2541     cout << "S4 : set branch address S4Level2" << endl;
2542     };
2543     // Neutron Detector
2544     if (ND) {
2545     t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
2546     cout << "NeutronD : set branch address NDLevel2" << endl;
2547     };
2548     // Anticounters
2549     if (AC) {
2550     t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
2551     cout << "Anticounter : set branch address AcLevel2" << endl;
2552     };
2553     // OrbitalInfo
2554     if (ORB) {
2555     t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2556     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
2557     };
2558     // GPamela
2559     if (GP) {
2560     // GetPointerTo("GPamela");
2561     if (!gp_obj)
2562     gp_obj = new GPamela();
2563     // gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2564     // // t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2565     if (SELLI)
2566     t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2567     else
2568     gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2569    
2570     cout << "h20 : set branch address GPamela " << endl;
2571     };
2572 pam-fi 1.61
2573 pam-fi 1.4 }
2574 pam-fi 1.8 /**
2575     * Set branch addresses for Pamela friend trees
2576     */
2577 pam-fi 1.81 void PamLevel2::SetBranchAddress(TChain *t) {
2578    
2579     // TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
2580     // TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
2581     // TRKh = TRKh & t->GetBranchStatus("TrkHough");
2582     // CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
2583     // CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
2584     // TOF = TOF & t->GetBranchStatus("ToFLevel2");
2585     // TRG = TRG & t->GetBranchStatus("TrigLevel2");
2586     // S4 = S4 & t->GetBranchStatus("S4Level2");
2587     // ND = ND & t->GetBranchStatus("NDLevel2");
2588     // AC = AC & t->GetBranchStatus("AcLevel2");
2589     // ORB = ORB & t->GetBranchStatus("OrbitalInfo");
2590     // GP = GP & t->GetBranchStatus("h20");
2591    
2592     // Tracker
2593     if (TRK2) {
2594     t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2595     cout << "Tracker : set branch address TrkLevel2" << endl;
2596     };
2597     if (TRK1) {
2598     t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2599     cout << "Tracker : set branch address TrkLevel1" << endl;
2600     };
2601     if (TRKh) {
2602     t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2603     cout << "Tracker : set branch address TrkHough" << endl;
2604     };
2605    
2606     // Calorimeter
2607     if (CAL2) {
2608     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
2609     cout << "Calorimeter : set branch address CaloLevel2" << endl;
2610     };
2611     if (CAL1) {
2612     t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
2613     cout << "Calorimeter : set branch address CaloLevel1" << endl;
2614     };
2615    
2616     // ToF
2617     if (TOF) {
2618     t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
2619     cout << "ToF : set branch address ToFLevel2" << endl;
2620     };
2621     // Trigger
2622     if (TRG) {
2623     t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
2624     cout << "Trigger : set branch address TrigLevel2" << endl;
2625     };
2626     // S4
2627     if (S4) {
2628     t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
2629     cout << "S4 : set branch address S4Level2" << endl;
2630     };
2631     // Neutron Detector
2632     if (ND) {
2633     t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
2634     cout << "NeutronD : set branch address NDLevel2" << endl;
2635     };
2636     // Anticounters
2637     if (AC) {
2638     t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
2639     cout << "Anticounter : set branch address AcLevel2" << endl;
2640     };
2641     // OrbitalInfo
2642     if (ORB) {
2643     t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2644     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
2645     };
2646     // GPamela
2647     // cout <<"GP "<<GP<<endl;
2648     if (GP) {
2649     // GetPointerTo("GPamela");
2650     if (!gp_obj)
2651     gp_obj = new GPamela();
2652     if (SELLI)
2653     t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2654     else
2655     gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2656     // gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2657     cout << "h20 : set branch address GPamela " << endl;
2658     };
2659     // SelectionList
2660     // if(SELLI==1){
2661     // t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2662     // cout << "SelectionList: set branch address RunEntry"<<endl;
2663     // t->SetBranchAddress("EventEntry",&irunentry);
2664     // cout << "SelectionList: set branch address EventEntry"<<endl;
2665 pam-fi 1.4
2666 pam-fi 1.81 // }
2667 pam-fi 1.56
2668 pam-fi 1.8 }
2669 pam-fi 1.9
2670 pam-fi 1.4 //--------------------------------------
2671     //
2672     //
2673     //--------------------------------------
2674     /**
2675     * Get the Run tree chain from a list of files.
2676 pam-fi 1.81 * @param fl Pointer to a TList of TSystemFiles
2677 pam-fi 1.4 * @return Pointer to a TChain
2678     */
2679 pam-fi 1.81 TChain *PamLevel2::GetRunTree(TList *fl) {
2680     //
2681     //
2682     //
2683     if (run_tree) {
2684     printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
2685     return run_tree;
2686     };
2687     //
2688    
2689    
2690     TChain *R = new TChain("Run");
2691 pam-fi 1.60
2692 pam-fi 1.81 // loop over files and create chains
2693     TIter next(fl);
2694     TSystemFile *questo = 0;
2695     while ((questo = (TSystemFile*) next())) {
2696     TString name = questo->GetName();
2697     // cout << "File: "<< name << endl;
2698     if (CheckLevel2File(name)) {
2699     R->Add(name);
2700     };
2701     }
2702    
2703     if (RUN && R->GetNtrees()) {
2704    
2705     R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2706     cout << "Run : set branch address RunInfo" << endl;
2707     R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2708     cout << "Software : set branch address SoftInfo" << endl; // Emiliano
2709    
2710     irunoffset = new int[R->GetNtrees()];
2711     if (DBG) {
2712     cout << "----------------------------------------------------" << endl;
2713     cout << "irun\t | ";
2714     cout << "tree\t |";
2715     // cout << "offset\t |";
2716     cout << "RUN\t";
2717     cout << "FRAG\t";
2718     cout << "NEVENTS\t";
2719     cout << "absolute time\t\t\t";
2720     cout << "on-board time";
2721     cout << endl;
2722     }
2723     for (Int_t ii = 0; ii < R->GetEntries(); ii++) {
2724     R->GetEntry(ii);
2725     if (DBG) {
2726     cout << ii << "\t | ";
2727     cout << R->GetTreeNumber() << "\t |";
2728     // cout << R->GetChainOffset()<< "\t |";
2729     cout << GetRunInfo()->ID << "\t";
2730     cout << GetRunInfo()->ID_RUN_FRAG << "\t";
2731     cout << GetRunInfo()->NEVENTS << "\t";
2732     cout << GetRunInfo()->RUNHEADER_TIME << " <---> " << GetRunInfo()->RUNTRAILER_TIME << "\t";
2733     cout << GetRunInfo()->RUNHEADER_OBT << " <---> " << GetRunInfo()->RUNTRAILER_OBT << "\t";
2734     cout << endl;
2735     }
2736     irunoffset[R->GetTreeNumber()] = R->GetChainOffset();
2737     }
2738     cout << "N.run = " << R->GetEntries() << endl;
2739     cout << "----------------------------------------------------" << endl;
2740    
2741     }
2742     else {
2743     delete R;
2744     R = 0;
2745     }
2746 pam-fi 1.60
2747 pam-fi 1.81 run_tree = R;
2748 pam-fi 1.20
2749 pam-fi 1.81 return R;
2750 pam-fi 1.20
2751 pam-fi 1.4 }
2752     //--------------------------------------
2753     //
2754     //
2755     //--------------------------------------
2756     /**
2757     * Get the Run tree from a file.
2758 pam-fi 1.81 * @param f Pointer to a TFile
2759 pam-fi 1.4 * @return Pointer to a TTree
2760     */
2761 pam-fi 1.81 TTree *PamLevel2::GetRunTree(TFile *f) {
2762     if (run_tree) {
2763     printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
2764     return run_tree;
2765     };
2766    
2767     cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte " << endl;
2768    
2769     TTree *T = (TTree*) f->Get("Run");
2770 pam-fi 1.4
2771 pam-fi 1.81 if (T) {
2772     T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2773     cout << "Run : set branch address RunInfo" << endl;
2774     T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2775     cout << "Software : set branch address SoftInfo" << endl; // Emiliano
2776 pam-fi 1.4
2777 pam-fi 1.81 }
2778 pam-fi 1.56
2779 pam-fi 1.81 run_tree = (TChain*) T;
2780 mocchiut 1.28
2781 pam-fi 1.81 return T;
2782 mocchiut 1.28
2783 pam-fi 1.2 }
2784 mocchiut 1.89
2785 mocchiut 1.90 Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
2786 pam-fi 1.36
2787 mocchiut 1.90 if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
2788 pam-fi 1.36
2789 pam-fi 1.81 if (!run_tree) {
2790     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
2791     return false;
2792     }
2793     if (run_tree->GetEntries() <= 0) {
2794     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty" << endl;
2795     return (false);
2796     }
2797    
2798 mocchiut 1.89
2799     Int_t oldrun = irun; // store current run index
2800    
2801     // -----------------------------------------------------------------------
2802     // the first time the routine is called, set run search from the beginning
2803     // -----------------------------------------------------------------------
2804    
2805     if (irun < 0) {
2806     irun = 0LL;
2807     irunt = 0LL;
2808     totrunentry = 0LL;
2809     totrunentrymin = 0LL;
2810     totrunentrymax = 0LL;
2811 mocchiut 1.90 irunentry = 0LL;
2812     il0entry = 0LL;
2813 mocchiut 1.89 prevshift = 0;
2814 mocchiut 1.90 yprevshift = 0;
2815 mocchiut 1.89 prevabstime = 0;
2816     prevpktnum = 0;
2817     abstime = 0ULL;
2818     pktnum = 0;
2819     isFragment = false;
2820     run_tree->GetEntry(irun);
2821     if (!GetOrbitalInfo())
2822     cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
2823     if (gltsync)
2824     delete gltsync; //Emiliano
2825     if (!dbc || (dbc && !dbc->IsConnected()))
2826     SetDBConnection(); //Emiliano
2827     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
2828     if (dbc){
2829     dbc->Close();// Emiliano
2830     delete dbc;
2831     dbc=0;
2832     };
2833     };
2834     // ---------------------------------------------------------------
2835     // retrieve OBT and absolute time of the event
2836     // ---------------------------------------------------------------
2837     Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
2838     prevabstime = abstime;
2839     prevpktnum = pktnum;
2840     if (GetOrbitalInfo()) {
2841     abstime = GetOrbitalInfo()->absTime;
2842     obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
2843     pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
2844     }
2845     else {
2846     abstime = GetRunInfo()->RUNHEADER_TIME;
2847     obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
2848     pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
2849     }
2850    
2851     if (DBG){
2852     printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2853     printf("0 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2854     printf("0 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2855     printf("0 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2856 mocchiut 1.90 printf("0 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
2857 mocchiut 1.89 printf("0 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2858     }
2859    
2860     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2861     // if it is a full file (not preselected)
2862     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2863     if (SELLI == 0 || SELLI == 2) { // Emiliano
2864    
2865     // ---------------------------------------------------------------
2866     // increment dead and live-time counters
2867     // (only when reading a file not preselected)
2868     // ---------------------------------------------------------------
2869     if (SELLI == 0) {
2870     if (GetTrigLevel2()) {
2871     totdltime[0] += GetTrigLevel2()->dltime[0];
2872     totdltime[1] += GetTrigLevel2()->dltime[1];
2873     }
2874     totdltime[2]++;
2875     }
2876    
2877     //
2878     if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
2879     || iev == 0 // or it is the first entry
2880 mocchiut 1.90 || (!isSync && (
2881     (abstime <= GetRunInfo()->RUNHEADER_TIME && obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) // or it is outside obt limits (and abstime limits for security reasons)
2882     || (abstime >= GetRunInfo()->RUNTRAILER_TIME && obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) ))// or it is outside obt limits (and abstime limits for security reasons)
2883     ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2884 mocchiut 1.89
2885     // check for a new run (ma prima il primo!)
2886     if (DBG){
2887     printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2888     printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2889     printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2890     printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2891 mocchiut 1.90 printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
2892 mocchiut 1.89 printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2893     }
2894 mocchiut 1.90 // printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2895     // printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2896     // printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2897     // printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2898     // printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
2899     // printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
2900 mocchiut 1.89
2901     totrunentry = 0LL;
2902 mocchiut 1.90 runfirstentry = 0LL;
2903 mocchiut 1.89 for (Int_t r=0; r< run_tree->GetEntries();r++){
2904     // -------------------------------------------------------------------
2905     // save the index of the first entry of the run, relative to pam_tree,
2906     // and read a new run
2907     // -------------------------------------------------------------------
2908     run_tree->GetEntry(r);//update runinfo
2909     if ( r > 0 ){
2910     totrunentrymin = totrunentrymax+1;
2911     } else {
2912     totrunentrymin = 0LL;
2913     }
2914     totrunentry += GetRunInfo()->NEVENTS;
2915 mocchiut 1.90 totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
2916 mocchiut 1.89 irun = r;
2917    
2918 mocchiut 1.90 if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
2919     ( !isSync &&
2920     ( abstime >= GetRunInfo()->RUNHEADER_TIME && obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) // or it is inside obt limits (and abstime limits for security reasons)
2921     && abstime <= GetRunInfo()->RUNTRAILER_TIME && obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT))) // or it is inside obt limits (and abstime limits for security reasons)
2922     ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2923     if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
2924     if ( !isSync ){
2925     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2926     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2927     // printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2928     // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2929     prevshift += (totrunentrymin-iev); // add the new shift to total shift
2930     totrunentrymin -= (totrunentrymin-iev); // shift run position min
2931     totrunentrymax -= (totrunentrymin-iev); // shift run position max
2932     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2933     // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2934     } else {
2935     printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
2936     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
2937     cout << "\nFor bug reporting instructions, please see for example:\n";
2938     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
2939     cout << " " << endl;
2940     }
2941     }
2942     runfirstentry = totrunentrymin; // first entry of the run in the level2
2943    
2944 mocchiut 1.89
2945     //
2946     if (gltsync)
2947     delete gltsync; // Emiliano
2948     if (!dbc || (dbc && !dbc->IsConnected()))
2949     SetDBConnection(); //Emiliano
2950     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
2951 mocchiut 1.92 TrkParams::Set(GetRunInfo(), dbc);
2952 mocchiut 1.89 if (dbc){
2953     dbc->Close(); // Emiliano
2954     delete dbc;
2955     dbc=0;
2956     }
2957     if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
2958     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
2959     << " has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
2960     cout
2961     << " (NB!! in this case some events could be assigned to a wrong run)"
2962     << endl;
2963     }
2964     //
2965     if (DBG) printf(" found \n");
2966 mocchiut 1.90 // printf(" found \n");//TOGLITOGLI
2967 mocchiut 1.89 //
2968     break;
2969     }
2970     } // loop over run
2971    
2972     // --------------------------------------
2973     // if there was no need to update the run
2974     // ---> exit with FALSE
2975     // --------------------------------------
2976 mocchiut 1.90 if (irun == oldrun){
2977     if (DBG) printf(" no new run \n");
2978     // printf(" no new run \n");//TOGLITOGLI
2979 mocchiut 1.89 return (false);
2980 mocchiut 1.90 }
2981 mocchiut 1.89 // --------------------------------------
2982     // ... otherwise
2983     // ---> exit with TRUE
2984     // --------------------------------------
2985    
2986     if (SELLI != 2) {
2987     /// decrement counters since this event belongs to a new run
2988     if (GetTrigLevel2()) {
2989     totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
2990     totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
2991     }
2992     totdltime[2]--; //event counter
2993     if (DBG) {
2994     cout << endl;
2995     cout << "n.events : " << totdltime[2] << endl;
2996     cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
2997     cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
2998     }
2999     // add an entry
3000     if (run_tree_clone && totdltime[2] > 0)
3001     if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3002     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3003     // reset counters
3004     if ( totdltime[2] > 0 ){
3005     if (GetTrigLevel2()) {
3006     totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
3007     totdltime[1] = 0; //dead-time
3008     }
3009     totdltime[2] = 1; //event counter
3010     }
3011     }
3012    
3013     if (DBG){
3014     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
3015 mocchiut 1.90 << endl;
3016 mocchiut 1.89 printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3017     printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3018     printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3019     printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3020 mocchiut 1.90 printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
3021 mocchiut 1.89 printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
3022     }
3023 mocchiut 1.90 // printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3024     // printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3025     // printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3026     // printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3027     // printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
3028     // printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
3029 mocchiut 1.89
3030     return (true);
3031     } // need for run upgrade
3032     if (DBG) printf("return false\n");
3033     return (false);
3034     }// SELLI = 0 SELLI = 2
3035    
3036     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3037     // if it is a preselected file (there is SelectionList)
3038     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3039     // irun = run entry relative to the chain
3040     // irunt = run entry relative to the tree
3041     if (SELLI == 1) {
3042     sel_tree->GetEntry(iev);// read irunt from SelectionList
3043     irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
3044     if (irun != oldrun) {
3045     if (irun < run_tree->GetEntries())
3046     run_tree->GetEntry(irun);
3047     // check if the time is ok (with merged files it is not...)
3048     // if not loop over run and look for the proper entry
3049     bool SECONDO_GIRO = false;
3050     // Long64_t irun_start = irun;
3051     int offset_start = irunoffset[sel_tree->GetTreeNumber()];
3052     while (((!(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
3053     abstime <= GetRunInfo()->RUNTRAILER_TIME)
3054     // ||
3055     // !(obt >= GetRunInfo()->RUNHEADER_OBT && // additional check on OBT (ms)
3056     // obt <= GetRunInfo()->RUNTRAILER_OBT)
3057     ) || GetRunInfo()->NEVENTS == 0)
3058     // && irun < run_tree->GetEntries()
3059     ) {
3060    
3061     if (DBG) {
3062     cout << " (test) ";
3063     cout << " tree " << sel_tree->GetTreeNumber();
3064     cout << " irunt " << irunt;
3065     cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
3066     cout << " abs " << abstime;
3067     cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
3068     cout << " obt " << obt;
3069     cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
3070     cout << " *** JUMP RUN *** irun " << irun;
3071     cout << endl;
3072     }
3073     // irun++;
3074     irunoffset[sel_tree->GetTreeNumber()]++;
3075     irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
3076     if (irun == run_tree->GetEntries() && SECONDO_GIRO) {
3077     // if(irun == irun_start ){
3078     cout << " ...grrrvzzkhhhajsdkj!!!! " << endl;
3079     irunoffset[sel_tree->GetTreeNumber()] = offset_start;
3080     return false;
3081     }
3082     if (irun >= run_tree->GetEntries() || irun < 0) {
3083     cout << "irun = " << irun << " >> search from the beginning... <<" << endl;
3084     SECONDO_GIRO = true;
3085     irun = 0;
3086     irunoffset[sel_tree->GetTreeNumber()] = -irunt;
3087     }
3088     run_tree->GetEntry(irun);
3089     }
3090    
3091     if (DBG) {
3092     cout << " (test) ";
3093     cout << " tree " << sel_tree->GetTreeNumber();
3094     cout << " irunt " << irunt;
3095     cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
3096     cout << " abs " << abstime;
3097     cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
3098     cout << " obt " << obt;
3099     cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
3100     }
3101     if (DBG)
3102     cout << endl;
3103     if (DBG)
3104     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " (run-entry "
3105     << irun << ")" << endl;
3106     // ----------------------------------------------------
3107     // update the tracker parameters
3108     // (non ho trovato nessun altro modo sicuro di farlo...)
3109     // ----------------------------------------------------
3110     if (!dbc || (dbc && !dbc->IsConnected()))
3111     SetDBConnection();
3112     TrkParams::Set(GetRunInfo(), dbc);
3113     if (dbc){
3114     dbc->Close();
3115     delete dbc;
3116     dbc=0;
3117     };
3118     // cout << endl;
3119     prevshift = 0;
3120 mocchiut 1.90 yprevshift = 0;
3121 mocchiut 1.89 return true;
3122     }
3123     return false;
3124     }
3125    
3126     return false;
3127     //
3128     }
3129    
3130 mocchiut 1.90 /**
3131     * Update the runinfo informations (to be used to have Run infos event by event basis)
3132     * @param run Pointer to the chain/tree which contains run infos
3133     * @return true if a new run has been read, false if it is still the same run
3134     */
3135     Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
3136     return (UpdateRunInfo(iev));
3137 pam-fi 1.81 }
3138 mocchiut 1.89
3139 mocchiut 1.17 /**
3140     * Update the runinfo informations (to be used to have Run infos event by event basis)
3141 pam-fi 1.81 * @param run Pointer to the chain/tree which contains run infos
3142 mocchiut 1.17 * @return true if a new run has been read, false if it is still the same run
3143     */
3144 pam-fi 1.81 Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
3145     return (UpdateRunInfo((TChain*) run, iev));
3146     }
3147 mocchiut 1.89
3148 mocchiut 1.17
3149 pam-fi 1.2 //--------------------------------------
3150     //
3151     //
3152     //--------------------------------------
3153     /**
3154 pam-fi 1.8 * Set which trees shoul be analysed
3155 pam-fi 1.81 * @param detlist TString containing the sequence of trees required
3156 pam-fi 1.55 */
3157 pam-fi 1.81 void PamLevel2::SetWhichTrees(TString detlist) {
3158    
3159     // if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
3160     if (detlist.Contains("+ALL", TString::kIgnoreCase)) {
3161    
3162     cout << " ======================================================== " << endl;
3163     cout << " (( WARNING )) " << endl;
3164     cout << " The meaning of the option +ALL has changed!! " << endl;
3165     cout << " Now it includes really all (level0+level1+level2+gpamela)" << endl;
3166     cout << " and the file is discarded if it does not contain " << endl;
3167     cout << " all trees or if level0 files are not available!! " << endl;
3168     cout << " ======================================================== " << endl;
3169    
3170     CAL0 = true;
3171     CAL1 = true;
3172     CAL2 = true;
3173     TRK2 = true;
3174     TRK1 = true;
3175     TRKh = true;
3176     TRK0 = true;
3177     TRG = true;
3178     TOF = true;
3179     TOF0 = true;
3180     S4 = true;
3181     ND = true;
3182     AC = true;
3183     ORB = true;
3184     GP = true;
3185     }
3186     else if (detlist.Contains("-ALL", TString::kIgnoreCase)) {
3187     CAL0 = false;
3188     CAL1 = false;
3189     CAL2 = false;
3190     TRK2 = false;
3191     TRK1 = false;
3192     TRKh = false;
3193     TRK0 = false;
3194     TRG = false;
3195     TOF = false;
3196     TOF0 = false;
3197     S4 = false;
3198     ND = false;
3199     AC = false;
3200     ORB = false;
3201     GP = false;
3202     };
3203    
3204     // -------------------------------------------------------------------------
3205     if (detlist.Contains("CAL1", TString::kIgnoreCase)) {
3206     if (detlist.Contains("-CAL1", TString::kIgnoreCase))
3207     CAL1 = false;
3208     if (detlist.Contains("+CAL1", TString::kIgnoreCase))
3209     CAL1 = true;
3210     };
3211    
3212     if (detlist.Contains("CAL0", TString::kIgnoreCase)) {
3213     if (detlist.Contains("-CAL0", TString::kIgnoreCase))
3214     CAL0 = false;
3215     if (detlist.Contains("+CAL0", TString::kIgnoreCase))
3216     CAL0 = true;
3217     };
3218    
3219     if (detlist.Contains("CAL2", TString::kIgnoreCase)) {
3220     if (detlist.Contains("-CAL2", TString::kIgnoreCase))
3221     CAL2 = false;
3222     if (detlist.Contains("+CAL2", TString::kIgnoreCase))
3223     CAL2 = true;
3224     };
3225    
3226     if (detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2)
3227     CAL2 = true;
3228     if (detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2) {
3229     CAL2 = false;
3230     CAL1 = false;
3231     }
3232     // -------------------------------------------------------------------------
3233     if (detlist.Contains("TRK0", TString::kIgnoreCase)) {
3234     if (detlist.Contains("-TRK0", TString::kIgnoreCase))
3235     TRK0 = false;
3236     if (detlist.Contains("+TRK0", TString::kIgnoreCase))
3237     TRK0 = true;
3238     };
3239    
3240     if (detlist.Contains("TRK1", TString::kIgnoreCase)) {
3241     if (detlist.Contains("-TRK1", TString::kIgnoreCase))
3242     TRK1 = false;
3243     if (detlist.Contains("+TRK1", TString::kIgnoreCase))
3244     TRK1 = true;
3245     };
3246    
3247     if (detlist.Contains("TRK2", TString::kIgnoreCase)) {
3248     if (detlist.Contains("-TRK2", TString::kIgnoreCase))
3249     TRK2 = false;
3250     if (detlist.Contains("+TRK2", TString::kIgnoreCase))
3251     TRK2 = true;
3252     };
3253    
3254     if (detlist.Contains("TRKh", TString::kIgnoreCase)) {
3255     if (detlist.Contains("-TRKh", TString::kIgnoreCase))
3256     TRKh = false;
3257     if (detlist.Contains("+TRKh", TString::kIgnoreCase))
3258     TRKh = true;
3259     };
3260    
3261     if (detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh)
3262     TRK2 = true;
3263     if (detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh) {
3264     TRK2 = false;
3265     TRK1 = false;
3266     TRKh = false;
3267     }
3268     // -------------------------------------------------------------------------
3269 pam-fi 1.20
3270 pam-fi 1.81 if (detlist.Contains("-TRG", TString::kIgnoreCase))
3271     TRG = false;
3272     else if (detlist.Contains("+TRG", TString::kIgnoreCase))
3273     TRG = true;
3274    
3275     if (detlist.Contains("-TOF", TString::kIgnoreCase))
3276     TOF = false;
3277     else if (detlist.Contains("+TOF", TString::kIgnoreCase))
3278     TOF = true;
3279 pam-fi 1.20
3280 pam-fi 1.81 if (detlist.Contains("-TOF0", TString::kIgnoreCase))
3281     TOF0 = false;
3282     else if (detlist.Contains("+TOF0", TString::kIgnoreCase))
3283     TOF0 = true;
3284 pam-fi 1.8
3285 pam-fi 1.81 if (detlist.Contains("-S4", TString::kIgnoreCase))
3286     S4 = false;
3287     else if (detlist.Contains("+S4", TString::kIgnoreCase))
3288     S4 = true;
3289    
3290     if (detlist.Contains("-ND", TString::kIgnoreCase))
3291     ND = false;
3292     else if (detlist.Contains("+ND", TString::kIgnoreCase))
3293     ND = true;
3294    
3295     if (detlist.Contains("-AC", TString::kIgnoreCase))
3296     AC = false;
3297     else if (detlist.Contains("+AC", TString::kIgnoreCase))
3298     AC = true;
3299    
3300     if (detlist.Contains("-ORB", TString::kIgnoreCase))
3301     ORB = false;
3302     else if (detlist.Contains("+ORB", TString::kIgnoreCase))
3303     ORB = true;
3304    
3305     if (detlist.Contains("-GP", TString::kIgnoreCase))
3306     GP = false;
3307     else if (detlist.Contains("+GP", TString::kIgnoreCase))
3308     GP = true;
3309    
3310     cout << "tree/branch list from input --> ";
3311     if (TRK0)
3312     cout << "TRK0 ";
3313     if (TRK1)
3314     cout << "TRK1 ";
3315     if (TRK2)
3316     cout << "TRK2 ";
3317     if (TRKh)
3318     cout << "TRKH ";
3319     if (CAL0)
3320     cout << "CAL0 ";
3321     if (CAL1)
3322     cout << "CAL1 ";
3323     if (CAL2)
3324     cout << "CAL2 ";
3325     if (TOF)
3326     cout << "TOF ";
3327     if (TRG)
3328     cout << "TRG ";
3329     if (AC)
3330     cout << "AC ";
3331     if (ND)
3332     cout << "ND ";
3333     if (S4)
3334     cout << "S4 ";
3335     if (ORB)
3336     cout << "ORB ";
3337     if (GP)
3338     cout << "GP ";
3339     cout << endl;
3340     // cout<< "Set detector list --> ";
3341     // if(TRK1)cout<<"TRK1 ";
3342     // if(TRK2)cout<<"TRK2 ";
3343     // if(TRKh)cout<<"TRKH ";
3344     // if(CAL1)cout<<"CAL1 ";
3345     // if(CAL2)cout<<"CAL2 ";
3346     // if(TOF0)cout<<"TOF0 ";
3347     // if(TOF)cout<<"TOF ";
3348     // if(TRG)cout<<"TRG ";
3349     // if(AC)cout<<"AC ";
3350     // if(ND)cout<<"ND ";
3351     // if(S4)cout<<"S4 ";
3352     // if(ORB)cout<<"ORB ";
3353     // cout << endl;
3354 pam-fi 1.11
3355 pam-fi 1.81 }
3356     ;
3357 pam-fi 1.11
3358     /**
3359     * Set tree/branch detector flags from the content of a tree
3360     */
3361 pam-fi 1.81 void PamLevel2::GetWhichTrees(TFile* f) {
3362 pam-fi 1.20
3363 pam-fi 1.81 // cout << "void PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
3364     // -----------
3365     // reset flags
3366     // -----------
3367     CAL1 = false;
3368     CAL2 = false;
3369     TRK2 = false;
3370     TRK1 = false;
3371     TRKh = false;
3372     TRG = false;
3373     TOF = false;
3374     S4 = false;
3375     ND = false;
3376     AC = false;
3377     ORB = false;
3378     GP = false;
3379    
3380     RUN = false;
3381    
3382     // cout << "Checking file: "<<f->GetName()<<endl;
3383     if (!f || f->IsZombie()) {
3384     cout << "File: " << f->GetName() << " Non valid root file" << endl;
3385     return;
3386     }
3387    
3388     TList *lk = f->GetListOfKeys();
3389     if (!lk)
3390     return;
3391     TIter next(lk);
3392     TKey *key = 0;
3393    
3394     Int_t nev = 0;
3395    
3396     while ((key = (TKey*) next())) {
3397    
3398     if (!strcmp(key->GetName(), "Run"))
3399     RUN = true;
3400    
3401     //=========================================================
3402     if (!strcmp(key->GetName(), "Trigger")) {
3403     TRG = true;
3404     Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
3405     if (nev && nevt != nev) {
3406     cout << "File: " << f->GetName() << " Trigger tree has " << nevt << " events instead of " << nev << endl;
3407     TRG = false;
3408     }
3409     else
3410     nev = nevt;
3411     }
3412     //=========================================================
3413     if (!strcmp(key->GetName(), "ToF")) {
3414     TOF = true;
3415     Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
3416     if (nev && nevt != nev) {
3417     cout << "File: " << f->GetName() << " ToF tree has " << nevt << " events instead of " << nev << endl;
3418     TOF = false;
3419     }
3420     else
3421     nev = nevt;
3422     }
3423     //=========================================================
3424     if (!strcmp(key->GetName(), "S4")) {
3425     S4 = true;
3426     Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
3427     if (nev && nevt != nev) {
3428     cout << "File: " << f->GetName() << " S4 tree has " << nevt << " events instead of " << nev << endl;
3429     S4 = false;
3430     }
3431     else
3432     nev = nevt;
3433     }
3434     //=========================================================
3435    
3436     if (!strcmp(key->GetName(), "NeutronD")) {
3437     ND = true;
3438     Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
3439     if (nev && nevt != nev) {
3440     cout << "File: " << f->GetName() << "NeutronD tree has " << nevt << " events instead of " << nev << endl;
3441     ND = false;
3442     }
3443     else
3444     nev = nevt;
3445     }
3446     //=========================================================
3447     if (!strcmp(key->GetName(), "Anticounter")) {
3448     AC = true;
3449     Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
3450     if (nev && nevt != nev) {
3451     cout << "File: " << f->GetName() << " Anticounter tree has " << nevt << " events instead of " << nev << endl;
3452     AC = false;
3453     }
3454     else
3455     nev = nevt;
3456     }
3457     //=========================================================
3458     if (!strcmp(key->GetName(), "OrbitalInfo")) {
3459     ORB = true;
3460     Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
3461     if (nev && nevt != nev) {
3462     cout << "File: " << f->GetName() << " OrbitalInfo tree has " << nevt << " events instead of " << nev << endl;
3463     ORB = false;
3464     }
3465     else
3466     nev = nevt;
3467     }
3468     //=========================================================
3469     if (!strcmp(key->GetName(), "Tracker")) {
3470     TTree *T = (TTree*) f->Get("Tracker");
3471     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3472     TString name = T->GetListOfBranches()->At(i)->GetName();
3473     if (!name.CompareTo("TrkLevel1"))
3474     TRK1 = true;
3475     if (!name.CompareTo("TrkLevel2"))
3476     TRK2 = true;
3477     if (!name.CompareTo("TrkHough"))
3478     TRKh = true;
3479     };
3480     Int_t nevt = T->GetEntries();
3481     if (nev && nevt != nev) {
3482     cout << "File: " << f->GetName() << " Tracker tree has " << nevt << " events instead of " << nev << endl;
3483     TRK1 = false;
3484     TRK2 = false;
3485     TRKh = false;
3486     }
3487     else
3488     nev = nevt;
3489     // T->Delete();
3490     };
3491     //=========================================================
3492     if (!strcmp(key->GetName(), "Calorimeter")) {
3493     TTree *T = (TTree*) f->Get("Calorimeter");
3494     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3495     TString name = T->GetListOfBranches()->At(i)->GetName();
3496     if (!name.CompareTo("CaloLevel1"))
3497     CAL1 = true;
3498     if (!name.CompareTo("CaloLevel2"))
3499     CAL2 = true;
3500     };
3501     Int_t nevt = T->GetEntries();
3502     if (nev && nevt != nev) {
3503     cout << "File: " << f->GetName() << " Calorimeter tree has " << nevt << " events instead of " << nev << endl;
3504     CAL1 = false;
3505     CAL2 = false;
3506     }
3507     else
3508     nev = nevt;
3509     // T->Delete();
3510     };
3511     //=========================================================
3512     if (!strcmp(key->GetName(), "h20")) {
3513     GP = true;
3514     Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
3515     if (nev && nevt != nev) {
3516     cout << "File: " << f->GetName() << " h20 tree has " << nevt << " events instead of " << nev << endl;
3517     GP = false;
3518     }
3519     else
3520     nev = nevt;
3521     }
3522 pam-fi 1.20
3523 pam-fi 1.81 };
3524 pam-fi 1.11
3525 pam-fi 1.81 // delete lk;
3526    
3527     cout << "tree/branch list from file --> ";
3528     if (TRK1)
3529     cout << "TRK1 ";
3530     if (TRK2)
3531     cout << "TRK2 ";
3532     if (TRKh)
3533     cout << "TRKH ";
3534     if (CAL1)
3535     cout << "CAL1 ";
3536     if (CAL2)
3537     cout << "CAL2 ";
3538     if (TOF)
3539     cout << "TOF ";
3540     if (TRG)
3541     cout << "TRG ";
3542     if (AC)
3543     cout << "AC ";
3544     if (ND)
3545     cout << "ND ";
3546     if (S4)
3547     cout << "S4 ";
3548     if (ORB)
3549     cout << "ORB ";
3550     if (GP)
3551     cout << "GP ";
3552     cout << endl;
3553 pam-fi 1.12
3554 pam-fi 1.81 return;
3555 pam-fi 1.11
3556 pam-fi 1.81 }
3557     ;
3558 pam-fi 1.11
3559 pam-fi 1.2 //--------------------------------------
3560     //
3561     //
3562     //--------------------------------------
3563     /**
3564 pam-fi 1.3 * Check if a file contains selected Pamela Level2 trees.
3565 pam-fi 1.2 * @param name File name
3566 pam-fi 1.4 * @return true if the file is ok.
3567 pam-fi 1.2 */
3568 pam-fi 1.81 Bool_t PamLevel2::CheckLevel2File(TString name) {
3569    
3570     Bool_t CAL1__ok = false;
3571     Bool_t CAL2__ok = false;
3572     Bool_t TRK2__ok = false;
3573     Bool_t TRK1__ok = false;
3574     Bool_t TRKh__ok = false;
3575     Bool_t TRG__ok = false;
3576     Bool_t TOF__ok = false;
3577     Bool_t S4__ok = false;
3578     Bool_t ND__ok = false;
3579     Bool_t AC__ok = false;
3580     Bool_t ORB__ok = false;
3581     Bool_t GP__ok = false;
3582    
3583     Bool_t RUN__ok = false;
3584    
3585     Bool_t SELLI__ok = false;
3586    
3587     // cout << "Checking file: "<<name<<endl;
3588     TFile *f = new TFile(name.Data());
3589     if (!f || f->IsZombie()) {
3590     cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
3591     return false;
3592     }
3593     // cout << "Get list of keys: "<<f<<endl;
3594     TList *lk = f->GetListOfKeys();
3595     // lk->Print();
3596     TIter next(lk);
3597     TKey *key = 0;
3598    
3599     Int_t nev = 0;
3600    
3601     while ((key = (TKey*) next())) {
3602    
3603     // cout << key->GetName() << endl;
3604     // cout << key->GetName() << ""<<key->GetClassName()<<endl;
3605     // cout << " Get tree: " << f->Get(key->GetName())<<endl;
3606     // nev_previous = nev;
3607     // cout << " n.entries "<< nev <<endl;
3608     // if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
3609     // nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
3610     // cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
3611     // return false;
3612     // };
3613    
3614     //=========================================================
3615     // check if the file
3616    
3617    
3618     if (!strcmp(key->GetName(), "Run"))
3619     RUN__ok = true;
3620    
3621     //=========================================================
3622     if (!strcmp(key->GetName(), "SelectionList")) {
3623     SELLI__ok = true;
3624     if (SELLI == 1) {
3625     Int_t nevt = ((TTree*) f->Get("SelectionList"))->GetEntries();
3626     if (nev && nevt != nev) {
3627     cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
3628     << " events instead of " << nev << endl;
3629     return false;
3630     }
3631     nev = nevt;
3632     }
3633     }
3634    
3635     //=========================================================
3636     if (!strcmp(key->GetName(), "Trigger")) {
3637     TRG__ok = true;
3638     if (TRG) {
3639     Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
3640     if (nev && nevt != nev) {
3641     cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
3642     << nev << endl;
3643     return false;
3644     }
3645     nev = nevt;
3646     }
3647     }
3648     //=========================================================
3649     if (!strcmp(key->GetName(), "ToF")) {
3650     TOF__ok = true;
3651     if (TOF) {
3652     Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
3653     if (nev && nevt != nev) {
3654     cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
3655     << endl;
3656     return false;
3657     }
3658     nev = nevt;
3659     }
3660     }
3661     //=========================================================
3662     if (!strcmp(key->GetName(), "S4")) {
3663     S4__ok = true;
3664     if (S4) {
3665     Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
3666     if (nev && nevt != nev) {
3667     cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
3668     << endl;
3669     return false;
3670     }
3671     nev = nevt;
3672     }
3673     }
3674     //=========================================================
3675    
3676     if (!strcmp(key->GetName(), "NeutronD")) {
3677     ND__ok = true;
3678     if (ND) {
3679     Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
3680     if (nev && nevt != nev) {
3681     cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
3682     << nev << endl;
3683     return false;
3684     }
3685     nev = nevt;
3686     }
3687     }
3688     //=========================================================
3689     if (!strcmp(key->GetName(), "Anticounter")) {
3690     AC__ok = true;
3691     if (AC) {
3692     Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
3693     if (nev && nevt != nev) {
3694     cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
3695     << nev << endl;
3696     return false;
3697     }
3698     nev = nevt;
3699     }
3700     }
3701     //=========================================================
3702     if (!strcmp(key->GetName(), "OrbitalInfo")) {
3703     ORB__ok = true;
3704     if (ORB) {
3705     Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
3706     if (nev && nevt != nev) {
3707     cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
3708     << nev << endl;
3709     return false;
3710     }
3711     nev = nevt;
3712     }
3713     }
3714     //=========================================================
3715     if (!strcmp(key->GetName(), "Tracker")) {
3716     TTree *T = (TTree*) f->Get("Tracker");
3717     if (TRK1 || TRK2 || TRKh) {
3718     Int_t nevt = T->GetEntries();
3719     if (nev && nevt != nev) {
3720     cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
3721     << nev << endl;
3722     return false;
3723     }
3724     nev = nevt;
3725     }
3726     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3727     TString name = T->GetListOfBranches()->At(i)->GetName();
3728     if (!name.CompareTo("TrkLevel1"))
3729     TRK1__ok = true;
3730     if (!name.CompareTo("TrkLevel2"))
3731     TRK2__ok = true;
3732     if (!name.CompareTo("TrkHough"))
3733     TRKh__ok = true;
3734     };
3735     T->Delete();
3736     };
3737     //=========================================================
3738     if (!strcmp(key->GetName(), "Calorimeter")) {
3739     TTree *T = (TTree*) f->Get("Calorimeter");
3740     if (CAL1 || CAL2) {
3741     Int_t nevt = T->GetEntries();
3742     if (nev && nevt != nev) {
3743     cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
3744     << nev << endl;
3745     return false;
3746     }
3747     nev = nevt;
3748     }
3749     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3750     TString name = T->GetListOfBranches()->At(i)->GetName();
3751     if (!name.CompareTo("CaloLevel1"))
3752     CAL1__ok = true;
3753     if (!name.CompareTo("CaloLevel2"))
3754     CAL2__ok = true;
3755     };
3756     T->Delete();
3757     };
3758     //=========================================================
3759     if (!strcmp(key->GetName(), "h20")) {
3760     ISGP = true;
3761     GP__ok = true;
3762     if (GP) {
3763     Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
3764     if (nev && nevt != nev) {
3765     cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
3766     << endl;
3767     return false;
3768     }
3769     nev = nevt;
3770     }
3771     }
3772    
3773     };
3774    
3775     if (SELLI == -1)
3776     SELLI = (Int_t) SELLI__ok;
3777     if (SELLI == 0 && SELLI__ok) {
3778     cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
3779     return false;
3780     }
3781     if (SELLI == 1 && !SELLI__ok) {
3782     cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
3783     return false;
3784     }
3785 pam-fi 1.8
3786 pam-fi 1.81 // cout << "SELLI "<<SELLI<<endl;
3787    
3788     // cout<< "CheckLevel2File(TString): detector list --> ";
3789     // if(TRK1__ok)cout<<"TRK1 ";
3790     // if(TRK2__ok)cout<<"TRK2 ";
3791     // if(TRKh__ok)cout<<"TRKH ";
3792     // if(CAL1__ok)cout<<"CAL1 ";
3793     // if(CAL2__ok)cout<<"CAL2 ";
3794     // if(TOF__ok)cout<<"TOF ";
3795     // if(TRG__ok)cout<<"TRG ";
3796     // if(AC__ok)cout<<"AC ";
3797     // if(ND__ok)cout<<"ND ";
3798     // if(S4__ok)cout<<"S4 ";
3799     // if(ORB__ok)cout<<"ORB ";
3800     // cout << endl;
3801    
3802    
3803     if (TRK2 && TRK1__ok)
3804     TRK1 = 1;
3805     // ----------------------------------------------------------------------------
3806     // NOTA
3807     // se c'e` il level1, lo devo necessarimente leggere.
3808     // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
3809     // comunque, ma non vengono disallocati da PamLevel2::Clear()
3810     // ----------------------------------------------------------------------------
3811    
3812    
3813     if (!RUN__ok) {
3814     cout << "File: " << f->GetName() << " *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"
3815     << endl;
3816     RUN = false;
3817     };
3818 pam-fi 1.20
3819 pam-fi 1.81 if (CAL1 && !CAL1__ok) {
3820     cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
3821     return false;
3822     };
3823     if (CAL2 && !CAL2__ok) {
3824     cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
3825     return false;
3826     };
3827     if (TRK2 && !TRK2__ok) {
3828     cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
3829     return false;
3830     };
3831     if (TRK1 && !TRK1__ok) {
3832     cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
3833     return false;
3834     };
3835     if (TRKh && !TRKh__ok) {
3836     cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
3837     return false;
3838     };
3839     if (ORB && !ORB__ok) {
3840     cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
3841     return false;
3842     };
3843     if (AC && !AC__ok) {
3844     cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
3845     return false;
3846     };
3847     if (S4 && !S4__ok) {
3848     cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
3849     return false;
3850     };
3851     if (TOF && !TOF__ok) {
3852     cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
3853     return false;
3854     };
3855 pam-fi 1.8
3856 pam-fi 1.81 if (ND && !ND__ok) {
3857     cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
3858     return false;
3859     };
3860     if (TRG && !TRG__ok) {
3861     cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
3862     return false;
3863     };
3864     if (GP && !GP__ok) {
3865     cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
3866     return false;
3867     };
3868 pam-fi 1.8
3869 pam-fi 1.81 // lk->Delete();
3870     // delete lk;
3871     f->Close();
3872    
3873     // cout<< "CheckLevel2File(TString): detector list --> ";
3874     // if(TRK1)cout<<"TRK1 ";
3875     // if(TRK2)cout<<"TRK2 ";
3876     // if(TRKh)cout<<"TRKH ";
3877     // if(CAL1)cout<<"CAL1 ";
3878     // if(CAL2)cout<<"CAL2 ";
3879     // if(TOF)cout<<"TOF ";
3880     // if(TRG)cout<<"TRG ";
3881     // if(AC)cout<<"AC ";
3882     // if(ND)cout<<"ND ";
3883     // if(S4)cout<<"S4 ";
3884     // if(ORB)cout<<"ORB ";
3885     // if(GP)cout<<"GP ";
3886     // cout << endl;
3887 pam-fi 1.8
3888 pam-fi 1.81 return true;
3889 pam-fi 1.2
3890 pam-fi 1.81 }
3891     ;
3892 pam-fi 1.2
3893 pam-fi 1.9 /**
3894     * Create clone-trees
3895     */
3896 pam-fi 1.81 void PamLevel2::CreateCloneTrees0(TChain *fChain, TFile *ofile) {
3897    
3898     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3899     cout << "Create clones of PAMELA trees " << endl;
3900 pam-fi 1.9
3901 pam-fi 1.81 Int_t i = 0;
3902     pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
3903     TString name = pam_tree_clone[i]->GetName();
3904     name.Append("_clone");
3905     // pam_tree_clone[i]->SetName(name.Data());
3906     cout << pam_tree_clone[i]->GetName() << endl;
3907     i++;
3908    
3909     TList *li = fChain->GetListOfFriends();
3910     TIter next(li);
3911     TFriendElement* T_friend = 0;
3912     ofile->cd();
3913     while ((T_friend = (TFriendElement*) next())) {
3914     // cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
3915     // cout<<T_friend->GetTree()->GetName()<< endl;
3916     pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
3917     pam_tree_clone[i]->SetAutoSave(1000000);
3918     name = pam_tree_clone[i]->GetName();
3919 pam-fi 1.9 name.Append("_clone");
3920 pam-fi 1.81 // pam_tree_clone[i]->SetName(name.Data());
3921     cout << pam_tree_clone[i]->GetName() << endl;
3922 pam-fi 1.9 i++;
3923 pam-fi 1.81 }
3924 pam-fi 1.9
3925 pam-fi 1.81 delete li;
3926 pam-fi 1.9
3927 pam-fi 1.81 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3928 pam-fi 1.9
3929     }
3930    
3931 pam-fi 1.11 /**
3932     * Create clone-trees
3933     */
3934 pam-fi 1.81 void PamLevel2::CreateCloneTrees(TFile *ofile) {
3935 pam-fi 1.11
3936 pam-fi 1.81 // if the pointer is null, create a default file
3937     if (!run_tree)
3938     return;
3939    
3940     if (!ofile) {
3941     cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root " << endl;
3942     ofile = new TFile("clone-tree.root", "recreate");
3943     }
3944    
3945     ofile->cd();
3946    
3947     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3948     cout << "Create new PAMELA trees " << endl;
3949    
3950     run_tree_clone = new TTree("Run", "PAMELA Level2 data from the GL_RUN table ");
3951     run_tree_clone->Branch("RunInfo", "GL_RUN", GetPointerTo("RunInfo"));
3952     cout << "Run : branch RunInfo" << endl;
3953     run_tree_clone->Branch("SoftInfo", "SoftInfo", GetPointerTo("SoftInfo"));
3954     cout << "Run : branch SoftInfo" << endl;
3955     // ------------------
3956     // replicate run tree
3957     // ------------------
3958     // cout << "----------------------------------------------------"<<endl;
3959     // cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3960     for (Int_t i = 0; i < run_tree->GetEntries(); i++) {
3961     run_tree->GetEntry(i);
3962     // cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3963     run_tree_clone->Fill();
3964     }
3965     cout << "----------------------------------------------------" << endl;
3966    
3967     // ------------------------------------
3968     // add branch with dead and live times
3969     // ------------------------------------
3970     if (SELLI != 2) { // EMILIANO
3971     run_tree_clone->Branch("DeadLiveTime", totdltime, "dltime[3]/l");
3972     cout << "Run : branch DeadLiveTime" << endl;
3973    
3974     sel_tree_clone = new TTree("SelectionList", "List of selected events ");
3975     // sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3976     sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
3977     sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
3978 mocchiut 1.91 // if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
3979 pam-fi 1.81 };
3980 pam-fi 1.39
3981 pam-fi 1.81 Int_t i = 0;
3982     if (TRK1 || TRK2 || TRKh) {
3983     pam_tree_clone[i] = new TTree("Tracker", "PAMELA tracker level2 data ");
3984     if (TRK1) {
3985     pam_tree_clone[i]->Branch("TrkLevel1", "TrkLevel1", GetPointerTo("TrkLevel1"));
3986     pam_tree_clone[i]->BranchRef();
3987     cout << "Tracker : branch TrkLevel1" << endl;
3988     // cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3989     };
3990     if (TRK2) {
3991     pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2", GetPointerTo("TrkLevel2"));
3992     cout << "Tracker : branch TrkLevel2" << endl;
3993     };
3994     if (TRKh) {
3995     pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
3996     cout << "Tracker : branch TrkHough" << endl;
3997     };
3998     i++;
3999     }
4000 pam-fi 1.39
4001 pam-fi 1.81 // Calorimeter
4002     if (CAL1 || CAL2) {
4003     pam_tree_clone[i] = new TTree("Calorimeter", "PAMELA calorimeter level2 data ");
4004     if (CAL1) {
4005     pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
4006     cout << "Calorimeter : branch CaloLevel1" << endl;
4007     };
4008     if (CAL2) {
4009     pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
4010     cout << "Calorimeter : branch CaloLevel2" << endl;
4011 mocchiut 1.72 };
4012 pam-fi 1.81 i++;
4013     }
4014 pam-fi 1.20
4015 pam-fi 1.81 // ToF
4016     if (TOF) {
4017     pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
4018     pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
4019     cout << "ToF : branch ToFLevel2" << endl;
4020     i++;
4021     };
4022     // Trigger
4023     if (TRG) {
4024     pam_tree_clone[i] = new TTree("Trigger", "PAMELA trigger level2 data ");
4025     pam_tree_clone[i]->Branch("TrigLevel2", "TrigLevel2", GetPointerTo("TrigLevel2"));
4026     cout << "Trigger : branch TrigLevel2" << endl;
4027     i++;
4028     };
4029     // S4
4030     if (S4) {
4031     pam_tree_clone[i] = new TTree("S4", "PAMELA S4 level2 data ");
4032     pam_tree_clone[i]->Branch("S4Level2", "S4Level2", GetPointerTo("S4Level2"));
4033     cout << "S4 : branch S4Level2" << endl;
4034     i++;
4035     };
4036     // Neutron Detector
4037     if (ND) {
4038     pam_tree_clone[i] = new TTree("NeutronD", "PAMELA neutron detector level2 data ");
4039     pam_tree_clone[i]->Branch("NDLevel2", "NDLevel2", GetPointerTo("NDLevel2"));
4040     cout << "NeutronD : branch NDLevel2" << endl;
4041     i++;
4042     };
4043     // Anticounters
4044     if (AC) {
4045     pam_tree_clone[i] = new TTree("Anticounter", "PAMELA anticounter detector level2 data ");
4046     pam_tree_clone[i]->Branch("AcLevel2", "AcLevel2", GetPointerTo("AcLevel2"));
4047     cout << "Anticounter : branch AcLevel2" << endl;
4048     i++;
4049     };
4050     // OrbitalInfo
4051     if (ORB) {
4052     pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info ");
4053     pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
4054     cout << "OrbitalInfo : branch OrbitalInfo" << endl;
4055     i++;
4056     };
4057     // GPamela
4058     if (GP) {
4059     pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
4060     pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
4061 pam-fi 1.88 cout << "GPamela : branch GPamela" << endl;
4062 pam-fi 1.81 i++;
4063     };
4064     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4065 pam-fi 1.9
4066 pam-fi 1.11 }
4067 pam-fi 1.9
4068     /**
4069     * Fill tree (created with CreateCloneTrees)
4070     *
4071     */
4072     //void PamLevel2::FillNewPamTree(TTree *T){
4073 pam-fi 1.81 void PamLevel2::FillCloneTrees() {
4074    
4075     // cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
4076 pam-fi 1.24
4077 pam-fi 1.81 for (Int_t i = 0; i < NCLONES; i++) {
4078     if (pam_tree_clone[i])
4079     pam_tree_clone[i]->Fill();
4080     }
4081     if (sel_tree_clone)
4082     sel_tree_clone->Fill();
4083 pam-fi 1.21
4084 pam-fi 1.9 }
4085    
4086 pam-fi 1.81 TTree* PamLevel2::GetCloneTree(TString name) {
4087 pam-fi 1.9
4088 pam-fi 1.81 for (Int_t i = 0; i < NCLONES; i++) {
4089     if (pam_tree_clone[i]) {
4090     TString na = pam_tree_clone[i]->GetName();
4091     if (!name.CompareTo(na))
4092     return pam_tree_clone[i];
4093     };
4094     }
4095     if (run_tree_clone) {
4096     TString na = run_tree_clone->GetName();
4097     if (!name.CompareTo(na))
4098     return run_tree_clone;
4099     }
4100     if (sel_tree_clone) {
4101     TString na = sel_tree_clone->GetName();
4102     if (!name.CompareTo(na))
4103     return sel_tree_clone;
4104     }
4105     return NULL;
4106 pam-fi 1.9
4107     }
4108 pam-fi 1.81 void PamLevel2::WriteCloneTrees() {
4109     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4110     cout << "Write clones of PAMELA trees " << endl;
4111     cout << run_tree_clone->GetName() << endl;
4112     if (SELLI != 2) {// Emiliano
4113     if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4114     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4115     };
4116     run_tree_clone->Write();
4117     if (SELLI != 2) { //Emiliano
4118     cout << sel_tree_clone->GetName() << endl;
4119     sel_tree_clone->Write();
4120     };
4121     for (Int_t i = 0; i < NCLONES; i++) {
4122     if (pam_tree_clone[i]) {
4123     cout << pam_tree_clone[i]->GetName() << endl;
4124     pam_tree_clone[i]->Write();
4125 mocchiut 1.72 };
4126 pam-fi 1.81 }
4127     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4128 pam-fi 1.9
4129     }
4130    
4131     /**
4132 pam-fi 1.24 * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
4133 pam-fi 1.9 */
4134 pam-fi 1.81 Int_t PamLevel2::GetEntry(Long64_t iee) {
4135    
4136     if (!pam_tree) {
4137     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
4138     return 0;
4139     }
4140 mocchiut 1.28
4141 pam-fi 1.81 //
4142     // This is a sort of bug: if you don't have the run tree you don't want to exit here you want to have loaded the event anyway...
4143     //
4144     // if(!run_tree ){
4145     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
4146     // return 0;
4147     // }
4148    
4149     //-------------------------------
4150 mocchiut 1.90 if (!pam_tree->GetEntry(iee)) {
4151 pam-fi 1.81 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
4152     return 0;
4153     }
4154     //
4155     // ... that's way I put it here. Notice that nothing change in the code (is backward compatible) since in any case you return with 0.
4156     // in theory one would like to return 1 if run is not loaded but now I don't have the will to add that 2 lines of code and it is not
4157     // a problem if you don't check the return code of getentry.
4158     //
4159     if (!RUN || !run_tree) {
4160     if (TRK0 || CAL0 || TOF0 || RUN) { //forse cosi` va bene per tornare 1?
4161     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded" << endl;
4162 mocchiut 1.77 return 0;
4163 pam-fi 1.33 }
4164 pam-fi 1.81 else {
4165     return 1; //cosi` se non c'e` run esce qua...
4166     }
4167     }
4168    
4169     //-------------------------------
4170 mocchiut 1.90 //
4171     if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
4172     if (SELLI == 0 || SELLI == 2) irunentry = iee - runfirstentry;
4173 pam-fi 1.81
4174     return 1;
4175 pam-fi 1.20
4176     }
4177    
4178 pam-fi 1.81 TrkLevel0 *PamLevel2::GetTrkLevel0() {
4179     if (!TRK0)
4180     return NULL;
4181     if (!GetYodaEntry()) {
4182     cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree" << endl;
4183     return 0;
4184     }
4185     return trk0_obj;
4186     }
4187     ;
4188     CaloLevel0 *PamLevel2::GetCaloLevel0() {
4189     if (!CAL0)
4190     return NULL;
4191     if (!GetYodaEntry()) {
4192     cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree" << endl;
4193     return 0;
4194     }
4195     return calo0_obj;
4196     }
4197     ;
4198 pam-fi 1.40
4199 pam-fi 1.20 /**
4200     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
4201     * Given the run ID (...), if needed it query the DB and load the proper file.
4202     * @return Pointer to the tree
4203     */
4204    
4205 pam-fi 1.81 TTree* PamLevel2::GetYodaTree() {
4206 pam-fi 1.20
4207 pam-fi 1.81 // cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
4208     //===================================
4209     // check if iroot has changed
4210     //===================================
4211     if (irun < 0) {
4212     cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
4213 mocchiut 1.90 if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
4214 pam-fi 1.81 return NULL;
4215     }
4216 mocchiut 1.90 Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
4217     if (DBG){
4218     cout << "iroot "<<iroot<<endl;
4219     cout << "irootnew "<<irootnew<<endl;
4220     }
4221 pam-fi 1.81
4222     //===================================
4223     // load the level0 file
4224     // (if not already loaded)
4225     //===================================
4226     if (iroot != irootnew || !l0_tree) {
4227     iroot = irootnew;
4228 pam-fi 1.20 //===================================
4229 pam-fi 1.81 // open the DB connection
4230     // (if not already opened)
4231 pam-fi 1.20 //===================================
4232 pam-fi 1.81 if (!dbc || (dbc && !dbc->IsConnected()))
4233     SetDBConnection();
4234     GL_ROOT glroot = GL_ROOT();
4235     if (glroot.Query_GL_ROOT(iroot, dbc)) {
4236     cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = " << iroot << " does not exists"
4237     << endl;
4238     return NULL;
4239     };
4240     TString filename = glroot.PATH + glroot.NAME;
4241     if (l0_file) {
4242     l0_file->Close();
4243     l0_file->Delete();
4244     }
4245     cout << "Opening LEVEL0 file: " << filename << endl;
4246     FileStat_t t;
4247     if (gSystem->GetPathInfo(filename.Data(), t)) {
4248     cout << " PamLevel2::GetYodaTree() -- ERROR opening file " << endl;
4249     return NULL;
4250     }
4251     l0_file = new TFile(filename);
4252     if (!l0_file)
4253     return NULL;
4254     l0_tree = (TTree*) l0_file->Get("Physics");
4255     if (!h0_obj)
4256     h0_obj = new EventHeader();
4257     l0_tree->SetBranchAddress("Header", &h0_obj);
4258 mocchiut 1.90 yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
4259 pam-fi 1.81 //---------------------------------------------------
4260     // TRACKER:
4261     if (TRK0) {
4262     if (!trk0_obj) {
4263     trk0_obj = new TrkLevel0();
4264     trk0_obj->Set();
4265     };
4266     l0_tree->SetBranchAddress("Tracker", trk0_obj->GetPointerToTrackerEvent());
4267     }
4268     //--------------------------------------------------
4269     // CALORIMETER:
4270     if (CAL0) {
4271     if (!calo0_obj) {
4272     calo0_obj = new CaloLevel0();
4273     calo0_obj->Set();
4274     };
4275     l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
4276     }
4277     //---------------------------------------------------
4278     // TOF:
4279     if (TOF0) {
4280     cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented " << endl;
4281     }
4282 pam-fi 1.20
4283 pam-fi 1.81 dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4284 pam-fi 1.87 delete dbc;
4285 mocchiut 1.82 dbc=0;
4286 mocchiut 1.51
4287 pam-fi 1.81 };
4288 mocchiut 1.51
4289 pam-fi 1.81 if (TRK0) {
4290 mocchiut 1.90 if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
4291 pam-fi 1.81 TrkParams::SetCalib(run_obj, dbc);
4292     TrkParams::LoadCalib();
4293     if (!TrkParams::CalibIsLoaded()) {
4294     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
4295 pam-fi 1.20 };
4296 mocchiut 1.82 if(dbc){
4297     dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4298 pam-fi 1.87 delete dbc;
4299 mocchiut 1.82 dbc=0;
4300     };
4301 pam-fi 1.81 }
4302     return l0_tree;
4303 pam-fi 1.20 }
4304 pam-fi 1.9
4305 pam-fi 1.20 /**
4306     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
4307     */
4308 pam-fi 1.81 Int_t PamLevel2::GetYodaEntry() {
4309    
4310 mocchiut 1.90 Long64_t iev = this->GetReadEntry();
4311 pam-fi 1.81
4312 mocchiut 1.90 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4313     // if it is a full file (not preselected)
4314     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4315 mocchiut 1.91 // if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
4316 pam-fi 1.81
4317 mocchiut 1.90 if (!GetYodaTree()){
4318     printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4319     return 0;
4320     }
4321 pam-fi 1.81
4322 mocchiut 1.90 if (irunentry < 0) {
4323     if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4324     // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4325     irunentry = 0LL;
4326     }
4327     // ---------------------------------
4328     // if file is NOT a preselected file
4329     // ---------------------------------
4330     Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4331    
4332     if (DBG){
4333     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4334     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4335     cout << " time "<< abstime << endl;
4336     }
4337    
4338     ULong64_t obt = 0;
4339     ULong64_t pktn = 0;
4340     if (GetOrbitalInfo()) {
4341     obt = GetOrbitalInfo()->OBT;
4342     pktn = GetOrbitalInfo()->pkt_num;
4343     }
4344 pam-fi 1.81
4345 mocchiut 1.90 if (!GetOrbitalInfo() && !ISGP) {
4346     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
4347     return 0;
4348 pam-fi 1.81 }
4349 mocchiut 1.90 if (obt == 0 && pktn == 0 && !ISGP) {
4350     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
4351 pam-fi 1.81 return 0;
4352     }
4353    
4354 mocchiut 1.90 // ---------------------------------------------------------------------
4355     // ATTENTION!!!
4356     // If data are not pre-processed with cleaner, the level0 tree may contain
4357     // spurious nested physics packets.
4358     // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
4359     // while level2 tree DOES NOT!!
4360     // This means that "quellagiusta" in these cases is not correct.
4361     // In order to retrieve the correct level0 event, I implemented a check
4362     // of the OBT and pkt-number. In case of mismatch, the level0 entry number
4363     // is shift forward until when the packets match.
4364     // ---------------------------------------------------------------------
4365     Long64_t shift = 0LL;
4366     Long64_t answer = quellagiusta + shift + yprevshift;
4367     Int_t readl0 = 0;
4368     readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4369    
4370     if (DBG){
4371     printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4372     }
4373    
4374    
4375 pam-fi 1.81 if (ISGP) {
4376     obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4377     pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4378     }
4379    
4380 mocchiut 1.90 while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4381     if ( isSync && shift == 0LL ){
4382     printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
4383     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4384     cout << "\nFor bug reporting instructions, please see for example:\n";
4385     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4386     cout << " " << endl;
4387     }
4388     if (shift > 0) {
4389 mocchiut 1.93 if (DBG) cout << " PKTNUM L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4390 mocchiut 1.90 if (DBG)
4391     cout << " RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4392     << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4393     if (DBG)
4394     cout << " L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4395     << " prevshift " << prevshift << " )" << endl;
4396     }
4397     answer = quellagiusta + shift+ yprevshift;
4398     readl0 = l0_tree->GetEntry(answer);
4399     // printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4400    
4401     if (!GetEventHeader()) {
4402     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4403     return 0;
4404     }
4405 pam-fi 1.81
4406 mocchiut 1.90 //
4407     if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4408     if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4409     // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4410     yprevshift = 0LL;
4411     shift = -1LL;
4412     };
4413    
4414     shift++;
4415     }
4416    
4417 pam-fi 1.81
4418 mocchiut 1.90 if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4419     if ( isSync ){
4420     printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
4421     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4422     cout << "\nFor bug reporting instructions, please see for example:\n";
4423     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4424     cout << " " << endl;
4425     }
4426     cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
4427     cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;
4428     cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
4429     // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
4430     // ma nel mezzo del cammin per ogni run che pesco devo vedere la posizione relativa di iev rispetto al runheader nel livello2 per andare a cercare nel posto giusto
4431     // connect to db
4432     if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
4433     //
4434     if (GetOrbitalInfo()){
4435     abstime = GetOrbitalInfo()->absTime;
4436     } else {
4437     printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
4438     return 0;
4439     }
4440     // query DB looking for runs containing the processed event
4441     TSQLResult *pResult;
4442     TSQLRow *Row = NULL;
4443     TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
4444     if ( DBG ) printf(" query is %s \n",myquery.Data());
4445     // printf(" query is %s \n",myquery.Data());// TOGLITOGLI
4446     pResult = dbc->Query(myquery.Data());
4447     if (!pResult->GetRowCount()){
4448     printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
4449     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4450     cout << "\nFor bug reporting instructions, please see for example:\n";
4451     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4452     cout << " " << endl;
4453     return 0;
4454     }
4455     if ( pResult->GetRowCount() == 1 ){
4456     if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
4457     // printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
4458     }
4459     for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
4460     if (Row) delete Row;
4461     Row = pResult->Next();
4462     if( Row == NULL ) break;
4463     UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
4464     UInt_t nev = (UInt_t)atoll(Row->GetField(1));
4465     if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
4466     // printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
4467    
4468     // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
4469     Bool_t rfound = false;
4470     totrunentry = 0LL;
4471     runfirstentry = 0LL;
4472     for (Int_t r=0; r< run_tree->GetEntries();r++){
4473     run_tree->GetEntry(r);//update runinfo
4474     if ( r > 0 ){
4475     totrunentrymin = totrunentrymax+1;
4476     } else {
4477     totrunentrymin = 0LL;
4478     }
4479     totrunentry += GetRunInfo()->NEVENTS;
4480     totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4481     irun = r;
4482    
4483     if (idrun == GetRunInfo()->ID){
4484     if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4485     if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4486     if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4487     // printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4488     // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4489     prevshift += (totrunentrymin-iev); // add the new shift to total shift
4490     totrunentrymin -= (totrunentrymin-iev); // shift run position min
4491     totrunentrymax -= (totrunentrymin-iev); // shift run position max
4492     if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4493     // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4494     }
4495     runfirstentry = totrunentrymin; // first entry of the run in the level2
4496    
4497 pam-fi 1.81
4498 mocchiut 1.90 //
4499     if (gltsync)
4500     delete gltsync; // Emiliano
4501     if (!dbc || (dbc && !dbc->IsConnected()))
4502     SetDBConnection(); //Emiliano
4503     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4504     if (dbc){
4505     dbc->Close(); // Emiliano
4506     delete dbc;
4507     dbc=0;
4508     }
4509     if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4510     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4511     << " has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4512     cout
4513     << " (NB!! in this case some events could be assigned to a wrong run)"
4514     << endl;
4515     }
4516     //
4517     if (DBG) printf(" found \n");
4518     // printf(" found \n");//TOGLITOGLI
4519     rfound = true;
4520     //
4521     break;
4522     }
4523     } // loop over run
4524     if ( !rfound ){
4525     printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
4526     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4527     cout << "\nFor bug reporting instructions, please see for example:\n";
4528     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4529     cout << " " << endl;
4530     return 0;
4531     }
4532 pam-fi 1.9
4533 mocchiut 1.90 // here we got the first run and we can check if it contains the level0 event
4534     if (!GetYodaTree()){
4535     printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4536     return 0;
4537     }
4538    
4539     // get the current run entry
4540     irunentry = iev - runfirstentry;
4541     if (irunentry < 0) {
4542     if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4543     // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4544     irunentry = 0LL;
4545     }
4546     // ---------------------------------
4547     // if file is NOT a preselected file
4548     // ---------------------------------
4549     quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4550    
4551     if (DBG){
4552     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4553     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4554     cout << " time "<< abstime << endl;
4555     }
4556     // cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4557     // cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4558     // cout << " time "<< abstime << endl; // TOGLITOGLI
4559    
4560     shift = 0;
4561     answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
4562     readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4563    
4564     if (DBG){
4565     printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4566     }
4567     // printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4568    
4569     while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4570     if (shift > 0) {
4571 mocchiut 1.93 if (DBG) cout << " PKTNUM L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4572 mocchiut 1.90 if (DBG)
4573     cout << " RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4574     << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4575     if (DBG)
4576     cout << " L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4577     << " prevshift " << prevshift << " )" << endl;
4578     }
4579     answer = quellagiusta + shift+ yprevshift;
4580     readl0 = l0_tree->GetEntry(answer);
4581     // printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4582    
4583     if (!GetEventHeader()) {
4584     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4585     return 0;
4586     }
4587     //
4588     if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4589     if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4590     // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4591     yprevshift = 0;
4592     shift = -1;
4593     };
4594    
4595     shift++;
4596     }
4597    
4598     if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4599     //still not the good run... continue with the nex one!
4600     printf("still not the good run... continue with the nex one!\n");
4601     } else {
4602     if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4603     // cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
4604     if (shift > 1) yprevshift = (shift - 1);
4605     if (Row) delete Row;
4606     delete pResult;
4607     if (dbc){
4608     dbc->Close(); // Emiliano
4609     delete dbc;
4610     dbc=0;
4611     }
4612     il0entry = answer;
4613     return readl0;
4614     }
4615     // perhaps it is all
4616     }// loop over runs containing the event
4617     if (Row) delete Row;
4618     delete pResult;
4619     if (dbc){
4620     dbc->Close(); // Emiliano
4621     delete dbc;
4622     dbc=0;
4623     }
4624     // arriving here it means no run found, cannot be! error!
4625     printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
4626     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4627     cout << "\nFor bug reporting instructions, please see for example:\n";
4628     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4629     cout << " " << endl;
4630     return 0;
4631     } else {
4632     if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4633     // cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4634     // printf("obt %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) %i pktn %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter() %i \n",obt,(UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()), pktn, (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) );
4635     if (shift > 1) yprevshift = (shift - 1);
4636     il0entry = answer;
4637     return readl0;
4638     }
4639    
4640 mocchiut 1.91 /* } // if selli 0 || 2
4641 mocchiut 1.90 if ( SELLI == 1 && hasL0EE ){
4642     sel_tree->GetEntry(iev);
4643     Long64_t answer = il0entry;
4644     Int_t readl0 = 0;
4645     readl0 = l0_tree->GetEntry(answer);
4646     return readl0;
4647 mocchiut 1.91 }*/
4648 mocchiut 1.90 printf(" PamLevel2::GetYodaEntry() ERROR! \n");
4649     cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4650     cout << "\nFor bug reporting instructions, please see for example:\n";
4651     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4652     cout << " " << endl;
4653     return 0;
4654 pam-fi 1.20 }
4655 mocchiut 1.90
4656 pam-fi 1.40 /**
4657     * \Brief Set DB connection
4658     */
4659 pam-fi 1.81 Bool_t PamLevel2::SetDBConnection() {
4660 mocchiut 1.30
4661 pam-fi 1.81 // cout << "PamLevel2::SetDBConnection()" << endl;
4662     if (DBG) {
4663     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
4664     cout << "Connecting to DB" << endl;
4665     cout << "HOST " << host << endl;
4666     cout << "USER " << user << endl;
4667     cout << "PSW " << psw << endl;
4668     }
4669 mocchiut 1.82 Bool_t notconn = true;
4670     Int_t trials = 0;
4671     while ( notconn && trials < 10 ){
4672     // gSystem->Sleep(500);
4673     dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
4674     //dbc->Connect(host.Data(), user.Data(), psw.Data());
4675     if ( dbc ) notconn = false;
4676     if (DBG) printf("<%i> test connection...\n ",trials);
4677     if (!dbc){
4678     if (DBG) printf(" :( failed, no pointer \n");
4679     notconn = true;
4680     // return false;
4681     };
4682     if (dbc && !dbc->IsConnected()){
4683     if (DBG) printf(" :( failed, no connection \n");
4684     notconn = true;
4685     // return false;
4686     };
4687     trials++;
4688     };
4689     if ( notconn ) return false;
4690     //
4691     if (DBG) printf("=connected!\n");
4692 pam-fi 1.81 stringstream myquery; // EMILIANO
4693     myquery.str(""); // EMILIANO
4694     myquery << "SET time_zone='+0:00'"; // EMILIANO
4695     dbc->Query(myquery.str().c_str()); // EMILIANO
4696 mocchiut 1.82 if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
4697 pam-fi 1.81 return true;
4698 mocchiut 1.30
4699 pam-fi 1.40 }
4700 pam-fi 1.45
4701     /**
4702     * \Brief Add a friend to the pamela chain.
4703     * @param cname name of the chain to be added
4704     */
4705    
4706 pam-fi 1.81 TChain* PamLevel2::AddFriend(TString cname) {
4707 pam-fi 1.45
4708 pam-fi 1.81 if (!GetPamTree()) {
4709     cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first" << endl;
4710     return NULL;
4711     }
4712 pam-fi 1.45
4713 pam-fi 1.81 TChain *c = new TChain(cname.Data());
4714 pam-fi 1.45
4715 pam-fi 1.81 TIter next(GetPamTree()->GetListOfFiles());
4716     Int_t nf = 0;
4717     TChainElement* element = 0;
4718     while ((element = (TChainElement*) next())) {
4719     c->Add(element->GetTitle());
4720     nf++;
4721     }
4722 pam-fi 1.45
4723 pam-fi 1.81 GetPamTree()->AddFriend(cname.Data());
4724 pam-fi 1.45
4725 pam-fi 1.81 cout << "external chain created and added to pamela friends :" << cname << endl;
4726     cout << "n.files " << nf << endl;
4727 pam-fi 1.45
4728 pam-fi 1.81 return c;
4729 pam-fi 1.45
4730 pam-fi 1.81 }
4731 pam-fi 1.45
4732 pam-fi 1.81 /**
4733     * Returns the current read entry. This method simply returns the result of the call to
4734     * pam_tree->GetReadEntry(), so it is entirely handled by ROOT.
4735     */
4736     Long64_t PamLevel2::GetReadEntry() {
4737     return pam_tree->GetReadEntry();
4738 pam-fi 1.45 }
4739 pam-fi 1.52
4740 pam-fi 1.81 /**
4741     * Sets the sorting method. If the new method is different from the previous, the issorted
4742     * flag is set to false, forcing a new sort the next time GetTrack is called.
4743     * @see GetTrack
4744     */
4745     void PamLevel2::SetSortingMethod(TString how) {
4746     if (howtosort != how) {
4747     issorted = false;
4748     }
4749     howtosort = how;
4750     }

  ViewVC Help
Powered by ViewVC 1.1.23