/[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.18 by pam-fi, Thu Jan 18 08:51:16 2007 UTC revision 1.42 by pam-fi, Tue Aug 7 15:37:12 2007 UTC
# Line 85  void PamTrack::Delete(){ Line 85  void PamTrack::Delete(){
85  //  //
86  //--------------------------------------  //--------------------------------------
87  /**  /**
88   * Constructor   * Default Constructor
89   */   */
90  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
91      Initialize();
92    };
93    
94    /**
95     * Constructor with given dir, list and detectors
96     */
97    PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
98      Initialize();
99      GetPamTree(GetListOfLevel2Files(ddir,list),detlist);
100      GetRunTree(GetListOfLevel2Files(ddir,list));
101    };
102    
103    /**
104     * Constructor with given dir and list
105     */
106    PamLevel2::PamLevel2(TString ddir,TString list){
107      Initialize();
108      GetPamTree(GetListOfLevel2Files(ddir,list),"");
109      GetRunTree(GetListOfLevel2Files(ddir,list));
110    };
111    
112    
113    void PamLevel2::Initialize(){
114                    
115  //     trk2_obj  = TrkLevel2::GetTrkLevel2();  //     trk2_obj  = TrkLevel2::GetTrkLevel2();
116  //     trk1_obj  = TrkLevel1::GetTrkLevel1();  //     trk1_obj  = TrkLevel1::GetTrkLevel1();
# Line 100  PamLevel2::PamLevel2(){ Line 123  PamLevel2::PamLevel2(){
123  //     nd_obj    = NDLevel2::GetNDLevel2();  //     nd_obj    = NDLevel2::GetNDLevel2();
124  //     ac_obj    = AcLevel2::GetAcLevel2();  //     ac_obj    = AcLevel2::GetAcLevel2();
125  //     orb_obj   = OrbitalInfo::GetOrbitalInfo();  //     orb_obj   = OrbitalInfo::GetOrbitalInfo();
126        
127        h0_obj    = 0;
128        trk0_obj  = 0;
129        calo0_obj  = 0;
130    
131      trk2_obj  = 0;      trk2_obj  = 0;
132      trk1_obj  = 0;      trk1_obj  = 0;
133      trkh_obj  = 0;      trkh_obj  = 0;
# Line 114  PamLevel2::PamLevel2(){ Line 142  PamLevel2::PamLevel2(){
142    
143      run_obj   = 0;//new GL_RUN();      run_obj   = 0;//new GL_RUN();
144      soft_obj   = 0;// Emiliano      soft_obj   = 0;// Emiliano
145      irun = -1;      irun = -1LL;
146      runfirstentry = 0ULL;      runfirstentry = 0LL;
147      runlastentry = 0ULL;      runlastentry = 0LL;
148    
149        l0_file = NULL;
150        l0_tree = NULL;
151        iroot   = -1;
152        dbc     = 0;
153    
154        prevshift = 0;
155        
156        run_tree = NULL;
157        run_tree_clone = NULL;
158        sel_tree = NULL;
159        sel_tree_clone = NULL;
160        
161        irunentry = -1LL;
162        pam_tree = NULL;
163        for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
164    
165        host = "mysql://localhost/pamelaprod";
166        user = "anonymous";
167        psw = "";
168        const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
169        const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
170        const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
171        if ( !pamdbhost ) pamdbhost = "";
172        if ( !pamdbuser ) pamdbuser = "";
173        if ( !pamdbpsw ) pamdbpsw = "";
174        if ( strcmp(pamdbhost,"") ) host = pamdbhost;
175        if ( strcmp(pamdbuser,"") ) user = pamdbuser;
176        if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
177    
178      for(Int_t i=0; i<8; i++ )tree_clone[i]=NULL;      
179    
180  //    sorted_tracks = 0;//new TRefArray();  //    sorted_tracks = 0;//new TRefArray();
181            
182        CAL0 = false;
183      CAL1 = true;      CAL1 = true;
184      CAL2 = true;      CAL2 = true;
185      TRK2 = true;      TRK2 = true;
186      TRK1 = false;      TRK1 = false;
187      TRKh = false;      TRKh = false;
188      TRG = true;      TRKh = false;
189      TOF = true;      TRG  = true;
190      S4  = true;      TOF  = true;
191      ND  = true;      TOF0 = false;
192      AC  = true;      S4   = true;
193      ORB = true;      ND   = true;
194        AC   = true;
195        ORB  = true;
196            
197      RUN = true;      RUN  = true;
198    
199        SELLI = -1;
200    
201      tsorted=0;      tsorted=0;
202      timage=0;      timage=0;
203        
204        howtosort = "+CAL+TOF";
205        //howtosort = "+TOF";
206        sortthr = 100.;
207    
208  };  };
209  /**  /**
# Line 149  void PamLevel2::Delete(){ Line 215  void PamLevel2::Delete(){
215      if(soft_obj)delete soft_obj; //Emiliano      if(soft_obj)delete soft_obj; //Emiliano
216    
217  //    cout << "void PamLevel2::Clear()"<<endl;  //    cout << "void PamLevel2::Clear()"<<endl;
218        if(h0_obj)   delete h0_obj;
219        if(trk0_obj) delete trk0_obj;      
220        if(calo0_obj) delete calo0_obj;    
221      if(trk1_obj) delete trk1_obj;            if(trk1_obj) delete trk1_obj;      
222      if(trk2_obj) delete trk2_obj;      if(trk2_obj) delete trk2_obj;
223      if(trkh_obj) delete trkh_obj;      if(trkh_obj) delete trkh_obj;
# Line 169  void PamLevel2::Delete(){ Line 238  void PamLevel2::Delete(){
238          timage->Delete();          timage->Delete();
239          delete timage;          delete timage;
240      }      }
241    
242        if(dbc){
243            dbc->Close();
244            delete dbc;
245        }
246        
247        if(l0_file)l0_file->Close();
248        //    if(pam_tree)pam_tree->Delete();;
249    
250      if ( pam_tree ){
251        //
252        // 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...
253        //
254        TList *temp = pam_tree->GetListOfFriends();
255        TList *contents  = new TList; // create chain friend list
256        contents->SetOwner();
257        TIter next(temp);
258        TChain *questo = 0;
259        while ( (questo = (TChain*) next()) ){
260          TString name =  questo->GetName();
261          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
262        };
263        //
264        // deleting the main chain
265        //
266        pam_tree->Delete();
267        //
268        // deleting the friends...
269        //
270        TIter next2(contents);
271        TChain *questa = 0;
272        while ( questa = (TChain*)next2() ){
273          TString name =  questa->GetName();
274          questa->Delete();      
275          questa=NULL;
276        };
277        //
278      };
279      pam_tree = NULL;
280    
281        if(run_tree)run_tree->Delete();;
282        if(sel_tree)sel_tree->Delete();;
283        for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
284        if(run_tree_clone)run_tree_clone->Delete();;
285        if(sel_tree_clone)sel_tree_clone->Delete();;
286        
287    
288  };  };
289    
290  /**  /**
# Line 186  void PamLevel2::Clear(){ Line 302  void PamLevel2::Clear(){
302  //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead  //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
303  //    if(soft_obj) soft_obj->Clear();  //    if(soft_obj) soft_obj->Clear();
304    
305        if(h0_obj)   h0_obj->Clear();      
306    //    if(trk0_obj) trk0_obj->Clear();  
307      if(trk1_obj) trk1_obj->Clear();          if(trk1_obj) trk1_obj->Clear();    
308      if(trk2_obj) trk2_obj->Clear();      if(trk2_obj) trk2_obj->Clear();
309      if(trkh_obj) trkh_obj->Clear();      if(trkh_obj) trkh_obj->Clear();
310        if(calo0_obj) calo0_obj->Clear();  
311      if(calo1_obj)calo1_obj->Clear();      if(calo1_obj)calo1_obj->Clear();
312      if(calo2_obj)calo2_obj->Clear();      if(calo2_obj)calo2_obj->Clear();
313      if(tof_obj)  tof_obj->Clear();      if(tof_obj)  tof_obj->Clear();
# Line 207  void PamLevel2::Clear(){ Line 326  void PamLevel2::Clear(){
326      if(timage){      if(timage){
327          timage->Delete();          timage->Delete();
328      }      }
       
329  };  };
330    
331    void PamLevel2::Reset(){
332      //
333      // First of all clear everything
334      //
335      Clear();
336      //
337      // close and reset chains and pointers
338      //    
339      if ( pam_tree ){
340        //
341        // 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...
342        //
343        TList *temp = pam_tree->GetListOfFriends();
344        TList *contents  = new TList; // create chain friend list
345        contents->SetOwner();
346        TIter next(temp);
347        TChain *questo = 0;
348        while ( (questo = (TChain*) next()) ){
349          TString name =  questo->GetName();
350          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
351        };
352        //
353        // deleting the main chain
354        //
355        pam_tree->Delete();
356        //
357        // deleting the friends...
358        //
359        TIter next2(contents);
360        TChain *questa = 0;
361        while ( questa = (TChain*)next2() ){
362          TString name =  questa->GetName();
363          questa->Delete();      
364          questa=NULL;
365        };
366        //
367      };
368      pam_tree = NULL;
369      //
370      if(run_tree)run_tree->Delete();;
371      run_tree = NULL;
372      if(sel_tree)sel_tree->Delete();;
373      sel_tree = NULL;
374      //
375      // Close file
376      //
377      if(l0_file)l0_file->Close("R");
378      l0_file = NULL;
379      //
380      h0_obj    = 0;
381      trk0_obj  = 0;
382      calo0_obj  = 0;
383      //
384      trk2_obj  = 0;
385      trk1_obj  = 0;
386      trkh_obj  = 0;
387      calo1_obj = 0;
388      calo2_obj = 0;
389      tof_obj   = 0;
390      trig_obj  = 0;
391      s4_obj    = 0;
392      nd_obj    = 0;
393      ac_obj    = 0;
394      orb_obj   = 0;
395      //
396      // Reset run pointers
397      //
398      run_obj   = 0;//new GL_RUN();
399      soft_obj   = 0;// Emiliano
400      irun = -1;
401      runfirstentry = 0ULL;
402      runlastentry = 0ULL;    
403      //
404    };
405    
406    Bool_t PamLevel2::IsGood(){
407      Bool_t goodev=true;
408      //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
409      if(calo2_obj && calo2_obj->good != 1) goodev = false;
410      if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
411      if(trig_obj && trig_obj->unpackError != 0) goodev = false;
412      if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
413      if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
414      if(ac_obj && ac_obj->unpackError != 255) goodev = false;  
415        //  if(orb_obj)  
416      return goodev;
417    };
418    
419  //--------------------------------------  //--------------------------------------
420  //  //
# Line 404  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 609  PamTrack* PamLevel2::GetStoredTrack(Int_
609  }  }
610  //--------------------------------------  //--------------------------------------
611  //  //
612    
613    /**
614     * 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.
615     **/
616    void PamLevel2::SortTracks(TString how){
617      printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
618      howtosort = how;  
619      SortTracks();
620    };
621    
622  //  //
623  //--------------------------------------  //--------------------------------------
624  /**  /**
# Line 417  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 632  PamTrack* PamLevel2::GetStoredTrack(Int_
632   *   *
633   * 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).
634   */   */
635  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
636      TString how = howtosort;
637    
638  //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;    //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;
639      if( !trk2_obj ){    if( !trk2_obj ){
640          cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
641          return;      return;
642      };    };
643  //    cout << "call SortTracs() "<<endl;    //    cout << "call SortTracs() "<<endl;
644    //Save current Object count    //Save current Object count
645      Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
646    
647      //    cout << "ObjectNumber  "<<ObjectNumber <<endl;
648    
649  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    //     if(!sorted_tracks)sorted_tracks = new TRefArray();
650      //     sorted_tracks->Clear();
651      //    sorted_tracks.Clear();
652    
653  //     if(!sorted_tracks)sorted_tracks = new TRefArray();    if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
654  //     sorted_tracks->Clear();    tsorted->Delete();
655  //    sorted_tracks.Clear();    TClonesArray &ttsorted = *tsorted;
656      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
657      timage->Delete();
658      TClonesArray &ttimage = *timage;
659    
     if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
     tsorted->Delete();  
     TClonesArray &ttsorted = *tsorted;  
     if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
     timage->Delete();  
     TClonesArray &ttimage = *timage;  
660    
661    
662      // loop over the tracks sorted by the tracker    //--------------------------------------------------
663      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);    // retrieve sorting method
664      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);    //--------------------------------------------------
665      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);    Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
666      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
667      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
668            
669      if( !CAL2 &&  use_CAL) use_CAL = false;    if( !CAL2 &&  use_CAL) use_CAL = false;
670      if( !TOF &&  use_TOF) use_TOF = false;    if( !TOF &&  use_TOF) use_TOF = false;
671            
672      if( !TRK2 ){    if( !TRK2 ){
673  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;      //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
674          return;      return;
675      };    };
676    
677      //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
678          
679      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){  
680              //--------------------------------------------------
681          TrkTrack   *ts = 0;    // loop over "physical" tracks sorted by the tracker
682          CaloTrkVar *cs = 0;    //--------------------------------------------------
683          ToFTrkVar  *os = 0;    for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
684                    
685          // get tracker tracks      TrkTrack   *ts = 0;
686          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker      CaloTrkVar *cs = 0;
687          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());      ToFTrkVar  *os = 0;
688          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());          
689        // get tracker tracks
690          TrkTrack   *ti = 0;              //tracker (image)      TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
691          CaloTrkVar *ci = 0;      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
692          ToFTrkVar  *oi = 0;      ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
693  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;  
694          // if track has an image, check image selection      TrkTrack   *ti = 0; //tracker (image)
695          if(tp->HasImage()){      CaloTrkVar *ci = 0;
696        ToFTrkVar  *oi = 0;
697        //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
698        // if track has an image, check image selection
699        if(tp->HasImage()){
700                            
701              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
702              ci = GetCaloStoredTrack(ti->GetSeqNo());        ci = GetCaloStoredTrack(ti->GetSeqNo());
703              oi = GetToFStoredTrack(ti->GetSeqNo());        oi = GetToFStoredTrack(ti->GetSeqNo());
704                            
705  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
706    
707              //assign starting scores        //assign starting scores
708              Int_t tp_score = 0;  //main track sorted by the tracker        Int_t tp_score = 0;  //main track sorted by the tracker
709              Int_t ti_score = 0;  //image track        Int_t ti_score = 0;  //image track
710                            
711              // ------------------------        // -----------------------------------------------------------------------------------------
712              // calorimeter check        // calorimeter check
713              // ------------------------        // -----------------------------------------------------------------------------------------
714              // check the Y spatial residual on the first calorimeter plane        // check the Y spatial residual on the first calorimeter plane
715              // (cut on calorimeter variables from Emiliano)        // (cut on calorimeter variables from Emiliano)
716              if( use_CAL && !calo2_obj ){        if( use_CAL && !calo2_obj ){
717                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
718                  return;          return;
719              };        };
720              if(        if(
721                  use_CAL            &&           use_CAL            &&
722                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view  //       calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view
723                  calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view  //       calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view
724                  true){           cp && ci &&
725             true){
726    
727                                    
728                  Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;  //      Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;
729                  Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;  //      Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;
730                                    
731                  if(resy_p <= resy_i) tp_score++;  //      if(resy_p <= resy_i) tp_score++;
732                  else                 ti_score++;  //      else                 ti_score++;
733            
734    
735  //              cout << "CALO "<<tp_score<<ti_score<<endl;            if( cp->npresh > ci->npresh) tp_score++;
736              if( cp->npresh < ci->npresh) ti_score++;
737              else ;//niente
738    
739            //      cout << "CALO "<<tp_score<<ti_score<<endl;
740    
741          };
742          // -----------------------------------------------------------------------------------------
743          // TOF check
744          // -----------------------------------------------------------------------------------------
745          // check the number of hit pmts along the track
746          // on S12 S21 and S32, where paddles are parallel to Y axis
747          if( use_TOF && !tof_obj ){
748            cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
749            return;
750          };
751          if( use_TOF && op && oi ){
752                    
753            //
754            Float_t sen = 0.;
755            for (Int_t ih=0; ih < op->npmtadc; ih++){
756              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
757              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
758            };
759            for (Int_t ih=0; ih < oi->npmtadc; ih++){
760              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
761              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
762            };
763            //
764            if (  sen >= sortthr ){
765              //printf(" IS A NUCLEUS! en = %f \n",sen);
766              //
767              // is a nucleus use a different algorithm
768              //
769              Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
770              for(Int_t ip=0; ip<nz; ip++)
771                zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
772              Trajectory *tr = new Trajectory(nz,zin);
773              //
774              Int_t nphit_p =0;
775              Int_t nphit_i =0;
776              Float_t enhit_p = 0.;
777              Float_t enhit_i = 0.;
778              //
779              for (Int_t ih=0; ih < op->npmtadc; ih++){
780                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
781                if(pl == 1 || pl == 2 || pl == 5){
782                  nphit_p++;
783                  enhit_p += (op->dedx).At(ih);
784                };
785              };
786              //
787              tp->DoTrack2(tr);
788              //
789              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
790                for (Int_t ih=0; ih < op->npmtadc; ih++){
791                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
792                  if(pl == 0){
793                    nphit_p++;
794                    enhit_p += (op->dedx).At(ih);
795                  };
796                };
797              };
798              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
799                for (Int_t ih=0; ih < op->npmtadc; ih++){
800                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
801                  if(pl == 3){
802                    nphit_p++;
803                    enhit_p += (op->dedx).At(ih);
804                  };
805                };
806              };
807              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
808                for (Int_t ih=0; ih < op->npmtadc; ih++){
809                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
810                  if(pl == 4){
811                    nphit_p++;
812                    enhit_p += (op->dedx).At(ih);
813                  };
814                };
815              };
816    
817              for (Int_t ih=0; ih < oi->npmtadc; ih++){
818                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
819                if(pl == 1 || pl == 2 || pl == 5){
820                  nphit_i++;        
821                  enhit_i += (op->dedx).At(ih);
822                };
823              };
824              //
825              ti->DoTrack2(tr);
826              //
827              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
828                for (Int_t ih=0; ih < oi->npmtadc; ih++){
829                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
830                  if(pl == 0){
831                    nphit_i++;
832                    enhit_i += (op->dedx).At(ih);
833                  };
834                };
835              };
836              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
837                for (Int_t ih=0; ih < oi->npmtadc; ih++){
838                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
839                  if(pl == 3){
840                    nphit_i++;
841                    enhit_i += (op->dedx).At(ih);
842                  };
843                };
844              };
845              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
846                for (Int_t ih=0; ih < oi->npmtadc; ih++){
847                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
848                  if(pl == 4){
849                    nphit_i++;
850                    enhit_i += (op->dedx).At(ih);
851                  };
852              };              };
853              // ------------------------            };            
854              // TOF check  
855              // ------------------------                      
856              // check the number of hit pmts along the track            if(
857              // on S12 S21 and S32, where paddles are parallel to Y axis               use_TOF            &&
858              if( use_TOF && !tof_obj ){               (nphit_p+nphit_i) !=0 &&  
859                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";               true){
860                  return;                      
861                //      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);
862                //      printf(" score p %i score i %i \n",tp_score,ti_score);
863                //            if( enhit_p > enhit_i ) tp_score++;
864                //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
865                if ( nphit_p > nphit_i ) tp_score++;
866                if ( nphit_p < nphit_i ) ti_score++;
867                if ( nphit_p == nphit_i ){
868                  if ( enhit_p > enhit_i ) tp_score++;
869                  else ti_score++;
870              };              };
871              if( use_TOF ){              //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
872              };
873              delete tr;
874              //
875            } else {
876              //
877              // NOT a NUCLEUS
878              //
879              //printf(" NOT a NUCLEUS! en = %f \n",sen);
880    
881              Int_t nphit_p =0;
882              Int_t nphit_i =0;
883                                    
                 Int_t nphit_p =0;  
                 Int_t nphit_i =0;  
884                                    
885              /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
886                cout << "track: npmttdc "<< op->npmttdc << endl;
887                cout << "image: npmtadc "<< oi->npmtadc << endl;
888                cout << "image: npmttdc "<< oi->npmttdc << endl;*/
889                                    
890  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;  //        for (Int_t ih=0; ih < op->npmtadc; ih++){
891                                  cout << "track: npmttdc "<< op->npmttdc << endl;  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
892                                  cout << "image: npmtadc "<< oi->npmtadc << endl;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
893                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/  //        };
894                                    
895                  for (Int_t ih=0; ih < op->npmtadc; ih++){  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
896                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
897                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
898                  };  //        };
899              // --- modified to count tdc signals (more efficient?)
900              // --- and to implement check on tdcflag
901              for (Int_t ih=0; ih < op->npmttdc; ih++){
902                Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
903    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
904                if( (op->tdcflag).At(ih)==0 )nphit_p++;
905              };
906                                    
907                  for (Int_t ih=0; ih < oi->npmtadc; ih++){            for (Int_t ih=0; ih < oi->npmttdc; ih++){
908                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
909                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
910                  };              if( (oi->tdcflag).At(ih)==0 )nphit_i++;    
911              };
912                                    
913                  if(            if(
914                      use_TOF            &&               (nphit_p+nphit_i) !=0 &&  
915                      (nphit_p+nphit_i) !=0 &&                   true){
                     true){  
916                                            
917                      if( nphit_p >= nphit_i) tp_score++;              if     ( nphit_p > nphit_i) tp_score++;
918                      else ti_score++;              else if( nphit_p < nphit_i) ti_score++;
919                  };              else ;//niente
920  //              cout << "TOF "<<tp_score<<ti_score<<endl;            };
921              };          };
922              if(tp_score == ti_score) use_TRK = true;          //      cout << "TOF "<<tp_score<<ti_score<<endl;
923              // ------------------------        };
924              // tracker check  
925              // ------------------------  
926              // chi**2 difference is not always large enough to distinguish among  //      if(tp_score == ti_score) use_TRK = true;
927              // the real track and its image.  
928              // Tracker check will be applied always when calorimeter and tof information is ambiguous.  
929              if(use_TRK){        // -----------------------------------------------------------------------------------------
930                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;        // tracker check
931                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;        // -----------------------------------------------------------------------------------------
932  //              cout << "TRK "<<tp_score<<ti_score<<endl;        // chi**2 difference is not always large enough to distinguish among
933              };        // the real track and its image.
934          // Tracker check will be applied always when calorimeter and tof information is ambiguous.
935          // *** MODIFIED ON AUGUST 2007 ***
936          if(use_TRK){
937    //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
938    //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
939    
940              // CHECK 1 : number of points along X  
941              if ( tp->GetNX() >= ti->GetNX() )tp_score++ ;
942              if ( tp->GetNX() <= ti->GetNX() )ti_score++ ;
943              // CHECK 2 : number of points along Y  
944              if ( tp->GetNY() >= ti->GetNY() )tp_score++ ;
945              if ( tp->GetNY() <= ti->GetNY() )ti_score++ ;
946              // CHECK 3 : chi**2 along X  
947    //        if(      tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;
948    //        if(      ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;
949              // CHECK 4 : chi**2 along Y  
950    //        if(      tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;
951    //        if(      ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;
952              // CHECK 5 : total chi**2  
953    //        if(      tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;
954    //        if(      ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;
955    
956            //      cout << "TRK "<<tp_score<<ti_score<<endl;
957          };
958                            
959              // ------------------------  
960              // the winner is....  
961              // ------------------------            // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
962              if      (tp_score > ti_score) {        // the winner is....
963  //              ts = tp;//the track sorted by the tracker!!        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
964  //              cs = cp;        if      (tp_score > ti_score) {
965  //              os = op;          
966              }else if (tp_score < ti_score) {  
967                  ts = ti;//its image!!        }else if (tp_score < ti_score) {
968                  cs = ci;  
969                  os = oi;  
970            ts = ti;//its image!!
971                  ti = tp;//its image!!          cs = ci;
972                  ci = cp;          os = oi;
973                  oi = op;  
974            ti = tp;//its image!!
975                  tp = ts;//its image!!          ci = cp;
976                  cp = cs;          oi = op;
977                  op = os;  
978            tp = ts;//its image!!
979            cp = cs;
980            op = os;
981    
982                                    
983              }else {        }else {
984  //              ts = tp;  
985  //              cs = cp;  //      cout << "Warning - track image ambiguity not solved" << endl;
986  //              os = op;  
987  //                              cout << "Warning - track image ambiguity not solved" << endl;        };
 //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
             };  
988                            
989          }else{      }else{
990  //          ts = tp;        //            ts = tp;
991  //          cs = cp;        //            cs = cp;
992  //          os = op;        //            os = op;
993          };      };
994                    
995  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
996  //      sorted_tracks->Add(ts);//save the track in the sorted array      //  sorted_tracks->Add(ts);//save the track in the sorted array
997  //      sorted_tracks.Add(ts);//save the track in the sorted array      //  sorted_tracks.Add(ts);//save the track in the sorted array
998  //      sorted_tracks.Add(tp);//save the track in the sorted array      //  sorted_tracks.Add(tp);//save the track in the sorted array
999  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;      //  cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1000  //      cout<<"o "<<tp<<endl;      //  cout<<"o "<<tp<<endl;
1001  //      cout<<"o "<<cp<<endl;      //  cout<<"o "<<cp<<endl;
1002  //      cout<<"o "<<op<<endl;      //  cout<<"o "<<op<<endl;
1003          new(ttsorted[i]) PamTrack(tp,cp,op);      new(ttsorted[i]) PamTrack(tp,cp,op);
1004          new(ttimage[i])  PamTrack(ti,ci,oi);      new(ttimage[i])  PamTrack(ti,ci,oi);
1005      };    };
1006    
1007      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){    if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1008          cout << "void PamLevel2::SortTracks(TString how): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1009          tsorted->Delete(); tsorted=0;      tsorted->Delete(); tsorted=0;
1010          timage->Delete(); timage=0;          timage->Delete(); timage=0;
1011      }    }
1012    
1013      //Restore Object count    //Restore Object count
1014      //To save space in the table keeping track of all referenced objects    //To save space in the table keeping track of all referenced objects
1015      //We reset the object count to what it was at the beginning of the event.    //We reset the object count to what it was at the beginning of the event.
1016      TProcessID::SetObjectCount(ObjectNumber);    TProcessID::SetObjectCount(ObjectNumber);
1017            
1018  };  };
1019  //--------------------------------------  //--------------------------------------
# Line 643  void PamLevel2::SortTracks(TString how){ Line 1037  void PamLevel2::SortTracks(TString how){
1037  TClonesArray *PamLevel2::GetTracks(){  TClonesArray *PamLevel2::GetTracks(){
1038    
1039  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1040      SortTracks("+CAL+TOF");      SortTracks();
1041  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1042    
1043      return tsorted;      return tsorted;
# Line 662  PamTrack *PamLevel2::GetTrack(int it){ Line 1056  PamTrack *PamLevel2::GetTrack(int it){
1056    
1057  //    if(!trk2_obj) return 0;  //    if(!trk2_obj) return 0;
1058                    
1059  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1060  //     SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
1061  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1062  // //    if(!sorted_tracks)return 0;  // //    if(!sorted_tracks)return 0;
# Line 681  PamTrack *PamLevel2::GetTrack(int it){ Line 1075  PamTrack *PamLevel2::GetTrack(int it){
1075    
1076  //    cout << "PamLevel2::GetTrack(int it) "<<endl;  //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1077  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1078      SortTracks("+CAL+TOF");      SortTracks();
1079  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1080      if(!tsorted)return 0;      if(!tsorted)return 0;
1081      if(!tsorted->GetEntries())return 0;      if(!tsorted->GetEntries())return 0;
# Line 738  PamTrack *PamLevel2::GetTrackImage(int i Line 1132  PamTrack *PamLevel2::GetTrackImage(int i
1132    
1133    
1134  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1135      SortTracks("+CAL+TOF");      SortTracks();
1136  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1137      if(!timage)return 0;      if(!timage)return 0;
1138      if(!timage->GetEntries())return 0;      if(!timage->GetEntries())return 0;
# Line 775  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1169  TTree *PamLevel2::GetPamTree(TFile *f, T
1169    
1170  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);
1171  //     else                    GetWhichTrees(f);  //     else                    GetWhichTrees(f);
1172      if ( pam_tree ){
1173        printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1174        return pam_tree;
1175      };
1176      //
1177    
1178        cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1179    
1180      SetWhichTrees(detlist);      SetWhichTrees(detlist);
1181    
1182      TTree *Trout =0;      TTree *Trout =0;
# Line 782  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1184  TTree *PamLevel2::GetPamTree(TFile *f, T
1184      TString fname = f->GetName();      TString fname = f->GetName();
1185      if(!CheckLevel2File(fname))return NULL;      if(!CheckLevel2File(fname))return NULL;
1186    
1187      UInt_t *found=0;      //    UInt_t *found=0;    
1188    
1189      cout<< "GetPamTree(TFile*,TString): detector list --> ";      cout<< "GetPamTree(TFile*,TString): detector list --> ";
1190      if(TRK1)cout<<"TRK1 ";      if(TRK1)cout<<"TRK1 ";
# Line 797  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1199  TTree *PamLevel2::GetPamTree(TFile *f, T
1199      if(S4)cout<<"S4 ";      if(S4)cout<<"S4 ";
1200      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1201      cout << endl;      cout << endl;
1202        if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1203    
1204      f->cd();      f->cd();
1205    
# Line 804  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1207  TTree *PamLevel2::GetPamTree(TFile *f, T
1207      TTree *T = (TTree*)f->Get("Tracker");      TTree *T = (TTree*)f->Get("Tracker");
1208      if(T && (TRK2||TRK1||TRKh)) {      if(T && (TRK2||TRK1||TRKh)) {
1209          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1210          else    T->SetBranchStatus("TrkLevel2",0,found);  //      else    T->SetBranchStatus("TrkLevel2",0,found);
1211          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1212          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1213          else    T->SetBranchStatus("TrkLevel1",0,found);  //      else    T->SetBranchStatus("TrkLevel1",0,found);
1214          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1215          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1216          else    T->SetBranchStatus("TrkHough",0,found);  //      else    T->SetBranchStatus("TrkHough",0,found);
1217          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1218          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1219          else Trout->AddFriend(T);          else Trout->AddFriend(T);
# Line 821  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1224  TTree *PamLevel2::GetPamTree(TFile *f, T
1224      TTree *C = (TTree*)f->Get("Calorimeter");      TTree *C = (TTree*)f->Get("Calorimeter");
1225      if(C && (CAL2||CAL1)) {      if(C && (CAL2||CAL1)) {
1226          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1227          else    C->SetBranchStatus("CaloLevel2",0,found);  //      else    C->SetBranchStatus("CaloLevel2",0,found);
1228          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1229          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1230          else    C->SetBranchStatus("CaloLevel1",0,found);  //      else    C->SetBranchStatus("CaloLevel1",0,found);
1231          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1232          if(!Trout)Trout=C;          if(!Trout)Trout=C;
1233          else Trout->AddFriend(C);          else Trout->AddFriend(C);
# Line 847  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1250  TTree *PamLevel2::GetPamTree(TFile *f, T
1250      if(R && TRG) {      if(R && TRG) {
1251          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1252          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1253          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1254          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1255      }else{      }else{
1256          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 857  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1260  TTree *PamLevel2::GetPamTree(TFile *f, T
1260      if(S && S4) {      if(S && S4) {
1261          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1262          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1263          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1264          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1265      }else{      }else{
1266          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 867  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1270  TTree *PamLevel2::GetPamTree(TFile *f, T
1270      if(N && ND) {      if(N && ND) {
1271          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1272          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1273          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1274          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1275      }else{      }else{
1276          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 877  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1280  TTree *PamLevel2::GetPamTree(TFile *f, T
1280      if(A && AC) {      if(A && AC) {
1281          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1282          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1283          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1284          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1285      }else{      }else{
1286          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
# Line 887  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1290  TTree *PamLevel2::GetPamTree(TFile *f, T
1290      if(B && ORB) {      if(B && ORB) {
1291          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1292          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1293          if(!Trout)Trout=O;          if(!Trout)Trout=B;
1294          else Trout->AddFriend(B);          else Trout->AddFriend(B);
1295      }else{      }else{
1296          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1297      };      };
1298    
1299        TTree *L = (TTree*)f->Get("SelectionList");
1300        if(L && SELLI==1) {
1301    //      L->SetBranchAddress("RunEntry",&irun);
1302    //      cout << "SelectionList: set branch address RunEntry"<<endl;
1303    //      L->SetBranchAddress("EventEntry",&irunentry);
1304    //      cout << "SelectionList: set branch address EventEntry"<<endl;
1305    //      if(!Trout)Trout=O;
1306    //      else Trout->AddFriend("SelectionList");
1307            cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;
1308            sel_tree = 0;
1309        }else{
1310            cout << "SelectionList  : missing tree"<<endl;
1311        };
1312            
1313      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1314    
1315        pam_tree = (TChain*)Trout;
1316            
1317      return Trout;      return Trout;
1318            
# Line 913  TList*  PamLevel2::GetListOfLevel2Files( Line 1332  TList*  PamLevel2::GetListOfLevel2Files(
1332                    
1333      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1334            
1335      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1336  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1337      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1338              cout << "Level2 data directory : "<<  ddir << endl;
1339        } else {
1340          cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1341        };
1342      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1343      contents->SetOwner();      contents->SetOwner();
1344            
# Line 932  TList*  PamLevel2::GetListOfLevel2Files( Line 1354  TList*  PamLevel2::GetListOfLevel2Files(
1354  //          return 0;  //          return 0;
1355  //      }        //      }      
1356  //      flisttxt = fullpath;  //      flisttxt = fullpath;
1357          if ( !flisttxt.EndsWith(".root") ){
1358    
1359          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1360    
# Line 963  TList*  PamLevel2::GetListOfLevel2Files( Line 1386  TList*  PamLevel2::GetListOfLevel2Files(
1386  //          cout <<"(2) " << file << endl;  //          cout <<"(2) " << file << endl;
1387  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1388  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1389              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));              if( file.EndsWith(".root") ){
1390              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list                TString filedir;
1391              delete fullpath;                if (ddir != ""){
1392                    filedir = ddir; // take the input dir
1393                  } else {
1394                    gSystem->ChangeDirectory(wdir); // back to the working directory
1395                    filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1396                  };
1397                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1398                    contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1399                    delete fullpath;
1400                }
1401  //          }else{  //          }else{
1402  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1403  //          };  //          };
1404          };          };
1405          in.close();          in.close();
1406                  } else {
1407              if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1408              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1409              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1410              delete fullpath;
1411          };        
1412      }else{      }else{
1413                    
1414          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1415          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1416  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1417                    if ( ddir == "" ){
1418              ddir = wdir;
1419              cout << "Level2 data directory : "<<  ddir << endl;
1420            };
1421    
1422          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1423          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1424  //              temp->Print();  //              temp->Print();
# Line 1021  TList*  PamLevel2::GetListOfLevel2Files( Line 1462  TList*  PamLevel2::GetListOfLevel2Files(
1462   * @return Pointer to a TChain   * @return Pointer to a TChain
1463   */   */
1464  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1465      //
1466      //
1467      //
1468      if ( pam_tree ){
1469        printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1470        return pam_tree;
1471      };
1472      //
1473            
1474      TChain *Trout =0;      TChain *Trout =0;
1475    
# Line 1041  TChain *PamLevel2::GetPamTree(TList *fl, Line 1490  TChain *PamLevel2::GetPamTree(TList *fl,
1490      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1491      cout << endl;      cout << endl;
1492    
1493      TChain *T = 0;             TChain *T = 0;    
1494      TChain *C = 0;       TChain *C = 0;
1495      TChain *O = 0;       TChain *O = 0;
1496      TChain *R = 0;       TChain *R = 0;
1497      TChain *S = 0;       TChain *S = 0;
1498      TChain *N = 0;       TChain *N = 0;
1499      TChain *A = 0;       TChain *A = 0;
1500      TChain *B = 0;       TChain *B = 0;
1501    
1502         TChain *L = 0;
1503            
1504      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");          if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1505      if(CAL2||CAL1)       C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
# Line 1058  TChain *PamLevel2::GetPamTree(TList *fl, Line 1509  TChain *PamLevel2::GetPamTree(TList *fl,
1509      if(ND)               N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1510      if(AC)               A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1511      if(ORB)              B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
1512         L = new TChain("SelectionList");
1513            
1514      // loop over files and create chains              // loop over files and create chains        
1515      TIter next(fl);      TIter next(fl);
# Line 1074  TChain *PamLevel2::GetPamTree(TList *fl, Line 1526  TChain *PamLevel2::GetPamTree(TList *fl,
1526              if(ND)               N->Add(name);              if(ND)               N->Add(name);
1527              if(AC)               A->Add(name);              if(AC)               A->Add(name);
1528              if(ORB)              B->Add(name);              if(ORB)              B->Add(name);
1529                if(SELLI==1)         L->Add(name);
1530          };          };
1531      }      };
1532            
1533      cout << "done chain \n";      cout << "done chain \n";
1534        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1535    
1536      UInt_t *found=0;  //    UInt_t *found=0;
1537  // Tracker  // Tracker
1538      if(T && (TRK2||TRK1||TRKh)) {      if(T && (TRK2||TRK1||TRKh)) {
1539          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1540          else    T->SetBranchStatus("TrkLevel2",0,found);  //      else    T->SetBranchStatus("TrkLevel2",0,found);
1541          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1542          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1543          else    T->SetBranchStatus("TrkLevel1",0,found);  //      else    T->SetBranchStatus("TrkLevel1",0,found);
1544          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1545          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1546          else    T->SetBranchStatus("TrkHough",0,found);  //      else    T->SetBranchStatus("TrkHough",0,found);
1547          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1548          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1549          else Trout->AddFriend("Tracker");          else Trout->AddFriend("Tracker");
1550      }else{      }else{
1551          cout << "Tracker      : missing tree"<<endl;          cout << "Tracker      : missing tree"<<endl;
1552      };      };
1553      // Calorimeter      // Calorimeter
1554      if(C && (CAL2||CAL1)) {      if(C && (CAL2||CAL1)) {
1555          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1556          else    C->SetBranchStatus("CaloLevel2",0,found);  //      else    C->SetBranchStatus("CaloLevel2",0,found);
1557          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1558          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1559          else    C->SetBranchStatus("CaloLevel1",0,found);  //      else    C->SetBranchStatus("CaloLevel1",0,found);
1560          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1561          if(!Trout)Trout=C;          if(!Trout)Trout=C;
1562          else Trout->AddFriend("Calorimeter");          else Trout->AddFriend("Calorimeter");
# Line 1163  TChain *PamLevel2::GetPamTree(TList *fl, Line 1617  TChain *PamLevel2::GetPamTree(TList *fl,
1617      }else{      }else{
1618          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1619      };      };
1620        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1621    
1622        // Selection List
1623        if(L && SELLI==1) {
1624            cout<<">>> Found selection-list <<<"<<endl;
1625            L->SetBranchAddress("RunEntry",&irun);
1626            cout << "SelectionList: set branch address RunEntry"<<endl;
1627            L->SetBranchAddress("EventEntry",&irunentry);
1628            cout << "SelectionList: set branch address EventEntry"<<endl;
1629            sel_tree = L;
1630    //      if(!Trout)Trout=O;
1631    //      else Trout->AddFriend("SelectionList");
1632            cout << "----------------------------------------------------" <<endl;
1633        }else{
1634    //      cout << "SelectionList  : missing tree"<<endl;
1635            if(L)L->Delete();
1636        };
1637            
1638  //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;  //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1639    
1640            pam_tree = Trout;
1641  //     // Tracker  
 //     if(TRK2||TRK1||TRKh){  
 //      if(!Tout)Tout=T;  
 //      else Tout->AddFriend("Tracker");  
 //     }  
 //     // Calorimeter  
 //     if(CAL2||CAL1) {          
 //      if(!Tout)Tout=C;  
 //      else Tout->AddFriend("Calorimeter");  
 //     };  
 //     // ToF      
 //     if(TOF) {  
 //      if(!Tout)Tout=O;  
 //      else Tout->AddFriend("ToF");  
 //     };  
 //     // Trigger  
 //     if(TRG) {  
 //      if(!Tout)Tout=R;  
 //      else Tout->AddFriend("Trigger");  
 //     };  
 //     // S4  
 //     if(S4) {  
 //      if(!Tout)Tout=S;  
 //      else Tout->AddFriend("S4");  
 //     };  
 //     // Neutron Detector  
 //     if(ND) {  
 //      if(!Tout)Tout=N;  
 //      else Tout->AddFriend("NeutronD");  
 //     };  
 //     // Anticounters  
 //     if(AC) {  
 //      if(!Tout)Tout=A;  
 //      else Tout->AddFriend("Anticounter");  
 //     };  
 //     // OrbitalInfo  
 //     if(ORB) {  
 //      if(!Tout)Tout=B;  
 //      else Tout->AddFriend("OrbitalInfo");  
 //     };  
       
 //    cout<<endl<<" Number of entries: "<<Tout->GetEntries()<<endl<<endl;  
       
 //    if( Tout->GetEntries() )PamLevel2::SetBranchAddress( Tout );  
       
1642      return Trout;      return Trout;
1643  }  }
1644    
# Line 1293  void PamLevel2::SetBranchAddress(TTree * Line 1720  void PamLevel2::SetBranchAddress(TTree *
1720          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1721          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1722      };      };
1723        // SelectionList
1724        if(SELLI==1) {
1725            t->SetBranchAddress("RunEntry", &irun);
1726            cout << "SelectionList: set branch address RunEntry"<<endl;
1727            t->SetBranchAddress("EventEntry", &irunentry);
1728            cout << "SelectionList: set branch address EventEntry"<<endl;
1729        };
1730            
1731  }  }
1732  /**  /**
# Line 1366  void PamLevel2::SetBranchAddress(TChain Line 1800  void PamLevel2::SetBranchAddress(TChain
1800          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1801          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1802      };      };
1803        // SelectionList
1804        if(SELLI==1) {
1805            t->SetBranchAddress("RunEntry", &irun);
1806            cout << "SelectionList: set branch address RunEntry"<<endl;
1807            t->SetBranchAddress("EventEntry", &irunentry);
1808            cout << "SelectionList: set branch address EventEntry"<<endl;
1809        };
1810            
1811  }  }
1812    
# Line 1380  void PamLevel2::SetBranchAddress(TChain Line 1821  void PamLevel2::SetBranchAddress(TChain
1821   * @return Pointer to a TChain   * @return Pointer to a TChain
1822   */   */
1823  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
1824              //
1825      TChain *R = new TChain("Run");    //
1826      //
1827      if ( run_tree ){
1828        printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1829        return run_tree;
1830      };    
1831      //
1832      TChain *R = new TChain("Run");
1833            
1834      // loop over files and create chains              // loop over files and create chains        
1835      TIter next(fl);      TIter next(fl);
# Line 1393  TChain *PamLevel2::GetRunTree(TList *fl) Line 1841  TChain *PamLevel2::GetRunTree(TList *fl)
1841              R->Add(name);              R->Add(name);
1842          };          };
1843      }      }
1844    
1845        if(R->GetNtrees()){
1846            
1847      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1848      cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
1849      R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1850      cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1851            }else{
1852            delete R;
1853            R=0;
1854        }
1855    
1856        run_tree = R;
1857    
1858      return R;      return R;
1859            
1860  }  }
# Line 1412  TChain *PamLevel2::GetRunTree(TList *fl) Line 1868  TChain *PamLevel2::GetRunTree(TList *fl)
1868   * @return Pointer to a TTree   * @return Pointer to a TTree
1869   */   */
1870  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
1871      if ( run_tree ){
1872        printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1873        return run_tree;
1874      };
1875            
1876    
1877        cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1878    
1879      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
       
     R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));  
     cout << "Run         : set branch address RunInfo"<<endl;  
     R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano  
     cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano  
1880            
1881      return R;      if(T){
1882            T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1883            cout << "Run         : set branch address RunInfo"<<endl;
1884            T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1885            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1886        }
1887    
1888        run_tree = (TChain*)T;
1889    
1890        return T;
1891            
1892  }  }
1893  /**  /**
# Line 1429  TTree *PamLevel2::GetRunTree(TFile *f){ Line 1895  TTree *PamLevel2::GetRunTree(TFile *f){
1895   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
1896   * @return true if a new run has been read, false if it is still the same run   * @return true if a new run has been read, false if it is still the same run
1897   */   */
1898  Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1899    //    //
1900    // check if we have already called once GetEntry, if not call it    // check if we have already called once GetEntry, if not call it
1901    //    //
1902    if ( run->GetEntries() <= 0 ) return(false);      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;
1903        if(!run){
1904              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1905              return(false);        
1906        }
1907        if ( run->GetEntries() <= 0 ) return(false);
1908    //    //
1909    Int_t oldrun = irun;    
1910    if ( irun < 0 ){    //  Int_t oldrun = irun;
1911      irun = 0;    Long64_t oldrun = irun;
1912      run->GetEntry(0);  
1913      runfirstentry = 0ULL;    // --------------------------------------
1914      runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS) - 1ULL;    // if it is a full file (not preselected)
1915      // --------------------------------------
1916      if(SELLI==0){
1917    
1918          //
1919          // the absolute time is necessary to relate the event with the run
1920          //
1921          if( !GetOrbitalInfo() ){
1922              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1923              return(false);
1924          }
1925    
1926          //
1927          // the first time the routine is called, set run search from the beginning
1928          //
1929          if ( irun < 0LL ){
1930              irun = 0LL;
1931              run->GetEntry(irun);
1932              runfirstentry = 0LL;
1933              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1934              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1935          };      
1936          //
1937           if ( irun == run->GetEntries()-1LL &&
1938               !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&
1939                 GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)
1940                ){
1941             irun = -1LL;
1942             runfirstentry = 0LL;
1943             runlastentry = -1LL;
1944           };
1945          // modificato il controllo sull'aggiornamento del run, per evitare problemi
1946          // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
1947          //
1948          bool fromfirst = true;
1949          //
1950          while ( !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
1951    //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
1952              irun++;
1953              run->GetEntry(irun);
1954              runfirstentry = runlastentry;
1955              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
1956              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1957    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1958    //        cout << "runfirstentry "<<runfirstentry<<endl;
1959              //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
1960              //      printf(" abstime %u trailertime %u \n",GetOrbitalInfo()->absTime,GetRunInfo()->RUNTRAILER_TIME);
1961              //      printf(" IDRUN %u \n",GetRunInfo()->ID);
1962              //
1963    //        prevshift = 0;
1964              //
1965              if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)){
1966                  printf(" resetting irun  (it should NOT happen!!!)\n");
1967                  fromfirst = false;
1968                  irun = 0;
1969                  run->GetEntry(irun);
1970                  runfirstentry = 0ULL;
1971                  runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1972                  if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1973              };
1974              //
1975          };
1976          //
1977          if (
1978               !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&
1979                 GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)
1980              ) {
1981              printf(" Something very wrong here: cannot find RUN containing absolute time %u \n",GetOrbitalInfo()->absTime);
1982              return false;
1983          }
1984          //
1985          if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
1986          //
1987          //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
1988          //
1989          prevshift = 0;
1990          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1991    //      cout << "runfirstentry "<<runfirstentry<<endl;
1992          return(true);    
1993    };    };
1994    while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){    // ----------------------------------------------------
1995      //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));    // if it is a preselected file (there is SelectionList)
1996      irun++;    // NBNB - the event tree MUST be read first
1997      run->GetEntry(irun);    // ----------------------------------------------------
1998      runfirstentry = runlastentry+1ULL;    if(SELLI==1){      
1999      runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);        sel_tree->GetEntry(iev);
2000    };  //      cout << irun << " "<< irunentry << endl;
2001    //        if(irun != oldrun){
2002    if ( irun == oldrun || irun >= run->GetEntries() ) return(false);            run->GetEntry(irun);
2003    //            cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2004    //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);            prevshift = 0;
2005    //            return true;
2006    return(true);            }
2007          return false;
2008      }
2009    
2010      return false;
2011    //    //
2012  };  };
2013    
2014    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2015    
2016        if(!run_tree){
2017            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2018            return false;
2019        }
2020        if ( run_tree->GetEntries() <= 0 ) {
2021            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2022            return(false);
2023        }
2024      
2025        Int_t oldrun = irun; // store current run index
2026    
2027        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2028        // if it is a full file (not preselected)
2029        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2030        if(SELLI==0){
2031    
2032            // ---------------------------------------------------------------
2033            // the absolute time is necessary to relate the event with the run
2034            // ---------------------------------------------------------------
2035            if( !GetOrbitalInfo() ){
2036                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2037                return(false);
2038            }
2039          
2040            // -----------------------------------------------------------------------
2041            // the first time the routine is called, set run search from the beginning
2042            // -----------------------------------------------------------------------
2043            if ( irun < 0 ){
2044                irun = 0LL;
2045                irunentry = 0;
2046                prevshift = 0;
2047                run_tree->GetEntry(irun);
2048            };      
2049            //
2050            bool fromfirst = true; // first loop over runs
2051    
2052    //      Bool_t hasfrag = false;
2053    //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2054    //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2055    
2056            // ------------------------------------------------------
2057            // loop over runs to find the one that contains the event
2058            // ------------------------------------------------------
2059            while (
2060                (
2061                    (
2062                        !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2063                          GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) &&
2064                        !(GetOrbitalInfo()->OBT >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2065                          GetOrbitalInfo()->OBT <= GetRunInfo()->RUNTRAILER_OBT)
2066                        )
2067                    || GetRunInfo()->NEVENTS==0
2068                    || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)
2069                    )
2070                    && irun < run_tree->GetEntries() ){
2071    
2072                irun++;
2073                // ------------------------------------
2074                // if the end of run tree is reached...
2075                // ------------------------------------
2076                if( irun == run_tree->GetEntries() ){
2077                    if(!fromfirst){
2078                        // -----------------------------------------------------
2079                        // if it happened already once and the run was not found
2080                        // ---> exit with error
2081                        // -----------------------------------------------------
2082                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2083                        return false;
2084                    }
2085                    // -----------------------------------------
2086                    // ...otherwise repeat search from beginning
2087                    // -----------------------------------------
2088                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2089                    fromfirst = false;
2090                    irun = 0LL;
2091                    runfirstentry = 0LL;
2092                }
2093                // -------------------------------------------------------------------
2094                // save the index of the first entry of the run, relative to pam_tree,
2095                // and read a new run
2096                // -------------------------------------------------------------------
2097                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2098                irunentry = 0;
2099                prevshift = 0;
2100                run_tree->GetEntry(irun);      
2101                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2102                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2103                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2104                }
2105    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2106    //              cout << "... where is the next fragment ??"<<endl;
2107    //          }
2108            };
2109    
2110    
2111            // --------------------------------------
2112            // if there was no need to update the run
2113            // ---> exit with FALSE
2114            // --------------------------------------
2115            if ( irun == oldrun ) return(false);
2116    
2117            // --------------------------------------
2118            // ... otherwise
2119            // ---> exit with TRUE
2120            // --------------------------------------
2121            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2122            // ----------------------------------------------------
2123            // then check if the run has a fragment
2124            // in this case we have to switch to the next fragment
2125            // when the end of the first fragment is reached
2126            // ----------------------------------------------------
2127            if(
2128                GetRunInfo()->ID_RUN_FRAG != 0 &&
2129    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2130                true ){
2131                cout << "* fragment *"<<endl;              
2132            }
2133            
2134            return(true);    
2135        };
2136        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2137        // if it is a preselected file (there is SelectionList)
2138        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2139        if(SELLI==1){      
2140            sel_tree->GetEntry(iev);
2141            if(irun != oldrun){
2142                run_tree->GetEntry(irun);
2143                cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2144                prevshift = 0;
2145                return true;
2146            }
2147            return false;
2148        }
2149    
2150        return false;
2151        //
2152    };
2153  /**  /**
2154   * Update the runinfo informations (to be used to have Run infos event by event basis)   * Update the runinfo informations (to be used to have Run infos event by event basis)
2155   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2156   * @return true if a new run has been read, false if it is still the same run   * @return true if a new run has been read, false if it is still the same run
2157   */   */
2158  Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2159    return(UpdateRunInfo((TChain*)run,iev));    return(UpdateRunInfo((TChain*)run,iev));
2160  };  };
2161    
# Line 1477  Bool_t PamLevel2::UpdateRunInfo(TTree *r Line 2170  Bool_t PamLevel2::UpdateRunInfo(TTree *r
2170  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
2171                    
2172      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2173            CAL0 = false;
2174          CAL1 = true;          CAL1 = true;
2175          CAL2 = true;          CAL2 = true;
2176          TRK2 = true;          TRK2 = true;
2177          TRK1 = false;          TRK1 = false;
2178          TRKh = false;          TRKh = false;
2179            TRK0 = false;
2180          TRG = true;          TRG = true;
2181          TOF = true;          TOF = true;
2182            TOF0 = false;
2183          S4  = true;          S4  = true;
2184          ND  = true;          ND  = true;
2185          AC  = true;          AC  = true;
2186          ORB = true;          ORB = true;
2187      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2188            CAL0 = false;
2189          CAL1 = false;          CAL1 = false;
2190          CAL2 = false;          CAL2 = false;
2191          TRK2 = false;          TRK2 = false;
2192          TRK1 = false;          TRK1 = false;
2193          TRKh = false;          TRKh = false;
2194            TRK0 = false;
2195          TRG = false;          TRG = false;
2196          TOF = false;          TOF = false;
2197            TOF0 = false;
2198          S4  = false;          S4  = false;
2199          ND  = false;          ND  = false;
2200          AC  = false;          AC  = false;
# Line 1507  void PamLevel2::SetWhichTrees(TString de Line 2206  void PamLevel2::SetWhichTrees(TString de
2206          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
2207          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
2208      };      };
2209    
2210        if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
2211            if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
2212            if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
2213        };
2214                    
2215      if( detlist.Contains("CAL2", TString::kIgnoreCase)){      if( detlist.Contains("CAL2", TString::kIgnoreCase)){
2216          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
# Line 1519  void PamLevel2::SetWhichTrees(TString de Line 2223  void PamLevel2::SetWhichTrees(TString de
2223          CAL1=false;          CAL1=false;
2224      }      }
2225  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
2226        if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
2227            if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
2228            if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
2229        };
2230    
2231      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
2232          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
2233          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
# Line 1547  void PamLevel2::SetWhichTrees(TString de Line 2256  void PamLevel2::SetWhichTrees(TString de
2256            
2257      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
2258      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
2259    
2260        if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
2261        else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
2262            
2263      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;
2264      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;
# Line 1581  void PamLevel2::SetWhichTrees(TString de Line 2293  void PamLevel2::SetWhichTrees(TString de
2293   * Set tree/branch detector flags from the content of a tree   * Set tree/branch detector flags from the content of a tree
2294   */   */
2295  void  PamLevel2::GetWhichTrees(TFile* f){  void  PamLevel2::GetWhichTrees(TFile* f){
2296                
2297    
2298    
2299        cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
2300      // -----------      // -----------
2301      // reset flags      // reset flags
2302      // -----------      // -----------
# Line 1753  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2468  Bool_t  PamLevel2::CheckLevel2File(TStri
2468            
2469      Bool_t RUN__ok    = false;      Bool_t RUN__ok    = false;
2470            
2471            Bool_t SELLI__ok  = false;
2472    
2473      cout << "Checking file: "<<name<<endl;      cout << "Checking file: "<<name<<endl;
2474      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
# Line 1770  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2485  Bool_t  PamLevel2::CheckLevel2File(TStri
2485    
2486      while( (key = (TKey*)next()) ){      while( (key = (TKey*)next()) ){
2487                    
2488    //      cout << key->GetName() << endl;
2489  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;
2490  //              cout << " Get tree: " << f->Get(key->GetName())<<endl;  //              cout << " Get tree: " << f->Get(key->GetName())<<endl;
2491  //      nev_previous = nev;  //      nev_previous = nev;
# Line 1780  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2496  Bool_t  PamLevel2::CheckLevel2File(TStri
2496  //          return false;  //          return false;
2497  //      };  //      };
2498    
2499    
2500          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
2501    
2502          //=========================================================              //=========================================================    
2503            if( !strcmp(key->GetName(),"SelectionList"    ) ){
2504                SELLI__ok = true;
2505                if(SELLI==1){
2506                    Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
2507                    if( nev && nevt!=nev){
2508                        cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
2509                        return false;
2510                    }
2511                    nev=nevt;
2512                }
2513            }
2514    
2515            //=========================================================    
2516          if( !strcmp(key->GetName(),"Trigger"    ) ){          if( !strcmp(key->GetName(),"Trigger"    ) ){
2517              TRG__ok = true;              TRG__ok = true;
2518              if(TRG){              if(TRG){
# Line 1894  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2624  Bool_t  PamLevel2::CheckLevel2File(TStri
2624          };                };      
2625    
2626      };      };
2627    
2628        if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
2629        if( SELLI==0 && SELLI__ok ){
2630            cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
2631            return false;  
2632        }
2633        if( SELLI==1 && !SELLI__ok ){
2634            cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
2635            return false;
2636        }
2637            
2638    //    cout << "SELLI "<<SELLI<<endl;
2639    
2640  //     cout<< "CheckLevel2File(TString): detector list --> ";  //     cout<< "CheckLevel2File(TString): detector list --> ";
2641  //     if(TRK1__ok)cout<<"TRK1 ";  //     if(TRK1__ok)cout<<"TRK1 ";
2642  //     if(TRK2__ok)cout<<"TRK2 ";  //     if(TRK2__ok)cout<<"TRK2 ";
# Line 2003  void PamLevel2::CreateCloneTrees0( TChai Line 2745  void PamLevel2::CreateCloneTrees0( TChai
2745      cout << "Create clones of PAMELA trees "<<endl;      cout << "Create clones of PAMELA trees "<<endl;
2746            
2747      Int_t i=0;      Int_t i=0;
2748      tree_clone[i] = fChain->GetTree()->CloneTree(0);      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
2749      TString name =  tree_clone[i]->GetName();      TString name =  pam_tree_clone[i]->GetName();
2750      name.Append("_clone");      name.Append("_clone");
2751  //    tree_clone[i]->SetName(name.Data());  //    pam_tree_clone[i]->SetName(name.Data());
2752      cout << tree_clone[i]->GetName() <<endl;      cout << pam_tree_clone[i]->GetName() <<endl;
2753      i++;      i++;
2754    
2755      TList *li = fChain->GetListOfFriends();      TList *li = fChain->GetListOfFriends();
# Line 2017  void PamLevel2::CreateCloneTrees0( TChai Line 2759  void PamLevel2::CreateCloneTrees0( TChai
2759      while( (T_friend = (TFriendElement*)next()) ){      while( (T_friend = (TFriendElement*)next()) ){
2760  //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;  //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
2761  //      cout<<T_friend->GetTree()->GetName()<< endl;  //      cout<<T_friend->GetTree()->GetName()<< endl;
2762          tree_clone[i] = T_friend->GetTree()->CloneTree(0);          pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
2763          tree_clone[i]->SetAutoSave(1000000);          pam_tree_clone[i]->SetAutoSave(1000000);
2764          name =  tree_clone[i]->GetName();          name =  pam_tree_clone[i]->GetName();
2765          name.Append("_clone");          name.Append("_clone");
2766  //      tree_clone[i]->SetName(name.Data());  //      pam_tree_clone[i]->SetName(name.Data());
2767          cout << tree_clone[i]->GetName() << endl;          cout << pam_tree_clone[i]->GetName() << endl;
2768          i++;          i++;
2769      }      }
2770            
# Line 2037  void PamLevel2::CreateCloneTrees0( TChai Line 2779  void PamLevel2::CreateCloneTrees0( TChai
2779   */   */
2780  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile){
2781    
2782    
2783    //  if the pointer is null, create a default file
2784    
2785        if(!ofile){
2786            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
2787            ofile = new TFile("clone-tree.root","recreate");
2788        }
2789    
2790      ofile->cd();      ofile->cd();
2791    
2792      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2793      cout << "Create new PAMELA trees "<<endl;      cout << "Create new PAMELA trees "<<endl;
2794                    
     Int_t i=0;  
2795    
2796        run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
2797        run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
2798        cout << "Run          : branch RunInfo"<<endl;
2799        run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
2800        cout << "Run          : branch SoftInfo"<<endl;
2801        // ------------------
2802        // replicate run tree
2803        // ------------------
2804        cout << "----------------------------------------------------"<<endl;
2805        cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
2806        for (Int_t i=0; i<run_tree->GetEntries(); i++){
2807            run_tree->GetEntry(i);
2808            cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
2809            run_tree_clone->Fill();
2810        }
2811        cout << "----------------------------------------------------"<<endl;
2812    
2813    
2814        sel_tree_clone = new TTree("SelectionList","List of selected events ");
2815        sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
2816        sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
2817        
2818    
2819        Int_t i=0;
2820      if(TRK1||TRK2||TRKh){      if(TRK1||TRK2||TRKh){
2821          tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");              pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
2822          if(TRK1) {          if(TRK1) {
2823              tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
2824              tree_clone[i]->BranchRef();              pam_tree_clone[i]->BranchRef();
2825              cout << "Tracker      : branch TrkLevel1"<<endl;              cout << "Tracker      : branch TrkLevel1"<<endl;
2826    //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
2827          };          };
2828          if(TRK2) {          if(TRK2) {
2829              tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
2830              cout << "Tracker      : branch TrkLevel2"<<endl;                      cout << "Tracker      : branch TrkLevel2"<<endl;        
2831          };          };
2832          if(TRKh) {          if(TRKh) {
2833              tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));              pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
2834              cout << "Tracker      : branch TrkHough"<<endl;              cout << "Tracker      : branch TrkHough"<<endl;
2835          };          };
2836          i++;          i++;
# Line 2064  void PamLevel2::CreateCloneTrees(TFile * Line 2838  void PamLevel2::CreateCloneTrees(TFile *
2838    
2839      // Calorimeter      // Calorimeter
2840      if(CAL1||CAL2){      if(CAL1||CAL2){
2841          tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");              pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
2842          if(CAL1) {          if(CAL1) {
2843              tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));              pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
2844              cout << "Calorimeter  : branch CaloLevel1"<<endl;              cout << "Calorimeter  : branch CaloLevel1"<<endl;
2845          };          };
2846          if(CAL2) {          if(CAL2) {
2847              tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));              pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
2848              cout << "Calorimeter  : branch CaloLevel2"<<endl;              cout << "Calorimeter  : branch CaloLevel2"<<endl;
2849          };          };
2850          i++;          i++;
# Line 2078  void PamLevel2::CreateCloneTrees(TFile * Line 2852  void PamLevel2::CreateCloneTrees(TFile *
2852    
2853      // ToF          // ToF    
2854      if(TOF) {      if(TOF) {
2855          tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");              pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
2856          tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));          pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
2857          cout << "ToF          : branch ToFLevel2"<<endl;          cout << "ToF          : branch ToFLevel2"<<endl;
2858          i++;          i++;
2859      };      };
2860      // Trigger      // Trigger
2861      if(TRG) {      if(TRG) {
2862          tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");              pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
2863          tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));          pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
2864          cout << "Trigger      : branch TrigLevel2"<<endl;          cout << "Trigger      : branch TrigLevel2"<<endl;
2865          i++;          i++;
2866      };      };
2867      // S4      // S4
2868      if(S4) {      if(S4) {
2869          tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");                pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");  
2870          tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));          pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
2871          cout << "S4           : branch S4Level2"<<endl;          cout << "S4           : branch S4Level2"<<endl;
2872          i++;          i++;
2873      };      };
2874      // Neutron Detector      // Neutron Detector
2875      if(ND) {      if(ND) {
2876          tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");            pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");      
2877          tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));          pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
2878          cout << "NeutronD     : branch NDLevel2"<<endl;          cout << "NeutronD     : branch NDLevel2"<<endl;
2879          i++;          i++;
2880      };      };
2881      // Anticounters      // Anticounters
2882      if(AC) {      if(AC) {
2883          tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");              pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");        
2884          tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));          pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
2885          cout << "Anticounter  : branch AcLevel2"<<endl;          cout << "Anticounter  : branch AcLevel2"<<endl;
2886          i++;          i++;
2887      };      };
2888      // OrbitalInfo      // OrbitalInfo
2889      if(ORB) {      if(ORB) {
2890          tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");                pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");  
2891          tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
2892          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
2893          i++;          i++;
2894      };      };
# Line 2129  void PamLevel2::CreateCloneTrees(TFile * Line 2903  void PamLevel2::CreateCloneTrees(TFile *
2903   */   */
2904  //void PamLevel2::FillNewPamTree(TTree *T){  //void PamLevel2::FillNewPamTree(TTree *T){
2905  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees(){
2906                
2907      for(Int_t i=0; i<8; i++){  //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
2908          if(tree_clone[i])tree_clone[i]->Fill();  
2909        for(Int_t i=0; i<NCLONES; i++){
2910            if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
2911      }      }
2912        if(sel_tree_clone)sel_tree_clone->Fill();    
2913    
2914  }  }
2915    
2916    
2917  TTree* PamLevel2::GetCloneTree(TString name){  TTree* PamLevel2::GetCloneTree(TString name){
2918    
2919      for(Int_t i=0; i<8; i++){        for(Int_t i=0; i<NCLONES; i++){    
2920          if(tree_clone[i]){          if(pam_tree_clone[i]){
2921              TString na = tree_clone[i]->GetName();              TString na = pam_tree_clone[i]->GetName();
2922              if(!name.CompareTo(na))return tree_clone[i];              if(!name.CompareTo(na))return pam_tree_clone[i];
2923          };          };
2924      }      }
2925        if(run_tree_clone){
2926            TString na = run_tree_clone->GetName();
2927            if(!name.CompareTo(na))return run_tree_clone;
2928        }
2929        if(sel_tree_clone){
2930            TString na = sel_tree_clone->GetName();
2931            if(!name.CompareTo(na))return sel_tree_clone;
2932        }
2933      return NULL;      return NULL;
2934    
2935  }  }
2936  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
2937      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2938      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
2939      for(Int_t i=0; i<8; i++){        cout << run_tree_clone->GetName()<<endl;    
2940          if(tree_clone[i]){      run_tree_clone->Write();
2941              cout << tree_clone[i]->GetName()<<endl;      cout << sel_tree_clone->GetName()<<endl;    
2942              tree_clone[i]->Write();      sel_tree_clone->Write();
2943        for(Int_t i=0; i<NCLONES; i++){    
2944            if(pam_tree_clone[i]){
2945                cout << pam_tree_clone[i]->GetName()<<endl;
2946                pam_tree_clone[i]->Write();
2947          };          };
2948      }      }
2949      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 2161  void PamLevel2::WriteCloneTrees(){ Line 2951  void PamLevel2::WriteCloneTrees(){
2951  }  }
2952    
2953  /**  /**
2954   * Create a new (empty) Pamela trees   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
2955     */
2956    //Int_t PamLevel2::GetEntry(Int_t iee){
2957    Int_t PamLevel2::GetEntry(Long64_t iee){
2958        
2959    //     cout << "-------------------------------------"<<endl;
2960    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
2961    
2962        if(!pam_tree){
2963            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
2964            return 0;
2965        }
2966    
2967    
2968        //
2969        // 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...
2970        //
2971        //    if(!run_tree ){
2972        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
2973        //  return 0;
2974        //    }
2975    
2976        Long64_t ii=0;
2977        //-------------------------------
2978        ii = iee;
2979        if( !pam_tree->GetEntry(ii) ){      
2980            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
2981            return 0;
2982        }
2983        //
2984        // ... 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.
2985        // 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
2986        // a problem if you don't check the return code of getentry.
2987        //
2988        if(!run_tree ){
2989            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
2990                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
2991                return 0;
2992            }  else {
2993                return 1; //cosi` se non c'e` run esce qua...
2994            }
2995        }
2996    
2997        //-------------------------------
2998        ii = iee;
2999    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3000    //    Bool_t UPDATED = UpdateRunInfo(ii);
3001        UpdateRunInfo(ii);
3002        if(SELLI==0)irunentry = iee-runfirstentry;
3003    //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3004      
3005    //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3006    
3007    //     cout << "irunentry     "<<irunentry << endl;
3008    //     cout << "runfirstentry "<<runfirstentry << endl;
3009    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3010    
3011    //     if( TRK0 || CAL0 || TOF0 ){
3012    //      if( !GetYodaEntry( ) ){
3013    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3014    //          return 0;
3015    //      }
3016    //     }
3017    
3018    
3019        return 1;
3020    
3021    }
3022    
3023    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3024        if( !TRK0 )return NULL;
3025        if( !GetYodaEntry( ) ){
3026            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3027            return 0;
3028        }
3029        return trk0_obj;
3030    };
3031    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3032        if( !CAL0 )return NULL;
3033        if( !GetYodaEntry( ) ){
3034            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3035            return 0;
3036        }
3037        return calo0_obj;
3038    };
3039    
3040    
3041    /**
3042     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3043     * Given the run ID (...), if needed it query the DB and load the proper file.
3044     * @return Pointer to the tree
3045   */   */
 // TTree* PamLevel2::GetNewPamTree(){  
3046    
 //     if(tree_clone)return tree_clone;  
3047    
3048  //     TTree *Tout = 0;  TTree* PamLevel2::GetYodaTree( ){
3049            
3050  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
3051  //     cout << "Create new PAMELA trees "<<endl;      //===================================
3052        // check if iroot has changed
3053        //===================================
3054        if( irun<0 ){
3055            cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3056    //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
3057            return NULL;
3058        }    
3059        Int_t irootnew = run_obj->ID_ROOT_L0;
3060    //      cout << "iroot    "<<iroot<<endl;
3061    //      cout << "irootnew "<<irootnew<<endl;
3062    
3063        //===================================
3064        // load the level0 file
3065        // (if not already loaded)
3066        //===================================
3067        if( iroot != irootnew || !l0_tree){
3068            iroot = irootnew;
3069            //===================================
3070            // open the DB connection
3071            // (if not already opened)
3072            //===================================
3073            if(!dbc || (dbc && !dbc->IsConnected())){
3074                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3075                cout<<"Connecting to DB"<<endl;
3076                cout<<"HOST "<<host<<endl;
3077                cout<<"USER "<<user<<endl;
3078                cout<<"PSW  "<<psw<<endl;
3079                dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3080                if( !dbc )return NULL;
3081                if( !dbc->IsConnected() )return NULL;    
3082    //          cout<<"...done"<<endl;
3083                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3084            }else{
3085    //          cout<<"DB already connected"<<endl;
3086            }
3087            GL_ROOT glroot = GL_ROOT();
3088            if( glroot.Query_GL_ROOT(iroot,dbc) ){
3089                cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3090                return NULL;
3091            };
3092            TString filename = glroot.PATH + glroot.NAME;
3093            if(l0_file){
3094                l0_file->Close();
3095                l0_file->Delete();
3096            }
3097            cout << "Opening LEVEL0 file: "<< filename << endl;
3098            FileStat_t t;
3099            if( gSystem->GetPathInfo(filename.Data(),t) ){
3100                cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
3101                return NULL;
3102            }
3103            l0_file = new TFile(filename);
3104            if( !l0_file )return NULL;
3105            l0_tree = (TTree*)l0_file->Get("Physics");
3106            if(!h0_obj)h0_obj = new EventHeader();
3107            l0_tree->SetBranchAddress("Header" ,&h0_obj);
3108            prevshift = 0;
3109            //---------------------------------------------------
3110            // TRACKER:
3111            if(TRK0){
3112                if(!trk0_obj){
3113                    trk0_obj = new TrkLevel0();
3114                    trk0_obj->Set();
3115                };
3116                l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
3117            }
3118            //--------------------------------------------------
3119            // CALORIMETER:
3120            if(CAL0){
3121                if(!calo0_obj){
3122                    calo0_obj = new CaloLevel0();
3123                    calo0_obj->Set();
3124                };
3125                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3126                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3127            }
3128            //---------------------------------------------------
3129            // TOF:
3130            if(TOF0){
3131                cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3132            }
3133    
3134        };
3135    
3136        if(!dbc || (dbc && !dbc->IsConnected())){
3137            cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3138        }
3139    
3140        if ( TRK0 ){
3141            TrkParams::Load(6);
3142            if( !TrkParams::IsLoaded(6) ){
3143                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3144            };
3145            TrkParams::SetCalib(run_obj,dbc);
3146            TrkParams::LoadCalib( );
3147            if( !TrkParams::CalibIsLoaded() ){
3148                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3149            };
3150        }
3151    
3152    //    cout << l0_tree << endl;
3153            
3154        return l0_tree;
3155    
3156    }
3157    
3158    /**
3159     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3160     */
3161    Int_t PamLevel2::GetYodaEntry(){
3162    
3163    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3164        if(!GetYodaTree())return 0;
3165            
3166  //     if(TRK1||TRK2||TRKh){      // patch
3167  //      TTree *T = new TTree("Tracker_clone","PAMELA tracker level2 data ");          if( irunentry < 0){
3168  //      if(TRK1) {  //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3169  //          trk1_clone = new TrkLevel1();          irunentry=0LL;
3170  //          T->Branch("TrkLevel1","TrkLevel1", &trk1_clone);      }
3171  //          T->BranchRef();      //  ---------------------------------
3172  //          cout << "Tracker      : branch TrkLevel1"<<endl;      //  if file is NOT a preselected file
3173  //      };      //  ---------------------------------
3174  //      if(TRK2) {      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3175  //          trk2_clone = new TrkLevel2();  //     cout << " irunentry "<<irunentry<<endl;
3176  //          T->Branch("TrkLevel2", "TrkLevel2",&trk2_clone);  //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3177  //          cout << "Tracker      : branch TrkLevel2"<<endl;          //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3178  //      };  
3179  //      if(TRKh) {  //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3180  //          trkh_clone = new TrkHough();  //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3181  //          T->Branch("TrkHough","TrkHough", &trkh_clone);  //     cout << " time "<< GetOrbitalInfo()->absTime << endl;
3182  //          cout << "Tracker      : branch TrkHough"<<endl;  //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3183  //      };      
3184  //      if(!Tout)Tout=T;      if( !GetOrbitalInfo() ){
3185  //      else Tout->AddFriend("Tracker_clone");          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3186  //     }          return 0;
3187        }
3188        if( GetOrbitalInfo()->OBT==0 && GetOrbitalInfo()->pkt_num==0 ){
3189            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3190            return 0;
3191        }
3192    
3193  //     // Calorimeter      // ---------------------------------------------------------------------
3194  //     if(CAL1||CAL2){      // ATTENTION!!!
3195  //      TTree *C = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");          // If data are not pre-processed with cleaner, the level0 tree may contain
3196  //      if(CAL1) {      // spurious nested physics packets.
3197  //          calo1_clone = new CaloLevel1();      // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3198  //          C->Branch("CaloLevel1", "CaloLevel1", &calo1_clone);      // while level2 tree DOES NOT!!
3199  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;      // This means that "quellagiusta" in these cases is not correct.
3200  //      };      // In order to retrieve the correct level0 event, I implemented a check
3201  //      if(CAL2) {      // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3202  //          calo2_clone = new CaloLevel2();      // is shift forward until when the packets match.
3203  //          C->Branch("CaloLevel2","CaloLevel2", &calo2_clone);      // ---------------------------------------------------------------------
3204  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;      Int_t answer = 0;
3205  //      };      Int_t shift =0;
3206  //      if(!Tout)Tout=C;      //    printf(" siamo qui %i %i \n",shift,prevshift);
3207  //      else Tout->AddFriend("Calorimeter_clone");      Int_t maxshift = 10;
3208  //     }          do{
3209            if(shift>0){    
3210  //     // ToF                  cout << " PKTNUM  L2 --- "<< GetOrbitalInfo()->pkt_num << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3211  //     if(TOF) {              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;
3212  //      TTree *O = new TTree("ToF_clone","PAMELA ToF level2 data ");                  cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3213  //      tof_clone = new ToFLevel2();          }
3214  //      O->Branch("ToFLevel2","ToFLevel2", &tof_clone);          answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3215  //      cout << "ToF          : branch ToFLevel2"<<endl;          shift++;
3216  //      if(!Tout)Tout=O;          if( !GetEventHeader() ){
3217  //      else Tout->AddFriend("ToF_clone");              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3218  //     };              return 0;
3219  //     // Trigger          }
3220  //     if(TRG) {  //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< GetOrbitalInfo()->pkt_num << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3221  //      TTree *R = new TTree("Trigger_clone","PAMELA trigger level2 data ");      //      cout << " L2 --- "<< GetOrbitalInfo()->OBT << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3222  //      trig_clone = new TrigLevel2();  //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3223  //      R->Branch("TrigLevel2","TrigLevel2", &trig_clone);  //      cout << " GetOrbitalInfo()->OBT "<< GetOrbitalInfo()->OBT << endl;
3224  //      cout << "Trigger      : branch TrigLevel2"<<endl;  //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3225  //      if(!Tout)Tout=R;  //      cout << " GetOrbitalInfo()->pkt_num "<< GetOrbitalInfo()->pkt_num << endl;
3226  //      else Tout->AddFriend("Trigger_clone");  //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3227  //     };  //      printf(" IDRUN %u \n",GetRunInfo()->ID);
3228  //     // S4  //
3229  //     if(S4) {          if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3230  //      TTree *S = new TTree("S4_clone","PAMELA S4 level2 data ");                  prevshift = 0;
3231  //      s4_clone = new S4Level2();            shift = -1;
3232  //      S->Branch("S4Level2","S4Level2", &s4_clone);          };
 //      cout << "S4           : branch S4Level2"<<endl;  
 //      if(!Tout)Tout=S;  
 //      else Tout->AddFriend("S4_clone");  
 //     };  
 //     // Neutron Detector  
 //     if(ND) {  
 //      TTree *N = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");    
 //      nd_clone = new NDLevel2();  
 //      N->Branch("NDLevel2","NDLevel2", &nd_clone);  
 //      cout << "NeutronD     : branch NDLevel2"<<endl;  
 //      if(!Tout)Tout=N;  
 //      else Tout->AddFriend("NeutronD_clone");  
 //     };  
 //     // Anticounters  
 //     if(AC) {  
 //      TTree *A = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");    
 //      ac_clone = new AcLevel2();  
 //      A->Branch("AcLevel2","AcLevel2", &ac_clone);  
 //      cout << "Anticounter  : branch AcLevel2"<<endl;  
 //      if(!Tout)Tout=A;  
 //      else Tout->AddFriend("Anticounter_clone");  
 //     };  
 //     // OrbitalInfo  
 //     if(ORB) {  
 //      TTree *B = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");      
 //      orb_clone = new OrbitalInfo();  
 //      B->Branch("OrbitalInfo","OrbitalInfo", &orb_clone);  
 //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;  
 //      if(!Tout)Tout=B;  
 //      else Tout->AddFriend("OrbitalInfo_clone");  
 //     };  
 //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
3233    
3234  //     tree_clone = Tout;      }while( ( GetOrbitalInfo()->OBT != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
 //     tree_clone->SetDirectory(0);  
3235    
3236  //     return Tout;      if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3237  // }          cout << " Big trouble here, no such event in Level0 data! " <<endl;
3238  // /**          return 0;
3239  //  * Fill a tree (created with GetNewPamTree)      }
3240  //  *  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3241  //  */  //    return GetYodaTree()->GetEntry(quellagiusta);
3242  // //void PamLevel2::FillNewPamTree(TTree *T){      if ( shift > 1 ) prevshift += (shift-1);
3243  // void PamLevel2::FillNewPamTree(){      
3244                return answer;
3245        
3246    }
3247    /**
3248     * \Brief Set DB connection
3249     */
3250    Bool_t PamLevel2::SetDBConnection(){
3251    
3252  //     if(trk1_clone)  *trk1_clone = *trk1_obj;      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3253  //     if(trk2_clone){      cout<<"Connecting to DB"<<endl;
3254  //      trk2_clone->Clear();      cout<<"HOST "<<host<<endl;
3255  //      trk2_obj->Copy(*trk2_clone);      cout<<"USER "<<user<<endl;
3256  // //   *trk2_clone = *trk2_obj;      cout<<"PSW  "<<psw<<endl;
3257  //     }      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3258  //     if(trkh_clone)  *trkh_clone = *trkh_obj;      if( !dbc )return false;
3259  //     if(calo1_clone){      if( !dbc->IsConnected() )return false;    
3260  // //   *calo1_clone = *calo1_obj;      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3261  //      calo1_clone->Clear();      return true;
 //      calo1_obj->Copy(*calo1_clone);  
 //     }  
 //     if(calo2_clone){  
 // //   *calo2_clone = *calo2_obj;  
 //      calo2_clone->Clear();  
 //      calo2_obj->Copy(*calo2_clone);  
 //     }  
 //     if(tof_clone)   *tof_clone  = *tof_obj;  
 //     if(trig_clone)  *trig_clone = *trig_obj;  
 //     if(s4_clone)    *s4_clone   = *s4_obj;  
 //     if(nd_clone)    *nd_clone   = *nd_obj;  
 //     if(ac_clone)    *ac_clone   = *ac_obj;  
 //     if(orb_clone)   *orb_clone  = *orb_obj;  
   
 //     TTree *T = tree_clone;  
   
 //     T->Fill(); //fill main tree  
 // //    cout<<T->IsA()->GetName()<<" "<<T->GetName()<<endl;  
 //     TList *li = T->GetListOfFriends();  
 //     TIter next(li);  
 //     TFriendElement* T_friend=0;  
 //     while( (T_friend = (TFriendElement*)next()) ){  
 // //   cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;  
 //      T_friend->GetTree()->Fill();//fill friends  
 //     }  
3262    
3263  // }  }

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.42

  ViewVC Help
Powered by ViewVC 1.1.23