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 
7 #include <HepMC/GenEvent.h>
8 #include <HepMC/GenParticle.h>
9 
10 #include <Pythia.h>
11 #include <HepMCInterface.h>
12 
14 
16 
20 
23 
29 
31 
32 #include "HepPID/ParticleIDTranslations.hh"
33 
35 
36 using namespace gen;
37 using namespace Pythia8;
38 
40  public:
41  Pythia8Hadronizer(const edm::ParameterSet &params);
43 
44  bool initializeForInternalPartons();
45  bool initializeForExternalPartons();
46 
47  bool declareStableParticles(const std::vector<int> &pdgIds);
48  bool declareSpecialSettings( const std::vector<std::string> );
49 
50  void statistics();
51 
52  bool generatePartonsAndHadronize();
53  bool hadronize();
54  bool decay();
55  bool residualDecay();
56  void finalizeEvent();
57 
58  const char *classname() const { return "Pythia8Hadronizer"; }
59 
60  private:
62 
64  double comEnergy;
66  unsigned int pythiaPylistVerbosity;
70  unsigned int maxEventsToPrint;
71 
73 
76 
77  std::auto_ptr<LHAupLesHouches> lhaUP;
78 
79  std::auto_ptr<Pythia> pythia;
80  Event *pythiaEvent;
81  HepMC::I_Pythia8 toHepMC;
82 
83 };
84 
85 
87  BaseHadronizer(params),
88  parameters(params.getParameter<edm::ParameterSet>("PythiaParameters")),
89  comEnergy(params.getParameter<double>("comEnergy")),
90  pythiaPylistVerbosity(params.getUntrackedParameter<int>("pythiaPylistVerbosity", 0)),
91  pythiaHepMCVerbosity(params.getUntrackedParameter<bool>("pythiaHepMCVerbosity", false)),
92  maxEventsToPrint(params.getUntrackedParameter<int>("maxEventsToPrint", 0)),
93  LHEInputFileName(params.getUntrackedParameter<string>("LHEInputFileName","")),
94  useUserHook(false)
95 {
96  if( params.exists( "useUserHook" ) )
97  useUserHook = params.getParameter<bool>("useUserHook");
99 }
100 
102 {
103 }
104 
106 {
107  //Old code that used Pythia8 own random engine
108  //edm::Service<edm::RandomNumberGenerator> rng;
109  //uint32_t seed = rng->mySeed();
110  //Pythia8::Rndm::init(seed);
111 
112  RandomP8* RP8 = new RandomP8();
113 
114  pythia.reset(new Pythia);
115 
116  pythia->setRndmEnginePtr(RP8);
117  if(useUserHook) pythia->setUserHooksPtr(new PtHatReweightUserHook());
118 
119  pythiaEvent = &pythia->event;
120 
122  line != parameters.end(); ++line) {
123  if (line->find("Random:") != std::string::npos)
124  throw cms::Exception("PythiaError")
125  << "Attempted to set random number "
126  "using Pythia commands. Please use "
127  "the RandomNumberGeneratorService."
128  << std::endl;
129 
130  if (!pythia->readString(*line))
131  throw cms::Exception("PythiaError")
132  << "Pythia 8 did not accept \""
133  << *line << "\"." << std::endl;
134  }
135 
136  if(pythiaPylistVerbosity > 10) {
138  pythia->settings.listAll();
140  pythia->particleData.listAll();
141  }
142 
143  pythia->init(2212, 2212, comEnergy);
144 
145  pythia->settings.listChanged();
146 
147  return true;
148 }
149 
150 
152 {
153 
154  std::cout << "Initializing for external partons" << std::endl;
155 
156  RandomP8* RP8 = new RandomP8();
157 
158  pythia.reset(new Pythia);
159 
160  pythia->setRndmEnginePtr(RP8);
161 
162  pythiaEvent = &pythia->event;
163 
165  line != parameters.end(); ++line) {
166  if (line->find("Random:") != std::string::npos)
167  throw cms::Exception("PythiaError")
168  << "Attempted to set random number "
169  "using Pythia commands. Please use "
170  "the RandomNumberGeneratorService."
171  << std::endl;
172 
173  if (!pythia->readString(*line))
174  throw cms::Exception("PythiaError")
175  << "Pythia 8 did not accept \""
176  << *line << "\"." << std::endl;
177  }
178 
179  if(pythiaPylistVerbosity > 10) {
181  pythia->settings.listAll();
183  pythia->particleData.listAll();
184  }
185 
186  if(LHEInputFileName != string()) {
187 
188  cout << endl;
189  cout << "LHE Input File Name = " << LHEInputFileName << endl;
190  cout << endl;
191  pythia->init(LHEInputFileName);
192 
193  } else {
194 
195  lhaUP.reset(new LHAupLesHouches());
196  lhaUP->loadRunInfo(lheRunInfo());
197  pythia->init(lhaUP.get());
198 
199  }
200 
201  return true;
202 }
203 
204 
205 #if 0
206 // naive Pythia8 HepMC status fixup
207 static int getStatus(const HepMC::GenParticle *p)
208 {
209  int status = p->status();
210  if (status > 0)
211  return status;
212  else if (status > -30 && status < 0)
213  return 3;
214  else
215  return 2;
216 }
217 #endif
218 
219 bool Pythia8Hadronizer::declareStableParticles(const std::vector<int> &pdgIds)
220 {
221 
222  for ( size_t i=0; i<pdgIds.size(); i++ )
223  {
224  // FIXME: need to double check if PID's are the same in Py6 & Py8,
225  // because the HepPDT translation tool is actually for **Py6**
226  //
227  int PyID = HepPID::translatePDTtoPythia( pdgIds[i] );
228  std::ostringstream pyCard ;
229  pyCard << PyID <<":mayDecay=false";
230  pythia->readString( pyCard.str() );
231  // alternative:
232  // set the 2nd input argument warn=false
233  // - this way Py8 will NOT print warnings about unknown particle code(s)
234  // pythia->readString( pyCard.str(), false )
235  }
236 
237  return true;
238 
239 }
240 bool Pythia8Hadronizer::declareSpecialSettings( const std::vector<std::string> )
241 {
242  return true;
243 }
244 
245 
247 {
248  pythia->statistics();
249 
250  double xsec = pythia->info.sigmaGen(); // cross section in mb
251  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
252  runInfo().setInternalXSec(xsec);
253 }
254 
256 {
257  if (!pythia->next())
258  return false;
259 
260  event().reset(new HepMC::GenEvent);
261  toHepMC.fill_next_event(*pythiaEvent, event().get());
262 
263  return true;
264 }
265 
267 {
268  if(LHEInputFileName == string()) {
269  //cout << "start loading event" << endl;
270  lhaUP->loadEvent(lheEvent());
271  //cout << "finish loading event" << endl;
272  }
273 
274  if (!pythia->next())
275  return false;
276 
277  // update LHE matching statistics
278  //
280 
281  event().reset(new HepMC::GenEvent);
282  toHepMC.fill_next_event(*pythiaEvent, event().get());
283 
284  return true;
285 }
286 
288 {
289  return true;
290 }
291 
293 {
294  return true;
295 }
296 
298 {
299  bool lhe = lheEvent() != 0;
300 
301  event()->set_signal_process_id(pythia->info.code());
302  event()->set_event_scale(pythia->info.pTHat()); //FIXME
303 
304  int id1 = pythia->info.id1();
305  int id2 = pythia->info.id2();
306  if (id1 == 21) id1 = 0;
307  if (id2 == 21) id2 = 0;
308  double x1 = pythia->info.x1();
309  double x2 = pythia->info.x2();
310  double Q = pythia->info.QRen();
311  double pdf1 = pythia->info.pdf1() / pythia->info.x1();
312  double pdf2 = pythia->info.pdf2() / pythia->info.x2();
313  event()->set_pdf_info(HepMC::PdfInfo(id1,id2,x1,x2,Q,pdf1,pdf2));
314 
315  event()->weights().push_back(pythia->info.weight());
316 
317  // now create the GenEventInfo product from the GenEvent and fill
318  // the missing pieces
319  eventInfo().reset( new GenEventInfoProduct( event().get() ) );
320 
321  // in pythia pthat is used to subdivide samples into different bins
322  // in LHE mode the binning is done by the external ME generator
323  // which is likely not pthat, so only filling it for Py6 internal mode
324  if (!lhe) {
325  eventInfo()->setBinningValues(std::vector<double>(1, pythia->info.pTHat()));
326  }
327 
328  //******** Verbosity ********
329 
330  if (maxEventsToPrint > 0 &&
333  if (pythiaPylistVerbosity) {
334  pythia->info.list(std::cout);
335  pythia->event.list(std::cout);
336  }
337 
338  if (pythiaHepMCVerbosity) {
339  std::cout << "Event process = "
340  << pythia->info.code() << "\n"
341  << "----------------------" << std::endl;
342  event()->print();
343  }
344  }
345 }
346 
349 
bool pythiaHepMCVerbosity
HepMC verbosity flag.
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
double comEnergy
Center-of-Mass energy.
edm::GeneratorFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8GeneratorFilter
bool declareStableParticles(const std::vector< int > &pdgIds)
std::auto_ptr< Pythia > pythia
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:198
bool declareSpecialSettings(const std::vector< std::string >)
static int getStatus(const HepMC::GenParticle *p)
void setInternalXSec(const XSec &xsec)
bool useUserHook
Switch User Hook flag.
std::auto_ptr< HepMC::GenEvent > & event()
CLHEP::HepRandomEngine & getEngineReference()
GenRunInfoProduct & runInfo()
std::auto_ptr< LHAupLesHouches > lhaUP
lhef::LHEEvent * lheEvent()
CLHEP::HepRandomEngine * randomEngine
Definition: PYR.cc:4
const char * classname() const
unsigned int pythiaPylistVerbosity
Pythia PYLIST Verbosity flag.
std::auto_ptr< GenEventInfoProduct > & eventInfo()
lhef::LHERunInfo * lheRunInfo()
ParameterCollector parameters
unsigned int maxEventsToPrint
Events to print if verbosity.
Pythia8Hadronizer(const edm::ParameterSet &params)
const_iterator end() const
const_iterator begin() const
tuple cout
Definition: gather_cfg.py:41
HepMC::I_Pythia8 toHepMC
tuple status
Definition: ntuplemaker.py:245
edm::HadronizerFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8HadronizerFilter