// $Id: hrichevtmixer.cc,v 1.8 2009-07-15 11:39:22 halo Exp $ // Last update by Thomas Eberl: 04/07/12 13:32:24 // using namespace std; #include "TObjArray.h" #include "TObjString.h" #include "TRandom.h" #include "hades.h" #include "hcategory.h" #include "hdebug.h" #include "hdetector.h" #include "hdihitmatch.h" #include "hdihitmatchsim.h" #include "hevent.h" #include "hhitmatch.h" #include "hhitmatchsim.h" #include "hiterator.h" #include "hlinearcategory.h" #include "hmatrixcatiter.h" #include "hrichcut.h" #include "hrichcuttracklet.h" #include "hrichcuttrackletsim.h" #include "hrichevtmixer.h" #include "hrichhistfac.h" #include "hrichhit.h" #include "hrichutilfunc.h" #include "hruntimedb.h" #include "hspectrometer.h" #include "richdef.h" #include #include ClassImp(HRichEvtMixer) HRichEvtMixer::HRichEvtMixer(const Text_t *name,const Text_t *title,const Char_t* world, Int_t evts, Int_t rndnr,const Char_t *cuts) : HReconstructor(name,title) { evtrange = evts; // number of events to store before mixing within these nrMixObjs = rndnr; // number of tracks per evt used for mixing // if 0 then all tracks are used if (nrMixObjs) cout<<"Taking "<1) { TString tmp; tmp=""; for (Int_t ii=0; ii < len; ii++) { TString s(sCuts[ii]); if (!s.CompareTo(",")) { cutT.Add(new TObjString(tmp.Data())); tmp=""; } else { tmp.Append(s); } if (ii==len-1) cutT.Add(new TObjString(tmp.Data())); } } if (cutT.GetLast()==0) {cutStateId = new TString(((TObjString*)(cutT[cutT.GetLast()]))->GetString());} else{ TString *a = new TString("T"); for (Int_t i=0;iGetString(); if (i>0 && !tmp.Contains("nocut")) {a->Append(".");a->Append("T");} if (!tmp.Contains("nocut")) a->Append(((TObjString*)(cutT[i]))->GetString()); } cutStateId = a; if (!a->Contains(".")) cutStateId->Remove(0,1); } cout<Data()<getCurrentEvent(); HRuntimeDb *rtdb=gHades->getRuntimeDb(); HSpectrometer *spec=gHades->getSetup(); if (event && rtdb) { HDetector *rich = spec->getDetector("Rich"); if (rich) { pHitMatchCat=event->getCategory(catMatchHit); if (!pHitMatchCat) { Error("init","No HIT MATCH category defined"); return kFALSE; } } pIterMatchHit = (HIterator*)getHitMatchCat()->MakeIterator("native"); // Setup output category pHitDiMatchCat=event->getCategory(catDiMatchHit); //cout<buildCategory(catDiMatchHit); //cout<<"pair cat created"<addCategory(catDiMatchHit, pHitDiMatchCat, "Rich"); } pIterDiMatchHit = (HIterator*)getHitDiMatchCat()->MakeIterator("native"); } } evtset = new TObjArray(evtrange); evtsStoredInRange = 0; nCntMixedEvts = 0; // counter for number of mixed events nCounterProcessedNbEvents=0; //total nb of evts seen by this task cuts = new TObjArray(1); if (isSim) { HRichCutTrackletSim *trackcut = new HRichCutTrackletSim("trackcut","default"); cuts->Add(trackcut); } else { HRichCutTracklet *trackcut = new HRichCutTracklet("trackcut","default"); cuts->Add(trackcut); } Warning("initCuts","default cuts created"); return kTRUE; } Bool_t HRichEvtMixer::finalize() { cout<<"************** EVT Mixer TASK ************************************"<getEntries()<getEntries()<1) return kSkipEvent; Int_t kReturnValue=1; if (evtsStoredInRange == evtrange) { // cout<<"now in exec mixer: "<0) doEventMixingTrackSubset(); else doEventMixingAllTracks(); deleteStoredEvents(); kReturnValue = 0;// exec next task } else { storeEvent(evtsStoredInRange++); kReturnValue = kSkipEvent;// skip next task } // ----------------------------------- nCounterProcessedNbEvents++; //cout<getEntries()<<" pairs in evt "<getEntries()<getEntries()); TString nn = "evt_"; nn+=n; arr->SetName(nn.Data()); evtset->Add(arr); //cout<GetName()<FindObject("trackcut")); } else { trackcut = (HRichCutTracklet*)(cuts->FindObject("trackcut")); } Bool_t isTrackletCut = kFALSE; if (trackcutsim && isSim) isTrackletCut=trackcutsim->switchTo(cutStateId->Data()); else if(trackcut && !isSim) isTrackletCut=trackcut->switchTo(cutStateId->Data()); else Error("selectTracklets","cut not found"); if (!isTrackletCut) return; HHitMatch *h=0; HHitMatchSim *hs=0; pIterMatchHit->Reset(); if (isSim) { while(( hs= (HHitMatchSim *)pIterMatchHit->Next())) { if (trackcutsim->check(hs)) { HHitMatchSim *ht = new HHitMatchSim(); *ht = *hs; // copy object arr->Add(ht); } } } else { while(( h= (HHitMatch *)pIterMatchHit->Next())) { if (trackcut->check(h)) { HHitMatch *ht = new HHitMatch(); *ht = *h; // copy object arr->Add(ht); } } } // cout<<"evts in "<GetName()<<" after store evts: "<GetLast()+1<GetLast()+1;evtcounter++) { TObjArray* evtc = ((TObjArray*)(*evtset)[evtcounter]); for (Int_t trackcounter=0;trackcounterGetLast()+1;trackcounter++) { if (isSim) delete ((HHitMatchSim*)(*evtc)[trackcounter]); else delete ((HHitMatch*)(*evtc)[trackcounter]); } delete evtc; } delete evtset; evtset = new TObjArray(evtrange); } void HRichEvtMixer::doEventMixingTrackSubset() { // use nrMixObjs random objs each from the rest of evtrange-1 evts to // make combinatorics with nrMixObjs random objs of this evt // clear HitMatch category getHitMatchCat()->Clear(); // clear DiHitMatch category getHitDiMatchCat()->Clear(); // memorize already stored HHitMatch objects TObjArray alreadystored(5); TObjArray alreadycopied(5); // cout<getEntries()<<" tracks in evt after clearing"<GetLast()+1; // nr of tracks in first evt // if nr of tracks in evt is smaller than selected nr of evts if ( nrMixObjsTmp > evt0max ) nrMixObjsTmp=evt0max; Int_t *ievt0 = new Int_t[nrMixObjsTmp]; for (Int_t ii=0;iiInteger(evt0max); while (nEvt0NewIndCounterInteger(evt0max); //cout<GetLast()+1;evtcounter++) { nCntMixedEvts++; // count number of mixed events //get next evt from evt set TObjArray* evtc = ((TObjArray*)(*evtset)[evtcounter]); if (!evtc) {Error("doEventMixingTrackSubset","no next event"); return;} Int_t nrMixObjsTmpC=nrMixObjs;//make a copy of the maximum nr of tracks per event Int_t nEvtCNewIndCounter=0;//counter for unique track indexes randomly chosen Int_t evtcmax = evtc->GetLast()+1; // nr of tracks in current evt // if nr of tracks in evt is smaller than selected nr of evts if ( nrMixObjsTmpC > evtcmax ) nrMixObjsTmpC=evtcmax; Int_t *ievtc = new Int_t[nrMixObjsTmpC]; for (Int_t ii=0;iiInteger(evtcmax); while (nEvtCNewIndCounterInteger(evtcmax); } nEvtCNewIndCounter++; // combine to pair // cout<<"--------------------------------------------------------------"<GetName()<Clear(); // clear DiHitMatch category getHitDiMatchCat()->Clear(); // memorize already stored HHitMatch objects TObjArray alreadystored(5); TObjArray alreadycopied(5); Int_t maxNbEvts = evtset->GetLast()+1; // cout<<"nb of stored evts for mixing: "<GetLast()+1; // nr of tracks in evt // cout<<"outer evt has entries: "<GetLast()+1; // nr of tracks in current evt // cout<<"inner evt has entries: "<getNewSlot(loc); if (hdi!=NULL) hdi = new(hdi) HDiHitMatch(h1,h2); // store the HHitMatch objs; HHitMatch *h1stored = 0; HHitMatch *h2stored = 0; if (stored.IndexOf(h1)<0) {h1stored = copyHitMatch(h1);stored.Add(h1);storedc.Add(h1stored);} else {h1stored = (HHitMatch*)storedc[stored.IndexOf(h1)];} if (stored.IndexOf(h2)<0) {h2stored = copyHitMatch(h2);stored.Add(h2);storedc.Add(h2stored);} else {h2stored = (HHitMatch*)storedc[stored.IndexOf(h2)];} if ( !h1stored || !h2stored ) { Error("createHitDiMatch","could not copy track to category"); return kFALSE; } //cout<getEntries()<<" tracks in evt after copy"<getIndex(h1stored); Int_t ind2 = getHitMatchCat()->getIndex(h2stored); hdi->setIndTrk1(ind1); hdi->setIndTrk2(ind2); if (hdi) return kTRUE; else return kFALSE;; } Bool_t HRichEvtMixer::createHitDiMatch(HHitMatchSim *h1, HHitMatchSim *h2,TObjArray &stored,TObjArray &storedc) { // make sure that only tracks that were used in the mixed pair creation are kept // in the category. // this is necessary as the tracks will be used to fill histograms to control the properties // of the mixed event track content // h1->dumpToStdoutSim(); //create a new HDiHitMatch object, input sector HLocation loc; loc.set(1,0);//dummy ?!! // sim pairs are not yet implemented HDiHitMatch *hdi = (HDiHitMatch*)((HLinearCategory*) getHitDiMatchCat())->getNewSlot(loc); if (hdi!=NULL) hdi = new(hdi) HDiHitMatch(h1,h2); // store the HHitMatch objs; HHitMatchSim *h1stored = 0; HHitMatchSim *h2stored = 0; if (stored.IndexOf(h1)<0) {h1stored = copyHitMatch(h1);stored.Add(h1);storedc.Add(h1stored);} else {h1stored = (HHitMatchSim*)storedc[stored.IndexOf(h1)];} if (stored.IndexOf(h2)<0) {h2stored = copyHitMatch(h2);stored.Add(h2);storedc.Add(h2stored);} else {h2stored = (HHitMatchSim*)storedc[stored.IndexOf(h2)];} if ( !h1stored || !h2stored ) { Error("createHitDiMatch","could not copy track to category"); return kFALSE; } // cout<getEntries()<<" tracks in evt after copy"<getIndex(h1stored); Int_t ind2 = getHitMatchCat()->getIndex(h2stored); hdi->setIndTrk1(ind1); hdi->setIndTrk2(ind2); if (hdi) return kTRUE; else return kFALSE;; } HHitMatch* HRichEvtMixer::copyHitMatch(HHitMatch *h) { //create a new HHitMatch object, input sector HLocation loc; loc.set(1,0); HHitMatch *pHitMatch = (HHitMatch*)((HLinearCategory*) getHitMatchCat())->getNewSlot(loc); if (pHitMatch!=NULL) pHitMatch = new(pHitMatch) HHitMatch; if (pHitMatch) *pHitMatch = *h; return pHitMatch; } HHitMatchSim* HRichEvtMixer::copyHitMatch(HHitMatchSim *h) { //create a new HHitMatch object, input sector HLocation loc; loc.set(1,0); HHitMatchSim *pHitMatch = (HHitMatchSim*)((HLinearCategory*) getHitMatchCat())->getNewSlot(loc); if (pHitMatch!=NULL) pHitMatch = new(pHitMatch) HHitMatchSim; if (pHitMatch) *pHitMatch = *h; return pHitMatch; }