3 #include "FairGenericStack.h"
4 #include "FairMCEventHeader.h"
6 #include "TDatabasePDG.h"
8 #include "TObjString.h"
9 #include "TClonesArray.h"
10 #include "TParticle.h"
16 : FairPrimaryGenerator(),
23 fdbPdg = TDatabasePDG::Instance();
25 std::vector<TString> tmpfPartNames = {
"e+",
"e-",
"mu+",
"mu-",
"pi+",
"pi-",
"k+",
"k-",
"p+",
"p-",
"n0",
"n0b",
"e+-",
"mu+-",
"pi+-",
"k+-",
"p+-",
"n00b" ,
"t+",
"t-",
"t+-",
"nt",
"any" };
26 std::vector<int> tmpfNamePdg = {-11 , 11 , -13 , 13 , 211 , -211 , 321 , -321, 2212, -2212, 2112, -2112, 11000 , 13000 , 211000 , 321000 , 2212000, 2112000, 1 , -1 , 2 , 0 , 3 };
27 std::vector<int> tmpfCombFsPdg = {-11 , 11 , -13 , 13 , 211 , -211 , 321 , -321, 2212, -2212 , 22};
45 : FairPrimaryGenerator(),
52 fdbPdg = TDatabasePDG::Instance();
54 std::vector<TString> tmpfPartNames = {
"e+",
"e-",
"mu+",
"mu-",
"pi+",
"pi-",
"k+",
"k-",
"p+",
"p-",
"n0",
"n0b",
"e+-",
"mu+-",
"pi+-",
"k+-",
"p+-",
"n00b" ,
"t+",
"t-",
"t+-",
"nt",
"any" };
55 std::vector<int> tmpfNamePdg = {-11 , 11 , -13 , 13 , 211 , -211 , 321 , -321, 2212, -2212, 2112, -2112, 11000 , 13000 , 211000 , 321000 , 2212000, 2112000, 1 , -1 , 2 , 0 , 3 };
56 std::vector<int> tmpfCombFsPdg = {-11 , 11 , -13 , 13 , 211 , -211 , 321 , -321, 2212, -2212 , 22};
80 for (
int ifs = 0; ifs < (int)
fFilterSets.size(); ++ifs)
82 cout <<
"FILTER SET "<< ifs<<endl<<endl;
95 TObjArray *tok = s.Tokenize(delim);
96 int N = tok->GetEntries();
99 TString token = (((TObjString*)tok->At(
i))->String()).Strip(TString::kBoth);
100 toks.push_back(token);
111 double curr_p = p4.P(); acc_trk = (curr_p >= f.
pmin && curr_p <= f.
pmax);
112 if (acc_trk) {
double curr_pt = p4.Pt(); acc_trk = (curr_pt >= f.
ptmin && curr_pt <= f.
ptmax); }
113 if (acc_trk) {
double curr_pz = p4.Pz(); acc_trk = (curr_pz >= f.
pzmin && curr_pz <= f.
pzmax); }
114 if (acc_trk) {
double curr_tht = p4.Theta()*57.2958; acc_trk = (curr_tht >= f.
thtmin && curr_tht <= f.
thtmax); }
115 if (acc_trk) {
double curr_phi = p4.Phi()*57.2958; acc_trk = (curr_phi >= f.
phimin && curr_phi <= f.
phimax); }
124 if (
fdbPdg->GetParticle(pdg)!=0x0 &&
fdbPdg->GetParticle(pdg)->AntiParticle()!=0x0)
125 return fdbPdg->GetParticle(pdg)->AntiParticle()->PdgCode();
134 int len = delim.Length();
135 a = (
TString(
s(0,s.Index(delim)))).Atof();
136 double tmpb = (
TString(
s(s.Index(delim)+len,10000))).Atof();
137 if (tmpb>0. || forceset) b = tmpb;
144 int len = delim.Length();
145 a = (
TString(
s(0,s.Index(delim)))).Atoi();
146 int tmpb = (
TString(
s(s.Index(delim)+len,10000))).Atoi();
161 filterStr.ReplaceAll(
":",
"&&");
162 filterStr.ReplaceAll(
"gamma",
"nt");
163 filterStr.ReplaceAll(
"gam",
"nt");
164 filterStr.ReplaceAll(
"ch0",
"nt");
165 filterStr.ReplaceAll(
"tr",
"t");
166 filterStr.ReplaceAll(
"ch",
"t");
167 filterStr.ReplaceAll(
"mass",
"m");
174 for (
int ifs=0; ifs < (int) filtersets_str.size(); ++ ifs)
184 for (
int i=0;
i<(int) filters_str.size();++
i)
188 if ( (!fs.EndsWith(
")")) || (fs.Contains(
",") && !fs.Contains(
"[")) )
190 cout <<
" --> Please check filter string '"<<fs<<
"'"<<endl;
195 TString tmpfs = fs(fs.Index(
"(")+1,fs.Length()-fs.Index(
"(")-2);
199 if (
fVerbose>3) cout<<endl<<fs<<endl<<tmpfs<<endl;
205 if (fs.BeginsWith(
"!")) {f.
veto =
true; fs.ReplaceAll(
"!",
"");}
208 if (fs.BeginsWith(
"m(")) f.
compo=
true;
210 for (
int j=0;j<(int)cuts.size();++j)
217 if (ct.Contains(
"["))
219 key = ct(0,ct.Index(
"["));
220 ct = ct(ct.Index(
"[")+1,ct.Length()-ct.Index(
"[")-2);
229 else if (ct.Atoi()!=0 || ct==
"0") f.
nmin = f.
nmax = ct.Atoi();
234 for (
int jj=0;
jj<(int)parts.size();++
jj)
239 if (parts[jj]==
"nocc") f.
nocc =
true;
246 else if (key==
"pdg") {f.
pdg[0] = ct.Atoi(); f.
ndau=1;}
259 cout <<
" --> Please check filter string '"<<fs<<
"'"<<endl;
262 else filters.push_back(f);
277 for (
int i0=0; i0<(int)l0->size(); ++i0)
283 if ((*l1) == (*l0)) st1 = i0+1;
286 for (
int i1=st1; i1<(int)l1->size(); ++i1)
292 if (l2==0) res.push_back(
PndSmpCand(pdg, c0, c1));
297 if (*l2==*l1) st2 = i1+1;
298 else if (*l2==*l0) st2 = i0+1;
301 for (
int i2=st2; i2<(int)l2->size(); ++i2)
307 if (l3==0) res.push_back(
PndSmpCand(pdg, c0, c1, c2));
312 if (*l3==*l2) st3 = i2+1;
313 else if (*l3==*l1) st3 = i1+1;
314 else if (*l3==*l0) st3 = i0+1;
317 for (
int i3=st3; i3<(int)l3->size(); ++i3)
323 if (l4==0) res.push_back(
PndSmpCand(pdg, c0, c1, c2, c3));
328 if (*l4==*l3) st4 = i3+1;
329 else if (*l4==*l2) st3 = i2+1;
330 else if (*l4==*l1) st3 = i1+1;
331 else if (*l4==*l0) st3 = i0+1;
334 for (
int i4=st4; i4<(int)l4->size(); ++i4)
339 res.push_back(
PndSmpCand(pdg, c0, c1, c2, c3, c4));
356 printf(
"LIST %s with %d candidates\n",name.Data(), (int)l.size());
357 for (
int i=0;
i<(int)l.size(); ++
i) {
printf(
" %2d : ",
i); l[
i].Print();}
368 bool acc_glob =
false;
378 FairPrimaryGenerator::GenerateEvent(pStack);
381 if (!active_filters) {acc_glob =
true;
continue;}
383 TClonesArray* fParticleList = pStack->GetListOfParticles();
391 std::map<int,PndSmpCandList> pdgList;
402 for (Int_t iPart=0; iPart<fParticleList->GetEntries(); ++iPart)
404 TParticle *
particle = (TParticle*)fParticleList->At(iPart);
407 Int_t pdg = particle->GetPdgCode();
413 TLorentzVector l(particle->Px(), particle->Py(), particle->Pz(), particle->Energy());
414 Float_t ch =
fdbPdg->GetParticle(pdg) ?
fdbPdg->GetParticle(pdg)->Charge()/3. : 0;
431 for (
unsigned int j=0;j<
fCombFsPdg.size();++j)
436 if (pdg_ch == (
int)ch)
438 l.SetVectM(l.Vect(), pdg_m);
457 int nc = pdgList[pdg].size();
470 for (
int ifs=0; ifs < (int)
fFilterSets.size(); ++ifs)
473 if (acc_glob)
continue;
476 bool acc_fset =
true;
484 if (!acc_fset)
continue;
492 if (f.
compo)
continue;
497 for (
int j=0 ; j<(int)all.size() ; ++j)
504 int trpdg = all[j].Pdg();
505 int trchg = (int) all[j].Charge();
508 int flpdg = f.
pdg[0];
511 if (all[j].Marker()>0)
514 if (flpdg == 3) trpdg = flpdg;
517 if (flpdg == 2) trpdg = abs(trchg)*2;
520 if (abs(flpdg)<2) trpdg = trchg;
524 if (flpdg%1000==0) trpdg = abs(trpdg)*1000;
530 if (
fVerbose>2) cout <<
"\033[1;34m ** ";
535 if (
fVerbose>2) {all[j].Print(); cout<<
"\033[0;0m";}
539 if (cnt_matches<f.nmin || cnt_matches>f.
nmax) acc_evt =
false;
542 acc_evt = acc_evt^f.
veto;
544 if (
fVerbose>2) cout <<
"Filterset "<<ifs<<
" Filter "<<
i<<
" : N_matched = "<<cnt_matches<<
" : event "<<(!acc_evt?
"not ":
"")<<
"accepted"<<endl<<endl<<endl;
547 acc_fset = (acc_fset && acc_evt);
558 if (!acc_fset)
continue;
566 if (!f.
compo)
continue;
574 std::vector<int> vpdg, vapdg;
576 for (
int j=0; j<f.
ndau; ++j)
578 vpdg.push_back(f.
pdg[j]);
581 l[j] = &(pdgList[f.
pdg[j]]);
589 if (!f.
nocc && !is_permutation(vpdg.begin(), vpdg.end(), vapdg.begin()))
592 comblist.insert(comblist.end(), anticomblist.begin(), anticomblist.end());
598 for (
int j=0 ; j<(int)comblist.size() ; ++j)
600 TLorentzVector p4 = comblist[j].P4();
609 if (
fVerbose>2) cout <<
"\033[1;35m ** ";
614 if (
fVerbose>2) { comblist[j].Print(); cout<<
"\033[0;0m";}
618 if (cnt_matches<f.nmin || cnt_matches>f.
nmax) acc_evt =
false;
621 acc_evt = acc_evt^f.
veto;
623 if (
fVerbose>2) cout <<
"Filterset "<<ifs<<
" Filter "<<
i<<
" : N_matched = "<<cnt_matches<<
" : event "<<(!acc_evt?
"not ":
"")<<
"accepted"<<endl<<endl<<endl;
626 acc_fset = (acc_fset && acc_evt);
633 acc_glob = (acc_glob || acc_fset);
649 cout <<
"\n -E PndFilteredPrimaryGenerator: No event was found within " << iTry <<
" tries which satisfies your event filter.\n ";
650 cout <<
"I accept a random event as evtNr " <<
fEventNrFiltered <<
" to avoid infinite loops. \n";
651 cout <<
"Try increasing the max. number of tries or change your filter\n\n";
653 cout << iTry <<
" events simulated until I found a good one.\n";
std::vector< PndSmpCand > PndSmpCandList
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
StrVec SplitString(TString s, TString delim=" ")
Splits a TString to substrings.
std::unordered_set< int > fSetFsPdg
set to identify particles from MC truth list which can be combined
Simple container for filter definition (criteria) for PndFilteredPrimaryGenerator.
PndFilteredPrimaryGenerator()
Default constructor.
std::map< TString, int > fNameCodeMap
mapes names to (pdg) codes
bool CheckKinematic(const PndSmpFilt &f, const TLorentzVector &p4)
Checks whether P4 kinematics match the criteria of a PndSmpFilt.
for(int j=0;j< ncounts;j++)
Int_t fEventNrFiltered
Event number (Set by the filtered primary generator.
virtual Bool_t Init()
Initialize the event generator(s) and the event (veto) filter(s).
std::map< int, TString > fCodeNameMap
mapes (pdg) codes to names
TDatabasePDG * fdbPdg
Shortcut to TDatabasePDG.
std::vector< TString > fPartNames
particle names for particles to count (with and w/o charged specification, also simply tracks and neu...
Primary generator with added event filtering capabilities.
vector< PndSmpFilt > PndSmpFilterSet
void SetP4(TLorentzVector p4)
Sets LorentzVector.
friend F32vec4 fabs(const F32vec4 &a)
Int_t fFailedFilterEvents
FairEvtFilterParams fEvtFilterStat
Contains the statistics of the event filtering process.
Bool_t Overlap(PndSmpCand *c)
virtual Bool_t GenerateEvent(FairGenericStack *pStack)
Calls event generators and the event filters.
void AddFilter(TString filterStr)
Registers a filter as a string to be parsed Each filter set consist of one filter definition or a num...
std::vector< PndSmpFilterSet > fFilterSets
Contains the filter-sets. Each filter set consist of one filter definition or a number of filters con...
Int_t fEventPrintFreq
Print frequency for filtered events.
void PrintSmpCandList(PndSmpCandList l, TString name="")
Prints a candidate lits.
void GetRangeInt(TString s, int &a, int &b, TString delim="..")
Turns a string of the form (e.g. "3..8") to a range a,b.
PndSmpCandList CombineList(int pdg, PndSmpCandList *l0, PndSmpCandList *l1, PndSmpCandList *l2=0, PndSmpCandList *l3=0, PndSmpCandList *l4=0)
Combines upt to five particle lists of PndSmpCand with overlap and double counting prevention...
Int_t fVerbose
Level of commenting output, 0 means no output, higher gives more output.
std::vector< int > fNamePdg
particle codes for particles to count (with and w/o charged specification, also simply tracks and neu...
int AntiPdgCode(int pdg)
Gets anti-pdg code, if exists. If not returns the code itself (particle is its anti-particle) ...
std::vector< int > fCombFsPdg
particle codes for the lists used for combinatorics; !!! the codes are not selected from MC truth...
void GetRangeDouble(TString s, double &a, double &b, TString delim=",", bool forceset=false)
Turns a string of the form (e.g. "124.2,178.3") to a range a,b.
Simple particle candidate to perform simple combinatorics and particle counting for event filtering...