CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
InputGenJetsParticleSelector.cc
Go to the documentation of this file.
1 /* \class GenJetInputParticleSelector
2 *
3 * Selects particles that are used as input for the GenJet collection.
4 * Logic: select all stable particles, except for particles specified in
5 * the config file that come from
6 * W,Z and H decays, and except for a special list, which can be used for
7 * unvisible BSM-particles.
8 * It is also possible to only selected the partonic final state,
9 * which means all particles before the hadronization step.
10 *
11 * The algorithm is based on code of Christophe Saout.
12 *
13 * Usage: [example for no resonance from nu an mu, and deselect invisible BSM
14 * particles ]
15 *
16 * module genJetParticles = InputGenJetsParticleSelector {
17 * InputTag src = "genParticles"
18 * bool partonicFinalState = false
19 * bool excludeResonances = true
20 * vuint32 excludeFromResonancePids = {13,12,14,16}
21 * bool tausAsJets = false
22 * vuint32 ignoreParticleIDs = { 1000022, 2000012, 2000014,
23 * 2000016, 1000039, 5000039,
24 * 4000012, 9900012, 9900014,
25 * 9900016, 39}
26 * }
27 *
28 *
29 * \author: Christophe Saout, Andreas Oehler
30 *
31 */
32 
36 //#include <iostream>
37 #include <memory>
39 
40 using namespace std;
41 
43  inTag(params.getParameter<edm::InputTag>("src")),
44  partonicFinalState(params.getParameter<bool>("partonicFinalState")),
45  excludeResonances(params.getParameter<bool>("excludeResonances")),
46  tausAsJets(params.getParameter<bool>("tausAsJets")),
47  ptMin(0.0){
48  if (params.exists("ignoreParticleIDs"))
49  setIgnoredParticles(params.getParameter<std::vector<unsigned int> >
50  ("ignoreParticleIDs"));
51  setExcludeFromResonancePids(params.getParameter<std::vector<unsigned int> >
52  ("excludeFromResonancePids"));
53 
54  produces <reco::GenParticleRefVector> ();
55 
56 }
57 
59 
60 void InputGenJetsParticleSelector::setIgnoredParticles(const std::vector<unsigned int> &particleIDs)
61 {
62  ignoreParticleIDs = particleIDs;
64 }
65 
66 void InputGenJetsParticleSelector::setExcludeFromResonancePids(const std::vector<unsigned int> &particleIDs)
67 {
68  excludeFromResonancePids = particleIDs;
70 }
71 
73  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
74  return (pdgId > 0 && pdgId < 6) || pdgId == 7 ||
75  pdgId == 9 || (tausAsJets && pdgId == 15) || pdgId == 21;
76  // tops are not considered "regular" partons
77  // but taus eventually are (since they may hadronize later)
78 }
79 
81 {
82  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
83  return (pdgId > 100 && pdgId < 900) ||
84  (pdgId > 1000 && pdgId < 9000);
85 }
86 
88 {
89  // gauge bosons and tops
90  pdgId = (pdgId > 0 ? pdgId : -pdgId) % 10000;
91  return (pdgId > 21 && pdgId <= 42) || pdgId == 6 || pdgId == 8 ; //BUG! war 21. 22=gamma..
92 }
93 
95 {
96  pdgId = pdgId > 0 ? pdgId : -pdgId;
97  std::vector<unsigned int>::const_iterator pos =
98  std::lower_bound(ignoreParticleIDs.begin(),
99  ignoreParticleIDs.end(),
100  (unsigned int)pdgId);
101  return pos != ignoreParticleIDs.end() && *pos == (unsigned int)pdgId;
102 }
103 
105 {
106  pdgId = pdgId > 0 ? pdgId : -pdgId;
107  std::vector<unsigned int>::const_iterator pos =
108  std::lower_bound(excludeFromResonancePids.begin(),
110  (unsigned int)pdgId);
111  return pos != excludeFromResonancePids.end() && *pos == (unsigned int)pdgId;
112 
113 }
114 
116  //const reco::GenParticle *particle)
117  const reco::GenParticle *particle)
118 {
119  InputGenJetsParticleSelector::ParticleVector::const_iterator pos =
120  std::lower_bound(p.begin(), p.end(), particle);
121  if (pos == p.end() || *pos != particle)
122  throw cms::Exception("CorruptedData")
123  << "reco::GenEvent corrupted: Unlisted particles"
124  " in decay tree." << std::endl;
125 
126  return pos - p.begin();
127 }
128 
131  const reco::GenParticle *particle)
132 {
133  unsigned int npart=particle->numberOfDaughters();
134  if (!npart) return;
135 
136  for (unsigned int i=0;i<npart;++i){
137  unsigned int idx=partIdx(p,dynamic_cast<const reco::GenParticle*>(particle->daughter(i)));
138  if (invalid[idx])
139  continue;
140  invalid[idx] = true;
141  //cout<<"Invalidated: ["<<setw(4)<<idx<<"] With pt:"<<particle->daughter(i)->pt()<<endl;
142  invalidateTree(invalid, p, dynamic_cast<const reco::GenParticle*>(particle->daughter(i)));
143  }
144 }
145 
146 
150  const reco::GenParticle *particle) const
151 {
152  unsigned int npart=particle->numberOfDaughters();
153  if (!npart) {return 0;}
154 
155  for (unsigned int i=0;i<npart;++i){
156  unsigned int idx = partIdx(p,dynamic_cast<const reco::GenParticle*>(particle->daughter(i)));
157  if (invalid[idx])
158  continue;
159  if (isParton((particle->daughter(i)->pdgId()))){
160  return 1;
161  }
162  if (isHadron((particle->daughter(i)->pdgId()))){
163  return -1;
164  }
165  int result = testPartonChildren(invalid,p,dynamic_cast<const reco::GenParticle*>(particle->daughter(i)));
166  if (result) return result;
167  }
168  return 0;
169 }
170 
173  const ParticleVector &p,
174  const reco::GenParticle *particle) const
175 {
176  unsigned int idx = partIdx(p, particle);
177  int id = particle->pdgId();
178 
179  if (invalid[idx]) return kIndirect;
180 
181  if (isResonance(id) && particle->status() == 3){
182  return kDirect;
183  }
184 
185 
186  if (!isIgnored(id) && (isParton(id)))
187  return kNo;
188 
189 
190 
191  unsigned int nMo=particle->numberOfMothers();
192  if (!nMo)
193  return kNo;
194 
195 
196  for(unsigned int i=0;i<nMo;++i){
197  ResonanceState result = fromResonance(invalid,p,dynamic_cast<const reco::GenParticle*>(particle->mother(i)));
198  switch(result) {
199  case kNo:
200  break;
201  case kDirect:
202  if (dynamic_cast<const reco::GenParticle*>(particle->mother(i))->pdgId()==id || isResonance(id))
203  return kDirect;
204  if(!isExcludedFromResonance(id))
205  break;
206  case kIndirect:
207  return kIndirect;
208  }
209  }
210 return kNo;
211 }
212 
213 
215  const ParticleVector &p,
216  const reco::GenParticle *particle) const {
217  return testPartonChildren(invalid, p, particle) > 0;
218 }
219 
220 //######################################################
221 //function NEEDED and called per EVENT by FRAMEWORK:
223 
224 
225  std::auto_ptr<reco::GenParticleRefVector> selected_ (new reco::GenParticleRefVector);
226 
228  // evt.getByLabel("genParticles", genParticles );
229  evt.getByLabel(inTag, genParticles );
230 
231 
232  ParticleVector particles;
233  for (reco::GenParticleCollection::const_iterator iter=genParticles->begin();iter!=genParticles->end();++iter){
234  particles.push_back(&*iter);
235  }
236 
237  std::sort(particles.begin(), particles.end());
238  unsigned int size = particles.size();
239 
240  ParticleBitmap selected(size, false);
241  ParticleBitmap invalid(size, false);
242 
243  for(unsigned int i = 0; i < size; i++) {
244  const reco::GenParticle *particle = particles[i];
245  if (invalid[i])
246  continue;
247  if (particle->status() == 1)
248  selected[i] = true;
249  if (partonicFinalState && isParton(particle->pdgId())) {
250 
251  if (particle->numberOfDaughters()==0 &&
252  particle->status() != 1) {
253  // some brokenness in event...
254  invalid[i] = true;
255  }
256  else if (!hasPartonChildren(invalid, particles,
257  particle)) {
258  selected[i] = true;
259  invalidateTree(invalid, particles,particle); //this?!?
260  }
261  }
262 
263  }
264  unsigned int count=0;
265  for(size_t idx=0;idx<genParticles->size();++idx){
266  const reco::GenParticle *particle = particles[idx];
267  if (!selected[idx] || invalid[idx]){
268  continue;
269  }
270 
271  if (excludeResonances &&
272  fromResonance(invalid, particles, particle)) {
273  invalid[idx] = true;
274  //cout<<"[INPUTSELECTOR] Invalidates FROM RESONANCE!: ["<<setw(4)<<idx<<"] "<<particle->pdgId()<<" "<<particle->pt()<<endl;
275  continue;
276  }
277 
278  if (isIgnored(particle->pdgId())){
279  continue;
280  }
281 
282 
283  if (particle->pt() >= ptMin){
284  edm::Ref<reco::GenParticleCollection> particleRef(genParticles,idx);
285  selected_->push_back(particleRef);
286  //cout<<"Finally we have: ["<<setw(4)<<idx<<"] "<<setw(4)<<particle->pdgId()<<" "<<particle->pt()<<endl;
287  count++;
288  }
289  }
290  evt.put(selected_);
291 }
292 
293 
294 
295 //define this as a plug-in
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool hasPartonChildren(ParticleBitmap &invalid, const ParticleVector &p, const reco::GenParticle *particle) const
std::vector< const reco::GenParticle * > ParticleVector
virtual int pdgId() const GCC11_FINAL
PDG identifier.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int testPartonChildren(ParticleBitmap &invalid, const ParticleVector &p, const reco::GenParticle *particle) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
double npart
Definition: HydjetWrapper.h:44
std::vector< unsigned int > excludeFromResonancePids
virtual void produce(edm::Event &evt, const edm::EventSetup &evtSetup)
void setIgnoredParticles(const std::vector< unsigned int > &particleIDs)
virtual int status() const GCC11_FINAL
status word
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
static void invalidateTree(InputGenJetsParticleSelector::ParticleBitmap &invalid, const InputGenJetsParticleSelector::ParticleVector &p, const reco::GenParticle *particle)
virtual size_t numberOfMothers() const
number of mothers
virtual size_t numberOfDaughters() const
number of daughters
tuple result
Definition: query.py:137
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
static unsigned int partIdx(const InputGenJetsParticleSelector::ParticleVector &p, const reco::GenParticle *particle)
virtual int pdgId() const =0
PDG identifier.
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
std::vector< unsigned int > ignoreParticleIDs
void setExcludeFromResonancePids(const std::vector< unsigned int > &particleIDs)
virtual float pt() const GCC11_FINAL
transverse momentum
virtual const Candidate * mother(size_type=0) const
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
tuple size
Write out results.
ResonanceState fromResonance(ParticleBitmap &invalid, const ParticleVector &p, const reco::GenParticle *particle) const