CMS 3D CMS Logo

PythiaDauVFilterMatchID.cc
Go to the documentation of this file.
3 #include <iostream>
4 #include <vector>
5 
6 using namespace edm;
7 using namespace std;
8 using namespace Pythia8;
9 
10 
12  fVerbose(iConfig.getUntrackedParameter("verbose",0)),
13  token_(consumes<edm::HepMCProduct>(edm::InputTag(iConfig.getUntrackedParameter("moduleLabel",std::string("generator")),"unsmeared"))),
14  particleID(iConfig.getUntrackedParameter("ParticleID", 0)),
15  motherID(iConfig.getUntrackedParameter("MotherID", 0)),
16  chargeconju(iConfig.getUntrackedParameter("ChargeConjugation", true)),
17  ndaughters(iConfig.getUntrackedParameter("NumberDaughters", 0)),
18  maxptcut(iConfig.getUntrackedParameter("MaxPt", 14000.))
19 {
20  //now do what ever initialization is needed
21  vector<int> defdauID;
22  defdauID.push_back(0);
23  dauIDs = iConfig.getUntrackedParameter< vector<int> >("DaughterIDs",defdauID);
24  vector<double> defminptcut;
25  defminptcut.push_back(0.);
26  minptcut = iConfig.getUntrackedParameter< vector<double> >("MinPt",defminptcut);
27  vector<double> defminetacut;
28  defminetacut.push_back(-10.);
29  minetacut = iConfig.getUntrackedParameter< vector<double> >("MinEta",defminetacut);
30  vector<double> defmaxetacut;
31  defmaxetacut.push_back(10.);
32  maxetacut = iConfig.getUntrackedParameter< vector<double> >("MaxEta",defmaxetacut);
33 
34  edm::LogInfo("PythiaDauVFilterMatchID") << "----------------------------------------------------------------------" << endl;
35  edm::LogInfo("PythiaDauVFilterMatchID") << "--- PythiaDauVFilterMatchID" << endl;
36  for (unsigned int i=0; i<dauIDs.size(); ++i) {
37  edm::LogInfo("PythiaDauVFilterMatchID") << "ID: " << dauIDs[i] << " pT > " << minptcut[i] << " " << minetacut[i] << " eta < " << maxetacut[i] << endl;
38  }
39  edm::LogInfo("PythiaDauVFilterMatchID") << "maxptcut = " << maxptcut << endl;
40  edm::LogInfo("PythiaDauVFilterMatchID") << "particleID = " << particleID << endl;
41  edm::LogInfo("PythiaDauVFilterMatchID") << "motherID = " << motherID << endl;
42 
43  // create pythia8 instance to access particle data
44  edm::LogInfo("PythiaDauVFilterMatchID") << "Creating pythia8 instance for particle properties" << endl;
45  if(!fLookupGen.get()) fLookupGen.reset(new Pythia());
46 }
47 
48 
50 {
51 
52  // do anything here that needs to be done at desctruction time
53  // (e.g. close files, deallocate resources etc.)
54 
55 }
56 
57 
58 //
59 // member functions
60 //
61 
62 // ------------ method called to produce the data ------------
64  using namespace edm;
65  bool accepted = false;
67  iEvent.getByToken(token_, evt);
68 
69  int OK(1);
70  vector<int> vparticles;
71 
72  HepMC::GenEvent *myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
73 
74  if (fVerbose > 5) {
75  edm::LogInfo("PythiaDauVFilterMatchID") << "looking for " << particleID << endl;
76  }
77 
78  for (HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
79 
80  if ((*p)->pdg_id() != particleID) continue ;
81 
82  // -- Check for mother of this particle
83  if (0 != motherID) {
84  OK = 0;
85  for (HepMC::GenVertex::particles_in_const_iterator des = (*p)->production_vertex()->particles_in_const_begin();
86  des != (*p)->production_vertex()->particles_in_const_end();
87  ++des) {
88  if (fVerbose > 10) {
89  edm::LogInfo("PythiaDauVFilterMatchID") << "mother: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp() << " eta: " << (*des)->momentum().eta() << endl;
90  }
91  if (abs(motherID) == abs((*des)->pdg_id())) {
92  OK = 1;
93  break;
94  }
95  }
96  }
97  if (0 == OK) continue;
98 
99  //generate targets
100  std::vector<decayTarget> targets;
101  for (unsigned int i=0;i<dauIDs.size();i++)
102  {
104  target.pdgID = dauIDs[i];
105  target.minPt = minptcut[i];
106  target.maxPt = maxptcut;
107  target.minEta = minetacut[i];
108  target.maxEta = maxetacut[i];
109  targets.push_back(target);
110  }
111  if (fVerbose>10)
112  {
113  edm::LogInfo("PythiaDauVFilterMatchID") << "created target: ";
114  for (unsigned int i=0;i<targets.size();i++) {edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";}
115  edm::LogInfo("PythiaDauVFilterMatchID") << endl;
116  }
117 
118  // -- check for daugthers
119  int ndau = 0;
120  if (fVerbose > 5) {
121  edm::LogInfo("PythiaDauVFilterMatchID") << "found ID: " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp() << " eta: " << (*p)->momentum().eta() << endl;
122  }
123  vparticles.push_back((*p)->pdg_id());
124  if ((*p)->end_vertex()) {
125  for (HepMC::GenVertex::particle_iterator des=(*p)->end_vertex()->particles_begin(HepMC::children);
126  des != (*p)->end_vertex()->particles_end(HepMC::children);
127  ++des) {
128  if ( TMath::Abs((*des)->pdg_id()) == 22 ) {continue;}
129  ++ndau;
130  if (fVerbose > 5) {
131  edm::LogInfo("PythiaDauVFilterMatchID") << "ID: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp() << " eta: " << (*des)->momentum().eta() << endl;
132  }
133  { // protect matchedIdx
134  int matchedIdx=-1;
135  for (unsigned int i=0;i<targets.size();i++)
136  {
137  if ( (*des)->pdg_id() != targets[i].pdgID ) {continue;}
138  if (fVerbose>5) {edm::LogInfo("PythiaDauVFilterMatchID") << "i = " << i << " pT = " << (*des)->momentum().perp() << " eta = " << (*des)->momentum().eta() << endl;}
139 
140  if ((*des)->momentum().perp() > targets[i].minPt &&
141  (*des)->momentum().perp() < targets[i].maxPt &&
142  (*des)->momentum().eta() > targets[i].minEta &&
143  (*des)->momentum().eta() < targets[i].maxEta )
144  {
145  vparticles.push_back((*des)->pdg_id());
146  if (fVerbose > 2) {
147  edm::LogInfo("PythiaDauVFilterMatchID") << " accepted this particle " << (*des)->pdg_id()
148  << " pT = " << (*des)->momentum().perp() << " eta = " << (*des)->momentum().eta() << endl;
149  }
150  matchedIdx=i;
151  break;
152  }
153  }
154  if (matchedIdx>-1) {targets.erase(targets.begin()+matchedIdx);}
155  if (fVerbose>10)
156  {
157  edm::LogInfo("PythiaDauVFilterMatchID") << "remaining targets: ";
158  for (unsigned int i=0;i<targets.size();i++) {edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";}
159  edm::LogInfo("PythiaDauVFilterMatchID") << endl;
160  }
161  }
162  }
163  }
164 
165  if (ndau == ndaughters && targets.size() == 0 )
166  {
167  accepted=true;
168  if (fVerbose > 0) {
169  edm::LogInfo("PythiaDauVFilterMatchID") << " accepted this decay: ";
170  for (unsigned int iv = 0; iv < vparticles.size(); ++iv) edm::LogInfo("PythiaDauVFilterMatchID") << vparticles[iv] << " ";
171  edm::LogInfo("PythiaDauVFilterMatchID") << " from mother = " << motherID << endl;
172  }
173  break;
174  }
175 
176  }
177 
178 
179  int anti_particleID = -particleID;
180  if ( !(fLookupGen->particleData.isParticle( anti_particleID )) ) {
181  anti_particleID = 0;
182  if (fVerbose > 5) edm::LogInfo("PythiaDauVFilterMatchID") << "Particle " << particleID << " is its own anti-particle, skipping further testing " << endl;
183  }
184  if (!accepted && chargeconju && anti_particleID) {
185  OK = 1;
186 
187  for (HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin();
188  p != myGenEvent->particles_end(); ++p) {
189 
190  if ((*p)->pdg_id() != anti_particleID) continue ;
191 
192  // -- Check for mother of this particle
193  if (0 != motherID) {
194  OK = 0;
195  for (HepMC::GenVertex::particles_in_const_iterator des = (*p)->production_vertex()->particles_in_const_begin();
196  des != (*p)->production_vertex()->particles_in_const_end();
197  ++des) {
198  if (fVerbose > 10) {
199  edm::LogInfo("PythiaDauVFilterMatchID") << "mother: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp() << " eta: " << (*des)->momentum().eta() << endl;
200  }
201  if (abs(motherID) == abs((*des)->pdg_id())) {
202  OK = 1;
203  break;
204  }
205  }
206  }
207  if (0 == OK) continue;
208 
209  //generate anti targets
210  std::vector<decayTarget> targets;
211  for (unsigned int i=0;i<dauIDs.size();i++)
212  {
214  int IDanti = -dauIDs[i];
215  if ( !(fLookupGen->particleData.isParticle( IDanti )) ) IDanti = dauIDs[i];
216  target.pdgID = IDanti;
217  target.minPt = minptcut[i];
218  target.maxPt = maxptcut;
219  target.minEta = minetacut[i];
220  target.maxEta = maxetacut[i];
221  targets.push_back(target);
222  }
223  if (fVerbose>10)
224  {
225  edm::LogInfo("PythiaDauVFilterMatchID") << "created anti target: ";
226  for (unsigned int i=0;i<targets.size();i++) {edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";}
227  edm::LogInfo("PythiaDauVFilterMatchID") << endl;
228  }
229 
230  if (fVerbose > 5) {
231  edm::LogInfo("PythiaDauVFilterMatchID") << "found ID: " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp() << " eta: " << (*p)->momentum().eta() << endl;
232  }
233  vparticles.push_back((*p)->pdg_id());
234  int ndau = 0;
235  if ((*p)->end_vertex()) {
236  for (HepMC::GenVertex::particle_iterator des=(*p)->end_vertex()->particles_begin(HepMC::children);
237  des != (*p)->end_vertex()->particles_end(HepMC::children);
238  ++des) {
239  if ( TMath::Abs((*des)->pdg_id()) == 22 ) {continue;}
240  ++ndau;
241  if (fVerbose > 5) {
242  edm::LogInfo("PythiaDauVFilterMatchID") << "ID: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp() << " eta: " << (*des)->momentum().eta() << endl;
243  }
244  { // protect matchedIdx
245  int matchedIdx=-1;
246  for (unsigned int i=0;i<targets.size();i++)
247  {
248  if ( (*des)->pdg_id() != targets[i].pdgID ) {continue;}
249  if (fVerbose>5) {edm::LogInfo("PythiaDauVFilterMatchID") << "i = " << i << " pT = " << (*des)->momentum().perp() << " eta = " << (*des)->momentum().eta() << endl;}
250 
251  if ((*des)->momentum().perp() > targets[i].minPt &&
252  (*des)->momentum().perp() < targets[i].maxPt &&
253  (*des)->momentum().eta() > targets[i].minEta &&
254  (*des)->momentum().eta() < targets[i].maxEta )
255  {
256  vparticles.push_back((*des)->pdg_id());
257  if (fVerbose > 2) {
258  edm::LogInfo("PythiaDauVFilterMatchID") << " accepted this particle " << (*des)->pdg_id()
259  << " pT = " << (*des)->momentum().perp() << " eta = " << (*des)->momentum().eta() << endl;
260  }
261  matchedIdx=i;
262  break;
263  }
264  }
265  if (matchedIdx>-1) {targets.erase(targets.begin()+matchedIdx);}
266  if (fVerbose>10)
267  {
268  edm::LogInfo("PythiaDauVFilterMatchID") << "remaining targets: ";
269  for (unsigned int i=0;i<targets.size();i++) {edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";}
270  edm::LogInfo("PythiaDauVFilterMatchID") << endl;
271  }
272  }
273  }
274  }
275  if (ndau == ndaughters && targets.size() == 0 )
276  {
277  accepted=true;
278  if (fVerbose > 0) {
279  edm::LogInfo("PythiaDauVFilterMatchID") << " accepted this decay: ";
280  for (unsigned int iv = 0; iv < vparticles.size(); ++iv) edm::LogInfo("PythiaDauVFilterMatchID") << vparticles[iv] << " ";
281  edm::LogInfo("PythiaDauVFilterMatchID") << " from mother = " << motherID << endl;
282  }
283  break;
284  }
285  }
286 
287  }
288 
289  delete myGenEvent;
290  return accepted;
291 
292 }
std::unique_ptr< Pythia8::Pythia > fLookupGen
T getUntrackedParameter(std::string const &, T const &) const
std::vector< double > minetacut
std::vector< double > maxetacut
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< double > minptcut
const edm::EDGetTokenT< edm::HepMCProduct > token_
int iEvent
Definition: GenABIO.cc:224
PythiaDauVFilterMatchID(const edm::ParameterSet &)
T Abs(T a)
Definition: MathUtil.h:49
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool filter(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
bool accepted(std::vector< std::string_view > const &, std::string_view)
HLT enums.