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

Annotation of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.43 - (hide annotations) (download)
Fri Aug 17 16:57:12 2007 UTC (17 years, 3 months ago) by pam-fi
Branch: MAIN
Changes since 1.42: +72 -36 lines
modified to work also with simulation

1 pam-fi 1.1 #include <PamLevel2.h>
2     //--------------------------------------
3     //
4     //
5     //--------------------------------------
6     /**
7     * Default constructor
8     */
9     PamTrack::PamTrack(){
10 pam-fi 1.13 trk_track = 0;
11     calo_track = 0;
12     tof_track = 0;
13     candeleteobj = 0;
14 pam-fi 1.1 };
15     //--------------------------------------
16     //
17     //
18     //--------------------------------------
19     /**
20     * Constructor
21     */
22     PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){
23 pam-fi 1.13
24     trk_track = 0;
25     calo_track = 0;
26     tof_track = 0;
27     // if(t)trk_track = new TrkTrack(*t);
28     // if(c)calo_track = new CaloTrkVar(*c);
29     // if(o)tof_track = new ToFTrkVar(*o);
30     if(t)trk_track = t;
31     if(c)calo_track = c;
32     if(o)tof_track = o;
33     candeleteobj = 0;
34    
35 pam-fi 1.1 };
36 pam-fi 1.13 PamTrack::PamTrack(const PamTrack& track){
37    
38     TrkTrack *t = track.trk_track;
39     CaloTrkVar *c = track.calo_track;
40     ToFTrkVar *o = track.tof_track;
41    
42     trk_track = 0;
43     calo_track = 0;
44     tof_track = 0;
45     if(t)trk_track = new TrkTrack(*t);
46     if(c)calo_track = new CaloTrkVar(*c);
47     if(o)tof_track = new ToFTrkVar(*o);
48     candeleteobj = 1;
49    
50     }
51     void PamTrack::Clear(){
52 pam-fi 1.1
53 pam-fi 1.13 // cout << "PamTrack::Clear() "<<candeleteobj<<endl;
54     if(candeleteobj){
55     if(trk_track) trk_track->TrkTrack::Clear();
56     if(calo_track) calo_track->CaloTrkVar::Clear();//???
57     if(tof_track) tof_track->ToFTrkVar::Clear();//???
58     }else{
59     trk_track = 0;
60     calo_track = 0;
61     tof_track = 0;
62     }
63     }
64     void PamTrack::Delete(){
65     // cout << "PamTrack::Delete() "<<candeleteobj<<endl;
66     if(candeleteobj){
67     if(trk_track) {
68     trk_track->TrkTrack::Clear();
69     delete trk_track;
70     }
71     if(calo_track){
72     calo_track->CaloTrkVar::Clear();//???
73     delete calo_track;
74     }
75     if(tof_track){
76     tof_track->ToFTrkVar::Clear();//???
77     delete tof_track;
78     }
79     }else{
80     Clear();
81     }
82     }
83 pam-fi 1.1 //--------------------------------------
84     //
85     //
86     //--------------------------------------
87     /**
88 mocchiut 1.35 * Default Constructor
89 pam-fi 1.1 */
90     PamLevel2::PamLevel2(){
91 mocchiut 1.35 Initialize();
92     };
93    
94     /**
95     * Constructor with given dir, list and detectors
96     */
97     PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
98     Initialize();
99     GetPamTree(GetListOfLevel2Files(ddir,list),detlist);
100     GetRunTree(GetListOfLevel2Files(ddir,list));
101     };
102    
103     /**
104     * Constructor with given dir and list
105     */
106     PamLevel2::PamLevel2(TString ddir,TString list){
107     Initialize();
108     GetPamTree(GetListOfLevel2Files(ddir,list),"");
109     GetRunTree(GetListOfLevel2Files(ddir,list));
110     };
111    
112    
113     void PamLevel2::Initialize(){
114 pam-fi 1.2
115 pam-fi 1.20 h0_obj = 0;
116     trk0_obj = 0;
117 mocchiut 1.30 calo0_obj = 0;
118 pam-fi 1.20
119 pam-fi 1.13 trk2_obj = 0;
120     trk1_obj = 0;
121     trkh_obj = 0;
122     calo1_obj = 0;
123     calo2_obj = 0;
124     tof_obj = 0;
125     trig_obj = 0;
126     s4_obj = 0;
127     nd_obj = 0;
128     ac_obj = 0;
129     orb_obj = 0;
130    
131     run_obj = 0;//new GL_RUN();
132 mocchiut 1.15 soft_obj = 0;// Emiliano
133 pam-fi 1.33 irun = -1LL;
134     runfirstentry = 0LL;
135     runlastentry = 0LL;
136 pam-fi 1.9
137 pam-fi 1.20 l0_file = NULL;
138     l0_tree = NULL;
139     iroot = -1;
140     dbc = 0;
141 mocchiut 1.30
142     prevshift = 0;
143 pam-fi 1.20
144     run_tree = NULL;
145     run_tree_clone = NULL;
146     sel_tree = NULL;
147     sel_tree_clone = NULL;
148    
149 pam-fi 1.33 irunentry = -1LL;
150 pam-fi 1.20 pam_tree = NULL;
151     for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
152    
153     host = "mysql://localhost/pamelaprod";
154     user = "anonymous";
155     psw = "";
156     const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
157     const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
158     const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
159     if ( !pamdbhost ) pamdbhost = "";
160     if ( !pamdbuser ) pamdbuser = "";
161     if ( !pamdbpsw ) pamdbpsw = "";
162     if ( strcmp(pamdbhost,"") ) host = pamdbhost;
163     if ( strcmp(pamdbuser,"") ) user = pamdbuser;
164     if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
165    
166    
167 pam-fi 1.9
168 pam-fi 1.13 // sorted_tracks = 0;//new TRefArray();
169 pam-fi 1.6
170 pam-fi 1.20 CAL0 = false;
171 pam-fi 1.9 CAL1 = true;
172 pam-fi 1.8 CAL2 = true;
173     TRK2 = true;
174     TRK1 = false;
175     TRKh = false;
176 pam-fi 1.20 TRKh = false;
177     TRG = true;
178     TOF = true;
179     TOF0 = false;
180     S4 = true;
181     ND = true;
182     AC = true;
183     ORB = true;
184 pam-fi 1.6
185 pam-fi 1.20 RUN = true;
186    
187     SELLI = -1;
188 pam-fi 1.8
189 pam-fi 1.43 ISGP = false;
190    
191 pam-fi 1.13 tsorted=0;
192     timage=0;
193 mocchiut 1.37
194     howtosort = "+CAL+TOF";
195     //howtosort = "+TOF";
196     sortthr = 100.;
197 pam-fi 1.13
198 pam-fi 1.2 };
199     /**
200 pam-fi 1.13 * Delete the event (act as Dtor)
201 pam-fi 1.2 */
202 pam-fi 1.13 void PamLevel2::Delete(){
203    
204     if(run_obj)delete run_obj;
205 mocchiut 1.15 if(soft_obj)delete soft_obj; //Emiliano
206 pam-fi 1.13
207     // cout << "void PamLevel2::Clear()"<<endl;
208 pam-fi 1.20 if(h0_obj) delete h0_obj;
209     if(trk0_obj) delete trk0_obj;
210 mocchiut 1.30 if(calo0_obj) delete calo0_obj;
211 pam-fi 1.13 if(trk1_obj) delete trk1_obj;
212     if(trk2_obj) delete trk2_obj;
213     if(trkh_obj) delete trkh_obj;
214     if(calo1_obj)delete calo1_obj;
215     if(calo2_obj)delete calo2_obj;
216     if(tof_obj) delete tof_obj;
217     if(trig_obj) delete trig_obj;
218     if(s4_obj) delete s4_obj;
219     if(nd_obj) delete nd_obj;
220     if(ac_obj) delete ac_obj;
221     if(orb_obj) delete orb_obj;
222    
223     if(tsorted){
224     tsorted->Delete();
225     delete tsorted;
226     }
227     if(timage){
228     timage->Delete();
229     delete timage;
230     }
231 pam-fi 1.20
232     if(dbc){
233     dbc->Close();
234     delete dbc;
235     }
236    
237     if(l0_file)l0_file->Close();
238 mocchiut 1.35 // if(pam_tree)pam_tree->Delete();;
239    
240     if ( pam_tree ){
241     //
242     // 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...
243     //
244     TList *temp = pam_tree->GetListOfFriends();
245     TList *contents = new TList; // create chain friend list
246     contents->SetOwner();
247     TIter next(temp);
248     TChain *questo = 0;
249     while ( (questo = (TChain*) next()) ){
250     TString name = questo->GetName();
251     contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
252     };
253     //
254     // deleting the main chain
255     //
256     pam_tree->Delete();
257     //
258     // deleting the friends...
259     //
260     TIter next2(contents);
261     TChain *questa = 0;
262     while ( questa = (TChain*)next2() ){
263     TString name = questa->GetName();
264     questa->Delete();
265     questa=NULL;
266     };
267     //
268     };
269     pam_tree = NULL;
270    
271 pam-fi 1.20 if(run_tree)run_tree->Delete();;
272     if(sel_tree)sel_tree->Delete();;
273     for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
274     if(run_tree_clone)run_tree_clone->Delete();;
275     if(sel_tree_clone)sel_tree_clone->Delete();;
276    
277    
278 pam-fi 1.2 };
279 pam-fi 1.13
280 pam-fi 1.2 /**
281 pam-fi 1.13 * Clear the event (NB! does not deallocate objects)
282 pam-fi 1.2 */
283     void PamLevel2::Clear(){
284 pam-fi 1.3
285 pam-fi 1.13 // cout << "void PamLevel2::Clear()"<<endl;
286 mocchiut 1.15
287     //
288 mocchiut 1.17 // 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
289     // want to load them for each event even if they are the same...
290     //
291 mocchiut 1.15 // if(run_obj)delete run_obj;
292 mocchiut 1.17 // if(run_obj) run_obj->Clear(); // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
293     // if(soft_obj) soft_obj->Clear();
294 pam-fi 1.13
295 pam-fi 1.20 if(h0_obj) h0_obj->Clear();
296 pam-fi 1.33 // if(trk0_obj) trk0_obj->Clear();
297 pam-fi 1.13 if(trk1_obj) trk1_obj->Clear();
298 mocchiut 1.35 if(trk2_obj) trk2_obj->Clear();
299 pam-fi 1.13 if(trkh_obj) trkh_obj->Clear();
300 pam-fi 1.40 if(calo0_obj) calo0_obj->Clear();
301 pam-fi 1.13 if(calo1_obj)calo1_obj->Clear();
302     if(calo2_obj)calo2_obj->Clear();
303     if(tof_obj) tof_obj->Clear();
304     if(trig_obj) trig_obj->Clear();
305     if(s4_obj) s4_obj->Clear();
306     if(nd_obj) nd_obj->Clear();
307     if(ac_obj) ac_obj->Clear();
308     if(orb_obj) orb_obj->Clear();
309 pam-fi 1.6
310 pam-fi 1.13 // if(sorted_tracks)sorted_tracks->Clear();
311     // sorted_tracks.Clear();
312    
313     if(tsorted){
314     tsorted->Delete();
315     }
316     if(timage){
317     timage->Delete();
318     }
319 mocchiut 1.35 };
320    
321     void PamLevel2::Reset(){
322     //
323     // First of all clear everything
324     //
325     Clear();
326     //
327     // close and reset chains and pointers
328     //
329     if ( pam_tree ){
330     //
331     // 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...
332     //
333     TList *temp = pam_tree->GetListOfFriends();
334     TList *contents = new TList; // create chain friend list
335     contents->SetOwner();
336     TIter next(temp);
337     TChain *questo = 0;
338     while ( (questo = (TChain*) next()) ){
339     TString name = questo->GetName();
340     contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
341     };
342     //
343     // deleting the main chain
344     //
345     pam_tree->Delete();
346     //
347     // deleting the friends...
348     //
349     TIter next2(contents);
350     TChain *questa = 0;
351     while ( questa = (TChain*)next2() ){
352     TString name = questa->GetName();
353     questa->Delete();
354     questa=NULL;
355     };
356     //
357     };
358     pam_tree = NULL;
359     //
360     if(run_tree)run_tree->Delete();;
361     run_tree = NULL;
362     if(sel_tree)sel_tree->Delete();;
363     sel_tree = NULL;
364     //
365     // Close file
366     //
367     if(l0_file)l0_file->Close("R");
368     l0_file = NULL;
369     //
370     h0_obj = 0;
371     trk0_obj = 0;
372     calo0_obj = 0;
373     //
374     trk2_obj = 0;
375     trk1_obj = 0;
376     trkh_obj = 0;
377     calo1_obj = 0;
378     calo2_obj = 0;
379     tof_obj = 0;
380     trig_obj = 0;
381     s4_obj = 0;
382     nd_obj = 0;
383     ac_obj = 0;
384     orb_obj = 0;
385     //
386     // Reset run pointers
387     //
388     run_obj = 0;//new GL_RUN();
389     soft_obj = 0;// Emiliano
390     irun = -1;
391     runfirstentry = 0ULL;
392     runlastentry = 0ULL;
393     //
394 pam-fi 1.1 };
395 pam-fi 1.2
396 mocchiut 1.38 Bool_t PamLevel2::IsGood(){
397     Bool_t goodev=true;
398     // if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
399     if(calo2_obj && calo2_obj->good != 1) goodev = false;
400     if(tof_obj && tof_obj->unpackError != 0) goodev = false;
401     if(trig_obj && trig_obj->unpackError != 0) goodev = false;
402     if(s4_obj && s4_obj->unpackError != 0) goodev = false;
403     if(nd_obj && nd_obj->unpackError != 0) goodev = false;
404     if(ac_obj && ac_obj->unpackError != 255) goodev = false;
405     // if(orb_obj)
406     return goodev;
407     };
408 pam-fi 1.2
409 pam-fi 1.1 //--------------------------------------
410     //
411     //
412     //--------------------------------------
413 pam-fi 1.9 void *PamLevel2::GetPointerTo(const char* c ){
414    
415     TString objname = c;
416 pam-fi 1.13
417     if(!objname.CompareTo("TrkLevel1")) {
418 pam-fi 1.14 if(!trk1_obj){
419     trk1_obj = new TrkLevel1();
420     trk1_obj->Set();
421     }
422 pam-fi 1.13 return &trk1_obj;
423     };
424     if(!objname.CompareTo("TrkLevel2")) {
425 pam-fi 1.14 if(!trk2_obj){
426     trk2_obj = new TrkLevel2();
427     trk2_obj->Set();
428     }
429 pam-fi 1.13 return &trk2_obj;
430     };
431     if(!objname.CompareTo("TrkHough")) {
432     if(!trkh_obj) trkh_obj = new TrkHough();
433     return &trkh_obj;
434     };
435     if(!objname.CompareTo("CaloLevel1")) {
436     if(!calo1_obj) calo1_obj = new CaloLevel1();
437     return &calo1_obj;
438     };
439     if(!objname.CompareTo("CaloLevel2")) {
440 pam-fi 1.14 if(!calo2_obj){
441     calo2_obj = new CaloLevel2();
442     calo2_obj->Set();
443     };
444 pam-fi 1.13 return &calo2_obj;
445     };
446     if(!objname.CompareTo("ToFLevel2")) {
447 pam-fi 1.14 if(!tof_obj){
448     tof_obj = new ToFLevel2();
449     tof_obj->Set();
450     }
451 pam-fi 1.13 return &tof_obj;
452     };
453     if(!objname.CompareTo("TrigLevel2")) {
454     if(!trig_obj) trig_obj = new TrigLevel2();
455     return &trig_obj;
456     };
457     if(!objname.CompareTo("S4Level2")) {
458     if(!s4_obj) s4_obj = new S4Level2();
459     return &s4_obj;
460     };
461     if(!objname.CompareTo("NDLevel2")) {
462     if(!nd_obj) nd_obj = new NDLevel2();
463     return &nd_obj;
464     };
465     if(!objname.CompareTo("AcLevel2")) {
466     if(!ac_obj) ac_obj = new AcLevel2();
467     return &ac_obj;
468     };
469     if(!objname.CompareTo("OrbitalInfo")){
470     if(!orb_obj) orb_obj = new OrbitalInfo();
471     return &orb_obj;
472     };
473    
474 pam-fi 1.9 if(!objname.CompareTo("RunInfo"))return &run_obj;
475 mocchiut 1.15
476     if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano
477    
478 pam-fi 1.9 return NULL;
479     };
480     //--------------------------------------
481     //
482     //
483     //--------------------------------------
484 pam-fi 1.1 /**
485 pam-fi 1.3 * Retrieves the calorimeter track matching the seqno-th tracker stored track.
486     * (If seqno = -1 retrieves the self-trigger calorimeter track)
487     */
488     CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
489 pam-fi 1.13
490     if( !calo2_obj )return 0;
491    
492     if( calo2_obj->CaloLevel2::ntrk()==0 ){
493 pam-fi 1.6 cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
494     return NULL;
495     };
496    
497     CaloTrkVar *c = 0;
498     Int_t it_calo=0;
499    
500     do{
501 pam-fi 1.13 c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
502 pam-fi 1.6 it_calo++;
503 pam-fi 1.13 } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
504 pam-fi 1.6
505     if(!c || seqno != c->trkseqno){
506     c = 0;
507     if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;
508     };
509     return c;
510    
511 pam-fi 1.3 };
512     //--------------------------------------
513     //
514     //
515     //--------------------------------------
516     /**
517     * Retrieves the ToF track matching the seqno-th tracker stored track.
518     * (If seqno = -1 retrieves the tracker-independent tof track)
519     */
520     ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
521    
522 pam-fi 1.13 if( !tof_obj )return 0;
523    
524     if( tof_obj->ToFLevel2::ntrk()==0 ){
525 pam-fi 1.6 cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
526     return NULL;
527     };
528    
529     ToFTrkVar *c = 0;
530     Int_t it_tof=0;
531    
532     do{
533 pam-fi 1.13 c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
534 pam-fi 1.6 it_tof++;
535 pam-fi 1.13 } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
536 pam-fi 1.6
537     if(!c || seqno != c->trkseqno){
538     c = 0;
539     if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;
540     };
541     return c;
542    
543 pam-fi 1.3 };
544    
545     //--------------------------------------
546     //
547     //
548     //--------------------------------------
549     /**
550     * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.
551     */
552     PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
553 pam-fi 1.13
554     cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
555     cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
556    
557 pam-fi 1.6 CaloTrkVar *c = 0;
558     ToFTrkVar *o = 0;
559    
560 pam-fi 1.8 if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
561 pam-fi 1.6 if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
562    
563 pam-fi 1.3 // if(t && c && o)track = new PamTrack(t,c,o);
564 pam-fi 1.6 PamTrack *track = new PamTrack(t,c,o);
565    
566     return track;
567 pam-fi 1.3
568     };
569     //--------------------------------------
570     //
571     //
572     //--------------------------------------
573     /**
574 pam-fi 1.1 * Retrieves the it-th stored track.
575     * It override TrkLevel2::GetTrack(int it).
576     * @param itrk Track number, ranging from 0 to GetNTracks().
577     */
578    
579     PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
580    
581 pam-fi 1.13 cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
582     cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;
583     cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
584 pam-fi 1.6 PamTrack *track = 0;
585    
586 pam-fi 1.13 if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){
587 pam-fi 1.3
588 pam-fi 1.13 TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
589 pam-fi 1.6 track = GetPamTrackAlong(t);
590 pam-fi 1.2
591 pam-fi 1.13
592    
593 pam-fi 1.6 }else{
594 pam-fi 1.13 cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
595 pam-fi 1.6 };
596    
597     return track;
598    
599 pam-fi 1.1 }
600     //--------------------------------------
601     //
602 mocchiut 1.37
603     /**
604     * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is PamLevel2::sorted_tracks. Left here as backward compatibility method.
605     **/
606     void PamLevel2::SortTracks(TString how){
607     printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
608     howtosort = how;
609     SortTracks();
610     };
611    
612 pam-fi 1.1 //
613     //--------------------------------------
614     /**
615 pam-fi 1.3 * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is PamLevel2::sorted_tracks.
616     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
617     * Sorting cryteria:
618 pam-fi 1.8 * TRK: lower chi**2
619 pam-fi 1.3 * CAL: lower Y spatial residual on the first calorimeter plane
620     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
621     * The default sorting cryterium is "TOF+CAL".
622     *
623     * 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).
624 pam-fi 1.1 */
625 mocchiut 1.37 void PamLevel2::SortTracks(){
626     TString how = howtosort;
627 pam-fi 1.1
628 mocchiut 1.37 // cout <<" PamLevel2::SortTracks(TString how) "<<endl;
629     if( !trk2_obj ){
630     cout << "void PamLevel2::SortTracks(): TrkLevel2 not loaded !!!";
631     return;
632     };
633     // cout << "call SortTracs() "<<endl;
634 pam-fi 1.4 //Save current Object count
635 mocchiut 1.37 Int_t ObjectNumber = TProcessID::GetObjectCount();
636 pam-fi 1.9
637 mocchiut 1.37 // cout << "ObjectNumber "<<ObjectNumber <<endl;
638 pam-fi 1.13
639 mocchiut 1.37 // if(!sorted_tracks)sorted_tracks = new TRefArray();
640     // sorted_tracks->Clear();
641     // sorted_tracks.Clear();
642 pam-fi 1.13
643 mocchiut 1.37 if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
644     tsorted->Delete();
645     TClonesArray &ttsorted = *tsorted;
646     if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
647     timage->Delete();
648     TClonesArray &ttimage = *timage;
649 pam-fi 1.13
650    
651 pam-fi 1.41
652     //--------------------------------------------------
653     // retrieve sorting method
654     //--------------------------------------------------
655 mocchiut 1.37 Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
656     Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
657     Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
658 pam-fi 1.6
659 mocchiut 1.37 if( !CAL2 && use_CAL) use_CAL = false;
660     if( !TOF && use_TOF) use_TOF = false;
661 pam-fi 1.6
662 mocchiut 1.37 if( !TRK2 ){
663     // cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
664     return;
665     };
666 pam-fi 1.9
667 mocchiut 1.37 // cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
668 pam-fi 1.41
669    
670     //--------------------------------------------------
671     // loop over "physical" tracks sorted by the tracker
672     //--------------------------------------------------
673 mocchiut 1.37 for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
674 pam-fi 1.4
675 mocchiut 1.37 TrkTrack *ts = 0;
676     CaloTrkVar *cs = 0;
677     ToFTrkVar *os = 0;
678    
679     // get tracker tracks
680 pam-fi 1.41 TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
681 mocchiut 1.37 CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
682     ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
683    
684 pam-fi 1.41 TrkTrack *ti = 0; //tracker (image)
685 mocchiut 1.37 CaloTrkVar *ci = 0;
686     ToFTrkVar *oi = 0;
687     // cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
688     // if track has an image, check image selection
689     if(tp->HasImage()){
690 pam-fi 1.6
691 mocchiut 1.37 ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
692     ci = GetCaloStoredTrack(ti->GetSeqNo());
693     oi = GetToFStoredTrack(ti->GetSeqNo());
694 pam-fi 1.6
695 mocchiut 1.37 // cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
696 pam-fi 1.9
697 mocchiut 1.37 //assign starting scores
698     Int_t tp_score = 0; //main track sorted by the tracker
699     Int_t ti_score = 0; //image track
700 pam-fi 1.6
701 pam-fi 1.41 // -----------------------------------------------------------------------------------------
702 mocchiut 1.37 // calorimeter check
703 pam-fi 1.41 // -----------------------------------------------------------------------------------------
704 mocchiut 1.37 // check the Y spatial residual on the first calorimeter plane
705     // (cut on calorimeter variables from Emiliano)
706     if( use_CAL && !calo2_obj ){
707     cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
708     return;
709     };
710     if(
711     use_CAL &&
712 pam-fi 1.41 // calo2_obj->npcfit[1] > 5 && //no. of fit planes on Y view
713     // calo2_obj->varcfit[1] < 1000. && //fit variance on Y view
714 mocchiut 1.37 cp && ci &&
715     true){
716 pam-fi 1.9
717 pam-fi 1.6
718 pam-fi 1.41 // Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;
719     // Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;
720 pam-fi 1.6
721 pam-fi 1.41 // if(resy_p <= resy_i) tp_score++;
722     // else ti_score++;
723 mocchiut 1.37
724 pam-fi 1.41
725 pam-fi 1.42 if( cp->npresh > ci->npresh) tp_score++;
726     if( cp->npresh < ci->npresh) ti_score++;
727     else ;//niente
728 pam-fi 1.41
729 mocchiut 1.37 // cout << "CALO "<<tp_score<<ti_score<<endl;
730 pam-fi 1.9
731 mocchiut 1.37 };
732 pam-fi 1.41 // -----------------------------------------------------------------------------------------
733 mocchiut 1.37 // TOF check
734 pam-fi 1.41 // -----------------------------------------------------------------------------------------
735 mocchiut 1.37 // check the number of hit pmts along the track
736     // on S12 S21 and S32, where paddles are parallel to Y axis
737     if( use_TOF && !tof_obj ){
738     cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
739     return;
740     };
741     if( use_TOF && op && oi ){
742    
743     //
744     Float_t sen = 0.;
745     for (Int_t ih=0; ih < op->npmtadc; ih++){
746     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
747     if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
748     };
749     for (Int_t ih=0; ih < oi->npmtadc; ih++){
750     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
751     if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
752     };
753     //
754     if ( sen >= sortthr ){
755     //printf(" IS A NUCLEUS! en = %f \n",sen);
756     //
757     // is a nucleus use a different algorithm
758     //
759     Int_t nz = 6; Float_t zin[6]; // << define TOF z-coordinates
760     for(Int_t ip=0; ip<nz; ip++)
761     zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
762     Trajectory *tr = new Trajectory(nz,zin);
763     //
764     Int_t nphit_p =0;
765     Int_t nphit_i =0;
766     Float_t enhit_p = 0.;
767     Float_t enhit_i = 0.;
768     //
769     for (Int_t ih=0; ih < op->npmtadc; ih++){
770     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
771     if(pl == 1 || pl == 2 || pl == 5){
772     nphit_p++;
773     enhit_p += (op->dedx).At(ih);
774     };
775     };
776     //
777     tp->DoTrack2(tr);
778     //
779     if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
780     for (Int_t ih=0; ih < op->npmtadc; ih++){
781     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
782     if(pl == 0){
783     nphit_p++;
784     enhit_p += (op->dedx).At(ih);
785     };
786     };
787     };
788     if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
789     for (Int_t ih=0; ih < op->npmtadc; ih++){
790     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
791     if(pl == 3){
792     nphit_p++;
793     enhit_p += (op->dedx).At(ih);
794     };
795     };
796     };
797     if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
798     for (Int_t ih=0; ih < op->npmtadc; ih++){
799     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
800     if(pl == 4){
801     nphit_p++;
802     enhit_p += (op->dedx).At(ih);
803     };
804     };
805     };
806 pam-fi 1.9
807 mocchiut 1.37 for (Int_t ih=0; ih < oi->npmtadc; ih++){
808     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
809     if(pl == 1 || pl == 2 || pl == 5){
810     nphit_i++;
811     enhit_i += (op->dedx).At(ih);
812     };
813     };
814     //
815     ti->DoTrack2(tr);
816     //
817     if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
818     for (Int_t ih=0; ih < oi->npmtadc; ih++){
819     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
820     if(pl == 0){
821     nphit_i++;
822     enhit_i += (op->dedx).At(ih);
823     };
824     };
825     };
826     if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
827     for (Int_t ih=0; ih < oi->npmtadc; ih++){
828     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
829     if(pl == 3){
830     nphit_i++;
831     enhit_i += (op->dedx).At(ih);
832     };
833 pam-fi 1.6 };
834 mocchiut 1.37 };
835     if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
836     for (Int_t ih=0; ih < oi->npmtadc; ih++){
837     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
838     if(pl == 4){
839     nphit_i++;
840     enhit_i += (op->dedx).At(ih);
841     };
842 pam-fi 1.13 };
843 mocchiut 1.37 };
844    
845 pam-fi 1.6
846 mocchiut 1.37 if(
847     use_TOF &&
848     (nphit_p+nphit_i) !=0 &&
849     true){
850    
851     // 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);
852     // printf(" score p %i score i %i \n",tp_score,ti_score);
853     // if( enhit_p > enhit_i ) tp_score++;
854     // if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
855     if ( nphit_p > nphit_i ) tp_score++;
856     if ( nphit_p < nphit_i ) ti_score++;
857     if ( nphit_p == nphit_i ){
858     if ( enhit_p > enhit_i ) tp_score++;
859     else ti_score++;
860     };
861     // printf(" dopo score p %i score i %i \n",tp_score,ti_score);
862     };
863     delete tr;
864     //
865     } else {
866     //
867     // NOT a NUCLEUS
868     //
869     //printf(" NOT a NUCLEUS! en = %f \n",sen);
870    
871     Int_t nphit_p =0;
872     Int_t nphit_i =0;
873 pam-fi 1.6
874    
875 mocchiut 1.37 /* cout << "track: npmtadc "<< op->npmtadc << endl;
876     cout << "track: npmttdc "<< op->npmttdc << endl;
877     cout << "image: npmtadc "<< oi->npmtadc << endl;
878     cout << "image: npmttdc "<< oi->npmttdc << endl;*/
879 pam-fi 1.6
880 pam-fi 1.41 // for (Int_t ih=0; ih < op->npmtadc; ih++){
881     // Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
882     // if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
883     // };
884    
885     // for (Int_t ih=0; ih < oi->npmtadc; ih++){
886     // Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
887     // if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
888     // };
889     // --- modified to count tdc signals (more efficient?)
890     // --- and to implement check on tdcflag
891     for (Int_t ih=0; ih < op->npmttdc; ih++){
892     Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
893     // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
894     if( (op->tdcflag).At(ih)==0 )nphit_p++;
895 mocchiut 1.37 };
896 pam-fi 1.6
897 pam-fi 1.41 for (Int_t ih=0; ih < oi->npmttdc; ih++){
898     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
899     // if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
900     if( (oi->tdcflag).At(ih)==0 )nphit_i++;
901 mocchiut 1.37 };
902 pam-fi 1.6
903 mocchiut 1.37 if(
904     (nphit_p+nphit_i) !=0 &&
905     true){
906 pam-fi 1.6
907 pam-fi 1.42 if ( nphit_p > nphit_i) tp_score++;
908     else if( nphit_p < nphit_i) ti_score++;
909     else ;//niente
910 mocchiut 1.37 };
911     };
912     // cout << "TOF "<<tp_score<<ti_score<<endl;
913     };
914 pam-fi 1.41
915    
916     // if(tp_score == ti_score) use_TRK = true;
917    
918    
919     // -----------------------------------------------------------------------------------------
920 mocchiut 1.37 // tracker check
921 pam-fi 1.41 // -----------------------------------------------------------------------------------------
922 mocchiut 1.37 // chi**2 difference is not always large enough to distinguish among
923     // the real track and its image.
924     // Tracker check will be applied always when calorimeter and tof information is ambiguous.
925 pam-fi 1.41 // *** MODIFIED ON AUGUST 2007 ***
926 mocchiut 1.37 if(use_TRK){
927 pam-fi 1.41 // if( tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
928     // else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
929    
930     // CHECK 1 : number of points along X
931     if ( tp->GetNX() >= ti->GetNX() )tp_score++ ;
932     if ( tp->GetNX() <= ti->GetNX() )ti_score++ ;
933     // CHECK 2 : number of points along Y
934     if ( tp->GetNY() >= ti->GetNY() )tp_score++ ;
935     if ( tp->GetNY() <= ti->GetNY() )ti_score++ ;
936     // CHECK 3 : chi**2 along X
937     // if( tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;
938     // if( ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;
939     // CHECK 4 : chi**2 along Y
940     // if( tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;
941     // if( ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;
942     // CHECK 5 : total chi**2
943     // if( tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;
944     // if( ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;
945    
946 mocchiut 1.37 // cout << "TRK "<<tp_score<<ti_score<<endl;
947     };
948 pam-fi 1.6
949 pam-fi 1.41
950    
951     // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
952 mocchiut 1.37 // the winner is....
953 pam-fi 1.41 // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
954 mocchiut 1.37 if (tp_score > ti_score) {
955 pam-fi 1.41
956 mocchiut 1.37
957     }else if (tp_score < ti_score) {
958    
959    
960     ts = ti;//its image!!
961     cs = ci;
962     os = oi;
963    
964     ti = tp;//its image!!
965     ci = cp;
966     oi = op;
967    
968     tp = ts;//its image!!
969     cp = cs;
970     op = os;
971 pam-fi 1.13
972    
973 mocchiut 1.37 }else {
974 pam-fi 1.41
975 pam-fi 1.39 // cout << "Warning - track image ambiguity not solved" << endl;
976 pam-fi 1.41
977 mocchiut 1.37 };
978 pam-fi 1.6
979 mocchiut 1.37 }else{
980     // ts = tp;
981     // cs = cp;
982     // os = op;
983     };
984    
985     // cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
986     // sorted_tracks->Add(ts);//save the track in the sorted array
987     // sorted_tracks.Add(ts);//save the track in the sorted array
988     // sorted_tracks.Add(tp);//save the track in the sorted array
989     // cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
990     // cout<<"o "<<tp<<endl;
991     // cout<<"o "<<cp<<endl;
992     // cout<<"o "<<op<<endl;
993     new(ttsorted[i]) PamTrack(tp,cp,op);
994     new(ttimage[i]) PamTrack(ti,ci,oi);
995     };
996    
997     if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
998     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
999     tsorted->Delete(); tsorted=0;
1000     timage->Delete(); timage=0;
1001     }
1002    
1003     //Restore Object count
1004     //To save space in the table keeping track of all referenced objects
1005     //We reset the object count to what it was at the beginning of the event.
1006     TProcessID::SetObjectCount(ObjectNumber);
1007 pam-fi 1.6
1008 pam-fi 1.1 };
1009     //--------------------------------------
1010     //
1011     //
1012     //--------------------------------------
1013     /**
1014 pam-fi 1.2 * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1015     * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1016     */
1017 pam-fi 1.13 // TRefArray *PamLevel2::GetTracks(){
1018    
1019     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1020     // SortTracks("+CAL+TOF");
1021     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1022    
1023     // // return sorted_tracks;
1024     // return &sorted_tracks;
1025    
1026     // };
1027     TClonesArray *PamLevel2::GetTracks(){
1028 pam-fi 1.8
1029     // *-*-*-*-*-*-*-*-*-*-*-*-*
1030 mocchiut 1.37 SortTracks();
1031 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1032    
1033 pam-fi 1.13 return tsorted;
1034    
1035 pam-fi 1.2 };
1036     //--------------------------------------
1037 pam-fi 1.3 //
1038     //
1039 pam-fi 1.2 //--------------------------------------
1040     /**
1041 pam-fi 1.1 * Retrieves the it-th Pamela "physical" track.
1042     * It override TrkLevel2::GetTrack(int it).
1043     * @param it Track number, ranging from 0 to GetNTracks().
1044     */
1045     PamTrack *PamLevel2::GetTrack(int it){
1046 pam-fi 1.13
1047     // if(!trk2_obj) return 0;
1048 pam-fi 1.3
1049 mocchiut 1.37 // // *-*-*-*+-*-*-*-*-*-*-*-*-*
1050 pam-fi 1.13 // SortTracks("+CAL+TOF");
1051     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1052     // // if(!sorted_tracks)return 0;
1053     // if(sorted_tracks.IsEmpty())return 0;
1054    
1055     // PamTrack *track = 0;
1056    
1057     // if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){
1058     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1059     // track = GetPamTrackAlong(t);
1060     // }else{
1061     // cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1062     // };
1063    
1064     // return track;
1065    
1066     // cout << "PamLevel2::GetTrack(int it) "<<endl;
1067 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1068 mocchiut 1.37 SortTracks();
1069 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1070 pam-fi 1.13 if(!tsorted)return 0;
1071     if(!tsorted->GetEntries())return 0;
1072    
1073 pam-fi 1.6 PamTrack *track = 0;
1074    
1075 pam-fi 1.13 if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){
1076     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1077     // track = GetPamTrackAlong(t);
1078     TClonesArray &t = *(tsorted);
1079     track = (PamTrack*)t[it];
1080    
1081 pam-fi 1.6 }else{
1082 pam-fi 1.13 cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1083 pam-fi 1.6 };
1084    
1085     return track;
1086    
1087 pam-fi 1.1 };
1088 pam-fi 1.3
1089 pam-fi 1.1 //--------------------------------------
1090     //
1091     //
1092     //--------------------------------------
1093     /**
1094 pam-fi 1.3 * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
1095 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
1096     */
1097     PamTrack *PamLevel2::GetTrackImage(int it){
1098    
1099 pam-fi 1.13 // if(!trk2_obj) return 0;
1100    
1101     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1102     // SortTracks("+CAL+TOF");
1103     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1104     // // if(!sorted_tracks)return 0;
1105     // if(sorted_tracks.IsEmpty())return 0;
1106    
1107     // PamTrack *image = 0;
1108    
1109     // if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1110     // TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);
1111     // if( temp->HasImage() ){
1112     // TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());
1113     // image = GetPamTrackAlong(t);
1114     // }else{
1115     // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1116     // };
1117     // }else{
1118     // cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1119     // };
1120    
1121     // return image;
1122    
1123    
1124 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1125 mocchiut 1.37 SortTracks();
1126 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1127 pam-fi 1.13 if(!timage)return 0;
1128     if(!timage->GetEntries())return 0;
1129 pam-fi 1.8
1130     PamTrack *image = 0;
1131    
1132 pam-fi 1.13 if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1133     TClonesArray &t = *(tsorted);
1134     PamTrack *temp = (PamTrack*)t[it];
1135     if( temp->GetTrkTrack()->HasImage() ){
1136     TClonesArray &t = *(timage);
1137     image = (PamTrack*)t[it];
1138 pam-fi 1.2 }else{
1139 pam-fi 1.13 // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1140 pam-fi 1.1 };
1141 pam-fi 1.8 }else{
1142 pam-fi 1.13 cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1143 pam-fi 1.8 };
1144 pam-fi 1.1
1145 pam-fi 1.8 return image;
1146 pam-fi 1.1 }
1147    
1148     //--------------------------------------
1149     //
1150     //
1151     //--------------------------------------
1152     /**
1153 pam-fi 1.2 * Get the Pamela detector trees in a single file and make them friends.
1154     * @param f TFile pointer
1155     * @param detlist String to select trees to be included
1156     * @return Pointer to a TTree
1157 pam-fi 1.1 */
1158 pam-fi 1.11 TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
1159 pam-fi 1.4
1160 pam-fi 1.12 // if( !detlist.IsNull() ) SetWhichTrees(detlist);
1161     // else GetWhichTrees(f);
1162 mocchiut 1.35 if ( pam_tree ){
1163     printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1164     return pam_tree;
1165     };
1166     //
1167    
1168 pam-fi 1.20 cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1169    
1170 pam-fi 1.12 SetWhichTrees(detlist);
1171 pam-fi 1.9
1172 pam-fi 1.6 TTree *Trout =0;
1173 pam-fi 1.9
1174     TString fname = f->GetName();
1175     if(!CheckLevel2File(fname))return NULL;
1176    
1177 pam-fi 1.20 // UInt_t *found=0;
1178 pam-fi 1.11
1179 pam-fi 1.12 cout<< "GetPamTree(TFile*,TString): detector list --> ";
1180     if(TRK1)cout<<"TRK1 ";
1181     if(TRK2)cout<<"TRK2 ";
1182     if(TRKh)cout<<"TRKH ";
1183     if(CAL1)cout<<"CAL1 ";
1184     if(CAL2)cout<<"CAL2 ";
1185     if(TOF)cout<<"TOF ";
1186     if(TRG)cout<<"TRG ";
1187     if(AC)cout<<"AC ";
1188     if(ND)cout<<"ND ";
1189     if(S4)cout<<"S4 ";
1190     if(ORB)cout<<"ORB ";
1191     cout << endl;
1192 pam-fi 1.20 if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1193 pam-fi 1.12
1194     f->cd();
1195    
1196 pam-fi 1.4 // Tracker
1197 pam-fi 1.1 TTree *T = (TTree*)f->Get("Tracker");
1198 pam-fi 1.8 if(T && (TRK2||TRK1||TRKh)) {
1199 pam-fi 1.9 if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1200 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel2",0,found);
1201 pam-fi 1.8 if(TRK2)cout << "Tracker : set branch address TrkLevel2"<<endl;
1202 pam-fi 1.9 if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1203 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel1",0,found);
1204 pam-fi 1.8 if(TRK1)cout << "Tracker : set branch address TrkLevel1"<<endl;
1205 pam-fi 1.9 if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1206 pam-fi 1.20 // else T->SetBranchStatus("TrkHough",0,found);
1207 pam-fi 1.9 if(TRKh)cout << "Tracker : set branch address TrkHough"<<endl;
1208     if(!Trout)Trout=T;
1209     else Trout->AddFriend(T);
1210 pam-fi 1.1 }else{
1211     cout << "Tracker : missing tree"<<endl;
1212     };
1213     // Calorimeter
1214     TTree *C = (TTree*)f->Get("Calorimeter");
1215 pam-fi 1.9 if(C && (CAL2||CAL1)) {
1216     if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1217 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel2",0,found);
1218 pam-fi 1.9 if(CAL2)cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1219     if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1220 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel1",0,found);
1221 pam-fi 1.9 if(CAL1)cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1222 pam-fi 1.4 if(!Trout)Trout=C;
1223     else Trout->AddFriend(C);
1224 pam-fi 1.1 }else{
1225     cout << "Calorimeter : missing tree"<<endl;
1226     };
1227 pam-fi 1.12
1228 pam-fi 1.1 // ToF
1229     TTree *O = (TTree*)f->Get("ToF");
1230 pam-fi 1.2 if(O && TOF) {
1231 pam-fi 1.9 O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1232 pam-fi 1.1 cout << "ToF : set branch address ToFLevel2"<<endl;
1233 pam-fi 1.4 if(!Trout)Trout=O;
1234     else Trout->AddFriend(O);
1235 pam-fi 1.1 }else{
1236     cout << "ToF : missing tree"<<endl;
1237     };
1238     // Trigger
1239     TTree *R = (TTree*)f->Get("Trigger");
1240 pam-fi 1.2 if(R && TRG) {
1241 pam-fi 1.9 R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1242 pam-fi 1.1 cout << "Trigger : set branch address TrigLevel2"<<endl;
1243 mocchiut 1.35 if(!Trout)Trout=R;
1244 pam-fi 1.4 else Trout->AddFriend(R);
1245 pam-fi 1.1 }else{
1246     cout << "Trigger : missing tree"<<endl;
1247     };
1248     // S4
1249     TTree *S = (TTree*)f->Get("S4");
1250 pam-fi 1.2 if(S && S4) {
1251 pam-fi 1.9 S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1252 pam-fi 1.1 cout << "S4 : set branch address S4Level2"<<endl;
1253 mocchiut 1.35 if(!Trout)Trout=S;
1254 pam-fi 1.4 else Trout->AddFriend(S);
1255 pam-fi 1.1 }else{
1256     cout << "S4 : missing tree"<<endl;
1257     };
1258     // Neutron Detector
1259     TTree *N = (TTree*)f->Get("NeutronD");
1260 pam-fi 1.2 if(N && ND) {
1261 pam-fi 1.9 N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1262 pam-fi 1.1 cout << "NeutronD : set branch address NDLevel2"<<endl;
1263 mocchiut 1.35 if(!Trout)Trout=N;
1264 pam-fi 1.4 else Trout->AddFriend(N);
1265 pam-fi 1.1 }else{
1266     cout << "NeutronD : missing tree"<<endl;
1267     };
1268     // Anticounters
1269     TTree *A = (TTree*)f->Get("Anticounter");
1270 pam-fi 1.2 if(A && AC) {
1271 pam-fi 1.9 A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1272 pam-fi 1.1 cout << "Anticounter : set branch address AcLevel2"<<endl;
1273 mocchiut 1.35 if(!Trout)Trout=A;
1274 pam-fi 1.4 else Trout->AddFriend(A);
1275 pam-fi 1.1 }else{
1276     cout << "Anticounter : missing tree"<<endl;
1277     };
1278 pam-fi 1.2 // Orbital Info
1279 pam-fi 1.11 TTree *B = (TTree*)f->Get("OrbitalInfo");
1280     if(B && ORB) {
1281     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1282     cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1283 mocchiut 1.35 if(!Trout)Trout=B;
1284 pam-fi 1.11 else Trout->AddFriend(B);
1285     }else{
1286     cout << "OrbitalInfo : missing tree"<<endl;
1287     };
1288 pam-fi 1.20
1289     TTree *L = (TTree*)f->Get("SelectionList");
1290     if(L && SELLI==1) {
1291     // L->SetBranchAddress("RunEntry",&irun);
1292     // cout << "SelectionList: set branch address RunEntry"<<endl;
1293     // L->SetBranchAddress("EventEntry",&irunentry);
1294     // cout << "SelectionList: set branch address EventEntry"<<endl;
1295     // if(!Trout)Trout=O;
1296     // else Trout->AddFriend("SelectionList");
1297     cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;
1298     sel_tree = 0;
1299     }else{
1300     cout << "SelectionList : missing tree"<<endl;
1301     };
1302 pam-fi 1.11
1303     cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1304 mocchiut 1.28
1305     pam_tree = (TChain*)Trout;
1306 pam-fi 1.1
1307 pam-fi 1.11 return Trout;
1308 pam-fi 1.1
1309     }
1310 pam-fi 1.2 //--------------------------------------
1311     //
1312     //
1313     //--------------------------------------
1314     /**
1315     * Get list of Level2 files.
1316     * @param ddir Level2 data directory.
1317     * @param flisttxt Name of txt file containing file list.
1318     * @return Pointer to a TList of TSystemFiles
1319     * If no input file list is given , all the Level2 files inside the directory are processed.
1320     */
1321     TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){
1322    
1323 pam-fi 1.6 TString wdir = gSystem->WorkingDirectory();
1324    
1325 mocchiut 1.35 // if(ddir=="")ddir = wdir;
1326 pam-fi 1.6 // TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1327 mocchiut 1.35 if ( ddir != ""){
1328     cout << "Level2 data directory : "<< ddir << endl;
1329     } else {
1330     cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1331     };
1332 pam-fi 1.6 TList *contents = new TList; // create output list
1333     contents->SetOwner();
1334    
1335 pam-fi 1.16 // char *fullpath;
1336 pam-fi 1.18 // const char *fullpath;
1337 pam-fi 1.6
1338 pam-fi 1.26 // if no input file list is given:
1339 pam-fi 1.6 if ( flisttxt != "" ){
1340 pam-fi 1.2
1341 pam-fi 1.16 // if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){
1342 pam-fi 1.18 // if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){
1343     // cout <<"File "<<flisttxt<<" not found"<<endl;
1344     // return 0;
1345     // }
1346     // flisttxt = fullpath;
1347 mocchiut 1.28 if ( !flisttxt.EndsWith(".root") ){
1348 pam-fi 1.18
1349     flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1350 pam-fi 1.16
1351 pam-fi 1.11 if( !gSystem->ChangeDirectory(ddir) ){
1352     cout << "Cannot change directory : "<<ddir<<endl;
1353     return 0;
1354     }
1355 pam-fi 1.2
1356 pam-fi 1.6 cout <<"Input file list : " << flisttxt <<endl;
1357     ifstream in;
1358     in.open(flisttxt, ios::in); //open input file list
1359 pam-fi 1.18 if(!in.good()){
1360     cout <<" ERROR opening the file "<<endl;
1361     gSystem->ChangeDirectory(wdir); // back to the working directory
1362     return 0;
1363     }
1364 pam-fi 1.9 int line=0;
1365 pam-fi 1.6 while (1) {
1366     TString file;
1367     in >> file;
1368     if (!in.good()) break;
1369 pam-fi 1.9 line++;
1370 pam-fi 1.8 // cout <<"(1) " << file << endl;
1371 pam-fi 1.9 if(file.IsNull()){
1372     cout << "-- list interrupted at line "<<line <<endl;
1373     break;
1374     }
1375 pam-fi 1.10 if(file.Contains("#"))file = file(0,file.First("#"));
1376     // cout <<"(2) " << file << endl;
1377 pam-fi 1.16 // if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1378 pam-fi 1.18 // if( (fullpath = gSystem->FindFile(ddir,file)) ){
1379 pam-fi 1.19 if( file.EndsWith(".root") ){
1380 mocchiut 1.35 TString filedir;
1381     if (ddir != ""){
1382     filedir = ddir; // take the input dir
1383     } else {
1384     gSystem->ChangeDirectory(wdir); // back to the working directory
1385     filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1386     };
1387     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1388 pam-fi 1.19 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1389     delete fullpath;
1390     }
1391 pam-fi 1.18 // }else{
1392 pam-fi 1.10 // if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1393 pam-fi 1.18 // };
1394 pam-fi 1.6 };
1395     in.close();
1396 mocchiut 1.28 } else {
1397 mocchiut 1.35 if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1398 mocchiut 1.28 char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1399     contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1400     delete fullpath;
1401     };
1402 pam-fi 1.6 }else{
1403 pam-fi 1.2
1404 pam-fi 1.6 cout << "No input file list given."<<endl;
1405     cout << "Check for existing root files."<<endl;
1406 pam-fi 1.2 // cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1407 mocchiut 1.35 if ( ddir == "" ){
1408     ddir = wdir;
1409     cout << "Level2 data directory : "<< ddir << endl;
1410     };
1411    
1412 pam-fi 1.6 TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1413     TList *temp = datadir->GetListOfFiles();
1414 pam-fi 1.2 // temp->Print();
1415     // cout << "*************************************" << endl;
1416    
1417 pam-fi 1.6 TIter next(temp);
1418     TSystemFile *questo = 0;
1419    
1420    
1421     while ( (questo = (TSystemFile*) next()) ) {
1422     TString name = questo-> GetName();
1423     if( name.EndsWith(".root") ){
1424 pam-fi 1.18 // const char *fullpath = gSystem->FindFile(ddir,name);
1425 pam-fi 1.16 // char *fullpath;
1426     // gSystem->IsFileInIncludePath(name,&fullpath);
1427 pam-fi 1.18 char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));
1428 pam-fi 1.6 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));
1429 pam-fi 1.18 delete fullpath;
1430 pam-fi 1.6 };
1431     }
1432     delete temp;
1433     delete datadir;
1434    
1435     };
1436     gSystem->ChangeDirectory(wdir); // back to the working directory
1437 pam-fi 1.5 // cout << endl << "Selected files:" << endl;
1438     // contents->Print();
1439 pam-fi 1.18 cout << contents->GetEntries()<<" files \n";
1440 pam-fi 1.5 // cout << endl;
1441 pam-fi 1.4 // cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
1442 pam-fi 1.6 return contents;
1443 pam-fi 1.2 };
1444     //--------------------------------------
1445     //
1446     //
1447     //--------------------------------------
1448     /**
1449     * Get the Pamela detector chains from a list of files and make them friends.
1450     * @param fl Pointer to a TList of TSystemFiles
1451     * @param detlist String to select trees to be included
1452     * @return Pointer to a TChain
1453     */
1454 pam-fi 1.11 TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1455 mocchiut 1.35 //
1456     //
1457     //
1458     if ( pam_tree ){
1459     printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1460     return pam_tree;
1461     };
1462     //
1463 pam-fi 1.11
1464     TChain *Trout =0;
1465 pam-fi 1.2
1466 pam-fi 1.12 // if( !detlist.IsNull() )SetWhichTrees(detlist);
1467     SetWhichTrees(detlist);
1468 pam-fi 1.6
1469 pam-fi 1.13 cout<< "GetPamTree(TList*,TString): input detector list --> ";
1470 pam-fi 1.12 if(TRK1)cout<<"TRK1 ";
1471     if(TRK2)cout<<"TRK2 ";
1472     if(TRKh)cout<<"TRKH ";
1473     if(CAL1)cout<<"CAL1 ";
1474     if(CAL2)cout<<"CAL2 ";
1475     if(TOF)cout<<"TOF ";
1476     if(TRG)cout<<"TRG ";
1477     if(AC)cout<<"AC ";
1478     if(ND)cout<<"ND ";
1479     if(S4)cout<<"S4 ";
1480     if(ORB)cout<<"ORB ";
1481     cout << endl;
1482    
1483 mocchiut 1.35 TChain *T = 0;
1484     TChain *C = 0;
1485     TChain *O = 0;
1486     TChain *R = 0;
1487     TChain *S = 0;
1488     TChain *N = 0;
1489     TChain *A = 0;
1490     TChain *B = 0;
1491 pam-fi 1.20
1492 mocchiut 1.35 TChain *L = 0;
1493 pam-fi 1.6
1494 pam-fi 1.8 if(TRK2||TRK1||TRKh) T = new TChain("Tracker");
1495 pam-fi 1.11 if(CAL2||CAL1) C = new TChain("Calorimeter");
1496     if(TOF) O = new TChain("ToF");
1497     if(TRG) R = new TChain("Trigger");
1498     if(S4) S = new TChain("S4");
1499     if(ND) N = new TChain("NeutronD");
1500     if(AC) A = new TChain("Anticounter");
1501     if(ORB) B = new TChain("OrbitalInfo");
1502 pam-fi 1.20 L = new TChain("SelectionList");
1503 pam-fi 1.6
1504     // loop over files and create chains
1505     TIter next(fl);
1506     TSystemFile *questo = 0;
1507     while ( (questo = (TSystemFile*) next()) ) {
1508     TString name = questo->GetName();
1509 pam-fi 1.13 cout << "File: "<< name << endl;
1510 pam-fi 1.6 if( CheckLevel2File(name) ){
1511 pam-fi 1.8 if(TRK2||TRK1||TRKh) T->Add(name);
1512 pam-fi 1.11 if(CAL1||CAL2) C->Add(name);
1513     if(TOF) O->Add(name);
1514     if(TRG) R->Add(name);
1515     if(S4) S->Add(name);
1516     if(ND) N->Add(name);
1517     if(AC) A->Add(name);
1518     if(ORB) B->Add(name);
1519 pam-fi 1.20 if(SELLI==1) L->Add(name);
1520 pam-fi 1.4 };
1521 mocchiut 1.35 };
1522 pam-fi 1.6
1523     cout << "done chain \n";
1524 pam-fi 1.36 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1525 pam-fi 1.11
1526 pam-fi 1.20 // UInt_t *found=0;
1527 pam-fi 1.11 // Tracker
1528     if(T && (TRK2||TRK1||TRKh)) {
1529     if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1530 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel2",0,found);
1531 pam-fi 1.11 if(TRK2)cout << "Tracker : set branch address TrkLevel2"<<endl;
1532     if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1533 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel1",0,found);
1534 pam-fi 1.11 if(TRK1)cout << "Tracker : set branch address TrkLevel1"<<endl;
1535     if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1536 pam-fi 1.20 // else T->SetBranchStatus("TrkHough",0,found);
1537 pam-fi 1.11 if(TRKh)cout << "Tracker : set branch address TrkHough"<<endl;
1538     if(!Trout)Trout=T;
1539 mocchiut 1.35 else Trout->AddFriend("Tracker");
1540 pam-fi 1.11 }else{
1541     cout << "Tracker : missing tree"<<endl;
1542     };
1543 pam-fi 1.6 // Calorimeter
1544 pam-fi 1.11 if(C && (CAL2||CAL1)) {
1545     if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1546 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel2",0,found);
1547 pam-fi 1.11 if(CAL2)cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1548     if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1549 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel1",0,found);
1550 pam-fi 1.11 if(CAL1)cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1551     if(!Trout)Trout=C;
1552     else Trout->AddFriend("Calorimeter");
1553     }else{
1554     cout << "Calorimeter : missing tree"<<endl;
1555 pam-fi 1.6 };
1556 pam-fi 1.4 // ToF
1557 pam-fi 1.11 if(O && TOF) {
1558     O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1559     cout << "ToF : set branch address ToFLevel2"<<endl;
1560     if(!Trout)Trout=O;
1561     else Trout->AddFriend("ToF");
1562     }else{
1563     cout << "ToF : missing tree"<<endl;
1564 pam-fi 1.6 };
1565 pam-fi 1.4 // Trigger
1566 pam-fi 1.11 if(R && TRG) {
1567     R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1568     cout << "Trigger : set branch address TrigLevel2"<<endl;
1569     if(!Trout)Trout=O;
1570     else Trout->AddFriend("Trigger");
1571     }else{
1572     cout << "Trigger : missing tree"<<endl;
1573 pam-fi 1.6 };
1574 pam-fi 1.4 // S4
1575 pam-fi 1.11 if(S && S4) {
1576     S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1577     cout << "S4 : set branch address S4Level2"<<endl;
1578     if(!Trout)Trout=O;
1579     else Trout->AddFriend("S4");
1580     }else{
1581     cout << "S4 : missing tree"<<endl;
1582 pam-fi 1.6 };
1583 pam-fi 1.4 // Neutron Detector
1584 pam-fi 1.11 if(N && ND) {
1585     N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1586     cout << "NeutronD : set branch address NDLevel2"<<endl;
1587     if(!Trout)Trout=O;
1588     else Trout->AddFriend("NeutronD");
1589     }else{
1590     cout << "NeutronD : missing tree"<<endl;
1591 pam-fi 1.6 };
1592 pam-fi 1.4 // Anticounters
1593 pam-fi 1.11 if(A && AC) {
1594     A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1595     cout << "Anticounter : set branch address AcLevel2"<<endl;
1596     if(!Trout)Trout=O;
1597     else Trout->AddFriend("Anticounter");
1598     }else{
1599     cout << "Anticounter : missing tree"<<endl;
1600 pam-fi 1.6 };
1601 pam-fi 1.11 // Orbital Info
1602     if(B && ORB) {
1603     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1604     cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1605     if(!Trout)Trout=O;
1606     else Trout->AddFriend("OrbitalInfo");
1607     }else{
1608     cout << "OrbitalInfo : missing tree"<<endl;
1609 pam-fi 1.6 };
1610 pam-fi 1.36 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1611 pam-fi 1.20
1612     // Selection List
1613     if(L && SELLI==1) {
1614     cout<<">>> Found selection-list <<<"<<endl;
1615     L->SetBranchAddress("RunEntry",&irun);
1616     cout << "SelectionList: set branch address RunEntry"<<endl;
1617     L->SetBranchAddress("EventEntry",&irunentry);
1618     cout << "SelectionList: set branch address EventEntry"<<endl;
1619     sel_tree = L;
1620     // if(!Trout)Trout=O;
1621     // else Trout->AddFriend("SelectionList");
1622 pam-fi 1.36 cout << "----------------------------------------------------" <<endl;
1623 pam-fi 1.20 }else{
1624     // cout << "SelectionList : missing tree"<<endl;
1625     if(L)L->Delete();
1626     };
1627 pam-fi 1.6
1628 pam-fi 1.11 // cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1629    
1630 pam-fi 1.20 pam_tree = Trout;
1631    
1632 pam-fi 1.11 return Trout;
1633 pam-fi 1.4 }
1634 pam-fi 1.8
1635    
1636    
1637 pam-fi 1.4 //--------------------------------------
1638     //
1639     //
1640     //--------------------------------------
1641     /**
1642     * Set branch addresses for Pamela friend trees
1643     */
1644 pam-fi 1.8 void PamLevel2::SetBranchAddress(TTree *t){
1645    
1646     TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
1647     TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
1648     TRKh = TRKh & t->GetBranchStatus("TrkHough");
1649     CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
1650     CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
1651     TOF = TOF & t->GetBranchStatus("ToFLevel2");
1652     TRG = TRG & t->GetBranchStatus("TrigLevel2");
1653     S4 = S4 & t->GetBranchStatus("S4Level2");
1654     ND = ND & t->GetBranchStatus("NDLevel2");
1655     AC = AC & t->GetBranchStatus("AcLevel2");
1656     ORB = ORB & t->GetBranchStatus("OrbitalInfo");
1657    
1658    
1659 pam-fi 1.4 // Tracker
1660 pam-fi 1.8 if(TRK1) {
1661 pam-fi 1.9 t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
1662 pam-fi 1.8 cout << "Tracker : set branch address TrkLevel1"<<endl;
1663     };
1664     if(TRK2) {
1665 pam-fi 1.9 t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
1666 pam-fi 1.6 cout << "Tracker : set branch address TrkLevel2"<<endl;
1667     };
1668 pam-fi 1.8 if(TRKh) {
1669 pam-fi 1.9 t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1670 pam-fi 1.8 cout << "Tracker : set branch address TrkHough"<<endl;
1671     };
1672 pam-fi 1.6
1673     // Calorimeter
1674 pam-fi 1.8 if(CAL1) {
1675 pam-fi 1.9 t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1676 pam-fi 1.8 cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1677     };
1678     if(CAL2) {
1679 pam-fi 1.9 t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1680 pam-fi 1.6 cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1681     };
1682    
1683 pam-fi 1.4 // ToF
1684 pam-fi 1.6 if(TOF) {
1685 pam-fi 1.9 t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1686 pam-fi 1.6 cout << "ToF : set branch address ToFLevel2"<<endl;
1687     };
1688 pam-fi 1.4 // Trigger
1689 pam-fi 1.6 if(TRG) {
1690 pam-fi 1.9 t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1691 pam-fi 1.6 cout << "Trigger : set branch address TrigLevel2"<<endl;
1692     };
1693 pam-fi 1.4 // S4
1694 pam-fi 1.6 if(S4) {
1695 pam-fi 1.9 t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1696 pam-fi 1.6 cout << "S4 : set branch address S4Level2"<<endl;
1697     };
1698 pam-fi 1.4 // Neutron Detector
1699 pam-fi 1.6 if(ND) {
1700 pam-fi 1.9 t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1701 pam-fi 1.6 cout << "NeutronD : set branch address NDLevel2"<<endl;
1702     };
1703 pam-fi 1.4 // Anticounters
1704 pam-fi 1.6 if(AC) {
1705 pam-fi 1.9 t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1706 pam-fi 1.6 cout << "Anticounter : set branch address AcLevel2"<<endl;
1707     };
1708 pam-fi 1.4 // OrbitalInfo
1709 pam-fi 1.6 if(ORB) {
1710 pam-fi 1.9 t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1711 pam-fi 1.6 cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1712     };
1713 pam-fi 1.20 // SelectionList
1714     if(SELLI==1) {
1715     t->SetBranchAddress("RunEntry", &irun);
1716     cout << "SelectionList: set branch address RunEntry"<<endl;
1717     t->SetBranchAddress("EventEntry", &irunentry);
1718     cout << "SelectionList: set branch address EventEntry"<<endl;
1719     };
1720 pam-fi 1.6
1721 pam-fi 1.4 }
1722 pam-fi 1.8 /**
1723     * Set branch addresses for Pamela friend trees
1724     */
1725     void PamLevel2::SetBranchAddress(TChain *t){
1726 pam-fi 1.4
1727 pam-fi 1.8 TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
1728     TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
1729     TRKh = TRKh & t->GetBranchStatus("TrkHough");
1730     CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
1731     CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
1732     TOF = TOF & t->GetBranchStatus("ToFLevel2");
1733     TRG = TRG & t->GetBranchStatus("TrigLevel2");
1734     S4 = S4 & t->GetBranchStatus("S4Level2");
1735     ND = ND & t->GetBranchStatus("NDLevel2");
1736     AC = AC & t->GetBranchStatus("AcLevel2");
1737     ORB = ORB & t->GetBranchStatus("OrbitalInfo");
1738    
1739     // Tracker
1740 pam-fi 1.9 if(TRK2) {
1741 pam-fi 1.11 t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1742 pam-fi 1.9 cout << "Tracker : set branch address TrkLevel2"<<endl;
1743     };
1744 pam-fi 1.8 if(TRK1) {
1745 pam-fi 1.9 t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1746 pam-fi 1.8 cout << "Tracker : set branch address TrkLevel1"<<endl;
1747     };
1748     if(TRKh) {
1749 pam-fi 1.9 t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1750 pam-fi 1.8 cout << "Tracker : set branch address TrkHough"<<endl;
1751     };
1752    
1753     // Calorimeter
1754 pam-fi 1.9 if(CAL2) {
1755     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1756     cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1757     };
1758 pam-fi 1.8 if(CAL1) {
1759 pam-fi 1.9 t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1760 pam-fi 1.8 cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1761     };
1762    
1763     // ToF
1764     if(TOF) {
1765 pam-fi 1.9 t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1766 pam-fi 1.8 cout << "ToF : set branch address ToFLevel2"<<endl;
1767     };
1768     // Trigger
1769     if(TRG) {
1770 pam-fi 1.9 t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1771 pam-fi 1.8 cout << "Trigger : set branch address TrigLevel2"<<endl;
1772     };
1773     // S4
1774     if(S4) {
1775 pam-fi 1.9 t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1776 pam-fi 1.8 cout << "S4 : set branch address S4Level2"<<endl;
1777     };
1778     // Neutron Detector
1779     if(ND) {
1780 pam-fi 1.9 t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1781 pam-fi 1.8 cout << "NeutronD : set branch address NDLevel2"<<endl;
1782     };
1783     // Anticounters
1784     if(AC) {
1785 pam-fi 1.9 t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1786 pam-fi 1.8 cout << "Anticounter : set branch address AcLevel2"<<endl;
1787     };
1788     // OrbitalInfo
1789     if(ORB) {
1790 pam-fi 1.9 t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1791 pam-fi 1.8 cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1792     };
1793 pam-fi 1.20 // SelectionList
1794     if(SELLI==1) {
1795     t->SetBranchAddress("RunEntry", &irun);
1796     cout << "SelectionList: set branch address RunEntry"<<endl;
1797     t->SetBranchAddress("EventEntry", &irunentry);
1798     cout << "SelectionList: set branch address EventEntry"<<endl;
1799     };
1800 pam-fi 1.8
1801     }
1802 pam-fi 1.9
1803    
1804 pam-fi 1.4 //--------------------------------------
1805     //
1806     //
1807     //--------------------------------------
1808     /**
1809     * Get the Run tree chain from a list of files.
1810     * @param fl Pointer to a TList of TSystemFiles
1811     * @return Pointer to a TChain
1812     */
1813     TChain *PamLevel2::GetRunTree(TList *fl){
1814 mocchiut 1.35 //
1815     //
1816     //
1817     if ( run_tree ){
1818     printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1819     return run_tree;
1820     };
1821     //
1822     TChain *R = new TChain("Run");
1823 pam-fi 1.6
1824     // loop over files and create chains
1825     TIter next(fl);
1826     TSystemFile *questo = 0;
1827     while ( (questo = (TSystemFile*) next()) ) {
1828     TString name = questo->GetName();
1829 pam-fi 1.4 // cout << "File: "<< name << endl;
1830 pam-fi 1.6 if( CheckLevel2File(name) ){
1831     R->Add(name);
1832     };
1833     }
1834 pam-fi 1.20
1835     if(R->GetNtrees()){
1836 pam-fi 1.6
1837 pam-fi 1.20 R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1838     cout << "Run : set branch address RunInfo"<<endl;
1839     R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1840     cout << "Software : set branch address SoftInfo"<<endl; // Emiliano
1841     }else{
1842     delete R;
1843     R=0;
1844     }
1845    
1846     run_tree = R;
1847    
1848 pam-fi 1.6 return R;
1849 pam-fi 1.4
1850     }
1851     //--------------------------------------
1852     //
1853     //
1854     //--------------------------------------
1855     /**
1856     * Get the Run tree from a file.
1857     * @param f Pointer to a TFile
1858     * @return Pointer to a TTree
1859     */
1860     TTree *PamLevel2::GetRunTree(TFile *f){
1861 mocchiut 1.35 if ( run_tree ){
1862     printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1863     return run_tree;
1864     };
1865    
1866 pam-fi 1.4
1867 pam-fi 1.20 cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1868 pam-fi 1.4
1869 mocchiut 1.35 TTree *T = (TTree*)f->Get("Run");
1870 pam-fi 1.6
1871 mocchiut 1.35 if(T){
1872     T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1873 pam-fi 1.20 cout << "Run : set branch address RunInfo"<<endl;
1874 mocchiut 1.35 T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1875 pam-fi 1.20 cout << "Software : set branch address SoftInfo"<<endl; // Emiliano
1876     }
1877 mocchiut 1.28
1878 mocchiut 1.35 run_tree = (TChain*)T;
1879 mocchiut 1.28
1880 mocchiut 1.35 return T;
1881 pam-fi 1.2
1882     }
1883 mocchiut 1.17 /**
1884     * Update the runinfo informations (to be used to have Run infos event by event basis)
1885     * @param run Pointer to the chain/tree which contains run infos
1886     * @return true if a new run has been read, false if it is still the same run
1887     */
1888 pam-fi 1.33 Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1889 mocchiut 1.17 //
1890     // check if we have already called once GetEntry, if not call it
1891     //
1892 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;
1893 pam-fi 1.34 if(!run){
1894     cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1895     return(false);
1896     }
1897     if ( run->GetEntries() <= 0 ) return(false);
1898 mocchiut 1.17 //
1899 pam-fi 1.20
1900 mocchiut 1.35 // Int_t oldrun = irun;
1901 pam-fi 1.33 Long64_t oldrun = irun;
1902 mocchiut 1.35
1903 pam-fi 1.20 // --------------------------------------
1904     // if it is a full file (not preselected)
1905     // --------------------------------------
1906     if(SELLI==0){
1907 pam-fi 1.33
1908 pam-fi 1.36 //
1909     // the absolute time is necessary to relate the event with the run
1910     //
1911 pam-fi 1.43 if( !GetOrbitalInfo() && !ISGP){
1912 pam-fi 1.33 cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1913     return(false);
1914     }
1915    
1916 pam-fi 1.43 ULong64_t abstime = 0;
1917     if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
1918    
1919    
1920 pam-fi 1.36 //
1921     // the first time the routine is called, set run search from the beginning
1922     //
1923 mocchiut 1.35 if ( irun < 0LL ){
1924     irun = 0LL;
1925 pam-fi 1.26 run->GetEntry(irun);
1926 pam-fi 1.33 runfirstentry = 0LL;
1927     runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1928     if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1929 pam-fi 1.43
1930     if( ISGP && run->GetEntries()!=1 ){
1931     cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
1932     cout << "** WARNING ** run will not be updated"<<endl;
1933     }
1934    
1935 pam-fi 1.24 };
1936 mocchiut 1.35 //
1937 pam-fi 1.43 if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
1938     //
1939     if ( irun == run->GetEntries()-1LL &&
1940     !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
1941     abstime <= GetRunInfo()->RUNTRAILER_TIME)
1942 mocchiut 1.35 ){
1943     irun = -1LL;
1944     runfirstentry = 0LL;
1945     runlastentry = -1LL;
1946     };
1947 pam-fi 1.24 // modificato il controllo sull'aggiornamento del run, per evitare problemi
1948     // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
1949 mocchiut 1.31 //
1950     bool fromfirst = true;
1951     //
1952 pam-fi 1.43 while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
1953 pam-fi 1.24 // while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
1954 pam-fi 1.20 irun++;
1955     run->GetEntry(irun);
1956 mocchiut 1.30 runfirstentry = runlastentry;
1957 pam-fi 1.33 if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
1958     runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1959     // cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1960     // cout << "runfirstentry "<<runfirstentry<<endl;
1961 mocchiut 1.31 // printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
1962 pam-fi 1.43 // printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
1963 mocchiut 1.31 // printf(" IDRUN %u \n",GetRunInfo()->ID);
1964     //
1965 pam-fi 1.33 // prevshift = 0;
1966 mocchiut 1.31 //
1967 pam-fi 1.43 if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
1968 pam-fi 1.33 printf(" resetting irun (it should NOT happen!!!)\n");
1969 pam-fi 1.32 fromfirst = false;
1970     irun = 0;
1971     run->GetEntry(irun);
1972     runfirstentry = 0ULL;
1973 pam-fi 1.33 runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1974     if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1975 mocchiut 1.31 };
1976     //
1977 pam-fi 1.20 };
1978 mocchiut 1.31 //
1979 mocchiut 1.35 if (
1980 pam-fi 1.43 !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
1981     abstime <= GetRunInfo()->RUNTRAILER_TIME)
1982 pam-fi 1.33 ) {
1983 pam-fi 1.43 printf(" Something very wrong here: cannot find RUN containing absolute time %u \n",abstime);
1984 pam-fi 1.33 return false;
1985     }
1986 pam-fi 1.20 //
1987 mocchiut 1.30 if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
1988 pam-fi 1.20 //
1989     // printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
1990     //
1991 pam-fi 1.33 prevshift = 0;
1992     cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1993 pam-fi 1.34 // cout << "runfirstentry "<<runfirstentry<<endl;
1994 pam-fi 1.20 return(true);
1995 mocchiut 1.17 };
1996 pam-fi 1.20 // ----------------------------------------------------
1997     // if it is a preselected file (there is SelectionList)
1998     // NBNB - the event tree MUST be read first
1999     // ----------------------------------------------------
2000     if(SELLI==1){
2001     sel_tree->GetEntry(iev);
2002 pam-fi 1.34 // cout << irun << " "<< irunentry << endl;
2003 pam-fi 1.20 if(irun != oldrun){
2004     run->GetEntry(irun);
2005 pam-fi 1.33 cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2006 pam-fi 1.32 prevshift = 0;
2007 pam-fi 1.20 return true;
2008     }
2009     return false;
2010     }
2011    
2012     return false;
2013 mocchiut 1.17 //
2014     };
2015 pam-fi 1.33
2016 pam-fi 1.36 Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2017    
2018     if(!run_tree){
2019     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2020     return false;
2021     }
2022     if ( run_tree->GetEntries() <= 0 ) {
2023     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2024     return(false);
2025     }
2026 pam-fi 1.33
2027 pam-fi 1.36 Int_t oldrun = irun; // store current run index
2028    
2029     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2030     // if it is a full file (not preselected)
2031     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2032     if(SELLI==0){
2033    
2034 pam-fi 1.43 ULong64_t abstime = 0;
2035     ULong64_t obt = 0;
2036     if( GetOrbitalInfo() ){
2037     abstime = GetOrbitalInfo()->absTime;
2038     obt = GetOrbitalInfo()->OBT;
2039     }
2040 pam-fi 1.36 // ---------------------------------------------------------------
2041     // the absolute time is necessary to relate the event with the run
2042     // ---------------------------------------------------------------
2043 pam-fi 1.43 if( !GetOrbitalInfo() && !ISGP ){
2044 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2045     return(false);
2046     }
2047    
2048     // -----------------------------------------------------------------------
2049     // the first time the routine is called, set run search from the beginning
2050     // -----------------------------------------------------------------------
2051     if ( irun < 0 ){
2052     irun = 0LL;
2053 pam-fi 1.39 irunentry = 0;
2054     prevshift = 0;
2055 pam-fi 1.36 run_tree->GetEntry(irun);
2056 pam-fi 1.43
2057     if( ISGP && run_tree->GetEntries()!=1 ){
2058     cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;
2059     cout << "** WARNING ** run will not be updated"<<endl;
2060     }
2061 pam-fi 1.36 };
2062 pam-fi 1.43
2063     // -----------------------------------------------------------------------
2064     // if it is simulation, assigh abstime by end (temporaneo!!!)
2065     // -----------------------------------------------------------------------
2066     if(ISGP){
2067     abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2068     obt = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2069     }
2070 pam-fi 1.36 //
2071     bool fromfirst = true; // first loop over runs
2072 pam-fi 1.39
2073     // Bool_t hasfrag = false;
2074     // if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2075     // ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2076    
2077 pam-fi 1.36 // ------------------------------------------------------
2078     // loop over runs to find the one that contains the event
2079     // ------------------------------------------------------
2080 pam-fi 1.39 while (
2081     (
2082     (
2083 pam-fi 1.43 !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
2084     abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2085     !(obt >= GetRunInfo()->RUNHEADER_OBT && // additional check on OBT (ms)
2086     obt <= GetRunInfo()->RUNTRAILER_OBT)
2087 pam-fi 1.39 )
2088     || GetRunInfo()->NEVENTS==0
2089     || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)
2090     )
2091     && irun < run_tree->GetEntries() ){
2092    
2093 pam-fi 1.36 irun++;
2094     // ------------------------------------
2095     // if the end of run tree is reached...
2096     // ------------------------------------
2097     if( irun == run_tree->GetEntries() ){
2098     if(!fromfirst){
2099     // -----------------------------------------------------
2100     // if it happened already once and the run was not found
2101     // ---> exit with error
2102     // -----------------------------------------------------
2103     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2104     return false;
2105     }
2106     // -----------------------------------------
2107     // ...otherwise repeat search from beginning
2108     // -----------------------------------------
2109     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2110     fromfirst = false;
2111     irun = 0LL;
2112     runfirstentry = 0LL;
2113     }
2114     // -------------------------------------------------------------------
2115     // save the index of the first entry of the run, relative to pam_tree,
2116     // and read a new run
2117     // -------------------------------------------------------------------
2118 pam-fi 1.39 if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2119     irunentry = 0;
2120     prevshift = 0;
2121 pam-fi 1.36 run_tree->GetEntry(irun);
2122 pam-fi 1.39 if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2123 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<" has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2124     cout << " (NB!! in this case some events are assigned to a wrong run)"<<endl;
2125     }
2126 pam-fi 1.39 // if(hasfrag && fragid != GetRunInfo()->ID){
2127     // cout << "... where is the next fragment ??"<<endl;
2128     // }
2129 pam-fi 1.36 };
2130 pam-fi 1.39
2131    
2132 pam-fi 1.36 // --------------------------------------
2133     // if there was no need to update the run
2134     // ---> exit with FALSE
2135     // --------------------------------------
2136     if ( irun == oldrun ) return(false);
2137    
2138     // --------------------------------------
2139     // ... otherwise
2140     // ---> exit with TRUE
2141     // --------------------------------------
2142     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2143 pam-fi 1.39 // ----------------------------------------------------
2144     // then check if the run has a fragment
2145     // in this case we have to switch to the next fragment
2146     // when the end of the first fragment is reached
2147     // ----------------------------------------------------
2148     if(
2149     GetRunInfo()->ID_RUN_FRAG != 0 &&
2150     // GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2151     true ){
2152     cout << "* fragment *"<<endl;
2153     }
2154    
2155 pam-fi 1.36 return(true);
2156     };
2157     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2158     // if it is a preselected file (there is SelectionList)
2159     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2160     if(SELLI==1){
2161     sel_tree->GetEntry(iev);
2162     if(irun != oldrun){
2163     run_tree->GetEntry(irun);
2164     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2165     prevshift = 0;
2166     return true;
2167     }
2168     return false;
2169     }
2170    
2171     return false;
2172     //
2173     };
2174 mocchiut 1.17 /**
2175     * Update the runinfo informations (to be used to have Run infos event by event basis)
2176     * @param run Pointer to the chain/tree which contains run infos
2177     * @return true if a new run has been read, false if it is still the same run
2178     */
2179 pam-fi 1.33 Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2180 mocchiut 1.17 return(UpdateRunInfo((TChain*)run,iev));
2181     };
2182    
2183 pam-fi 1.2 //--------------------------------------
2184     //
2185     //
2186     //--------------------------------------
2187     /**
2188 pam-fi 1.8 * Set which trees shoul be analysed
2189 pam-fi 1.2 * @param detlist TString containing the sequence of trees required
2190     */
2191     void PamLevel2::SetWhichTrees(TString detlist){
2192    
2193 pam-fi 1.12 if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2194 pam-fi 1.20 CAL0 = false;
2195 pam-fi 1.8 CAL1 = true;
2196     CAL2 = true;
2197     TRK2 = true;
2198     TRK1 = false;
2199     TRKh = false;
2200 pam-fi 1.20 TRK0 = false;
2201 pam-fi 1.6 TRG = true;
2202     TOF = true;
2203 pam-fi 1.20 TOF0 = false;
2204 pam-fi 1.6 S4 = true;
2205     ND = true;
2206     AC = true;
2207     ORB = true;
2208     }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2209 pam-fi 1.20 CAL0 = false;
2210 pam-fi 1.8 CAL1 = false;
2211     CAL2 = false;
2212     TRK2 = false;
2213     TRK1 = false;
2214     TRKh = false;
2215 pam-fi 1.20 TRK0 = false;
2216 pam-fi 1.6 TRG = false;
2217     TOF = false;
2218 pam-fi 1.20 TOF0 = false;
2219 pam-fi 1.6 S4 = false;
2220     ND = false;
2221     AC = false;
2222     ORB = false;
2223     };
2224    
2225 pam-fi 1.8 // -------------------------------------------------------------------------
2226     if( detlist.Contains("CAL1", TString::kIgnoreCase) ){
2227     if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
2228     if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
2229     };
2230 pam-fi 1.20
2231     if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
2232     if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
2233     if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
2234     };
2235 pam-fi 1.8
2236     if( detlist.Contains("CAL2", TString::kIgnoreCase)){
2237     if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
2238     if ( detlist.Contains("+CAL2", TString::kIgnoreCase) )CAL2=true;
2239     };
2240    
2241     if( detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2 )CAL2=true;
2242     if( detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2 ){
2243     CAL2=false;
2244     CAL1=false;
2245     }
2246     // -------------------------------------------------------------------------
2247 pam-fi 1.20 if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
2248     if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
2249     if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
2250     };
2251    
2252 pam-fi 1.8 if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
2253     if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
2254     if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
2255     };
2256    
2257     if( detlist.Contains("TRK2", TString::kIgnoreCase)){
2258     if ( detlist.Contains("-TRK2", TString::kIgnoreCase) )TRK2=false;
2259     if ( detlist.Contains("+TRK2", TString::kIgnoreCase) )TRK2=true;
2260     };
2261    
2262     if( detlist.Contains("TRKh", TString::kIgnoreCase)){
2263     if ( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh=false;
2264     if ( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh=true;
2265     };
2266    
2267     if( detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh )TRK2=true;
2268     if( detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh){
2269     TRK2=false;
2270     TRK1=false;
2271     TRKh=false;
2272     }
2273     // -------------------------------------------------------------------------
2274 pam-fi 1.6
2275     if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;
2276     else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;
2277    
2278     if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
2279     else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
2280 pam-fi 1.20
2281     if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
2282     else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
2283 pam-fi 1.6
2284     if( detlist.Contains("-S4", TString::kIgnoreCase) )S4 = false;
2285     else if( detlist.Contains("+S4", TString::kIgnoreCase) )S4 = true;
2286    
2287     if( detlist.Contains("-ND", TString::kIgnoreCase) )ND = false;
2288     else if( detlist.Contains("+ND", TString::kIgnoreCase) )ND = true;
2289    
2290     if( detlist.Contains("-AC", TString::kIgnoreCase) )AC = false;
2291     else if( detlist.Contains("+AC", TString::kIgnoreCase) )AC = true;
2292    
2293     if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
2294     else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
2295 pam-fi 1.11
2296 pam-fi 1.12 // cout<< "Set detector list --> ";
2297     // if(TRK1)cout<<"TRK1 ";
2298     // if(TRK2)cout<<"TRK2 ";
2299     // if(TRKh)cout<<"TRKH ";
2300     // if(CAL1)cout<<"CAL1 ";
2301     // if(CAL2)cout<<"CAL2 ";
2302     // if(TOF)cout<<"TOF ";
2303     // if(TRG)cout<<"TRG ";
2304     // if(AC)cout<<"AC ";
2305     // if(ND)cout<<"ND ";
2306     // if(S4)cout<<"S4 ";
2307     // if(ORB)cout<<"ORB ";
2308     // cout << endl;
2309 pam-fi 1.6
2310 pam-fi 1.2 };
2311 pam-fi 1.11
2312    
2313     /**
2314     * Set tree/branch detector flags from the content of a tree
2315     */
2316     void PamLevel2::GetWhichTrees(TFile* f){
2317 pam-fi 1.20
2318    
2319    
2320     cout << "void PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
2321 pam-fi 1.11 // -----------
2322     // reset flags
2323     // -----------
2324 pam-fi 1.12 CAL1 = false;
2325     CAL2 = false;
2326     TRK2 = false;
2327     TRK1 = false;
2328     TRKh = false;
2329     TRG = false;
2330     TOF = false;
2331     S4 = false;
2332     ND = false;
2333     AC = false;
2334     ORB = false;
2335 pam-fi 1.11
2336 pam-fi 1.12 RUN = false;
2337 pam-fi 1.11
2338     cout << "Checking file: "<<f->GetName()<<endl;
2339     if( !f || f->IsZombie() ){
2340     cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
2341     return;
2342     }
2343    
2344     TList *lk = f->GetListOfKeys();
2345     TIter next(lk);
2346     TKey *key =0;
2347    
2348     Int_t nev = 0;
2349    
2350     while( (key = (TKey*)next()) ){
2351    
2352     if( !strcmp(key->GetName(),"Run" ) )RUN = true;
2353    
2354     //=========================================================
2355     if( !strcmp(key->GetName(),"Trigger" ) ){
2356     TRG = true;
2357     Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
2358     if( nev && nevt!=nev){
2359     cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
2360     TRG = false;
2361     }else nev=nevt;
2362     }
2363     //=========================================================
2364     if( !strcmp(key->GetName(),"ToF" ) ){
2365     TOF = true;
2366     Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
2367     if( nev && nevt!=nev){
2368     cout << "File: "<< f->GetName() <<" ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
2369     TOF = false;
2370     }else nev=nevt;
2371     }
2372     //=========================================================
2373     if( !strcmp(key->GetName(),"S4" ) ){
2374     S4 = true;
2375     Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
2376     if( nev && nevt!=nev){
2377     cout << "File: "<< f->GetName() <<" S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
2378     S4 = false;
2379     }else nev=nevt;
2380     }
2381     //=========================================================
2382    
2383     if( !strcmp(key->GetName(),"NeutronD" ) ){
2384     ND = true;
2385     Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2386     if( nev && nevt!=nev){
2387     cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2388     ND =false;
2389     }else nev=nevt;
2390     }
2391     //=========================================================
2392     if( !strcmp(key->GetName(),"Anticounter") ){
2393     AC = true;
2394     Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2395     if( nev && nevt!=nev){
2396     cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2397     AC =false;
2398     }else nev=nevt;
2399     }
2400     //=========================================================
2401     if( !strcmp(key->GetName(),"OrbitalInfo") ){
2402     ORB = true;
2403     Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2404     if( nev && nevt!=nev){
2405     cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2406     ORB = false;
2407     }else nev=nevt;
2408     }
2409     //=========================================================
2410     if( !strcmp(key->GetName(),"Tracker" ) ){
2411     TTree *T = (TTree*)f->Get("Tracker");
2412     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2413     TString name = T->GetListOfBranches()->At(i)->GetName();
2414     if( !name.CompareTo("TrkLevel1") )TRK1=true;
2415     if( !name.CompareTo("TrkLevel2") )TRK2=true;
2416     if( !name.CompareTo("TrkHough") )TRKh=true;
2417     };
2418     Int_t nevt = T->GetEntries();
2419     if( nev && nevt!=nev){
2420     cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2421     TRK1 = false;
2422     TRK2 = false;
2423     TRKh = false;
2424     }else nev=nevt;
2425     T->Delete();
2426     };
2427     //=========================================================
2428     if( !strcmp(key->GetName(),"Calorimeter" ) ){
2429     TTree *T = (TTree*)f->Get("Calorimeter");
2430     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2431     TString name = T->GetListOfBranches()->At(i)->GetName();
2432     if( !name.CompareTo("CaloLevel1") )CAL1=true;
2433     if( !name.CompareTo("CaloLevel2") )CAL2=true;
2434     };
2435     Int_t nevt = T->GetEntries();
2436     if( nev && nevt!=nev){
2437     cout << "File: "<< f->GetName() <<" Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2438     CAL1 = false;
2439     CAL2 = false;
2440     }else nev=nevt;
2441     T->Delete();
2442     };
2443    
2444     };
2445    
2446 pam-fi 1.13 delete lk;
2447 pam-fi 1.12
2448     // cout<< "Get detector list from input file --> ";
2449     // if(TRK1)cout<<"TRK1 ";
2450     // if(TRK2)cout<<"TRK2 ";
2451     // if(TRKh)cout<<"TRKH ";
2452     // if(CAL1)cout<<"CAL1 ";
2453     // if(CAL2)cout<<"CAL2 ";
2454     // if(TOF)cout<<"TOF ";
2455     // if(TRG)cout<<"TRG ";
2456     // if(AC)cout<<"AC ";
2457     // if(ND)cout<<"ND ";
2458     // if(S4)cout<<"S4 ";
2459     // if(ORB)cout<<"ORB ";
2460     // cout << endl;
2461 pam-fi 1.11
2462     return ;
2463    
2464     };
2465    
2466    
2467 pam-fi 1.2 //--------------------------------------
2468     //
2469     //
2470     //--------------------------------------
2471     /**
2472 pam-fi 1.3 * Check if a file contains selected Pamela Level2 trees.
2473 pam-fi 1.2 * @param name File name
2474 pam-fi 1.4 * @return true if the file is ok.
2475 pam-fi 1.2 */
2476     Bool_t PamLevel2::CheckLevel2File(TString name){
2477    
2478 pam-fi 1.8 Bool_t CAL1__ok = false;
2479     Bool_t CAL2__ok = false;
2480     Bool_t TRK2__ok = false;
2481     Bool_t TRK1__ok = false;
2482     Bool_t TRKh__ok = false;
2483 pam-fi 1.7 Bool_t TRG__ok = false;
2484     Bool_t TOF__ok = false;
2485     Bool_t S4__ok = false;
2486     Bool_t ND__ok = false;
2487     Bool_t AC__ok = false;
2488     Bool_t ORB__ok = false;
2489 pam-fi 1.6
2490 pam-fi 1.7 Bool_t RUN__ok = false;
2491 pam-fi 1.6
2492 pam-fi 1.20 Bool_t SELLI__ok = false;
2493 pam-fi 1.8
2494     cout << "Checking file: "<<name<<endl;
2495 pam-fi 1.6 TFile *f = new TFile(name.Data());
2496     if( !f || f->IsZombie() ){
2497     cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;
2498     }
2499 pam-fi 1.7 // cout << "Get list of keys: "<<f<<endl;
2500 pam-fi 1.6 TList *lk = f->GetListOfKeys();
2501 pam-fi 1.8 // lk->Print();
2502 pam-fi 1.6 TIter next(lk);
2503     TKey *key =0;
2504 pam-fi 1.8
2505     Int_t nev = 0;
2506    
2507 pam-fi 1.6 while( (key = (TKey*)next()) ){
2508    
2509 pam-fi 1.20 // cout << key->GetName() << endl;
2510 pam-fi 1.12 // cout << key->GetName() << ""<<key->GetClassName()<<endl;
2511     // cout << " Get tree: " << f->Get(key->GetName())<<endl;
2512 pam-fi 1.7 // nev_previous = nev;
2513     // cout << " n.entries "<< nev <<endl;
2514     // if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
2515     // nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
2516     // cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
2517     // return false;
2518     // };
2519 pam-fi 1.6
2520 pam-fi 1.20
2521 pam-fi 1.6 if( !strcmp(key->GetName(),"Run" ) )RUN__ok = true;
2522 pam-fi 1.8
2523     //=========================================================
2524 pam-fi 1.20 if( !strcmp(key->GetName(),"SelectionList" ) ){
2525     SELLI__ok = true;
2526     if(SELLI==1){
2527     Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
2528     if( nev && nevt!=nev){
2529     cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
2530     return false;
2531     }
2532     nev=nevt;
2533     }
2534     }
2535    
2536     //=========================================================
2537 pam-fi 1.8 if( !strcmp(key->GetName(),"Trigger" ) ){
2538     TRG__ok = true;
2539     if(TRG){
2540     Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
2541     if( nev && nevt!=nev){
2542     cout << "File: "<< f->GetName() <<" discarded ---- Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
2543     return false;
2544     }
2545     nev=nevt;
2546     }
2547     }
2548     //=========================================================
2549     if( !strcmp(key->GetName(),"ToF" ) ){
2550     TOF__ok = true;
2551     if(TOF){
2552     Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
2553     if( nev && nevt!=nev){
2554     cout << "File: "<< f->GetName() <<" discarded ---- ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
2555     return false;
2556     }
2557     nev=nevt;
2558     }
2559     }
2560     //=========================================================
2561     if( !strcmp(key->GetName(),"S4" ) ){
2562     S4__ok = true;
2563     if(S4){
2564     Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
2565     if( nev && nevt!=nev){
2566     cout << "File: "<< f->GetName() <<" discarded ---- S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
2567     return false;
2568     }
2569     nev=nevt;
2570     }
2571     }
2572     //=========================================================
2573    
2574     if( !strcmp(key->GetName(),"NeutronD" ) ){
2575     ND__ok = true;
2576     if(ND){
2577     Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2578     if( nev && nevt!=nev){
2579     cout << "File: "<< f->GetName() <<" discarded ---- NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2580     return false;
2581     }
2582     nev=nevt;
2583     }
2584     }
2585     //=========================================================
2586     if( !strcmp(key->GetName(),"Anticounter") ){
2587     AC__ok = true;
2588     if(AC){
2589     Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2590     if( nev && nevt!=nev){
2591     cout << "File: "<< f->GetName() <<" discarded ---- Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2592     return false;
2593     }
2594     nev=nevt;
2595     }
2596     }
2597     //=========================================================
2598     if( !strcmp(key->GetName(),"OrbitalInfo") ){
2599     ORB__ok = true;
2600     if(ORB){
2601     Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2602     if( nev && nevt!=nev){
2603     cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2604     return false;
2605     }
2606     nev=nevt;
2607     }
2608     }
2609     //=========================================================
2610 pam-fi 1.6 if( !strcmp(key->GetName(),"Tracker" ) ){
2611     TTree *T = (TTree*)f->Get("Tracker");
2612 pam-fi 1.8 if(TRK1||TRK2||TRKh){
2613     Int_t nevt = T->GetEntries();
2614     if( nev && nevt!=nev){
2615     cout << "File: "<< f->GetName() <<" discarded ---- Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2616     return false;
2617     }
2618     nev=nevt;
2619     }
2620 pam-fi 1.6 for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2621     TString name = T->GetListOfBranches()->At(i)->GetName();
2622 pam-fi 1.8 if( !name.CompareTo("TrkLevel1") )TRK1__ok=true;
2623     if( !name.CompareTo("TrkLevel2") )TRK2__ok=true;
2624     if( !name.CompareTo("TrkHough") )TRKh__ok=true;
2625     };
2626     T->Delete();
2627 pam-fi 1.2 };
2628 pam-fi 1.8 //=========================================================
2629     if( !strcmp(key->GetName(),"Calorimeter" ) ){
2630     TTree *T = (TTree*)f->Get("Calorimeter");
2631     if(CAL1||CAL2){
2632     Int_t nevt = T->GetEntries();
2633     if( nev && nevt!=nev){
2634     cout << "File: "<< f->GetName() <<" discarded ---- Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2635     return false;
2636     }
2637     nev=nevt;
2638     }
2639     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2640     TString name = T->GetListOfBranches()->At(i)->GetName();
2641     if( !name.CompareTo("CaloLevel1") )CAL1__ok=true;
2642     if( !name.CompareTo("CaloLevel2") )CAL2__ok=true;
2643     };
2644     T->Delete();
2645     };
2646    
2647 pam-fi 1.6 };
2648 pam-fi 1.20
2649     if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
2650     if( SELLI==0 && SELLI__ok ){
2651     cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
2652     return false;
2653     }
2654     if( SELLI==1 && !SELLI__ok ){
2655     cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
2656     return false;
2657     }
2658 pam-fi 1.6
2659 pam-fi 1.20 // cout << "SELLI "<<SELLI<<endl;
2660    
2661 pam-fi 1.12 // cout<< "CheckLevel2File(TString): detector list --> ";
2662     // if(TRK1__ok)cout<<"TRK1 ";
2663     // if(TRK2__ok)cout<<"TRK2 ";
2664     // if(TRKh__ok)cout<<"TRKH ";
2665     // if(CAL1__ok)cout<<"CAL1 ";
2666     // if(CAL2__ok)cout<<"CAL2 ";
2667     // if(TOF__ok)cout<<"TOF ";
2668     // if(TRG__ok)cout<<"TRG ";
2669     // if(AC__ok)cout<<"AC ";
2670     // if(ND__ok)cout<<"ND ";
2671     // if(S4__ok)cout<<"S4 ";
2672     // if(ORB__ok)cout<<"ORB ";
2673     // cout << endl;
2674 pam-fi 1.8
2675    
2676 pam-fi 1.13 if(TRK2 && TRK1__ok)TRK1=1;
2677     // ----------------------------------------------------------------------------
2678     // NOTA
2679     // se c'e` il level1, lo devo necessarimente leggere.
2680     // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
2681     // comunque, ma non vengono disallocati da PamLevel2::Clear()
2682     // ----------------------------------------------------------------------------
2683    
2684    
2685 pam-fi 1.6 if(!RUN__ok) {
2686 pam-fi 1.9 cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree"<< endl;
2687     // return false;
2688 pam-fi 1.8 };
2689    
2690     if(CAL1 && !CAL1__ok){
2691     cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel1 branch"<< endl;
2692     return false;
2693     };
2694     if(CAL2 && !CAL2__ok){
2695     cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel2 branch"<< endl;
2696     return false;
2697     };
2698     if(TRK2 && !TRK2__ok){
2699     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel2 branch"<< endl;
2700     return false;
2701     };
2702     if(TRK1 && !TRK1__ok){
2703     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel1 branch"<< endl;
2704     return false;
2705     };
2706     if(TRKh && !TRKh__ok){
2707     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkHough branch"<< endl;
2708     return false;
2709 pam-fi 1.6 };
2710 pam-fi 1.8 if(ORB && !ORB__ok){
2711     cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl;
2712     return false;
2713 pam-fi 1.6 };
2714 pam-fi 1.8 if(AC && !AC__ok){
2715     cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl;
2716     return false;
2717 pam-fi 1.6 };
2718     if(S4 && !S4__ok){
2719 pam-fi 1.8 cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl;
2720     return false;
2721 pam-fi 1.6 };
2722 pam-fi 1.8 if(TOF && !TOF__ok){
2723     cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl;
2724     return false;
2725 pam-fi 1.6 };
2726 pam-fi 1.8
2727 pam-fi 1.12 if(ND && !ND__ok){
2728     cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;
2729     return false;
2730     };
2731     if(TRG && !TRG__ok){
2732     cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
2733     return false;
2734     };
2735    
2736 pam-fi 1.8
2737 pam-fi 1.13 // lk->Delete();
2738     // delete lk;
2739 pam-fi 1.8 f->Close();
2740 pam-fi 1.12
2741     // cout<< "CheckLevel2File(TString): detector list --> ";
2742     // if(TRK1)cout<<"TRK1 ";
2743     // if(TRK2)cout<<"TRK2 ";
2744     // if(TRKh)cout<<"TRKH ";
2745     // if(CAL1)cout<<"CAL1 ";
2746     // if(CAL2)cout<<"CAL2 ";
2747     // if(TOF)cout<<"TOF ";
2748     // if(TRG)cout<<"TRG ";
2749     // if(AC)cout<<"AC ";
2750     // if(ND)cout<<"ND ";
2751     // if(S4)cout<<"S4 ";
2752     // if(ORB)cout<<"ORB ";
2753     // cout << endl;
2754 pam-fi 1.8
2755     return true;
2756 pam-fi 1.2
2757     };
2758    
2759    
2760 pam-fi 1.9 /**
2761     * Create clone-trees
2762     */
2763 pam-fi 1.11 void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){
2764 pam-fi 1.9
2765     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2766     cout << "Create clones of PAMELA trees "<<endl;
2767    
2768     Int_t i=0;
2769 pam-fi 1.20 pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
2770     TString name = pam_tree_clone[i]->GetName();
2771 pam-fi 1.9 name.Append("_clone");
2772 pam-fi 1.20 // pam_tree_clone[i]->SetName(name.Data());
2773     cout << pam_tree_clone[i]->GetName() <<endl;
2774 pam-fi 1.9 i++;
2775    
2776     TList *li = fChain->GetListOfFriends();
2777     TIter next(li);
2778     TFriendElement* T_friend=0;
2779 pam-fi 1.11 ofile->cd();
2780 pam-fi 1.9 while( (T_friend = (TFriendElement*)next()) ){
2781     // cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
2782     // cout<<T_friend->GetTree()->GetName()<< endl;
2783 pam-fi 1.20 pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
2784     pam_tree_clone[i]->SetAutoSave(1000000);
2785     name = pam_tree_clone[i]->GetName();
2786 pam-fi 1.9 name.Append("_clone");
2787 pam-fi 1.20 // pam_tree_clone[i]->SetName(name.Data());
2788     cout << pam_tree_clone[i]->GetName() << endl;
2789 pam-fi 1.9 i++;
2790     }
2791 pam-fi 1.13
2792     delete li;
2793 pam-fi 1.9
2794     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2795    
2796     }
2797    
2798 pam-fi 1.11 /**
2799     * Create clone-trees
2800     */
2801     void PamLevel2::CreateCloneTrees(TFile *ofile){
2802    
2803 pam-fi 1.39
2804     // if the pointer is null, create a default file
2805    
2806     if(!ofile){
2807     cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
2808     ofile = new TFile("clone-tree.root","recreate");
2809     }
2810    
2811 pam-fi 1.11 ofile->cd();
2812 pam-fi 1.9
2813 pam-fi 1.11 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2814     cout << "Create new PAMELA trees "<<endl;
2815 pam-fi 1.9
2816 pam-fi 1.20
2817     run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
2818     run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
2819     cout << "Run : branch RunInfo"<<endl;
2820     run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
2821     cout << "Run : branch SoftInfo"<<endl;
2822 pam-fi 1.36 // ------------------
2823     // replicate run tree
2824     // ------------------
2825     cout << "----------------------------------------------------"<<endl;
2826     cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
2827     for (Int_t i=0; i<run_tree->GetEntries(); i++){
2828     run_tree->GetEntry(i);
2829     cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
2830     run_tree_clone->Fill();
2831     }
2832     cout << "----------------------------------------------------"<<endl;
2833 pam-fi 1.20
2834    
2835     sel_tree_clone = new TTree("SelectionList","List of selected events ");
2836 pam-fi 1.34 sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
2837     sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
2838 pam-fi 1.20
2839    
2840 pam-fi 1.11 Int_t i=0;
2841     if(TRK1||TRK2||TRKh){
2842 pam-fi 1.20 pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
2843 pam-fi 1.11 if(TRK1) {
2844 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
2845     pam_tree_clone[i]->BranchRef();
2846 pam-fi 1.11 cout << "Tracker : branch TrkLevel1"<<endl;
2847 pam-fi 1.36 // cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
2848 pam-fi 1.11 };
2849     if(TRK2) {
2850 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
2851 pam-fi 1.11 cout << "Tracker : branch TrkLevel2"<<endl;
2852     };
2853     if(TRKh) {
2854 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
2855 pam-fi 1.11 cout << "Tracker : branch TrkHough"<<endl;
2856     };
2857     i++;
2858     }
2859 pam-fi 1.9
2860 pam-fi 1.11 // Calorimeter
2861     if(CAL1||CAL2){
2862 pam-fi 1.20 pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
2863 pam-fi 1.11 if(CAL1) {
2864 pam-fi 1.20 pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
2865 pam-fi 1.11 cout << "Calorimeter : branch CaloLevel1"<<endl;
2866     };
2867     if(CAL2) {
2868 pam-fi 1.20 pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
2869 pam-fi 1.11 cout << "Calorimeter : branch CaloLevel2"<<endl;
2870     };
2871     i++;
2872     }
2873 pam-fi 1.9
2874 pam-fi 1.11 // ToF
2875     if(TOF) {
2876 pam-fi 1.20 pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
2877     pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
2878 pam-fi 1.11 cout << "ToF : branch ToFLevel2"<<endl;
2879     i++;
2880     };
2881     // Trigger
2882     if(TRG) {
2883 pam-fi 1.20 pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
2884     pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
2885 pam-fi 1.11 cout << "Trigger : branch TrigLevel2"<<endl;
2886     i++;
2887     };
2888     // S4
2889     if(S4) {
2890 pam-fi 1.20 pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");
2891     pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
2892 pam-fi 1.11 cout << "S4 : branch S4Level2"<<endl;
2893     i++;
2894     };
2895     // Neutron Detector
2896     if(ND) {
2897 pam-fi 1.20 pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");
2898     pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
2899 pam-fi 1.11 cout << "NeutronD : branch NDLevel2"<<endl;
2900     i++;
2901     };
2902     // Anticounters
2903     if(AC) {
2904 pam-fi 1.20 pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");
2905     pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
2906 pam-fi 1.11 cout << "Anticounter : branch AcLevel2"<<endl;
2907     i++;
2908     };
2909     // OrbitalInfo
2910     if(ORB) {
2911 pam-fi 1.20 pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info ");
2912     pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
2913 pam-fi 1.11 cout << "OrbitalInfo : branch OrbitalInfo"<<endl;
2914     i++;
2915     };
2916     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2917 pam-fi 1.9
2918 pam-fi 1.11 }
2919 pam-fi 1.9
2920    
2921     /**
2922     * Fill tree (created with CreateCloneTrees)
2923     *
2924     */
2925     //void PamLevel2::FillNewPamTree(TTree *T){
2926     void PamLevel2::FillCloneTrees(){
2927 pam-fi 1.20
2928     // cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
2929 pam-fi 1.24
2930 pam-fi 1.20 for(Int_t i=0; i<NCLONES; i++){
2931     if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
2932     }
2933     if(sel_tree_clone)sel_tree_clone->Fill();
2934 pam-fi 1.21
2935 pam-fi 1.9 }
2936    
2937    
2938     TTree* PamLevel2::GetCloneTree(TString name){
2939    
2940 pam-fi 1.20 for(Int_t i=0; i<NCLONES; i++){
2941     if(pam_tree_clone[i]){
2942     TString na = pam_tree_clone[i]->GetName();
2943     if(!name.CompareTo(na))return pam_tree_clone[i];
2944 pam-fi 1.9 };
2945     }
2946 pam-fi 1.20 if(run_tree_clone){
2947     TString na = run_tree_clone->GetName();
2948     if(!name.CompareTo(na))return run_tree_clone;
2949     }
2950     if(sel_tree_clone){
2951     TString na = sel_tree_clone->GetName();
2952     if(!name.CompareTo(na))return sel_tree_clone;
2953     }
2954 pam-fi 1.9 return NULL;
2955    
2956     }
2957     void PamLevel2::WriteCloneTrees(){
2958     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2959     cout << "Write clones of PAMELA trees "<<endl;
2960 pam-fi 1.20 cout << run_tree_clone->GetName()<<endl;
2961     run_tree_clone->Write();
2962     cout << sel_tree_clone->GetName()<<endl;
2963     sel_tree_clone->Write();
2964     for(Int_t i=0; i<NCLONES; i++){
2965     if(pam_tree_clone[i]){
2966     cout << pam_tree_clone[i]->GetName()<<endl;
2967     pam_tree_clone[i]->Write();
2968 pam-fi 1.9 };
2969     }
2970     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2971    
2972     }
2973    
2974     /**
2975 pam-fi 1.24 * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
2976 pam-fi 1.9 */
2977 pam-fi 1.33 //Int_t PamLevel2::GetEntry(Int_t iee){
2978     Int_t PamLevel2::GetEntry(Long64_t iee){
2979 pam-fi 1.9
2980 pam-fi 1.39 // cout << "-------------------------------------"<<endl;
2981     // cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
2982    
2983 pam-fi 1.20 if(!pam_tree){
2984 mocchiut 1.28 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
2985 pam-fi 1.20 return 0;
2986     }
2987 mocchiut 1.28
2988    
2989     //
2990     // 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...
2991     //
2992     // if(!run_tree ){
2993     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
2994     // return 0;
2995     // }
2996 pam-fi 1.20
2997 pam-fi 1.33 Long64_t ii=0;
2998 pam-fi 1.20 //-------------------------------
2999     ii = iee;
3000 pam-fi 1.33 if( !pam_tree->GetEntry(ii) ){
3001     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
3002     return 0;
3003     }
3004 mocchiut 1.28 //
3005     // ... 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.
3006     // 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
3007     // a problem if you don't check the return code of getentry.
3008     //
3009     if(!run_tree ){
3010 mocchiut 1.29 if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3011 pam-fi 1.36 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3012     return 0;
3013 mocchiut 1.29 } else {
3014 pam-fi 1.36 return 1; //cosi` se non c'e` run esce qua...
3015 mocchiut 1.29 }
3016 mocchiut 1.28 }
3017    
3018 pam-fi 1.20 //-------------------------------
3019     ii = iee;
3020 pam-fi 1.33 // Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3021 pam-fi 1.36 // Bool_t UPDATED = UpdateRunInfo(ii);
3022     UpdateRunInfo(ii);
3023 pam-fi 1.20 if(SELLI==0)irunentry = iee-runfirstentry;
3024 pam-fi 1.36 // if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3025    
3026 pam-fi 1.20 // cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3027    
3028 pam-fi 1.39 // cout << "irunentry "<<irunentry << endl;
3029     // cout << "runfirstentry "<<runfirstentry << endl;
3030     // cout << "nevents "<<GetRunInfo()->NEVENTS<< endl;
3031 pam-fi 1.40
3032     // if( TRK0 || CAL0 || TOF0 ){
3033     // if( !GetYodaEntry( ) ){
3034     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3035     // return 0;
3036     // }
3037     // }
3038    
3039    
3040 pam-fi 1.20 return 1;
3041    
3042     }
3043    
3044 pam-fi 1.40 TrkLevel0 *PamLevel2::GetTrkLevel0(){
3045     if( !TRK0 )return NULL;
3046     if( !GetYodaEntry( ) ){
3047     cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3048     return 0;
3049     }
3050     return trk0_obj;
3051     };
3052     CaloLevel0 *PamLevel2::GetCaloLevel0(){
3053     if( !CAL0 )return NULL;
3054     if( !GetYodaEntry( ) ){
3055     cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3056     return 0;
3057     }
3058     return calo0_obj;
3059     };
3060    
3061    
3062 pam-fi 1.20 /**
3063     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3064     * Given the run ID (...), if needed it query the DB and load the proper file.
3065     * @return Pointer to the tree
3066     */
3067    
3068    
3069     TTree* PamLevel2::GetYodaTree( ){
3070    
3071     // cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
3072     //===================================
3073     // check if iroot has changed
3074     //===================================
3075     if( irun<0 ){
3076 pam-fi 1.34 cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3077     // cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
3078 pam-fi 1.20 return NULL;
3079     }
3080     Int_t irootnew = run_obj->ID_ROOT_L0;
3081 pam-fi 1.39 // cout << "iroot "<<iroot<<endl;
3082     // cout << "irootnew "<<irootnew<<endl;
3083 pam-fi 1.20
3084     //===================================
3085     // load the level0 file
3086     // (if not already loaded)
3087     //===================================
3088     if( iroot != irootnew || !l0_tree){
3089     iroot = irootnew;
3090     //===================================
3091     // open the DB connection
3092     // (if not already opened)
3093     //===================================
3094     if(!dbc || (dbc && !dbc->IsConnected())){
3095     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3096     cout<<"Connecting to DB"<<endl;
3097     cout<<"HOST "<<host<<endl;
3098     cout<<"USER "<<user<<endl;
3099     cout<<"PSW "<<psw<<endl;
3100     dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3101     if( !dbc )return NULL;
3102     if( !dbc->IsConnected() )return NULL;
3103     // cout<<"...done"<<endl;
3104     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3105     }else{
3106     // cout<<"DB already connected"<<endl;
3107     }
3108     GL_ROOT glroot = GL_ROOT();
3109 pam-fi 1.24 if( glroot.Query_GL_ROOT(iroot,dbc) ){
3110     cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3111     return NULL;
3112     };
3113 pam-fi 1.20 TString filename = glroot.PATH + glroot.NAME;
3114     if(l0_file){
3115     l0_file->Close();
3116     l0_file->Delete();
3117     }
3118     cout << "Opening LEVEL0 file: "<< filename << endl;
3119     FileStat_t t;
3120     if( gSystem->GetPathInfo(filename.Data(),t) ){
3121     cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
3122     return NULL;
3123     }
3124     l0_file = new TFile(filename);
3125     if( !l0_file )return NULL;
3126     l0_tree = (TTree*)l0_file->Get("Physics");
3127     if(!h0_obj)h0_obj = new EventHeader();
3128     l0_tree->SetBranchAddress("Header" ,&h0_obj);
3129 mocchiut 1.30 prevshift = 0;
3130 pam-fi 1.20 //---------------------------------------------------
3131     // TRACKER:
3132     if(TRK0){
3133     if(!trk0_obj){
3134     trk0_obj = new TrkLevel0();
3135     trk0_obj->Set();
3136     };
3137     l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
3138     }
3139 pam-fi 1.40 //--------------------------------------------------
3140 pam-fi 1.20 // CALORIMETER:
3141     if(CAL0){
3142 mocchiut 1.30 if(!calo0_obj){
3143     calo0_obj = new CaloLevel0();
3144     calo0_obj->Set();
3145     };
3146     l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3147     // cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3148 pam-fi 1.20 }
3149     //---------------------------------------------------
3150     // TOF:
3151     if(TOF0){
3152     cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3153     }
3154    
3155     };
3156    
3157 pam-fi 1.26 if(!dbc || (dbc && !dbc->IsConnected())){
3158     cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3159     }
3160    
3161 pam-fi 1.33 if ( TRK0 ){
3162     TrkParams::Load(6);
3163     if( !TrkParams::IsLoaded(6) ){
3164     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3165     };
3166     TrkParams::SetCalib(run_obj,dbc);
3167     TrkParams::LoadCalib( );
3168     if( !TrkParams::CalibIsLoaded() ){
3169     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3170     };
3171     }
3172 pam-fi 1.26
3173 pam-fi 1.24 // cout << l0_tree << endl;
3174    
3175 pam-fi 1.20 return l0_tree;
3176 pam-fi 1.9
3177 pam-fi 1.20 }
3178 pam-fi 1.9
3179 pam-fi 1.20 /**
3180     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3181     */
3182     Int_t PamLevel2::GetYodaEntry(){
3183 pam-fi 1.9
3184 pam-fi 1.20 // cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3185     if(!GetYodaTree())return 0;
3186    
3187 pam-fi 1.27 // patch
3188 pam-fi 1.33 if( irunentry < 0){
3189 pam-fi 1.36 // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3190 pam-fi 1.33 irunentry=0LL;
3191 pam-fi 1.27 }
3192 pam-fi 1.20 // ---------------------------------
3193     // if file is NOT a preselected file
3194     // ---------------------------------
3195 pam-fi 1.33 Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3196 pam-fi 1.36 // cout << " irunentry "<<irunentry<<endl;
3197     // cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3198     // cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3199 pam-fi 1.24
3200 pam-fi 1.26 // cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3201     // cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3202 pam-fi 1.43 // cout << " time "<< abstime << endl;
3203 pam-fi 1.26 // cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3204    
3205 pam-fi 1.43 ULong64_t obt = 0;
3206     ULong64_t pktn = 0;
3207     if( GetOrbitalInfo() ){
3208     obt = GetOrbitalInfo()->OBT;
3209     pktn = GetOrbitalInfo()->pkt_num;
3210     }
3211    
3212     if( !GetOrbitalInfo() && !ISGP){
3213 pam-fi 1.24 cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3214     return 0;
3215     }
3216 pam-fi 1.43 if( obt==0 && pktn==0 && !ISGP ){
3217 pam-fi 1.34 cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3218     return 0;
3219     }
3220 pam-fi 1.24
3221     // ---------------------------------------------------------------------
3222     // ATTENTION!!!
3223     // If data are not pre-processed with cleaner, the level0 tree may contain
3224     // spurious nested physics packets.
3225     // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3226     // while level2 tree DOES NOT!!
3227     // This means that "quellagiusta" in these cases is not correct.
3228     // In order to retrieve the correct level0 event, I implemented a check
3229     // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3230     // is shift forward until when the packets match.
3231     // ---------------------------------------------------------------------
3232     Int_t answer = 0;
3233     Int_t shift =0;
3234 mocchiut 1.31 // printf(" siamo qui %i %i \n",shift,prevshift);
3235 pam-fi 1.39 Int_t maxshift = 10;
3236 pam-fi 1.24 do{
3237 mocchiut 1.30 if(shift>0){
3238 pam-fi 1.43 cout << " PKTNUM L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3239 pam-fi 1.36 cout << " RUN: ID "<< GetRunInfo()->ID << " ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<" ID_RUN_FRAG "<<GetRunInfo()->ID_RUN_FRAG << " EV_FROM "<<GetRunInfo()->EV_FROM <<endl;
3240     cout << " L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3241 pam-fi 1.26 }
3242 pam-fi 1.33 answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3243 pam-fi 1.24 shift++;
3244     if( !GetEventHeader() ){
3245     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3246     return 0;
3247     }
3248 pam-fi 1.43
3249     if(ISGP){
3250     obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
3251     pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
3252     }
3253    
3254     // cout << "PKTNUM "<<shift<<" == L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3255     // cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3256 pam-fi 1.26 // if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3257 pam-fi 1.43 // cout << " obt "<< obt << endl;
3258 pam-fi 1.26 // cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3259 pam-fi 1.43 // cout << " pktn "<< pktn << endl;
3260 pam-fi 1.26 // cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3261 mocchiut 1.31 // printf(" IDRUN %u \n",GetRunInfo()->ID);
3262     //
3263 pam-fi 1.33 if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3264 mocchiut 1.30 prevshift = 0;
3265     shift = -1;
3266     };
3267 pam-fi 1.26
3268 pam-fi 1.43 }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
3269     pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) )
3270     && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
3271 mocchiut 1.30
3272 pam-fi 1.39 if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3273 pam-fi 1.36 cout << " Big trouble here, no such event in Level0 data! " <<endl;
3274     return 0;
3275     }
3276 pam-fi 1.20 // cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3277 pam-fi 1.24 // return GetYodaTree()->GetEntry(quellagiusta);
3278 mocchiut 1.30 if ( shift > 1 ) prevshift += (shift-1);
3279 pam-fi 1.20
3280 pam-fi 1.24 return answer;
3281 pam-fi 1.20
3282     }
3283 pam-fi 1.40 /**
3284     * \Brief Set DB connection
3285     */
3286     Bool_t PamLevel2::SetDBConnection(){
3287 mocchiut 1.30
3288 pam-fi 1.40 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3289     cout<<"Connecting to DB"<<endl;
3290     cout<<"HOST "<<host<<endl;
3291     cout<<"USER "<<user<<endl;
3292     cout<<"PSW "<<psw<<endl;
3293     dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3294     if( !dbc )return false;
3295     if( !dbc->IsConnected() )return false;
3296     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3297     return true;
3298 mocchiut 1.30
3299 pam-fi 1.40 }

  ViewVC Help
Powered by ViewVC 1.1.23