CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetInput.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <vector>
3 
4 #include <Math/GenVector/PxPyPzE4D.h>
5 
6 #include <HepMC/GenEvent.h>
7 #include <HepMC/GenParticle.h>
8 #include <HepMC/GenVertex.h>
9 
12 
14 
15 namespace lhef {
16 
18  partonicFinalState(false),
19  onlyHardProcess(false),
20  excludeResonances(false),
21  tausAsJets(false),
22  ptMin(0.0)
23 {
24 }
25 
27  partonicFinalState(params.getParameter<bool>("partonicFinalState")),
28  onlyHardProcess(params.getParameter<bool>("onlyHardProcess")),
29  excludeResonances(false),
30  tausAsJets(params.getParameter<bool>("tausAsJets")),
31  ptMin(0.0)
32 {
33  if (params.exists("ignoreParticleIDs"))
35  params.getParameter<std::vector<unsigned int> >(
36  "ignoreParticleIDs"));
37  if (params.exists("excludedResonances"))
39  params.getParameter<std::vector<unsigned int> >(
40  "excludedResonances"));
41  if (params.exists("excludedFromResonances"))
43  params.getParameter<std::vector<unsigned int> >(
44  "excludedFromResonances"));
45 }
46 
48 {
49 }
50 
52  const std::vector<unsigned int> &particleIDs)
53 {
54  ignoreParticleIDs = particleIDs;
56 }
57 
59  const std::vector<unsigned int> &particleIDs)
60 {
62  excludedResonances = particleIDs;
64 }
65 
67  const std::vector<unsigned int> &particleIDs)
68 {
69  excludedFromResonances = particleIDs;
71 }
72 
73 bool JetInput::isParton(int pdgId) const
74 {
75  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
76  return (pdgId > 0 && pdgId < 6) || pdgId == 7 ||
77  pdgId == 9 || (tausAsJets && pdgId == 15) || pdgId == 21;
78  // tops are not considered "regular" partons
79  // but taus eventually are (since they may hadronize later)
80 }
81 
83 {
84  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
85  return (pdgId > 100 && pdgId < 900) ||
86  (pdgId > 1000 && pdgId < 9000);
87 }
88 
89 static inline bool isContained(const std::vector<unsigned int> &list, int id)
90 {
91  unsigned int absId = (unsigned int)(id > 0 ? id : -id);
92  std::vector<unsigned int>::const_iterator pos =
93  std::lower_bound(list.begin(), list.end(), absId);
94  return pos != list.end() && *pos == absId;
95 }
96 
98 {
99  if (excludedResonances.empty()) {
100  // gauge bosons and tops
101  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
102  return (pdgId > 21 && pdgId <= 39) || pdgId == 6 || pdgId == 8;
103  }
104 
105  return isContained(excludedResonances, pdgId);
106 }
107 
108 bool JetInput::isIgnored(int pdgId) const
109 {
110  return isContained(ignoreParticleIDs, pdgId);
111 }
112 
114 {
115  if (excludedFromResonances.empty())
116  return true;
117 
118  return isContained(excludedFromResonances, pdgId);
119 }
120 
121 bool JetInput::isHardProcess(const HepMC::GenVertex *vertex,
122  const VertexVector &hardProcess) const
123 {
124  std::vector<const HepMC::GenVertex*>::const_iterator pos =
125  std::lower_bound(hardProcess.begin(),
126  hardProcess.end(), vertex);
127  return pos != hardProcess.end() && *pos == vertex;
128 }
129 
130 static unsigned int partIdx(const JetInput::ParticleVector &p,
131  const HepMC::GenParticle *particle)
132 {
133  JetInput::ParticleVector::const_iterator pos =
134  std::lower_bound(p.begin(), p.end(), particle);
135  if (pos == p.end() || *pos != particle)
136  throw cms::Exception("CorruptedData")
137  << "HepMC::GenEvent corrupted: Unlisted particles"
138  " in decay tree." << std::endl;
139 
140  return pos - p.begin();
141 }
142 
145  const HepMC::GenVertex *v)
146 {
147  if (!v)
148  return;
149 
150  for(HepMC::GenVertex::particles_out_const_iterator iter =
151  v->particles_out_const_begin();
152  iter != v->particles_out_const_end(); ++iter) {
153  unsigned int idx = partIdx(p, *iter);
154 
155  if (invalid[idx])
156  continue;
157 
158  invalid[idx] = true;
159 
160  const HepMC::GenVertex *v = (*iter)->end_vertex();
161  invalidateTree(invalid, p, v);
162  }
163 }
164 
167  const HepMC::GenVertex *v) const
168 {
169  if (!v)
170  return 0;
171 
172  for(HepMC::GenVertex::particles_out_const_iterator iter =
173  v->particles_out_const_begin();
174  iter != v->particles_out_const_end(); ++iter) {
175  unsigned int idx = partIdx(p, *iter);
176 
177  if (invalid[idx])
178  continue;
179 
180  if (isParton((*iter)->pdg_id()))
181  return 1;
182  if (isHadron((*iter)->pdg_id()))
183  return -1;
184 
185  const HepMC::GenVertex *v = (*iter)->end_vertex();
186  int result = testPartonChildren(invalid, p, v);
187  if (result)
188  return result;
189  }
190 
191  return 0;
192 }
193 
195  const ParticleVector &p,
196  const HepMC::GenParticle *particle) const
197 {
198  return testPartonChildren(invalid, p, particle->end_vertex()) > 0;
199 }
200 
202  const ParticleVector &p,
203  const HepMC::GenParticle *particle,
204  const VertexVector &hardProcess) const
205 {
206  unsigned int idx = partIdx(p, particle);
207 
208  if (invalid[idx])
209  return false;
210 
211  const HepMC::GenVertex *v = particle->production_vertex();
212  if (!v)
213  return false;
214  else if (isHardProcess(v, hardProcess))
215  return true;
216 
217  for(HepMC::GenVertex::particles_in_const_iterator iter =
218  v->particles_in_const_begin();
219  iter != v->particles_in_const_end(); ++iter)
220  if (fromHardProcess(invalid, p, *iter, hardProcess))
221  return true;
222 
223  return false;
224 }
225 
227  const ParticleVector &p,
228  const HepMC::GenParticle *particle,
229  const HepMC::GenVertex *signalVertex) const
230 {
231  unsigned int idx = partIdx(p, particle);
232 
233  if (invalid[idx])
234  return false;
235 
236  const HepMC::GenVertex *v = particle->production_vertex();
237  if (!v)
238  return false;
239  else if (v == signalVertex)
240  return true;
241 
242  for(HepMC::GenVertex::particles_in_const_iterator iter =
243  v->particles_in_const_begin();
244  iter != v->particles_in_const_end(); ++iter)
245  if (fromSignalVertex(invalid, p, *iter, signalVertex))
246  return true;
247 
248  return false;
249 }
250 
253  const ParticleVector &p,
254  const HepMC::GenParticle *particle,
255  const HepMC::GenVertex *signalVertex,
256  const VertexVector &hardProcess) const
257 {
258  unsigned int idx = partIdx(p, particle);
259  int id = particle->pdg_id();
260 
261  if (invalid[idx])
262  return kIndirect;
263 
264  if (particle->status() == 3 && isResonance(id))
265  return kDirect;
266 
267  if (!isIgnored(id) && excludedFromResonances.empty() && isParton(id))
268  return kNo;
269 
270  const HepMC::GenVertex *v = particle->production_vertex();
271  if (!v)
272  return kNo;
273  else if (v == signalVertex && excludedResonances.empty())
274  return kIndirect;
275  else if (v == signalVertex || isHardProcess(v, hardProcess))
276  return kNo;
277 
278  for(HepMC::GenVertex::particles_in_const_iterator iter =
279  v->particles_in_const_begin();
280  iter != v->particles_in_const_end(); ++iter) {
282  fromResonance(invalid, p, *iter,
283  signalVertex, hardProcess);
284  switch(result) {
285  case kNo:
286  break;
287  case kDirect:
288  if ((*iter)->pdg_id() == id)
289  return kDirect;
290  if (!isExcludedFromResonances(id))
291  break;
292  case kIndirect:
293  return kIndirect;
294  }
295  }
296 
297  return kNo;
298 }
299 
301  const HepMC::GenEvent *event) const
302 {
303  if (!event->signal_process_vertex())
304  throw cms::Exception("InvalidHepMCEvent")
305  << "HepMC event is lacking signal vertex."
306  << std::endl;
307 
308  VertexVector hardProcess, toLookAt;
309  std::pair<HepMC::GenParticle*,HepMC::GenParticle*> beamParticles =
310  event->beam_particles();
311  toLookAt.push_back(event->signal_process_vertex());
312  while(!toLookAt.empty()) {
313  std::vector<const HepMC::GenVertex*> next;
314  for(std::vector<const HepMC::GenVertex*>::const_iterator v =
315  toLookAt.begin(); v != toLookAt.end(); ++v) {
316  if (!*v || isHardProcess(*v, hardProcess))
317  continue;
318 
319  bool veto = false;
320  for(HepMC::GenVertex::particles_in_const_iterator iter =
321  (*v)->particles_in_const_begin();
322  iter != (*v)->particles_in_const_end(); ++iter) {
323  if (*iter == beamParticles.first ||
324  *iter == beamParticles.second) {
325  veto = true;
326  break;
327  }
328  }
329  if (veto)
330  continue;
331 
332  hardProcess.push_back(*v);
333  std::sort(hardProcess.begin(), hardProcess.end());
334 
335  for(HepMC::GenVertex::particles_in_const_iterator iter =
336  (*v)->particles_in_const_begin();
337  iter != (*v)->particles_in_const_end(); ++iter) {
338  const HepMC::GenVertex *pv =
339  (*iter)->production_vertex();
340  if (pv)
341  next.push_back(pv);
342  }
343  }
344 
345  toLookAt = next;
346  std::sort(toLookAt.begin(), toLookAt.end());
347  }
348 
349  ParticleVector particles;
350  for(HepMC::GenEvent::particle_const_iterator iter = event->particles_begin();
351  iter != event->particles_end(); ++iter)
352  particles.push_back(*iter);
353 
354  std::sort(particles.begin(), particles.end());
355  unsigned int size = particles.size();
356 
357  ParticleBitmap selected(size, false);
358  ParticleBitmap invalid(size, false);
359 
360  for(unsigned int i = 0; i < size; i++) {
361  const HepMC::GenParticle *particle = particles[i];
362  if (invalid[i])
363  continue;
364  if (particle->status() == 1)
365  selected[i] = true;
366 
367  if (partonicFinalState && isParton(particle->pdg_id())) {
368  if (!particle->end_vertex() &&
369  particle->status() != 1)
370  // some brokenness in event...
371  invalid[i] = true;
372  else if (!hasPartonChildren(invalid, particles,
373  particle)) {
374  selected[i] = true;
375  invalidateTree(invalid, particles,
376  particle->end_vertex());
377  }
378  }
379 
380  if (onlyHardProcess &&
381  !fromHardProcess(invalid, particles,
382  particle, hardProcess) &&
383  !isHardProcess(particle->end_vertex(), hardProcess))
384  invalid[i] = true;
385  }
386 
388  for(unsigned int i = 0; i < size; i++) {
389  const HepMC::GenParticle *particle = particles[i];
390  if (!selected[i] || invalid[i])
391  continue;
392 
393  if (excludeResonances &&
394  fromResonance(invalid, particles, particle,
395  event->signal_process_vertex(),
396  hardProcess)) {
397  invalid[i] = true;
398  continue;
399  }
400 
401  if (isIgnored(particle->pdg_id()))
402  continue;
403 
404  if (particle->momentum().perp() >= ptMin)
405  result.push_back(particle);
406  }
407 
408  return result;
409 }
410 
411 } // namespace lhef
T getParameter(std::string const &) const
static unsigned int partIdx(const JetInput::ParticleVector &p, const HepMC::GenParticle *particle)
Definition: JetInput.cc:130
int i
Definition: DBlmapReader.cc:9
static bool isContained(const std::vector< unsigned int > &list, int id)
Definition: JetInput.cc:89
void setExcludeResonances(bool flag=true)
Definition: JetInput.h:43
std::vector< bool > ParticleBitmap
Definition: JetInput.h:16
bool isHardProcess(const HepMC::GenVertex *vertex, const VertexVector &hardProcess) const
Definition: JetInput.cc:121
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< unsigned int > excludedFromResonances
Definition: JetInput.h:87
bool fromSignalVertex(ParticleBitmap &invalid, const ParticleVector &p, const HepMC::GenParticle *particle, const HepMC::GenVertex *signalVertex) const
Definition: JetInput.cc:226
bool partonicFinalState
Definition: JetInput.h:88
bool excludeResonances
Definition: JetInput.h:90
bool fromHardProcess(ParticleBitmap &invalid, const ParticleVector &p, const HepMC::GenParticle *particle, const VertexVector &hardProcess) const
Definition: JetInput.cc:201
double ptMin
Definition: JetInput.h:92
bool isExcludedFromResonances(int pdgId) const
Definition: JetInput.cc:113
tuple result
Definition: query.py:137
static bool isHadron(int pdgId)
Definition: JetInput.cc:82
ParticleVector operator()(const HepMC::GenEvent *event) const
Definition: JetInput.cc:300
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void setExcludedFromResonances(const std::vector< unsigned int > &particleIds)
Definition: JetInput.cc:66
static void invalidateTree(JetInput::ParticleBitmap &invalid, const JetInput::ParticleVector &p, const HepMC::GenVertex *v)
Definition: JetInput.cc:143
bool isParton(int pdgId) const
Definition: JetInput.cc:73
std::vector< unsigned int > ignoreParticleIDs
Definition: JetInput.h:85
bool onlyHardProcess
Definition: JetInput.h:89
bool isResonance(int pdgId) const
Definition: JetInput.cc:97
ResonanceState fromResonance(ParticleBitmap &invalid, const ParticleVector &p, const HepMC::GenParticle *particle, const HepMC::GenVertex *signalVertex, const VertexVector &hardProcess) const
Definition: JetInput.cc:252
std::vector< const HepMC::GenParticle * > ParticleVector
Definition: JetInput.h:17
int testPartonChildren(ParticleBitmap &invalid, const ParticleVector &p, const HepMC::GenVertex *v) const
Definition: JetInput.cc:165
std::vector< const HepMC::GenVertex * > VertexVector
Definition: JetInput.h:18
bool tausAsJets
Definition: JetInput.h:91
void setIgnoredParticles(const std::vector< unsigned int > &particleIDs)
Definition: JetInput.cc:51
bool hasPartonChildren(ParticleBitmap &invalid, const ParticleVector &p, const HepMC::GenParticle *particle) const
Definition: JetInput.cc:194
void setExcludedResonances(const std::vector< unsigned int > &particleIds)
Definition: JetInput.cc:58
bool isIgnored(int pdgId) const
Definition: JetInput.cc:108
std::vector< unsigned int > excludedResonances
Definition: JetInput.h:86
tuple size
Write out results.
mathSSE::Vec4< T > v
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run