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 "Pythia8/Pythia.h"
12 #include "Pythia8/Pythia8ToHepMC.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 hooks
93  //
94  UserHooks* fReweightUserHook;
95  UserHooks* fReweightRapUserHook;
97 
98  // PS matching prototype
99  //
101 
102  // Emission Veto Hooks
103  //
106 
115 
116  static const std::vector<std::string> p8SharedResources;
117 
119 };
120 
122 
124  BaseHadronizer(params), Py8InterfaceBase(params),
125  comEnergy(params.getParameter<double>("comEnergy")),
126  LHEInputFileName(params.getUntrackedParameter<string>("LHEInputFileName","")),
127  fInitialState(PP),
128  fReweightUserHook(0),fReweightRapUserHook(0),fReweightPtHatRapUserHook(0),
129  fJetMatchingHook(0),
130  fEmissionVetoHook(0),fEmissionVetoHook1(0)
131 {
132 
133  // J.Y.: the following 3 parameters are hacked "for a reason"
134  //
135  if ( params.exists( "PPbarInitialState" ) )
136  {
137  if ( fInitialState == PP )
138  {
140  edm::LogImportant("GeneratorInterface|Pythia8Interface")
141  << "Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
142  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
143  }
144  else
145  {
146  // probably need to throw on attempt to override ?
147  }
148  }
149  else if ( params.exists( "ElectronPositronInitialState" ) )
150  {
151  if ( fInitialState == PP )
152  {
154  edm::LogInfo("GeneratorInterface|Pythia8Interface")
155  << "Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
156  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
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  slhafile_ = f1.fullPath();
174  std::string pythiacommandslha = std::string("SLHA:file = ") + slhafile_;
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  if( params.exists( "reweightGenRap" ) )
190  {
191  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenRap";
192  edm::ParameterSet rgrParams =
193  params.getParameter<edm::ParameterSet>("reweightGenRap");
195  new RapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
196  rgrParams.getParameter<double>("yLabPower"),
197  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
198  rgrParams.getParameter<double>("yCMPower"),
199  rgrParams.getParameter<double>("pTHatMin"),
200  rgrParams.getParameter<double>("pTHatMax"));
201  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenRap";
202  }
203  if( params.exists( "reweightGenPtHatRap" ) )
204  {
205  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenPtHatRap";
206  edm::ParameterSet rgrParams =
207  params.getParameter<edm::ParameterSet>("reweightGenPtHatRap");
209  new PtHatRapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
210  rgrParams.getParameter<double>("yLabPower"),
211  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
212  rgrParams.getParameter<double>("yCMPower"),
213  rgrParams.getParameter<double>("pTHatMin"),
214  rgrParams.getParameter<double>("pTHatMax"));
215  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenPtHatRap";
216  }
217 
218  if( params.exists( "useUserHook" ) )
219  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
220  <<" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
221 
222  // PS matching prototype
223  //
224  if ( params.exists("jetMatching") )
225  {
226  edm::ParameterSet jmParams =
227  params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
228  std::string scheme = jmParams.getParameter<std::string>("scheme");
229  if ( scheme == "Madgraph" || scheme == "MadgraphFastJet" )
230  {
231  fJetMatchingHook = new JetMatchingHook( jmParams, &fMasterGen->info );
232  }
233  }
234 
235  // Emission vetos
236  //
237  if ( params.exists("emissionVeto") )
238  {
240  }
241 
242  if ( params.exists("emissionVeto1") )
243  {
244  EV1_nFinal = -1;
245  if(params.exists("EV1_nFinal")) EV1_nFinal = params.getParameter<int>("EV1_nFinal");
246  EV1_vetoOn = true;
247  if(params.exists("EV1_vetoOn")) EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
248  EV1_maxVetoCount = 10;
249  if(params.exists("EV1_maxVetoCount")) EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
250  EV1_pThardMode = 1;
251  if(params.exists("EV1_pThardMode")) EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
252  EV1_pTempMode = 0;
253  if(params.exists("EV1_pTempMode")) EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
254  if(EV1_pTempMode > 2 || EV1_pTempMode < 0)
255  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
256  <<" Wrong value for EV1_pTempMode code\n";
257  EV1_emittedMode = 0;
258  if(params.exists("EV1_emittedMode")) EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
259  EV1_pTdefMode = 1;
260  if(params.exists("EV1_pTdefMode")) EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
261  EV1_MPIvetoOn = false;
262  if(params.exists("EV1_MPIvetoOn")) EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
266  }
267 
268  int NHooks=0;
269  if(fReweightUserHook) NHooks++;
270  if(fReweightRapUserHook) NHooks++;
271  if(fReweightPtHatRapUserHook) NHooks++;
272  if(fJetMatchingHook) NHooks++;
273  if(fEmissionVetoHook) NHooks++;
274  if(fEmissionVetoHook1) NHooks++;
275  if(NHooks > 1)
276  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
277  <<" Too many User Hooks. \n Please choose one from: reweightGen, reweightGenRap, reweightGenPtHatRap, jetMatching, emissionVeto, emissionVeto1 \n";
278  if(fReweightUserHook) fMasterGen->setUserHooksPtr(fReweightUserHook);
281  if(fJetMatchingHook) fMasterGen->setUserHooksPtr(fJetMatchingHook);
283  std::cout << "Turning on Emission Veto Hook";
284  if(fEmissionVetoHook1) std::cout << " 1";
285  std::cout << std::endl;
286  if(fEmissionVetoHook) fMasterGen->setUserHooksPtr(fEmissionVetoHook);
287  if(fEmissionVetoHook1) fMasterGen->setUserHooksPtr(fEmissionVetoHook1);
288  }
289 }
290 
291 
293 {
294 // do we need to delete UserHooks/JetMatchingHook here ???
295 
298 }
299 
301 {
302 
303  // pythiaEvent = &pythia->event;
304 
305  if ( fInitialState == PP ) // default
306  {
307  fMasterGen->init(2212, 2212, comEnergy);
308  }
309  else if ( fInitialState == PPbar )
310  {
311  fMasterGen->init(2212, -2212, comEnergy);
312  }
313  else if ( fInitialState == ElectronPositron )
314  {
315  fMasterGen->init(11, -11, comEnergy);
316  }
317  else
318  {
319  // throw on unknown initial state !
320  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
321  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
322  }
323 
324  fMasterGen->settings.listChanged();
325 
326  if ( pythiaPylistVerbosity > 10 )
327  {
328  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
329  fMasterGen->settings.listAll();
330  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
331  fMasterGen->particleData.listAll();
332  }
333 
334  // init decayer
335  fDecayer->readString("ProcessLevel:all = off"); // trick
336  fDecayer->readString("ProcessLevel::resonanceDecays=on");
337  fDecayer->init();
338 
339  return true;
340 }
341 
342 
344 {
345 
346  std::cout << "Initializing for external partons" << std::endl;
347 
348  if(LHEInputFileName != string()) {
349 
350  std::cout << std::endl;
351  std::cout << "LHE Input File Name = " << LHEInputFileName << std::endl;
352  std::cout << std::endl;
354 
355  } else {
356 
357  lhaUP.reset(new LHAupLesHouches());
358  lhaUP->loadRunInfo(lheRunInfo());
359 
360  if ( fJetMatchingHook )
361  {
363  }
364 
365  //pythia 8 doesn't currently support reading SLHA table from lhe header in memory
366  //so dump it to a temp file and set the appropriate pythia parameters to read it
367  std::vector<std::string> slha = lheRunInfo()->findHeader("slha");
368  const char *fname = std::tmpnam(NULL);
369  //read slha header from lhe only if header is present AND no slha header was specified
370  //for manual loading.
371  bool doslha = !slha.empty() && slhafile_.empty();
372 
373  if (doslha) {
374  std::ofstream file(fname, std::fstream::out | std::fstream::trunc);
376  for(std::vector<std::string>::const_iterator iter = slha.begin();
377  iter != slha.end(); ++iter) {
378  file << *iter;
379  }
380  file.close();
381 
382  std::string lhareadcmd = "SLHA:readFrom = 2";
383  std::string lhafilecmd = std::string("SLHA:file = ") + std::string(fname);
384 
385  fMasterGen->readString(lhareadcmd);
386  fMasterGen->readString(lhafilecmd);
387  }
388 
389  fMasterGen->init(lhaUP.get());
390 
391  if (doslha) {
392  std::remove( fname );
393  }
394 
395  }
396 
397  if ( pythiaPylistVerbosity > 10 )
398  {
399  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
400  fMasterGen->settings.listAll();
401  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
402  fMasterGen->particleData.listAll();
403  }
404 
405  // init decayer
406  fDecayer->readString("ProcessLevel:all = off"); // trick
407  fDecayer->readString("ProcessLevel::resonanceDecays=on");
408  fDecayer->init();
409 
410  return true;
411 }
412 
413 
415 {
416  fMasterGen->statistics();
417 
418  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
419  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
420  runInfo().setInternalXSec(xsec);
421 }
422 
423 
425 {
426 
427  if (!fMasterGen->next()) return false;
428 
429  event().reset(new HepMC::GenEvent);
430  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
431 
432 }
433 
434 
436 {
437  if(LHEInputFileName == string()) lhaUP->loadEvent(lheEvent());
438 
439  if ( fJetMatchingHook )
440  {
443  }
444 
445  bool py8next = fMasterGen->next();
446 
447  if (!py8next)
448  {
450  event().reset();
451  return false;
452  }
453 
454  // update LHE matching statistics
455  //
457 
458  event().reset(new HepMC::GenEvent);
459  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
460 
461 }
462 
463 
465 {
466 
467  Event* pythiaEvent = &(fMasterGen->event);
468 
469  int NPartsBeforeDecays = pythiaEvent->size();
470  int NPartsAfterDecays = event().get()->particles_size();
471  int NewBarcode = NPartsAfterDecays;
472 
473  for ( int ipart=NPartsAfterDecays; ipart>NPartsBeforeDecays; ipart-- )
474  {
475 
476  HepMC::GenParticle* part = event().get()->barcode_to_particle( ipart );
477 
478  if ( part->status() == 1 )
479  {
480  fDecayer->event.reset();
481  Particle py8part( part->pdg_id(), 93, 0, 0, 0, 0, 0, 0,
482  part->momentum().x(),
483  part->momentum().y(),
484  part->momentum().z(),
485  part->momentum().t(),
486  part->generated_mass() );
487  HepMC::GenVertex* ProdVtx = part->production_vertex();
488  py8part.vProd( ProdVtx->position().x(), ProdVtx->position().y(),
489  ProdVtx->position().z(), ProdVtx->position().t() );
490  py8part.tau( (fDecayer->particleData).tau0( part->pdg_id() ) );
491  fDecayer->event.append( py8part );
492  int nentries = fDecayer->event.size();
493  if ( !fDecayer->event[nentries-1].mayDecay() ) continue;
494  fDecayer->next();
495  int nentries1 = fDecayer->event.size();
496  if ( nentries1 <= nentries ) continue; //same number of particles, no decays...
497 
498  part->set_status(2);
499 
500  Particle& py8daughter = fDecayer->event[nentries]; // the 1st daughter
501  HepMC::GenVertex* DecVtx = new HepMC::GenVertex( HepMC::FourVector(py8daughter.xProd(),
502  py8daughter.yProd(),
503  py8daughter.zProd(),
504  py8daughter.tProd()) );
505 
506  DecVtx->add_particle_in( part ); // this will cleanup end_vertex if exists, replace with the new one
507  // I presume (vtx) barcode will be given automatically
508 
509  HepMC::FourVector pmom( py8daughter.px(), py8daughter.py(), py8daughter.pz(), py8daughter.e() );
510 
511  HepMC::GenParticle* daughter =
512  new HepMC::GenParticle( pmom, py8daughter.id(), 1 );
513 
514  NewBarcode++;
515  daughter->suggest_barcode( NewBarcode );
516  DecVtx->add_particle_out( daughter );
517 
518  for ( int ipart1=nentries+1; ipart1<nentries1; ipart1++ )
519  {
520  py8daughter = fDecayer->event[ipart1];
521  HepMC::FourVector pmomN( py8daughter.px(), py8daughter.py(), py8daughter.pz(), py8daughter.e() );
522  HepMC::GenParticle* daughterN =
523  new HepMC::GenParticle( pmomN, py8daughter.id(), 1 );
524  NewBarcode++;
525  daughterN->suggest_barcode( NewBarcode );
526  DecVtx->add_particle_out( daughterN );
527  }
528 
529  event().get()->add_vertex( DecVtx );
530 
531  }
532  }
533  return true;
534 
535 }
536 
537 
539 {
540  bool lhe = lheEvent() != 0;
541 
542  // now create the GenEventInfo product from the GenEvent and fill
543  // the missing pieces
544  eventInfo().reset( new GenEventInfoProduct( event().get() ) );
545 
546  // in pythia pthat is used to subdivide samples into different bins
547  // in LHE mode the binning is done by the external ME generator
548  // which is likely not pthat, so only filling it for Py6 internal mode
549  if (!lhe) {
550  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
551  }
552 
553  //******** Verbosity ********
554 
555  if (maxEventsToPrint > 0 &&
558  if (pythiaPylistVerbosity) {
559  fMasterGen->info.list(std::cout);
560  fMasterGen->event.list(std::cout);
561  }
562 
563  if (pythiaHepMCVerbosity) {
564  std::cout << "Event process = "
565  << fMasterGen->info.code() << "\n"
566  << "----------------------" << std::endl;
567  event()->print();
568  }
569  }
570 }
571 
574 
575 
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
#define NULL
Definition: scimark2.h:8
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:203
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()
UserHooks * fReweightPtHatRapUserHook
static const std::vector< std::string > p8SharedResources
unsigned int pythiaPylistVerbosity
const char * classname() const override
UserHooks * fReweightRapUserHook
std::auto_ptr< GenEventInfoProduct > & eventInfo()
lhef::LHERunInfo * lheRunInfo()
bool generatePartonsAndHadronize() override
tuple out
Definition: dbtoconf.py:99
virtual void init(lhef::LHERunInfo *runInfo)
JetMatchingHook * fJetMatchingHook
part
Definition: HCALResponse.h:20
unsigned int maxEventsToPrint
std::auto_ptr< Pythia8::Pythia > fDecayer
string fname
main script
Pythia8Hadronizer(const edm::ParameterSet &params)
void resetMatchingStatus()
const_iterator end() const
const_iterator begin() const
HepMC::Pythia8ToHepMC toHepMC
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:390
static const std::string kPythia8
void finalizeEvent() override
virtual void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
edm::HadronizerFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8HadronizerFilter