CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HadronAndPartonSelector.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HadronAndPartonSelector
4 // Class: HadronAndPartonSelector
5 //
37 //
38 // Original Author: Dinko Ferencek
39 // Created: Tue Nov 5 22:43:43 CET 2013
40 //
41 //
42 
43 
44 // system include files
45 #include <memory>
46 
47 // user include files
50 
53 
55 
69 
70 //
71 // constants, enums and typedefs
72 //
73 typedef boost::shared_ptr<BasePartonSelector> PartonSelectorPtr;
74 
75 //
76 // class declaration
77 //
78 
80  public:
83 
84  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
85 
86  private:
87  virtual void produce(edm::Event&, const edm::EventSetup&);
88 
89  // ----------member data ---------------------------
90  const edm::EDGetTokenT<GenEventInfoProduct> srcToken_; // To get handronizer module type
91  const edm::EDGetTokenT<reco::GenParticleCollection> particlesToken_; // Input GenParticle collection
92 
93  std::string partonMode_; // Parton selection mode
96 };
97 
98 //
99 // static data member definitions
100 //
101 
102 //
103 // constructors and destructor
104 //
106 
107  srcToken_(mayConsume<GenEventInfoProduct>( iConfig.getParameter<edm::InputTag>("src") )),
108  particlesToken_(consumes<reco::GenParticleCollection>( iConfig.getParameter<edm::InputTag>("particles") )),
109  partonMode_(iConfig.getParameter<std::string>("partonMode"))
110 
111 {
112  //register your products
113  produces<reco::GenParticleRefVector>( "bHadrons" );
114  produces<reco::GenParticleRefVector>( "cHadrons" );
115  produces<reco::GenParticleRefVector>( "algorithmicPartons" );
116  produces<reco::GenParticleRefVector>( "physicsPartons" );
117  produces<reco::GenParticleRefVector>( "leptons" );
118 
119  partonSelectorSet_=false;
120  partonSelector_=nullptr;
121 }
122 
123 
125 {
126 
127  // do anything here that needs to be done at desctruction time
128  // (e.g. close files, deallocate resources etc.)
129 
130 }
131 
132 
133 //
134 // member functions
135 //
136 
137 // ------------ method called to produce the data ------------
138 void
140 {
141  // determine hadronizer type (done only once per job)
142  if( partonMode_=="Auto" )
143  {
144  edm::Handle<GenEventInfoProduct> genEvtInfoProduct;
145  iEvent.getByToken(srcToken_, genEvtInfoProduct);
146 
147  std::string moduleName = "";
148  if( genEvtInfoProduct.isValid() ) {
149  const edm::Provenance& prov = iEvent.getProvenance(genEvtInfoProduct.id());
150  moduleName = edm::moduleName(prov);
151  }
152 
153  if( moduleName.find("Pythia6")!=std::string::npos )
154  partonMode_="Pythia6";
155  else if( moduleName.find("Pythia8")!=std::string::npos )
156  partonMode_="Pythia8";
157  else if( moduleName.find("Herwig6")!=std::string::npos )
158  partonMode_="Herwig6";
159  else if( moduleName.find("ThePEG")!=std::string::npos )
160  partonMode_="Herwig++";
161  else if( moduleName.find("Sherpa")!=std::string::npos )
162  partonMode_="Sherpa";
163  else
164  partonMode_="Undefined";
165  }
166 
167  // set the parton selection mode (done only once per job)
168  if( !partonSelectorSet_ )
169  {
170  if ( partonMode_=="Undefined" )
171  edm::LogWarning("UndefinedPartonMode") << "Could not automatically determine the hadronizer type and set the correct parton selection mode. Parton-based jet flavour will not be defined.";
172  else if ( partonMode_=="Pythia6" )
173  {
175  edm::LogInfo("PartonModeDefined") << "Using Pythia6 parton selection mode.";
176  }
177  else if ( partonMode_=="Pythia8" )
178  {
180  edm::LogInfo("PartonModeDefined") << "Using Pythia8 parton selection mode.";
181  }
182  else if ( partonMode_=="Herwig6" )
183  {
185  edm::LogInfo("PartonModeDefined") << "Using Herwig6 parton selection mode.";
186  }
187  else if ( partonMode_=="Herwig++" )
188  {
190  edm::LogInfo("PartonModeDefined") << "Using Herwig++ parton selection mode.";
191  }
192  else if ( partonMode_=="Sherpa" )
193  {
195  edm::LogInfo("PartonModeDefined") << "Using Sherpa parton selection mode.";
196  }
197  else
198  throw cms::Exception("InvalidPartonMode") <<"Parton selection mode is invalid: " << partonMode_ << ", use Auto | Pythia6 | Pythia8 | Herwig6 | Herwig++ | Sherpa" << std::endl;
199 
200  partonSelectorSet_=true;
201  }
202 
204  iEvent.getByToken(particlesToken_, particles);
205 
206  std::auto_ptr<reco::GenParticleRefVector> bHadrons ( new reco::GenParticleRefVector );
207  std::auto_ptr<reco::GenParticleRefVector> cHadrons ( new reco::GenParticleRefVector );
208  std::auto_ptr<reco::GenParticleRefVector> partons ( new reco::GenParticleRefVector );
209  std::auto_ptr<reco::GenParticleRefVector> physicsPartons ( new reco::GenParticleRefVector );
210  std::auto_ptr<reco::GenParticleRefVector> leptons ( new reco::GenParticleRefVector );
211 
212  // loop over particles and select b and c hadrons and leptons
213  for(reco::GenParticleCollection::const_iterator it = particles->begin(); it != particles->end(); ++it)
214  {
215  // if b hadron
216  if( CandMCTagUtils::hasBottom( *it ) )
217  {
218  // check if any of the daughters is also a b hadron
219  bool hasbHadronDaughter = false;
220  for(size_t i=0; i < it->numberOfDaughters(); ++i)
221  {
222  if( CandMCTagUtils::hasBottom( *(it->daughter(i)) ) ) { hasbHadronDaughter = true; break; }
223  }
224  if( hasbHadronDaughter ) continue; // skip excited b hadrons that have other b hadrons as daughters
225 
226  bHadrons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
227  }
228 
229  // if c hadron
230  if( CandMCTagUtils::hasCharm( *it ) )
231  {
232  // check if any of the daughters is also a c hadron
233  bool hascHadronDaughter = false;
234  for(size_t i=0; i < it->numberOfDaughters(); ++i)
235  {
236  if( CandMCTagUtils::hasCharm( *(it->daughter(i)) ) ) { hascHadronDaughter = true; break; }
237  }
238  if( hascHadronDaughter ) continue; // skip excited c hadrons that have other c hadrons as daughters
239 
240  cHadrons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
241  }
242 
243  // status==1 electrons and muons
244  if( ( reco::isElectron( *it ) || reco::isMuon( *it ) ) && it->status()==1 )
245  leptons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
246 
247  // status==2 taus
248  if( reco::isTau( *it ) && it->status()==2 )
249  leptons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
250  }
251 
252  // select partons
253  if ( partonMode_!="Undefined" ) {
254  partonSelector_->run(particles,partons);
255  for(reco::GenParticleCollection::const_iterator it = particles->begin(); it != particles->end(); ++it)
256  {
257  if( !(it->status()==3 || (( partonMode_=="Pythia8" ) && (it->status()==23)))) continue;
258  if( !CandMCTagUtils::isParton( *it ) ) continue; // skip particle if not a parton
259  physicsPartons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
260  }
261  }
262 
263  iEvent.put( bHadrons, "bHadrons" );
264  iEvent.put( cHadrons, "cHadrons" );
265  iEvent.put( partons, "algorithmicPartons" );
266  iEvent.put( physicsPartons, "physicsPartons" );
267  iEvent.put( leptons, "leptons" );
268 }
269 
270 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
271 void
273  //The following says we do not know what parameters are allowed so do no validation
274  // Please change this to state exactly what you do use, even if it is no parameters
276  desc.setUnknown();
277  descriptions.addDefault(desc);
278 }
279 
280 //define this as a plug-in
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
int i
Definition: DBlmapReader.cc:9
Selects hadrons and partons from a collection of GenParticles.
bool isMuon(const Candidate &part)
Definition: pdgIdUtils.h:11
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool hasCharm(const reco::Candidate &c)
Definition: CandMCTag.cc:37
Herwig++ parton selector derived from the base parton selector.
const edm::EDGetTokenT< reco::GenParticleCollection > particlesToken_
Herwig6 parton selector derived from the base parton selector.
int iEvent
Definition: GenABIO.cc:230
std::string moduleName(Provenance const &provenance)
Definition: Provenance.cc:27
Sherpa parton selector derived from the base parton selector.
virtual void produce(edm::Event &, const edm::EventSetup &)
void addDefault(ParameterSetDescription const &psetDescription)
bool isElectron(const Candidate &part)
Definition: pdgIdUtils.h:7
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
bool isValid() const
Definition: HandleBase.h:75
boost::shared_ptr< BasePartonSelector > PartonSelectorPtr
Pythia6 parton selector derived from the base parton selector.
Pythia8 parton selector derived from the base parton selector.
bool isParton(const reco::Candidate &c)
Definition: CandMCTag.cc:48
HadronAndPartonSelector(const edm::ParameterSet &)
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:80
bool hasBottom(const reco::Candidate &c)
Definition: CandMCTag.cc:26
const edm::EDGetTokenT< GenEventInfoProduct > srcToken_
bool isTau(const Candidate &part)
Definition: pdgIdUtils.h:15