CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Pythia8Hadronizer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <memory>
5 #include <stdint.h>
6 #include <vector>
7 
8 #include <HepMC/GenEvent.h>
9 #include <HepMC/GenParticle.h>
10 
11 #include <Pythia.h>
12 #include <HepMCInterface.h>
13 
15 
17 
18 // PS matchning prototype
19 //
21 
22 
23 // Emission Veto Hooks
24 //
27 
33 
36 
40 
42 
43 #include "HepPID/ParticleIDTranslations.hh"
44 
46 
47 namespace CLHEP {
48  class HepRandomEngine;
49 }
50 
51 using namespace gen;
52 using namespace Pythia8;
53 
55 
56  public:
57 
58  Pythia8Hadronizer(const edm::ParameterSet &params);
60 
61  bool initializeForInternalPartons() override;
62  bool initializeForExternalPartons();
63 
64  bool generatePartonsAndHadronize() override;
65  bool hadronize();
66 
67  virtual bool residualDecay();
68 
69  void finalizeEvent() override;
70 
71  void statistics() override;
72 
73  const char *classname() const override { return "Pythia8Hadronizer"; }
74 
75  private:
76 
77  virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { p8SetRandomEngine(v); }
78  virtual std::vector<std::string> const& doSharedResources() const override { return p8SharedResources; }
79 
81  double comEnergy;
82 
84  std::auto_ptr<LHAupLesHouches> lhaUP;
85 
86  enum { PP, PPbar, ElectronPositron };
87  int fInitialState ; // pp, ppbar, or e-e+
88 
89  double fBeam1PZ;
90  double fBeam2PZ;
91 
92  // Reweight user hook
93  //
94  UserHooks* fReweightUserHook;
95 
96  // PS matching protot6ype
97  //
99 
100  // Emission Veto Hooks
101  //
104 
113 
114  static const std::vector<std::string> p8SharedResources;
115 };
116 
118 
120  BaseHadronizer(params), Py8InterfaceBase(params),
121  comEnergy(params.getParameter<double>("comEnergy")),
122  LHEInputFileName(params.getUntrackedParameter<string>("LHEInputFileName","")),
123  fInitialState(PP),
124  fReweightUserHook(0),
125  fJetMatchingHook(0),
126  fEmissionVetoHook(0),fEmissionVetoHook1(0)
127 {
128 
129  // J.Y.: the following 3 parameters are hacked "for a reason"
130  //
131  if ( params.exists( "PPbarInitialState" ) )
132  {
133  if ( fInitialState == PP )
134  {
136  edm::LogInfo("GeneratorInterface|Pythia6Interface")
137  << "Pythia6 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
138  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
139  std::cout << "Pythia6 will be initialized for PROTON-ANTIPROTON INITIAL STATE." << std::endl;
140  std::cout << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
141  }
142  else
143  {
144  // probably need to throw on attempt to override ?
145  }
146  }
147  else if ( params.exists( "ElectronPositronInitialState" ) )
148  {
149  if ( fInitialState == PP )
150  {
152  edm::LogInfo("GeneratorInterface|Pythia6Interface")
153  << "Pythia6 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
154  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
155  std::cout << "Pythia6 will be initialized for ELECTRON-POSITRON INITIAL STATE." << std::endl;
156  std::cout << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
157  }
158  else
159  {
160  // probably need to throw on attempt to override ?
161  }
162  }
163  else if ( params.exists( "ElectronProtonInitialState" ) || params.exists( "PositronProtonInitialState" ) )
164  {
165  // throw on unknown initial state !
166  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
167  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
168  }
169 
170  if( params.exists( "SLHAFileForPythia8" ) ) {
171  std::string slhafilenameshort = params.getParameter<string>("SLHAFileForPythia8");
172  edm::FileInPath f1( slhafilenameshort );
173  std::string slhafilename = f1.fullPath();
174  std::string pythiacommandslha = std::string("SLHA:file = ") + slhafilename;
175  fMasterGen->readString(pythiacommandslha);
177  line != fParameters.end(); ++line ) {
178  if (line->find("SLHA:file") != std::string::npos)
179  throw cms::Exception("PythiaError") << "Attempted to set SLHA file name twice, "
180  << "using Pythia8 card SLHA:file and Pythia8Interface card SLHAFileForPythia8"
181  << std::endl;
182  }
183  }
184 
185  // Reweight user hook
186  //
187  if( params.exists( "reweightGen" ) )
189 
190  if( params.exists( "useUserHook" ) )
191  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
192  <<" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
193 
194  // PS matching prototype
195  //
196  if ( params.exists("jetMatching") )
197  {
198  edm::ParameterSet jmParams =
199  params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
200  std::string scheme = jmParams.getParameter<std::string>("scheme");
201  if ( scheme == "Madgraph" || scheme == "MadgraphFastJet" )
202  {
203  fJetMatchingHook = new JetMatchingHook( jmParams, &fMasterGen->info );
204  }
205  }
206 
207  // Emission vetos
208  //
209  if ( params.exists("emissionVeto") )
210  {
212  }
213 
214  if ( params.exists("emissionVeto1") )
215  {
216  EV1_nFinal = -1;
217  if(params.exists("EV1_nFinal")) EV1_nFinal = params.getParameter<int>("EV1_nFinal");
218  EV1_vetoOn = true;
219  if(params.exists("EV1_vetoOn")) EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
220  EV1_maxVetoCount = 10;
221  if(params.exists("EV1_maxVetoCount")) EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
222  EV1_pThardMode = 1;
223  if(params.exists("EV1_pThardMode")) EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
224  EV1_pTempMode = 0;
225  if(params.exists("EV1_pTempMode")) EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
226  if(EV1_pTempMode > 2 || EV1_pTempMode < 0)
227  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
228  <<" Wrong value for EV1_pTempMode code\n";
229  EV1_emittedMode = 0;
230  if(params.exists("EV1_emittedMode")) EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
231  EV1_pTdefMode = 1;
232  if(params.exists("EV1_pTdefMode")) EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
233  EV1_MPIvetoOn = false;
234  if(params.exists("EV1_MPIvetoOn")) EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
238  }
239 
240  int NHooks=0;
241  if(fReweightUserHook) NHooks++;
242  if(fJetMatchingHook) NHooks++;
243  if(fEmissionVetoHook) NHooks++;
244  if(fEmissionVetoHook1) NHooks++;
245  if(NHooks > 1)
246  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
247  <<" Too many User Hooks. \n Please choose one from: reweightGen, jetMatching, emissionVeto \n";
248 
249  if(fReweightUserHook) fMasterGen->setUserHooksPtr(fReweightUserHook);
250  if(fJetMatchingHook) fMasterGen->setUserHooksPtr(fJetMatchingHook);
252  cout << "Turning on Emission Veto Hook";
253  if(fEmissionVetoHook1) cout << " 1";
254  cout << endl;
255  int nversion = (int)(1000.*(fMasterGen->settings.parm("Pythia:versionNumber") - 8.));
256  if(nversion < 157) {
257  cout << "obsolete pythia8 version for this Emission Veto code" << endl;
258  cout << "Please update pythia8 version using the instructions here:" << endl;
259  cout << "https://twiki.cern.ch/twiki/bin/view/CMS/Pythia8Interface" << endl;
260  cout << "or try to use tag V00-01-28 of this interface" << endl;
261  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
262  <<" Obsolete pythia8 version for this Emission Veto code\n";
263  }
264  if(fEmissionVetoHook) fMasterGen->setUserHooksPtr(fEmissionVetoHook);
265  if(fEmissionVetoHook1) fMasterGen->setUserHooksPtr(fEmissionVetoHook1);
266  }
267 }
268 
269 
271 {
272 // do we need to delete UserHooks/JetMatchingHook here ???
273 
276 }
277 
279 {
280 
281  // pythiaEvent = &pythia->event;
282 
283  if ( fInitialState == PP ) // default
284  {
285  fMasterGen->init(2212, 2212, comEnergy);
286  }
287  else if ( fInitialState == PPbar )
288  {
289  fMasterGen->init(2212, -2212, comEnergy);
290  }
291  else if ( fInitialState == ElectronPositron )
292  {
293  fMasterGen->init(11, -11, comEnergy);
294  }
295  else
296  {
297  // throw on unknown initial state !
298  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
299  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
300  }
301 
302  fMasterGen->settings.listChanged();
303 
304  if ( pythiaPylistVerbosity > 10 )
305  {
306  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
307  fMasterGen->settings.listAll();
308  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
309  fMasterGen->particleData.listAll();
310  }
311 
312  // init decayer
313  fDecayer->readString("ProcessLevel:all = off"); // trick
314  fDecayer->readString("Standalone:allowResDec=on");
315  // pythia->readString("ProcessLevel::resonanceDecays=on");
316  fDecayer->init();
317 
318  return true;
319 }
320 
321 
323 {
324 
325  std::cout << "Initializing for external partons" << std::endl;
326 
327  // pythiaEvent = &pythia->event;
328 
329  if(LHEInputFileName != string()) {
330 
331  cout << endl;
332  cout << "LHE Input File Name = " << LHEInputFileName << endl;
333  cout << endl;
335 
336  } else {
337 
338  lhaUP.reset(new LHAupLesHouches());
339  lhaUP->loadRunInfo(lheRunInfo());
340 
341  if ( fJetMatchingHook )
342  {
344  }
345 
346  fMasterGen->init(lhaUP.get());
347 
348  }
349 
350  if ( pythiaPylistVerbosity > 10 )
351  {
352  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
353  fMasterGen->settings.listAll();
354  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
355  fMasterGen->particleData.listAll();
356  }
357 
358  // init decayer
359  fDecayer->readString("ProcessLevel:all = off"); // trick
360  fDecayer->readString("Standalone:allowResDec=on");
361  // pythia->readString("ProcessLevel::resonanceDecays=on");
362  fDecayer->init();
363 
364  return true;
365 }
366 
367 #if 0
368 // naive Pythia8 HepMC status fixup
369 static int getStatus(const HepMC::GenParticle *p)
370 {
371  int status = p->status();
372  if (status > 0)
373  return status;
374  else if (status > -30 && status < 0)
375  return 3;
376  else
377  return 2;
378 }
379 #endif
380 
381 
383 {
384  fMasterGen->statistics();
385 
386  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
387  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
388  runInfo().setInternalXSec(xsec);
389 }
390 
391 
393 {
394 
395  if (!fMasterGen->next()) return false;
396 
397  event().reset(new HepMC::GenEvent);
398  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
399 
400 }
401 
402 
404 {
405  if(LHEInputFileName == string()) lhaUP->loadEvent(lheEvent());
406 
407  if ( fJetMatchingHook )
408  {
411  }
412 
413  bool py8next = fMasterGen->next();
414 
415  if (!py8next)
416  {
418  event().reset();
419  return false;
420  }
421 
422  // update LHE matching statistics
423  //
425 
426  event().reset(new HepMC::GenEvent);
427  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
428 
429 }
430 
431 
433 {
434 
435  Event* pythiaEvent = &(fMasterGen->event);
436 
437  int NPartsBeforeDecays = pythiaEvent->size();
438  int NPartsAfterDecays = event().get()->particles_size();
439  int NewBarcode = NPartsAfterDecays;
440 
441  for ( int ipart=NPartsAfterDecays; ipart>NPartsBeforeDecays; ipart-- )
442  {
443 
444  HepMC::GenParticle* part = event().get()->barcode_to_particle( ipart );
445 
446  if ( part->status() == 1 )
447  {
448  fDecayer->event.reset();
449  Particle py8part( part->pdg_id(), 93, 0, 0, 0, 0, 0, 0,
450  part->momentum().x(),
451  part->momentum().y(),
452  part->momentum().z(),
453  part->momentum().t(),
454  part->generated_mass() );
455  HepMC::GenVertex* ProdVtx = part->production_vertex();
456  py8part.vProd( ProdVtx->position().x(), ProdVtx->position().y(),
457  ProdVtx->position().z(), ProdVtx->position().t() );
458  py8part.tau( (fDecayer->particleData).tau0( part->pdg_id() ) );
459  fDecayer->event.append( py8part );
460  int nentries = fDecayer->event.size();
461  if ( !fDecayer->event[nentries-1].mayDecay() ) continue;
462  fDecayer->next();
463  int nentries1 = fDecayer->event.size();
464  // fDecayer->event.list(std::cout);
465  if ( nentries1 <= nentries ) continue; //same number of particles, no decays...
466 
467  part->set_status(2);
468 
469  Particle& py8daughter = fDecayer->event[nentries]; // the 1st daughter
470  HepMC::GenVertex* DecVtx = new HepMC::GenVertex( HepMC::FourVector(py8daughter.xProd(),
471  py8daughter.yProd(),
472  py8daughter.zProd(),
473  py8daughter.tProd()) );
474 
475  DecVtx->add_particle_in( part ); // this will cleanup end_vertex if exists, replace with the new one
476  // I presume (vtx) barcode will be given automatically
477 
478  HepMC::FourVector pmom( py8daughter.px(), py8daughter.py(), py8daughter.pz(), py8daughter.e() );
479 
480  HepMC::GenParticle* daughter =
481  new HepMC::GenParticle( pmom, py8daughter.id(), 1 );
482 
483  NewBarcode++;
484  daughter->suggest_barcode( NewBarcode );
485  DecVtx->add_particle_out( daughter );
486 
487  for ( int ipart1=nentries+1; ipart1<nentries1; ipart1++ )
488  {
489  py8daughter = fDecayer->event[ipart1];
490  HepMC::FourVector pmomN( py8daughter.px(), py8daughter.py(), py8daughter.pz(), py8daughter.e() );
491  HepMC::GenParticle* daughterN =
492  new HepMC::GenParticle( pmomN, py8daughter.id(), 1 );
493  NewBarcode++;
494  daughterN->suggest_barcode( NewBarcode );
495  DecVtx->add_particle_out( daughterN );
496  }
497 
498  event().get()->add_vertex( DecVtx );
499  // fCurrentEventState->add_vertex( DecVtx );
500 
501  }
502  }
503  return true;
504 
505 }
506 
507 
509 {
510  bool lhe = lheEvent() != 0;
511 
512  // now create the GenEventInfo product from the GenEvent and fill
513  // the missing pieces
514  eventInfo().reset( new GenEventInfoProduct( event().get() ) );
515 
516  // in pythia pthat is used to subdivide samples into different bins
517  // in LHE mode the binning is done by the external ME generator
518  // which is likely not pthat, so only filling it for Py6 internal mode
519  if (!lhe) {
520  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
521  }
522 
523  //******** Verbosity ********
524 
525  if (maxEventsToPrint > 0 &&
528  if (pythiaPylistVerbosity) {
529  fMasterGen->info.list(std::cout);
530  fMasterGen->event.list(std::cout);
531  }
532 
533  if (pythiaHepMCVerbosity) {
534  std::cout << "Event process = "
535  << fMasterGen->info.code() << "\n"
536  << "----------------------" << std::endl;
537  event()->print();
538  }
539  }
540 }
541 
544 
545 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
virtual bool residualDecay()
ParameterCollector fParameters
double comEnergy
Center-of-Mass energy.
std::auto_ptr< Pythia8::Pythia > fMasterGen
EmissionVetoHook1 * fEmissionVetoHook1
edm::GeneratorFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8GeneratorFilter
bool initializeForInternalPartons() override
UserHooks * fReweightUserHook
EmissionVetoHook * fEmissionVetoHook
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void statistics() override
bool exists(std::string const &parameterName) const
checks if a parameter exists
HepMC::I_Pythia8 toHepMC
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:212
void setInternalXSec(const XSec &xsec)
virtual void beforeHadronization(lhef::LHEEvent *lhee)
double v[5][pyjets_maxn]
std::auto_ptr< HepMC::GenEvent > & event()
virtual std::vector< std::string > const & doSharedResources() const override
GenRunInfoProduct & runInfo()
std::auto_ptr< LHAupLesHouches > lhaUP
lhef::LHEEvent * lheEvent()
static const std::vector< std::string > p8SharedResources
unsigned int pythiaPylistVerbosity
const char * classname() const override
std::auto_ptr< GenEventInfoProduct > & eventInfo()
lhef::LHERunInfo * lheRunInfo()
bool generatePartonsAndHadronize() override
virtual void init(lhef::LHERunInfo *runInfo)
JetMatchingHook * fJetMatchingHook
part
Definition: HCALResponse.h:20
unsigned int maxEventsToPrint
std::auto_ptr< Pythia8::Pythia > fDecayer
Pythia8Hadronizer(const edm::ParameterSet &params)
void resetMatchingStatus()
const_iterator end() const
const_iterator begin() const
tuple cout
Definition: gather_cfg.py:121
static const std::string kPythia8
void finalizeEvent() override
tuple status
Definition: ntuplemaker.py:245
virtual void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
edm::HadronizerFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8HadronizerFilter