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

Diff of /PamelaLevel2/src/PamLevel2.cpp

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

revision 1.82 by mocchiut, Thu Jun 4 15:10:47 2009 UTC revision 1.96 by pam-ts, Wed Oct 15 10:02: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 = "STD";//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();
# Line 633  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 646  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 690  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 700  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 747  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 778  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 792  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 811  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 821  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 833  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 875  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 902  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 974  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 987  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 1012  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 1044  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 1056  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 1197  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 1312  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 1329  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 1347  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 1356  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 1367  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 1376  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 1385  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 1394  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 1405  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 1414  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 1423  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 1467  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 1488  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 1663  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 1671  void PamLevel2::SortTracks() { Line 2581  void PamLevel2::SortTracks() {
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 1681  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  //  //
2600  //--------------------------------------  //--------------------------------------
2601    
2602  /**  /**
2603   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
2604   * It override TrkLevel2::GetTrack(int it).   * It override TrkLevel2::GetTrack(int it).
2605   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2606   */   */
2607  PamTrack *PamLevel2::GetTrack(int it) {  PamTrack *PamLevel2::GetTrackOld(int it) {
   
   //    if(!trk2_obj) return 0;  
   
   // //  *-*-*-*+-*-*-*-*-*-*-*-*-*  
   //     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;  
   //     };  
2608    
2609    //     return track;    PamTrack *track = NULL;
2610    
   //    cout << "PamLevel2::GetTrack(int it) "<<endl;  
2611    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2612    SortTracks();    SortTracks();
2613    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2614    if (!tsorted)    if (!tsorted)
2615      return 0;      return track;
2616    if (!tsorted->GetEntries())    if (!tsorted->GetEntries())
2617      return 0;      return track;
2618    
   PamTrack *track = 0;  
2619    
2620    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];  
2621      track = (PamTrack*)((*tsorted)[it]);      track = (PamTrack*)((*tsorted)[it]);
2622    }    }
2623    else {    else {
2624      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "      cout << "PamLevel2::GetTrackOld(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2625          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2626    };    };
2627    
2628    return track;    return track;
2629    
2630    };
2631    
2632    //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2633    
2634    /**
2635     * Retrieves the it-th Pamela "physical" track.
2636     * It override TrkLevel2::GetTrack(int it).
2637     * @param it Track number, ranging from 0 to GetNTracks().
2638     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2639     */
2640    PamTrack *PamLevel2::GetTrack(int it, const char* alg) {
2641    
2642        TString s(alg);
2643        if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrackOld(it); //old algorythm
2644    
2645    
2646        SortTracksNew();
2647        // >> fill tsorted, timage and text
2648    
2649        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2650    
2651            if( s.Contains("NUC")){
2652                if(
2653                    tsorted_nuc &&
2654                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2655                    it >= 0 &&                                                //valid index
2656                    true) return (PamTrack*)((*tsorted_nuc)[it]);                 //ok return the track
2657            }else{
2658                if(
2659                    tsorted &&
2660                    it < tsorted->GetEntries() &&                             //enough tracks found
2661                    it >= 0 &&                                                //valid index
2662                    true )return (PamTrack*)((*tsorted)[it]);                 //ok return the track
2663            }
2664    
2665        }
2666        
2667    
2668        /////////////////////////////////////////////////////////////////////////
2669        /// if requested get track from extended algorythm output
2670        /////////////////////////////////////////////////////////////////////////
2671        
2672        if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2673            
2674            if(s.Contains("NUC")){
2675                if(
2676                    text_nuc &&
2677                    it < text_nuc->GetEntries() &&                             //enough tracks found
2678                    it >= 0 &&                                                //valid index
2679                    true) return (PamTrack*)((*text_nuc)[it]);
2680            }else{
2681                if(
2682                    text &&
2683                    it < text->GetEntries() &&                             //enough tracks found
2684                    it >= 0 &&                                                //valid index
2685                    true) return (PamTrack*)((*text)[it]);
2686            }
2687    
2688        };
2689        
2690        
2691        cout << "PamTrack *PamLevel2::GetTrack("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2692        
2693        return NULL;
2694    
2695    }
2696    ;
2697    TClonesArray *PamLevel2::GetTracks() {
2698    
2699      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2700      SortTracks();
2701      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2702    
2703      return tsorted;
2704    
2705  }  }
2706  ;  ;
2707    Int_t  PamLevel2::GetNTracks(const char* alg) {
2708    
2709    
2710    //    cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2711    
2712        TString s(alg);
2713    
2714        if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2715            
2716        if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2717            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2718            return trk_ext_obj->GetEntries();//ok
2719        }
2720        if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2721            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2722                return (trk2_nuc_obj->TrkLevel2::GetNTracks() ?  trk2_nuc_obj->TrkLevel2::GetNTracks() :  trk_ext_nuc_obj->GetEntries() );  
2723            return (trk2_obj->TrkLevel2::GetNTracks() ?  trk2_obj->TrkLevel2::GetNTracks() :  trk_ext_obj->GetEntries() );
2724        }
2725        if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2726            return trk2_nuc_obj->TrkLevel2::GetNTracks();
2727        
2728        cout << "Int_t  PamLevel2::GetNTracks("<<alg<<") -- unrecognised algorithm"<<endl;
2729    
2730        return 0;
2731    
2732    }
2733    
2734    
2735  //--------------------------------------  //--------------------------------------
2736  //  //
# Line 1757  PamTrack *PamLevel2::GetTrack(int it) { Line 2740  PamTrack *PamLevel2::GetTrack(int it) {
2740   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2741   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2742   */   */
2743  PamTrack *PamLevel2::GetTrackImage(int it) {  PamTrack *PamLevel2::GetTrackImageOld(int it) {
2744    
2745    //  *-*-*-*-*-*-*-*-*-*-*-*-*    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2746    SortTracks();    SortTracks();
# Line 1781  PamTrack *PamLevel2::GetTrackImage(int i Line 2764  PamTrack *PamLevel2::GetTrackImage(int i
2764      };      };
2765    }    }
2766    else {    else {
2767      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "      cout << "PamLevel2::GetTrackImageOld(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2768          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2769    };    };
2770    
2771    return image;    return image;
2772  }  }
   
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
2773  /**  /**
2774   * Get the Pamela detector trees in a single file and make them friends.   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2775   * @param f TFile pointer   * @param it Track number, ranging from 0 to GetNTracks().
2776   * @param detlist String to select trees to be included   * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
  * @return Pointer to a TTree  
2777   */   */
2778  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {  PamTrack *PamLevel2::GetTrackImage(int it, const char* alg) {
2779    
2780    if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {      TString s(alg);
2781        if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrackImageOld(it); //old algorythm
2782    
2783    
2784        SortTracksNew();
2785        // >> fill tsorted, timage and text
2786    
2787        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2788    
2789            if( s.Contains("NUC")){
2790                if(
2791                    tsorted_nuc &&
2792                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2793                    it >= 0 &&                                                //valid index
2794                    true){
2795                    TClonesArray &t = *(tsorted_nuc);
2796                    PamTrack *temp = (PamTrack*) t[it];
2797                    if (temp->GetTrkTrack()->HasImage()) {
2798                        return (PamTrack*)((*timage_nuc)[it]);                 //ok return the track
2799                    }else{
2800                        return NULL;
2801                    }
2802                    
2803                }
2804            }else{
2805                if(
2806                    tsorted &&
2807                    it < tsorted->GetEntries() &&                             //enough tracks found
2808                    it >= 0 &&                                                //valid index
2809                    true ){
2810                    TClonesArray &t = *(tsorted);
2811                    PamTrack *temp = (PamTrack*) t[it];
2812                    if (temp->GetTrkTrack()->HasImage()) {              
2813                        return (PamTrack*)((*timage)[it]);                 //ok return the track
2814                    }else{
2815                        return NULL;
2816                    }
2817                }
2818            }
2819    
2820        }
2821    
2822        cout << "PamTrack *PamLevel2::GetTrackImage("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2823          
2824        return NULL;
2825    
2826    }
2827    ;
2828    
2829    //--------------------------------------
2830    //
2831    //
2832    //--------------------------------------
2833    /**
2834     * Get the Pamela detector trees in a single file and make them friends.
2835     * @param f TFile pointer
2836     * @param detlist String to select trees to be included
2837     * @return Pointer to a TTree
2838     */
2839    TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
2840    
2841      if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
2842      cout << "+AUTO" << endl;      cout << "+AUTO" << endl;
2843      GetWhichTrees(f);      GetWhichTrees(f);
2844    };    };
# Line 1876  TTree *PamLevel2::GetPamTree(TFile *f, T Line 2914  TTree *PamLevel2::GetPamTree(TFile *f, T
2914      if (!Trout)      if (!Trout)
2915        Trout = T;        Trout = T;
2916      else      else
2917        Trout->AddFriend(T);          Trout->AddFriend(T);
2918    }    }
2919    else {    else {
2920      cout << "Tracker      : missing tree" << endl;      cout << "Tracker      : missing tree" << endl;
# Line 2113  TList* PamLevel2::GetListOfLevel2Files(T Line 3151  TList* PamLevel2::GetListOfLevel2Files(T
3151          if (file.EndsWith(".root")) {          if (file.EndsWith(".root")) {
3152            TString filedir;            TString filedir;
3153            cout << ddir << endl;            cout << ddir << endl;
3154            if (ddir != "") {            if ( ddir != "" ) {
3155              filedir = ddir; // take the input dir              filedir = ddir; // take the input dir
3156            }            }
3157            else {            else {
# Line 2121  TList* PamLevel2::GetListOfLevel2Files(T Line 3159  TList* PamLevel2::GetListOfLevel2Files(T
3159              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
3160            };            };
3161            filedir.Append("/");            filedir.Append("/");
3162            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3163              char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3164            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
3165            cout << fullpath << endl;            cout << fullpath << endl;
3166            delete fullpath;            delete fullpath;
# Line 2288  TChain *PamLevel2::GetPamTree(TList *fl, Line 3327  TChain *PamLevel2::GetPamTree(TList *fl,
3327          G->Add(name);          G->Add(name);
3328        if (SELLI == 1)        if (SELLI == 1)
3329          L->Add(name);          L->Add(name);
3330      };       };
3331    };    };
3332    
3333    cout << "done chains\n";    cout << "done data-tree chains  "<<  T->GetNtrees() <<" \n";
3334    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3335    
3336    // -------------------------------------------    // -------------------------------------------
3337    // make friends    // make friends
# Line 2375  TChain *PamLevel2::GetPamTree(TList *fl, Line 3414  TChain *PamLevel2::GetPamTree(TList *fl,
3414    //  =====================================    //  =====================================
3415    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3416    //  =====================================    //  =====================================
3417    SetBranchAddress(Trout);    if( Trout->GetNtrees() )SetBranchAddress(Trout);
3418    
3419    //  ------------------------------------    //  ------------------------------------
3420    //  finally handle selection trees...    //  finally handle selection trees...
3421    //  (it is not friend of pamela tree)    //  (it is not friend of pamela tree)
3422    //  ------------------------------------    //  ------------------------------------
3423    
3424    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "----------------------------------------------------" << endl;
3425    
3426    // Selection List    // Selection List
3427    if (L && SELLI == 1) {    if (L && SELLI == 1) {
# Line 2392  TChain *PamLevel2::GetPamTree(TList *fl, Line 3431  TChain *PamLevel2::GetPamTree(TList *fl,
3431      cout << "SelectionList: set branch address RunEntry" << endl;      cout << "SelectionList: set branch address RunEntry" << endl;
3432      L->SetBranchAddress("EventEntry", &irunentry);      L->SetBranchAddress("EventEntry", &irunentry);
3433      cout << "SelectionList: set branch address EventEntry" << endl;      cout << "SelectionList: set branch address EventEntry" << endl;
3434        /*    if ( L->GetBranch("L0EventEntry") ){
3435          hasL0EE = true;
3436          L->SetBranchAddress("L0EventEntry", &il0entry);
3437          cout << "SelectionList: set branch address L0EventEntry" << endl;
3438        } else {
3439          hasL0EE = false; // backward compatibility with old preselected files...
3440          }*/
3441      sel_tree = L;      sel_tree = L;
3442      //          if(!Trout)Trout=O;      //          if(!Trout)Trout=O;
3443      //          else Trout->AddFriend("SelectionList");      //          else Trout->AddFriend("SelectionList");
3444      cout << "+SelectionList" << endl;      cout << "+SelectionList" << endl;
3445      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;      cout << "----------------------------------------------------" << endl;
3446    }    }
3447    else {    else {
3448      //  cout << "SelectionList  : missing tree"<<endl;      //  cout << "SelectionList  : missing tree"<<endl;
# Line 2422  TChain *PamLevel2::GetPamTree(TList *fl, Line 3468  TChain *PamLevel2::GetPamTree(TList *fl,
3468   */   */
3469  void PamLevel2::SetBranchAddress(TTree *t) {  void PamLevel2::SetBranchAddress(TTree *t) {
3470    
3471    TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");  //   TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3472    TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");  //   TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3473    TRKh = TRKh & t->GetBranchStatus("TrkHough");  //   TRKh = TRKh & t->GetBranchStatus("TrkHough");
3474    CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");  //   CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3475    CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");  //   CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3476    TOF = TOF & t->GetBranchStatus("ToFLevel2");  //   TOF = TOF & t->GetBranchStatus("ToFLevel2");
3477    TRG = TRG & t->GetBranchStatus("TrigLevel2");  //   TRG = TRG & t->GetBranchStatus("TrigLevel2");
3478    S4 = S4 & t->GetBranchStatus("S4Level2");  //   S4 = S4 & t->GetBranchStatus("S4Level2");
3479    ND = ND & t->GetBranchStatus("NDLevel2");  //   ND = ND & t->GetBranchStatus("NDLevel2");
3480    AC = AC & t->GetBranchStatus("AcLevel2");  //   AC = AC & t->GetBranchStatus("AcLevel2");
3481    ORB = ORB & t->GetBranchStatus("OrbitalInfo");  //   ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3482    GP = GP & t->GetBranchStatus("h20");  //   GP = GP & t->GetBranchStatus("h20");
3483    
3484    
3485    // Tracker    // Tracker
3486    if (TRK1) {    if (TRK1) {
# Line 2443  void PamLevel2::SetBranchAddress(TTree * Line 3490  void PamLevel2::SetBranchAddress(TTree *
3490    if (TRK2) {    if (TRK2) {
3491      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3492      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3493        NUC = t->GetBranchStatus("TrackNuclei");
3494        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );    
3495    };    };
3496    if (TRKh) {    if (TRKh) {
3497      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
# Line 2503  void PamLevel2::SetBranchAddress(TTree * Line 3552  void PamLevel2::SetBranchAddress(TTree *
3552    
3553      cout << "h20          : set branch address GPamela " << endl;      cout << "h20          : set branch address GPamela " << endl;
3554    };    };
3555      if(NUC){
3556          
3557          cout << "Found nuclei-track branches" << endl;
3558    
3559          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3560          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3561          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3562          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3563          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3564          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3565          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3566          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3567    
3568          ///copy the vector content inside a "fake" object (all aother info are missing)    
3569    
3570          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3571          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3572          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3573          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3574    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3575    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3576    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3577    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3578    
3579          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3580          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3581          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3582          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3583    
3584          
3585      }    
3586    
3587      if(EXT){
3588          
3589          cout << "Found extended tracking algorythm branches" << endl;
3590    
3591          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3592          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3593          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3594          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3595          
3596          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3597          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3598          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3599          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3600    
3601        
3602          if(NUC){
3603              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3604              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3605              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3606              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3607              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3608              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3609              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3610              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3611          }
3612      }    
3613  }  }
3614  /**  /**
3615   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
# Line 2527  void PamLevel2::SetBranchAddress(TChain Line 3633  void PamLevel2::SetBranchAddress(TChain
3633    if (TRK2) {    if (TRK2) {
3634      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3635      cout << "Tracker      : set branch address TrkLevel2" << endl;      cout << "Tracker      : set branch address TrkLevel2" << endl;
3636        NUC = t->GetBranchStatus("TrackNuclei");
3637        EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3638    };    };
3639    if (TRK1) {    if (TRK1) {
3640      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
# Line 2540  void PamLevel2::SetBranchAddress(TChain Line 3648  void PamLevel2::SetBranchAddress(TChain
3648    // Calorimeter    // Calorimeter
3649    if (CAL2) {    if (CAL2) {
3650      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3651      cout << "Calorimeter  : set branch address CaloLevel2" << endl;      cout << "Calorimeter  : set branch address CaloLevel2" << endl;    
3652    };    };
3653    if (CAL1) {    if (CAL1) {
3654      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
# Line 2598  void PamLevel2::SetBranchAddress(TChain Line 3706  void PamLevel2::SetBranchAddress(TChain
3706    //    cout << "SelectionList: set branch address EventEntry"<<endl;    //    cout << "SelectionList: set branch address EventEntry"<<endl;
3707    
3708    //     }    //     }
3709      if(NUC){
3710          
3711          cout << "Found nuclei-track branches" << endl;
3712    
3713          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3714          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3715          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3716          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3717          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3718          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3719          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3720          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3721          
3722          ///copy the vector content inside a "fake" object (all aother info are missing)    
3723    
3724          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3725          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3726          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3727          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3728    
3729    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3730    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3731    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3732    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3733          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3734          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3735          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3736          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3737    
3738      }    
3739      if(EXT){
3740          
3741          cout << "Found extended tracking algorythm branches" << endl;
3742    
3743          t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3744    
3745          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3746          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3747          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3748          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3749          
3750          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3751          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3752          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3753          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3754          
3755          if(NUC){
3756              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3757              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3758              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3759              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3760              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3761              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3762              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3763              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3764          }
3765      }    
3766      
3767  }  }
3768    
3769    /**
3770     * Set the tracking algorythm
3771     * @param alg String to choose the track.
3772     * "" --> take the output of the standard tracking algorythm
3773     * "STD" --> take the output of the standard tracking algorythm
3774     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3775     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3776     * "EXTF" --> force the extended tracking algorythm
3777     * "NUCEXT" --> as "EXT", but for nuclei
3778     * "NUCEXTF" --> as "EXTF", but for nuclei
3779     */
3780    // void PamLevel2::SetTrackingAlgorythm(const char *alg){
3781    
3782    
3783    //     TString s(alg);
3784    //     if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
3785    //      cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
3786    //     if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
3787    //      cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
3788                
3789    //     trkAlg = alg;
3790    
3791    // };
3792    // const char* PamLevel2::GetTrackingAlgorythm(){
3793    
3794    
3795    //     cout<<endl<<" Implemented tracking algorythm: ";
3796    //     cout<<endl<<"  \"\" or \"STD\"  --> take the output of the standard tracking algorythm";
3797    //     cout<<endl<<"  \"NUC\"      --> take the output of the standard tracking algorythm for nuclei cluster selection";
3798    //     cout<<endl<<"  \"EXT\"      --> in case the standard tracking algorythm has not found any track,";
3799    //     cout<<endl<<"                 take the output of the extended one";
3800    //     cout<<endl<<"  \"EXTF\"     --> force the extended tracking algorythm";
3801    //     cout<<endl<<"  \"NUCEXT\"   --> as \"EXT\", but for nuclei ";
3802    //     cout<<endl<<"  \"NUCEXTF\"  --> as \"EXTF\", but for nuclei";
3803    
3804    //     cout<<endl;
3805    //     cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
3806    //     cout<<endl;
3807    
3808    //     return trkAlg;
3809    // };
3810    
3811    
3812    
3813  //--------------------------------------  //--------------------------------------
3814  //  //
3815  //  //
# Line 2634  TChain *PamLevel2::GetRunTree(TList *fl) Line 3843  TChain *PamLevel2::GetRunTree(TList *fl)
3843      };      };
3844    }    }
3845    
3846      cout << "done run chain  "<<  R->GetNtrees() <<" \n";
3847    
3848    
3849    if (RUN && R->GetNtrees()) {    if (RUN && R->GetNtrees()) {
3850    
3851      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
# Line 2673  TChain *PamLevel2::GetRunTree(TList *fl) Line 3885  TChain *PamLevel2::GetRunTree(TList *fl)
3885      cout << "----------------------------------------------------" << endl;      cout << "----------------------------------------------------" << endl;
3886    
3887    }    }
3888    else {  //   else {
3889      delete R;  //     delete R;
3890      R = 0;  //     R = 0;
3891    }  //   }
3892    
3893    run_tree = R;    run_tree = R;
3894    
# Line 2715  TTree *PamLevel2::GetRunTree(TFile *f) { Line 3927  TTree *PamLevel2::GetRunTree(TFile *f) {
3927    return T;    return T;
3928    
3929  }  }
 /**  
  * 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;  
   //  
 }  
 ;  
3930    
3931  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
3932    
3933      if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
3934    
3935    if (!run_tree) {    if (!run_tree) {
3936      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;
3937      return false;      return false;
# Line 2865  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3941  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3941      return (false);      return (false);
3942    }    }
3943    
3944    
3945    Int_t oldrun = irun; // store current run index    Int_t oldrun = irun; // store current run index
3946    
3947    // -----------------------------------------------------------------------    // -----------------------------------------------------------------------
# Line 2874  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3951  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3951    if (irun < 0) {    if (irun < 0) {
3952      irun = 0LL;      irun = 0LL;
3953      irunt = 0LL;      irunt = 0LL;
3954      irunentry = 0;      totrunentry = 0LL;
3955        totrunentrymin = 0LL;
3956        totrunentrymax = 0LL;
3957        irunentry = 0LL;
3958        il0entry = 0LL;
3959      prevshift = 0;      prevshift = 0;
3960        yprevshift = 0;
3961        prevabstime = 0;
3962        prevpktnum = 0;
3963        abstime = 0ULL;
3964        pktnum = 0;
3965        isFragment = false;
3966      run_tree->GetEntry(irun);      run_tree->GetEntry(irun);
3967      if (!GetOrbitalInfo())      if (!GetOrbitalInfo())
3968        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
3969      if (gltsync)      if ( fUseDBinRunInfo ){
3970        delete gltsync; //Emiliano        if (gltsync)
3971      if (!dbc || (dbc && !dbc->IsConnected()))          delete gltsync; //Emiliano
3972        SetDBConnection(); //Emiliano        if (!dbc || (dbc && !dbc->IsConnected()))
3973      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
3974      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)
3975        dbc->Close();// Emiliano        if (dbc){
3976        dbc=0;          dbc->Close();// Emiliano
3977      };          delete dbc;
3978    };          dbc=0;
3979          }
3980        }
3981      }
3982    // ---------------------------------------------------------------    // ---------------------------------------------------------------
3983    // retrieve OBT and absolute time of the event    // retrieve OBT and absolute time of the event
3984    // ---------------------------------------------------------------    // ---------------------------------------------------------------
   ULong64_t abstime = 0LL;  
   //    ULong64_t obt     = 0LL; // Emiliano  
3985    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
3986      prevabstime = abstime;
3987      prevpktnum = pktnum;
3988    if (GetOrbitalInfo()) {    if (GetOrbitalInfo()) {
3989      abstime = GetOrbitalInfo()->absTime;      abstime = GetOrbitalInfo()->absTime;
3990      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
3991        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
3992    }    }
3993    else {    else {
3994      abstime = GetRunInfo()->RUNHEADER_TIME;      abstime = GetRunInfo()->RUNHEADER_TIME;
3995      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
3996        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
3997      }
3998    
3999      if (DBG){
4000        printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4001        printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4002        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4003        if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4004        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4005        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4006    }    }
4007    
4008    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 2922  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4023  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4023      }      }
4024    
4025      //      //
4026      bool fromfirst = true; // first loop over runs      bool a = true;
4027        bool b = true;
4028        if ( fUseDBinRunInfo ){
4029          a = false;    
4030          b = false;
4031          if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4032          if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4033        }
4034        if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4035             || iev == 0 // or it is the first entry
4036             || (!isSync && (
4037                             (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4038                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4039             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4040          
4041          // check for a new run (ma prima il primo!)
4042          if (DBG){
4043            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4044            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4045            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4046            if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4047            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4048            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4049          }
4050          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4051          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4052          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4053          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4054          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4055          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4056    
4057      // ------------------------------------------------------        totrunentry = 0LL;
4058      // loop over runs to find the one that contains the event        runfirstentry = 0LL;
4059      // ------------------------------------------------------        for (Int_t r=0; r< run_tree->GetEntries();r++){
4060      while ((          // -------------------------------------------------------------------
4061      //          (          // save the index of the first entry of the run, relative to pam_tree,
4062      //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)          // and read a new run
4063      //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          // -------------------------------------------------------------------
4064      //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)          run_tree->GetEntry(r);//update runinfo
4065      //                obt <= GetRunInfo()->RUNTRAILER_OBT)          if ( r > 0 ){
4066      //              )            totrunentrymin = totrunentrymax+1;
4067            } else {
4068      !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)            totrunentrymin = 0LL;
4069          abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano          }
4070          obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano          totrunentry += GetRunInfo()->NEVENTS;
4071          || GetRunInfo()->NEVENTS == 0          totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4072      //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento          irun = r;        
4073          || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {          if ( fUseDBinRunInfo ){
4074              a = false;    
4075        //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;            b = false;
4076        //            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;
4077        //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;            if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4078        //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;          }
4079        // - - - - - - - - - - - - -          if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4080        // irunentry = position of current entry, relative to the run               ( !isSync &&
4081        // 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)
4082        // - - - - - - - - - - - - -                   && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
4083                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4084        // -----------------------------------------            if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4085        // store dead and live-time of previous run              if ( !isSync ){
4086        // -----------------------------------------                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4087        //            if(SELLI==0){                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4088        if (SELLI != 2) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4089          if (fromfirst) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4090            if (oldrun == irun) {                prevshift += (totrunentrymin-iev); // add the new shift to total shift
4091              /// decrement counters                totrunentrymin -= (totrunentrymin-iev); // shift run position min
4092              if (GetTrigLevel2()) {                totrunentrymax -= (totrunentrymin-iev); // shift run position max
4093                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);
4094                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
4095              }              } else {
4096              totdltime[2]--; //event counter                printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4097              if (DBG) {                cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4098                cout << endl;                cout << "\nFor bug reporting instructions, please see for example:\n";
4099                cout << "n.events     : " << totdltime[2] << endl;                cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4100                cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;                cout << "  " << endl;
               cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;  
4101              }              }
4102            }            }
4103            else {            runfirstentry = totrunentrymin; // first entry of the run in the level2
4104              totdltime[0] = 0;//live-time            
4105              totdltime[1] = 0;//dead-time  
4106              totdltime[2] = 0; //event counter            //
4107              if (DBG)            if ( fUseDBinRunInfo ){
4108                cout << " *** JUMP RUN *** irun " << irun << endl;              if (gltsync)
4109                  delete gltsync; // Emiliano
4110                if (!dbc || (dbc && !dbc->IsConnected()))
4111                  SetDBConnection(); //Emiliano
4112                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4113                TrkParams::Set(GetRunInfo(), dbc);
4114                if (dbc){
4115                  dbc->Close(); // Emiliano
4116                  delete dbc;
4117                  dbc=0;
4118                }          
4119                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4120                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4121                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4122                  cout
4123                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4124                    << endl;
4125                }
4126            }            }
4127            /// add an entry            //
4128            if (run_tree_clone)            if (DBG) printf(" found \n");
4129              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
4130                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
4131            /// reset counters            break;
4132            }
4133          } // loop over run
4134          
4135          // --------------------------------------
4136          // if there was no need to update the run
4137          // ---> exit with FALSE
4138          // --------------------------------------
4139          if (irun == oldrun){
4140            if (DBG) printf(" no new run \n");
4141            //        printf(" no new run \n");//TOGLITOGLI
4142            return (false);
4143          }      
4144          // --------------------------------------
4145          // ... otherwise
4146          // ---> exit with TRUE
4147          // --------------------------------------
4148    
4149          if (SELLI != 2) {
4150            /// decrement counters since this event belongs to a new run
4151            if (GetTrigLevel2()) {
4152              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4153              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4154            }
4155            totdltime[2]--; //event counter
4156            if (DBG) {
4157              cout << endl;
4158              cout << "n.events     : " << totdltime[2] << endl;
4159              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4160              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4161            }
4162            // add an entry
4163            if (run_tree_clone && totdltime[2] > 0)
4164              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4165                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4166            // reset counters
4167            if ( totdltime[2] > 0 ){
4168            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
4169              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4170              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2991  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4172  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4172            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
4173          }          }
4174        }        }
       //            }  
4175    
4176        irun++;        if (DBG){
4177        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4178        // if the end of run tree is reached...               << endl;        
4179        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4180        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4181          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4182            // -----------------------------------------------------          if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4183            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4184            // ---> 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;  
4185        }        }
4186        // -------------------------------------------------------------------        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4187        // 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);
4188        // and read a new run        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4189        // -------------------------------------------------------------------        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4190        if (irun > 0)        //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4191          runfirstentry += (GetRunInfo()->NEVENTS) - prevshift;        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4192        irunentry = 0;  
4193        prevshift = 0;        return (true);
4194        run_tree->GetEntry(irun);//update runinfo      } // need for run upgrade
4195        irunt = irun - irunoffset[run_tree->GetTreeNumber()];      if (DBG) printf("return false\n");
4196        if (gltsync)      return (false);
4197          delete gltsync; // Emiliano    }// SELLI = 0 SELLI = 2
4198        if (!dbc || (dbc && !dbc->IsConnected()))    
         SetDBConnection(); //Emiliano  
       gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano  
       if (dbc){  
         dbc->Close(); // Emiliano  
         dbc=0;  
       };  
       if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano  
         cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun  
             << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;  
         cout  
             << "                                                            (NB!! in this case some events are assigned to a wrong run)"  
             << endl;  
       }  
       //            if(hasfrag &&  fragid != GetRunInfo()->ID){  
       //                cout << "... where is the next fragment ??"<<endl;  
       //            }  
     };  
   
     // --------------------------------------  
     // if there was no need to update the run  
     // ---> exit with FALSE  
     // --------------------------------------  
     if (irun == oldrun)  
       return (false);  
   
     // --------------------------------------  
     // ... otherwise  
     // --------------------------------------  
   
   
     // --------------------------------------  
     // ---> exit with TRUE  
     // --------------------------------------  
     if (DBG)  
       cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun  
           << endl;  
     // ----------------------------------------------------  
     // update the tracker parameters  
     // (non ho trovato nessun altro modo sicuro di farlo...)  
     // ----------------------------------------------------  
     if (!dbc || (dbc && !dbc->IsConnected()))  
       SetDBConnection();  
     TrkParams::Set(GetRunInfo(), dbc);  
     if (dbc){  
       dbc->Close();  
       dbc=0;  
     };  
   
     // ----------------------------------------------------  
     // then check if the run has a fragment  
     // in this case we have to switch to the next fragment  
     // when the end of the first fragment is reached  
     // ----------------------------------------------------  
     if (GetRunInfo()->ID_RUN_FRAG != 0 &&  
     //      GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&  
         DBG && true) {  
       cout << "* fragment *" << endl;  
     }  
   
     return (true);  
   };  
4199    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4200    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
4201    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3165  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4270  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4270        // update the tracker parameters        // update the tracker parameters
4271        // (non ho trovato nessun altro modo sicuro di farlo...)        // (non ho trovato nessun altro modo sicuro di farlo...)
4272        // ----------------------------------------------------        // ----------------------------------------------------
4273        if (!dbc || (dbc && !dbc->IsConnected()))        if ( fUseDBinRunInfo ){
4274          SetDBConnection();          if (!dbc || (dbc && !dbc->IsConnected()))
4275        TrkParams::Set(GetRunInfo(), dbc);            SetDBConnection();
4276        if (dbc){          TrkParams::Set(GetRunInfo(), dbc);
4277          dbc->Close();          if (dbc){
4278          dbc=0;            dbc->Close();
4279        };            delete dbc;
4280              dbc=0;
4281            }
4282          }
4283        //            cout << endl;        //            cout << endl;
4284        prevshift = 0;        prevshift = 0;
4285          yprevshift = 0;
4286        return true;        return true;
4287      }      }
4288      return false;      return false;
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4291  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4291    return false;    return false;
4292    //    //
4293  }  }
4294  ;  
4295    /**
4296     * Update the runinfo informations (to be used to have Run infos event by event basis)
4297     * @param run Pointer to the chain/tree which contains run infos
4298     * @return true if a new run has been read, false if it is still the same run
4299     */
4300    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4301      return (UpdateRunInfo(iev));
4302    }
4303    
4304  /**  /**
4305   * 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)
4306   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
# Line 3191  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 4309  Bool_t PamLevel2::UpdateRunInfo(Long64_t
4309  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4310    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
4311  }  }
4312  ;  
4313    
4314  //--------------------------------------  //--------------------------------------
4315  //  //
# Line 4022  void PamLevel2::CreateCloneTrees(TFile * Line 5140  void PamLevel2::CreateCloneTrees(TFile *
5140      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5141      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5142      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5143        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5144    };    };
5145    
5146    Int_t i = 0;    Int_t i = 0;
# Line 4041  void PamLevel2::CreateCloneTrees(TFile * Line 5160  void PamLevel2::CreateCloneTrees(TFile *
5160        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5161        cout << "Tracker      : branch TrkHough" << endl;        cout << "Tracker      : branch TrkHough" << endl;
5162      };      };
5163        if(NUC){
5164            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5165            cout << "Tracker      : branch TrackNuclei" << endl;    
5166        }
5167        if(EXT){
5168            pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5169            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5170            cout << "Tracker      : branch RecoveredTrack" << endl;
5171            if(NUC){
5172                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5173                cout << "Tracker      : branch RecoveredTrackNuclei" << endl;                  
5174            }
5175        }
5176    
5177      i++;      i++;
5178    }    }
5179    
# Line 4055  void PamLevel2::CreateCloneTrees(TFile * Line 5188  void PamLevel2::CreateCloneTrees(TFile *
5188        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5189        cout << "Calorimeter  : branch CaloLevel2" << endl;        cout << "Calorimeter  : branch CaloLevel2" << endl;
5190      };      };
5191        if(NUC){
5192            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5193            cout << "Calorimeter  : branch TrackNuclei" << endl;    
5194        }
5195        if(EXT){
5196            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5197            cout << "Calorimeter  : branch RecoveredTrack" << endl;
5198            if(NUC){
5199                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5200                cout << "Calorimeter  : branch RecoveredTrackNuclei" << endl;                  
5201            }
5202        }
5203      i++;      i++;
5204    }    }
5205    
# Line 4063  void PamLevel2::CreateCloneTrees(TFile * Line 5208  void PamLevel2::CreateCloneTrees(TFile *
5208      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5209      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5210      cout << "ToF          : branch ToFLevel2" << endl;      cout << "ToF          : branch ToFLevel2" << endl;
5211        if(NUC){
5212            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5213            cout << "ToF          : branch TrackNuclei" << endl;    
5214        }
5215        if(EXT){
5216            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5217            cout << "ToF          : branch RecoveredTrack" << endl;
5218            if(NUC){
5219                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5220                cout << "ToF          : branch RecoveredTrackNuclei" << endl;                  
5221            }
5222        }
5223      i++;      i++;
5224    };    };
5225    // Trigger    // Trigger
# Line 4098  void PamLevel2::CreateCloneTrees(TFile * Line 5255  void PamLevel2::CreateCloneTrees(TFile *
5255      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
5256      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5257      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
5258        if(NUC){
5259            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5260            cout << "OrbitalInfo  : branch TrackNuclei" << endl;    
5261        }
5262        if(EXT){
5263            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5264            cout << "OrbitalInfo  : branch RecoveredTrack" << endl;
5265            if(NUC){
5266                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5267                cout << "OrbitalInfo  : branch RecoveredTrackNuclei" << endl;                  
5268            }
5269        }
5270      i++;      i++;
5271    };    };
5272    // GPamela    // GPamela
5273    if (GP) {    if (GP) {
5274      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5275      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5276      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
5277      i++;      i++;
5278    };    };
5279    
5280    
5281    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5282    
5283  }  }
# Line 4177  void PamLevel2::WriteCloneTrees() { Line 5348  void PamLevel2::WriteCloneTrees() {
5348  /**  /**
5349   * 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.
5350   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
5351  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
5352    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
5353    if (!pam_tree) {    if (!pam_tree) {
5354      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;
5355      return 0;      return 0;
# Line 4196  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5363  Int_t PamLevel2::GetEntry(Long64_t iee)
5363    //    return 0;    //    return 0;
5364    //    }    //    }
5365    
   Long64_t ii = 0;  
5366    //-------------------------------    //-------------------------------
5367    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
5368      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;
5369      return 0;      return 0;
5370    }    }
# Line 4219  Int_t PamLevel2::GetEntry(Long64_t iee) Line 5384  Int_t PamLevel2::GetEntry(Long64_t iee)
5384    }    }
5385    
5386    //-------------------------------    //-------------------------------
5387    ii = iee;    //
5388    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5389    //    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;  
   //    }  
   //     }  
   
5390    
5391    return 1;    return 1;
5392    
# Line 4280  TTree* PamLevel2::GetYodaTree() { Line 5427  TTree* PamLevel2::GetYodaTree() {
5427    //===================================    //===================================
5428    if (irun < 0) {    if (irun < 0) {
5429      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5430      //  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;
5431      return NULL;      return NULL;
5432    }    }
5433    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5434    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
5435    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
5436        cout << "irootnew "<<irootnew<<endl;
5437      }
5438    
5439    //===================================    //===================================
5440    // load the level0 file    // load the level0 file
# Line 4323  TTree* PamLevel2::GetYodaTree() { Line 5472  TTree* PamLevel2::GetYodaTree() {
5472      if (!h0_obj)      if (!h0_obj)
5473        h0_obj = new EventHeader();        h0_obj = new EventHeader();
5474      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
5475      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5476      //---------------------------------------------------      //---------------------------------------------------
5477      // TRACKER:      // TRACKER:
5478      if (TRK0) {      if (TRK0) {
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 5490  TTree* PamLevel2::GetYodaTree() {
5490          calo0_obj->Set();          calo0_obj->Set();
5491        };        };
5492        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
5493      }      }
5494      //---------------------------------------------------      //---------------------------------------------------
5495      // TOF:      // TOF:
# Line 4350  TTree* PamLevel2::GetYodaTree() { Line 5498  TTree* PamLevel2::GetYodaTree() {
5498      }      }
5499    
5500      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5501        delete dbc;
5502      dbc=0;      dbc=0;
5503    
5504    };    };
5505    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
5506    if (TRK0) {    if (TRK0) {
5507      //  TrkParams::Load(6);      if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
     //  if( !TrkParams::IsLoaded(6) ){  
     //      cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;  
     //  };  
     if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection(); // PERCHE` ERA COMMENTATA QUESTA RIGA?  
5508      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
5509      TrkParams::LoadCalib();      TrkParams::LoadCalib();
5510      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
# Line 4371  TTree* PamLevel2::GetYodaTree() { Line 5512  TTree* PamLevel2::GetYodaTree() {
5512      };      };
5513      if(dbc){      if(dbc){
5514        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5515          delete dbc;
5516        dbc=0;        dbc=0;
5517      };      };
5518    }    }
   
   //    cout << l0_tree << endl;  
5519    return l0_tree;    return l0_tree;
   
5520  }  }
5521    
5522  /**  /**
# Line 4385  TTree* PamLevel2::GetYodaTree() { Line 5524  TTree* PamLevel2::GetYodaTree() {
5524   */   */
5525  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
5526    
5527    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
5528    
5529    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5530    if (irunentry < 0) {    // if it is a full file (not preselected)
5531      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5532      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;  
5533    
5534    ULong64_t obt = 0;      if (!GetYodaTree()){
5535    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5536    if (GetOrbitalInfo()) {        return 0;
5537      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
5538    
5539    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
5540      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5541      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5542    }        irunentry = 0LL;
5543    if (obt == 0 && pktn == 0 && !ISGP) {      }
5544      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
5545      return 0;      //  if file is NOT a preselected file
5546    }      //  ---------------------------------
5547        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5548    
5549        if (DBG){
5550          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5551          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5552          cout << " time "<< abstime << endl;
5553        }
5554    
5555        ULong64_t obt = 0;
5556        ULong64_t pktn = 0;
5557        if (GetOrbitalInfo()) {
5558          obt = GetOrbitalInfo()->OBT;
5559          pktn = GetOrbitalInfo()->pkt_num;
5560        }
5561    
5562    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
5563    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5564    // 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;  
5565      }      }
5566      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
5567      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
5568        return 0;        return 0;
5569      }      }
5570    
5571        // ---------------------------------------------------------------------
5572        // ATTENTION!!!
5573        // If data are not pre-processed with cleaner, the level0 tree may contain
5574        // spurious nested physics packets.
5575        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
5576        // while level2 tree DOES NOT!!
5577        // This means that "quellagiusta" in these cases is not correct.
5578        // In order to retrieve the correct level0 event, I implemented a check
5579        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
5580        // is shift forward until when the packets match.
5581        // ---------------------------------------------------------------------
5582        Long64_t shift = 0LL;
5583        Long64_t answer = quellagiusta + shift + yprevshift;
5584        Int_t readl0 = 0;
5585        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5586    
5587        if (DBG){
5588          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5589        }
5590    
5591    
5592      if (ISGP) {      if (ISGP) {
5593        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5594        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5595      }      }
5596    
5597      //  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 ){
5598      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
5599      //  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");
5600      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5601      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
5602      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5603      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
5604      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
5605      //        if (shift > 0) {
5606      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5607        prevshift = 0;          if (DBG)
5608        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5609      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5610            if (DBG)
5611              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5612                   << " prevshift " << prevshift << " )" << endl;
5613          }
5614          answer = quellagiusta +  shift+ yprevshift;
5615          readl0 = l0_tree->GetEntry(answer);
5616          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5617    
5618    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        if (!GetEventHeader()) {
5619        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5620        < GetYodaTree()->GetEntries() && shift < maxshift);          return 0;
5621          }
5622    
5623    if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {        //
5624      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5625      return 0;          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5626    }          //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5627    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          yprevshift = 0LL;
5628    //    return GetYodaTree()->GetEntry(quellagiusta);          shift = -1LL;
5629    if (shift > 1)        };
5630      prevshift += (shift - 1);      
5631          shift++;
5632        }
5633                                      
5634    
5635    return answer;      if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5636          if ( isSync ){
5637            printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5638            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5639            cout << "\nFor bug reporting instructions, please see for example:\n";
5640            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5641            cout << "  " << endl;
5642          }
5643          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5644          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
5645          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5646          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5647          // 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
5648          // connect to db
5649          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5650          //
5651          if (GetOrbitalInfo()){
5652            abstime = GetOrbitalInfo()->absTime;
5653          } else {
5654            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5655            return 0;
5656          }
5657          // query DB looking for runs containing the processed event
5658          TSQLResult *pResult;
5659          TSQLRow *Row = NULL;
5660          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5661          if ( DBG ) printf(" query is %s \n",myquery.Data());
5662          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5663          pResult = dbc->Query(myquery.Data());    
5664          if (!pResult->GetRowCount()){
5665            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5666            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5667            cout << "\nFor bug reporting instructions, please see for example:\n";
5668            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5669            cout << "  " << endl;
5670            return 0;
5671          }
5672          if ( pResult->GetRowCount() == 1 ){
5673            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5674            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5675          }
5676          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5677            if (Row) delete Row;
5678            Row = pResult->Next();  
5679            if( Row == NULL ) break;
5680            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5681            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5682            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5683            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5684    
5685            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5686            Bool_t rfound = false;
5687            totrunentry = 0LL;
5688            runfirstentry = 0LL;
5689            for (Int_t r=0; r< run_tree->GetEntries();r++){
5690              run_tree->GetEntry(r);//update runinfo
5691              if ( r > 0 ){
5692                totrunentrymin = totrunentrymax+1;
5693              } else {
5694                totrunentrymin = 0LL;
5695              }
5696              totrunentry += GetRunInfo()->NEVENTS;
5697              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5698              irun = r;        
5699    
5700              if (idrun == GetRunInfo()->ID){
5701                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5702                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5703                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5704                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5705                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5706                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
5707                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
5708                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
5709                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5710                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5711                }
5712                runfirstentry = totrunentrymin; // first entry of the run in the level2
5713              
5714    
5715                //
5716                if (gltsync)
5717                  delete gltsync; // Emiliano
5718                if (!dbc || (dbc && !dbc->IsConnected()))
5719                  SetDBConnection(); //Emiliano
5720                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
5721                if (dbc){
5722                  dbc->Close(); // Emiliano
5723                  delete dbc;
5724                  dbc=0;
5725                }          
5726                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
5727                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
5728                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
5729                  cout
5730                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
5731                    << endl;
5732                }
5733                //
5734                if (DBG) printf(" found \n");
5735                //            printf(" found \n");//TOGLITOGLI
5736                rfound = true;
5737                //
5738                break;
5739              }
5740            } // loop over run
5741            if ( !rfound ){
5742              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
5743              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5744              cout << "\nFor bug reporting instructions, please see for example:\n";
5745              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5746              cout << "  " << endl;        
5747              return 0;
5748            }
5749    
5750            // here we got the first run and we can check if it contains the level0 event
5751            if (!GetYodaTree()){
5752              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5753              return 0;
5754            }      
5755    
5756            // get the current run entry
5757            irunentry = iev - runfirstentry;
5758            if (irunentry < 0) {
5759              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5760              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5761              irunentry = 0LL;
5762            }
5763            //  ---------------------------------
5764            //  if file is NOT a preselected file
5765            //  ---------------------------------
5766            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5767          
5768            if (DBG){
5769              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5770              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5771              cout << " time "<< abstime << endl;
5772            }
5773            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5774            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5775            //        cout << " time "<< abstime << endl; // TOGLITOGLI
5776          
5777            shift = 0;
5778            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
5779            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5780    
5781            if (DBG){
5782              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5783            }
5784            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5785          
5786            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5787              if (shift > 0) {
5788                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5789                if (DBG)
5790                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5791                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5792                if (DBG)
5793                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5794                       << " prevshift " << prevshift << " )" << endl;
5795              }
5796              answer = quellagiusta +  shift+ yprevshift;
5797              readl0 = l0_tree->GetEntry(answer);
5798              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5799            
5800              if (!GetEventHeader()) {
5801                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5802                return 0;
5803              }
5804              //
5805              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5806                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5807                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5808                yprevshift = 0;
5809                shift = -1;
5810              };
5811            
5812              shift++;
5813            }
5814          
5815            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5816              //still not the good run... continue with the nex one!
5817              printf("still not the good run... continue with the nex one!\n");
5818            } else {
5819              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
5820              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
5821              if (shift > 1) yprevshift = (shift - 1);
5822              if (Row) delete Row;
5823              delete pResult;  
5824              if (dbc){
5825                dbc->Close(); // Emiliano
5826                delete dbc;
5827                dbc=0;
5828              }    
5829              il0entry = answer;
5830              return readl0;
5831            }
5832            // perhaps it is all
5833          }// loop over runs containing the event
5834          if (Row) delete Row;
5835          delete pResult;  
5836          if (dbc){
5837            dbc->Close(); // Emiliano
5838            delete dbc;
5839            dbc=0;
5840          }          
5841          // arriving here it means no run found, cannot be! error!
5842          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
5843          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5844          cout << "\nFor bug reporting instructions, please see for example:\n";
5845          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5846          cout << "  " << endl;        
5847          return 0;
5848        } else {
5849          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5850          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5851          //    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()) );
5852          if (shift > 1) yprevshift = (shift - 1);
5853          il0entry = answer;
5854          return readl0;
5855        }
5856    
5857        /*  } // if selli 0 || 2
5858      if ( SELLI == 1 && hasL0EE ){
5859        sel_tree->GetEntry(iev);  
5860        Long64_t answer = il0entry;
5861        Int_t readl0 = 0;
5862        readl0 = l0_tree->GetEntry(answer);
5863        return readl0;
5864        }*/
5865      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
5866      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5867      cout << "\nFor bug reporting instructions, please see for example:\n";
5868      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5869      cout << "  " << endl;
5870      return 0;
5871  }  }
5872    
5873  /**  /**
5874   * \Brief Set DB connection   * \Brief Set DB connection
5875   */   */
# Line 4582  Long64_t PamLevel2::GetReadEntry() { Line 5962  Long64_t PamLevel2::GetReadEntry() {
5962  void PamLevel2::SetSortingMethod(TString how) {  void PamLevel2::SetSortingMethod(TString how) {
5963    if (howtosort != how) {    if (howtosort != how) {
5964      issorted = false;      issorted = false;
5965        issorted_new = false;
5966    }    }
5967    howtosort = how;    howtosort = how;
5968  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.23