/[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.82 by mocchiut, Thu Jun 4 15:10:47 2009 UTC revision 1.102 by mocchiut, Fri Oct 17 16:15:06 2014 UTC
# Line 30  void GPamela::Clear() { Line 30  void GPamela::Clear() {
30  void GPamela::Delete() {  void GPamela::Delete() {
31    
32    Clear();    Clear();
33      /* EM all these are in the stack not in the heap, so no need to delete by hand...
34    delete[] Ipltof;    delete[] Ipltof;
35    delete[] Ipaddle;    delete[] Ipaddle;
36    delete[] Ipartof;    delete[] Ipartof;
# Line 131  void GPamela::Delete() { Line 131  void GPamela::Delete() {
131    delete[] Timecard;    delete[] Timecard;
132    delete[] Pathcard;    delete[] Pathcard;
133    delete[] P0card;    delete[] P0card;
134      */
135  }  }
136  ;  
137    
138  void GPamela::SetBranchAddress(TChain* fhBookTree) {  void GPamela::SetBranchAddress(TChain* fhBookTree) {
139    
# Line 278  ClassImp( GPamela); Line 278  ClassImp( GPamela);
278   */   */
279  PamTrack::PamTrack() {  PamTrack::PamTrack() {
280    trk_track = 0;    trk_track = 0;
281      trk_ext_track = 0;
282    calo_track = 0;    calo_track = 0;
283    tof_track = 0;    tof_track = 0;
284    orb_track = 0;    orb_track = 0;
# Line 295  PamTrack::PamTrack() { Line 296  PamTrack::PamTrack() {
296   */   */
297  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
298    
299      trk_ext_track = 0;
300    trk_track = 0;    trk_track = 0;
301    calo_track = 0;    calo_track = 0;
302    tof_track = 0;    tof_track = 0;
# Line 302  PamTrack::PamTrack(TrkTrack* t, CaloTrkV Line 304  PamTrack::PamTrack(TrkTrack* t, CaloTrkV
304    //     if(t)trk_track  = new TrkTrack(*t);    //     if(t)trk_track  = new TrkTrack(*t);
305    //     if(c)calo_track = new CaloTrkVar(*c);    //     if(c)calo_track = new CaloTrkVar(*c);
306    //     if(o)tof_track  = new ToFTrkVar(*o);    //     if(o)tof_track  = new ToFTrkVar(*o);
307    if (t)  //   if (t)
308      trk_track = t;  //     trk_track = t;
309    //   if (c)
310    //     calo_track = c;
311    //   if (o)
312    //     tof_track = o;
313    //   if (r)
314    //     orb_track = r;
315    
316    //   candeleteobj = 0;
317    
318    
319      if (t){
320          trk_track = new TrkTrack(*t);
321          trk_ext_track = new ExtTrack(*t);//NB!! ha dimensione 6 invece che 8
322      }
323    if (c)    if (c)
324      calo_track = c;      calo_track = new CaloTrkVar(*c);
325    if (o)    if (o)
326      tof_track = o;      tof_track = new ToFTrkVar(*o);
327    if (r)    if (r)
328      orb_track = r;      orb_track = new OrbitalInfoTrkVar(*r);
329      candeleteobj = 1;
330    
331    candeleteobj = 0;  }
332    ;
333    /**
334     * Constructor
335     */
336    PamTrack::PamTrack(ExtTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
337    
338      trk_ext_track = 0;
339      trk_track = 0;
340      calo_track = 0;
341      tof_track = 0;
342      orb_track = 0;
343      //     if(t)trk_track  = new TrkTrack(*t);
344      //     if(c)calo_track = new CaloTrkVar(*c);
345      //     if(o)tof_track  = new ToFTrkVar(*o);
346    //   if (t)
347    //     trk_track = t;
348    //   if (c)
349    //     calo_track = c;
350    //   if (o)
351    //     tof_track = o;
352    //   if (r)
353    //     orb_track = r;
354    
355    //   candeleteobj = 0;
356    
357    
358      if (t){
359    ////      trk_track = new TrkTrack(*t);//in this case TrkTrack object remains null
360          trk_ext_track = new ExtTrack(*t);
361      }
362      if (c)
363        calo_track = new CaloTrkVar(*c);
364      if (o)
365        tof_track = new ToFTrkVar(*o);
366      if (r)
367        orb_track = new OrbitalInfoTrkVar(*r);
368      candeleteobj = 1;
369      pscore = 0;
370      iscore = 0;
371    
372  }  }
373  ;  ;
374  PamTrack::PamTrack(const PamTrack& track) {  PamTrack::PamTrack(const PamTrack& track) {
375    
376    TrkTrack *t = track.trk_track;    TrkTrack *t = track.trk_track;
377      ExtTrack *e = track.trk_ext_track;
378    CaloTrkVar *c = track.calo_track;    CaloTrkVar *c = track.calo_track;
379    ToFTrkVar *o = track.tof_track;    ToFTrkVar *o = track.tof_track;
380    OrbitalInfoTrkVar *r = track.orb_track;    OrbitalInfoTrkVar *r = track.orb_track;
381    
382    trk_track = 0;    trk_ext_track = 0;
383    calo_track = 0;    trk_track     = 0;
384    tof_track = 0;    calo_track    = 0;
385    orb_track = 0;    tof_track     = 0;
386      orb_track     = 0;
387      if(e)
388          trk_ext_track = new ExtTrack(*e);
389    if (t)    if (t)
390      trk_track = new TrkTrack(*t);      trk_track = new TrkTrack(*t);
391    if (c)    if (c)
# Line 334  PamTrack::PamTrack(const PamTrack& track Line 394  PamTrack::PamTrack(const PamTrack& track
394      tof_track = new ToFTrkVar(*o);      tof_track = new ToFTrkVar(*o);
395    if (r)    if (r)
396      orb_track = new OrbitalInfoTrkVar(*r);      orb_track = new OrbitalInfoTrkVar(*r);
397    
398    candeleteobj = 1;    candeleteobj = 1;
399      pscore = 0;
400      iscore = 0;
401    
402  }  }
403  void PamTrack::Clear() {  void PamTrack::Clear(Option_t *option) {
404    
405    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;  //    cout << "PamTrack::Clear( "<<option<<" ) "<<candeleteobj<<endl;
406    if (candeleteobj) {    if (candeleteobj) {
407          
408        if (trk_ext_track)
409          trk_ext_track->ExtTrack::Clear(option);
410      if (trk_track)      if (trk_track)
411        trk_track->TrkTrack::Clear();        trk_track->TrkTrack::Clear();
412      if (calo_track)      if (calo_track)
# Line 351  void PamTrack::Clear() { Line 417  void PamTrack::Clear() {
417        orb_track->OrbitalInfoTrkVar::Clear();//???        orb_track->OrbitalInfoTrkVar::Clear();//???
418    }    }
419    else {    else {
420        trk_ext_track = 0;
421      trk_track = 0;      trk_track = 0;
422      calo_track = 0;      calo_track = 0;
423      tof_track = 0;      tof_track = 0;
# Line 363  void PamTrack::Clear() { Line 430  void PamTrack::Clear() {
430  void PamTrack::Delete() {  void PamTrack::Delete() {
431    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
432    if (candeleteobj) {    if (candeleteobj) {
433        if (trk_ext_track) {
434          trk_ext_track->ExtTrack::Clear("C");
435          delete trk_ext_track;
436        }
437      if (trk_track) {      if (trk_track) {
438        trk_track->TrkTrack::Clear();        trk_track->TrkTrack::Clear();
439        delete trk_track;        delete trk_track;
# Line 384  void PamTrack::Delete() { Line 455  void PamTrack::Delete() {
455      Clear();      Clear();
456    }    }
457  }  }
458    
459    
460    
461    
462  //--------------------------------------  //--------------------------------------
463  //  //
464  //  //
# Line 394  void PamTrack::Delete() { Line 469  void PamTrack::Delete() {
469  PamLevel2::PamLevel2() {  PamLevel2::PamLevel2() {
470    Initialize();    Initialize();
471  }  }
472  ;  
473    
474  /**  /**
475   * Constructor   * Constructor
# Line 417  PamLevel2::PamLevel2(TString ddir, TStri Line 492  PamLevel2::PamLevel2(TString ddir, TStri
492      GetPamTree(listf, detlist);      GetPamTree(listf, detlist);
493    if (listf)    if (listf)
494      GetRunTree(listf);      GetRunTree(listf);
495      SetMaxShift(-1);
496  }  }
497  ;  
498    
499  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
500    Initialize();    Initialize();
501    GetPamTree(llist, detlist);    GetPamTree(llist, detlist);
502    GetRunTree(llist);    GetRunTree(llist);
503      SetMaxShift(-1);
504  }  }
505  ;  
506  /**  /**
507   * Constructor   * Constructor
508   * @param ddir Name of directory where level2 files are stored.   * @param ddir Name of directory where level2 files are stored.
# Line 435  PamLevel2::PamLevel2(TString ddir, TList Line 512  PamLevel2::PamLevel2(TString ddir, TList
512   */   */
513  PamLevel2::PamLevel2(TString ddir, TString llist) {  PamLevel2::PamLevel2(TString ddir, TString llist) {
514    Initialize();    Initialize();
515    TList* listf = GetListOfLevel2Files(ddir, llist);    TList* listf = GetListOfLevel2Files(ddir, llist);
516      cout << "GetPamTree: "<<endl;
517    GetPamTree(listf, "");    GetPamTree(listf, "");
518      cout << "GetRunTree: "<<endl;
519    GetRunTree(listf);    GetRunTree(listf);
520      SetMaxShift(-1);
521  }  }
522  ;  
523    
524  void PamLevel2::Initialize() {  void PamLevel2::Initialize() {
525    
# Line 452  void PamLevel2::Initialize() { Line 532  void PamLevel2::Initialize() {
532    trkh_obj = 0;    trkh_obj = 0;
533    calo1_obj = 0;    calo1_obj = 0;
534    calo2_obj = 0;    calo2_obj = 0;
535    tof_obj = 0;    tof2_obj = 0;
536    trig_obj = 0;    trig_obj = 0;
537    s4_obj = 0;    s4_obj = 0;
538    nd_obj = 0;    nd_obj = 0;
539    ac_obj = 0;    ac_obj = 0;
540    orb_obj = 0;    orb2_obj = 0;
541    gp_obj = 0;    gp_obj = 0;
542    
543      extAlgFlag=0;
544    
545      trk_ext_obj     = 0;
546      trk_ext_nuc_obj = 0;
547      trk_nuc_obj     = 0;
548      
549      calo_ext_obj     = 0;
550      calo_ext_nuc_obj = 0;
551      calo_nuc_obj     = 0;
552      
553      tof_ext_obj     = 0;
554      tof_ext_nuc_obj = 0;
555      tof_nuc_obj     = 0;
556      
557      orb_ext_obj     = 0;
558      orb_ext_nuc_obj = 0;
559      orb_nuc_obj     = 0;
560    
561      trk2_nuc_obj  = 0;
562      calo2_nuc_obj = 0;
563      tof2_nuc_obj  = 0;
564      orb2_nuc_obj  = 0;
565    
566    
567    run_obj = 0;//new GL_RUN();    run_obj = 0;//new GL_RUN();
568    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
569      proc_obj = 0;// Emiliano
570    irun = -1LL;    irun = -1LL;
571    irunt = -1LL;    irunt = -1LL;
572      totrunentry = 0LL;
573      totrunentrymax = 0LL;
574      totrunentrymin = 0LL;
575    runfirstentry = 0LL;    runfirstentry = 0LL;
576    runlastentry = 0LL;    runlastentry = 0LL;
577    gltsync = 0; // Emiliano    gltsync = 0; // Emiliano
578      fUpdateRunInfo = true; // Emiliano
579      fUseDBinRunInfo = true; // Emiliano
580      fDiscarded = false; //EM
581      isSync = false; // by default assume that the level2 file(s) is(are) not sinchronized between L0/DB and L2, that is we miss some packets in L2 due to nested/DV-skipped events
582      il0entry = 0LL;
583      //  hasL0EE = true;
584    
585    l0_file = NULL;    l0_file = NULL;
586    l0_tree = NULL;    l0_tree = NULL;
# Line 474  void PamLevel2::Initialize() { Line 588  void PamLevel2::Initialize() {
588    dbc = 0;    dbc = 0;
589    
590    prevshift = 0;    prevshift = 0;
591    maxshift = 10; //EMILIANO    yprevshift = 0;
592      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
593    
594    run_tree = NULL;    run_tree = NULL;
595    run_tree_clone = NULL;    run_tree_clone = NULL;
596    
597      proc_tree = NULL;
598      proc_tree_clone = NULL;
599    
600    sel_tree = NULL;    sel_tree = NULL;
601    sel_tree_clone = NULL;    sel_tree_clone = NULL;
602    
# Line 525  void PamLevel2::Initialize() { Line 644  void PamLevel2::Initialize() {
644    ND = true;    ND = true;
645    AC = true;    AC = true;
646    ORB = true;    ORB = true;
647      PROC = true;
648    GP = false;    GP = false;
649    
650      EXT = false;
651      NUC = false;
652      trkAlg = "STD";//default tracking algorythm
653    
654    RUN = true;    RUN = true;
655    
656    SELLI = -1;    SELLI = -1;
# Line 537  void PamLevel2::Initialize() { Line 661  void PamLevel2::Initialize() {
661    
662    tsorted = 0;    tsorted = 0;
663    timage = 0;    timage = 0;
664      text = 0 ;
665    
666      tsorted_nuc = 0;
667      timage_nuc = 0;
668      text_nuc = 0 ;
669    
670    howtosort = "+CAL+TOF";    howtosort = "+CAL+TOF";
671    //howtosort = "+TOF";    //howtosort = "+TOF";
# Line 544  void PamLevel2::Initialize() { Line 673  void PamLevel2::Initialize() {
673    
674    issorted = false;    issorted = false;
675    lastsorted = -1;    lastsorted = -1;
676      issorted_new = false;
677      lastsorted_new = -1;
678    
679  }  }
680  ;  ;
# Line 556  void PamLevel2::Delete() { Line 687  void PamLevel2::Delete() {
687      delete run_obj;      delete run_obj;
688    if (soft_obj)    if (soft_obj)
689      delete soft_obj; //Emiliano      delete soft_obj; //Emiliano
690      if (proc_obj)
691        delete proc_obj; //Emiliano
692    
693    //    cout << "void PamLevel2::Clear()"<<endl;    //    cout << "void PamLevel2::Clear()"<<endl;
694    if (h0_obj)    if (h0_obj)
# Line 574  void PamLevel2::Delete() { Line 707  void PamLevel2::Delete() {
707      delete calo1_obj;      delete calo1_obj;
708    if (calo2_obj)    if (calo2_obj)
709      delete calo2_obj;      delete calo2_obj;
710    if (tof_obj)    if (tof2_obj)
711      delete tof_obj;      delete tof2_obj;
712    if (trig_obj)    if (trig_obj)
713      delete trig_obj;      delete trig_obj;
714    if (s4_obj)    if (s4_obj)
# Line 584  void PamLevel2::Delete() { Line 717  void PamLevel2::Delete() {
717      delete nd_obj;      delete nd_obj;
718    if (ac_obj)    if (ac_obj)
719      delete ac_obj;      delete ac_obj;
720    if (orb_obj)    if (orb2_obj)
721      delete orb_obj;      delete orb2_obj;
722    if (gp_obj)    if (gp_obj)
723      delete gp_obj;      delete gp_obj;
724    
725      if(trk_nuc_obj)trk_nuc_obj->Delete();
726      if(trk_ext_obj)trk_ext_obj->Delete();
727      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Delete();
728    
729      if(calo_nuc_obj)calo_nuc_obj->Delete();
730      if(calo_ext_obj)calo_ext_obj->Delete();
731      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Delete();
732    
733      if(tof_nuc_obj)tof_nuc_obj->Delete();
734      if(tof_ext_obj)tof_ext_obj->Delete();
735      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Delete();
736    
737      if(orb_nuc_obj)orb_nuc_obj->Delete();
738      if(orb_ext_obj)orb_ext_obj->Delete();
739      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Delete();
740    
741    
742      if(trk2_nuc_obj)trk2_nuc_obj->Delete();;
743      if( calo2_nuc_obj)calo2_nuc_obj->Delete();;
744      if(tof2_nuc_obj)tof2_nuc_obj->Delete();;
745      if(orb2_nuc_obj)orb2_nuc_obj->Delete();;
746      
747    
748    
749    if (tsorted) {    if (tsorted) {
750      tsorted->Delete();      tsorted->Delete();
751      delete tsorted;      delete tsorted;
# Line 597  void PamLevel2::Delete() { Line 754  void PamLevel2::Delete() {
754      timage->Delete();      timage->Delete();
755      delete timage;      delete timage;
756    }    }
757      if (text) {
758        text->Delete();
759        delete text;
760      }
761      if (tsorted_nuc) {
762        tsorted_nuc->Delete();
763        delete tsorted_nuc;
764      }
765      if (timage_nuc) {
766        timage_nuc->Delete();
767        delete timage_nuc;
768      }
769      if (text_nuc) {
770        text_nuc->Delete();
771        delete text_nuc;
772      }
773    
774    
775    
776    if (dbc) {    if (dbc) {
777      dbc->Close();      dbc->Close();
# Line 633  void PamLevel2::Delete() { Line 808  void PamLevel2::Delete() {
808      //      //
809      TIter next2(contents);      TIter next2(contents);
810      TChain *questa = 0;      TChain *questa = 0;
811      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*)next2()) ) {
812        TString name = questa->GetName();        TString name = questa->GetName();
813        questa->Delete();        questa->Delete();
814        questa = NULL;        questa = NULL;
# Line 646  void PamLevel2::Delete() { Line 821  void PamLevel2::Delete() {
821      run_tree->Delete();;      run_tree->Delete();;
822    if (sel_tree)    if (sel_tree)
823      sel_tree->Delete();;      sel_tree->Delete();;
824    for (Int_t i = 0; i < NCLONES; i++)  
825      // The following lines are commented out since they may generate a double delete error
826      // if the file containing the clone trees is closed. This is because the file owns the
827      // clone trees which are written into it, so it will delete them when it is closed; if
828      // also PamLevel2 will try to delete these trees, a double delete error will be generated
829      // when exiting from analysis program. (Nicola 28/11/2011)
830    
831      /*for (Int_t i = 0; i < NCLONES; i++)
832      if (pam_tree_clone[i])      if (pam_tree_clone[i])
833        pam_tree_clone[i]->Delete();;        pam_tree_clone[i]->Delete();;
834    if (run_tree_clone)    if (run_tree_clone)
835      run_tree_clone->Delete();;      run_tree_clone->Delete();;
836    if (sel_tree_clone)    if (sel_tree_clone)
837      sel_tree_clone->Delete();;      sel_tree_clone->Delete();;*/
838    
839    if (irunoffset)    if (irunoffset)
840      delete[] irunoffset;      delete[] irunoffset;
841    
842    
843      Initialize();
844    
845  }  }
846  ;  ;
847    
# Line 690  void PamLevel2::Clear() { Line 875  void PamLevel2::Clear() {
875      calo1_obj->Clear();      calo1_obj->Clear();
876    if (calo2_obj)    if (calo2_obj)
877      calo2_obj->Clear();      calo2_obj->Clear();
878    if (tof_obj)    if (tof2_obj)
879      tof_obj->Clear();      tof2_obj->Clear();
880    if (trig_obj)    if (trig_obj)
881      trig_obj->Clear();      trig_obj->Clear();
882    if (s4_obj)    if (s4_obj)
# Line 700  void PamLevel2::Clear() { Line 885  void PamLevel2::Clear() {
885      nd_obj->Clear();      nd_obj->Clear();
886    if (ac_obj)    if (ac_obj)
887      ac_obj->Clear();      ac_obj->Clear();
888    if (orb_obj)    if (orb2_obj)
889      orb_obj->Clear();      orb2_obj->Clear();
890    if (gp_obj)    if (gp_obj)
891      gp_obj->Clear();      gp_obj->Clear();
892      if (proc_obj)
893        proc_obj->Clear();
894    
895    //    if(sorted_tracks)sorted_tracks->Clear();    //    if(sorted_tracks)sorted_tracks->Clear();
896    //    sorted_tracks.Clear();    //    sorted_tracks.Clear();
897    
898    if (tsorted) {    if(trk_nuc_obj)trk_nuc_obj->Clear();
899      tsorted->Delete();    if(trk_ext_obj)trk_ext_obj->Clear();
900    }    if(trk_ext_nuc_obj)trk_ext_nuc_obj->Clear();
901    if (timage) {  
902      timage->Delete();    if(calo_nuc_obj)calo_nuc_obj->Clear();
903    }    if(calo_ext_obj)calo_ext_obj->Clear();
904      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Clear();
905    
906      if(tof_nuc_obj)tof_nuc_obj->Clear();
907      if(tof_ext_obj)tof_ext_obj->Clear();
908      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Clear();
909    
910      if(orb_nuc_obj)orb_nuc_obj->Clear();
911      if(orb_ext_obj)orb_ext_obj->Clear();
912      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Clear();
913    
914      if(trk2_nuc_obj)trk2_nuc_obj->Clear();;
915      if( calo2_nuc_obj)calo2_nuc_obj->Clear();;
916      if(tof2_nuc_obj)tof2_nuc_obj->Clear();;
917      if(orb2_nuc_obj)orb2_nuc_obj->Clear();;
918    
919      if (tsorted)tsorted->Delete();
920      if (timage)timage->Delete();
921      if (text) text->Delete();
922    
923      if (tsorted_nuc)tsorted_nuc->Delete();
924      if (timage_nuc)timage_nuc->Delete();
925      if (text_nuc) text_nuc->Delete();
926  }  }
927  ;  ;
928    
# Line 747  void PamLevel2::Reset() { Line 956  void PamLevel2::Reset() {
956      //      //
957      TIter next2(contents);      TIter next2(contents);
958      TChain *questa = 0;      TChain *questa = 0;
959      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*) next2()) ) {
960        TString name = questa->GetName();        TString name = questa->GetName();
961        questa->Delete();        questa->Delete();
962        questa = NULL;        questa = NULL;
# Line 762  void PamLevel2::Reset() { Line 971  void PamLevel2::Reset() {
971    if (sel_tree)    if (sel_tree)
972      sel_tree->Delete();;      sel_tree->Delete();;
973    sel_tree = NULL;    sel_tree = NULL;
974    
975      if (proc_tree)
976        proc_tree->Delete();
977      proc_tree = NULL;
978    //    //
979    // Close file    // Close file
980    //    //
# Line 778  void PamLevel2::Reset() { Line 991  void PamLevel2::Reset() {
991    trkh_obj = 0;    trkh_obj = 0;
992    calo1_obj = 0;    calo1_obj = 0;
993    calo2_obj = 0;    calo2_obj = 0;
994    tof_obj = 0;    tof2_obj = 0;
995    trig_obj = 0;    trig_obj = 0;
996    s4_obj = 0;    s4_obj = 0;
997    nd_obj = 0;    nd_obj = 0;
998    ac_obj = 0;    ac_obj = 0;
999    orb_obj = 0;    orb2_obj = 0;
1000    gp_obj = 0;    gp_obj = 0;
1001      proc_obj = 0;
1002    
1003      trk_ext_obj     = 0;
1004      trk_ext_nuc_obj = 0;
1005      trk_nuc_obj     = 0;
1006      
1007      calo_ext_obj     = 0;
1008      calo_ext_nuc_obj = 0;
1009      calo_nuc_obj     = 0;
1010      
1011      tof_ext_obj     = 0;
1012      tof_ext_nuc_obj = 0;
1013      tof_nuc_obj     = 0;
1014      
1015      orb_ext_obj     = 0;
1016      orb_ext_nuc_obj = 0;
1017      orb_nuc_obj     = 0;
1018    
1019      trk2_nuc_obj  = 0;
1020      calo2_nuc_obj = 0;
1021      tof2_nuc_obj  = 0;
1022      orb2_nuc_obj  = 0;
1023    
1024      trk2_nuc_obj  = 0;
1025      calo2_nuc_obj = 0;
1026      tof2_nuc_obj  = 0;
1027      orb2_nuc_obj  = 0;
1028    //    //
1029    // Reset run pointers    // Reset run pointers
1030    //    //
1031    run_obj = 0;//new GL_RUN();    run_obj = 0;//new GL_RUN();
1032    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
1033      proc_obj = 0;// Emiliano
1034    irun = -1;    irun = -1;
1035    irunt = -1;    irunt = -1;
1036      totrunentry = 0LL;
1037      totrunentrymax = 0LL;
1038      totrunentrymin = 0LL;
1039    runfirstentry = 0ULL;    runfirstentry = 0ULL;
1040    runlastentry = 0ULL;    runlastentry = 0ULL;
1041      prevabstime = 0ULL;
1042      prevpktnum = 0;
1043      abstime = 0ULL;
1044      pktnum = 0;
1045      isFragment = false;
1046    //    //
1047    totdltime[0] = 0LL;    totdltime[0] = 0LL;
1048    totdltime[1] = 0LL;    totdltime[1] = 0LL;
# Line 811  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1060  Bool_t PamLevel2::IsGood(Bool_t strict)
1060    if (strict) {    if (strict) {
1061      if (trk2_obj && trk2_obj->UnpackError() != 0)      if (trk2_obj && trk2_obj->UnpackError() != 0)
1062        goodev = false;        goodev = false;
1063      if (tof_obj && tof_obj->unpackError != 0)      if (tof2_obj && tof2_obj->unpackError != 0)
1064        goodev = false;        goodev = false;
1065      if (trig_obj && trig_obj->unpackError != 0)      if (trig_obj && trig_obj->unpackError != 0)
1066        goodev = false;        goodev = false;
# Line 821  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1070  Bool_t PamLevel2::IsGood(Bool_t strict)
1070        goodev = false;        goodev = false;
1071      if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))      if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
1072        goodev = false;        goodev = false;
1073      //  if(orb_obj)      //  if(orb2_obj)
1074    }    }
1075    else {    else {
1076      if (nd_obj && nd_obj->unpackError != 0)      if (nd_obj && nd_obj->unpackError != 0)
# Line 833  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1082  Bool_t PamLevel2::IsGood(Bool_t strict)
1082  }  }
1083  ;  ;
1084    
1085    void PamLevel2::SkipRunInfoUpdate(){
1086      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
1087      fUpdateRunInfo = false;
1088      this->SetSELLI(2);
1089      printf(" ===============> W A R N I N G <================ \n");
1090      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
1091      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
1092      if ( run_tree_clone ){
1093        printf(" ===============> W A R N I N G <================ \n");
1094        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
1095        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
1096      };
1097    }
1098    
1099    void PamLevel2::SetMaxShift(Int_t sh){
1100      if ( sh >=  0 ){
1101        printf("PamLevel2::SetMaxShift(Int_t) --WARNING-- the default is optimized by checking the level2 file\n it is strongly suggested to let PamLevel2 choose the max shift!\n");
1102        maxshift = sh;
1103      } else {
1104        ULong64_t nev = GetEntries();
1105        ULong64_t runnev = 0ULL;
1106        for (Int_t r=0; r< run_tree->GetEntries();r++){
1107          run_tree->GetEntry(r);//update runinfo
1108          runnev += GetRunInfo()->NEVENTS;
1109        }
1110        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
1111        if ( (runnev-nev) == 0 ) isSync = true;
1112        if (DBG) printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift);
1113        //    printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift); // TOGLITOGLI
1114      }
1115    }
1116    
1117  //--------------------------------------  //--------------------------------------
1118  //  //
1119  //  //
# Line 875  void *PamLevel2::GetPointerTo(const char Line 1156  void *PamLevel2::GetPointerTo(const char
1156      return &calo2_obj;      return &calo2_obj;
1157    };    };
1158    if (!objname.CompareTo("ToFLevel2")) {    if (!objname.CompareTo("ToFLevel2")) {
1159      if (!tof_obj) {      if (!tof2_obj) {
1160        tof_obj = new ToFLevel2();        tof2_obj = new ToFLevel2();
1161        tof_obj->Set();        tof2_obj->Set();
1162      }      }
1163      return &tof_obj;      return &tof2_obj;
1164    };    };
1165    if (!objname.CompareTo("TrigLevel2")) {    if (!objname.CompareTo("TrigLevel2")) {
1166      if (!trig_obj)      if (!trig_obj)
# Line 902  void *PamLevel2::GetPointerTo(const char Line 1183  void *PamLevel2::GetPointerTo(const char
1183      return &ac_obj;      return &ac_obj;
1184    };    };
1185    if (!objname.CompareTo("OrbitalInfo")) {    if (!objname.CompareTo("OrbitalInfo")) {
1186      if (!orb_obj) {      if (!orb2_obj) {
1187        orb_obj = new OrbitalInfo();        orb2_obj = new OrbitalInfo();
1188        orb_obj->Set();        orb2_obj->Set();
1189      }      }
1190      return &orb_obj;      return &orb2_obj;
1191    };    };
1192    //     if(!objname.CompareTo("OrbitalInfo")){    //     if(!objname.CompareTo("OrbitalInfo")){
1193    //    if(!orb_obj)   orb_obj   = new OrbitalInfo();    //    if(!orb2_obj)   orb2_obj   = new OrbitalInfo();
1194    //    return &orb_obj;    //    return &orb2_obj;
1195    //     };    //     };
1196    if (!objname.CompareTo("GPamela")) {    if (!objname.CompareTo("GPamela")) {
1197      if (!gp_obj)      if (!gp_obj)
# Line 924  void *PamLevel2::GetPointerTo(const char Line 1205  void *PamLevel2::GetPointerTo(const char
1205    if (!objname.CompareTo("SoftInfo"))    if (!objname.CompareTo("SoftInfo"))
1206      return &soft_obj; // Emiliano      return &soft_obj; // Emiliano
1207    
1208      if (!objname.CompareTo("ProcInfo")){
1209        if (!proc_obj)
1210          proc_obj = new ProcInfo();    
1211        return &proc_obj; // Emiliano
1212      }
1213    
1214    return NULL;    return NULL;
1215  }  }
1216  ;  ;
# Line 974  CaloTrkVar *PamLevel2::GetCaloStoredTrac Line 1261  CaloTrkVar *PamLevel2::GetCaloStoredTrac
1261   */   */
1262  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
1263    
1264    if (!tof_obj)    if (!tof2_obj)
1265      return 0;      return 0;
1266    
1267    if (tof_obj->ToFLevel2::ntrk() == 0) {    if (tof2_obj->ToFLevel2::ntrk() == 0) {
1268      cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"      cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
1269          << endl;          << endl;
1270      return NULL;      return NULL;
# Line 987  ToFTrkVar *PamLevel2::GetToFStoredTrack( Line 1274  ToFTrkVar *PamLevel2::GetToFStoredTrack(
1274    Int_t it_tof = 0;    Int_t it_tof = 0;
1275    
1276    do {    do {
1277      c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);      c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof);
1278      it_tof++;      it_tof++;
1279    } while (c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());    } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk());
1280    
1281    if (!c || seqno != c->trkseqno) {    if (!c || seqno != c->trkseqno) {
1282      c = 0;      c = 0;
# Line 1012  ToFTrkVar *PamLevel2::GetToFStoredTrack( Line 1299  ToFTrkVar *PamLevel2::GetToFStoredTrack(
1299   */   */
1300  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
1301    
1302    if (!orb_obj)    if (!orb2_obj)
1303      return 0;      return 0;
1304    
1305    if (orb_obj->OrbitalInfo::ntrk() == 0) {    if (orb2_obj->OrbitalInfo::ntrk() == 0) {
1306      //       // TRICK BEGIN      //       // TRICK BEGIN
1307      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1308      //       Int_t nn = 0;      //       Int_t nn = 0;
1309      //       TClonesArray &tor = *orb_obj->OrbitalInfoTrk;      //       TClonesArray &tor = *orb2_obj->OrbitalInfoTrk;
1310      //       for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){      //       for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){
1311      //  //      //  //
1312      //  ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt);      //  ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt);
1313      //  if ( ptt->trkseqno != -1  ){      //  if ( ptt->trkseqno != -1  ){
1314      //    //      //    //
1315      //    r->trkseqno = ptt->trkseqno;      //    r->trkseqno = ptt->trkseqno;
# Line 1044  OrbitalInfoTrkVar *PamLevel2::GetOrbital Line 1331  OrbitalInfoTrkVar *PamLevel2::GetOrbital
1331      //       };      //       };
1332      //       delete r;      //       delete r;
1333      //       OrbitalInfoTrkVar *c = 0;      //       OrbitalInfoTrkVar *c = 0;
1334      //       c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);      //       c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1335      //       return c;      //       return c;
1336      //       //TRICK END      //       //TRICK END
1337      cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno      cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
# Line 1056  OrbitalInfoTrkVar *PamLevel2::GetOrbital Line 1343  OrbitalInfoTrkVar *PamLevel2::GetOrbital
1343    Int_t it_tof = 0;    Int_t it_tof = 0;
1344    
1345    do {    do {
1346      c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);      c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1347      it_tof++;      it_tof++;
1348    } while (c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());    } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk());
1349    
1350    if (!c || seqno != c->trkseqno) {    if (!c || seqno != c->trkseqno) {
1351      c = 0;      c = 0;
# Line 1189  void PamLevel2::SortTracks() { Line 1476  void PamLevel2::SortTracks() {
1476    // create TCloneArrays to store tracks and its images    // create TCloneArrays to store tracks and its images
1477    if (!tsorted)    if (!tsorted)
1478      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1479    tsorted->Delete();    tsorted->Clear("C+C");//Delete();
1480    TClonesArray &ttsorted = *tsorted;    TClonesArray &ttsorted = *tsorted;
1481    
1482    if (!timage)    if (!timage)
1483      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1484    timage->Delete();    timage->Clear("C+C");//Delete();
1485    TClonesArray &ttimage = *timage;    TClonesArray &ttimage = *timage;
1486    
1487    
1488    
1489    //--------------------------------------------------    //--------------------------------------------------
1490    // retrieve sorting method    // retrieve sorting method
1491    //--------------------------------------------------    //--------------------------------------------------
# Line 1312  void PamLevel2::SortTracks() { Line 1601  void PamLevel2::SortTracks() {
1601        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1602        // check the number of hit pmts along the track        // check the number of hit pmts along the track
1603        // on S12 S21 and S32, where paddles are parallel to Y axis        // on S12 S21 and S32, where paddles are parallel to Y axis
1604        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof_obj) {        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) {
1605          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1606          return;          return;
1607        };        };
# Line 1329  void PamLevel2::SortTracks() { Line 1618  void PamLevel2::SortTracks() {
1618          //          //
1619          Float_t sen = 0.;          Float_t sen = 0.;
1620          for (Int_t ih = 0; ih < op->npmtadc; ih++) {          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1621            Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));            Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1622            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1623              sen += (op->dedx).At(ih);              sen += (op->dedx).At(ih);
1624          };          };
1625          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1626            Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));            Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1627            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1628              sen += (oi->dedx).At(ih);              sen += (oi->dedx).At(ih);
1629          };          };
# Line 1347  void PamLevel2::SortTracks() { Line 1636  void PamLevel2::SortTracks() {
1636            Int_t nz = 6;            Int_t nz = 6;
1637            Float_t zin[6]; // << define TOF z-coordinates            Float_t zin[6]; // << define TOF z-coordinates
1638            for (Int_t ip = 0; ip < nz; ip++)            for (Int_t ip = 0; ip < nz; ip++)
1639              zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates              zin[ip] = tof2_obj->ToFLevel2::GetZTOF(tof2_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1640            Trajectory *tr = new Trajectory(nz, zin);            Trajectory *tr = new Trajectory(nz, zin);
1641            //            //
1642            Int_t nphit_p = 0;            Int_t nphit_p = 0;
# Line 1356  void PamLevel2::SortTracks() { Line 1645  void PamLevel2::SortTracks() {
1645            Float_t enhit_i = 0.;            Float_t enhit_i = 0.;
1646            //            //
1647            for (Int_t ih = 0; ih < op->npmtadc; ih++) {            for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1648              Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1649              if (pl == 1 || pl == 2 || pl == 5) {              if (pl == 1 || pl == 2 || pl == 5) {
1650                nphit_p++;                nphit_p++;
1651                enhit_p += (op->dedx).At(ih);                enhit_p += (op->dedx).At(ih);
# Line 1367  void PamLevel2::SortTracks() { Line 1656  void PamLevel2::SortTracks() {
1656            //            //
1657            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1658              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1659                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1660                if (pl == 0) {                if (pl == 0) {
1661                  nphit_p++;                  nphit_p++;
1662                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1376  void PamLevel2::SortTracks() { Line 1665  void PamLevel2::SortTracks() {
1665            };            };
1666            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1667              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1668                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1669                if (pl == 3) {                if (pl == 3) {
1670                  nphit_p++;                  nphit_p++;
1671                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1385  void PamLevel2::SortTracks() { Line 1674  void PamLevel2::SortTracks() {
1674            };            };
1675            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1676              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1677                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1678                if (pl == 4) {                if (pl == 4) {
1679                  nphit_p++;                  nphit_p++;
1680                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1394  void PamLevel2::SortTracks() { Line 1683  void PamLevel2::SortTracks() {
1683            };            };
1684    
1685            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1686              Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1687              if (pl == 1 || pl == 2 || pl == 5) {              if (pl == 1 || pl == 2 || pl == 5) {
1688                nphit_i++;                nphit_i++;
1689                enhit_i += (op->dedx).At(ih);                enhit_i += (op->dedx).At(ih);
# Line 1405  void PamLevel2::SortTracks() { Line 1694  void PamLevel2::SortTracks() {
1694            //            //
1695            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1696              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1697                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1698                if (pl == 0) {                if (pl == 0) {
1699                  nphit_i++;                  nphit_i++;
1700                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1414  void PamLevel2::SortTracks() { Line 1703  void PamLevel2::SortTracks() {
1703            };            };
1704            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1705              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1706                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1707                if (pl == 3) {                if (pl == 3) {
1708                  nphit_i++;                  nphit_i++;
1709                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1423  void PamLevel2::SortTracks() { Line 1712  void PamLevel2::SortTracks() {
1712            };            };
1713            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1714              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1715                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1716                if (pl == 4) {                if (pl == 4) {
1717                  nphit_i++;                  nphit_i++;
1718                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1467  void PamLevel2::SortTracks() { Line 1756  void PamLevel2::SortTracks() {
1756             cout << "image: npmttdc "<< oi->npmttdc << endl;*/             cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1757    
1758            //      for (Int_t ih=0; ih < op->npmtadc; ih++){            //      for (Int_t ih=0; ih < op->npmtadc; ih++){
1759            //        Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            //        Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1760            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1761            //      };            //      };
1762    
1763            //      for (Int_t ih=0; ih < oi->npmtadc; ih++){            //      for (Int_t ih=0; ih < oi->npmtadc; ih++){
1764            //        Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            //        Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1765            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1766            //      };            //      };
1767            // --- modified to count tdc signals (more efficient?)            // --- modified to count tdc signals (more efficient?)
1768            // --- and to implement check on tdcflag            // --- and to implement check on tdcflag
1769            for (Int_t ih = 0; ih < op->npmttdc; ih++) {            for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1770              Int_t pl = tof_obj->GetPlaneIndex((op->pmttdc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih));
1771              //      if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;              //      if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1772              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1773                  || (use_S3 && (pl == 4 || pl == 5))) {                  || (use_S3 && (pl == 4 || pl == 5))) {
# Line 1488  void PamLevel2::SortTracks() { Line 1777  void PamLevel2::SortTracks() {
1777            };            };
1778    
1779            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1780              Int_t pl = tof_obj->GetPlaneIndex((oi->pmttdc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1781              //      if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;              //      if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
1782              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1783                  || (use_S3 && (pl == 4 || pl == 5))) {                  || (use_S3 && (pl == 4 || pl == 5))) {
# Line 1663  void PamLevel2::SortTracks() { Line 1952  void PamLevel2::SortTracks() {
1952    
1953  }  }
1954  ;  ;
1955    //
1956    //--------------------------------------
1957    /**
1958     * Sort physical (tracker) tracks.
1959     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1960     * Sorting cryteria:
1961     * TRK: lower chi**2
1962     * CAL: lower Y spatial residual on the first calorimeter plane
1963     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
1964     * S1: (ask Emiliano)
1965     * S2: (ask Emiliano)
1966     * S3: (ask Emiliano)
1967     * GP: more GP hits
1968     * The default sorting cryterium is "TOF+CAL".
1969     *
1970     * 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).
1971     *
1972     * New version, with handling of extended tracks and nuclei tracks.
1973     */
1974    void PamLevel2::SortTracksNew() {
1975    
1976      //--------------------------------------------------
1977      //Check if the current event has already been sorted
1978      //--------------------------------------------------
1979      if (issorted_new && lastsorted_new == GetReadEntry()) {
1980          return; //already done for this event
1981      }
1982    
1983    
1984    //  cout << "SORT" << endl;
1985    
1986      //Reset the sort flags, just in case something will go wrong...
1987      issorted_new = false;
1988      lastsorted_new = -1;
1989    
1990      //--------------------------------------------------
1991      // set input variables
1992      //--------------------------------------------------
1993    
1994      TString how = howtosort;
1995      //Save current Object count
1996      Int_t ObjectNumber = TProcessID::GetObjectCount();
1997    
1998    
1999    
2000    
2001      TrkLevel2  * trk2 ;
2002      CaloLevel2 * calo2;
2003      ToFLevel2  * tof2  ;
2004      OrbitalInfo  * orb2 ;
2005      
2006      TClonesArray * trkext ;
2007      TClonesArray * caloext;
2008      TClonesArray * tofext ;
2009      TClonesArray * orbext ;
2010    
2011    
2012    //   cout << "trk2_obj" << trk2_obj<< endl;
2013    //   cout << "trk2_nuc_obj" << trk2_nuc_obj<< endl;
2014    //   cout << " trk_ext_obj "<<trk_ext_obj<< endl;
2015    //   cout << " trk_ext_nuc_obj "<<trk_ext_nuc_obj<< endl;
2016    
2017      //-----------------------------------------------------------
2018      // create/reset TCloneArrays to store tracks and their images
2019      //-----------------------------------------------------------
2020    
2021    //  cout << " PamLevel2::SortTracksNew() --- Clear TClonesArray objects"<<endl;
2022      
2023      // main tracks from standard alg
2024    //   if (!tsorted)
2025    //       tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2026    //   tsorted->Clear("C+C");//Delete();
2027    //   // track images from standard alg
2028    //   if (!timage)
2029    //       timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2030    //   timage->Clear("C+C");//Delete();
2031    //   // tracks from extended algorythm
2032    //   if(EXT && !text)
2033    //       text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2034    //   if(text)text->Clear("C+C");//Delete();
2035    
2036      if(tsorted)delete tsorted;
2037      if(timage) delete timage;
2038      if(text)   delete text;
2039      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2040      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2041      text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2042    
2043      //-----------------------------------------------------------
2044      // create/reset TCloneArrays to store tracks and their images
2045      //-----------------------------------------------------------
2046      if(NUC){
2047    
2048          
2049          if(tsorted_nuc)delete tsorted_nuc;
2050          if(timage_nuc) delete timage_nuc;
2051          if(text_nuc)   delete text_nuc;
2052          tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2053          timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2054          text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2055    
2056      // main tracks from standard alg
2057    //       if (!tsorted_nuc)
2058    //        tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2059    //       tsorted_nuc->Clear("C+C");//Delete();
2060    //       // track images from standard alg
2061    //       if (!timage_nuc)
2062    //        timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2063    //       timage_nuc->Clear("C+C");//Delete();
2064    //       // tracks from extended algorythm
2065    //       if(EXT && !text_nuc)
2066    //        text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2067    //       if(text_nuc)text_nuc->Clear("C+C");//Delete();
2068    
2069      }
2070      //--------------------------------------------------
2071      // retrieve sorting method
2072      //--------------------------------------------------
2073      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
2074      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
2075      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
2076      Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
2077      Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
2078      Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
2079      Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
2080    
2081      if (use_TOF) {
2082        use_S1 = true;
2083        use_S2 = true;
2084        use_S3 = true;
2085      };
2086      if (!CAL2 && use_CAL)
2087        use_CAL = false;
2088      if (!TOF) {
2089        use_TOF = false;
2090        use_S1 = false;
2091        use_S2 = false;
2092        use_S3 = false;
2093      }
2094      if (!GP) {
2095        use_GP = false;
2096      }
2097    
2098      if (!TRK2) {
2099        cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
2100        return;
2101      };
2102    
2103    
2104      ///////////////////////////////////////////////////////////////////////////////////
2105      //
2106      //   sort tracks and fill PamTrack arrays
2107      //
2108      ///////////////////////////////////////////////////////////////////////////////////
2109      for(int doit=0; doit<2; doit++){
2110    
2111    //       cout << "doit "<<doit<<endl;
2112    
2113    
2114          if(doit == 0){
2115    
2116              trk2  = trk2_obj;
2117              calo2 = calo2_obj;
2118              tof2  = tof2_obj;
2119              orb2  = orb2_obj;
2120          
2121              trkext = trk_ext_obj;
2122              caloext = calo_ext_obj;
2123              tofext = tof_ext_obj;
2124              orbext = orb_ext_obj;
2125        
2126          
2127          
2128          
2129    
2130          }else if (doit == 1){
2131    
2132              if(!NUC)break;
2133    
2134          
2135              trk2  = trk2_nuc_obj;
2136              calo2 = calo2_nuc_obj;
2137              tof2  = tof2_nuc_obj;
2138              orb2  = orb2_nuc_obj;
2139          
2140              trkext = trk_ext_nuc_obj;
2141              caloext = calo_ext_nuc_obj;
2142              tofext = tof_ext_nuc_obj;
2143              orbext = orb_ext_nuc_obj;
2144          
2145          
2146          
2147    
2148          }
2149    
2150    //       cout << "trk2" << trk2<<endl;
2151    //       cout << "calo2" << calo2<<endl;
2152    //       cout << "tof2" << tof2<<endl;
2153    //       cout << "orb2" << orb2<<endl;
2154    //       cout << "trkext" <<trkext <<endl;
2155    //       cout << "tofext" << tofext<<endl;
2156    //       cout << "caloext" << caloext<<endl;
2157    //       cout << "orbext" << orbext<<endl;
2158    
2159          TClonesArray &ttext    = (doit==0 ? *text : *text_nuc);
2160          TClonesArray &ttimage  = (doit==0 ? *timage : *timage_nuc);
2161          TClonesArray &ttsorted = (doit==0 ? *tsorted : *tsorted_nuc);
2162      
2163    //       cout << "tsorted + timage "<<doit<<endl;
2164    
2165          //--------------------------------------------------
2166          // loop over "physical" tracks sorted by the tracker
2167          //--------------------------------------------------
2168          for (Int_t i = 0; i < trk2->TrkLevel2::GetNTracks(); i++) {
2169    
2170              TrkTrack *ts = 0;
2171              CaloTrkVar *cs = 0;
2172              ToFTrkVar *os = 0;
2173              OrbitalInfoTrkVar *rs = 0;
2174    
2175              // get tracker tracks
2176              TrkTrack *tp = trk2->GetTrack(i); //tracker
2177              CaloTrkVar *cp = calo2->GetCaloStoredTrack(tp->GetSeqNo());
2178              ToFTrkVar *op = tof2->GetToFStoredTrack(tp->GetSeqNo());
2179              OrbitalInfoTrkVar *rp = orb2->GetOrbitalInfoStoredTrack(tp->GetSeqNo());
2180    
2181              TrkTrack *ti = 0; //tracker (image)
2182              CaloTrkVar *ci = 0;
2183              ToFTrkVar *oi = 0;
2184              OrbitalInfoTrkVar *ri = 0;
2185              //    cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
2186              // if track has an image, check image selection
2187    
2188              Int_t tp_score = 0; //main track sorted by the tracker
2189              Int_t ti_score = 0; //image track
2190              Int_t totp_score = 0; //main track sorted by the tracker
2191              Int_t toti_score = 0; //image track
2192    
2193              if (tp->HasImage()) {
2194    
2195                  ti = trk2->GetTrackImage(i); //tracker (image)
2196                  ci = calo2->GetCaloStoredTrack(ti->GetSeqNo());
2197                  oi = tof2->GetToFStoredTrack(ti->GetSeqNo());
2198                  ri = orb2->GetOrbitalInfoStoredTrack(ti->GetSeqNo());
2199    
2200                  //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
2201    
2202                  //assign starting scores
2203                  tp_score = 0; //main track sorted by the tracker
2204                  ti_score = 0; //image track
2205    
2206                  // -----------------------------------------------------------------------------------------
2207                  // *****************************************************************************************
2208                  // -----------------------------------------------------------------------------------------
2209                  // calorimeter check
2210                  // -----------------------------------------------------------------------------------------
2211                  if (use_CAL && !calo2) {
2212                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
2213                      return;
2214                  };
2215                  if (use_CAL && !cp && ci) {
2216                      ti_score++;
2217                      toti_score++;
2218                  };
2219                  if (use_CAL && cp && !ci) {
2220                      tp_score++;
2221                      totp_score++;
2222                  };
2223                  if (use_CAL && cp && ci && true) {
2224    
2225                      if (cp->npresh > ci->npresh && true) {
2226                          tp_score++;
2227                          totp_score++;
2228                      };
2229                      if (cp->npresh < ci->npresh && true) {
2230                          ti_score++;
2231                          toti_score++;
2232                      };
2233    
2234                      //    cout << "CALO "<<tp_score<<ti_score<<endl;
2235    
2236                  };
2237                  // -----------------------------------------------------------------------------------------
2238                  // *****************************************************************************************
2239                  // -----------------------------------------------------------------------------------------
2240                  // TOF check
2241                  // -----------------------------------------------------------------------------------------
2242                  // check the number of hit pmts along the track
2243                  // on S12 S21 and S32, where paddles are parallel to Y axis
2244                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2) {
2245                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
2246                      return;
2247                  };
2248                  //
2249                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
2250                      ti_score++;
2251                      toti_score++;
2252                  };
2253                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
2254                      tp_score++;
2255                      totp_score++;
2256                  };
2257                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
2258                      //
2259                      Float_t sen = 0.;
2260                      for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2261                          Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2262                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2263                              sen += (op->dedx).At(ih);
2264                      };
2265                      for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2266                          Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2267                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2268                              sen += (oi->dedx).At(ih);
2269                      };
2270                      //
2271                      if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
2272                          //printf(" IS A NUCLEUS! en = %f \n",sen);
2273                          //
2274                          // is a nucleus use a different algorithm
2275                          //
2276                          Int_t nz = 6;
2277                          Float_t zin[6]; // << define TOF z-coordinates
2278                          for (Int_t ip = 0; ip < nz; ip++)
2279                              zin[ip] = tof2->GetZTOF(tof2->GetToFPlaneID(ip)); // << read ToF plane z-coordinates
2280                          Trajectory *tr = new Trajectory(nz, zin);
2281                          //
2282                          Int_t nphit_p = 0;
2283                          Int_t nphit_i = 0;
2284                          Float_t enhit_p = 0.;
2285                          Float_t enhit_i = 0.;
2286                          //
2287                          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2288                              Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2289                              if (pl == 1 || pl == 2 || pl == 5) {
2290                                  nphit_p++;
2291                                  enhit_p += (op->dedx).At(ih);
2292                              };
2293                          };
2294                          //
2295                          tp->DoTrack2(tr);
2296                          //
2297                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2298                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2299                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2300                                  if (pl == 0) {
2301                                      nphit_p++;
2302                                      enhit_p += (op->dedx).At(ih);
2303                                  };
2304                              };
2305                          };
2306                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2307                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2308                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2309                                  if (pl == 3) {
2310                                      nphit_p++;
2311                                      enhit_p += (op->dedx).At(ih);
2312                                  };
2313                              };
2314                          };
2315                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2316                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2317                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2318                                  if (pl == 4) {
2319                                      nphit_p++;
2320                                      enhit_p += (op->dedx).At(ih);
2321                                  };
2322                              };
2323                          };
2324    
2325                          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2326                              Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2327                              if (pl == 1 || pl == 2 || pl == 5) {
2328                                  nphit_i++;
2329                                  enhit_i += (op->dedx).At(ih);
2330                              };
2331                          };
2332                          //
2333                          ti->DoTrack2(tr);
2334                          //
2335                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2336                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2337                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2338                                  if (pl == 0) {
2339                                      nphit_i++;
2340                                      enhit_i += (op->dedx).At(ih);
2341                                  };
2342                              };
2343                          };
2344                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2345                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2346                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2347                                  if (pl == 3) {
2348                                      nphit_i++;
2349                                      enhit_i += (op->dedx).At(ih);
2350                                  };
2351                              };
2352                          };
2353                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2354                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2355                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2356                                  if (pl == 4) {
2357                                      nphit_i++;
2358                                      enhit_i += (op->dedx).At(ih);
2359                                  };
2360                              };
2361                          };
2362    
2363                          if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
2364    
2365                              //        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);
2366                              //        printf(" score p %i score i %i \n",tp_score,ti_score);
2367                              //              if( enhit_p > enhit_i ) tp_score++;
2368                              //              if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
2369                              if (nphit_p > nphit_i)
2370                                  tp_score++;
2371                              if (nphit_p < nphit_i)
2372                                  ti_score++;
2373                              if (nphit_p == nphit_i) {
2374                                  if (enhit_p > enhit_i)
2375                                      tp_score++;
2376                                  else
2377                                      ti_score++;
2378                              };
2379                              //        printf(" dopo score p %i score i %i \n",tp_score,ti_score);
2380                          };
2381                          delete tr;
2382                          //
2383                      }
2384                      else {
2385                          // -------------
2386                          // NOT a NUCLEUS
2387                          // -------------
2388                          //printf(" NOT a NUCLEUS! en = %f \n",sen);
2389    
2390                          Int_t nphit_p = 0;
2391                          Int_t nphit_i = 0;
2392    
2393                          /*                                cout << "track: npmtadc "<< op->npmtadc << endl;
2394                            cout << "track: npmttdc "<< op->npmttdc << endl;
2395                            cout << "image: npmtadc "<< oi->npmtadc << endl;
2396                            cout << "image: npmttdc "<< oi->npmttdc << endl;*/
2397    
2398                          //          for (Int_t ih=0; ih < op->npmtadc; ih++){
2399                          //            Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
2400                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
2401                          //          };
2402    
2403                          //          for (Int_t ih=0; ih < oi->npmtadc; ih++){
2404                          //            Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
2405                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
2406                          //          };
2407                          // --- modified to count tdc signals (more efficient?)
2408                          // --- and to implement check on tdcflag
2409                          for (Int_t ih = 0; ih < op->npmttdc; ih++) {
2410                              Int_t pl = tof2->GetPlaneIndex((op->pmttdc).At(ih));
2411                              //        if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
2412                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2413                                  || (use_S3 && (pl == 4 || pl == 5))) {
2414                                  if ((op->tdcflag).At(ih) == 0)
2415                                      nphit_p++;
2416                              };
2417                          };
2418    
2419                          for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
2420                              Int_t pl = tof2->GetPlaneIndex((oi->pmttdc).At(ih));
2421                              //        if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
2422                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2423                                  || (use_S3 && (pl == 4 || pl == 5))) {
2424                                  if ((oi->tdcflag).At(ih) == 0)
2425                                      nphit_i++;
2426                              };
2427                          };
2428    
2429                          if ((nphit_p + nphit_i) != 0 && true) {
2430    
2431                              if (nphit_p != nphit_i) {
2432                                  totp_score += nphit_p;
2433                                  toti_score += nphit_i;
2434                                  tp_score += nphit_p;
2435                                  ti_score += nphit_i;
2436                              };
2437                              //        if     ( nphit_p > nphit_i) tp_score+=nphit_p;
2438                              //        else if( nphit_p < nphit_i) ti_score+=nphit_i;
2439                              //        else ;//niente
2440                          };
2441                      };
2442                      //    cout << "TOF "<<tp_score<<ti_score<<endl;
2443                  };
2444    
2445                  //      if(tp_score == ti_score) use_TRK = true;
2446    
2447    
2448                  // -----------------------------------------------------------------------------------------
2449                  // *****************************************************************************************
2450                  // -----------------------------------------------------------------------------------------
2451                  // tracker check
2452                  // -----------------------------------------------------------------------------------------
2453                  // chi**2 difference is not always large enough to distinguish among
2454                  // the real track and its image.
2455                  // Tracker check will be applied always when calorimeter and tof information is ambiguous.
2456                  // *** MODIFIED ON AUGUST 2007 ***
2457                  if (use_TRK) {
2458                      //    if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
2459                      //    else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
2460    
2461                      // CHECK 1 : number of points along X
2462                      if (tp->GetNX() >= ti->GetNX()) {
2463                          tp_score++;
2464                          totp_score++;
2465                      };
2466                      if (tp->GetNX() <= ti->GetNX()) {
2467                          ti_score++;
2468                          toti_score++;
2469                      };
2470                      // CHECK 2 : number of points along Y
2471                      if (tp->GetNY() >= ti->GetNY()) {
2472                          tp_score++;
2473                          totp_score++;
2474                      };
2475                      if (tp->GetNY() <= ti->GetNY()) {
2476                          ti_score++;
2477                          toti_score++;
2478                      };
2479    
2480                      //    cout << "TRK "<<tp_score<<ti_score<<endl;
2481                  };
2482    
2483                  // -----------------------------------------------------------------------------------------
2484                  // *****************************************************************************************
2485                  // -----------------------------------------------------------------------------------------
2486                  // GPamela check
2487                  // -----------------------------------------------------------------------------------------
2488    
2489                  //---------------------------------------------------
2490                  // count the number of GP hits
2491                  //---------------------------------------------------
2492                  if (use_GP) {
2493                      int ngphits_p = 0;
2494                      int ngphits_i = 0;
2495                      float toll = 0.02; //200 micron
2496                      for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
2497                          int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
2498                          if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2499                              ngphits_p++;
2500                          if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2501                              ngphits_i++;
2502                      }
2503                      if (ngphits_p > ngphits_i && true) {
2504                          tp_score++;
2505                          totp_score++;
2506                      }
2507                      if (ngphits_p < ngphits_i && true) {
2508                          ti_score++;
2509                          toti_score++;
2510                      }
2511                  }
2512    
2513                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2514                  // the winner is....
2515                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2516                  if (tp_score > ti_score) {
2517    
2518                  }
2519                  else if (tp_score < ti_score) {
2520    
2521                      ts = ti;//its image!!
2522                      cs = ci;
2523                      os = oi;
2524                      rs = ri;
2525                      Int_t totis = toti_score;
2526    
2527                      ti = tp;//its image!!
2528                      ci = cp;
2529                      oi = op;
2530                      ri = rp;
2531    
2532                      tp = ts;//its image!!
2533                      cp = cs;
2534                      op = os;
2535                      rp = rs;
2536    
2537                      toti_score = totp_score;
2538                      totp_score = totis;
2539    
2540                  }
2541                  else {
2542    
2543                      //    cout << "Warning - track image ambiguity not solved" << endl;
2544    
2545                  };
2546    
2547              }
2548              else {
2549                  totp_score = 1;
2550                  toti_score = 0;
2551    
2552                  //            ts = tp;
2553                  //            cs = cp;
2554                  //            os = op;
2555              };
2556    
2557              //    cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
2558              //    sorted_tracks->Add(ts);//save the track in the sorted array
2559              //    sorted_tracks.Add(ts);//save the track in the sorted array
2560              //    sorted_tracks.Add(tp);//save the track in the sorted array
2561              //    cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
2562              //    cout<<"o "<<tp<<endl;
2563              //    cout<<"o "<<cp<<endl;
2564              //    cout<<"o "<<op<<endl;
2565    
2566              new (ttsorted[i]) PamTrack(tp, cp, op, rp);
2567              new (ttimage[i]) PamTrack(ti, ci, oi, ri);
2568    
2569              ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
2570              ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
2571              ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
2572              ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
2573          };
2574    
2575    
2576    
2577    
2578    
2579    //   if (tsorted->GetEntries() != trk2->GetNTracks()) {
2580    //     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2581    //         << " != trk2->GetNTracks() = " << trk2->GetNTracks() << endl;
2582    //     tsorted->Delete();
2583    //     tsorted = 0;
2584    //     timage->Delete();
2585    //     timage = 0;
2586    //   }
2587    
2588    
2589    //       cout << "text "<<doit<<endl;
2590    
2591    
2592          //--------------------------------------------------
2593          // fill array with extended tracks (this is easy...)
2594          //--------------------------------------------------
2595          if(EXT){
2596              for(int it=0; it<trkext->GetEntries(); it++){
2597                  
2598                  new (ttext[it]) PamTrack((ExtTrack*)(*trkext)[it], (CaloTrkVar*)(*caloext)[it], (ToFTrkVar*)(*tofext)[it], (OrbitalInfoTrkVar*)(*orbext)[it]);
2599                  
2600              }
2601          }
2602          
2603    
2604    
2605      };
2606    
2607    
2608      //Restore Object count
2609      //To save space in the table keeping track of all referenced objects
2610      //We reset the object count to what it was at the beginning of the event.
2611      TProcessID::SetObjectCount(ObjectNumber);
2612    
2613      //Everything went fine so the current event can be tagged as sorted
2614      issorted_new = true;
2615      lastsorted_new = GetReadEntry();
2616    
2617    //   cout << " tsorted "<< tsorted << " "<<(tsorted ? tsorted->GetEntries() : 0)<<endl;
2618    //   cout << " timage  "<< timage  << " "<<(timage  ? timage->GetEntries() : 0)<<endl;
2619    //   cout << " text    "<< text    << " "<<(text    ? text->GetEntries() : 0)<<endl;
2620    
2621    
2622    };
2623  //--------------------------------------  //--------------------------------------
2624  //  //
2625  //  //
# Line 1671  void PamLevel2::SortTracks() { Line 2628  void PamLevel2::SortTracks() {
2628   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
2629   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
2630   */   */
2631    
2632  // TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
2633    
2634  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
# Line 1681  void PamLevel2::SortTracks() { Line 2639  void PamLevel2::SortTracks() {
2639  //     return &sorted_tracks;  //     return &sorted_tracks;
2640    
2641  //  };  //  };
 TClonesArray *PamLevel2::GetTracks() {  
2642    
   //  *-*-*-*-*-*-*-*-*-*-*-*-*  
   SortTracks();  
   //  *-*-*-*-*-*-*-*-*-*-*-*-*  
   
   return tsorted;  
2643    
 }  
 ;  
2644  //--------------------------------------  //--------------------------------------
2645  //  //
2646  //  //
2647  //--------------------------------------  //--------------------------------------
2648    
2649  /**  /**
2650   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
2651   * It override TrkLevel2::GetTrack(int it).   * It override TrkLevel2::GetTrack(int it).
2652   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2653   */   */
2654  PamTrack *PamLevel2::GetTrack(int it) {  PamTrack *PamLevel2::GetTrackOld(int it) {
2655    
2656    //    if(!trk2_obj) return 0;    PamTrack *track = NULL;
2657    
   // //  *-*-*-*+-*-*-*-*-*-*-*-*-*  
   //     SortTracks("+CAL+TOF");  
   // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
   // //    if(!sorted_tracks)return 0;  
   //     if(sorted_tracks.IsEmpty())return 0;  
   
   //     PamTrack *track = 0;  
   
   //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){  
   //    TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);  
   //    track = GetPamTrackAlong(t);  
   //     }else{  
   //    cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
   //     };  
   
   //     return track;  
   
   //    cout << "PamLevel2::GetTrack(int it) "<<endl;  
2658    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2659    SortTracks();    SortTracks();
2660    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2661    if (!tsorted)    if (!tsorted)
2662      return 0;      return track;
2663    if (!tsorted->GetEntries())    if (!tsorted->GetEntries())
2664      return 0;      return track;
2665    
   PamTrack *track = 0;  
2666    
2667    if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {    if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
     //  TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);  
     //  track = GetPamTrackAlong(t);  
     //TClonesArray &t = *(tsorted);  
     //track = (PamTrack*)t[it];  
2668      track = (PamTrack*)((*tsorted)[it]);      track = (PamTrack*)((*tsorted)[it]);
2669    }    }
2670    else {    else {
2671      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "      cout << "PamLevel2::GetTrackOld(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2672          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2673    };    };
2674    
2675    return track;    return track;
2676    
2677    };
2678    
2679    //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2680    
2681    /**
2682     * Retrieves the it-th Pamela "physical" track.
2683     * It override TrkLevel2::GetTrack(int it).
2684     * @param it Track number, ranging from 0 to GetNTracks().
2685     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2686     */
2687    PamTrack *PamLevel2::GetTrack(int it, const char* alg) {
2688    
2689        TString s(alg);
2690        if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrackOld(it); //old algorythm
2691    
2692    
2693        SortTracksNew();
2694        // >> fill tsorted, timage and text
2695    
2696        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2697    
2698            if( s.Contains("NUC")){
2699                if(
2700                    tsorted_nuc &&
2701                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2702                    it >= 0 &&                                                //valid index
2703                    true) return (PamTrack*)((*tsorted_nuc)[it]);                 //ok return the track
2704            }else{
2705                if(
2706                    tsorted &&
2707                    it < tsorted->GetEntries() &&                             //enough tracks found
2708                    it >= 0 &&                                                //valid index
2709                    true )return (PamTrack*)((*tsorted)[it]);                 //ok return the track
2710            }
2711    
2712        }
2713        
2714    
2715        /////////////////////////////////////////////////////////////////////////
2716        /// if requested get track from extended algorythm output
2717        /////////////////////////////////////////////////////////////////////////
2718        
2719        if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2720            
2721            if(s.Contains("NUC")){
2722                if(
2723                    text_nuc &&
2724                    it < text_nuc->GetEntries() &&                             //enough tracks found
2725                    it >= 0 &&                                                //valid index
2726                    true) return (PamTrack*)((*text_nuc)[it]);
2727            }else{
2728                if(
2729                    text &&
2730                    it < text->GetEntries() &&                             //enough tracks found
2731                    it >= 0 &&                                                //valid index
2732                    true) return (PamTrack*)((*text)[it]);
2733            }
2734    
2735        };
2736        
2737        
2738        cout << "PamTrack *PamLevel2::GetTrack("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2739        
2740        return NULL;
2741    
2742    }
2743    ;
2744    TClonesArray *PamLevel2::GetTracks() {
2745    
2746      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2747      SortTracks();
2748      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2749    
2750      return tsorted;
2751    
2752  }  }
2753  ;  ;
2754    Int_t  PamLevel2::GetNTracks(const char* alg) {
2755    
2756    
2757    //    cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2758    
2759        TString s(alg);
2760    
2761        if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2762            
2763        if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2764            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2765            return trk_ext_obj->GetEntries();//ok
2766        }
2767        if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2768            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2769                return (trk2_nuc_obj->TrkLevel2::GetNTracks() ?  trk2_nuc_obj->TrkLevel2::GetNTracks() :  trk_ext_nuc_obj->GetEntries() );  
2770            return (trk2_obj->TrkLevel2::GetNTracks() ?  trk2_obj->TrkLevel2::GetNTracks() :  trk_ext_obj->GetEntries() );
2771        }
2772        if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2773            return trk2_nuc_obj->TrkLevel2::GetNTracks();
2774        
2775        cout << "Int_t  PamLevel2::GetNTracks("<<alg<<") -- unrecognised algorithm"<<endl;
2776    
2777        return 0;
2778    
2779    }
2780    
2781    
2782  //--------------------------------------  //--------------------------------------
2783  //  //
# Line 1757  PamTrack *PamLevel2::GetTrack(int it) { Line 2787  PamTrack *PamLevel2::GetTrack(int it) {
2787   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2788   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2789   */   */
2790  PamTrack *PamLevel2::GetTrackImage(int it) {  PamTrack *PamLevel2::GetTrackImageOld(int it) {
2791    
2792    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2793    SortTracks();    SortTracks();
# Line 1781  PamTrack *PamLevel2::GetTrackImage(int i Line 2811  PamTrack *PamLevel2::GetTrackImage(int i
2811      };      };
2812    }    }
2813    else {    else {
2814      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "      cout << "PamLevel2::GetTrackImageOld(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2815          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2816    };    };
2817    
2818    return image;    return image;
2819  }  }
   
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
2820  /**  /**
2821   * Get the Pamela detector trees in a single file and make them friends.   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2822     * @param it Track number, ranging from 0 to GetNTracks().
2823     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2824     */
2825    PamTrack *PamLevel2::GetTrackImage(int it, const char* alg) {
2826    
2827        TString s(alg);
2828        if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrackImageOld(it); //old algorythm
2829    
2830    
2831        SortTracksNew();
2832        // >> fill tsorted, timage and text
2833    
2834        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2835    
2836            if( s.Contains("NUC")){
2837                if(
2838                    tsorted_nuc &&
2839                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2840                    it >= 0 &&                                                //valid index
2841                    true){
2842                    TClonesArray &t = *(tsorted_nuc);
2843                    PamTrack *temp = (PamTrack*) t[it];
2844                    if (temp->GetTrkTrack()->HasImage()) {
2845                        return (PamTrack*)((*timage_nuc)[it]);                 //ok return the track
2846                    }else{
2847                        return NULL;
2848                    }
2849                    
2850                }
2851            }else{
2852                if(
2853                    tsorted &&
2854                    it < tsorted->GetEntries() &&                             //enough tracks found
2855                    it >= 0 &&                                                //valid index
2856                    true ){
2857                    TClonesArray &t = *(tsorted);
2858                    PamTrack *temp = (PamTrack*) t[it];
2859                    if (temp->GetTrkTrack()->HasImage()) {              
2860                        return (PamTrack*)((*timage)[it]);                 //ok return the track
2861                    }else{
2862                        return NULL;
2863                    }
2864                }
2865            }
2866    
2867        }
2868    
2869        cout << "PamTrack *PamLevel2::GetTrackImage("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2870          
2871        return NULL;
2872    
2873    }
2874    ;
2875    
2876    //--------------------------------------
2877    //
2878    //
2879    //--------------------------------------
2880    /**
2881     * Get the Pamela detector trees in a single file and make them friends.
2882   * @param f TFile pointer   * @param f TFile pointer
2883   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
2884   * @return Pointer to a TTree   * @return Pointer to a TTree
# Line 1876  TTree *PamLevel2::GetPamTree(TFile *f, T Line 2961  TTree *PamLevel2::GetPamTree(TFile *f, T
2961      if (!Trout)      if (!Trout)
2962        Trout = T;        Trout = T;
2963      else      else
2964        Trout->AddFriend(T);          Trout->AddFriend(T);
2965    }    }
2966    else {    else {
2967      cout << "Tracker      : missing tree" << endl;      cout << "Tracker      : missing tree" << endl;
# Line 2113  TList* PamLevel2::GetListOfLevel2Files(T Line 3198  TList* PamLevel2::GetListOfLevel2Files(T
3198          if (file.EndsWith(".root")) {          if (file.EndsWith(".root")) {
3199            TString filedir;            TString filedir;
3200            cout << ddir << endl;            cout << ddir << endl;
3201            if (ddir != "") {            if ( ddir != "" ) {
3202              filedir = ddir; // take the input dir              filedir = ddir; // take the input dir
3203            }            }
3204            else {            else {
# Line 2121  TList* PamLevel2::GetListOfLevel2Files(T Line 3206  TList* PamLevel2::GetListOfLevel2Files(T
3206              filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir              filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
3207            };            };
3208            filedir.Append("/");            filedir.Append("/");
3209            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3210              char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3211            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3212            cout << fullpath << endl;            cout << fullpath << endl;
3213            delete fullpath;            delete fullpath;
# Line 2240  TChain *PamLevel2::GetPamTree(TList *fl, Line 3326  TChain *PamLevel2::GetPamTree(TList *fl,
3326    TChain *G = 0;    TChain *G = 0;
3327    
3328    TChain *L = 0;    TChain *L = 0;
3329      TChain *P = 0;
3330    
3331    if (TRK2 || TRK1 || TRKh)    if (TRK2 || TRK1 || TRKh)
3332      T = new TChain("Tracker");      T = new TChain("Tracker");
# Line 2259  TChain *PamLevel2::GetPamTree(TList *fl, Line 3346  TChain *PamLevel2::GetPamTree(TList *fl,
3346      B = new TChain("OrbitalInfo");      B = new TChain("OrbitalInfo");
3347    if (GP)    if (GP)
3348      G = new TChain("h20");      G = new TChain("h20");
3349      if (PROC)
3350        P = new TChain("ProcessingInfo");
3351    L = new TChain("SelectionList");    L = new TChain("SelectionList");
3352    
3353    // loop over files and create chains    // loop over files and create chains
# Line 2286  TChain *PamLevel2::GetPamTree(TList *fl, Line 3375  TChain *PamLevel2::GetPamTree(TList *fl,
3375          B->Add(name);          B->Add(name);
3376        if (GP)        if (GP)
3377          G->Add(name);          G->Add(name);
3378          if (P)
3379            P->Add(name);
3380        if (SELLI == 1)        if (SELLI == 1)
3381          L->Add(name);          L->Add(name);
3382      };       };
3383    };    };
3384    
3385    cout << "done chains\n";    cout << "done data-tree chains  "<<  T->GetNtrees() <<" \n";
3386    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3387    
3388    // -------------------------------------------    // -------------------------------------------
3389    // make friends    // make friends
# Line 2375  TChain *PamLevel2::GetPamTree(TList *fl, Line 3466  TChain *PamLevel2::GetPamTree(TList *fl,
3466    //  =====================================    //  =====================================
3467    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3468    //  =====================================    //  =====================================
3469    SetBranchAddress(Trout);    if( Trout->GetNtrees() )SetBranchAddress(Trout);
3470    
3471    //  ------------------------------------    //  ------------------------------------
3472    //  finally handle selection trees...    //  finally handle selection trees...
3473    //  (it is not friend of pamela tree)    //  (it is not friend of pamela tree)
3474    //  ------------------------------------    //  ------------------------------------
3475    
3476    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3477    
3478    // Selection List    // Selection List
3479    if (L && SELLI == 1) {    if (L && SELLI == 1) {
# Line 2392  TChain *PamLevel2::GetPamTree(TList *fl, Line 3483  TChain *PamLevel2::GetPamTree(TList *fl,
3483      cout << "SelectionList: set branch address RunEntry" << endl;      cout << "SelectionList: set branch address RunEntry" << endl;
3484      L->SetBranchAddress("EventEntry", &irunentry);      L->SetBranchAddress("EventEntry", &irunentry);
3485      cout << "SelectionList: set branch address EventEntry" << endl;      cout << "SelectionList: set branch address EventEntry" << endl;
3486        /*    if ( L->GetBranch("L0EventEntry") ){
3487          hasL0EE = true;
3488          L->SetBranchAddress("L0EventEntry", &il0entry);
3489          cout << "SelectionList: set branch address L0EventEntry" << endl;
3490        } else {
3491          hasL0EE = false; // backward compatibility with old preselected files...
3492          }*/
3493      sel_tree = L;      sel_tree = L;
3494      //          if(!Trout)Trout=O;      //          if(!Trout)Trout=O;
3495      //          else Trout->AddFriend("SelectionList");      //          else Trout->AddFriend("SelectionList");
3496      cout << "+SelectionList" << endl;      cout << "+SelectionList" << endl;
3497      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;      cout << "----------------------------------------------------" << endl;
3498    }    }
3499    else {    else {
3500      //  cout << "SelectionList  : missing tree"<<endl;      //  cout << "SelectionList  : missing tree"<<endl;
# Line 2404  TChain *PamLevel2::GetPamTree(TList *fl, Line 3502  TChain *PamLevel2::GetPamTree(TList *fl,
3502        L->Delete();        L->Delete();
3503    };    };
3504    
3505      //ProcessingInfo EM
3506      if ( P && P->GetEntries() ){
3507        cout << "----------------------------------------------------" << endl;
3508        cout << ">>> Found ProcessingInfo <<<" << endl;
3509        //  L->SetBranchAddress("RunEntry",&irun);
3510        P->SetBranchAddress("ProcInfo", &proc_obj);//NEWNEW
3511        proc_tree = P;
3512      }
3513    //  --------------------------------------------    //  --------------------------------------------
3514    //  return the pamela chain with all the friends    //  return the pamela chain with all the friends
3515    //  --------------------------------------------    //  --------------------------------------------
3516    
3517    pam_tree = Trout;    pam_tree = Trout;
   
3518    return Trout;    return Trout;
3519  }  }
3520    
# Line 2422  TChain *PamLevel2::GetPamTree(TList *fl, Line 3527  TChain *PamLevel2::GetPamTree(TList *fl,
3527   */   */
3528  void PamLevel2::SetBranchAddress(TTree *t) {  void PamLevel2::SetBranchAddress(TTree *t) {
3529    
3530    TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");  //   TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3531    TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");  //   TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3532    TRKh = TRKh & t->GetBranchStatus("TrkHough");  //   TRKh = TRKh & t->GetBranchStatus("TrkHough");
3533    CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");  //   CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3534    CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");  //   CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3535    TOF = TOF & t->GetBranchStatus("ToFLevel2");  //   TOF = TOF & t->GetBranchStatus("ToFLevel2");
3536    TRG = TRG & t->GetBranchStatus("TrigLevel2");  //   TRG = TRG & t->GetBranchStatus("TrigLevel2");
3537    S4 = S4 & t->GetBranchStatus("S4Level2");  //   S4 = S4 & t->GetBranchStatus("S4Level2");
3538    ND = ND & t->GetBranchStatus("NDLevel2");  //   ND = ND & t->GetBranchStatus("NDLevel2");
3539    AC = AC & t->GetBranchStatus("AcLevel2");  //   AC = AC & t->GetBranchStatus("AcLevel2");
3540    ORB = ORB & t->GetBranchStatus("OrbitalInfo");  //   ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3541    GP = GP & t->GetBranchStatus("h20");  //   GP = GP & t->GetBranchStatus("h20");
3542    
3543    
3544    // Tracker    // Tracker
3545    if (TRK1) {    if (TRK1) {
# Line 2443  void PamLevel2::SetBranchAddress(TTree * Line 3549  void PamLevel2::SetBranchAddress(TTree *
3549    if (TRK2) {    if (TRK2) {
3550      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3551      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3552        NUC = t->GetBranchStatus("TrackNuclei");
3553        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );    
3554    };    };
3555    if (TRKh) {    if (TRKh) {
3556      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
# Line 2503  void PamLevel2::SetBranchAddress(TTree * Line 3611  void PamLevel2::SetBranchAddress(TTree *
3611    
3612      cout << "h20          : set branch address GPamela " << endl;      cout << "h20          : set branch address GPamela " << endl;
3613    };    };
3614      if(NUC){
3615          
3616          cout << "Found nuclei-track branches" << endl;
3617    
3618          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3619          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3620          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3621          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3622          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3623          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3624          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3625          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3626    
3627          ///copy the vector content inside a "fake" object (all aother info are missing)    
3628    
3629          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3630          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3631          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3632          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3633    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3634    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3635    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3636    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3637    
3638          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3639          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3640          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3641          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3642    
3643          
3644      }    
3645    
3646      if(EXT){
3647          
3648          cout << "Found extended tracking algorythm branches" << endl;
3649    
3650          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3651          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3652          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3653          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3654          
3655          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3656          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3657          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3658          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3659    
3660        
3661          if(NUC){
3662              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3663              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3664              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3665              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3666              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3667              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3668              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3669              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3670          }
3671      }    
3672  }  }
3673  /**  /**
3674   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
# Line 2527  void PamLevel2::SetBranchAddress(TChain Line 3692  void PamLevel2::SetBranchAddress(TChain
3692    if (TRK2) {    if (TRK2) {
3693      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3694      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3695        NUC = t->GetBranchStatus("TrackNuclei");
3696        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3697    };    };
3698    if (TRK1) {    if (TRK1) {
3699      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
# Line 2540  void PamLevel2::SetBranchAddress(TChain Line 3707  void PamLevel2::SetBranchAddress(TChain
3707    // Calorimeter    // Calorimeter
3708    if (CAL2) {    if (CAL2) {
3709      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3710      cout << "Calorimeter  : set branch address CaloLevel2" << endl;      cout << "Calorimeter  : set branch address CaloLevel2" << endl;    
3711    };    };
3712    if (CAL1) {    if (CAL1) {
3713      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
# Line 2598  void PamLevel2::SetBranchAddress(TChain Line 3765  void PamLevel2::SetBranchAddress(TChain
3765    //    cout << "SelectionList: set branch address EventEntry"<<endl;    //    cout << "SelectionList: set branch address EventEntry"<<endl;
3766    
3767    //     }    //     }
3768      if(NUC){
3769          
3770          cout << "Found nuclei-track branches" << endl;
3771    
3772          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3773          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3774          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3775          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3776          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3777          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3778          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3779          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3780          
3781          ///copy the vector content inside a "fake" object (all aother info are missing)    
3782    
3783          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3784          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3785          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3786          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3787    
3788    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3789    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3790    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3791    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3792          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3793          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3794          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3795          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3796    
3797      }    
3798      if(EXT){
3799          
3800          cout << "Found extended tracking algorythm branches" << endl;
3801    
3802          t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3803    
3804          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3805          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3806          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3807          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3808          
3809          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3810          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3811          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3812          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3813          
3814          if(NUC){
3815              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3816              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3817              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3818              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3819              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3820              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3821              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3822              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3823          }
3824      }    
3825      
3826  }  }
3827    
3828    /**
3829     * Set the tracking algorythm
3830     * @param alg String to choose the track.
3831     * "" --> take the output of the standard tracking algorythm
3832     * "STD" --> take the output of the standard tracking algorythm
3833     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3834     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3835     * "EXTF" --> force the extended tracking algorythm
3836     * "NUCEXT" --> as "EXT", but for nuclei
3837     * "NUCEXTF" --> as "EXTF", but for nuclei
3838     */
3839    // void PamLevel2::SetTrackingAlgorythm(const char *alg){
3840    
3841    
3842    //     TString s(alg);
3843    //     if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
3844    //      cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
3845    //     if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
3846    //      cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
3847                
3848    //     trkAlg = alg;
3849    
3850    // };
3851    // const char* PamLevel2::GetTrackingAlgorythm(){
3852    
3853    
3854    //     cout<<endl<<" Implemented tracking algorythm: ";
3855    //     cout<<endl<<"  \"\" or \"STD\"  --> take the output of the standard tracking algorythm";
3856    //     cout<<endl<<"  \"NUC\"      --> take the output of the standard tracking algorythm for nuclei cluster selection";
3857    //     cout<<endl<<"  \"EXT\"      --> in case the standard tracking algorythm has not found any track,";
3858    //     cout<<endl<<"                 take the output of the extended one";
3859    //     cout<<endl<<"  \"EXTF\"     --> force the extended tracking algorythm";
3860    //     cout<<endl<<"  \"NUCEXT\"   --> as \"EXT\", but for nuclei ";
3861    //     cout<<endl<<"  \"NUCEXTF\"  --> as \"EXTF\", but for nuclei";
3862    
3863    //     cout<<endl;
3864    //     cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
3865    //     cout<<endl;
3866    
3867    //     return trkAlg;
3868    // };
3869    
3870    
3871    
3872  //--------------------------------------  //--------------------------------------
3873  //  //
3874  //  //
# Line 2634  TChain *PamLevel2::GetRunTree(TList *fl) Line 3902  TChain *PamLevel2::GetRunTree(TList *fl)
3902      };      };
3903    }    }
3904    
3905      cout << "done run chain  "<<  R->GetNtrees() <<" \n";
3906    
3907    
3908    if (RUN && R->GetNtrees()) {    if (RUN && R->GetNtrees()) {
3909    
3910      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
# Line 2673  TChain *PamLevel2::GetRunTree(TList *fl) Line 3944  TChain *PamLevel2::GetRunTree(TList *fl)
3944      cout << "----------------------------------------------------" << endl;      cout << "----------------------------------------------------" << endl;
3945    
3946    }    }
3947    else {  //   else {
3948      delete R;  //     delete R;
3949      R = 0;  //     R = 0;
3950    }  //   }
3951    
3952    run_tree = R;    run_tree = R;
3953    
# Line 2715  TTree *PamLevel2::GetRunTree(TFile *f) { Line 3986  TTree *PamLevel2::GetRunTree(TFile *f) {
3986    return T;    return T;
3987    
3988  }  }
 /**  
  * Update the runinfo informations (to be used to have Run infos event by event basis)  
  * @param run Pointer to the chain/tree which contains run infos  
  * @return true if a new run has been read, false if it is still the same run  
  */  
 Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {  
   //  
   // check if we have already called once GetEntry, if not call it  
   //  
   cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "  
       << endl;  
   if (!run) {  
     cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree " << endl;  
     return (false);  
   }  
   if (run->GetEntries() <= 0)  
     return (false);  
   //  
   
   //  Int_t oldrun = irun;  
   Long64_t oldrun = irun;  
   
   // --------------------------------------  
   // if it is a full file (not preselected)  
   // --------------------------------------  
   if (SELLI == 0) {  
   
     //  
     // the absolute time is necessary to relate the event with the run  
     //  
     if (!GetOrbitalInfo() && !ISGP) {  
       cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo " << endl;  
       return (false);  
     }  
   
     ULong64_t abstime = 0;  
     if (GetOrbitalInfo())  
       abstime = GetOrbitalInfo()->absTime;  
   
     //  
     // the first time the routine is called, set run search from the beginning  
     //  
     if (irun < 0LL) {  
       irun = 0LL;  
       run->GetEntry(irun);  
       runfirstentry = 0LL;  
       runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
       if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
         runlastentry -= 1LL;  
   
       if (ISGP && run->GetEntries() != 1) {  
         cout << "** WARNING ** simulated files are assumed to have 1 single run, not " << run->GetEntries() << endl;  
         cout << "** WARNING ** run will not be updated" << endl;  
       }  
   
     };  
     //  
     if (ISGP)  
       abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO  
     //  
     if (irun == run->GetEntries() - 1LL && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime  
         <= GetRunInfo()->RUNTRAILER_TIME)) {  
       irun = -1LL;  
       irunt = -1LL;  
       runfirstentry = 0LL;  
       runlastentry = -1LL;  
     };  
     // modificato il controllo sull'aggiornamento del run, per evitare problemi  
     // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)  
     //  
     bool fromfirst = true;  
     //  
     while (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun  
         < run->GetEntries() - 1LL) {  
       //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  
       irun++;  
       run->GetEntry(irun);  
       runfirstentry = runlastentry;  
       if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
         runfirstentry += 1LL;  
       runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
       //          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
       //          cout << "runfirstentry "<<runfirstentry<<endl;  
       //          printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));  
       //          printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);  
       //          printf(" IDRUN %u \n",GetRunInfo()->ID);  
       //  
       //          prevshift = 0;  
       //  
       if (irun == (Long64_t)(run->GetEntries() - 1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME  
           && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {  
         printf(" resetting irun  (it should NOT happen!!!)\n");  
         fromfirst = false;  
         irun = 0;  
         run->GetEntry(irun);  
         runfirstentry = 0ULL;  
         runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
         if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
           runlastentry -= 1LL;  
       };  
       //  
     };  
     //  
     if (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {  
       printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n", abstime);  
       return false;  
     }  
     //  
     if (irun == oldrun || irun >= run->GetEntries())  
       return (false);  
     //  
     //  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;  
     cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;  
     //      cout << "runfirstentry "<<runfirstentry<<endl;  
     return (true);  
   };  
   // ----------------------------------------------------  
   // if it is a preselected file (there is SelectionList)  
   // NBNB - the event tree MUST be read first  
   // ----------------------------------------------------  
   if (SELLI == 1) {  
     sel_tree->GetEntry(iev);  
     //      cout << irun << " "<< irunentry << endl;  
     if (irun != oldrun) {  
       run->GetEntry(irun);  
       cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;  
       prevshift = 0;  
       return true;  
     }  
     return false;  
   }  
   
   return false;  
   //  
 }  
 ;  
3989    
3990  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
3991    
3992      if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
3993    
3994    if (!run_tree) {    if (!run_tree) {
3995      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
3996      return false;      return false;
# Line 2865  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4000  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4000      return (false);      return (false);
4001    }    }
4002    
4003    
4004    Int_t oldrun = irun; // store current run index    Int_t oldrun = irun; // store current run index
4005    
4006    // -----------------------------------------------------------------------    // -----------------------------------------------------------------------
# Line 2874  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4010  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4010    if (irun < 0) {    if (irun < 0) {
4011      irun = 0LL;      irun = 0LL;
4012      irunt = 0LL;      irunt = 0LL;
4013      irunentry = 0;      totrunentry = 0LL;
4014        totrunentrymin = 0LL;
4015        totrunentrymax = 0LL;
4016        irunentry = 0LL;
4017        il0entry = 0LL;
4018      prevshift = 0;      prevshift = 0;
4019        yprevshift = 0;
4020        prevabstime = 0;
4021        prevpktnum = 0;
4022        abstime = 0ULL;
4023        pktnum = 0;
4024        isFragment = false;
4025      run_tree->GetEntry(irun);      run_tree->GetEntry(irun);
4026      if (!GetOrbitalInfo())      if (!GetOrbitalInfo())
4027        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
4028      if (gltsync)      if ( fUseDBinRunInfo ){
4029        delete gltsync; //Emiliano        if (gltsync)
4030      if (!dbc || (dbc && !dbc->IsConnected()))          delete gltsync; //Emiliano
4031        SetDBConnection(); //Emiliano        if (!dbc || (dbc && !dbc->IsConnected()))
4032      gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)          SetDBConnection(); //Emiliano
4033      if (dbc){        gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
4034        dbc->Close();// Emiliano        if (dbc){
4035        dbc=0;          dbc->Close();// Emiliano
4036      };          delete dbc;
4037    };          dbc=0;
4038          }
4039        }
4040      }
4041    // ---------------------------------------------------------------    // ---------------------------------------------------------------
4042    // retrieve OBT and absolute time of the event    // retrieve OBT and absolute time of the event
4043    // ---------------------------------------------------------------    // ---------------------------------------------------------------
   ULong64_t abstime = 0LL;  
   //    ULong64_t obt     = 0LL; // Emiliano  
4044    Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted    Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
4045      prevabstime = abstime;
4046      prevpktnum = pktnum;
4047    if (GetOrbitalInfo()) {    if (GetOrbitalInfo()) {
4048      abstime = GetOrbitalInfo()->absTime;      abstime = GetOrbitalInfo()->absTime;
4049      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
4050        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
4051    }    }
4052    else {    else {
4053      abstime = GetRunInfo()->RUNHEADER_TIME;      abstime = GetRunInfo()->RUNHEADER_TIME;
4054      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
4055        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
4056      }
4057    
4058      if (DBG){
4059        printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4060        printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4061        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4062        if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4063        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4064        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4065    }    }
4066    
4067    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 2922  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4082  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4082      }      }
4083    
4084      //      //
4085      bool fromfirst = true; // first loop over runs      bool a = true;
4086        bool b = true;
4087        if ( fUseDBinRunInfo ){
4088          a = false;    
4089          b = false;
4090          if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4091          if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4092        }
4093        if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4094             || iev == 0 // or it is the first entry
4095             || (!isSync && (
4096                             (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4097                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4098             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4099          
4100          // check for a new run (ma prima il primo!)
4101          if (DBG){
4102            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4103            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4104            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4105            if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4106            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4107            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4108          }
4109          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4110          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4111          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4112          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4113          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4114          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4115    
4116      // ------------------------------------------------------        totrunentry = 0LL;
4117      // loop over runs to find the one that contains the event        runfirstentry = 0LL;
4118      // ------------------------------------------------------        for (Int_t r=0; r< run_tree->GetEntries();r++){
4119      while ((          // -------------------------------------------------------------------
4120      //          (          // save the index of the first entry of the run, relative to pam_tree,
4121      //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)          // and read a new run
4122      //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          // -------------------------------------------------------------------
4123      //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)          run_tree->GetEntry(r);//update runinfo
4124      //                obt <= GetRunInfo()->RUNTRAILER_OBT)          if ( r > 0 ){
4125      //              )            totrunentrymin = totrunentrymax+1;
4126            } else {
4127      !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)            totrunentrymin = 0LL;
4128          abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano          }
4129          obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano          totrunentry += GetRunInfo()->NEVENTS;
4130          || GetRunInfo()->NEVENTS == 0          totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4131      //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento          irun = r;        
4132          || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {          if ( fUseDBinRunInfo ){
4133              a = false;    
4134        //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;            b = false;
4135        //            if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;            if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4136        //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;            if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4137        //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;          }
4138        // - - - - - - - - - - - - -          if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4139        // irunentry = position of current entry, relative to the run               ( !isSync &&
4140        // prevshift = shift needed to synchronize l0 and l2 data (nested events)                 ( abstime >= GetRunInfo()->RUNHEADER_TIME  && a // or it is inside obt limits (and abstime limits for security reasons)
4141        // - - - - - - - - - - - - -                   && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
4142                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4143        // -----------------------------------------            if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4144        // store dead and live-time of previous run              if ( !isSync ){
4145        // -----------------------------------------                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4146        //            if(SELLI==0){                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4147        if (SELLI != 2) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4148          if (fromfirst) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4149            if (oldrun == irun) {                prevshift += (totrunentrymin-iev); // add the new shift to total shift
4150              /// decrement counters                totrunentrymin -= (totrunentrymin-iev); // shift run position min
4151              if (GetTrigLevel2()) {                totrunentrymax -= (totrunentrymin-iev); // shift run position max
4152                totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4153                totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4154              }              } else {
4155              totdltime[2]--; //event counter                printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4156              if (DBG) {                cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4157                cout << endl;                cout << "\nFor bug reporting instructions, please see for example:\n";
4158                cout << "n.events     : " << totdltime[2] << endl;                cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4159                cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;                cout << "  " << endl;
               cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;  
4160              }              }
4161            }            }
4162            else {            runfirstentry = totrunentrymin; // first entry of the run in the level2
4163              totdltime[0] = 0;//live-time            
4164              totdltime[1] = 0;//dead-time  
4165              totdltime[2] = 0; //event counter            //
4166              if (DBG)            if ( fUseDBinRunInfo ){
4167                cout << " *** JUMP RUN *** irun " << irun << endl;              if (gltsync)
4168                  delete gltsync; // Emiliano
4169                if (!dbc || (dbc && !dbc->IsConnected()))
4170                  SetDBConnection(); //Emiliano
4171                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4172                TrkParams::Set(GetRunInfo(), dbc);
4173                if (dbc){
4174                  dbc->Close(); // Emiliano
4175                  delete dbc;
4176                  dbc=0;
4177                }          
4178                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4179                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4180                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4181                  cout
4182                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4183                    << endl;
4184                }
4185            }            }
4186            /// add an entry            //
4187            if (run_tree_clone)            if (DBG) printf(" found \n");
4188              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
4189                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
4190            /// reset counters            break;
4191            }
4192          } // loop over run
4193          
4194          // --------------------------------------
4195          // if there was no need to update the run
4196          // ---> exit with FALSE
4197          // --------------------------------------
4198          if (irun == oldrun){
4199            if (DBG) printf(" no new run \n");
4200            //        printf(" no new run \n");//TOGLITOGLI
4201            return (false);
4202          }      
4203          // --------------------------------------
4204          // ... otherwise
4205          // ---> exit with TRUE
4206          // --------------------------------------
4207    
4208          if (SELLI != 2) {
4209            /// decrement counters since this event belongs to a new run
4210            if (GetTrigLevel2()) {
4211              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4212              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4213            }
4214            totdltime[2]--; //event counter
4215            if (DBG) {
4216              cout << endl;
4217              cout << "n.events     : " << totdltime[2] << endl;
4218              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4219              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4220            }
4221            // add an entry
4222            if (run_tree_clone && totdltime[2] > 0)
4223              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4224                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4225            // reset counters
4226            if ( totdltime[2] > 0 ){
4227            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
4228              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4229              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2991  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4231  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4231            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
4232          }          }
4233        }        }
       //            }  
4234    
4235        irun++;        if (DBG){
4236        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4237        // if the end of run tree is reached...               << endl;        
4238        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4239        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4240          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4241            // -----------------------------------------------------          if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4242            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4243            // ---> exit with error          printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
           // -----------------------------------------------------  
           cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #" << iev  
               << " does not belong to any run (should not happen)" << endl;  
           return false;  
         }  
         // -----------------------------------------  
         // ...otherwise repeat search from beginning  
         // -----------------------------------------  
         cout  
             << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning "  
             << endl;  
         fromfirst = false;  
         irun = 0LL;  
         runfirstentry = 0LL;  
4244        }        }
4245        // -------------------------------------------------------------------        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4246        // save the index of the first entry of the run, relative to pam_tree,        //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4247        // and read a new run        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4248        // -------------------------------------------------------------------        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4249        if (irun > 0)        //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4250          runfirstentry += (GetRunInfo()->NEVENTS) - prevshift;        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4251        irunentry = 0;  
4252        prevshift = 0;        return (true);
4253        run_tree->GetEntry(irun);//update runinfo      } // need for run upgrade
4254        irunt = irun - irunoffset[run_tree->GetTreeNumber()];      if (DBG) printf("return false\n");
4255        if (gltsync)      return (false);
4256          delete gltsync; // Emiliano    }// SELLI = 0 SELLI = 2
4257        if (!dbc || (dbc && !dbc->IsConnected()))    
         SetDBConnection(); //Emiliano  
       gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano  
       if (dbc){  
         dbc->Close(); // Emiliano  
         dbc=0;  
       };  
       if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano  
         cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun  
             << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;  
         cout  
             << "                                                            (NB!! in this case some events are assigned to a wrong run)"  
             << endl;  
       }  
       //            if(hasfrag &&  fragid != GetRunInfo()->ID){  
       //                cout << "... where is the next fragment ??"<<endl;  
       //            }  
     };  
   
     // --------------------------------------  
     // if there was no need to update the run  
     // ---> exit with FALSE  
     // --------------------------------------  
     if (irun == oldrun)  
       return (false);  
   
     // --------------------------------------  
     // ... otherwise  
     // --------------------------------------  
   
   
     // --------------------------------------  
     // ---> exit with TRUE  
     // --------------------------------------  
     if (DBG)  
       cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun  
           << endl;  
     // ----------------------------------------------------  
     // update the tracker parameters  
     // (non ho trovato nessun altro modo sicuro di farlo...)  
     // ----------------------------------------------------  
     if (!dbc || (dbc && !dbc->IsConnected()))  
       SetDBConnection();  
     TrkParams::Set(GetRunInfo(), dbc);  
     if (dbc){  
       dbc->Close();  
       dbc=0;  
     };  
   
     // ----------------------------------------------------  
     // then check if the run has a fragment  
     // in this case we have to switch to the next fragment  
     // when the end of the first fragment is reached  
     // ----------------------------------------------------  
     if (GetRunInfo()->ID_RUN_FRAG != 0 &&  
     //      GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&  
         DBG && true) {  
       cout << "* fragment *" << endl;  
     }  
   
     return (true);  
   };  
4258    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4259    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
4260    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3165  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4329  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4329        // update the tracker parameters        // update the tracker parameters
4330        // (non ho trovato nessun altro modo sicuro di farlo...)        // (non ho trovato nessun altro modo sicuro di farlo...)
4331        // ----------------------------------------------------        // ----------------------------------------------------
4332        if (!dbc || (dbc && !dbc->IsConnected()))        if ( fUseDBinRunInfo ){
4333          SetDBConnection();          if (!dbc || (dbc && !dbc->IsConnected()))
4334        TrkParams::Set(GetRunInfo(), dbc);            SetDBConnection();
4335        if (dbc){          TrkParams::Set(GetRunInfo(), dbc);
4336          dbc->Close();          if (dbc){
4337          dbc=0;            dbc->Close();
4338        };            delete dbc;
4339              dbc=0;
4340            }
4341          }
4342        //            cout << endl;        //            cout << endl;
4343        prevshift = 0;        prevshift = 0;
4344          yprevshift = 0;
4345        return true;        return true;
4346      }      }
4347      return false;      return false;
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4350  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4350    return false;    return false;
4351    //    //
4352  }  }
4353  ;  
4354    /**
4355     * Update the runinfo informations (to be used to have Run infos event by event basis)
4356     * @param run Pointer to the chain/tree which contains run infos
4357     * @return true if a new run has been read, false if it is still the same run
4358     */
4359    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4360      return (UpdateRunInfo(iev));
4361    }
4362    
4363  /**  /**
4364   * 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)
4365   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
# Line 3191  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4368  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4368  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4369    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
4370  }  }
4371  ;  
4372    
4373  //--------------------------------------  //--------------------------------------
4374  //  //
# Line 3429  void PamLevel2::GetWhichTrees(TFile* f) Line 4606  void PamLevel2::GetWhichTrees(TFile* f)
4606    //    cout << "Checking file: "<<f->GetName()<<endl;    //    cout << "Checking file: "<<f->GetName()<<endl;
4607    if (!f || f->IsZombie()) {    if (!f || f->IsZombie()) {
4608      cout << "File: " << f->GetName() << " Non valid root file" << endl;      cout << "File: " << f->GetName() << " Non valid root file" << endl;
4609        fDiscarded = true;
4610      return;      return;
4611    }    }
4612    
# Line 3635  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4813  Bool_t PamLevel2::CheckLevel2File(TStrin
4813    TFile *f = new TFile(name.Data());    TFile *f = new TFile(name.Data());
4814    if (!f || f->IsZombie()) {    if (!f || f->IsZombie()) {
4815      cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;      cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
4816        fDiscarded = true;
4817      return false;      return false;
4818    }    }
4819    //    cout << "Get list of keys: "<<f<<endl;    //    cout << "Get list of keys: "<<f<<endl;
# Line 3673  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4852  Bool_t PamLevel2::CheckLevel2File(TStrin
4852          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4853            cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt            cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
4854                << " events instead of " << nev << endl;                << " events instead of " << nev << endl;
4855              fDiscarded = true;
4856            return false;            return false;
4857          }          }
4858          nev = nevt;          nev = nevt;
# Line 3687  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4867  Bool_t PamLevel2::CheckLevel2File(TStrin
4867          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4868            cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
4869                << nev << endl;                << nev << endl;
4870              fDiscarded = true;
4871            return false;            return false;
4872          }          }
4873          nev = nevt;          nev = nevt;
# Line 3700  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4881  Bool_t PamLevel2::CheckLevel2File(TStrin
4881          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4882            cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev            cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
4883                << endl;                << endl;
4884              fDiscarded = true;
4885            return false;            return false;
4886          }          }
4887          nev = nevt;          nev = nevt;
# Line 3713  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4895  Bool_t PamLevel2::CheckLevel2File(TStrin
4895          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4896            cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev            cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
4897                << endl;                << endl;
4898              fDiscarded = true;
4899            return false;            return false;
4900          }          }
4901          nev = nevt;          nev = nevt;
# Line 3727  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4910  Bool_t PamLevel2::CheckLevel2File(TStrin
4910          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4911            cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
4912                << nev << endl;                << nev << endl;
4913              fDiscarded = true;
4914            return false;            return false;
4915          }          }
4916          nev = nevt;          nev = nevt;
# Line 3740  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4924  Bool_t PamLevel2::CheckLevel2File(TStrin
4924          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4925            cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
4926                << nev << endl;                << nev << endl;
4927              fDiscarded = true;
4928            return false;            return false;
4929          }          }
4930          nev = nevt;          nev = nevt;
# Line 3753  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4938  Bool_t PamLevel2::CheckLevel2File(TStrin
4938          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4939            cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
4940                << nev << endl;                << nev << endl;
4941              fDiscarded = true;
4942            return false;            return false;
4943          }          }
4944          nev = nevt;          nev = nevt;
# Line 3766  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4952  Bool_t PamLevel2::CheckLevel2File(TStrin
4952          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4953            cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
4954                << nev << endl;                << nev << endl;
4955              fDiscarded = true;
4956            return false;            return false;
4957          }          }
4958          nev = nevt;          nev = nevt;
# Line 3789  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4976  Bool_t PamLevel2::CheckLevel2File(TStrin
4976          if (nev && nevt != nev) {          if (nev && nevt != nev) {
4977            cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "            cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
4978                << nev << endl;                << nev << endl;
4979              fDiscarded = true;
4980            return false;            return false;
4981          }          }
4982          nev = nevt;          nev = nevt;
# Line 3811  Bool_t PamLevel2::CheckLevel2File(TStrin Line 4999  Bool_t PamLevel2::CheckLevel2File(TStrin
4999          if (nev && nevt != nev) {          if (nev && nevt != nev) {
5000            cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev            cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
5001                << endl;                << endl;
5002              fDiscarded = true;
5003            return false;            return false;
5004          }          }
5005          nev = nevt;          nev = nevt;
# Line 3823  Bool_t PamLevel2::CheckLevel2File(TStrin Line 5012  Bool_t PamLevel2::CheckLevel2File(TStrin
5012      SELLI = (Int_t) SELLI__ok;      SELLI = (Int_t) SELLI__ok;
5013    if (SELLI == 0 && SELLI__ok) {    if (SELLI == 0 && SELLI__ok) {
5014      cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;      cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
5015        fDiscarded = true;
5016      return false;      return false;
5017    }    }
5018    if (SELLI == 1 && !SELLI__ok) {    if (SELLI == 1 && !SELLI__ok) {
5019      cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;      cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
5020        fDiscarded = true;
5021      return false;      return false;
5022    }    }
5023    
# Line 3865  Bool_t PamLevel2::CheckLevel2File(TStrin Line 5056  Bool_t PamLevel2::CheckLevel2File(TStrin
5056    
5057    if (CAL1 && !CAL1__ok) {    if (CAL1 && !CAL1__ok) {
5058      cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
5059        fDiscarded = true;
5060      return false;      return false;
5061    };    };
5062    if (CAL2 && !CAL2__ok) {    if (CAL2 && !CAL2__ok) {
5063      cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
5064        fDiscarded = true;
5065      return false;      return false;
5066    };    };
5067    if (TRK2 && !TRK2__ok) {    if (TRK2 && !TRK2__ok) {
5068      cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
5069        fDiscarded = true;
5070      return false;      return false;
5071    };    };
5072    if (TRK1 && !TRK1__ok) {    if (TRK1 && !TRK1__ok) {
5073      cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
5074        fDiscarded = true;
5075      return false;      return false;
5076    };    };
5077    if (TRKh && !TRKh__ok) {    if (TRKh && !TRKh__ok) {
5078      cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
5079        fDiscarded = true;
5080      return false;      return false;
5081    };    };
5082    if (ORB && !ORB__ok) {    if (ORB && !ORB__ok) {
5083      cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
5084        fDiscarded = true;
5085      return false;      return false;
5086    };    };
5087    if (AC && !AC__ok) {    if (AC && !AC__ok) {
5088      cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
5089        fDiscarded = true;
5090      return false;      return false;
5091    };    };
5092    if (S4 && !S4__ok) {    if (S4 && !S4__ok) {
5093      cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
5094        fDiscarded = true;
5095      return false;      return false;
5096    };    };
5097    if (TOF && !TOF__ok) {    if (TOF && !TOF__ok) {
5098      cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
5099        fDiscarded = true;
5100      return false;      return false;
5101    };    };
5102    
5103    if (ND && !ND__ok) {    if (ND && !ND__ok) {
5104      cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
5105        fDiscarded = true;
5106      return false;      return false;
5107    };    };
5108    if (TRG && !TRG__ok) {    if (TRG && !TRG__ok) {
5109      cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
5110        fDiscarded = true;
5111      return false;      return false;
5112    };    };
5113    if (GP && !GP__ok) {    if (GP && !GP__ok) {
5114      cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
5115        fDiscarded = true;
5116      return false;      return false;
5117    };    };
5118    
# Line 4009  void PamLevel2::CreateCloneTrees(TFile * Line 5212  void PamLevel2::CreateCloneTrees(TFile *
5212      //  cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;      //  cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5213      run_tree_clone->Fill();      run_tree_clone->Fill();
5214    }    }
5215    cout << "----------------------------------------------------" << endl;    if ( PROC ){
5216        proc_tree_clone = new TTree("ProcessingInfo","Log of data processing");
5217        proc_tree_clone->Branch("ProcInfo", "ProcInfo", GetPointerTo("ProcInfo"));
5218        cout << "ProcessingInfo: branch ProcessingInfo" << endl;  
5219        // ------------------
5220        // replicate processinginfo tree
5221        // ------------------
5222        //    cout << "----------------------------------------------------"<<endl;
5223        //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5224        for (Int_t i = 0; i < proc_tree->GetEntries(); i++) {
5225          proc_tree->GetEntry(i);
5226          //        cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5227          proc_tree_clone->Fill();
5228        }
5229        if ( SELLI != 2 || true ){      
5230          proc_obj->runID = 0;
5231          TTimeStamp *dt = new TTimeStamp();
5232          proc_obj->date = dt->AsString();
5233          delete dt;
5234          proc_obj->commandLine = Form("PamelaLevel2 was called");
5235          proc_obj->outputFilename = "";
5236          proc_obj->localDir = gSystem->WorkingDirectory();
5237          proc_obj->uname = gSystem->GetFromPipe("uname -a");
5238          proc_obj->DB = Form("mysql://%s/%s",dbc->GetHost(),dbc->GetDB());
5239          proc_tree_clone->Fill();
5240        }
5241        cout << "----------------------------------------------------" << endl;
5242      }
5243    // ------------------------------------    // ------------------------------------
5244    // add branch with dead and live times    // add branch with dead and live times
5245    // ------------------------------------    // ------------------------------------
# Line 4022  void PamLevel2::CreateCloneTrees(TFile * Line 5251  void PamLevel2::CreateCloneTrees(TFile *
5251      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5252      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5253      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5254        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5255    };    };
5256    
5257    Int_t i = 0;    Int_t i = 0;
# Line 4041  void PamLevel2::CreateCloneTrees(TFile * Line 5271  void PamLevel2::CreateCloneTrees(TFile *
5271        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5272        cout << "Tracker      : branch TrkHough" << endl;        cout << "Tracker      : branch TrkHough" << endl;
5273      };      };
5274        if(NUC){
5275            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5276            cout << "Tracker      : branch TrackNuclei" << endl;    
5277        }
5278        if(EXT){
5279            pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5280            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5281            cout << "Tracker      : branch RecoveredTrack" << endl;
5282            if(NUC){
5283                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5284                cout << "Tracker      : branch RecoveredTrackNuclei" << endl;                  
5285            }
5286        }
5287    
5288      i++;      i++;
5289    }    }
5290    
# Line 4055  void PamLevel2::CreateCloneTrees(TFile * Line 5299  void PamLevel2::CreateCloneTrees(TFile *
5299        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5300        cout << "Calorimeter  : branch CaloLevel2" << endl;        cout << "Calorimeter  : branch CaloLevel2" << endl;
5301      };      };
5302        if(NUC){
5303            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5304            cout << "Calorimeter  : branch TrackNuclei" << endl;    
5305        }
5306        if(EXT){
5307            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5308            cout << "Calorimeter  : branch RecoveredTrack" << endl;
5309            if(NUC){
5310                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5311                cout << "Calorimeter  : branch RecoveredTrackNuclei" << endl;                  
5312            }
5313        }
5314      i++;      i++;
5315    }    }
5316    
# Line 4063  void PamLevel2::CreateCloneTrees(TFile * Line 5319  void PamLevel2::CreateCloneTrees(TFile *
5319      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5320      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5321      cout << "ToF          : branch ToFLevel2" << endl;      cout << "ToF          : branch ToFLevel2" << endl;
5322        if(NUC){
5323            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5324            cout << "ToF          : branch TrackNuclei" << endl;    
5325        }
5326        if(EXT){
5327            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5328            cout << "ToF          : branch RecoveredTrack" << endl;
5329            if(NUC){
5330                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5331                cout << "ToF          : branch RecoveredTrackNuclei" << endl;                  
5332            }
5333        }
5334      i++;      i++;
5335    };    };
5336    // Trigger    // Trigger
# Line 4098  void PamLevel2::CreateCloneTrees(TFile * Line 5366  void PamLevel2::CreateCloneTrees(TFile *
5366      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
5367      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5368      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
5369        if(NUC){
5370            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5371            cout << "OrbitalInfo  : branch TrackNuclei" << endl;    
5372        }
5373        if(EXT){
5374            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5375            cout << "OrbitalInfo  : branch RecoveredTrack" << endl;
5376            if(NUC){
5377                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5378                cout << "OrbitalInfo  : branch RecoveredTrackNuclei" << endl;                  
5379            }
5380        }
5381      i++;      i++;
5382    };    };
5383    // GPamela    // GPamela
5384    if (GP) {    if (GP) {
5385      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5386      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5387      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
5388      i++;      i++;
5389    };    };
5390    
5391    
5392    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5393    
5394  }  }
# Line 4148  TTree* PamLevel2::GetCloneTree(TString n Line 5430  TTree* PamLevel2::GetCloneTree(TString n
5430      if (!name.CompareTo(na))      if (!name.CompareTo(na))
5431        return sel_tree_clone;        return sel_tree_clone;
5432    }    }
5433      if (proc_tree_clone && PROC) {
5434        TString na = proc_tree_clone->GetName();
5435        if (!name.CompareTo(na))
5436          return proc_tree_clone;
5437      }
5438    return NULL;    return NULL;
5439    
5440  }  }
# Line 4167  void PamLevel2::WriteCloneTrees() { Line 5454  void PamLevel2::WriteCloneTrees() {
5454    for (Int_t i = 0; i < NCLONES; i++) {    for (Int_t i = 0; i < NCLONES; i++) {
5455      if (pam_tree_clone[i]) {      if (pam_tree_clone[i]) {
5456        cout << pam_tree_clone[i]->GetName() << endl;        cout << pam_tree_clone[i]->GetName() << endl;
5457        pam_tree_clone[i]->Write();        pam_tree_clone[i]->Write(pam_tree_clone[i]->GetName(),TObject::kOverwrite);
5458      };      };
5459    }    }
5460      
5461      if ( PROC ){
5462        proc_tree_clone->Write("ProcessingInfo",TObject::kOverwrite);
5463      }
5464    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5465    
5466  }  }
# Line 4177  void PamLevel2::WriteCloneTrees() { Line 5468  void PamLevel2::WriteCloneTrees() {
5468  /**  /**
5469   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
5470   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
5471  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
5472    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
5473    if (!pam_tree) {    if (!pam_tree) {
5474      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
5475      return 0;      return 0;
# Line 4196  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5483  Int_t PamLevel2::GetEntry(Long64_t iee)
5483    //    return 0;    //    return 0;
5484    //    }    //    }
5485    
   Long64_t ii = 0;  
5486    //-------------------------------    //-------------------------------
5487    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
5488      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
5489      return 0;      return 0;
5490    }    }
# Line 4219  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5504  Int_t PamLevel2::GetEntry(Long64_t iee)
5504    }    }
5505    
5506    //-------------------------------    //-------------------------------
5507    ii = iee;    //
5508    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5509    //    Bool_t UPDATED = UpdateRunInfo(ii);    if (SELLI == 0 || SELLI == 2) irunentry = iee - runfirstentry;
   UpdateRunInfo(ii);  
   if (SELLI == 0 || SELLI == 2)  
     irunentry = iee - runfirstentry;  
   //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();  
   
   //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  
   
   //     cout << "irunentry     "<<irunentry << endl;  
   //     cout << "runfirstentry "<<runfirstentry << endl;  
   //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;  
   
   //     if( TRK0 || CAL0 || TOF0 ){  
   //    if( !GetYodaEntry( ) ){  
   //        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;  
   //        return 0;  
   //    }  
   //     }  
   
5510    
5511    return 1;    return 1;
5512    
# Line 4280  TTree* PamLevel2::GetYodaTree() { Line 5547  TTree* PamLevel2::GetYodaTree() {
5547    //===================================    //===================================
5548    if (irun < 0) {    if (irun < 0) {
5549      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5550      //  cout << "In order to use this method you have to first load the RunInfo tree "<<endl;      if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
5551      return NULL;      return NULL;
5552    }    }
5553    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5554    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
5555    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
5556        cout << "irootnew "<<irootnew<<endl;
5557      }
5558    
5559    //===================================    //===================================
5560    // load the level0 file    // load the level0 file
# Line 4323  TTree* PamLevel2::GetYodaTree() { Line 5592  TTree* PamLevel2::GetYodaTree() {
5592      if (!h0_obj)      if (!h0_obj)
5593        h0_obj = new EventHeader();        h0_obj = new EventHeader();
5594      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
5595      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5596      //---------------------------------------------------      //---------------------------------------------------
5597      // TRACKER:      // TRACKER:
5598      if (TRK0) {      if (TRK0) {
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 5610  TTree* PamLevel2::GetYodaTree() {
5610          calo0_obj->Set();          calo0_obj->Set();
5611        };        };
5612        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
5613      }      }
5614      //---------------------------------------------------      //---------------------------------------------------
5615      // TOF:      // TOF:
# Line 4350  TTree* PamLevel2::GetYodaTree() { Line 5618  TTree* PamLevel2::GetYodaTree() {
5618      }      }
5619    
5620      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5621        delete dbc;
5622      dbc=0;      dbc=0;
5623    
5624    };    };
5625    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
5626    if (TRK0) {    if (TRK0) {
5627      //  TrkParams::Load(6);      if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
     //  if( !TrkParams::IsLoaded(6) ){  
     //      cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;  
     //  };  
     if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection(); // PERCHE` ERA COMMENTATA QUESTA RIGA?  
5628      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
5629      TrkParams::LoadCalib();      TrkParams::LoadCalib();
5630      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
# Line 4371  TTree* PamLevel2::GetYodaTree() { Line 5632  TTree* PamLevel2::GetYodaTree() {
5632      };      };
5633      if(dbc){      if(dbc){
5634        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5635          delete dbc;
5636        dbc=0;        dbc=0;
5637      };      };
5638    }    }
   
   //    cout << l0_tree << endl;  
5639    return l0_tree;    return l0_tree;
   
5640  }  }
5641    
5642  /**  /**
# Line 4385  TTree* PamLevel2::GetYodaTree() { Line 5644  TTree* PamLevel2::GetYodaTree() {
5644   */   */
5645  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
5646    
5647    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
5648    
5649    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5650    if (irunentry < 0) {    // if it is a full file (not preselected)
5651      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5652      irunentry = 0LL;    //  if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
   }  
   //  ---------------------------------  
   //  if file is NOT a preselected file  
   //  ---------------------------------  
   Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);  
   //     cout << " irunentry "<<irunentry<<endl;  
   //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;  
   //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;  
   
   //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;  
   //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;  
   //     cout << " time "<< abstime << endl;  
   //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;  
5653    
5654    ULong64_t obt = 0;      if (!GetYodaTree()){
5655    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5656    if (GetOrbitalInfo()) {        return 0;
5657      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
5658    
5659    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
5660      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5661      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5662    }        irunentry = 0LL;
5663    if (obt == 0 && pktn == 0 && !ISGP) {      }
5664      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
5665      return 0;      //  if file is NOT a preselected file
5666    }      //  ---------------------------------
5667        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5668    
5669        if (DBG){
5670          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5671          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5672          cout << " time "<< abstime << endl;
5673        }
5674    
5675        ULong64_t obt = 0;
5676        ULong64_t pktn = 0;
5677        if (GetOrbitalInfo()) {
5678          obt = GetOrbitalInfo()->OBT;
5679          pktn = GetOrbitalInfo()->pkt_num;
5680        }
5681    
5682    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
5683    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5684    // If data are not pre-processed with cleaner, the level0 tree may contain        return 0;
   // spurious nested physics packets.  
   // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries  
   // while level2 tree DOES NOT!!  
   // This means that "quellagiusta" in these cases is not correct.  
   // In order to retrieve the correct level0 event, I implemented a check  
   // of the OBT and pkt-number. In case of mismatch, the level0 entry number  
   // is shift forward until when the packets match.  
   // ---------------------------------------------------------------------  
   Int_t answer = 0;  
   Int_t shift = 0;  
   //    printf(" siamo qui %i %i \n",shift,prevshift);  
   //    Int_t maxshift = 50; // EMILIANO  
   do {  
     if (shift > 0) {  
       cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;  
       if (DBG)  
         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;  
       if (DBG)  
         cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift  
             << " prevshift " << prevshift << " )" << endl;  
5685      }      }
5686      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
5687      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
5688        return 0;        return 0;
5689      }      }
5690    
5691        // ---------------------------------------------------------------------
5692        // ATTENTION!!!
5693        // If data are not pre-processed with cleaner, the level0 tree may contain
5694        // spurious nested physics packets.
5695        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
5696        // while level2 tree DOES NOT!!
5697        // This means that "quellagiusta" in these cases is not correct.
5698        // In order to retrieve the correct level0 event, I implemented a check
5699        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
5700        // is shift forward until when the packets match.
5701        // ---------------------------------------------------------------------
5702        Long64_t shift = 0LL;
5703        Long64_t answer = quellagiusta + shift + yprevshift;
5704        Int_t readl0 = 0;
5705        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5706    
5707        if (DBG){
5708          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5709        }
5710    
5711    
5712      if (ISGP) {      if (ISGP) {
5713        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5714        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5715      }      }
5716    
5717      //  cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;      while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5718      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
5719      //  if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
5720      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5721      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
5722      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5723      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
5724      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
5725      //        if (shift > 0) {
5726      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5727        prevshift = 0;          if (DBG)
5728        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5729      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5730            if (DBG)
5731              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5732                   << " prevshift " << prevshift << " )" << endl;
5733          }
5734          answer = quellagiusta +  shift+ yprevshift;
5735          readl0 = l0_tree->GetEntry(answer);
5736          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5737    
5738    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        if (!GetEventHeader()) {
5739        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5740        < GetYodaTree()->GetEntries() && shift < maxshift);          return 0;
5741          }
5742    
5743    if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {        //
5744      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5745      return 0;          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5746    }          //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5747    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          yprevshift = 0LL;
5748    //    return GetYodaTree()->GetEntry(quellagiusta);          shift = -1LL;
5749    if (shift > 1)        };
5750      prevshift += (shift - 1);      
5751          shift++;
5752        }
5753                                      
5754    
5755        if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5756          if ( isSync ){
5757            printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5758            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5759            cout << "\nFor bug reporting instructions, please see for example:\n";
5760            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5761            cout << "  " << endl;
5762          }
5763          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5764          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
5765          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5766          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5767          // ma nel mezzo del cammin per ogni run che pesco devo vedere la posizione relativa di iev rispetto al runheader nel livello2 per andare a cercare nel posto giusto
5768          // connect to db
5769          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5770          //
5771          if (GetOrbitalInfo()){
5772            abstime = GetOrbitalInfo()->absTime;
5773          } else {
5774            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5775            return 0;
5776          }
5777          // query DB looking for runs containing the processed event
5778          TSQLResult *pResult;
5779          TSQLRow *Row = NULL;
5780          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5781          if ( DBG ) printf(" query is %s \n",myquery.Data());
5782          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5783          pResult = dbc->Query(myquery.Data());    
5784          if (!pResult->GetRowCount()){
5785            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5786            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5787            cout << "\nFor bug reporting instructions, please see for example:\n";
5788            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5789            cout << "  " << endl;
5790            return 0;
5791          }
5792          if ( pResult->GetRowCount() == 1 ){
5793            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5794            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5795          }
5796          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5797            if (Row) delete Row;
5798            Row = pResult->Next();  
5799            if( Row == NULL ) break;
5800            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5801            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5802            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5803            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5804    
5805            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5806            Bool_t rfound = false;
5807            totrunentry = 0LL;
5808            runfirstentry = 0LL;
5809            for (Int_t r=0; r< run_tree->GetEntries();r++){
5810              run_tree->GetEntry(r);//update runinfo
5811              if ( r > 0 ){
5812                totrunentrymin = totrunentrymax+1;
5813              } else {
5814                totrunentrymin = 0LL;
5815              }
5816              totrunentry += GetRunInfo()->NEVENTS;
5817              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5818              irun = r;        
5819    
5820              if (idrun == GetRunInfo()->ID){
5821                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5822                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5823                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5824                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5825                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5826                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
5827                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
5828                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
5829                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5830                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5831                }
5832                runfirstentry = totrunentrymin; // first entry of the run in the level2
5833              
5834    
5835                //
5836                if (gltsync)
5837                  delete gltsync; // Emiliano
5838                if (!dbc || (dbc && !dbc->IsConnected()))
5839                  SetDBConnection(); //Emiliano
5840                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
5841                if (dbc){
5842                  dbc->Close(); // Emiliano
5843                  delete dbc;
5844                  dbc=0;
5845                }          
5846                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
5847                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
5848                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
5849                  cout
5850                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
5851                    << endl;
5852                }
5853                //
5854                if (DBG) printf(" found \n");
5855                //            printf(" found \n");//TOGLITOGLI
5856                rfound = true;
5857                //
5858                break;
5859              }
5860            } // loop over run
5861            if ( !rfound ){
5862              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
5863              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5864              cout << "\nFor bug reporting instructions, please see for example:\n";
5865              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5866              cout << "  " << endl;        
5867              return 0;
5868            }
5869    
5870    return answer;          // here we got the first run and we can check if it contains the level0 event
5871            if (!GetYodaTree()){
5872              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5873              return 0;
5874            }      
5875    
5876            // get the current run entry
5877            irunentry = iev - runfirstentry;
5878            if (irunentry < 0) {
5879              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5880              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5881              irunentry = 0LL;
5882            }
5883            //  ---------------------------------
5884            //  if file is NOT a preselected file
5885            //  ---------------------------------
5886            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5887          
5888            if (DBG){
5889              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5890              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5891              cout << " time "<< abstime << endl;
5892            }
5893            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5894            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5895            //        cout << " time "<< abstime << endl; // TOGLITOGLI
5896          
5897            shift = 0;
5898            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
5899            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5900    
5901            if (DBG){
5902              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5903            }
5904            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5905          
5906            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5907              if (shift > 0) {
5908                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5909                if (DBG)
5910                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5911                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5912                if (DBG)
5913                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5914                       << " prevshift " << prevshift << " )" << endl;
5915              }
5916              answer = quellagiusta +  shift+ yprevshift;
5917              readl0 = l0_tree->GetEntry(answer);
5918              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5919            
5920              if (!GetEventHeader()) {
5921                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5922                return 0;
5923              }
5924              //
5925              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5926                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5927                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5928                yprevshift = 0;
5929                shift = -1;
5930              };
5931            
5932              shift++;
5933            }
5934          
5935            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5936              //still not the good run... continue with the nex one!
5937              printf("still not the good run... continue with the nex one!\n");
5938            } else {
5939              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
5940              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
5941              if (shift > 1) yprevshift = (shift - 1);
5942              if (Row) delete Row;
5943              delete pResult;  
5944              if (dbc){
5945                dbc->Close(); // Emiliano
5946                delete dbc;
5947                dbc=0;
5948              }    
5949              il0entry = answer;
5950              return readl0;
5951            }
5952            // perhaps it is all
5953          }// loop over runs containing the event
5954          if (Row) delete Row;
5955          delete pResult;  
5956          if (dbc){
5957            dbc->Close(); // Emiliano
5958            delete dbc;
5959            dbc=0;
5960          }          
5961          // arriving here it means no run found, cannot be! error!
5962          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
5963          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5964          cout << "\nFor bug reporting instructions, please see for example:\n";
5965          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5966          cout << "  " << endl;        
5967          return 0;
5968        } else {
5969          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5970          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5971          //    printf("obt %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) %i  pktn %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter() %i \n",obt,(UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()), pktn, (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) );
5972          if (shift > 1) yprevshift = (shift - 1);
5973          il0entry = answer;
5974          return readl0;
5975        }
5976    
5977        /*  } // if selli 0 || 2
5978      if ( SELLI == 1 && hasL0EE ){
5979        sel_tree->GetEntry(iev);  
5980        Long64_t answer = il0entry;
5981        Int_t readl0 = 0;
5982        readl0 = l0_tree->GetEntry(answer);
5983        return readl0;
5984        }*/
5985      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
5986      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5987      cout << "\nFor bug reporting instructions, please see for example:\n";
5988      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5989      cout << "  " << endl;
5990      return 0;
5991  }  }
5992    
5993  /**  /**
5994   * \Brief Set DB connection   * \Brief Set DB connection
5995   */   */
# Line 4582  Long64_t PamLevel2::GetReadEntry() { Line 6082  Long64_t PamLevel2::GetReadEntry() {
6082  void PamLevel2::SetSortingMethod(TString how) {  void PamLevel2::SetSortingMethod(TString how) {
6083    if (howtosort != how) {    if (howtosort != how) {
6084      issorted = false;      issorted = false;
6085        issorted_new = false;
6086    }    }
6087    howtosort = how;    howtosort = how;
6088  }  }

Legend:
Removed from v.1.82  
changed lines
  Added in v.1.102

  ViewVC Help
Powered by ViewVC 1.1.23