/[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.81 by pam-fi, Wed Apr 1 09:25:04 2009 UTC revision 1.95 by pam-ts, Wed Oct 15 08:51:51 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  ;  ;
# Line 335  PamTrack::PamTrack(const PamTrack& track Line 391  PamTrack::PamTrack(const PamTrack& track
391    if (r)    if (r)
392      orb_track = new OrbitalInfoTrkVar(*r);      orb_track = new OrbitalInfoTrkVar(*r);
393    candeleteobj = 1;    candeleteobj = 1;
394      pscore = 0;
395      iscore = 0;
396    
397  }  }
398  void PamTrack::Clear() {  void PamTrack::Clear() {
399    
400    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;
401    if (candeleteobj) {    if (candeleteobj) {
402          
403        if (trk_ext_track)
404          trk_ext_track->ExtTrack::Clear();
405      if (trk_track)      if (trk_track)
406        trk_track->TrkTrack::Clear();        trk_track->TrkTrack::Clear();
407      if (calo_track)      if (calo_track)
# Line 351  void PamTrack::Clear() { Line 412  void PamTrack::Clear() {
412        orb_track->OrbitalInfoTrkVar::Clear();//???        orb_track->OrbitalInfoTrkVar::Clear();//???
413    }    }
414    else {    else {
415        trk_ext_track = 0;
416      trk_track = 0;      trk_track = 0;
417      calo_track = 0;      calo_track = 0;
418      tof_track = 0;      tof_track = 0;
# Line 363  void PamTrack::Clear() { Line 425  void PamTrack::Clear() {
425  void PamTrack::Delete() {  void PamTrack::Delete() {
426    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
427    if (candeleteobj) {    if (candeleteobj) {
428        if (trk_ext_track) {
429          trk_ext_track->ExtTrack::Clear();
430          delete trk_ext_track;
431        }
432      if (trk_track) {      if (trk_track) {
433        trk_track->TrkTrack::Clear();        trk_track->TrkTrack::Clear();
434        delete trk_track;        delete trk_track;
# Line 384  void PamTrack::Delete() { Line 450  void PamTrack::Delete() {
450      Clear();      Clear();
451    }    }
452  }  }
453    
454    
455    
456    
457  //--------------------------------------  //--------------------------------------
458  //  //
459  //  //
# Line 394  void PamTrack::Delete() { Line 464  void PamTrack::Delete() {
464  PamLevel2::PamLevel2() {  PamLevel2::PamLevel2() {
465    Initialize();    Initialize();
466  }  }
467  ;  
468    
469  /**  /**
470   * Constructor   * Constructor
# Line 417  PamLevel2::PamLevel2(TString ddir, TStri Line 487  PamLevel2::PamLevel2(TString ddir, TStri
487      GetPamTree(listf, detlist);      GetPamTree(listf, detlist);
488    if (listf)    if (listf)
489      GetRunTree(listf);      GetRunTree(listf);
490      SetMaxShift(-1);
491  }  }
492  ;  
493    
494  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
495    Initialize();    Initialize();
496    GetPamTree(llist, detlist);    GetPamTree(llist, detlist);
497    GetRunTree(llist);    GetRunTree(llist);
498      SetMaxShift(-1);
499  }  }
500  ;  
501  /**  /**
502   * Constructor   * Constructor
503   * @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 507  PamLevel2::PamLevel2(TString ddir, TList
507   */   */
508  PamLevel2::PamLevel2(TString ddir, TString llist) {  PamLevel2::PamLevel2(TString ddir, TString llist) {
509    Initialize();    Initialize();
510    TList* listf = GetListOfLevel2Files(ddir, llist);    TList* listf = GetListOfLevel2Files(ddir, llist);
511      cout << "GetPamTree: "<<endl;
512    GetPamTree(listf, "");    GetPamTree(listf, "");
513      cout << "GetRunTree: "<<endl;
514    GetRunTree(listf);    GetRunTree(listf);
515      SetMaxShift(-1);
516  }  }
517  ;  
518    
519  void PamLevel2::Initialize() {  void PamLevel2::Initialize() {
520    
# Line 452  void PamLevel2::Initialize() { Line 527  void PamLevel2::Initialize() {
527    trkh_obj = 0;    trkh_obj = 0;
528    calo1_obj = 0;    calo1_obj = 0;
529    calo2_obj = 0;    calo2_obj = 0;
530    tof_obj = 0;    tof2_obj = 0;
531    trig_obj = 0;    trig_obj = 0;
532    s4_obj = 0;    s4_obj = 0;
533    nd_obj = 0;    nd_obj = 0;
534    ac_obj = 0;    ac_obj = 0;
535    orb_obj = 0;    orb2_obj = 0;
536    gp_obj = 0;    gp_obj = 0;
537    
538      extAlgFlag=0;
539    
540      trk_ext_obj     = 0;
541      trk_ext_nuc_obj = 0;
542      trk_nuc_obj     = 0;
543      
544      calo_ext_obj     = 0;
545      calo_ext_nuc_obj = 0;
546      calo_nuc_obj     = 0;
547      
548      tof_ext_obj     = 0;
549      tof_ext_nuc_obj = 0;
550      tof_nuc_obj     = 0;
551      
552      orb_ext_obj     = 0;
553      orb_ext_nuc_obj = 0;
554      orb_nuc_obj     = 0;
555    
556      trk2_nuc_obj  = 0;
557      calo2_nuc_obj = 0;
558      tof2_nuc_obj  = 0;
559      orb2_nuc_obj  = 0;
560    
561    
562    run_obj = 0;//new GL_RUN();    run_obj = 0;//new GL_RUN();
563    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
564    irun = -1LL;    irun = -1LL;
565    irunt = -1LL;    irunt = -1LL;
566      totrunentry = 0LL;
567      totrunentrymax = 0LL;
568      totrunentrymin = 0LL;
569    runfirstentry = 0LL;    runfirstentry = 0LL;
570    runlastentry = 0LL;    runlastentry = 0LL;
571    gltsync = 0; // Emiliano    gltsync = 0; // Emiliano
572      fUpdateRunInfo = true; // Emiliano
573      fUseDBinRunInfo = true; // Emiliano
574      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
575      il0entry = 0LL;
576      //  hasL0EE = true;
577    
578    l0_file = NULL;    l0_file = NULL;
579    l0_tree = NULL;    l0_tree = NULL;
# Line 474  void PamLevel2::Initialize() { Line 581  void PamLevel2::Initialize() {
581    dbc = 0;    dbc = 0;
582    
583    prevshift = 0;    prevshift = 0;
584    maxshift = 10; //EMILIANO    yprevshift = 0;
585      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
586    
587    run_tree = NULL;    run_tree = NULL;
588    run_tree_clone = NULL;    run_tree_clone = NULL;
# Line 527  void PamLevel2::Initialize() { Line 635  void PamLevel2::Initialize() {
635    ORB = true;    ORB = true;
636    GP = false;    GP = false;
637    
638      EXT = false;
639      NUC = false;
640      trkAlg = "";//default tracking algorythm
641    
642    RUN = true;    RUN = true;
643    
644    SELLI = -1;    SELLI = -1;
# Line 537  void PamLevel2::Initialize() { Line 649  void PamLevel2::Initialize() {
649    
650    tsorted = 0;    tsorted = 0;
651    timage = 0;    timage = 0;
652      text = 0 ;
653    
654      tsorted_nuc = 0;
655      timage_nuc = 0;
656      text_nuc = 0 ;
657    
658    howtosort = "+CAL+TOF";    howtosort = "+CAL+TOF";
659    //howtosort = "+TOF";    //howtosort = "+TOF";
# Line 544  void PamLevel2::Initialize() { Line 661  void PamLevel2::Initialize() {
661    
662    issorted = false;    issorted = false;
663    lastsorted = -1;    lastsorted = -1;
664      issorted_new = false;
665      lastsorted_new = -1;
666    
667  }  }
668  ;  ;
# Line 574  void PamLevel2::Delete() { Line 693  void PamLevel2::Delete() {
693      delete calo1_obj;      delete calo1_obj;
694    if (calo2_obj)    if (calo2_obj)
695      delete calo2_obj;      delete calo2_obj;
696    if (tof_obj)    if (tof2_obj)
697      delete tof_obj;      delete tof2_obj;
698    if (trig_obj)    if (trig_obj)
699      delete trig_obj;      delete trig_obj;
700    if (s4_obj)    if (s4_obj)
# Line 584  void PamLevel2::Delete() { Line 703  void PamLevel2::Delete() {
703      delete nd_obj;      delete nd_obj;
704    if (ac_obj)    if (ac_obj)
705      delete ac_obj;      delete ac_obj;
706    if (orb_obj)    if (orb2_obj)
707      delete orb_obj;      delete orb2_obj;
708    if (gp_obj)    if (gp_obj)
709      delete gp_obj;      delete gp_obj;
710    
711      if(trk_nuc_obj)trk_nuc_obj->Delete();
712      if(trk_ext_obj)trk_ext_obj->Delete();
713      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Delete();
714    
715      if(calo_nuc_obj)calo_nuc_obj->Delete();
716      if(calo_ext_obj)calo_ext_obj->Delete();
717      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Delete();
718    
719      if(tof_nuc_obj)tof_nuc_obj->Delete();
720      if(tof_ext_obj)tof_ext_obj->Delete();
721      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Delete();
722    
723      if(orb_nuc_obj)orb_nuc_obj->Delete();
724      if(orb_ext_obj)orb_ext_obj->Delete();
725      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Delete();
726    
727    
728      if(trk2_nuc_obj)trk2_nuc_obj->Delete();;
729      if( calo2_nuc_obj)calo2_nuc_obj->Delete();;
730      if(tof2_nuc_obj)tof2_nuc_obj->Delete();;
731      if(orb2_nuc_obj)orb2_nuc_obj->Delete();;
732      
733    
734    
735    if (tsorted) {    if (tsorted) {
736      tsorted->Delete();      tsorted->Delete();
737      delete tsorted;      delete tsorted;
# Line 597  void PamLevel2::Delete() { Line 740  void PamLevel2::Delete() {
740      timage->Delete();      timage->Delete();
741      delete timage;      delete timage;
742    }    }
743      if (text) {
744        text->Delete();
745        delete text;
746      }
747      if (tsorted_nuc) {
748        tsorted_nuc->Delete();
749        delete tsorted_nuc;
750      }
751      if (timage_nuc) {
752        timage_nuc->Delete();
753        delete timage_nuc;
754      }
755      if (text_nuc) {
756        text_nuc->Delete();
757        delete text_nuc;
758      }
759    
760    
761    
762    if (dbc) {    if (dbc) {
763      dbc->Close();      dbc->Close();
764      delete dbc;      delete dbc;
765        dbc=0;
766    }    }
767    
768    if (gltsync)    if (gltsync)
# Line 632  void PamLevel2::Delete() { Line 794  void PamLevel2::Delete() {
794      //      //
795      TIter next2(contents);      TIter next2(contents);
796      TChain *questa = 0;      TChain *questa = 0;
797      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*)next2()) ) {
798        TString name = questa->GetName();        TString name = questa->GetName();
799        questa->Delete();        questa->Delete();
800        questa = NULL;        questa = NULL;
# Line 645  void PamLevel2::Delete() { Line 807  void PamLevel2::Delete() {
807      run_tree->Delete();;      run_tree->Delete();;
808    if (sel_tree)    if (sel_tree)
809      sel_tree->Delete();;      sel_tree->Delete();;
810    for (Int_t i = 0; i < NCLONES; i++)  
811      // The following lines are commented out since they may generate a double delete error
812      // if the file containing the clone trees is closed. This is because the file owns the
813      // clone trees which are written into it, so it will delete them when it is closed; if
814      // also PamLevel2 will try to delete these trees, a double delete error will be generated
815      // when exiting from analysis program. (Nicola 28/11/2011)
816    
817      /*for (Int_t i = 0; i < NCLONES; i++)
818      if (pam_tree_clone[i])      if (pam_tree_clone[i])
819        pam_tree_clone[i]->Delete();;        pam_tree_clone[i]->Delete();;
820    if (run_tree_clone)    if (run_tree_clone)
821      run_tree_clone->Delete();;      run_tree_clone->Delete();;
822    if (sel_tree_clone)    if (sel_tree_clone)
823      sel_tree_clone->Delete();;      sel_tree_clone->Delete();;*/
824    
825    if (irunoffset)    if (irunoffset)
826      delete[] irunoffset;      delete[] irunoffset;
827    
828    
829      Initialize();
830    
831  }  }
832  ;  ;
833    
# Line 689  void PamLevel2::Clear() { Line 861  void PamLevel2::Clear() {
861      calo1_obj->Clear();      calo1_obj->Clear();
862    if (calo2_obj)    if (calo2_obj)
863      calo2_obj->Clear();      calo2_obj->Clear();
864    if (tof_obj)    if (tof2_obj)
865      tof_obj->Clear();      tof2_obj->Clear();
866    if (trig_obj)    if (trig_obj)
867      trig_obj->Clear();      trig_obj->Clear();
868    if (s4_obj)    if (s4_obj)
# Line 699  void PamLevel2::Clear() { Line 871  void PamLevel2::Clear() {
871      nd_obj->Clear();      nd_obj->Clear();
872    if (ac_obj)    if (ac_obj)
873      ac_obj->Clear();      ac_obj->Clear();
874    if (orb_obj)    if (orb2_obj)
875      orb_obj->Clear();      orb2_obj->Clear();
876    if (gp_obj)    if (gp_obj)
877      gp_obj->Clear();      gp_obj->Clear();
878    
879    //    if(sorted_tracks)sorted_tracks->Clear();    //    if(sorted_tracks)sorted_tracks->Clear();
880    //    sorted_tracks.Clear();    //    sorted_tracks.Clear();
881    
882    if (tsorted) {    if(trk_nuc_obj)trk_nuc_obj->Clear();
883      tsorted->Delete();    if(trk_ext_obj)trk_ext_obj->Clear();
884    }    if(trk_ext_nuc_obj)trk_ext_nuc_obj->Clear();
885    if (timage) {  
886      timage->Delete();    if(calo_nuc_obj)calo_nuc_obj->Clear();
887    }    if(calo_ext_obj)calo_ext_obj->Clear();
888      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Clear();
889    
890      if(tof_nuc_obj)tof_nuc_obj->Clear();
891      if(tof_ext_obj)tof_ext_obj->Clear();
892      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Clear();
893    
894      if(orb_nuc_obj)orb_nuc_obj->Clear();
895      if(orb_ext_obj)orb_ext_obj->Clear();
896      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Clear();
897    
898      if(trk2_nuc_obj)trk2_nuc_obj->Clear();;
899      if( calo2_nuc_obj)calo2_nuc_obj->Clear();;
900      if(tof2_nuc_obj)tof2_nuc_obj->Clear();;
901      if(orb2_nuc_obj)orb2_nuc_obj->Clear();;
902    
903      if (tsorted)tsorted->Delete();
904      if (timage)timage->Delete();
905      if (text) text->Delete();
906    
907      if (tsorted_nuc)tsorted_nuc->Delete();
908      if (timage_nuc)timage_nuc->Delete();
909      if (text_nuc) text_nuc->Delete();
910  }  }
911  ;  ;
912    
# Line 746  void PamLevel2::Reset() { Line 940  void PamLevel2::Reset() {
940      //      //
941      TIter next2(contents);      TIter next2(contents);
942      TChain *questa = 0;      TChain *questa = 0;
943      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*) next2()) ) {
944        TString name = questa->GetName();        TString name = questa->GetName();
945        questa->Delete();        questa->Delete();
946        questa = NULL;        questa = NULL;
# Line 777  void PamLevel2::Reset() { Line 971  void PamLevel2::Reset() {
971    trkh_obj = 0;    trkh_obj = 0;
972    calo1_obj = 0;    calo1_obj = 0;
973    calo2_obj = 0;    calo2_obj = 0;
974    tof_obj = 0;    tof2_obj = 0;
975    trig_obj = 0;    trig_obj = 0;
976    s4_obj = 0;    s4_obj = 0;
977    nd_obj = 0;    nd_obj = 0;
978    ac_obj = 0;    ac_obj = 0;
979    orb_obj = 0;    orb2_obj = 0;
980    gp_obj = 0;    gp_obj = 0;
981    
982      trk_ext_obj     = 0;
983      trk_ext_nuc_obj = 0;
984      trk_nuc_obj     = 0;
985      
986      calo_ext_obj     = 0;
987      calo_ext_nuc_obj = 0;
988      calo_nuc_obj     = 0;
989      
990      tof_ext_obj     = 0;
991      tof_ext_nuc_obj = 0;
992      tof_nuc_obj     = 0;
993      
994      orb_ext_obj     = 0;
995      orb_ext_nuc_obj = 0;
996      orb_nuc_obj     = 0;
997    
998      trk2_nuc_obj  = 0;
999      calo2_nuc_obj = 0;
1000      tof2_nuc_obj  = 0;
1001      orb2_nuc_obj  = 0;
1002    
1003      trk2_nuc_obj  = 0;
1004      calo2_nuc_obj = 0;
1005      tof2_nuc_obj  = 0;
1006      orb2_nuc_obj  = 0;
1007    //    //
1008    // Reset run pointers    // Reset run pointers
1009    //    //
# Line 791  void PamLevel2::Reset() { Line 1011  void PamLevel2::Reset() {
1011    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
1012    irun = -1;    irun = -1;
1013    irunt = -1;    irunt = -1;
1014      totrunentry = 0LL;
1015      totrunentrymax = 0LL;
1016      totrunentrymin = 0LL;
1017    runfirstentry = 0ULL;    runfirstentry = 0ULL;
1018    runlastentry = 0ULL;    runlastentry = 0ULL;
1019      prevabstime = 0ULL;
1020      prevpktnum = 0;
1021      abstime = 0ULL;
1022      pktnum = 0;
1023      isFragment = false;
1024    //    //
1025    totdltime[0] = 0LL;    totdltime[0] = 0LL;
1026    totdltime[1] = 0LL;    totdltime[1] = 0LL;
# Line 810  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1038  Bool_t PamLevel2::IsGood(Bool_t strict)
1038    if (strict) {    if (strict) {
1039      if (trk2_obj && trk2_obj->UnpackError() != 0)      if (trk2_obj && trk2_obj->UnpackError() != 0)
1040        goodev = false;        goodev = false;
1041      if (tof_obj && tof_obj->unpackError != 0)      if (tof2_obj && tof2_obj->unpackError != 0)
1042        goodev = false;        goodev = false;
1043      if (trig_obj && trig_obj->unpackError != 0)      if (trig_obj && trig_obj->unpackError != 0)
1044        goodev = false;        goodev = false;
# Line 820  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1048  Bool_t PamLevel2::IsGood(Bool_t strict)
1048        goodev = false;        goodev = false;
1049      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)))
1050        goodev = false;        goodev = false;
1051      //  if(orb_obj)      //  if(orb2_obj)
1052    }    }
1053    else {    else {
1054      if (nd_obj && nd_obj->unpackError != 0)      if (nd_obj && nd_obj->unpackError != 0)
# Line 832  Bool_t PamLevel2::IsGood(Bool_t strict) Line 1060  Bool_t PamLevel2::IsGood(Bool_t strict)
1060  }  }
1061  ;  ;
1062    
1063    void PamLevel2::SkipRunInfoUpdate(){
1064      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
1065      fUpdateRunInfo = false;
1066      this->SetSELLI(2);
1067      printf(" ===============> W A R N I N G <================ \n");
1068      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
1069      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
1070      if ( run_tree_clone ){
1071        printf(" ===============> W A R N I N G <================ \n");
1072        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
1073        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
1074      };
1075    }
1076    
1077    void PamLevel2::SetMaxShift(Int_t sh){
1078      if ( sh >=  0 ){
1079        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");
1080        maxshift = sh;
1081      } else {
1082        ULong64_t nev = GetEntries();
1083        ULong64_t runnev = 0ULL;
1084        for (Int_t r=0; r< run_tree->GetEntries();r++){
1085          run_tree->GetEntry(r);//update runinfo
1086          runnev += GetRunInfo()->NEVENTS;
1087        }
1088        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
1089        if ( (runnev-nev) == 0 ) isSync = true;
1090        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);
1091        //    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
1092      }
1093    }
1094    
1095  //--------------------------------------  //--------------------------------------
1096  //  //
1097  //  //
# Line 874  void *PamLevel2::GetPointerTo(const char Line 1134  void *PamLevel2::GetPointerTo(const char
1134      return &calo2_obj;      return &calo2_obj;
1135    };    };
1136    if (!objname.CompareTo("ToFLevel2")) {    if (!objname.CompareTo("ToFLevel2")) {
1137      if (!tof_obj) {      if (!tof2_obj) {
1138        tof_obj = new ToFLevel2();        tof2_obj = new ToFLevel2();
1139        tof_obj->Set();        tof2_obj->Set();
1140      }      }
1141      return &tof_obj;      return &tof2_obj;
1142    };    };
1143    if (!objname.CompareTo("TrigLevel2")) {    if (!objname.CompareTo("TrigLevel2")) {
1144      if (!trig_obj)      if (!trig_obj)
# Line 901  void *PamLevel2::GetPointerTo(const char Line 1161  void *PamLevel2::GetPointerTo(const char
1161      return &ac_obj;      return &ac_obj;
1162    };    };
1163    if (!objname.CompareTo("OrbitalInfo")) {    if (!objname.CompareTo("OrbitalInfo")) {
1164      if (!orb_obj) {      if (!orb2_obj) {
1165        orb_obj = new OrbitalInfo();        orb2_obj = new OrbitalInfo();
1166        orb_obj->Set();        orb2_obj->Set();
1167      }      }
1168      return &orb_obj;      return &orb2_obj;
1169    };    };
1170    //     if(!objname.CompareTo("OrbitalInfo")){    //     if(!objname.CompareTo("OrbitalInfo")){
1171    //    if(!orb_obj)   orb_obj   = new OrbitalInfo();    //    if(!orb2_obj)   orb2_obj   = new OrbitalInfo();
1172    //    return &orb_obj;    //    return &orb2_obj;
1173    //     };    //     };
1174    if (!objname.CompareTo("GPamela")) {    if (!objname.CompareTo("GPamela")) {
1175      if (!gp_obj)      if (!gp_obj)
# Line 973  CaloTrkVar *PamLevel2::GetCaloStoredTrac Line 1233  CaloTrkVar *PamLevel2::GetCaloStoredTrac
1233   */   */
1234  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
1235    
1236    if (!tof_obj)    if (!tof2_obj)
1237      return 0;      return 0;
1238    
1239    if (tof_obj->ToFLevel2::ntrk() == 0) {    if (tof2_obj->ToFLevel2::ntrk() == 0) {
1240      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"
1241          << endl;          << endl;
1242      return NULL;      return NULL;
# Line 986  ToFTrkVar *PamLevel2::GetToFStoredTrack( Line 1246  ToFTrkVar *PamLevel2::GetToFStoredTrack(
1246    Int_t it_tof = 0;    Int_t it_tof = 0;
1247    
1248    do {    do {
1249      c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);      c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof);
1250      it_tof++;      it_tof++;
1251    } while (c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());    } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk());
1252    
1253    if (!c || seqno != c->trkseqno) {    if (!c || seqno != c->trkseqno) {
1254      c = 0;      c = 0;
# Line 1011  ToFTrkVar *PamLevel2::GetToFStoredTrack( Line 1271  ToFTrkVar *PamLevel2::GetToFStoredTrack(
1271   */   */
1272  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
1273    
1274    if (!orb_obj)    if (!orb2_obj)
1275      return 0;      return 0;
1276    
1277    if (orb_obj->OrbitalInfo::ntrk() == 0) {    if (orb2_obj->OrbitalInfo::ntrk() == 0) {
1278      //       // TRICK BEGIN      //       // TRICK BEGIN
1279      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1280      //       Int_t nn = 0;      //       Int_t nn = 0;
1281      //       TClonesArray &tor = *orb_obj->OrbitalInfoTrk;      //       TClonesArray &tor = *orb2_obj->OrbitalInfoTrk;
1282      //       for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){      //       for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){
1283      //  //      //  //
1284      //  ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt);      //  ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt);
1285      //  if ( ptt->trkseqno != -1  ){      //  if ( ptt->trkseqno != -1  ){
1286      //    //      //    //
1287      //    r->trkseqno = ptt->trkseqno;      //    r->trkseqno = ptt->trkseqno;
# Line 1043  OrbitalInfoTrkVar *PamLevel2::GetOrbital Line 1303  OrbitalInfoTrkVar *PamLevel2::GetOrbital
1303      //       };      //       };
1304      //       delete r;      //       delete r;
1305      //       OrbitalInfoTrkVar *c = 0;      //       OrbitalInfoTrkVar *c = 0;
1306      //       c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);      //       c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1307      //       return c;      //       return c;
1308      //       //TRICK END      //       //TRICK END
1309      cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno      cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
# Line 1055  OrbitalInfoTrkVar *PamLevel2::GetOrbital Line 1315  OrbitalInfoTrkVar *PamLevel2::GetOrbital
1315    Int_t it_tof = 0;    Int_t it_tof = 0;
1316    
1317    do {    do {
1318      c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);      c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1319      it_tof++;      it_tof++;
1320    } while (c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());    } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk());
1321    
1322    if (!c || seqno != c->trkseqno) {    if (!c || seqno != c->trkseqno) {
1323      c = 0;      c = 0;
# Line 1196  void PamLevel2::SortTracks() { Line 1456  void PamLevel2::SortTracks() {
1456    timage->Delete();    timage->Delete();
1457    TClonesArray &ttimage = *timage;    TClonesArray &ttimage = *timage;
1458    
1459    
1460    
1461    //--------------------------------------------------    //--------------------------------------------------
1462    // retrieve sorting method    // retrieve sorting method
1463    //--------------------------------------------------    //--------------------------------------------------
# Line 1311  void PamLevel2::SortTracks() { Line 1573  void PamLevel2::SortTracks() {
1573        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1574        // check the number of hit pmts along the track        // check the number of hit pmts along the track
1575        // on S12 S21 and S32, where paddles are parallel to Y axis        // on S12 S21 and S32, where paddles are parallel to Y axis
1576        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof_obj) {        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) {
1577          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1578          return;          return;
1579        };        };
# Line 1328  void PamLevel2::SortTracks() { Line 1590  void PamLevel2::SortTracks() {
1590          //          //
1591          Float_t sen = 0.;          Float_t sen = 0.;
1592          for (Int_t ih = 0; ih < op->npmtadc; ih++) {          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1593            Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));            Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1594            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1595              sen += (op->dedx).At(ih);              sen += (op->dedx).At(ih);
1596          };          };
1597          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1598            Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));            Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1599            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1600              sen += (oi->dedx).At(ih);              sen += (oi->dedx).At(ih);
1601          };          };
# Line 1346  void PamLevel2::SortTracks() { Line 1608  void PamLevel2::SortTracks() {
1608            Int_t nz = 6;            Int_t nz = 6;
1609            Float_t zin[6]; // << define TOF z-coordinates            Float_t zin[6]; // << define TOF z-coordinates
1610            for (Int_t ip = 0; ip < nz; ip++)            for (Int_t ip = 0; ip < nz; ip++)
1611              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
1612            Trajectory *tr = new Trajectory(nz, zin);            Trajectory *tr = new Trajectory(nz, zin);
1613            //            //
1614            Int_t nphit_p = 0;            Int_t nphit_p = 0;
# Line 1355  void PamLevel2::SortTracks() { Line 1617  void PamLevel2::SortTracks() {
1617            Float_t enhit_i = 0.;            Float_t enhit_i = 0.;
1618            //            //
1619            for (Int_t ih = 0; ih < op->npmtadc; ih++) {            for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1620              Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1621              if (pl == 1 || pl == 2 || pl == 5) {              if (pl == 1 || pl == 2 || pl == 5) {
1622                nphit_p++;                nphit_p++;
1623                enhit_p += (op->dedx).At(ih);                enhit_p += (op->dedx).At(ih);
# Line 1366  void PamLevel2::SortTracks() { Line 1628  void PamLevel2::SortTracks() {
1628            //            //
1629            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1630              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1631                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1632                if (pl == 0) {                if (pl == 0) {
1633                  nphit_p++;                  nphit_p++;
1634                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1375  void PamLevel2::SortTracks() { Line 1637  void PamLevel2::SortTracks() {
1637            };            };
1638            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1639              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1640                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1641                if (pl == 3) {                if (pl == 3) {
1642                  nphit_p++;                  nphit_p++;
1643                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1384  void PamLevel2::SortTracks() { Line 1646  void PamLevel2::SortTracks() {
1646            };            };
1647            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1648              for (Int_t ih = 0; ih < op->npmtadc; ih++) {              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1649                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1650                if (pl == 4) {                if (pl == 4) {
1651                  nphit_p++;                  nphit_p++;
1652                  enhit_p += (op->dedx).At(ih);                  enhit_p += (op->dedx).At(ih);
# Line 1393  void PamLevel2::SortTracks() { Line 1655  void PamLevel2::SortTracks() {
1655            };            };
1656    
1657            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1658              Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1659              if (pl == 1 || pl == 2 || pl == 5) {              if (pl == 1 || pl == 2 || pl == 5) {
1660                nphit_i++;                nphit_i++;
1661                enhit_i += (op->dedx).At(ih);                enhit_i += (op->dedx).At(ih);
# Line 1404  void PamLevel2::SortTracks() { Line 1666  void PamLevel2::SortTracks() {
1666            //            //
1667            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1668              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1669                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1670                if (pl == 0) {                if (pl == 0) {
1671                  nphit_i++;                  nphit_i++;
1672                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1413  void PamLevel2::SortTracks() { Line 1675  void PamLevel2::SortTracks() {
1675            };            };
1676            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1677              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1678                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1679                if (pl == 3) {                if (pl == 3) {
1680                  nphit_i++;                  nphit_i++;
1681                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1422  void PamLevel2::SortTracks() { Line 1684  void PamLevel2::SortTracks() {
1684            };            };
1685            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1686              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1687                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1688                if (pl == 4) {                if (pl == 4) {
1689                  nphit_i++;                  nphit_i++;
1690                  enhit_i += (op->dedx).At(ih);                  enhit_i += (op->dedx).At(ih);
# Line 1466  void PamLevel2::SortTracks() { Line 1728  void PamLevel2::SortTracks() {
1728             cout << "image: npmttdc "<< oi->npmttdc << endl;*/             cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1729    
1730            //      for (Int_t ih=0; ih < op->npmtadc; ih++){            //      for (Int_t ih=0; ih < op->npmtadc; ih++){
1731            //        Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            //        Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1732            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1733            //      };            //      };
1734    
1735            //      for (Int_t ih=0; ih < oi->npmtadc; ih++){            //      for (Int_t ih=0; ih < oi->npmtadc; ih++){
1736            //        Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            //        Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1737            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1738            //      };            //      };
1739            // --- modified to count tdc signals (more efficient?)            // --- modified to count tdc signals (more efficient?)
1740            // --- and to implement check on tdcflag            // --- and to implement check on tdcflag
1741            for (Int_t ih = 0; ih < op->npmttdc; ih++) {            for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1742              Int_t pl = tof_obj->GetPlaneIndex((op->pmttdc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih));
1743              //      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++;
1744              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))
1745                  || (use_S3 && (pl == 4 || pl == 5))) {                  || (use_S3 && (pl == 4 || pl == 5))) {
# Line 1487  void PamLevel2::SortTracks() { Line 1749  void PamLevel2::SortTracks() {
1749            };            };
1750    
1751            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1752              Int_t pl = tof_obj->GetPlaneIndex((oi->pmttdc).At(ih));              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1753              //      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++;
1754              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))
1755                  || (use_S3 && (pl == 4 || pl == 5))) {                  || (use_S3 && (pl == 4 || pl == 5))) {
# Line 1662  void PamLevel2::SortTracks() { Line 1924  void PamLevel2::SortTracks() {
1924    
1925  }  }
1926  ;  ;
1927    //
1928    //--------------------------------------
1929    /**
1930     * Sort physical (tracker) tracks.
1931     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1932     * Sorting cryteria:
1933     * TRK: lower chi**2
1934     * CAL: lower Y spatial residual on the first calorimeter plane
1935     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
1936     * S1: (ask Emiliano)
1937     * S2: (ask Emiliano)
1938     * S3: (ask Emiliano)
1939     * GP: more GP hits
1940     * The default sorting cryterium is "TOF+CAL".
1941     *
1942     * 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).
1943     *
1944     * New version, with handling of extended tracks and nuclei tracks.
1945     */
1946    void PamLevel2::SortTracksNew() {
1947    
1948      //--------------------------------------------------
1949      //Check if the current event has already been sorted
1950      //--------------------------------------------------
1951      if (issorted_new && lastsorted_new == GetReadEntry()) {
1952          return; //already done for this event
1953      }
1954    
1955    
1956    //  cout << "SORT" << endl;
1957    
1958      //Reset the sort flags, just in case something will go wrong...
1959      issorted_new = false;
1960      lastsorted_new = -1;
1961    
1962      //--------------------------------------------------
1963      // set input variables
1964      //--------------------------------------------------
1965    
1966      TString how = howtosort;
1967      //Save current Object count
1968      Int_t ObjectNumber = TProcessID::GetObjectCount();
1969    
1970    
1971    
1972    
1973      TrkLevel2  * trk2 ;
1974      CaloLevel2 * calo2;
1975      ToFLevel2  * tof2  ;
1976      OrbitalInfo  * orb2 ;
1977      
1978      TClonesArray * trkext ;
1979      TClonesArray * caloext;
1980      TClonesArray * tofext ;
1981      TClonesArray * orbext ;
1982    
1983    
1984    //   cout << "trk2_obj" << trk2_obj<< endl;
1985    //   cout << "trk2_nuc_obj" << trk2_nuc_obj<< endl;
1986    //   cout << " trk_ext_obj "<<trk_ext_obj<< endl;
1987    //   cout << " trk_ext_nuc_obj "<<trk_ext_nuc_obj<< endl;
1988    
1989      //-----------------------------------------------------------
1990      // create/reset TCloneArrays to store tracks and their images
1991      //-----------------------------------------------------------
1992      
1993      // main tracks from standard alg
1994      if (!tsorted)
1995          tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1996      tsorted->Delete();
1997      // track images from standard alg
1998      if (!timage)
1999          timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2000      timage->Delete();
2001      // tracks from extended algorythm
2002      if(EXT && !text)
2003          text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2004      if(text)text->Delete();
2005    
2006      //-----------------------------------------------------------
2007      // create/reset TCloneArrays to store tracks and their images
2008      //-----------------------------------------------------------
2009      if(NUC){
2010      // main tracks from standard alg
2011          if (!tsorted_nuc)
2012              tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2013          tsorted_nuc->Delete();
2014          // track images from standard alg
2015          if (!timage_nuc)
2016              timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2017          timage_nuc->Delete();
2018          // tracks from extended algorythm
2019          if(EXT && !text_nuc)
2020              text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2021          if(text_nuc)text_nuc->Delete();
2022      }
2023      //--------------------------------------------------
2024      // retrieve sorting method
2025      //--------------------------------------------------
2026      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
2027      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
2028      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
2029      Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
2030      Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
2031      Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
2032      Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
2033    
2034      if (use_TOF) {
2035        use_S1 = true;
2036        use_S2 = true;
2037        use_S3 = true;
2038      };
2039      if (!CAL2 && use_CAL)
2040        use_CAL = false;
2041      if (!TOF) {
2042        use_TOF = false;
2043        use_S1 = false;
2044        use_S2 = false;
2045        use_S3 = false;
2046      }
2047      if (!GP) {
2048        use_GP = false;
2049      }
2050    
2051      if (!TRK2) {
2052        cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
2053        return;
2054      };
2055    
2056    
2057      ///////////////////////////////////////////////////////////////////////////////////
2058      //
2059      //   sort tracks and fill PamTrack arrays
2060      //
2061      ///////////////////////////////////////////////////////////////////////////////////
2062      for(int doit=0; doit<2; doit++){
2063    
2064    //       cout << "doit "<<doit<<endl;
2065    
2066    
2067          if(doit == 0){
2068    
2069              trk2  = trk2_obj;
2070              calo2 = calo2_obj;
2071              tof2  = tof2_obj;
2072              orb2  = orb2_obj;
2073          
2074              trkext = trk_ext_obj;
2075              caloext = calo_ext_obj;
2076              tofext = tof_ext_obj;
2077              orbext = orb_ext_obj;
2078        
2079          
2080          
2081          
2082    
2083          }else if (doit == 1){
2084    
2085              if(!NUC)break;
2086    
2087          
2088              trk2  = trk2_nuc_obj;
2089              calo2 = calo2_nuc_obj;
2090              tof2  = tof2_nuc_obj;
2091              orb2  = orb2_nuc_obj;
2092          
2093              trkext = trk_ext_nuc_obj;
2094              caloext = calo_ext_nuc_obj;
2095              tofext = tof_ext_nuc_obj;
2096              orbext = orb_ext_nuc_obj;
2097          
2098          
2099          
2100    
2101          }
2102    
2103    //       cout << "trk2" << trk2<<endl;
2104    //       cout << "calo2" << calo2<<endl;
2105    //       cout << "tof2" << tof2<<endl;
2106    //       cout << "orb2" << orb2<<endl;
2107    //       cout << "trkext" <<trkext <<endl;
2108    //       cout << "tofext" << tofext<<endl;
2109    //       cout << "caloext" << caloext<<endl;
2110    //       cout << "orbext" << orbext<<endl;
2111    
2112          TClonesArray &ttext    = (doit==0 ? *text : *text_nuc);
2113          TClonesArray &ttimage  = (doit==0 ? *timage : *timage_nuc);
2114          TClonesArray &ttsorted = (doit==0 ? *tsorted : *tsorted_nuc);
2115      
2116    //       cout << "tsorted + timage "<<doit<<endl;
2117    
2118          //--------------------------------------------------
2119          // loop over "physical" tracks sorted by the tracker
2120          //--------------------------------------------------
2121          for (Int_t i = 0; i < trk2->TrkLevel2::GetNTracks(); i++) {
2122    
2123              TrkTrack *ts = 0;
2124              CaloTrkVar *cs = 0;
2125              ToFTrkVar *os = 0;
2126              OrbitalInfoTrkVar *rs = 0;
2127    
2128              // get tracker tracks
2129              TrkTrack *tp = trk2->GetTrack(i); //tracker
2130              CaloTrkVar *cp = calo2->GetCaloStoredTrack(tp->GetSeqNo());
2131              ToFTrkVar *op = tof2->GetToFStoredTrack(tp->GetSeqNo());
2132              OrbitalInfoTrkVar *rp = orb2->GetOrbitalInfoStoredTrack(tp->GetSeqNo());
2133    
2134              TrkTrack *ti = 0; //tracker (image)
2135              CaloTrkVar *ci = 0;
2136              ToFTrkVar *oi = 0;
2137              OrbitalInfoTrkVar *ri = 0;
2138              //    cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
2139              // if track has an image, check image selection
2140    
2141              Int_t tp_score = 0; //main track sorted by the tracker
2142              Int_t ti_score = 0; //image track
2143              Int_t totp_score = 0; //main track sorted by the tracker
2144              Int_t toti_score = 0; //image track
2145    
2146              if (tp->HasImage()) {
2147    
2148                  ti = trk2->GetTrackImage(i); //tracker (image)
2149                  ci = calo2->GetCaloStoredTrack(ti->GetSeqNo());
2150                  oi = tof2->GetToFStoredTrack(ti->GetSeqNo());
2151                  ri = orb2->GetOrbitalInfoStoredTrack(ti->GetSeqNo());
2152    
2153                  //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
2154    
2155                  //assign starting scores
2156                  tp_score = 0; //main track sorted by the tracker
2157                  ti_score = 0; //image track
2158    
2159                  // -----------------------------------------------------------------------------------------
2160                  // *****************************************************************************************
2161                  // -----------------------------------------------------------------------------------------
2162                  // calorimeter check
2163                  // -----------------------------------------------------------------------------------------
2164                  if (use_CAL && !calo2) {
2165                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
2166                      return;
2167                  };
2168                  if (use_CAL && !cp && ci) {
2169                      ti_score++;
2170                      toti_score++;
2171                  };
2172                  if (use_CAL && cp && !ci) {
2173                      tp_score++;
2174                      totp_score++;
2175                  };
2176                  if (use_CAL && cp && ci && true) {
2177    
2178                      if (cp->npresh > ci->npresh && true) {
2179                          tp_score++;
2180                          totp_score++;
2181                      };
2182                      if (cp->npresh < ci->npresh && true) {
2183                          ti_score++;
2184                          toti_score++;
2185                      };
2186    
2187                      //    cout << "CALO "<<tp_score<<ti_score<<endl;
2188    
2189                  };
2190                  // -----------------------------------------------------------------------------------------
2191                  // *****************************************************************************************
2192                  // -----------------------------------------------------------------------------------------
2193                  // TOF check
2194                  // -----------------------------------------------------------------------------------------
2195                  // check the number of hit pmts along the track
2196                  // on S12 S21 and S32, where paddles are parallel to Y axis
2197                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2) {
2198                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
2199                      return;
2200                  };
2201                  //
2202                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
2203                      ti_score++;
2204                      toti_score++;
2205                  };
2206                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
2207                      tp_score++;
2208                      totp_score++;
2209                  };
2210                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
2211                      //
2212                      Float_t sen = 0.;
2213                      for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2214                          Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2215                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2216                              sen += (op->dedx).At(ih);
2217                      };
2218                      for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2219                          Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2220                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2221                              sen += (oi->dedx).At(ih);
2222                      };
2223                      //
2224                      if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
2225                          //printf(" IS A NUCLEUS! en = %f \n",sen);
2226                          //
2227                          // is a nucleus use a different algorithm
2228                          //
2229                          Int_t nz = 6;
2230                          Float_t zin[6]; // << define TOF z-coordinates
2231                          for (Int_t ip = 0; ip < nz; ip++)
2232                              zin[ip] = tof2->GetZTOF(tof2->GetToFPlaneID(ip)); // << read ToF plane z-coordinates
2233                          Trajectory *tr = new Trajectory(nz, zin);
2234                          //
2235                          Int_t nphit_p = 0;
2236                          Int_t nphit_i = 0;
2237                          Float_t enhit_p = 0.;
2238                          Float_t enhit_i = 0.;
2239                          //
2240                          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2241                              Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2242                              if (pl == 1 || pl == 2 || pl == 5) {
2243                                  nphit_p++;
2244                                  enhit_p += (op->dedx).At(ih);
2245                              };
2246                          };
2247                          //
2248                          tp->DoTrack2(tr);
2249                          //
2250                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2251                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2252                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2253                                  if (pl == 0) {
2254                                      nphit_p++;
2255                                      enhit_p += (op->dedx).At(ih);
2256                                  };
2257                              };
2258                          };
2259                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2260                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2261                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2262                                  if (pl == 3) {
2263                                      nphit_p++;
2264                                      enhit_p += (op->dedx).At(ih);
2265                                  };
2266                              };
2267                          };
2268                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2269                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2270                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2271                                  if (pl == 4) {
2272                                      nphit_p++;
2273                                      enhit_p += (op->dedx).At(ih);
2274                                  };
2275                              };
2276                          };
2277    
2278                          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2279                              Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2280                              if (pl == 1 || pl == 2 || pl == 5) {
2281                                  nphit_i++;
2282                                  enhit_i += (op->dedx).At(ih);
2283                              };
2284                          };
2285                          //
2286                          ti->DoTrack2(tr);
2287                          //
2288                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2289                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2290                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2291                                  if (pl == 0) {
2292                                      nphit_i++;
2293                                      enhit_i += (op->dedx).At(ih);
2294                                  };
2295                              };
2296                          };
2297                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2298                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2299                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2300                                  if (pl == 3) {
2301                                      nphit_i++;
2302                                      enhit_i += (op->dedx).At(ih);
2303                                  };
2304                              };
2305                          };
2306                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2307                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2308                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2309                                  if (pl == 4) {
2310                                      nphit_i++;
2311                                      enhit_i += (op->dedx).At(ih);
2312                                  };
2313                              };
2314                          };
2315    
2316                          if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
2317    
2318                              //        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);
2319                              //        printf(" score p %i score i %i \n",tp_score,ti_score);
2320                              //              if( enhit_p > enhit_i ) tp_score++;
2321                              //              if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
2322                              if (nphit_p > nphit_i)
2323                                  tp_score++;
2324                              if (nphit_p < nphit_i)
2325                                  ti_score++;
2326                              if (nphit_p == nphit_i) {
2327                                  if (enhit_p > enhit_i)
2328                                      tp_score++;
2329                                  else
2330                                      ti_score++;
2331                              };
2332                              //        printf(" dopo score p %i score i %i \n",tp_score,ti_score);
2333                          };
2334                          delete tr;
2335                          //
2336                      }
2337                      else {
2338                          // -------------
2339                          // NOT a NUCLEUS
2340                          // -------------
2341                          //printf(" NOT a NUCLEUS! en = %f \n",sen);
2342    
2343                          Int_t nphit_p = 0;
2344                          Int_t nphit_i = 0;
2345    
2346                          /*                                cout << "track: npmtadc "<< op->npmtadc << endl;
2347                            cout << "track: npmttdc "<< op->npmttdc << endl;
2348                            cout << "image: npmtadc "<< oi->npmtadc << endl;
2349                            cout << "image: npmttdc "<< oi->npmttdc << endl;*/
2350    
2351                          //          for (Int_t ih=0; ih < op->npmtadc; ih++){
2352                          //            Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
2353                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
2354                          //          };
2355    
2356                          //          for (Int_t ih=0; ih < oi->npmtadc; ih++){
2357                          //            Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
2358                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
2359                          //          };
2360                          // --- modified to count tdc signals (more efficient?)
2361                          // --- and to implement check on tdcflag
2362                          for (Int_t ih = 0; ih < op->npmttdc; ih++) {
2363                              Int_t pl = tof2->GetPlaneIndex((op->pmttdc).At(ih));
2364                              //        if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
2365                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2366                                  || (use_S3 && (pl == 4 || pl == 5))) {
2367                                  if ((op->tdcflag).At(ih) == 0)
2368                                      nphit_p++;
2369                              };
2370                          };
2371    
2372                          for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
2373                              Int_t pl = tof2->GetPlaneIndex((oi->pmttdc).At(ih));
2374                              //        if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
2375                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2376                                  || (use_S3 && (pl == 4 || pl == 5))) {
2377                                  if ((oi->tdcflag).At(ih) == 0)
2378                                      nphit_i++;
2379                              };
2380                          };
2381    
2382                          if ((nphit_p + nphit_i) != 0 && true) {
2383    
2384                              if (nphit_p != nphit_i) {
2385                                  totp_score += nphit_p;
2386                                  toti_score += nphit_i;
2387                                  tp_score += nphit_p;
2388                                  ti_score += nphit_i;
2389                              };
2390                              //        if     ( nphit_p > nphit_i) tp_score+=nphit_p;
2391                              //        else if( nphit_p < nphit_i) ti_score+=nphit_i;
2392                              //        else ;//niente
2393                          };
2394                      };
2395                      //    cout << "TOF "<<tp_score<<ti_score<<endl;
2396                  };
2397    
2398                  //      if(tp_score == ti_score) use_TRK = true;
2399    
2400    
2401                  // -----------------------------------------------------------------------------------------
2402                  // *****************************************************************************************
2403                  // -----------------------------------------------------------------------------------------
2404                  // tracker check
2405                  // -----------------------------------------------------------------------------------------
2406                  // chi**2 difference is not always large enough to distinguish among
2407                  // the real track and its image.
2408                  // Tracker check will be applied always when calorimeter and tof information is ambiguous.
2409                  // *** MODIFIED ON AUGUST 2007 ***
2410                  if (use_TRK) {
2411                      //    if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
2412                      //    else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
2413    
2414                      // CHECK 1 : number of points along X
2415                      if (tp->GetNX() >= ti->GetNX()) {
2416                          tp_score++;
2417                          totp_score++;
2418                      };
2419                      if (tp->GetNX() <= ti->GetNX()) {
2420                          ti_score++;
2421                          toti_score++;
2422                      };
2423                      // CHECK 2 : number of points along Y
2424                      if (tp->GetNY() >= ti->GetNY()) {
2425                          tp_score++;
2426                          totp_score++;
2427                      };
2428                      if (tp->GetNY() <= ti->GetNY()) {
2429                          ti_score++;
2430                          toti_score++;
2431                      };
2432    
2433                      //    cout << "TRK "<<tp_score<<ti_score<<endl;
2434                  };
2435    
2436                  // -----------------------------------------------------------------------------------------
2437                  // *****************************************************************************************
2438                  // -----------------------------------------------------------------------------------------
2439                  // GPamela check
2440                  // -----------------------------------------------------------------------------------------
2441    
2442                  //---------------------------------------------------
2443                  // count the number of GP hits
2444                  //---------------------------------------------------
2445                  if (use_GP) {
2446                      int ngphits_p = 0;
2447                      int ngphits_i = 0;
2448                      float toll = 0.02; //200 micron
2449                      for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
2450                          int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
2451                          if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2452                              ngphits_p++;
2453                          if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2454                              ngphits_i++;
2455                      }
2456                      if (ngphits_p > ngphits_i && true) {
2457                          tp_score++;
2458                          totp_score++;
2459                      }
2460                      if (ngphits_p < ngphits_i && true) {
2461                          ti_score++;
2462                          toti_score++;
2463                      }
2464                  }
2465    
2466                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2467                  // the winner is....
2468                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2469                  if (tp_score > ti_score) {
2470    
2471                  }
2472                  else if (tp_score < ti_score) {
2473    
2474                      ts = ti;//its image!!
2475                      cs = ci;
2476                      os = oi;
2477                      rs = ri;
2478                      Int_t totis = toti_score;
2479    
2480                      ti = tp;//its image!!
2481                      ci = cp;
2482                      oi = op;
2483                      ri = rp;
2484    
2485                      tp = ts;//its image!!
2486                      cp = cs;
2487                      op = os;
2488                      rp = rs;
2489    
2490                      toti_score = totp_score;
2491                      totp_score = totis;
2492    
2493                  }
2494                  else {
2495    
2496                      //    cout << "Warning - track image ambiguity not solved" << endl;
2497    
2498                  };
2499    
2500              }
2501              else {
2502                  totp_score = 1;
2503                  toti_score = 0;
2504    
2505                  //            ts = tp;
2506                  //            cs = cp;
2507                  //            os = op;
2508              };
2509    
2510              //    cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
2511              //    sorted_tracks->Add(ts);//save the track in the sorted array
2512              //    sorted_tracks.Add(ts);//save the track in the sorted array
2513              //    sorted_tracks.Add(tp);//save the track in the sorted array
2514              //    cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
2515              //    cout<<"o "<<tp<<endl;
2516              //    cout<<"o "<<cp<<endl;
2517              //    cout<<"o "<<op<<endl;
2518    
2519              new (ttsorted[i]) PamTrack(tp, cp, op, rp);
2520              new (ttimage[i]) PamTrack(ti, ci, oi, ri);
2521    
2522              ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
2523              ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
2524              ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
2525              ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
2526          };
2527    
2528    
2529    
2530    
2531    
2532    //   if (tsorted->GetEntries() != trk2->GetNTracks()) {
2533    //     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2534    //         << " != trk2->GetNTracks() = " << trk2->GetNTracks() << endl;
2535    //     tsorted->Delete();
2536    //     tsorted = 0;
2537    //     timage->Delete();
2538    //     timage = 0;
2539    //   }
2540    
2541    
2542    //       cout << "text "<<doit<<endl;
2543    
2544    
2545          //--------------------------------------------------
2546          // fill array with extended tracks (this is easy...)
2547          //--------------------------------------------------
2548          if(EXT){
2549              for(int it=0; it<trkext->GetEntries(); it++){
2550                  
2551                  new (ttext[it]) PamTrack((ExtTrack*)(*trkext)[it], (CaloTrkVar*)(*caloext)[it], (ToFTrkVar*)(*tofext)[it], (OrbitalInfoTrkVar*)(*orbext)[it]);
2552                  
2553              }
2554          }
2555          
2556    
2557    
2558      };
2559    
2560    
2561      //Restore Object count
2562      //To save space in the table keeping track of all referenced objects
2563      //We reset the object count to what it was at the beginning of the event.
2564      TProcessID::SetObjectCount(ObjectNumber);
2565    
2566      //Everything went fine so the current event can be tagged as sorted
2567      issorted_new = true;
2568      lastsorted_new = GetReadEntry();
2569    
2570    //   cout << " tsorted "<< tsorted << " "<<(tsorted ? tsorted->GetEntries() : 0)<<endl;
2571    //   cout << " timage  "<< timage  << " "<<(timage  ? timage->GetEntries() : 0)<<endl;
2572    //   cout << " text    "<< text    << " "<<(text    ? text->GetEntries() : 0)<<endl;
2573    
2574    
2575    };
2576  //--------------------------------------  //--------------------------------------
2577  //  //
2578  //  //
# Line 1669  void PamLevel2::SortTracks() { Line 2580  void PamLevel2::SortTracks() {
2580  /**  /**
2581   * 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.
2582   * 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.
2583   */   /*
2584    
2585  // TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
2586    
2587  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
# Line 1680  void PamLevel2::SortTracks() { Line 2592  void PamLevel2::SortTracks() {
2592  //     return &sorted_tracks;  //     return &sorted_tracks;
2593    
2594  //  };  //  };
 TClonesArray *PamLevel2::GetTracks() {  
   
   //  *-*-*-*-*-*-*-*-*-*-*-*-*  
   SortTracks();  
   //  *-*-*-*-*-*-*-*-*-*-*-*-*  
2595    
   return tsorted;  
2596    
 }  
 ;  
2597  //--------------------------------------  //--------------------------------------
2598  //  //
2599  //  //
# Line 1701  TClonesArray *PamLevel2::GetTracks() { Line 2605  TClonesArray *PamLevel2::GetTracks() {
2605   */   */
2606  PamTrack *PamLevel2::GetTrack(int it) {  PamTrack *PamLevel2::GetTrack(int it) {
2607    
2608    //    if(!trk2_obj) return 0;    PamTrack *track = NULL;
   
   // //  *-*-*-*+-*-*-*-*-*-*-*-*-*  
   //     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;  
2609    
   //    cout << "PamLevel2::GetTrack(int it) "<<endl;  
2610    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2611    SortTracks();    SortTracks();
2612    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2613    if (!tsorted)    if (!tsorted)
2614      return 0;      return track;
2615    if (!tsorted->GetEntries())    if (!tsorted->GetEntries())
2616      return 0;      return track;
2617    
   PamTrack *track = 0;  
2618    
2619    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];  
2620      track = (PamTrack*)((*tsorted)[it]);      track = (PamTrack*)((*tsorted)[it]);
2621    }    }
2622    else {    else {
# Line 1745  PamTrack *PamLevel2::GetTrack(int it) { Line 2626  PamTrack *PamLevel2::GetTrack(int it) {
2626    
2627    return track;    return track;
2628    
2629    };
2630    
2631    //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2632    
2633    /**
2634     * Retrieves the it-th Pamela "physical" track.
2635     * It override TrkLevel2::GetTrack(int it).
2636     * @param it Track number, ranging from 0 to GetNTracks().
2637     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2638     */
2639    PamTrack *PamLevel2::GetTrack(int it, char* alg) {
2640    
2641        TString s(alg);
2642        if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrack(it); //old algorythm
2643    
2644    
2645        SortTracksNew();
2646        // >> fill tsorted, timage and text
2647    
2648        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2649    
2650            if( s.Contains("NUC")){
2651                if(
2652                    tsorted_nuc &&
2653                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2654                    it >= 0 &&                                                //valid index
2655                    true) return (PamTrack*)((*tsorted_nuc)[it]);                 //ok return the track
2656            }else{
2657                if(
2658                    tsorted &&
2659                    it < tsorted->GetEntries() &&                             //enough tracks found
2660                    it >= 0 &&                                                //valid index
2661                    true )return (PamTrack*)((*tsorted)[it]);                 //ok return the track
2662            }
2663    
2664        }
2665        
2666    
2667        /////////////////////////////////////////////////////////////////////////
2668        /// if requested get track from extended algorythm output
2669        /////////////////////////////////////////////////////////////////////////
2670        
2671        if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2672            
2673            if(s.Contains("NUC")){
2674                if(
2675                    text_nuc &&
2676                    it < text_nuc->GetEntries() &&                             //enough tracks found
2677                    it >= 0 &&                                                //valid index
2678                    true) return (PamTrack*)((*text_nuc)[it]);
2679            }else{
2680                if(
2681                    text &&
2682                    it < text->GetEntries() &&                             //enough tracks found
2683                    it >= 0 &&                                                //valid index
2684                    true) return (PamTrack*)((*text)[it]);
2685            }
2686    
2687        };
2688        
2689        
2690        
2691        return NULL;
2692    
2693    }
2694    ;
2695    TClonesArray *PamLevel2::GetTracks() {
2696    
2697      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2698      SortTracks();
2699      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2700    
2701      return tsorted;
2702    
2703  }  }
2704  ;  ;
2705    Int_t  PamLevel2::GetNTracks(char* alg) {
2706    
2707    
2708    //    cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2709    
2710        TString s(alg);
2711    
2712        if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2713            
2714        if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2715            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2716            return trk_ext_obj->GetEntries();//ok
2717        }
2718        if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2719            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2720                return (trk2_nuc_obj->TrkLevel2::GetNTracks() ?  trk2_nuc_obj->TrkLevel2::GetNTracks() :  trk_ext_nuc_obj->GetEntries() );  
2721            return (trk2_obj->TrkLevel2::GetNTracks() ?  trk2_obj->TrkLevel2::GetNTracks() :  trk_ext_obj->GetEntries() );
2722        }
2723        if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2724            return trk2_nuc_obj->TrkLevel2::GetNTracks();
2725        
2726        return 0;
2727    
2728    }
2729    
2730    
2731  //--------------------------------------  //--------------------------------------
2732  //  //
# Line 1786  PamTrack *PamLevel2::GetTrackImage(int i Line 2766  PamTrack *PamLevel2::GetTrackImage(int i
2766    
2767    return image;    return image;
2768  }  }
2769    /**
2770     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2771     * @param it Track number, ranging from 0 to GetNTracks().
2772     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2773     */
2774    PamTrack *PamLevel2::GetTrackImage(int it, char* alg) {
2775    
2776        TString s(alg);
2777        if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrack(it); //old algorythm
2778    
2779    
2780        SortTracksNew();
2781        // >> fill tsorted, timage and text
2782    
2783        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2784    
2785            if( s.Contains("NUC")){
2786                if(
2787                    tsorted_nuc &&
2788                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2789                    it >= 0 &&                                                //valid index
2790                    true){
2791                    TClonesArray &t = *(tsorted_nuc);
2792                    PamTrack *temp = (PamTrack*) t[it];
2793                    if (temp->GetTrkTrack()->HasImage()) {
2794                        return (PamTrack*)((*timage_nuc)[it]);                 //ok return the track
2795                    }
2796                    
2797                }
2798            }else{
2799                if(
2800                    tsorted &&
2801                    it < tsorted->GetEntries() &&                             //enough tracks found
2802                    it >= 0 &&                                                //valid index
2803                    true ){
2804                    TClonesArray &t = *(tsorted);
2805                    PamTrack *temp = (PamTrack*) t[it];
2806                    if (temp->GetTrkTrack()->HasImage()) {              
2807                        return (PamTrack*)((*timage)[it]);                 //ok return the track
2808                    }
2809                }
2810            }
2811    
2812        }
2813          
2814        return NULL;
2815    
2816    }
2817    ;
2818    
2819  //--------------------------------------  //--------------------------------------
2820  //  //
# Line 1875  TTree *PamLevel2::GetPamTree(TFile *f, T Line 2904  TTree *PamLevel2::GetPamTree(TFile *f, T
2904      if (!Trout)      if (!Trout)
2905        Trout = T;        Trout = T;
2906      else      else
2907        Trout->AddFriend(T);          Trout->AddFriend(T);
2908    }    }
2909    else {    else {
2910      cout << "Tracker      : missing tree" << endl;      cout << "Tracker      : missing tree" << endl;
# Line 2112  TList* PamLevel2::GetListOfLevel2Files(T Line 3141  TList* PamLevel2::GetListOfLevel2Files(T
3141          if (file.EndsWith(".root")) {          if (file.EndsWith(".root")) {
3142            TString filedir;            TString filedir;
3143            cout << ddir << endl;            cout << ddir << endl;
3144            if (ddir != "") {            if ( ddir != "" ) {
3145              filedir = ddir; // take the input dir              filedir = ddir; // take the input dir
3146            }            }
3147            else {            else {
# Line 2120  TList* PamLevel2::GetListOfLevel2Files(T Line 3149  TList* PamLevel2::GetListOfLevel2Files(T
3149              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
3150            };            };
3151            filedir.Append("/");            filedir.Append("/");
3152            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3153              char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3154            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
3155            cout << fullpath << endl;            cout << fullpath << endl;
3156            delete fullpath;            delete fullpath;
# Line 2287  TChain *PamLevel2::GetPamTree(TList *fl, Line 3317  TChain *PamLevel2::GetPamTree(TList *fl,
3317          G->Add(name);          G->Add(name);
3318        if (SELLI == 1)        if (SELLI == 1)
3319          L->Add(name);          L->Add(name);
3320      };       };
3321    };    };
3322    
3323    cout << "done chains\n";    cout << "done data-tree chains  "<<  T->GetNtrees() <<" \n";
3324    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3325    
3326    // -------------------------------------------    // -------------------------------------------
3327    // make friends    // make friends
# Line 2374  TChain *PamLevel2::GetPamTree(TList *fl, Line 3404  TChain *PamLevel2::GetPamTree(TList *fl,
3404    //  =====================================    //  =====================================
3405    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3406    //  =====================================    //  =====================================
3407    SetBranchAddress(Trout);    if( Trout->GetNtrees() )SetBranchAddress(Trout);
3408    
3409    //  ------------------------------------    //  ------------------------------------
3410    //  finally handle selection trees...    //  finally handle selection trees...
3411    //  (it is not friend of pamela tree)    //  (it is not friend of pamela tree)
3412    //  ------------------------------------    //  ------------------------------------
3413    
3414    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3415    
3416    // Selection List    // Selection List
3417    if (L && SELLI == 1) {    if (L && SELLI == 1) {
# Line 2391  TChain *PamLevel2::GetPamTree(TList *fl, Line 3421  TChain *PamLevel2::GetPamTree(TList *fl,
3421      cout << "SelectionList: set branch address RunEntry" << endl;      cout << "SelectionList: set branch address RunEntry" << endl;
3422      L->SetBranchAddress("EventEntry", &irunentry);      L->SetBranchAddress("EventEntry", &irunentry);
3423      cout << "SelectionList: set branch address EventEntry" << endl;      cout << "SelectionList: set branch address EventEntry" << endl;
3424        /*    if ( L->GetBranch("L0EventEntry") ){
3425          hasL0EE = true;
3426          L->SetBranchAddress("L0EventEntry", &il0entry);
3427          cout << "SelectionList: set branch address L0EventEntry" << endl;
3428        } else {
3429          hasL0EE = false; // backward compatibility with old preselected files...
3430          }*/
3431      sel_tree = L;      sel_tree = L;
3432      //          if(!Trout)Trout=O;      //          if(!Trout)Trout=O;
3433      //          else Trout->AddFriend("SelectionList");      //          else Trout->AddFriend("SelectionList");
3434      cout << "+SelectionList" << endl;      cout << "+SelectionList" << endl;
3435      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;      cout << "----------------------------------------------------" << endl;
3436    }    }
3437    else {    else {
3438      //  cout << "SelectionList  : missing tree"<<endl;      //  cout << "SelectionList  : missing tree"<<endl;
# Line 2421  TChain *PamLevel2::GetPamTree(TList *fl, Line 3458  TChain *PamLevel2::GetPamTree(TList *fl,
3458   */   */
3459  void PamLevel2::SetBranchAddress(TTree *t) {  void PamLevel2::SetBranchAddress(TTree *t) {
3460    
3461    TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");  //   TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3462    TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");  //   TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3463    TRKh = TRKh & t->GetBranchStatus("TrkHough");  //   TRKh = TRKh & t->GetBranchStatus("TrkHough");
3464    CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");  //   CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3465    CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");  //   CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3466    TOF = TOF & t->GetBranchStatus("ToFLevel2");  //   TOF = TOF & t->GetBranchStatus("ToFLevel2");
3467    TRG = TRG & t->GetBranchStatus("TrigLevel2");  //   TRG = TRG & t->GetBranchStatus("TrigLevel2");
3468    S4 = S4 & t->GetBranchStatus("S4Level2");  //   S4 = S4 & t->GetBranchStatus("S4Level2");
3469    ND = ND & t->GetBranchStatus("NDLevel2");  //   ND = ND & t->GetBranchStatus("NDLevel2");
3470    AC = AC & t->GetBranchStatus("AcLevel2");  //   AC = AC & t->GetBranchStatus("AcLevel2");
3471    ORB = ORB & t->GetBranchStatus("OrbitalInfo");  //   ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3472    GP = GP & t->GetBranchStatus("h20");  //   GP = GP & t->GetBranchStatus("h20");
3473    
3474    
3475    // Tracker    // Tracker
3476    if (TRK1) {    if (TRK1) {
# Line 2442  void PamLevel2::SetBranchAddress(TTree * Line 3480  void PamLevel2::SetBranchAddress(TTree *
3480    if (TRK2) {    if (TRK2) {
3481      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3482      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3483        NUC = t->GetBranchStatus("TrackNuclei");
3484        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );    
3485    };    };
3486    if (TRKh) {    if (TRKh) {
3487      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
# Line 2502  void PamLevel2::SetBranchAddress(TTree * Line 3542  void PamLevel2::SetBranchAddress(TTree *
3542    
3543      cout << "h20          : set branch address GPamela " << endl;      cout << "h20          : set branch address GPamela " << endl;
3544    };    };
3545      if(NUC){
3546          
3547          cout << "Found nuclei-track branches" << endl;
3548    
3549          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3550          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3551          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3552          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3553          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3554          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3555          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3556          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3557    
3558          ///copy the vector content inside a "fake" object (all aother info are missing)    
3559    
3560          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3561          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3562          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3563          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3564    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3565    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3566    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3567    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3568    
3569          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3570          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3571          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3572          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3573    
3574          
3575      }    
3576    
3577      if(EXT){
3578          
3579          cout << "Found extended tracking algorythm branches" << endl;
3580    
3581          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3582          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3583          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3584          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3585          
3586          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3587          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3588          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3589          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3590    
3591        
3592          if(NUC){
3593              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3594              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3595              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3596              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3597              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3598              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3599              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3600              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3601          }
3602      }    
3603  }  }
3604  /**  /**
3605   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
# Line 2526  void PamLevel2::SetBranchAddress(TChain Line 3623  void PamLevel2::SetBranchAddress(TChain
3623    if (TRK2) {    if (TRK2) {
3624      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3625      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3626        NUC = t->GetBranchStatus("TrackNuclei");
3627        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3628    };    };
3629    if (TRK1) {    if (TRK1) {
3630      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
# Line 2539  void PamLevel2::SetBranchAddress(TChain Line 3638  void PamLevel2::SetBranchAddress(TChain
3638    // Calorimeter    // Calorimeter
3639    if (CAL2) {    if (CAL2) {
3640      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3641      cout << "Calorimeter  : set branch address CaloLevel2" << endl;      cout << "Calorimeter  : set branch address CaloLevel2" << endl;    
3642    };    };
3643    if (CAL1) {    if (CAL1) {
3644      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
# Line 2597  void PamLevel2::SetBranchAddress(TChain Line 3696  void PamLevel2::SetBranchAddress(TChain
3696    //    cout << "SelectionList: set branch address EventEntry"<<endl;    //    cout << "SelectionList: set branch address EventEntry"<<endl;
3697    
3698    //     }    //     }
3699      if(NUC){
3700          
3701          cout << "Found nuclei-track branches" << endl;
3702    
3703          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3704          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3705          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3706          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3707          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3708          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3709          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3710          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3711          
3712          ///copy the vector content inside a "fake" object (all aother info are missing)    
3713    
3714          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3715          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3716          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3717          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3718    
3719    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3720    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3721    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3722    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3723          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3724          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3725          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3726          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3727    
3728      }    
3729      if(EXT){
3730          
3731          cout << "Found extended tracking algorythm branches" << endl;
3732    
3733          t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3734    
3735          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3736          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3737          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3738          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3739          
3740          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3741          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3742          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3743          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3744          
3745          if(NUC){
3746              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3747              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3748              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3749              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3750              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3751              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3752              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3753              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3754          }
3755      }    
3756      
3757  }  }
3758    
3759    /**
3760     * Set the tracking algorythm
3761     * @param alg String to choose the track.
3762     * "" --> take the output of the standard tracking algorythm
3763     * "STD" --> take the output of the standard tracking algorythm
3764     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3765     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3766     * "EXTF" --> force the extended tracking algorythm
3767     * "NUCEXT" --> as "EXT", but for nuclei
3768     * "NUCEXTF" --> as "EXTF", but for nuclei
3769     */
3770    void PamLevel2::SetTrakingAlgorythm(char *alg){
3771    
3772    
3773        TString s(alg);
3774        if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
3775            cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
3776        if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
3777            cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
3778                
3779        trkAlg = alg;
3780    
3781    };
3782    char* PamLevel2::GetTrakingAlgorythm(){
3783    
3784    
3785        cout<<endl<<" Implemented tracking algorythm: ";
3786        cout<<endl<<"  \"\" or \"STD\"  --> take the output of the standard tracking algorythm";
3787        cout<<endl<<"  \"NUC\"      --> take the output of the standard tracking algorythm for nuclei cluster selection";
3788        cout<<endl<<"  \"EXT\"      --> in case the standard tracking algorythm has not found any track,";
3789        cout<<endl<<"                 take the output of the extended one";
3790        cout<<endl<<"  \"EXTF\"     --> force the extended tracking algorythm";
3791        cout<<endl<<"  \"NUCEXT\"   --> as \"EXT\", but for nuclei ";
3792        cout<<endl<<"  \"NUCEXTF\"  --> as \"EXTF\", but for nuclei";
3793    
3794        cout<<endl;
3795        cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
3796        cout<<endl;
3797    
3798        return trkAlg;
3799    };
3800    
3801    
3802    
3803  //--------------------------------------  //--------------------------------------
3804  //  //
3805  //  //
# Line 2633  TChain *PamLevel2::GetRunTree(TList *fl) Line 3833  TChain *PamLevel2::GetRunTree(TList *fl)
3833      };      };
3834    }    }
3835    
3836      cout << "done run chain  "<<  R->GetNtrees() <<" \n";
3837    
3838    
3839    if (RUN && R->GetNtrees()) {    if (RUN && R->GetNtrees()) {
3840    
3841      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
# Line 2672  TChain *PamLevel2::GetRunTree(TList *fl) Line 3875  TChain *PamLevel2::GetRunTree(TList *fl)
3875      cout << "----------------------------------------------------" << endl;      cout << "----------------------------------------------------" << endl;
3876    
3877    }    }
3878    else {  //   else {
3879      delete R;  //     delete R;
3880      R = 0;  //     R = 0;
3881    }  //   }
3882    
3883    run_tree = R;    run_tree = R;
3884    
# Line 2714  TTree *PamLevel2::GetRunTree(TFile *f) { Line 3917  TTree *PamLevel2::GetRunTree(TFile *f) {
3917    return T;    return T;
3918    
3919  }  }
 /**  
  * 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;  
   //  
 }  
 ;  
3920    
3921  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
3922    
3923      if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
3924    
3925    if (!run_tree) {    if (!run_tree) {
3926      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;
3927      return false;      return false;
# Line 2864  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3931  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3931      return (false);      return (false);
3932    }    }
3933    
3934    
3935    Int_t oldrun = irun; // store current run index    Int_t oldrun = irun; // store current run index
3936    
3937    // -----------------------------------------------------------------------    // -----------------------------------------------------------------------
# Line 2873  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3941  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3941    if (irun < 0) {    if (irun < 0) {
3942      irun = 0LL;      irun = 0LL;
3943      irunt = 0LL;      irunt = 0LL;
3944      irunentry = 0;      totrunentry = 0LL;
3945        totrunentrymin = 0LL;
3946        totrunentrymax = 0LL;
3947        irunentry = 0LL;
3948        il0entry = 0LL;
3949      prevshift = 0;      prevshift = 0;
3950        yprevshift = 0;
3951        prevabstime = 0;
3952        prevpktnum = 0;
3953        abstime = 0ULL;
3954        pktnum = 0;
3955        isFragment = false;
3956      run_tree->GetEntry(irun);      run_tree->GetEntry(irun);
3957      if (!GetOrbitalInfo())      if (!GetOrbitalInfo())
3958        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
3959      if (gltsync)      if ( fUseDBinRunInfo ){
3960        delete gltsync; //Emiliano        if (gltsync)
3961      if (!dbc || (dbc && !dbc->IsConnected()))          delete gltsync; //Emiliano
3962        SetDBConnection(); //Emiliano        if (!dbc || (dbc && !dbc->IsConnected()))
3963      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
3964      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)
3965        dbc->Close();// Emiliano        if (dbc){
3966    };          dbc->Close();// Emiliano
3967            delete dbc;
3968            dbc=0;
3969          }
3970        }
3971      }
3972    // ---------------------------------------------------------------    // ---------------------------------------------------------------
3973    // retrieve OBT and absolute time of the event    // retrieve OBT and absolute time of the event
3974    // ---------------------------------------------------------------    // ---------------------------------------------------------------
   ULong64_t abstime = 0LL;  
   //    ULong64_t obt     = 0LL; // Emiliano  
3975    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
3976      prevabstime = abstime;
3977      prevpktnum = pktnum;
3978    if (GetOrbitalInfo()) {    if (GetOrbitalInfo()) {
3979      abstime = GetOrbitalInfo()->absTime;      abstime = GetOrbitalInfo()->absTime;
3980      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
3981        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
3982    }    }
3983    else {    else {
3984      abstime = GetRunInfo()->RUNHEADER_TIME;      abstime = GetRunInfo()->RUNHEADER_TIME;
3985      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
3986        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
3987      }
3988    
3989      if (DBG){
3990        printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3991        printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3992        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3993        if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3994        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3995        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
3996    }    }
3997    
3998    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 2919  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4013  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4013      }      }
4014    
4015      //      //
4016      bool fromfirst = true; // first loop over runs      bool a = true;
4017        bool b = true;
4018        if ( fUseDBinRunInfo ){
4019          a = false;    
4020          b = false;
4021          if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4022          if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4023        }
4024        if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4025             || iev == 0 // or it is the first entry
4026             || (!isSync && (
4027                             (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4028                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4029             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4030          
4031          // check for a new run (ma prima il primo!)
4032          if (DBG){
4033            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4034            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4035            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4036            if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4037            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4038            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4039          }
4040          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4041          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4042          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4043          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4044          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4045          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4046    
4047      // ------------------------------------------------------        totrunentry = 0LL;
4048      // loop over runs to find the one that contains the event        runfirstentry = 0LL;
4049      // ------------------------------------------------------        for (Int_t r=0; r< run_tree->GetEntries();r++){
4050      while ((          // -------------------------------------------------------------------
4051      //          (          // save the index of the first entry of the run, relative to pam_tree,
4052      //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)          // and read a new run
4053      //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          // -------------------------------------------------------------------
4054      //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)          run_tree->GetEntry(r);//update runinfo
4055      //                obt <= GetRunInfo()->RUNTRAILER_OBT)          if ( r > 0 ){
4056      //              )            totrunentrymin = totrunentrymax+1;
4057            } else {
4058      !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)            totrunentrymin = 0LL;
4059          abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano          }
4060          obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano          totrunentry += GetRunInfo()->NEVENTS;
4061          || GetRunInfo()->NEVENTS == 0          totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4062      //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento          irun = r;        
4063          || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {          if ( fUseDBinRunInfo ){
4064              a = false;    
4065        //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;            b = false;
4066        //            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;
4067        //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;            if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4068        //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;          }
4069        // - - - - - - - - - - - - -          if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4070        // irunentry = position of current entry, relative to the run               ( !isSync &&
4071        // 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)
4072        // - - - - - - - - - - - - -                   && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
4073                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4074        // -----------------------------------------            if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4075        // store dead and live-time of previous run              if ( !isSync ){
4076        // -----------------------------------------                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4077        //            if(SELLI==0){                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4078        if (SELLI != 2) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4079          if (fromfirst) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4080            if (oldrun == irun) {                prevshift += (totrunentrymin-iev); // add the new shift to total shift
4081              /// decrement counters                totrunentrymin -= (totrunentrymin-iev); // shift run position min
4082              if (GetTrigLevel2()) {                totrunentrymax -= (totrunentrymin-iev); // shift run position max
4083                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);
4084                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
4085              }              } else {
4086              totdltime[2]--; //event counter                printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4087              if (DBG) {                cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4088                cout << endl;                cout << "\nFor bug reporting instructions, please see for example:\n";
4089                cout << "n.events     : " << totdltime[2] << endl;                cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4090                cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;                cout << "  " << endl;
               cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;  
4091              }              }
4092            }            }
4093            else {            runfirstentry = totrunentrymin; // first entry of the run in the level2
4094              totdltime[0] = 0;//live-time            
4095              totdltime[1] = 0;//dead-time  
4096              totdltime[2] = 0; //event counter            //
4097              if (DBG)            if ( fUseDBinRunInfo ){
4098                cout << " *** JUMP RUN *** irun " << irun << endl;              if (gltsync)
4099                  delete gltsync; // Emiliano
4100                if (!dbc || (dbc && !dbc->IsConnected()))
4101                  SetDBConnection(); //Emiliano
4102                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4103                TrkParams::Set(GetRunInfo(), dbc);
4104                if (dbc){
4105                  dbc->Close(); // Emiliano
4106                  delete dbc;
4107                  dbc=0;
4108                }          
4109                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4110                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4111                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4112                  cout
4113                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4114                    << endl;
4115                }
4116            }            }
4117            /// add an entry            //
4118            if (run_tree_clone)            if (DBG) printf(" found \n");
4119              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
4120                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
4121            /// reset counters            break;
4122            }
4123          } // loop over run
4124          
4125          // --------------------------------------
4126          // if there was no need to update the run
4127          // ---> exit with FALSE
4128          // --------------------------------------
4129          if (irun == oldrun){
4130            if (DBG) printf(" no new run \n");
4131            //        printf(" no new run \n");//TOGLITOGLI
4132            return (false);
4133          }      
4134          // --------------------------------------
4135          // ... otherwise
4136          // ---> exit with TRUE
4137          // --------------------------------------
4138    
4139          if (SELLI != 2) {
4140            /// decrement counters since this event belongs to a new run
4141            if (GetTrigLevel2()) {
4142              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4143              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4144            }
4145            totdltime[2]--; //event counter
4146            if (DBG) {
4147              cout << endl;
4148              cout << "n.events     : " << totdltime[2] << endl;
4149              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4150              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4151            }
4152            // add an entry
4153            if (run_tree_clone && totdltime[2] > 0)
4154              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4155                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4156            // reset counters
4157            if ( totdltime[2] > 0 ){
4158            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
4159              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4160              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2988  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4162  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4162            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
4163          }          }
4164        }        }
       //            }  
4165    
4166        irun++;        if (DBG){
4167        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4168        // if the end of run tree is reached...               << endl;        
4169        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4170        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4171          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4172            // -----------------------------------------------------          if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4173            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4174            // ---> 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;  
4175        }        }
4176        // -------------------------------------------------------------------        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4177        // 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);
4178        // and read a new run        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4179        // -------------------------------------------------------------------        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4180        if (irun > 0)        //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4181          runfirstentry += (GetRunInfo()->NEVENTS) - prevshift;        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4182        irunentry = 0;  
4183        prevshift = 0;        return (true);
4184        run_tree->GetEntry(irun);//update runinfo      } // need for run upgrade
4185        irunt = irun - irunoffset[run_tree->GetTreeNumber()];      if (DBG) printf("return false\n");
4186        if (gltsync)      return (false);
4187          delete gltsync; // Emiliano    }// SELLI = 0 SELLI = 2
4188        if (!dbc || (dbc && !dbc->IsConnected()))    
         SetDBConnection(); //Emiliano  
       gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano  
       if (dbc)  
         dbc->Close(); // Emiliano  
       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();  
   
     // ----------------------------------------------------  
     // 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);  
   };  
4189    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4190    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
4191    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3158  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4260  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4260        // update the tracker parameters        // update the tracker parameters
4261        // (non ho trovato nessun altro modo sicuro di farlo...)        // (non ho trovato nessun altro modo sicuro di farlo...)
4262        // ----------------------------------------------------        // ----------------------------------------------------
4263        if (!dbc || (dbc && !dbc->IsConnected()))        if ( fUseDBinRunInfo ){
4264          SetDBConnection();          if (!dbc || (dbc && !dbc->IsConnected()))
4265        TrkParams::Set(GetRunInfo(), dbc);            SetDBConnection();
4266        if (dbc)          TrkParams::Set(GetRunInfo(), dbc);
4267          dbc->Close();          if (dbc){
4268              dbc->Close();
4269              delete dbc;
4270              dbc=0;
4271            }
4272          }
4273        //            cout << endl;        //            cout << endl;
4274        prevshift = 0;        prevshift = 0;
4275          yprevshift = 0;
4276        return true;        return true;
4277      }      }
4278      return false;      return false;
# Line 3173  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4281  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4281    return false;    return false;
4282    //    //
4283  }  }
4284  ;  
4285    /**
4286     * Update the runinfo informations (to be used to have Run infos event by event basis)
4287     * @param run Pointer to the chain/tree which contains run infos
4288     * @return true if a new run has been read, false if it is still the same run
4289     */
4290    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4291      return (UpdateRunInfo(iev));
4292    }
4293    
4294  /**  /**
4295   * 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)
4296   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4299  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4299  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4300    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
4301  }  }
4302  ;  
4303    
4304  //--------------------------------------  //--------------------------------------
4305  //  //
# Line 4013  void PamLevel2::CreateCloneTrees(TFile * Line 5130  void PamLevel2::CreateCloneTrees(TFile *
5130      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5131      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5132      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5133        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5134    };    };
5135    
5136    Int_t i = 0;    Int_t i = 0;
# Line 4032  void PamLevel2::CreateCloneTrees(TFile * Line 5150  void PamLevel2::CreateCloneTrees(TFile *
5150        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5151        cout << "Tracker      : branch TrkHough" << endl;        cout << "Tracker      : branch TrkHough" << endl;
5152      };      };
5153        if(NUC){
5154            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5155            cout << "Tracker      : branch TrackNuclei" << endl;    
5156        }
5157        if(EXT){
5158            pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5159            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5160            cout << "Tracker      : branch RecoveredTrack" << endl;
5161            if(NUC){
5162                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5163                cout << "Tracker      : branch RecoveredTrackNuclei" << endl;                  
5164            }
5165        }
5166    
5167      i++;      i++;
5168    }    }
5169    
# Line 4046  void PamLevel2::CreateCloneTrees(TFile * Line 5178  void PamLevel2::CreateCloneTrees(TFile *
5178        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5179        cout << "Calorimeter  : branch CaloLevel2" << endl;        cout << "Calorimeter  : branch CaloLevel2" << endl;
5180      };      };
5181        if(NUC){
5182            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5183            cout << "Calorimeter  : branch TrackNuclei" << endl;    
5184        }
5185        if(EXT){
5186            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5187            cout << "Calorimeter  : branch RecoveredTrack" << endl;
5188            if(NUC){
5189                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5190                cout << "Calorimeter  : branch RecoveredTrackNuclei" << endl;                  
5191            }
5192        }
5193      i++;      i++;
5194    }    }
5195    
# Line 4054  void PamLevel2::CreateCloneTrees(TFile * Line 5198  void PamLevel2::CreateCloneTrees(TFile *
5198      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5199      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5200      cout << "ToF          : branch ToFLevel2" << endl;      cout << "ToF          : branch ToFLevel2" << endl;
5201        if(NUC){
5202            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5203            cout << "ToF          : branch TrackNuclei" << endl;    
5204        }
5205        if(EXT){
5206            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5207            cout << "ToF          : branch RecoveredTrack" << endl;
5208            if(NUC){
5209                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5210                cout << "ToF          : branch RecoveredTrackNuclei" << endl;                  
5211            }
5212        }
5213      i++;      i++;
5214    };    };
5215    // Trigger    // Trigger
# Line 4089  void PamLevel2::CreateCloneTrees(TFile * Line 5245  void PamLevel2::CreateCloneTrees(TFile *
5245      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
5246      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5247      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
5248        if(NUC){
5249            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5250            cout << "OrbitalInfo  : branch TrackNuclei" << endl;    
5251        }
5252        if(EXT){
5253            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5254            cout << "OrbitalInfo  : branch RecoveredTrack" << endl;
5255            if(NUC){
5256                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5257                cout << "OrbitalInfo  : branch RecoveredTrackNuclei" << endl;                  
5258            }
5259        }
5260      i++;      i++;
5261    };    };
5262    // GPamela    // GPamela
5263    if (GP) {    if (GP) {
5264      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5265      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5266      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
5267      i++;      i++;
5268    };    };
5269    
5270    
5271    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5272    
5273  }  }
# Line 4168  void PamLevel2::WriteCloneTrees() { Line 5338  void PamLevel2::WriteCloneTrees() {
5338  /**  /**
5339   * 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.
5340   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
5341  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
5342    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
5343    if (!pam_tree) {    if (!pam_tree) {
5344      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;
5345      return 0;      return 0;
# Line 4187  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5353  Int_t PamLevel2::GetEntry(Long64_t iee)
5353    //    return 0;    //    return 0;
5354    //    }    //    }
5355    
   Long64_t ii = 0;  
5356    //-------------------------------    //-------------------------------
5357    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
5358      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;
5359      return 0;      return 0;
5360    }    }
# Line 4210  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5374  Int_t PamLevel2::GetEntry(Long64_t iee)
5374    }    }
5375    
5376    //-------------------------------    //-------------------------------
5377    ii = iee;    //
5378    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5379    //    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;  
   //    }  
   //     }  
   
5380    
5381    return 1;    return 1;
5382    
# Line 4271  TTree* PamLevel2::GetYodaTree() { Line 5417  TTree* PamLevel2::GetYodaTree() {
5417    //===================================    //===================================
5418    if (irun < 0) {    if (irun < 0) {
5419      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5420      //  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;
5421      return NULL;      return NULL;
5422    }    }
5423    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5424    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
5425    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
5426        cout << "irootnew "<<irootnew<<endl;
5427      }
5428    
5429    //===================================    //===================================
5430    // load the level0 file    // load the level0 file
# Line 4314  TTree* PamLevel2::GetYodaTree() { Line 5462  TTree* PamLevel2::GetYodaTree() {
5462      if (!h0_obj)      if (!h0_obj)
5463        h0_obj = new EventHeader();        h0_obj = new EventHeader();
5464      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
5465      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5466      //---------------------------------------------------      //---------------------------------------------------
5467      // TRACKER:      // TRACKER:
5468      if (TRK0) {      if (TRK0) {
# Line 4332  TTree* PamLevel2::GetYodaTree() { Line 5480  TTree* PamLevel2::GetYodaTree() {
5480          calo0_obj->Set();          calo0_obj->Set();
5481        };        };
5482        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
5483      }      }
5484      //---------------------------------------------------      //---------------------------------------------------
5485      // TOF:      // TOF:
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 5488  TTree* PamLevel2::GetYodaTree() {
5488      }      }
5489    
5490      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5491        delete dbc;
5492        dbc=0;
5493    
5494    };    };
5495    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
5496    if (TRK0) {    if (TRK0) {
5497      //  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();  
5498      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
5499      TrkParams::LoadCalib();      TrkParams::LoadCalib();
5500      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
5501        cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;        cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
5502      };      };
5503      //  if(dbc)dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      if(dbc){
5504          dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5505          delete dbc;
5506          dbc=0;
5507        };
5508    }    }
   
   //    cout << l0_tree << endl;  
5509    return l0_tree;    return l0_tree;
   
5510  }  }
5511    
5512  /**  /**
# Line 4373  TTree* PamLevel2::GetYodaTree() { Line 5514  TTree* PamLevel2::GetYodaTree() {
5514   */   */
5515  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
5516    
5517    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
5518    
5519    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5520    if (irunentry < 0) {    // if it is a full file (not preselected)
5521      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5522      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;  
5523    
5524    ULong64_t obt = 0;      if (!GetYodaTree()){
5525    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5526    if (GetOrbitalInfo()) {        return 0;
5527      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
5528    
5529    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
5530      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5531      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5532    }        irunentry = 0LL;
5533    if (obt == 0 && pktn == 0 && !ISGP) {      }
5534      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
5535      return 0;      //  if file is NOT a preselected file
5536    }      //  ---------------------------------
5537        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5538    
5539        if (DBG){
5540          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5541          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5542          cout << " time "<< abstime << endl;
5543        }
5544    
5545        ULong64_t obt = 0;
5546        ULong64_t pktn = 0;
5547        if (GetOrbitalInfo()) {
5548          obt = GetOrbitalInfo()->OBT;
5549          pktn = GetOrbitalInfo()->pkt_num;
5550        }
5551    
5552    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
5553    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5554    // 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;  
5555      }      }
5556      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
5557      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
5558        return 0;        return 0;
5559      }      }
5560    
5561        // ---------------------------------------------------------------------
5562        // ATTENTION!!!
5563        // If data are not pre-processed with cleaner, the level0 tree may contain
5564        // spurious nested physics packets.
5565        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
5566        // while level2 tree DOES NOT!!
5567        // This means that "quellagiusta" in these cases is not correct.
5568        // In order to retrieve the correct level0 event, I implemented a check
5569        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
5570        // is shift forward until when the packets match.
5571        // ---------------------------------------------------------------------
5572        Long64_t shift = 0LL;
5573        Long64_t answer = quellagiusta + shift + yprevshift;
5574        Int_t readl0 = 0;
5575        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5576    
5577        if (DBG){
5578          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5579        }
5580    
5581    
5582      if (ISGP) {      if (ISGP) {
5583        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5584        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5585      }      }
5586    
5587      //  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 ){
5588      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
5589      //  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");
5590      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5591      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
5592      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5593      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
5594      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
5595      //        if (shift > 0) {
5596      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5597        prevshift = 0;          if (DBG)
5598        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5599      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5600            if (DBG)
5601              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5602                   << " prevshift " << prevshift << " )" << endl;
5603          }
5604          answer = quellagiusta +  shift+ yprevshift;
5605          readl0 = l0_tree->GetEntry(answer);
5606          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5607    
5608          if (!GetEventHeader()) {
5609            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5610            return 0;
5611          }
5612    
5613    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        //
5614        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5615        < GetYodaTree()->GetEntries() && shift < maxshift);          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5616            //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5617            yprevshift = 0LL;
5618            shift = -1LL;
5619          };
5620        
5621          shift++;
5622        }
5623                                      
5624    
5625    if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {      if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5626      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if ( isSync ){
5627      return 0;          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5628    }          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5629    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
5630    //    return GetYodaTree()->GetEntry(quellagiusta);          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5631    if (shift > 1)          cout << "  " << endl;
5632      prevshift += (shift - 1);        }
5633          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5634          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
5635          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5636          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5637          // 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
5638          // connect to db
5639          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5640          //
5641          if (GetOrbitalInfo()){
5642            abstime = GetOrbitalInfo()->absTime;
5643          } else {
5644            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5645            return 0;
5646          }
5647          // query DB looking for runs containing the processed event
5648          TSQLResult *pResult;
5649          TSQLRow *Row = NULL;
5650          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5651          if ( DBG ) printf(" query is %s \n",myquery.Data());
5652          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5653          pResult = dbc->Query(myquery.Data());    
5654          if (!pResult->GetRowCount()){
5655            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5656            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5657            cout << "\nFor bug reporting instructions, please see for example:\n";
5658            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5659            cout << "  " << endl;
5660            return 0;
5661          }
5662          if ( pResult->GetRowCount() == 1 ){
5663            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5664            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5665          }
5666          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5667            if (Row) delete Row;
5668            Row = pResult->Next();  
5669            if( Row == NULL ) break;
5670            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5671            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5672            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5673            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5674    
5675            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5676            Bool_t rfound = false;
5677            totrunentry = 0LL;
5678            runfirstentry = 0LL;
5679            for (Int_t r=0; r< run_tree->GetEntries();r++){
5680              run_tree->GetEntry(r);//update runinfo
5681              if ( r > 0 ){
5682                totrunentrymin = totrunentrymax+1;
5683              } else {
5684                totrunentrymin = 0LL;
5685              }
5686              totrunentry += GetRunInfo()->NEVENTS;
5687              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5688              irun = r;        
5689    
5690              if (idrun == GetRunInfo()->ID){
5691                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5692                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5693                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5694                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5695                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5696                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
5697                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
5698                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
5699                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5700                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5701                }
5702                runfirstentry = totrunentrymin; // first entry of the run in the level2
5703              
5704    
5705    return answer;              //
5706                if (gltsync)
5707                  delete gltsync; // Emiliano
5708                if (!dbc || (dbc && !dbc->IsConnected()))
5709                  SetDBConnection(); //Emiliano
5710                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
5711                if (dbc){
5712                  dbc->Close(); // Emiliano
5713                  delete dbc;
5714                  dbc=0;
5715                }          
5716                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
5717                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
5718                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
5719                  cout
5720                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
5721                    << endl;
5722                }
5723                //
5724                if (DBG) printf(" found \n");
5725                //            printf(" found \n");//TOGLITOGLI
5726                rfound = true;
5727                //
5728                break;
5729              }
5730            } // loop over run
5731            if ( !rfound ){
5732              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
5733              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5734              cout << "\nFor bug reporting instructions, please see for example:\n";
5735              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5736              cout << "  " << endl;        
5737              return 0;
5738            }
5739    
5740            // here we got the first run and we can check if it contains the level0 event
5741            if (!GetYodaTree()){
5742              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5743              return 0;
5744            }      
5745    
5746            // get the current run entry
5747            irunentry = iev - runfirstentry;
5748            if (irunentry < 0) {
5749              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5750              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5751              irunentry = 0LL;
5752            }
5753            //  ---------------------------------
5754            //  if file is NOT a preselected file
5755            //  ---------------------------------
5756            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5757          
5758            if (DBG){
5759              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5760              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5761              cout << " time "<< abstime << endl;
5762            }
5763            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5764            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5765            //        cout << " time "<< abstime << endl; // TOGLITOGLI
5766          
5767            shift = 0;
5768            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
5769            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5770    
5771            if (DBG){
5772              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5773            }
5774            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5775          
5776            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5777              if (shift > 0) {
5778                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5779                if (DBG)
5780                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5781                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5782                if (DBG)
5783                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5784                       << " prevshift " << prevshift << " )" << endl;
5785              }
5786              answer = quellagiusta +  shift+ yprevshift;
5787              readl0 = l0_tree->GetEntry(answer);
5788              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5789            
5790              if (!GetEventHeader()) {
5791                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5792                return 0;
5793              }
5794              //
5795              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5796                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5797                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5798                yprevshift = 0;
5799                shift = -1;
5800              };
5801            
5802              shift++;
5803            }
5804          
5805            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5806              //still not the good run... continue with the nex one!
5807              printf("still not the good run... continue with the nex one!\n");
5808            } else {
5809              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
5810              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
5811              if (shift > 1) yprevshift = (shift - 1);
5812              if (Row) delete Row;
5813              delete pResult;  
5814              if (dbc){
5815                dbc->Close(); // Emiliano
5816                delete dbc;
5817                dbc=0;
5818              }    
5819              il0entry = answer;
5820              return readl0;
5821            }
5822            // perhaps it is all
5823          }// loop over runs containing the event
5824          if (Row) delete Row;
5825          delete pResult;  
5826          if (dbc){
5827            dbc->Close(); // Emiliano
5828            delete dbc;
5829            dbc=0;
5830          }          
5831          // arriving here it means no run found, cannot be! error!
5832          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
5833          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5834          cout << "\nFor bug reporting instructions, please see for example:\n";
5835          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5836          cout << "  " << endl;        
5837          return 0;
5838        } else {
5839          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5840          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5841          //    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()) );
5842          if (shift > 1) yprevshift = (shift - 1);
5843          il0entry = answer;
5844          return readl0;
5845        }
5846    
5847        /*  } // if selli 0 || 2
5848      if ( SELLI == 1 && hasL0EE ){
5849        sel_tree->GetEntry(iev);  
5850        Long64_t answer = il0entry;
5851        Int_t readl0 = 0;
5852        readl0 = l0_tree->GetEntry(answer);
5853        return readl0;
5854        }*/
5855      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
5856      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5857      cout << "\nFor bug reporting instructions, please see for example:\n";
5858      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5859      cout << "  " << endl;
5860      return 0;
5861  }  }
5862    
5863  /**  /**
5864   * \Brief Set DB connection   * \Brief Set DB connection
5865   */   */
# Line 4491  Bool_t PamLevel2::SetDBConnection() { Line 5873  Bool_t PamLevel2::SetDBConnection() {
5873      cout << "USER " << user << endl;      cout << "USER " << user << endl;
5874      cout << "PSW  " << psw << endl;      cout << "PSW  " << psw << endl;
5875    }    }
5876    dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());    Bool_t notconn = true;
5877    if (!dbc)    Int_t trials = 0;
5878      return false;    while ( notconn && trials < 10 ){
5879    if (!dbc->IsConnected())      //    gSystem->Sleep(500);
5880      return false;      dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
5881        //dbc->Connect(host.Data(), user.Data(), psw.Data());
5882        if ( dbc ) notconn = false;
5883        if (DBG) printf("<%i> test connection...\n ",trials);
5884        if (!dbc){
5885          if (DBG) printf(" :( failed, no pointer \n");
5886          notconn = true;
5887          //      return false;
5888        };
5889        if (dbc && !dbc->IsConnected()){
5890          if (DBG) printf(" :( failed, no connection \n");
5891          notconn = true;
5892          //      return false;
5893        };
5894        trials++;
5895      };
5896      if ( notconn ) return false;
5897      //
5898      if (DBG) printf("=connected!\n");
5899    stringstream myquery; // EMILIANO    stringstream myquery; // EMILIANO
5900    myquery.str(""); // EMILIANO    myquery.str(""); // EMILIANO
5901    myquery << "SET time_zone='+0:00'"; // EMILIANO    myquery << "SET time_zone='+0:00'"; // EMILIANO
5902    dbc->Query(myquery.str().c_str()); // EMILIANO    dbc->Query(myquery.str().c_str()); // EMILIANO
5903    if (DBG)    if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;  
5904    return true;    return true;
5905    
5906  }  }
# Line 4553  Long64_t PamLevel2::GetReadEntry() { Line 5952  Long64_t PamLevel2::GetReadEntry() {
5952  void PamLevel2::SetSortingMethod(TString how) {  void PamLevel2::SetSortingMethod(TString how) {
5953    if (howtosort != how) {    if (howtosort != how) {
5954      issorted = false;      issorted = false;
5955        issorted_new = false;
5956    }    }
5957    howtosort = how;    howtosort = how;
5958  }  }

Legend:
Removed from v.1.81  
changed lines
  Added in v.1.95

  ViewVC Help
Powered by ViewVC 1.1.23