--- PamelaLevel2/src/PamLevel2.cpp 2009/04/01 09:25:04 1.81 +++ PamelaLevel2/src/PamLevel2.cpp 2014/10/15 08:51:51 1.95 @@ -30,7 +30,7 @@ void GPamela::Delete() { Clear(); - + /* EM all these are in the stack not in the heap, so no need to delete by hand... delete[] Ipltof; delete[] Ipaddle; delete[] Ipartof; @@ -131,9 +131,9 @@ delete[] Timecard; delete[] Pathcard; delete[] P0card; - + */ } -; + void GPamela::SetBranchAddress(TChain* fhBookTree) { @@ -278,6 +278,7 @@ */ PamTrack::PamTrack() { trk_track = 0; + trk_ext_track = 0; calo_track = 0; tof_track = 0; orb_track = 0; @@ -295,6 +296,7 @@ */ PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) { + trk_ext_track = 0; trk_track = 0; calo_track = 0; tof_track = 0; @@ -302,16 +304,70 @@ // if(t)trk_track = new TrkTrack(*t); // if(c)calo_track = new CaloTrkVar(*c); // if(o)tof_track = new ToFTrkVar(*o); - if (t) - trk_track = t; +// if (t) +// trk_track = t; +// if (c) +// calo_track = c; +// if (o) +// tof_track = o; +// if (r) +// orb_track = r; + +// candeleteobj = 0; + + + if (t){ + trk_track = new TrkTrack(*t); + trk_ext_track = new ExtTrack(*t);//NB!! ha dimensione 6 invece che 8 + } if (c) - calo_track = c; + calo_track = new CaloTrkVar(*c); if (o) - tof_track = o; + tof_track = new ToFTrkVar(*o); if (r) - orb_track = r; + orb_track = new OrbitalInfoTrkVar(*r); + candeleteobj = 1; - candeleteobj = 0; +} +; +/** + * Constructor + */ +PamTrack::PamTrack(ExtTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) { + + trk_ext_track = 0; + trk_track = 0; + calo_track = 0; + tof_track = 0; + orb_track = 0; + // if(t)trk_track = new TrkTrack(*t); + // if(c)calo_track = new CaloTrkVar(*c); + // if(o)tof_track = new ToFTrkVar(*o); +// if (t) +// trk_track = t; +// if (c) +// calo_track = c; +// if (o) +// tof_track = o; +// if (r) +// orb_track = r; + +// candeleteobj = 0; + + + if (t){ +//// trk_track = new TrkTrack(*t);//in this case TrkTrack object remains null + trk_ext_track = new ExtTrack(*t); + } + if (c) + calo_track = new CaloTrkVar(*c); + if (o) + tof_track = new ToFTrkVar(*o); + if (r) + orb_track = new OrbitalInfoTrkVar(*r); + candeleteobj = 1; + pscore = 0; + iscore = 0; } ; @@ -335,12 +391,17 @@ if (r) orb_track = new OrbitalInfoTrkVar(*r); candeleteobj = 1; + pscore = 0; + iscore = 0; } void PamTrack::Clear() { // cout << "PamTrack::Clear() "<ExtTrack::Clear(); if (trk_track) trk_track->TrkTrack::Clear(); if (calo_track) @@ -351,6 +412,7 @@ orb_track->OrbitalInfoTrkVar::Clear();//??? } else { + trk_ext_track = 0; trk_track = 0; calo_track = 0; tof_track = 0; @@ -363,6 +425,10 @@ void PamTrack::Delete() { // cout << "PamTrack::Delete() "<ExtTrack::Clear(); + delete trk_ext_track; + } if (trk_track) { trk_track->TrkTrack::Clear(); delete trk_track; @@ -384,6 +450,10 @@ Clear(); } } + + + + //-------------------------------------- // // @@ -394,7 +464,7 @@ PamLevel2::PamLevel2() { Initialize(); } -; + /** * Constructor @@ -417,15 +487,17 @@ GetPamTree(listf, detlist); if (listf) GetRunTree(listf); + SetMaxShift(-1); } -; + PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) { Initialize(); GetPamTree(llist, detlist); GetRunTree(llist); + SetMaxShift(-1); } -; + /** * Constructor * @param ddir Name of directory where level2 files are stored. @@ -435,11 +507,14 @@ */ PamLevel2::PamLevel2(TString ddir, TString llist) { Initialize(); - TList* listf = GetListOfLevel2Files(ddir, llist); + TList* listf = GetListOfLevel2Files(ddir, llist); + cout << "GetPamTree: "<Delete(); + if(trk_ext_obj)trk_ext_obj->Delete(); + if(trk_ext_nuc_obj)trk_ext_nuc_obj->Delete(); + + if(calo_nuc_obj)calo_nuc_obj->Delete(); + if(calo_ext_obj)calo_ext_obj->Delete(); + if(calo_ext_nuc_obj)calo_ext_nuc_obj->Delete(); + + if(tof_nuc_obj)tof_nuc_obj->Delete(); + if(tof_ext_obj)tof_ext_obj->Delete(); + if(tof_ext_nuc_obj)tof_ext_nuc_obj->Delete(); + + if(orb_nuc_obj)orb_nuc_obj->Delete(); + if(orb_ext_obj)orb_ext_obj->Delete(); + if(orb_ext_nuc_obj)orb_ext_nuc_obj->Delete(); + + + if(trk2_nuc_obj)trk2_nuc_obj->Delete();; + if( calo2_nuc_obj)calo2_nuc_obj->Delete();; + if(tof2_nuc_obj)tof2_nuc_obj->Delete();; + if(orb2_nuc_obj)orb2_nuc_obj->Delete();; + + + if (tsorted) { tsorted->Delete(); delete tsorted; @@ -597,10 +740,29 @@ timage->Delete(); delete timage; } + if (text) { + text->Delete(); + delete text; + } + if (tsorted_nuc) { + tsorted_nuc->Delete(); + delete tsorted_nuc; + } + if (timage_nuc) { + timage_nuc->Delete(); + delete timage_nuc; + } + if (text_nuc) { + text_nuc->Delete(); + delete text_nuc; + } + + if (dbc) { dbc->Close(); delete dbc; + dbc=0; } if (gltsync) @@ -632,7 +794,7 @@ // TIter next2(contents); TChain *questa = 0; - while (questa = (TChain*) next2()) { + while ( (questa = (TChain*)next2()) ) { TString name = questa->GetName(); questa->Delete(); questa = NULL; @@ -645,17 +807,27 @@ run_tree->Delete();; if (sel_tree) sel_tree->Delete();; - for (Int_t i = 0; i < NCLONES; i++) + + // The following lines are commented out since they may generate a double delete error + // if the file containing the clone trees is closed. This is because the file owns the + // clone trees which are written into it, so it will delete them when it is closed; if + // also PamLevel2 will try to delete these trees, a double delete error will be generated + // when exiting from analysis program. (Nicola 28/11/2011) + + /*for (Int_t i = 0; i < NCLONES; i++) if (pam_tree_clone[i]) pam_tree_clone[i]->Delete();; if (run_tree_clone) run_tree_clone->Delete();; if (sel_tree_clone) - sel_tree_clone->Delete();; + sel_tree_clone->Delete();;*/ if (irunoffset) delete[] irunoffset; + + Initialize(); + } ; @@ -689,8 +861,8 @@ calo1_obj->Clear(); if (calo2_obj) calo2_obj->Clear(); - if (tof_obj) - tof_obj->Clear(); + if (tof2_obj) + tof2_obj->Clear(); if (trig_obj) trig_obj->Clear(); if (s4_obj) @@ -699,20 +871,42 @@ nd_obj->Clear(); if (ac_obj) ac_obj->Clear(); - if (orb_obj) - orb_obj->Clear(); + if (orb2_obj) + orb2_obj->Clear(); if (gp_obj) gp_obj->Clear(); // if(sorted_tracks)sorted_tracks->Clear(); // sorted_tracks.Clear(); - if (tsorted) { - tsorted->Delete(); - } - if (timage) { - timage->Delete(); - } + if(trk_nuc_obj)trk_nuc_obj->Clear(); + if(trk_ext_obj)trk_ext_obj->Clear(); + if(trk_ext_nuc_obj)trk_ext_nuc_obj->Clear(); + + if(calo_nuc_obj)calo_nuc_obj->Clear(); + if(calo_ext_obj)calo_ext_obj->Clear(); + if(calo_ext_nuc_obj)calo_ext_nuc_obj->Clear(); + + if(tof_nuc_obj)tof_nuc_obj->Clear(); + if(tof_ext_obj)tof_ext_obj->Clear(); + if(tof_ext_nuc_obj)tof_ext_nuc_obj->Clear(); + + if(orb_nuc_obj)orb_nuc_obj->Clear(); + if(orb_ext_obj)orb_ext_obj->Clear(); + if(orb_ext_nuc_obj)orb_ext_nuc_obj->Clear(); + + if(trk2_nuc_obj)trk2_nuc_obj->Clear();; + if( calo2_nuc_obj)calo2_nuc_obj->Clear();; + if(tof2_nuc_obj)tof2_nuc_obj->Clear();; + if(orb2_nuc_obj)orb2_nuc_obj->Clear();; + + if (tsorted)tsorted->Delete(); + if (timage)timage->Delete(); + if (text) text->Delete(); + + if (tsorted_nuc)tsorted_nuc->Delete(); + if (timage_nuc)timage_nuc->Delete(); + if (text_nuc) text_nuc->Delete(); } ; @@ -746,7 +940,7 @@ // TIter next2(contents); TChain *questa = 0; - while (questa = (TChain*) next2()) { + while ( (questa = (TChain*) next2()) ) { TString name = questa->GetName(); questa->Delete(); questa = NULL; @@ -777,13 +971,39 @@ trkh_obj = 0; calo1_obj = 0; calo2_obj = 0; - tof_obj = 0; + tof2_obj = 0; trig_obj = 0; s4_obj = 0; nd_obj = 0; ac_obj = 0; - orb_obj = 0; + orb2_obj = 0; gp_obj = 0; + + trk_ext_obj = 0; + trk_ext_nuc_obj = 0; + trk_nuc_obj = 0; + + calo_ext_obj = 0; + calo_ext_nuc_obj = 0; + calo_nuc_obj = 0; + + tof_ext_obj = 0; + tof_ext_nuc_obj = 0; + tof_nuc_obj = 0; + + orb_ext_obj = 0; + orb_ext_nuc_obj = 0; + orb_nuc_obj = 0; + + trk2_nuc_obj = 0; + calo2_nuc_obj = 0; + tof2_nuc_obj = 0; + orb2_nuc_obj = 0; + + trk2_nuc_obj = 0; + calo2_nuc_obj = 0; + tof2_nuc_obj = 0; + orb2_nuc_obj = 0; // // Reset run pointers // @@ -791,8 +1011,16 @@ soft_obj = 0;// Emiliano irun = -1; irunt = -1; + totrunentry = 0LL; + totrunentrymax = 0LL; + totrunentrymin = 0LL; runfirstentry = 0ULL; runlastentry = 0ULL; + prevabstime = 0ULL; + prevpktnum = 0; + abstime = 0ULL; + pktnum = 0; + isFragment = false; // totdltime[0] = 0LL; totdltime[1] = 0LL; @@ -810,7 +1038,7 @@ if (strict) { if (trk2_obj && trk2_obj->UnpackError() != 0) goodev = false; - if (tof_obj && tof_obj->unpackError != 0) + if (tof2_obj && tof2_obj->unpackError != 0) goodev = false; if (trig_obj && trig_obj->unpackError != 0) goodev = false; @@ -820,7 +1048,7 @@ goodev = false; if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1))) goodev = false; - // if(orb_obj) + // if(orb2_obj) } else { if (nd_obj && nd_obj->unpackError != 0) @@ -832,6 +1060,38 @@ } ; +void PamLevel2::SkipRunInfoUpdate(){ + printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n"); + fUpdateRunInfo = false; + this->SetSELLI(2); + printf(" ===============> W A R N I N G <================ \n"); + printf(" in case PamLevel2::CreateCloneTrees() will be called \n"); + printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n"); + if ( run_tree_clone ){ + printf(" ===============> W A R N I N G <================ \n"); + printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n"); + printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n"); + }; +} + +void PamLevel2::SetMaxShift(Int_t sh){ + if ( sh >= 0 ){ + 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"); + maxshift = sh; + } else { + ULong64_t nev = GetEntries(); + ULong64_t runnev = 0ULL; + for (Int_t r=0; r< run_tree->GetEntries();r++){ + run_tree->GetEntry(r);//update runinfo + runnev += GetRunInfo()->NEVENTS; + } + maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative + if ( (runnev-nev) == 0 ) isSync = true; + 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); + // 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 + } +} + //-------------------------------------- // // @@ -874,11 +1134,11 @@ return &calo2_obj; }; if (!objname.CompareTo("ToFLevel2")) { - if (!tof_obj) { - tof_obj = new ToFLevel2(); - tof_obj->Set(); + if (!tof2_obj) { + tof2_obj = new ToFLevel2(); + tof2_obj->Set(); } - return &tof_obj; + return &tof2_obj; }; if (!objname.CompareTo("TrigLevel2")) { if (!trig_obj) @@ -901,15 +1161,15 @@ return &ac_obj; }; if (!objname.CompareTo("OrbitalInfo")) { - if (!orb_obj) { - orb_obj = new OrbitalInfo(); - orb_obj->Set(); + if (!orb2_obj) { + orb2_obj = new OrbitalInfo(); + orb2_obj->Set(); } - return &orb_obj; + return &orb2_obj; }; // if(!objname.CompareTo("OrbitalInfo")){ - // if(!orb_obj) orb_obj = new OrbitalInfo(); - // return &orb_obj; + // if(!orb2_obj) orb2_obj = new OrbitalInfo(); + // return &orb2_obj; // }; if (!objname.CompareTo("GPamela")) { if (!gp_obj) @@ -973,10 +1233,10 @@ */ ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) { - if (!tof_obj) + if (!tof2_obj) return 0; - if (tof_obj->ToFLevel2::ntrk() == 0) { + if (tof2_obj->ToFLevel2::ntrk() == 0) { cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored" << endl; return NULL; @@ -986,9 +1246,9 @@ Int_t it_tof = 0; do { - c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof); + c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof); it_tof++; - } while (c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk()); + } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk()); if (!c || seqno != c->trkseqno) { c = 0; @@ -1011,17 +1271,17 @@ */ OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) { - if (!orb_obj) + if (!orb2_obj) return 0; - if (orb_obj->OrbitalInfo::ntrk() == 0) { + if (orb2_obj->OrbitalInfo::ntrk() == 0) { // // TRICK BEGIN // OrbitalInfoTrkVar *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK // Int_t nn = 0; - // TClonesArray &tor = *orb_obj->OrbitalInfoTrk; - // for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){ + // TClonesArray &tor = *orb2_obj->OrbitalInfoTrk; + // for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){ // // - // ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt); + // ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt); // if ( ptt->trkseqno != -1 ){ // // // r->trkseqno = ptt->trkseqno; @@ -1043,7 +1303,7 @@ // }; // delete r; // OrbitalInfoTrkVar *c = 0; - // c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0); + // c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0); // return c; // //TRICK END cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno @@ -1055,9 +1315,9 @@ Int_t it_tof = 0; do { - c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof); + c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof); it_tof++; - } while (c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk()); + } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk()); if (!c || seqno != c->trkseqno) { c = 0; @@ -1196,6 +1456,8 @@ timage->Delete(); TClonesArray &ttimage = *timage; + + //-------------------------------------------------- // retrieve sorting method //-------------------------------------------------- @@ -1311,7 +1573,7 @@ // ----------------------------------------------------------------------------------------- // check the number of hit pmts along the track // on S12 S21 and S32, where paddles are parallel to Y axis - if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof_obj) { + if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) { cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!"; return; }; @@ -1328,12 +1590,12 @@ // Float_t sen = 0.; for (Int_t ih = 0; ih < op->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih)); if (pl == 2 || pl == 3 || pl == 4 || pl == 5) sen += (op->dedx).At(ih); }; for (Int_t ih = 0; ih < oi->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih)); if (pl == 2 || pl == 3 || pl == 4 || pl == 5) sen += (oi->dedx).At(ih); }; @@ -1346,7 +1608,7 @@ Int_t nz = 6; Float_t zin[6]; // << define TOF z-coordinates for (Int_t ip = 0; ip < nz; ip++) - 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 Trajectory *tr = new Trajectory(nz, zin); // Int_t nphit_p = 0; @@ -1355,7 +1617,7 @@ Float_t enhit_i = 0.; // for (Int_t ih = 0; ih < op->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih)); if (pl == 1 || pl == 2 || pl == 5) { nphit_p++; enhit_p += (op->dedx).At(ih); @@ -1366,7 +1628,7 @@ // if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) { for (Int_t ih = 0; ih < op->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih)); if (pl == 0) { nphit_p++; enhit_p += (op->dedx).At(ih); @@ -1375,7 +1637,7 @@ }; if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) { for (Int_t ih = 0; ih < op->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih)); if (pl == 3) { nphit_p++; enhit_p += (op->dedx).At(ih); @@ -1384,7 +1646,7 @@ }; if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) { for (Int_t ih = 0; ih < op->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih)); if (pl == 4) { nphit_p++; enhit_p += (op->dedx).At(ih); @@ -1393,7 +1655,7 @@ }; for (Int_t ih = 0; ih < oi->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih)); if (pl == 1 || pl == 2 || pl == 5) { nphit_i++; enhit_i += (op->dedx).At(ih); @@ -1404,7 +1666,7 @@ // if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) { for (Int_t ih = 0; ih < oi->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih)); if (pl == 0) { nphit_i++; enhit_i += (op->dedx).At(ih); @@ -1413,7 +1675,7 @@ }; if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) { for (Int_t ih = 0; ih < oi->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih)); if (pl == 3) { nphit_i++; enhit_i += (op->dedx).At(ih); @@ -1422,7 +1684,7 @@ }; if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) { for (Int_t ih = 0; ih < oi->npmtadc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih)); if (pl == 4) { nphit_i++; enhit_i += (op->dedx).At(ih); @@ -1466,18 +1728,18 @@ cout << "image: npmttdc "<< oi->npmttdc << endl;*/ // for (Int_t ih=0; ih < op->npmtadc; ih++){ - // Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) ); + // Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) ); // if(pl == 1 || pl == 2 || pl == 5)nphit_p++; // }; // for (Int_t ih=0; ih < oi->npmtadc; ih++){ - // Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) ); + // Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) ); // if(pl == 1 || pl == 2 || pl == 5)nphit_i++; // }; // --- modified to count tdc signals (more efficient?) // --- and to implement check on tdcflag for (Int_t ih = 0; ih < op->npmttdc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((op->pmttdc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih)); // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++; if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3)) || (use_S3 && (pl == 4 || pl == 5))) { @@ -1487,7 +1749,7 @@ }; for (Int_t ih = 0; ih < oi->npmttdc; ih++) { - Int_t pl = tof_obj->GetPlaneIndex((oi->pmttdc).At(ih)); + Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih)); // if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++; if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3)) || (use_S3 && (pl == 4 || pl == 5))) { @@ -1662,6 +1924,655 @@ } ; +// +//-------------------------------------- +/** + * Sort physical (tracker) tracks. + * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...). + * Sorting cryteria: + * TRK: lower chi**2 + * CAL: lower Y spatial residual on the first calorimeter plane + * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis). + * S1: (ask Emiliano) + * S2: (ask Emiliano) + * S3: (ask Emiliano) + * GP: more GP hits + * The default sorting cryterium is "TOF+CAL". + * + * 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). + * + * New version, with handling of extended tracks and nuclei tracks. + */ +void PamLevel2::SortTracksNew() { + + //-------------------------------------------------- + //Check if the current event has already been sorted + //-------------------------------------------------- + if (issorted_new && lastsorted_new == GetReadEntry()) { + return; //already done for this event + } + + +// cout << "SORT" << endl; + + //Reset the sort flags, just in case something will go wrong... + issorted_new = false; + lastsorted_new = -1; + + //-------------------------------------------------- + // set input variables + //-------------------------------------------------- + + TString how = howtosort; + //Save current Object count + Int_t ObjectNumber = TProcessID::GetObjectCount(); + + + + + TrkLevel2 * trk2 ; + CaloLevel2 * calo2; + ToFLevel2 * tof2 ; + OrbitalInfo * orb2 ; + + TClonesArray * trkext ; + TClonesArray * caloext; + TClonesArray * tofext ; + TClonesArray * orbext ; + + +// cout << "trk2_obj" << trk2_obj<< endl; +// cout << "trk2_nuc_obj" << trk2_nuc_obj<< endl; +// cout << " trk_ext_obj "<GetNTracks()); + tsorted->Delete(); + // track images from standard alg + if (!timage) + timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks()); + timage->Delete(); + // tracks from extended algorythm + if(EXT && !text) + text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries()); + if(text)text->Delete(); + + //----------------------------------------------------------- + // create/reset TCloneArrays to store tracks and their images + //----------------------------------------------------------- + if(NUC){ + // main tracks from standard alg + if (!tsorted_nuc) + tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks()); + tsorted_nuc->Delete(); + // track images from standard alg + if (!timage_nuc) + timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks()); + timage_nuc->Delete(); + // tracks from extended algorythm + if(EXT && !text_nuc) + text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries()); + if(text_nuc)text_nuc->Delete(); + } + //-------------------------------------------------- + // retrieve sorting method + //-------------------------------------------------- + Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase); + Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase); + Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase); + Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase); + Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase); + Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase); + Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase); + + if (use_TOF) { + use_S1 = true; + use_S2 = true; + use_S3 = true; + }; + if (!CAL2 && use_CAL) + use_CAL = false; + if (!TOF) { + use_TOF = false; + use_S1 = false; + use_S2 = false; + use_S3 = false; + } + if (!GP) { + use_GP = false; + } + + if (!TRK2) { + cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl; + return; + }; + + + /////////////////////////////////////////////////////////////////////////////////// + // + // sort tracks and fill PamTrack arrays + // + /////////////////////////////////////////////////////////////////////////////////// + for(int doit=0; doit<2; doit++){ + +// cout << "doit "<TrkLevel2::GetNTracks(); i++) { + + TrkTrack *ts = 0; + CaloTrkVar *cs = 0; + ToFTrkVar *os = 0; + OrbitalInfoTrkVar *rs = 0; + + // get tracker tracks + TrkTrack *tp = trk2->GetTrack(i); //tracker + CaloTrkVar *cp = calo2->GetCaloStoredTrack(tp->GetSeqNo()); + ToFTrkVar *op = tof2->GetToFStoredTrack(tp->GetSeqNo()); + OrbitalInfoTrkVar *rp = orb2->GetOrbitalInfoStoredTrack(tp->GetSeqNo()); + + TrkTrack *ti = 0; //tracker (image) + CaloTrkVar *ci = 0; + ToFTrkVar *oi = 0; + OrbitalInfoTrkVar *ri = 0; + // cout << "trk track n. "<HasImage()) { + + ti = trk2->GetTrackImage(i); //tracker (image) + ci = calo2->GetCaloStoredTrack(ti->GetSeqNo()); + oi = tof2->GetToFStoredTrack(ti->GetSeqNo()); + ri = orb2->GetOrbitalInfoStoredTrack(ti->GetSeqNo()); + + // cout << "its image "<npresh > ci->npresh && true) { + tp_score++; + totp_score++; + }; + if (cp->npresh < ci->npresh && true) { + ti_score++; + toti_score++; + }; + + // cout << "CALO "<npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih)); + if (pl == 2 || pl == 3 || pl == 4 || pl == 5) + sen += (op->dedx).At(ih); + }; + for (Int_t ih = 0; ih < oi->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih)); + if (pl == 2 || pl == 3 || pl == 4 || pl == 5) + sen += (oi->dedx).At(ih); + }; + // + if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!) + //printf(" IS A NUCLEUS! en = %f \n",sen); + // + // is a nucleus use a different algorithm + // + Int_t nz = 6; + Float_t zin[6]; // << define TOF z-coordinates + for (Int_t ip = 0; ip < nz; ip++) + zin[ip] = tof2->GetZTOF(tof2->GetToFPlaneID(ip)); // << read ToF plane z-coordinates + Trajectory *tr = new Trajectory(nz, zin); + // + Int_t nphit_p = 0; + Int_t nphit_i = 0; + Float_t enhit_p = 0.; + Float_t enhit_i = 0.; + // + for (Int_t ih = 0; ih < op->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih)); + if (pl == 1 || pl == 2 || pl == 5) { + nphit_p++; + enhit_p += (op->dedx).At(ih); + }; + }; + // + tp->DoTrack2(tr); + // + if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) { + for (Int_t ih = 0; ih < op->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih)); + if (pl == 0) { + nphit_p++; + enhit_p += (op->dedx).At(ih); + }; + }; + }; + if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) { + for (Int_t ih = 0; ih < op->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih)); + if (pl == 3) { + nphit_p++; + enhit_p += (op->dedx).At(ih); + }; + }; + }; + if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) { + for (Int_t ih = 0; ih < op->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih)); + if (pl == 4) { + nphit_p++; + enhit_p += (op->dedx).At(ih); + }; + }; + }; + + for (Int_t ih = 0; ih < oi->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih)); + if (pl == 1 || pl == 2 || pl == 5) { + nphit_i++; + enhit_i += (op->dedx).At(ih); + }; + }; + // + ti->DoTrack2(tr); + // + if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) { + for (Int_t ih = 0; ih < oi->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih)); + if (pl == 0) { + nphit_i++; + enhit_i += (op->dedx).At(ih); + }; + }; + }; + if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) { + for (Int_t ih = 0; ih < oi->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih)); + if (pl == 3) { + nphit_i++; + enhit_i += (op->dedx).At(ih); + }; + }; + }; + if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) { + for (Int_t ih = 0; ih < oi->npmtadc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih)); + if (pl == 4) { + nphit_i++; + enhit_i += (op->dedx).At(ih); + }; + }; + }; + + if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) { + + // 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); + // printf(" score p %i score i %i \n",tp_score,ti_score); + // if( enhit_p > enhit_i ) tp_score++; + // if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++; + if (nphit_p > nphit_i) + tp_score++; + if (nphit_p < nphit_i) + ti_score++; + if (nphit_p == nphit_i) { + if (enhit_p > enhit_i) + tp_score++; + else + ti_score++; + }; + // printf(" dopo score p %i score i %i \n",tp_score,ti_score); + }; + delete tr; + // + } + else { + // ------------- + // NOT a NUCLEUS + // ------------- + //printf(" NOT a NUCLEUS! en = %f \n",sen); + + Int_t nphit_p = 0; + Int_t nphit_i = 0; + + /* cout << "track: npmtadc "<< op->npmtadc << endl; + cout << "track: npmttdc "<< op->npmttdc << endl; + cout << "image: npmtadc "<< oi->npmtadc << endl; + cout << "image: npmttdc "<< oi->npmttdc << endl;*/ + + // for (Int_t ih=0; ih < op->npmtadc; ih++){ + // Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) ); + // if(pl == 1 || pl == 2 || pl == 5)nphit_p++; + // }; + + // for (Int_t ih=0; ih < oi->npmtadc; ih++){ + // Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) ); + // if(pl == 1 || pl == 2 || pl == 5)nphit_i++; + // }; + // --- modified to count tdc signals (more efficient?) + // --- and to implement check on tdcflag + for (Int_t ih = 0; ih < op->npmttdc; ih++) { + Int_t pl = tof2->GetPlaneIndex((op->pmttdc).At(ih)); + // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++; + if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3)) + || (use_S3 && (pl == 4 || pl == 5))) { + if ((op->tdcflag).At(ih) == 0) + nphit_p++; + }; + }; + + for (Int_t ih = 0; ih < oi->npmttdc; ih++) { + Int_t pl = tof2->GetPlaneIndex((oi->pmttdc).At(ih)); + // if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++; + if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3)) + || (use_S3 && (pl == 4 || pl == 5))) { + if ((oi->tdcflag).At(ih) == 0) + nphit_i++; + }; + }; + + if ((nphit_p + nphit_i) != 0 && true) { + + if (nphit_p != nphit_i) { + totp_score += nphit_p; + toti_score += nphit_i; + tp_score += nphit_p; + ti_score += nphit_i; + }; + // if ( nphit_p > nphit_i) tp_score+=nphit_p; + // else if( nphit_p < nphit_i) ti_score+=nphit_i; + // else ;//niente + }; + }; + // cout << "TOF "<chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ; + // else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ; + + // CHECK 1 : number of points along X + if (tp->GetNX() >= ti->GetNX()) { + tp_score++; + totp_score++; + }; + if (tp->GetNX() <= ti->GetNX()) { + ti_score++; + toti_score++; + }; + // CHECK 2 : number of points along Y + if (tp->GetNY() >= ti->GetNY()) { + tp_score++; + totp_score++; + }; + if (tp->GetNY() <= ti->GetNY()) { + ti_score++; + toti_score++; + }; + + // cout << "TRK "<Nthspe; ih++) { + int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1; + if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true) + ngphits_p++; + if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true) + ngphits_i++; + } + if (ngphits_p > ngphits_i && true) { + tp_score++; + totp_score++; + } + if (ngphits_p < ngphits_i && true) { + ti_score++; + toti_score++; + } + } + + // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* + // the winner is.... + // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* + if (tp_score > ti_score) { + + } + else if (tp_score < ti_score) { + + ts = ti;//its image!! + cs = ci; + os = oi; + rs = ri; + Int_t totis = toti_score; + + ti = tp;//its image!! + ci = cp; + oi = op; + ri = rp; + + tp = ts;//its image!! + cp = cs; + op = os; + rp = rs; + + toti_score = totp_score; + totp_score = totis; + + } + else { + + // cout << "Warning - track image ambiguity not solved" << endl; + + }; + + } + else { + totp_score = 1; + toti_score = 0; + + // ts = tp; + // cs = cp; + // os = op; + }; + + // cout <<" SortTracks() "<Add(ts);//save the track in the sorted array + // sorted_tracks.Add(ts);//save the track in the sorted array + // sorted_tracks.Add(tp);//save the track in the sorted array + // cout << "SortTracks:: sorted_tracks->Add(it) "<SetPScore(totp_score); + ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score); + ((PamTrack*) (ttimage[i]))->SetPScore(totp_score); + ((PamTrack*) (ttimage[i]))->SetIScore(toti_score); + }; + + + + + +// if (tsorted->GetEntries() != trk2->GetNTracks()) { +// cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries() +// << " != trk2->GetNTracks() = " << trk2->GetNTracks() << endl; +// tsorted->Delete(); +// tsorted = 0; +// timage->Delete(); +// timage = 0; +// } + + +// cout << "text "<GetEntries(); it++){ + + new (ttext[it]) PamTrack((ExtTrack*)(*trkext)[it], (CaloTrkVar*)(*caloext)[it], (ToFTrkVar*)(*tofext)[it], (OrbitalInfoTrkVar*)(*orbext)[it]); + + } + } + + + + }; + + + //Restore Object count + //To save space in the table keeping track of all referenced objects + //We reset the object count to what it was at the beginning of the event. + TProcessID::SetObjectCount(ObjectNumber); + + //Everything went fine so the current event can be tagged as sorted + issorted_new = true; + lastsorted_new = GetReadEntry(); + +// cout << " tsorted "<< tsorted << " "<<(tsorted ? tsorted->GetEntries() : 0)<GetEntries() : 0)<GetEntries() : 0)<=0 && it < trk2_obj->TrkLevel2::GetNTracks() && itGetEntries()) - return 0; + return track; - PamTrack *track = 0; 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]; track = (PamTrack*)((*tsorted)[it]); } else { @@ -1745,8 +2626,107 @@ return track; +}; + +//PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); }; + +/** + * Retrieves the it-th Pamela "physical" track. + * It override TrkLevel2::GetTrack(int it). + * @param it Track number, ranging from 0 to GetNTracks(). + * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation. + */ +PamTrack *PamLevel2::GetTrack(int it, char* alg) { + + TString s(alg); + if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrack(it); //old algorythm + + + SortTracksNew(); + // >> fill tsorted, timage and text + + if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing) + + if( s.Contains("NUC")){ + if( + tsorted_nuc && + it < tsorted_nuc->GetEntries() && //enough tracks found + it >= 0 && //valid index + true) return (PamTrack*)((*tsorted_nuc)[it]); //ok return the track + }else{ + if( + tsorted && + it < tsorted->GetEntries() && //enough tracks found + it >= 0 && //valid index + true )return (PamTrack*)((*tsorted)[it]); //ok return the track + } + + } + + + ///////////////////////////////////////////////////////////////////////// + /// if requested get track from extended algorythm output + ///////////////////////////////////////////////////////////////////////// + + if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested + + if(s.Contains("NUC")){ + if( + text_nuc && + it < text_nuc->GetEntries() && //enough tracks found + it >= 0 && //valid index + true) return (PamTrack*)((*text_nuc)[it]); + }else{ + if( + text && + it < text->GetEntries() && //enough tracks found + it >= 0 && //valid index + true) return (PamTrack*)((*text)[it]); + } + + }; + + + + return NULL; + +} +; +TClonesArray *PamLevel2::GetTracks() { + + // *-*-*-*-*-*-*-*-*-*-*-*-* + SortTracks(); + // *-*-*-*-*-*-*-*-*-*-*-*-* + + return tsorted; + } ; +Int_t PamLevel2::GetNTracks(char* alg) { + + +// cout << " trk_nuc_obj->GetEntries() "<GetEntries()<<" trk2_nuc_obj->GetNTracks() "<GetNTracks()<TrkLevel2::GetNTracks(); //standard algorythm + + if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){ + if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok + return trk_ext_obj->GetEntries();//ok + } + if( s.Contains("EXT", TString::kIgnoreCase) && EXT) { + if(s.Contains("NUC", TString::kIgnoreCase) && NUC) + return (trk2_nuc_obj->TrkLevel2::GetNTracks() ? trk2_nuc_obj->TrkLevel2::GetNTracks() : trk_ext_nuc_obj->GetEntries() ); + return (trk2_obj->TrkLevel2::GetNTracks() ? trk2_obj->TrkLevel2::GetNTracks() : trk_ext_obj->GetEntries() ); + } + if(s.Contains("NUC", TString::kIgnoreCase) && NUC ) + return trk2_nuc_obj->TrkLevel2::GetNTracks(); + + return 0; + +} + //-------------------------------------- // @@ -1786,6 +2766,55 @@ return image; } +/** + * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks(). + * @param it Track number, ranging from 0 to GetNTracks(). + * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation. + */ +PamTrack *PamLevel2::GetTrackImage(int it, char* alg) { + + TString s(alg); + if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrack(it); //old algorythm + + + SortTracksNew(); + // >> fill tsorted, timage and text + + if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing) + + if( s.Contains("NUC")){ + if( + tsorted_nuc && + it < tsorted_nuc->GetEntries() && //enough tracks found + it >= 0 && //valid index + true){ + TClonesArray &t = *(tsorted_nuc); + PamTrack *temp = (PamTrack*) t[it]; + if (temp->GetTrkTrack()->HasImage()) { + return (PamTrack*)((*timage_nuc)[it]); //ok return the track + } + + } + }else{ + if( + tsorted && + it < tsorted->GetEntries() && //enough tracks found + it >= 0 && //valid index + true ){ + TClonesArray &t = *(tsorted); + PamTrack *temp = (PamTrack*) t[it]; + if (temp->GetTrkTrack()->HasImage()) { + return (PamTrack*)((*timage)[it]); //ok return the track + } + } + } + + } + + return NULL; + +} +; //-------------------------------------- // @@ -1875,7 +2904,7 @@ if (!Trout) Trout = T; else - Trout->AddFriend(T); + Trout->AddFriend(T); } else { cout << "Tracker : missing tree" << endl; @@ -2112,7 +3141,7 @@ if (file.EndsWith(".root")) { TString filedir; cout << ddir << endl; - if (ddir != "") { + if ( ddir != "" ) { filedir = ddir; // take the input dir } else { @@ -2120,7 +3149,8 @@ filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir }; filedir.Append("/"); - char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file)); + // char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file)); + char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file)); contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list cout << fullpath << endl; delete fullpath; @@ -2287,11 +3317,11 @@ G->Add(name); if (SELLI == 1) L->Add(name); - }; + }; }; - cout << "done chains\n"; - cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl; + cout << "done data-tree chains "<< T->GetNtrees() <<" \n"; + cout << "----------------------------------------------------" << endl; // ------------------------------------------- // make friends @@ -2374,14 +3404,14 @@ // ===================================== // SET BRANCH-ADDRESS AFTER CHAIN+FRIEND // ===================================== - SetBranchAddress(Trout); + if( Trout->GetNtrees() )SetBranchAddress(Trout); // ------------------------------------ // finally handle selection trees... // (it is not friend of pamela tree) // ------------------------------------ - cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl; + cout << "----------------------------------------------------" << endl; // Selection List if (L && SELLI == 1) { @@ -2391,11 +3421,18 @@ cout << "SelectionList: set branch address RunEntry" << endl; L->SetBranchAddress("EventEntry", &irunentry); cout << "SelectionList: set branch address EventEntry" << endl; + /* if ( L->GetBranch("L0EventEntry") ){ + hasL0EE = true; + L->SetBranchAddress("L0EventEntry", &il0entry); + cout << "SelectionList: set branch address L0EventEntry" << endl; + } else { + hasL0EE = false; // backward compatibility with old preselected files... + }*/ sel_tree = L; // if(!Trout)Trout=O; // else Trout->AddFriend("SelectionList"); cout << "+SelectionList" << endl; - cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl; + cout << "----------------------------------------------------" << endl; } else { // cout << "SelectionList : missing tree"<GetBranchStatus("TrkLevel2"); - TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1"); - TRKh = TRKh & t->GetBranchStatus("TrkHough"); - CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2"); - CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1"); - TOF = TOF & t->GetBranchStatus("ToFLevel2"); - TRG = TRG & t->GetBranchStatus("TrigLevel2"); - S4 = S4 & t->GetBranchStatus("S4Level2"); - ND = ND & t->GetBranchStatus("NDLevel2"); - AC = AC & t->GetBranchStatus("AcLevel2"); - ORB = ORB & t->GetBranchStatus("OrbitalInfo"); - GP = GP & t->GetBranchStatus("h20"); +// TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2"); +// TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1"); +// TRKh = TRKh & t->GetBranchStatus("TrkHough"); +// CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2"); +// CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1"); +// TOF = TOF & t->GetBranchStatus("ToFLevel2"); +// TRG = TRG & t->GetBranchStatus("TrigLevel2"); +// S4 = S4 & t->GetBranchStatus("S4Level2"); +// ND = ND & t->GetBranchStatus("NDLevel2"); +// AC = AC & t->GetBranchStatus("AcLevel2"); +// ORB = ORB & t->GetBranchStatus("OrbitalInfo"); +// GP = GP & t->GetBranchStatus("h20"); + // Tracker if (TRK1) { @@ -2442,6 +3480,8 @@ if (TRK2) { t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1")); cout << "Tracker : set branch address TrkLevel2" << endl; + NUC = t->GetBranchStatus("TrackNuclei"); + EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true ); }; if (TRKh) { t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough")); @@ -2502,7 +3542,64 @@ cout << "h20 : set branch address GPamela " << endl; }; - + if(NUC){ + + cout << "Found nuclei-track branches" << endl; + + if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack"); + if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar"); + if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar"); + if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar"); + if (TRK2)t-> SetBranchAddress("TrackNuclei",&trk_nuc_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("TrackNuclei",&tof_nuc_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj); + + ///copy the vector content inside a "fake" object (all aother info are missing) + + if( !trk2_nuc_obj )trk2_nuc_obj = new TrkLevel2(); + if( !calo2_nuc_obj )calo2_nuc_obj = new CaloLevel2(); + if( !tof2_nuc_obj )tof2_nuc_obj = new ToFLevel2(); + if( !orb2_nuc_obj )orb2_nuc_obj = new OrbitalInfo(); +// *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj); +// *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj); +// *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj); +// *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj); + + trk2_nuc_obj->SetTrackArray( trk_nuc_obj ); + calo2_nuc_obj->SetTrackArray( calo_nuc_obj ); + tof2_nuc_obj->SetTrackArray( tof_nuc_obj ); + orb2_nuc_obj->SetTrackArray( orb_nuc_obj ); + + + } + + if(EXT){ + + cout << "Found extended tracking algorythm branches" << endl; + + if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack"); + if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar"); + if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar"); + if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar"); + + if (TRK2)t-> SetBranchAddress("RecoveredTrack",&trk_ext_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrack",&tof_ext_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj); + + + if(NUC){ + if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack"); + if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar"); + if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar"); + if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar"); + if (TRK2)t-> SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj); + } + } } /** * Set branch addresses for Pamela friend trees @@ -2526,6 +3623,8 @@ if (TRK2) { t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2")); cout << "Tracker : set branch address TrkLevel2" << endl; + NUC = t->GetBranchStatus("TrackNuclei"); + EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true ); }; if (TRK1) { t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1")); @@ -2539,7 +3638,7 @@ // Calorimeter if (CAL2) { t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2")); - cout << "Calorimeter : set branch address CaloLevel2" << endl; + cout << "Calorimeter : set branch address CaloLevel2" << endl; }; if (CAL1) { t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1")); @@ -2597,9 +3696,110 @@ // cout << "SelectionList: set branch address EventEntry"< SetBranchAddress("TrackNuclei",&trk_nuc_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("TrackNuclei",&tof_nuc_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj); + + ///copy the vector content inside a "fake" object (all aother info are missing) + + if( !trk2_nuc_obj )trk2_nuc_obj = new TrkLevel2(); + if( !calo2_nuc_obj )calo2_nuc_obj = new CaloLevel2(); + if( !tof2_nuc_obj )tof2_nuc_obj = new ToFLevel2(); + if( !orb2_nuc_obj )orb2_nuc_obj = new OrbitalInfo(); + +// *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj); +// *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj); +// *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj); +// *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj); + trk2_nuc_obj->SetTrackArray( trk_nuc_obj ); + calo2_nuc_obj->SetTrackArray( calo_nuc_obj ); + tof2_nuc_obj->SetTrackArray( tof_nuc_obj ); + orb2_nuc_obj->SetTrackArray( orb_nuc_obj ); + + } + if(EXT){ + + cout << "Found extended tracking algorythm branches" << endl; + + t->SetBranchAddress("extAlgFlag",&extAlgFlag); + + if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack"); + if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar"); + if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar"); + if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar"); + + if (TRK2)t-> SetBranchAddress("RecoveredTrack",&trk_ext_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrack",&tof_ext_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj); + + if(NUC){ + if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack"); + if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar"); + if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar"); + if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar"); + if (TRK2)t-> SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj); + if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj); + if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj); + if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj); + } + } + } +/** + * Set the tracking algorythm + * @param alg String to choose the track. + * "" --> take the output of the standard tracking algorythm + * "STD" --> take the output of the standard tracking algorythm + * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection + * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one + * "EXTF" --> force the extended tracking algorythm + * "NUCEXT" --> as "EXT", but for nuclei + * "NUCEXTF" --> as "EXTF", but for nuclei + */ +void PamLevel2::SetTrakingAlgorythm(char *alg){ + + + TString s(alg); + if(s.Contains("NUC", TString::kIgnoreCase) && !NUC) + cout << "Warning! NUC algorythm requested, but branches are missing"< take the output of the standard tracking algorythm"; + cout< take the output of the standard tracking algorythm for nuclei cluster selection"; + cout< in case the standard tracking algorythm has not found any track,"; + cout< force the extended tracking algorythm"; + cout< as \"EXT\", but for nuclei "; + cout< as \"EXTF\", but for nuclei"; + + cout<> "<GetNtrees() <<" \n"; + + if (RUN && R->GetNtrees()) { R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo")); @@ -2672,10 +3875,10 @@ cout << "----------------------------------------------------" << endl; } - else { - delete R; - R = 0; - } +// else { +// delete R; +// R = 0; +// } run_tree = R; @@ -2714,147 +3917,11 @@ return T; } -/** - * 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 "<ID<<" irun "<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 "<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; - // -} -; Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) { + if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n"); + if (!run_tree) { cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl; return false; @@ -2864,6 +3931,7 @@ return (false); } + Int_t oldrun = irun; // store current run index // ----------------------------------------------------------------------- @@ -2873,32 +3941,58 @@ if (irun < 0) { irun = 0LL; irunt = 0LL; - irunentry = 0; + totrunentry = 0LL; + totrunentrymin = 0LL; + totrunentrymax = 0LL; + irunentry = 0LL; + il0entry = 0LL; prevshift = 0; + yprevshift = 0; + prevabstime = 0; + prevpktnum = 0; + abstime = 0ULL; + pktnum = 0; + isFragment = false; run_tree->GetEntry(irun); if (!GetOrbitalInfo()) cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl; - if (gltsync) - delete gltsync; //Emiliano - if (!dbc || (dbc && !dbc->IsConnected())) - SetDBConnection(); //Emiliano - gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here) - if (dbc) - dbc->Close();// Emiliano - }; + if ( fUseDBinRunInfo ){ + if (gltsync) + delete gltsync; //Emiliano + if (!dbc || (dbc && !dbc->IsConnected())) + SetDBConnection(); //Emiliano + gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here) + if (dbc){ + dbc->Close();// Emiliano + delete dbc; + dbc=0; + } + } + } // --------------------------------------------------------------- // retrieve OBT and absolute time of the event // --------------------------------------------------------------- - ULong64_t abstime = 0LL; - // ULong64_t obt = 0LL; // Emiliano Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted + prevabstime = abstime; + prevpktnum = pktnum; if (GetOrbitalInfo()) { abstime = GetOrbitalInfo()->absTime; - obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano + if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano + pktnum = GetOrbitalInfo()->pkt_num; // Emiliano } else { abstime = GetRunInfo()->RUNHEADER_TIME; - obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano + if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano + pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano + } + + if (DBG){ + printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt); + printf("0 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS); + printf("0 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT); + if ( fUseDBinRunInfo ) printf("0 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)); + printf("0 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun); + printf("0 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry); } // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- @@ -2919,68 +4013,148 @@ } // - bool fromfirst = true; // first loop over runs - + bool a = true; + bool b = true; + if ( fUseDBinRunInfo ){ + a = false; + b = false; + if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true; + if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true; + } + if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits + || iev == 0 // or it is the first entry + || (!isSync && ( + (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons) + || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons) + ){ // check on abstime and obt needed to handle nested+DV_skipped packets + + // check for a new run (ma prima il primo!) + if (DBG){ + printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt); + printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS); + printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT); + if ( fUseDBinRunInfo ) printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)); + printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun); + printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry); + } + // printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt); + // printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS); + // printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT); + // printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)); + // printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun); + // printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI - // ------------------------------------------------------ - // loop over runs to find the one that contains the event - // ------------------------------------------------------ - while (( - // ( - // !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s) - // abstime <= GetRunInfo()->RUNTRAILER_TIME) && - // !(obt >= GetRunInfo()->RUNHEADER_OBT && // additional check on OBT (ms) - // obt <= GetRunInfo()->RUNTRAILER_OBT) - // ) - - !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s) - abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano - obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano - || GetRunInfo()->NEVENTS == 0 - // || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento - || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) { - - // if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME) )cout << "ABS TIME "<RUNTRAILER_TIME <= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl; - // if( GetRunInfo()->NEVENTS==0 )cout <<"GetRunInfo()->NEVENTS==0 "<NEVENTS-1-prevshift) ) cout << "irunentry > "<NEVENTS-1-prevshift << endl; - // - - - - - - - - - - - - - - // irunentry = position of current entry, relative to the run - // prevshift = shift needed to synchronize l0 and l2 data (nested events) - // - - - - - - - - - - - - - - - // ----------------------------------------- - // store dead and live-time of previous run - // ----------------------------------------- - // if(SELLI==0){ - if (SELLI != 2) { - if (fromfirst) { - if (oldrun == irun) { - /// decrement counters - if (GetTrigLevel2()) { - totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time - totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time - } - totdltime[2]--; //event counter - if (DBG) { - cout << endl; - cout << "n.events : " << totdltime[2] << endl; - cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl; - cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl; + totrunentry = 0LL; + runfirstentry = 0LL; + for (Int_t r=0; r< run_tree->GetEntries();r++){ + // ------------------------------------------------------------------- + // save the index of the first entry of the run, relative to pam_tree, + // and read a new run + // ------------------------------------------------------------------- + run_tree->GetEntry(r);//update runinfo + if ( r > 0 ){ + totrunentrymin = totrunentrymax+1; + } else { + totrunentrymin = 0LL; + } + totrunentry += GetRunInfo()->NEVENTS; + totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets + irun = r; + if ( fUseDBinRunInfo ){ + a = false; + b = false; + if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true; + if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true; + } + if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits + ( !isSync && + ( abstime >= GetRunInfo()->RUNHEADER_TIME && a // or it is inside obt limits (and abstime limits for security reasons) + && abstime <= GetRunInfo()->RUNTRAILER_TIME && b)) // or it is inside obt limits (and abstime limits for security reasons) + ){ // check on abstime and obt needed to handle nested+DV_skipped packets + if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets) + if ( !isSync ){ + if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n"); + if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax); + // printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n"); + // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI + prevshift += (totrunentrymin-iev); // add the new shift to total shift + totrunentrymin -= (totrunentrymin-iev); // shift run position min + totrunentrymax -= (totrunentrymin-iev); // shift run position max + if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax); + // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI + } else { + printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; } } - else { - totdltime[0] = 0;//live-time - totdltime[1] = 0;//dead-time - totdltime[2] = 0; //event counter - if (DBG) - cout << " *** JUMP RUN *** irun " << irun << endl; + runfirstentry = totrunentrymin; // first entry of the run in the level2 + + + // + if ( fUseDBinRunInfo ){ + if (gltsync) + delete gltsync; // Emiliano + if (!dbc || (dbc && !dbc->IsConnected())) + SetDBConnection(); //Emiliano + gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano + TrkParams::Set(GetRunInfo(), dbc); + if (dbc){ + dbc->Close(); // Emiliano + delete dbc; + 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 could be assigned to a wrong run)" + << endl; + } } - /// add an entry - if (run_tree_clone) - if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries()) - run_tree_clone->GetBranch("DeadLiveTime")->Fill(); - /// reset counters + // + if (DBG) printf(" found \n"); + // printf(" found \n");//TOGLITOGLI + // + break; + } + } // loop over run + + // -------------------------------------- + // if there was no need to update the run + // ---> exit with FALSE + // -------------------------------------- + if (irun == oldrun){ + if (DBG) printf(" no new run \n"); + // printf(" no new run \n");//TOGLITOGLI + return (false); + } + // -------------------------------------- + // ... otherwise + // ---> exit with TRUE + // -------------------------------------- + + if (SELLI != 2) { + /// decrement counters since this event belongs to a new run + if (GetTrigLevel2()) { + totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time + totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time + } + totdltime[2]--; //event counter + if (DBG) { + cout << endl; + cout << "n.events : " << totdltime[2] << endl; + cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl; + cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl; + } + // add an entry + if (run_tree_clone && totdltime[2] > 0) + if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries()) + run_tree_clone->GetBranch("DeadLiveTime")->Fill(); + // reset counters + if ( totdltime[2] > 0 ){ if (GetTrigLevel2()) { totdltime[0] = GetTrigLevel2()->dltime[0];//live-time totdltime[1] = 0; //dead-time @@ -2988,102 +4162,30 @@ totdltime[2] = 1; //event counter } } - // } - irun++; - // ------------------------------------ - // if the end of run tree is reached... - // ------------------------------------ - if (irun == run_tree->GetEntries()) { - if (!fromfirst) { - // ----------------------------------------------------- - // if it happened already once and the run was not found - // ---> exit with error - // ----------------------------------------------------- - 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; + if (DBG){ + cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun + << endl; + printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt); + printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS); + printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT); + if ( fUseDBinRunInfo ) printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)); + printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun); + printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry); } - // ------------------------------------------------------------------- - // save the index of the first entry of the run, relative to pam_tree, - // and read a new run - // ------------------------------------------------------------------- - if (irun > 0) - runfirstentry += (GetRunInfo()->NEVENTS) - prevshift; - irunentry = 0; - prevshift = 0; - run_tree->GetEntry(irun);//update runinfo - irunt = irun - irunoffset[run_tree->GetTreeNumber()]; - if (gltsync) - delete gltsync; // Emiliano - if (!dbc || (dbc && !dbc->IsConnected())) - SetDBConnection(); //Emiliano - gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano - if (dbc) - dbc->Close(); // Emiliano - if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano - cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun - << " has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl; - cout - << " (NB!! in this case some events are assigned to a wrong run)" - << endl; - } - // if(hasfrag && fragid != GetRunInfo()->ID){ - // cout << "... where is the next fragment ??"< exit with FALSE - // -------------------------------------- - if (irun == oldrun) - return (false); - - // -------------------------------------- - // ... otherwise - // -------------------------------------- - - - // -------------------------------------- - // ---> exit with TRUE - // -------------------------------------- - if (DBG) - cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun - << endl; - // ---------------------------------------------------- - // update the tracker parameters - // (non ho trovato nessun altro modo sicuro di farlo...) - // ---------------------------------------------------- - if (!dbc || (dbc && !dbc->IsConnected())) - SetDBConnection(); - TrkParams::Set(GetRunInfo(), dbc); - if (dbc) - dbc->Close(); - - // ---------------------------------------------------- - // then check if the run has a fragment - // in this case we have to switch to the next fragment - // when the end of the first fragment is reached - // ---------------------------------------------------- - if (GetRunInfo()->ID_RUN_FRAG != 0 && - // GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID && - DBG && true) { - cout << "* fragment *" << endl; - } - - return (true); - }; + // printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt); + // printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS); + // printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT); + // printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)); + // printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun); + // printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI + + return (true); + } // need for run upgrade + if (DBG) printf("return false\n"); + return (false); + }// SELLI = 0 SELLI = 2 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- // if it is a preselected file (there is SelectionList) // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- @@ -3158,13 +4260,19 @@ // 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(); + if ( fUseDBinRunInfo ){ + if (!dbc || (dbc && !dbc->IsConnected())) + SetDBConnection(); + TrkParams::Set(GetRunInfo(), dbc); + if (dbc){ + dbc->Close(); + delete dbc; + dbc=0; + } + } // cout << endl; prevshift = 0; + yprevshift = 0; return true; } return false; @@ -3173,7 +4281,16 @@ return false; // } -; + +/** + * 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) { + return (UpdateRunInfo(iev)); +} + /** * 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 @@ -3182,7 +4299,7 @@ Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) { return (UpdateRunInfo((TChain*) run, iev)); } -; + //-------------------------------------- // @@ -4013,6 +5130,7 @@ // sel_tree_clone->Branch("RunEntry",&irun,"runentry/L"); sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L"); + // if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L"); }; Int_t i = 0; @@ -4032,6 +5150,20 @@ pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough")); cout << "Tracker : branch TrkHough" << endl; }; + if(NUC){ + pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj); + cout << "Tracker : branch TrackNuclei" << endl; + } + if(EXT){ + pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I"); + pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj); + cout << "Tracker : branch RecoveredTrack" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj); + cout << "Tracker : branch RecoveredTrackNuclei" << endl; + } + } + i++; } @@ -4046,6 +5178,18 @@ pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2")); cout << "Calorimeter : branch CaloLevel2" << endl; }; + if(NUC){ + pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj); + cout << "Calorimeter : branch TrackNuclei" << endl; + } + if(EXT){ + pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj); + cout << "Calorimeter : branch RecoveredTrack" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj); + cout << "Calorimeter : branch RecoveredTrackNuclei" << endl; + } + } i++; } @@ -4054,6 +5198,18 @@ pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data "); pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2")); cout << "ToF : branch ToFLevel2" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj); + cout << "ToF : branch TrackNuclei" << endl; + } + if(EXT){ + pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj); + cout << "ToF : branch RecoveredTrack" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj); + cout << "ToF : branch RecoveredTrackNuclei" << endl; + } + } i++; }; // Trigger @@ -4089,15 +5245,29 @@ pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info "); pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo")); cout << "OrbitalInfo : branch OrbitalInfo" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj); + cout << "OrbitalInfo : branch TrackNuclei" << endl; + } + if(EXT){ + pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj); + cout << "OrbitalInfo : branch RecoveredTrack" << endl; + if(NUC){ + pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj); + cout << "OrbitalInfo : branch RecoveredTrackNuclei" << endl; + } + } i++; }; // GPamela if (GP) { pam_tree_clone[i] = new TTree("h20", "GPAMELA info "); pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split - cout << "OrbitalInfo : branch OrbitalInfo" << endl; + cout << "GPamela : branch GPamela" << endl; i++; }; + + cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl; } @@ -4168,12 +5338,8 @@ /** * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry. */ -//Int_t PamLevel2::GetEntry(Int_t iee){ Int_t PamLevel2::GetEntry(Long64_t iee) { - // cout << "-------------------------------------"<GetEntry(ii)) { + if (!pam_tree->GetEntry(iee)) { cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl; return 0; } @@ -4210,27 +5374,9 @@ } //------------------------------- - ii = iee; - // Bool_t UPDATED = UpdateRunInfo(run_tree,ii); - // Bool_t UPDATED = UpdateRunInfo(ii); - UpdateRunInfo(ii); - if (SELLI == 0 || SELLI == 2) - irunentry = iee - runfirstentry; - // if(UPDATED && run_tree_clone)run_tree_clone->Fill(); - - // cout << "PamLevel2::GetEntry("<NEVENTS<< endl; - - // if( TRK0 || CAL0 || TOF0 ){ - // if( !GetYodaEntry( ) ){ - // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<ID_ROOT_L0; - // cout << "iroot "<ID_ROOT_L0; + if (DBG){ + cout << "iroot "<SetBranchAddress("Header", &h0_obj); - prevshift = 0; + yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2 //--------------------------------------------------- // TRACKER: if (TRK0) { @@ -4332,7 +5480,6 @@ calo0_obj->Set(); }; l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent()); - // cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<Close(); // EMILIANO, do not leave open connections, open only when needed - + delete dbc; + dbc=0; }; - // if(!dbc || (dbc && !dbc->IsConnected())){ - // cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<IsConnected()))SetDBConnection(); + if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection(); TrkParams::SetCalib(run_obj, dbc); TrkParams::LoadCalib(); if (!TrkParams::CalibIsLoaded()) { cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl; }; - // if(dbc)dbc->Close(); // EMILIANO, do not leave open connections, open only when needed + if(dbc){ + dbc->Close(); // EMILIANO, do not leave open connections, open only when needed + delete dbc; + dbc=0; + }; } - - // cout << l0_tree << endl; return l0_tree; - } /** @@ -4373,111 +5514,352 @@ */ Int_t PamLevel2::GetYodaEntry() { - // cout << "Int_t PamLevel2::GetYodaEntry()"<GetReadEntry(); - // patch - if (irunentry < 0) { - // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<EV_FROM); - // cout << " irunentry "<EV_FROM<EV_FROM "<EV_FROM <<" quella giusta "<ID_ROOT_L0 "<ID_ROOT_L0<TRK_CALIB_USED<< endl; + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + // if it is a full file (not preselected) + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + // if (SELLI == 0 || SELLI == 2 || !hasL0EE) { - ULong64_t obt = 0; - ULong64_t pktn = 0; - if (GetOrbitalInfo()) { - obt = GetOrbitalInfo()->OBT; - pktn = GetOrbitalInfo()->pkt_num; - } + if (!GetYodaTree()){ + printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n"); + return 0; + } - if (!GetOrbitalInfo() && !ISGP) { - cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl; - return 0; - } - if (obt == 0 && pktn == 0 && !ISGP) { - cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl; - return 0; - } + if (irunentry < 0) { + if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<EV_FROM); // prevshift already included in irunentry + + if (DBG){ + cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<EV_FROM <<" quella giusta "<ID_ROOT_L0 "<ID_ROOT_L0<OBT; + pktn = GetOrbitalInfo()->pkt_num; + } - // --------------------------------------------------------------------- - // ATTENTION!!! - // If data are not pre-processed with cleaner, the level0 tree may contain - // 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; + if (!GetOrbitalInfo() && !ISGP) { + cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl; + return 0; } - answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift); - shift++; - if (!GetEventHeader()) { - cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl; + if (obt == 0 && pktn == 0 && !ISGP) { + cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl; return 0; } + // --------------------------------------------------------------------- + // ATTENTION!!! + // If data are not pre-processed with cleaner, the level0 tree may contain + // 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. + // --------------------------------------------------------------------- + Long64_t shift = 0LL; + Long64_t answer = quellagiusta + shift + yprevshift; + Int_t readl0 = 0; + readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry + + if (DBG){ + printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer); + } + + if (ISGP) { obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO } - // cout << "PKTNUM "<GetPscuHeader()->GetCounter()<GetPscuHeader()->GetOrbitalTime()<GetEntries()+1 )cout << ">>> end of level0 tree <<<"<GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl; - // cout << " pktn "<< pktn << endl; - // cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl; - // printf(" IDRUN %u \n",GetRunInfo()->ID); - // - if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) { - prevshift = 0; - shift = -1; - }; + while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){ + if ( isSync && shift == 0LL ){ + printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n"); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + } + if (shift > 0) { + if (DBG) 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; + } + answer = quellagiusta + shift+ yprevshift; + readl0 = l0_tree->GetEntry(answer); + // printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI + + if (!GetEventHeader()) { + cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl; + return 0; + } - } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)( - GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) - < GetYodaTree()->GetEntries() && shift < maxshift); + // + if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) { + if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer); + // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI + yprevshift = 0LL; + shift = -1LL; + }; + + shift++; + } + - if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) { - cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl; - return 0; - } - // cout << "LA ENTRY GIUSTA E`: "<GetEntry(quellagiusta); - if (shift > 1) - prevshift += (shift - 1); + if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){ + if ( isSync ){ + printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n"); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + } + cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl; + cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl; + cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <IsConnected())) SetDBConnection(); //Emiliano + // + if (GetOrbitalInfo()){ + abstime = GetOrbitalInfo()->absTime; + } else { + printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n"); + return 0; + } + // query DB looking for runs containing the processed event + TSQLResult *pResult; + TSQLRow *Row = NULL; + TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime); + if ( DBG ) printf(" query is %s \n",myquery.Data()); + // printf(" query is %s \n",myquery.Data());// TOGLITOGLI + pResult = dbc->Query(myquery.Data()); + if (!pResult->GetRowCount()){ + printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n"); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + return 0; + } + if ( pResult->GetRowCount() == 1 ){ + if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n"); + // printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI + } + for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event + if (Row) delete Row; + Row = pResult->Next(); + if( Row == NULL ) break; + UInt_t idrun = (UInt_t)atoll(Row->GetField(0)); + UInt_t nev = (UInt_t)atoll(Row->GetField(1)); + if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev); + // printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI + + // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course + Bool_t rfound = false; + totrunentry = 0LL; + runfirstentry = 0LL; + for (Int_t r=0; r< run_tree->GetEntries();r++){ + run_tree->GetEntry(r);//update runinfo + if ( r > 0 ){ + totrunentrymin = totrunentrymax+1; + } else { + totrunentrymin = 0LL; + } + totrunentry += GetRunInfo()->NEVENTS; + totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets + irun = r; + + if (idrun == GetRunInfo()->ID){ + if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets) + if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n"); + if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax); + // printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n"); + // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI + prevshift += (totrunentrymin-iev); // add the new shift to total shift + totrunentrymin -= (totrunentrymin-iev); // shift run position min + totrunentrymax -= (totrunentrymin-iev); // shift run position max + if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax); + // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI + } + runfirstentry = totrunentrymin; // first entry of the run in the level2 + - return answer; + // + if (gltsync) + delete gltsync; // Emiliano + if (!dbc || (dbc && !dbc->IsConnected())) + SetDBConnection(); //Emiliano + gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano + if (dbc){ + dbc->Close(); // Emiliano + delete dbc; + 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 could be assigned to a wrong run)" + << endl; + } + // + if (DBG) printf(" found \n"); + // printf(" found \n");//TOGLITOGLI + rfound = true; + // + break; + } + } // loop over run + if ( !rfound ){ + printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n"); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + return 0; + } + + // here we got the first run and we can check if it contains the level0 event + if (!GetYodaTree()){ + printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n"); + return 0; + } + + // get the current run entry + irunentry = iev - runfirstentry; + if (irunentry < 0) { + if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<EV_FROM); // prevshift already included in irunentry + + if (DBG){ + cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<EV_FROM <<" quella giusta "<ID_ROOT_L0 "<ID_ROOT_L0<EV_FROM "<EV_FROM <<" quella giusta "<ID_ROOT_L0 "<ID_ROOT_L0<GetEntry(answer); // prevshift already included in irunentry + if (DBG){ + printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer); + } + // printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI + + while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){ + if (shift > 0) { + if (DBG) 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; + } + answer = quellagiusta + shift+ yprevshift; + readl0 = l0_tree->GetEntry(answer); + // printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI + + if (!GetEventHeader()) { + cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl; + return 0; + } + // + if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) { + if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer); + // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI + yprevshift = 0; + shift = -1; + }; + + shift++; + } + + if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){ + //still not the good run... continue with the nex one! + printf("still not the good run... continue with the nex one!\n"); + } else { + if (DBG) cout << "LA ENTRY GIUSTA E`: "< 1) yprevshift = (shift - 1); + if (Row) delete Row; + delete pResult; + if (dbc){ + dbc->Close(); // Emiliano + delete dbc; + dbc=0; + } + il0entry = answer; + return readl0; + } + // perhaps it is all + }// loop over runs containing the event + if (Row) delete Row; + delete pResult; + if (dbc){ + dbc->Close(); // Emiliano + delete dbc; + dbc=0; + } + // arriving here it means no run found, cannot be! error! + printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n"); + cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + return 0; + } else { + if (DBG) cout << "=> LA ENTRY GIUSTA E`: "< LA ENTRY GIUSTA E`: "<GetPscuHeader()->GetOrbitalTime()) %i pktn %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter() %i \n",obt,(UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()), pktn, (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ); + if (shift > 1) yprevshift = (shift - 1); + il0entry = answer; + return readl0; + } + + /* } // if selli 0 || 2 + if ( SELLI == 1 && hasL0EE ){ + sel_tree->GetEntry(iev); + Long64_t answer = il0entry; + Int_t readl0 = 0; + readl0 = l0_tree->GetEntry(answer); + return readl0; + }*/ + printf(" PamLevel2::GetYodaEntry() ERROR! \n"); + cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl; + cout << "\nFor bug reporting instructions, please see for example:\n"; + cout << " .\n"; + cout << " " << endl; + return 0; } + /** * \Brief Set DB connection */ @@ -4491,17 +5873,34 @@ cout << "USER " << user << endl; cout << "PSW " << psw << endl; } - dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data()); - if (!dbc) - return false; - if (!dbc->IsConnected()) - return false; + Bool_t notconn = true; + Int_t trials = 0; + while ( notconn && trials < 10 ){ + // gSystem->Sleep(500); + dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data()); + //dbc->Connect(host.Data(), user.Data(), psw.Data()); + if ( dbc ) notconn = false; + if (DBG) printf("<%i> test connection...\n ",trials); + if (!dbc){ + if (DBG) printf(" :( failed, no pointer \n"); + notconn = true; + // return false; + }; + if (dbc && !dbc->IsConnected()){ + if (DBG) printf(" :( failed, no connection \n"); + notconn = true; + // return false; + }; + trials++; + }; + if ( notconn ) return false; + // + if (DBG) printf("=connected!\n"); stringstream myquery; // EMILIANO myquery.str(""); // EMILIANO myquery << "SET time_zone='+0:00'"; // EMILIANO dbc->Query(myquery.str().c_str()); // EMILIANO - if (DBG) - cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; + if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); return true; } @@ -4553,6 +5952,7 @@ void PamLevel2::SetSortingMethod(TString how) { if (howtosort != how) { issorted = false; + issorted_new = false; } howtosort = how; }