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

Diff of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.46

  ViewVC Help
Powered by ViewVC 1.1.23