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 beginJob() ;
88  virtual void produce(edm::Event&, const edm::EventSetup&);
89  virtual void endJob() ;
90 
91  virtual void beginRun(edm::Run&, edm::EventSetup const&);
92  virtual void endRun(edm::Run&, edm::EventSetup const&);
95 
96  // ----------member data ---------------------------
97  const edm::EDGetTokenT<GenEventInfoProduct> srcToken_; // To get handronizer module type
98  const edm::EDGetTokenT<reco::GenParticleCollection> particlesToken_; // Input GenParticle collection
99 
100  std::string partonMode_; // Parton selection mode
102 };
103 
104 //
105 // static data member definitions
106 //
107 
108 //
109 // constructors and destructor
110 //
112 
113  srcToken_(mayConsume<GenEventInfoProduct>( iConfig.getParameter<edm::InputTag>("src") )),
114  particlesToken_(consumes<reco::GenParticleCollection>( iConfig.getParameter<edm::InputTag>("particles") )),
115  partonMode_(iConfig.getParameter<std::string>("partonMode"))
116 
117 {
118  //register your products
119  produces<reco::GenParticleRefVector>( "bHadrons" );
120  produces<reco::GenParticleRefVector>( "cHadrons" );
121  produces<reco::GenParticleRefVector>( "partons" );
122  produces<reco::GenParticleRefVector>( "leptons" );
123 }
124 
125 
127 {
128 
129  // do anything here that needs to be done at desctruction time
130  // (e.g. close files, deallocate resources etc.)
131 
132 }
133 
134 
135 //
136 // member functions
137 //
138 
139 // ------------ method called to produce the data ------------
140 void
142 {
143  // determine hadronizer type (done only once per job)
144  if( partonMode_=="Auto" )
145  {
146  edm::Handle<GenEventInfoProduct> genEvtInfoProduct;
147  iEvent.getByToken(srcToken_, genEvtInfoProduct);
148 
149  std::string moduleName = "";
150  const edm::Provenance& prov = iEvent.getProvenance(genEvtInfoProduct.id());
151  if( genEvtInfoProduct.isValid() )
152  moduleName = edm::moduleName(prov);
153 
154  if( moduleName.find("Pythia6")!=std::string::npos )
155  partonMode_="Pythia6";
156  else if( moduleName.find("Pythia8")!=std::string::npos )
157  partonMode_="Pythia8";
158  else if( moduleName.find("Herwig6")!=std::string::npos )
159  partonMode_="Herwig6";
160  else if( moduleName.find("ThePEG")!=std::string::npos )
161  partonMode_="Herwig++";
162  else if( moduleName.find("Sherpa")!=std::string::npos )
163  partonMode_="Sherpa";
164  else
165  partonMode_="Undefined";
166  }
167 
168  // set the parton selection mode (done only once per job)
169  if( !partonSelector_ )
170  {
171  if ( partonMode_=="Undefined" )
172  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.";
173  else if ( partonMode_=="Pythia6" )
174  {
176  edm::LogInfo("PartonModeDefined") << "Using Pythia6 parton selection mode.";
177  }
178  else if ( partonMode_=="Pythia8" )
179  {
181  edm::LogInfo("PartonModeDefined") << "Using Pythia8 parton selection mode.";
182  }
183  else if ( partonMode_=="Herwig6" )
184  {
186  edm::LogInfo("PartonModeDefined") << "Using Herwig6 parton selection mode.";
187  }
188  else if ( partonMode_=="Herwig++" )
189  {
191  edm::LogInfo("PartonModeDefined") << "Using Herwig++ parton selection mode.";
192  }
193  else if ( partonMode_=="Sherpa" )
194  {
196  edm::LogInfo("PartonModeDefined") << "Using Sherpa parton selection mode.";
197  }
198  else
199  throw cms::Exception("InvalidPartonMode") <<"Parton selection mode is invalid: " << partonMode_ << ", use Auto | Pythia6 | Pythia8 | Herwig6 | Herwig++ | Sherpa" << std::endl;
200  }
201 
203  iEvent.getByToken(particlesToken_, particles);
204 
205  std::auto_ptr<reco::GenParticleRefVector> bHadrons ( new reco::GenParticleRefVector );
206  std::auto_ptr<reco::GenParticleRefVector> cHadrons ( new reco::GenParticleRefVector );
207  std::auto_ptr<reco::GenParticleRefVector> partons ( new reco::GenParticleRefVector );
208  std::auto_ptr<reco::GenParticleRefVector> leptons ( new reco::GenParticleRefVector );
209 
210  // loop over particles and select b and c hadrons and leptons
211  for(reco::GenParticleCollection::const_iterator it = particles->begin(); it != particles->end(); ++it)
212  {
213  // if b hadron
214  if( CandMCTagUtils::hasBottom( *it ) )
215  {
216  // check if any of the daughters is also a b hadron
217  bool hasbHadronDaughter = false;
218  for(size_t i=0; i < it->numberOfDaughters(); ++i)
219  {
220  if( CandMCTagUtils::hasBottom( *(it->daughter(i)) ) ) { hasbHadronDaughter = true; break; }
221  }
222  if( hasbHadronDaughter ) continue; // skip excited b hadrons that have other b hadrons as daughters
223 
224  bHadrons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
225  }
226 
227  // if c hadron
228  if( CandMCTagUtils::hasCharm( *it ) )
229  {
230  // check if any of the daughters is also a c hadron
231  bool hascHadronDaughter = false;
232  for(size_t i=0; i < it->numberOfDaughters(); ++i)
233  {
234  if( CandMCTagUtils::hasCharm( *(it->daughter(i)) ) ) { hascHadronDaughter = true; break; }
235  }
236  if( hascHadronDaughter ) continue; // skip excited c hadrons that have other c hadrons as daughters
237 
238  cHadrons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
239  }
240 
241  // status==1 electrons and muons
242  if( ( reco::isElectron( *it ) || reco::isMuon( *it ) ) && it->status()==1 )
243  leptons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
244 
245  // status==2 taus
246  if( reco::isTau( *it ) && it->status()==2 )
247  leptons->push_back( reco::GenParticleRef( particles, it - particles->begin() ) );
248  }
249 
250  // select partons
251  if ( partonMode_!="Undefined" )
252  partonSelector_->run(particles,partons);
253 
254  iEvent.put( bHadrons, "bHadrons" );
255  iEvent.put( cHadrons, "cHadrons" );
256  iEvent.put( partons, "partons" );
257  iEvent.put( leptons, "leptons" );
258 }
259 
260 // ------------ method called once each job just before starting event loop ------------
261 void
263 {
264 }
265 
266 // ------------ method called once each job just after ending the event loop ------------
267 void
269 }
270 
271 // ------------ method called when starting to processes a run ------------
272 void
274 {
275 }
276 
277 // ------------ method called when ending the processing of a run ------------
278 void
280 {
281 }
282 
283 // ------------ method called when starting to processes a luminosity block ------------
284 void
286 {
287 }
288 
289 // ------------ method called when ending the processing of a luminosity block ------------
290 void
292 {
293 }
294 
295 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
296 void
298  //The following says we do not know what parameters are allowed so do no validation
299  // Please change this to state exactly what you do use, even if it is no parameters
301  desc.setUnknown();
302  descriptions.addDefault(desc);
303 }
304 
305 //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:434
#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 &)
virtual void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
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:116
bool isValid() const
Definition: HandleBase.h:76
boost::shared_ptr< BasePartonSelector > PartonSelectorPtr
Pythia6 parton selector derived from the base parton selector.
Pythia8 parton selector derived from the base parton selector.
virtual void endLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
virtual void beginRun(edm::Run &, edm::EventSetup const &)
HadronAndPartonSelector(const edm::ParameterSet &)
virtual void endRun(edm::Run &, edm::EventSetup const &)
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:76
bool hasBottom(const reco::Candidate &c)
Definition: CandMCTag.cc:26
Definition: Run.h:41
const edm::EDGetTokenT< GenEventInfoProduct > srcToken_
bool isTau(const Candidate &part)
Definition: pdgIdUtils.h:15