CMS 3D CMS Logo

ThePEGHadronizer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <sstream>
3 
4 #include <HepMC/GenEvent.h>
5 #include <HepMC/IO_BaseClass.h>
6 
7 #include <ThePEG/Repository/Repository.h>
8 #include <ThePEG/EventRecord/Event.h>
9 #include <ThePEG/Config/ThePEG.h>
10 #include <ThePEG/LesHouches/LesHouchesReader.h>
11 
13 
17 
21 
23 
25 
26 namespace CLHEP {
27  class HepRandomEngine;
28 }
29 
31  public:
32  ThePEGHadronizer(const edm::ParameterSet &params);
33  ~ThePEGHadronizer() noexcept override {};
34 
35  bool readSettings( int ) { return true; }
36  bool initializeForInternalPartons();
37  bool initializeForExternalPartons();
38  bool declareStableParticles(const std::vector<int> &pdgIds);
39  bool declareSpecialSettings( const std::vector<std::string> ) { return true; }
40 
41  void statistics();
42 
43  bool generatePartonsAndHadronize();
44  bool hadronize();
45  bool decay();
46  bool residualDecay();
47  void finalizeEvent();
48 
49  const char *classname() const { return "ThePEGHadronizer"; }
50 
51  private:
52 
53  void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { setPEGRandomEngine(v); }
54 
55  unsigned int eventsToPrint;
56 
57  ThePEG::EventPtr thepegEvent;
58 
59  boost::shared_ptr<lhef::LHEProxy> proxy_;
61 };
62 
64  ThePEGInterface(pset),
65  BaseHadronizer(pset),
66  eventsToPrint(pset.getUntrackedParameter<unsigned int>("eventsToPrint", 0)),
67  handlerDirectory_(pset.getParameter<std::string>("eventHandlers"))
68 {
69  initRepository(pset);
70 
71 }
72 
74 {
75  initGenerator();
76  return true;
77 }
78 
80 {
82 
83  std::ostringstream ss;
84  ss << proxy_->getID();
85 
86  std::ostringstream logstream;
87  ThePEG::Repository::exec("set " + handlerDirectory_ +
88  "/LHEReader:ProxyID " + ss.str(), logstream);
89  edm::LogInfo("Generator|LHEInterface") << logstream.str();
90 
91  proxy_->loadRunInfo(getLHERunInfo());
92 
93  initGenerator();
94  return true;
95 }
96 
97 bool ThePEGHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
98 {
99  return false;
100 }
101 
103 {
105  eg_->integratedXSec() / ThePEG::picobarn,
106  eg_->integratedXSecErr() / ThePEG::picobarn));
107 }
108 
110 {
111  edm::LogInfo("Generator|ThePEGHadronizer") << "Start production";
112 
114 
115  try {
116  thepegEvent = eg_->shoot();
117  } catch (std::exception& exc) {
118  edm::LogWarning("Generator|ThePEGHadronizer") << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
119  return false;
120  } catch (...) {
121  edm::LogWarning("Generator|ThePEGHadronizer") << "EGPtr::shoot() thrown an unknown exception, event skipped";
122  return false;
123  }
124 
125  if (!thepegEvent) {
126  edm::LogWarning("Generator|ThePEGHadronizer") << "thepegEvent not initialized";
127  return false;
128  }
129 
131  if (!event().get()) {
132  edm::LogWarning("Generator|ThePEGHadronizer") << "genEvent not initialized";
133  return false;
134  }
135 
136  return true;
137 }
138 
140 {
141  edm::LogInfo("Generator|ThePEGHadronizer") << "Start production";
142 
144 
145  //need to copy lhe event here unfortunately because of interface mismatch
146  proxy_->loadEvent(boost::shared_ptr<lhef::LHEEvent>(new lhef::LHEEvent(*lheEvent())));
147 
148  //dummy for now
149  double mergeweight = 1.0;
150 
151  try {
152  thepegEvent = eg_->shoot();
153  } catch (std::exception& exc) {
154  edm::LogWarning("Generator|ThePEGHadronizer") << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
155  lheEvent()->count( lhef::LHERunInfo::kSelected, 1.0, mergeweight );
156  return false;
157  } catch (...) {
158  edm::LogWarning("Generator|ThePEGHadronizer") << "EGPtr::shoot() thrown an unknown exception, event skipped";
159  lheEvent()->count( lhef::LHERunInfo::kSelected, 1.0, mergeweight );
160  return false;
161  }
162 
163  if (!thepegEvent) {
164  edm::LogWarning("Generator|ThePEGHadronizer") << "thepegEvent not initialized";
165  lheEvent()->count( lhef::LHERunInfo::kSelected, 1.0, mergeweight );
166  return false;
167  }
168 
170  if (!event().get()) {
171  edm::LogWarning("Generator|ThePEGHadronizer") << "genEvent not initialized";
172  lheEvent()->count( lhef::LHERunInfo::kSelected, 1.0, mergeweight );
173  return false;
174  }
175 
176  //Fill LHE weight (since it's not otherwise propagated)
177  event()->weights()[0] *= lheEvent()->getHEPEUP()->XWGTUP;
178 
179  HepMC::PdfInfo pdf;
180  clearAuxiliary(event().get(), &pdf);
181  lheEvent()->fillPdfInfo(&pdf);
182  fillAuxiliary(event().get(), &pdf, thepegEvent);
183  event()->set_pdf_info(pdf);
184 
185  // update LHE matching statistics
186  //
187  lheEvent()->count( lhef::LHERunInfo::kAccepted, 1.0, mergeweight );
188 
189  return true;
190 
191 }
192 
194 {
195  HepMC::PdfInfo pdf;
196  clearAuxiliary(event().get(), &pdf);
197  fillAuxiliary(event().get(), &pdf, thepegEvent);
198  event()->set_pdf_info(pdf);
199 
200  eventInfo().reset(new GenEventInfoProduct(event().get()));
201  eventInfo()->setBinningValues(
202  std::vector<double>(1, pthat(thepegEvent)));
203 
204  if (eventsToPrint) {
205  eventsToPrint--;
206  event()->print();
207  }
208 
209  if (iobc_.get())
210  iobc_->write_event(event().get());
211 
212  edm::LogInfo("Generator|ThePEGHadronizer") << "Event produced";
213 }
214 
216 {
217  return true;
218 }
219 
221 {
222  return true;
223 }
224 
226 
229 
unsigned int eventsToPrint
void initRepository(const edm::ParameterSet &params) const
edm::HadronizerFilter< ThePEGHadronizer, gen::ExternalDecayDriver > ThePEGHadronizerFilter
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
#define noexcept
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:207
const std::string handlerDirectory_
static double pthat(const ThePEG::EventPtr &event)
void setInternalXSec(const XSec &xsec)
const HEPEUP * getHEPEUP() const
Definition: LHEEvent.h:43
std::auto_ptr< HepMC::GenEvent > & event()
bool declareStableParticles(const std::vector< int > &pdgIds)
GenRunInfoProduct & runInfo()
bool initializeForInternalPartons()
lhef::LHEEvent * lheEvent()
edm::GeneratorFilter< ThePEGHadronizer, gen::ExternalDecayDriver > ThePEGGeneratorFilter
void fillPdfInfo(HepMC::PdfInfo *info) const
Definition: LHEEvent.cc:221
void flushRandomNumberGenerator()
ThePEG::EventPtr thepegEvent
std::auto_ptr< HepMC::IO_BaseClass > iobc_
ThePEG::EGPtr eg_
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
bool generatePartonsAndHadronize()
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::auto_ptr< GenEventInfoProduct > & eventInfo()
ThePEGHadronizer(const edm::ParameterSet &params)
bool initializeForExternalPartons()
boost::shared_ptr< lhef::LHEProxy > proxy_
const char * classname() const
~ThePEGHadronizer() override
bool declareSpecialSettings(const std::vector< std::string >)
double XWGTUP
Definition: LesHouches.h:196
const boost::shared_ptr< lhef::LHERunInfo > & getLHERunInfo() const
static void clearAuxiliary(HepMC::GenEvent *hepmc, HepMC::PdfInfo *pdf)
Definition: event.py:1
static boost::shared_ptr< LHEProxy > create()
Definition: LHEProxy.cc:41
static void fillAuxiliary(HepMC::GenEvent *hepmc, HepMC::PdfInfo *pdf, const ThePEG::EventPtr &event)