CMS 3D CMS Logo

PythiaFilterMultiAncestor.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 
6 using namespace edm;
7 using namespace std;
8 
10  : token_(consumes<edm::HepMCProduct>(
11  edm::InputTag(iConfig.getUntrackedParameter("moduleLabel", std::string("generator")), "unsmeared"))),
12  particleID(iConfig.getUntrackedParameter("ParticleID", 0)),
13  minpcut(iConfig.getUntrackedParameter("MinP", 0.)),
14  maxpcut(iConfig.getUntrackedParameter("MaxP", 10000.)),
15  minptcut(iConfig.getUntrackedParameter("MinPt", 0.)),
16  maxptcut(iConfig.getUntrackedParameter("MaxPt", 10000.)),
17  minetacut(iConfig.getUntrackedParameter("MinEta", -10.)),
18  maxetacut(iConfig.getUntrackedParameter("MaxEta", 10.)),
19  minrapcut(iConfig.getUntrackedParameter("MinRapidity", -20.)),
20  maxrapcut(iConfig.getUntrackedParameter("MaxRapidity", 20.)),
21  minphicut(iConfig.getUntrackedParameter("MinPhi", -3.5)),
22  maxphicut(iConfig.getUntrackedParameter("MaxPhi", 3.5)),
23  status(iConfig.getUntrackedParameter("Status", 0)),
24  motherIDs(iConfig.getUntrackedParameter("MotherIDs", std::vector<int>{0})),
25  daughterIDs(iConfig.getUntrackedParameter("DaughterIDs", std::vector<int>{0})),
26  daughterMinPts(iConfig.getUntrackedParameter("DaughterMinPts", std::vector<double>{0.})),
27  daughterMaxPts(iConfig.getUntrackedParameter("DaughterMaxPts", std::vector<double>{10000.})),
28  daughterMinEtas(iConfig.getUntrackedParameter("DaughterMinEtas", std::vector<double>{-10.})),
29  daughterMaxEtas(iConfig.getUntrackedParameter("DaughterMaxEtas", std::vector<double>{10.})),
30  processID(iConfig.getUntrackedParameter("ProcessID", 0)),
31  betaBoost(iConfig.getUntrackedParameter("BetaBoost", 0.)) {
32  //now do what ever initialization is needed
33 }
34 
36  // do anything here that needs to be done at desctruction time
37  // (e.g. close files, deallocate resources etc.)
38 }
39 
40 //
41 // member functions
42 //
43 
44 // ------------ access the full genealogy ---------
46  for (HepMC::GenVertex::particle_iterator ancestor = particle->production_vertex()->particles_begin(HepMC::ancestors);
47  ancestor != particle->production_vertex()->particles_end(HepMC::ancestors);
48  ++ancestor) {
49  // std::cout << __LINE__ << "]\t particle's PDG ID " << particle->pdg_id()
50  // << " \t particle's ancestor's PDG ID " << (*ancestor)->pdg_id()
51  // << " \t ID to match " << IDtoMatch << std::endl;
52 
53  if (abs((*ancestor)->pdg_id()) == abs(IDtoMatch)) {
54  // std::cout << __LINE__ << "]\t found!" << std::endl;
55  return true;
56  }
57  }
58 
59  // std::cout << __LINE__ << "]\t nope, no luck" << std::endl;
60  return false;
61 }
62 
63 // ------------ method called to produce the data ------------
65  using namespace edm;
66  bool accepted = false;
68  iEvent.getByToken(token_, evt);
69 
70  const HepMC::GenEvent* myGenEvent = evt->GetEvent();
71 
72  if (processID == 0 || processID == myGenEvent->signal_process_id()) {
73  for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
74  ++p) {
75  HepMC::FourVector mom = MCFilterZboostHelper::zboost((*p)->momentum(), betaBoost);
76  rapidity = 0.5 * log((mom.e() + mom.pz()) / (mom.e() - mom.pz()));
77 
78  if (abs((*p)->pdg_id()) == particleID && mom.rho() > minpcut && mom.rho() < maxpcut &&
79  (*p)->momentum().perp() > minptcut && (*p)->momentum().perp() < maxptcut && mom.eta() > minetacut &&
80  mom.eta() < maxetacut && rapidity > minrapcut && rapidity < maxrapcut && (*p)->momentum().phi() > minphicut &&
81  (*p)->momentum().phi() < maxphicut) {
82  // find the mother
83  for (std::vector<int>::const_iterator motherID = motherIDs.begin(); motherID != motherIDs.end(); ++motherID) {
84  // check status if no mother's pdgID is specified
85  if (status == 0 && *motherID == 0) {
86  accepted = true;
87  }
88  if (status != 0 && *motherID == 0) {
89  if ((*p)->status() == status)
90  accepted = true;
91  }
92 
93  // HepMC::GenParticle* mother = (*((*p)->production_vertex()->particles_in_const_begin()));
94 
95  // check the mother's pdgID
96  if (status == 0 && *motherID != 0) {
97  // if (abs(mother->pdg_id()) == abs(*motherID)) {
98  if (isAncestor(*p, *motherID)) {
99  accepted = true;
100  }
101  }
102  if (status != 0 && *motherID != 0) {
103  // if ((*p)->status() == status && abs(mother->pdg_id()) == abs(*motherID)){
104  if ((*p)->status() == status && isAncestor(*p, *motherID)) {
105  accepted = true;
106  }
107  }
108  }
109 
110  // find the daughters
111  if (accepted & (!daughterIDs.empty())) {
112  // if you got this far it means that the mother was found
113  // now let's check the daughters
114  // use a counter, if there's enough daughter that match the pdg and kinematic
115  // criteria accept the event
116  uint good_dau = 0;
117  for (HepMC::GenVertex::particle_iterator dau = (*p)->end_vertex()->particles_begin(HepMC::children);
118  dau != (*p)->end_vertex()->particles_end(HepMC::children);
119  ++dau) {
120  for (unsigned int i = 0; i < daughterIDs.size(); ++i) {
121  // if a daughter has its pdgID among the desired ones, apply kin cuts on it
122  // if it survives, add a notch to the counter
123  if ((*dau)->pdg_id() == daughterIDs[i]) {
124  if ((*dau)->momentum().perp() < daughterMinPts[i])
125  continue;
126  if ((*dau)->momentum().perp() > daughterMaxPts[i])
127  continue;
128  if ((*dau)->momentum().eta() < daughterMinEtas[i])
129  continue;
130  if ((*dau)->momentum().eta() > daughterMaxEtas[i])
131  continue;
132  ++good_dau;
133  }
134  }
135  }
136  if (good_dau < daughterIDs.size())
137  accepted = false;
138  }
139 
140  /*
141  if (status == 0 && motherID != 0){
142  if (abs(((*p)->mother())->pdg_id()) == abs(motherID)) {
143  accepted = true;
144  }
145  }
146  if (status != 0 && motherID != 0){
147  if ((*p)->status() == status && abs(((*p)->mother())->pdg_id()) == abs(motherID)){
148  accepted = true;
149  }
150  }
151  */
152  }
153  // only need to satisfy the conditions _once_
154  if (accepted)
155  break;
156  }
157 
158  } else {
159  accepted = true;
160  }
161 
162  if (accepted) {
163  return true;
164  } else {
165  return false;
166  }
167 }
HepMC::FourVector zboost(const HepMC::FourVector &, double)
PythiaFilterMultiAncestor(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< double > daughterMaxPts
bool isAncestor(HepMC::GenParticle *particle, int IDtoMatch)
std::vector< double > daughterMinPts
int iEvent
Definition: GenABIO.cc:224
bool filter(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::HepMCProduct > token_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< double > daughterMinEtas
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
std::vector< double > daughterMaxEtas
bool accepted(std::vector< std::string_view > const &, std::string_view)
def uint(string)
HLT enums.