CMS 3D CMS Logo

JetPartonMatcher.cc
Go to the documentation of this file.
1 //
2 // Translation of BTag MCJetFlavour tool to identify real flavour of a jet
3 // work with CaloJet objects
4 // Store Infos by Values in JetFlavour.h
5 // Author: Attilio
6 // Date: 05.10.2007
7 //
8 //
9 // \class JetPartonMatcher
10 //
11 // \brief Interface to create a specified "matched" parton to jet match based
12 // on the user interpretation.
13 //
14 // Algorithm for definitions:
15 //
16 // If the particle is matched to any item in the priority list (given by user),
17 // then count that prioritized particle as the match.
18 //
19 // If not resort to default behavior:
20 //
21 //
22 // 1) Algorithmic Definition:
23 //
24 // A particle is a parton if its daughter is a string(code=92) or a cluster(code=93)
25 // If (parton is within the cone defined by coneSizeToAssociate) then:
26 // if (parton is a b) then associatedParton is the b
27 // else if (associatedParton =! b and parton is a c) then associatedParton is the c
28 // else if (associatedParton =! b and associatedParton =! c) then associatedParton is the one with the highest pT
29 // associatedParton can be -1 --> no partons in the cone
30 // True Flavour of the jet --> flavour of the associatedParton
31 //
32 // ToDo: if more than one b(c) in the cone --> the selected parton is not always the one with highest pT
33 //
34 //
35 // 2) Physics Definition:
36 //
37 // A particle is a parton if its daughter is a string(code=92) or a cluster(code=93)
38 // if( only one initialParticle within the cone defined by coneSizeToAssociate) associatedInitialParticle is the initialParticle
39 // TheBiggerConeSize = 0.7 --> it's hard coded!
40 // if( a parton not coming from associatedInitialParticle in TheBiggerConeSize is a b or a c) reject the association
41 //
42 // associatedInitialParticle can be -1 --> no initialParticle in the cone or rejected association
43 // True Flavour of the jet --> flavour of the associatedInitialParticle
44 //
45 //
46 // Modifications:
47 //
48 // 09.03.2008: Sal Rappoccio.
49 // Added capability for "priority" list.
50 //
51 
52 //=======================================================================
53 
54 // user include files
59 
65 
73 
77 //#include "DataFormats/Candidate/interface/Candidate.h"
78 //#include "DataFormats/Candidate/interface/CandidateFwd.h"
81 
82 #include <memory>
83 #include <string>
84 #include <iostream>
85 #include <vector>
86 #include <Math/VectorUtil.h>
87 #include <TMath.h>
88 
89 using namespace std;
90 using namespace reco;
91 using namespace edm;
92 using namespace ROOT::Math::VectorUtil;
93 
95 {
96  public:
99 
100  private:
101  virtual void produce(edm::StreamID, edm::Event&, const edm::EventSetup& ) const override;
102 
105  int theHeaviest = 0;
106  int theNearest2 =0;
107  int theNearest3 = 0;
108  int theHardest = 0;
109  };
110 
111  int fillAlgoritDefinition( const Jet&, WorkingVariables& ) const;
112  int fillPhysicsDefinition( const Jet&, WorkingVariables& ) const;
113 
119  vector<int> priorityList;
120 
121 };
122 
123 //=========================================================================
124 
126 {
127  produces<JetMatchedPartonsCollection>();
128  m_jetsSrcToken = consumes<edm::View <reco::Jet> >(iConfig.getParameter<edm::InputTag>("jets"));
129  m_ParticleSrcToken = consumes<GenParticleRefVector>(iConfig.getParameter<edm::InputTag>("partons"));
130  coneSizeToAssociate = iConfig.getParameter<double>("coneSizeToAssociate");
131  if ( iConfig.exists("doPriority") ) {
132  doPriority = iConfig.getParameter<bool>("doPriority");
133  priorityList = iConfig.getParameter<vector<int> >("priorityList");
134  } else {
135  doPriority = false;
136  priorityList.clear();
137  }
138 }
139 
140 //=========================================================================
141 
143 {
144 }
145 
146 // ------------ method called to produce the data ------------
147 
149 {
150  WorkingVariables wv;
152  iEvent.getByToken(m_jetsSrcToken, jets_h );
153  iEvent.getByToken(m_ParticleSrcToken, wv.particles );
154 
155  edm::LogVerbatim("JetPartonMatcher") << "=== Partons size:" << wv.particles->size();
156 
157  for( size_t m = 0; m != wv.particles->size(); ++ m ) {
158  const GenParticle & aParton = *(wv.particles->at(m).get());
159  edm::LogVerbatim("JetPartonMatcher") << aParton.status() << " " <<
160  aParton.pdgId() << " " <<
161  aParton.pt() << " " <<
162  aParton.eta() << " " <<
163  aParton.phi() << endl;
164  }
165 
166  auto jetMatchedPartons = std::make_unique<JetMatchedPartonsCollection>(reco::JetRefBaseProd(jets_h));
167 
168  for (size_t j = 0; j < jets_h->size(); j++) {
169 
170  const int theMappedPartonAlg = fillAlgoritDefinition( (*jets_h)[j], wv );
171  const int theMappedPartonPhy = fillPhysicsDefinition( (*jets_h)[j], wv );
172 
173  GenParticleRef pHV;
174  GenParticleRef pN2;
175  GenParticleRef pN3;
176  GenParticleRef pPH;
177  GenParticleRef pAL;
178 
179  if(wv.theHeaviest>=0) pHV = wv.particles->at( wv.theHeaviest );
180  if(wv.theNearest2>=0) pN2 = wv.particles->at( wv.theNearest2 );
181  if(wv.theNearest3>=0) pN3 = wv.particles->at( wv.theNearest3 );
182  if(theMappedPartonPhy>=0) pPH = wv.particles->at( theMappedPartonPhy );
183  if(theMappedPartonAlg>=0) pAL = wv.particles->at( theMappedPartonAlg );
184 
185  (*jetMatchedPartons)[jets_h->refAt(j)]=MatchedPartons(pHV,pN2,pN3,pPH,pAL);
186  }
187 
188  iEvent.put(std::move(jetMatchedPartons));
189 
190 }
191 
192 //
193 // Algorithmic Definition:
194 // Output: define one associatedParton
195 // Loop on all particle.
196 //
197 // (Note: This part added by Salvatore Rappoccio)
198 // [begin]
199 // If the particle is matched to any item in the priority list (given by user),
200 // then count that prioritized particle as the match.
201 //
202 // If not resort to default behavior:
203 // [end]
204 //
205 // A particle is a parton if its daughter is a string(code=92) or a cluster(code=93)
206 // If (parton is within the cone defined by coneSizeToAssociate) then:
207 // if (parton is a b) then associatedParton is the b
208 // else if (associatedParton =! b and parton is a c) then associatedParton is the c
209 // else if (associatedParton =! b and associatedParton =! c) then associatedParton is the one with the highest pT
210 // associatedParton can be -1 --> no partons in the cone
211 // True Flavour of the jet --> flavour of the associatedParton
212 //
213 // ToDo: if more than one b(c) in the cone --> the selected parton is not always the one with highest pT
214 //
216 
217  int tempParticle = -1;
218  int tempPartonHighestPt = -1;
219  int tempNearest = -1;
220  float maxPt = 0;
221  float minDr = 1000;
222  bool foundPriority = false;
223 
224  // Loop over the particles in question until we find a priority
225  // "hit", or if we find none in the priority list (or we don't want
226  // to consider priority), then loop through all particles and fill
227  // standard definition.
228 
229  //
230  // Matching:
231  //
232  // 1) First try to match by hand. The "priority list" is given
233  // by the user. The algorithm finds any particles in that
234  // "priority list" that are within the specified cone size.
235  // If it finds one, it counts the object as associating to that
236  // particle.
237  // NOTE! The objects in the priority list are given in order
238  // of priority. So if the user specifies:
239  // 6, 24, 21,
240  // then it will first look for top quarks, then W bosons, then gluons.
241  // 2) If no priority items are found, do the default "standard"
242  // matching.
243  for( size_t m = 0; m != wv.particles->size() && !foundPriority; ++ m ) {
244  const Candidate & aParton = *(wv.particles->at(m).get());
245 
246  // "Priority" behavoir:
247  // Associate to the first particle found in the priority list, regardless
248  // of delta R.
249  if ( doPriority ) {
250  int ipdgId = aParton.pdgId();
251  vector<int>::const_iterator ipriority = find( priorityList.begin(), priorityList.end(), abs(ipdgId) );
252  // Check to see if this particle is in our priority list
253  if ( ipriority != priorityList.end() ) {
254  // This particle is on our priority list. If it matches,
255  // we break, since we take in order of priority, not deltaR
256  double dist = DeltaR( theJet.p4(), aParton.p4() );
257  if( dist <= coneSizeToAssociate ) {
258  tempParticle = m;
259  foundPriority = true;
260  }
261  }
262  }
263  // Here we do not want to do priority matching. Ensure the "foundPriority" swtich
264  // is turned off.
265  else {
266  foundPriority = false;
267  }
268 
269  // Default behavior:
270  // Look for partons before the color string to associate.
271  // Do this if we don't want to do priority matching, or if
272  // we didn't find a priority object.
273 
274  if( !foundPriority && aParton.status() != 3 ) {
275  double dist = DeltaR( theJet.p4(), aParton.p4() );
276  if( dist <= coneSizeToAssociate ) {
277  if( dist < minDr ) {
278  minDr = dist;
279  tempNearest = m;
280  }
281  if( tempParticle == -1 && ( abs( aParton.pdgId() ) == 4 ) ) tempParticle = m;
282  if( abs( aParton.pdgId() ) == 5 ) tempParticle = m;
283  if( aParton.pt() > maxPt ) {
284  maxPt = aParton.pt();
285  tempPartonHighestPt = m;
286  }
287  }
288  }
289  }
290 
291  if ( foundPriority ) {
292  wv.theHeaviest = tempParticle; // The priority-matched particle
293  wv.theHardest = -1; // set the rest to -1
294  wv.theNearest2 = -1; // " "
295  } else {
296  wv.theHeaviest = tempParticle;
297  wv.theHardest = tempPartonHighestPt;
298  wv.theNearest2 = tempNearest;
299  if ( tempParticle == -1 ) tempParticle = tempPartonHighestPt;
300  }
301  return tempParticle;
302 }
303 
304 //
305 // Physics Definition:
306 // A initialParticle is a particle with status=3
307 // Output: define one associatedInitialParticle
308 // Loop on all particles
309 //
310 // (Note: This part added by Salvatore Rappoccio)
311 // [begin]
312 // If the particle is matched to any item in the priority list (given by user),
313 // then count that prioritized particle as the match.
314 //
315 // If not resort to default behavior:
316 // [end]
317 //
318 // A particle is a parton if its daughter is a string(code=92) or a cluster(code=93)
319 // if( only one initialParticle within the cone defined by coneSizeToAssociate) associatedInitialParticle is the initialParticle
320 // TheBiggerConeSize = 0.7 --> it's hard coded!
321 // if( a parton not coming from associatedInitialParticle in TheBiggerConeSize is a b or a c) reject the association
322 //
323 // associatedInitialParticle can be -1 --> no initialParticle in the cone or rejected association
324 // True Flavour of the jet --> flavour of the associatedInitialParticle
325 //
327 
328  float TheBiggerConeSize = 0.7; // In HepMC it's 0.3 --> it's a mistake: value has to be 0.7
329  int tempParticle = -1;
330  int nInTheCone = 0;
331  int tempNearest = -1;
332  float minDr = 1000;
333  bool foundPriority = false;
334 
335  vector<const reco::Candidate *> theContaminations;
336  theContaminations.clear();
337 
338  // Loop over the particles in question until we find a priority
339  // "hit", or if we find none in the priority list (or we don't want
340  // to consider priority), then loop through all particles and fill
341  // standard definition.
342 
343  //
344  // Matching:
345  //
346  // 1) First try to match by hand. The "priority list" is given
347  // by the user. The algorithm finds any particles in that
348  // "priority list" that are within the specified cone size.
349  // If it finds one, it counts the object as associating to that
350  // particle.
351  // NOTE! The objects in the priority list are given in order
352  // of priority. So if the user specifies:
353  // 6, 24, 21,
354  // then it will first look for top quarks, then W bosons, then gluons.
355  // 2) If no priority items are found, do the default "standard"
356  // matching.
357  for( size_t m = 0; m != wv.particles->size() && !foundPriority; ++ m ) {
358 
359  const Candidate & aParticle = *(wv.particles->at(m).get());
360 
361  // "Priority" behavoir:
362  // Associate to the first particle found in the priority list, regardless
363  // of delta R.
364  if ( doPriority ) {
365  int ipdgId = aParticle.pdgId();
366  vector<int>::const_iterator ipriority = find( priorityList.begin(), priorityList.end(), abs(ipdgId) );
367  // Check to see if this particle is in our priority list
368  if ( ipriority != priorityList.end() ) {
369  // This particle is on our priority list. If it matches,
370  // we break, since we take in order of priority, not deltaR
371  double dist = DeltaR( theJet.p4(), aParticle.p4() );
372  if( dist <= coneSizeToAssociate ) {
373  tempParticle = m;
374  foundPriority = true;
375  }
376  }
377  }
378  // Here we do not want to do priority matching. Ensure the "foundPriority" swtich
379  // is turned off.
380  else {
381  foundPriority = false;
382  }
383 
384  // Default behavior:
385  if ( !foundPriority ) {
386 
387  // skipping all particle but udscbg (is this correct/enough?!?!)
388  bool isAParton = false;
389  int flavour = abs(aParticle.pdgId());
390  if(flavour == 1 ||
391  flavour == 2 ||
392  flavour == 3 ||
393  flavour == 4 ||
394  flavour == 5 ||
395  flavour == 21 ) isAParton = true;
396  if(!isAParton) continue;
397  double dist = DeltaR( theJet.p4(), aParticle.p4() );
398  if( aParticle.status() == 3 && dist < minDr ) {
399  minDr = dist;
400  tempNearest = m;
401  }
402  if( aParticle.status() == 3 && dist <= coneSizeToAssociate ) {
403  //cout << "particle in small cone=" << aParticle.pdgId() << endl;
404  tempParticle = m;
405  nInTheCone++;
406  }
407  // Look for heavy partons in TheBiggerConeSize now
408  if( aParticle.numberOfDaughters() > 0 && aParticle.status() != 3 ) {
409  if( flavour == 1 ||
410  flavour == 2 ||
411  flavour == 3 ||
412  flavour == 21 ) continue;
413  if( dist < TheBiggerConeSize ) theContaminations.push_back( &aParticle );
414  }
415  }
416  }
417 
418 
419  // Here's the default behavior for assignment if there is no priority.
420  if ( !foundPriority ) {
421  wv.theNearest3 = tempNearest;
422 
423  if(nInTheCone != 1) return -1; // rejected --> only one initialParton requested
424  if(theContaminations.size() == 0 ) return tempParticle; //no contamination
425  int initialPartonFlavour = abs( (wv.particles->at(tempParticle).get()) ->pdgId() );
426 
427  vector<const Candidate *>::const_iterator itCont = theContaminations.begin();
428  for( ; itCont != theContaminations.end(); itCont++ ) {
429  int contaminatingFlavour = abs( (*itCont)->pdgId() );
430  if( (*itCont)->numberOfMothers()>0 && (*itCont)->mother(0) == wv.particles->at(tempParticle).get() ) continue; // mother is the initialParton --> OK
431  if( initialPartonFlavour == 4 ) {
432  if( contaminatingFlavour == 4 ) continue; // keep association --> the initialParton is a c --> the contaminated parton is a c
433  tempParticle = -1; // all the other cases reject!
434  return tempParticle;
435  }
436  }
437  }
438  // If there is priority, then just set the heaviest to priority, the rest are -1.
439  else {
440  wv.theHeaviest = tempParticle; // Set the heaviest to tempParticle
441  wv.theNearest2 = -1; // Set the rest to -1
442  wv.theNearest3 = -1; // " "
443  wv.theHardest = -1; // " "
444  }
445 
446  return tempParticle;
447 }
448 
449 //define this as a plug-in
451 
T getParameter(std::string const &) const
virtual double pt() const final
transverse momentum
virtual void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
virtual double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int fillAlgoritDefinition(const Jet &, WorkingVariables &) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
virtual int status() const final
status word
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
virtual double phi() const final
momentum azimuthal angle
edm::RefToBaseProd< reco::Jet > JetRefBaseProd
Definition: JetCollection.h:14
virtual int status() const =0
status word
edm::EDGetTokenT< GenParticleRefVector > m_ParticleSrcToken
int iEvent
Definition: GenABIO.cc:230
Definition: Jet.py:1
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
virtual int pdgId() const =0
PDG identifier.
virtual int pdgId() const final
PDG identifier.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
JetPartonMatcher(const edm::ParameterSet &)
vector< int > priorityList
virtual double pt() const =0
transverse momentum
fixed size matrix
HLT enums.
int fillPhysicsDefinition(const Jet &, WorkingVariables &) const
edm::EDGetTokenT< edm::View< reco::Jet > > m_jetsSrcToken
virtual const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
virtual size_type numberOfDaughters() const =0
number of daughters
def move(src, dest)
Definition: eostools.py:510
Handle< GenParticleRefVector > particles