CMS 3D CMS Logo

Herwig7Hadronizer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <sstream>
3 #include <fstream>
4 
5 #include <HepMC/GenEvent.h>
6 #include <HepMC/IO_BaseClass.h>
7 
8 #include <ThePEG/Repository/Repository.h>
9 #include <ThePEG/EventRecord/Event.h>
10 #include <ThePEG/Config/ThePEG.h>
11 #include <ThePEG/LesHouches/LesHouchesReader.h>
14 
16 
20 
24 
26 
28 #include <Herwig/API/HerwigAPI.h>
29 #include "CLHEP/Random/RandomEngine.h"
30 
31 namespace CLHEP {
32  class HepRandomEngine;
33 }
34 
36  public:
37  Herwig7Hadronizer(const edm::ParameterSet &params);
38  ~Herwig7Hadronizer() override;
39 
40  bool readSettings( int ) { return true; }
41  bool initializeForInternalPartons();
42  bool initializeForExternalPartons();
43  bool declareStableParticles(const std::vector<int> &pdgIds);
44  bool declareSpecialSettings( const std::vector<std::string> ) { return true; }
45 
46  void statistics();
47 
48  bool generatePartonsAndHadronize();
49  bool hadronize();
50  bool decay();
51  bool residualDecay();
52  void finalizeEvent();
53 
54  const char *classname() const { return "Herwig7Hadronizer"; }
55 
56  std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const override;
57  void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&);
58  void randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine);
59 
60 
61  private:
62 
63  void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { setPEGRandomEngine(v); }
64 
65  unsigned int eventsToPrint;
66 
67  ThePEG::EventPtr thepegEvent;
68  bool haveEvt = false;
69 
70  std::shared_ptr<lhef::LHEProxy> proxy_;
74 
75  unsigned int firstLumiBlock=0;
76  unsigned int currentLumiBlock=0;
77 };
78 
80  Herwig7Interface(pset),
81  BaseHadronizer(pset),
82  eventsToPrint(pset.getUntrackedParameter<unsigned int>("eventsToPrint", 0)),
83  handlerDirectory_(pset.getParameter<std::string>("eventHandlers")),
84  runFileName(pset.getParameter<std::string>("run"))
85 {
86  initRepository(pset);
88 }
89 
91 {
92 }
93 
95 {
97  {
98  std::ifstream runFile(runFileName+".run");
99  if (runFile.fail()) //required for showering of LHE files
100  {
102  }
103  if (!initGenerator())
104  {
105  edm::LogInfo("Generator|Herwig7Hadronizer") << "No run step for Herwig chosen. Program will be aborted.";
106  exit(0);
107  }
108  }
109  return true;
110 }
111 
113 {
115  {
116  std::ifstream runFile(runFileName+".run");
117  if (runFile.fail()) //required for showering of LHE files
118  {
120  }
121  if (!initGenerator())
122  {
123  edm::LogInfo("Generator|Herwig7Hadronizer") << "No run step for Herwig chosen. Program will be aborted.";
124  exit(0);
125  }
126  }
127  return true;
128 }
129 
130 bool Herwig7Hadronizer::declareStableParticles(const std::vector<int> &pdgIds)
131 {
132  return false;
133 }
134 
136 {
137  if(eg_){
139  eg_->integratedXSec() / ThePEG::picobarn,
140  eg_->integratedXSecErr() / ThePEG::picobarn));
141  }
142 }
143 
145 {
146  edm::LogInfo("Generator|Herwig7Hadronizer") << "Start production";
147 
148  try {
149  thepegEvent = eg_->shoot();
150  } catch (std::exception& exc) {
151  edm::LogWarning("Generator|Herwig7Hadronizer") << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
152  return false;
153  }
154 
155  if (!thepegEvent) {
156  edm::LogWarning("Generator|Herwig7Hadronizer") << "thepegEvent not initialized";
157  return false;
158  }
159 
161  if (!event().get()) {
162  edm::LogWarning("Generator|Herwig7Hadronizer") << "genEvent not initialized";
163  return false;
164  }
165 
166  return true;
167 }
168 
170 {
171  if (!haveEvt) {
172  try {
173  thepegEvent = eg_->shoot();
174  haveEvt = true;
175  } catch (std::exception& exc) {
176  edm::LogWarning("Generator|Herwig7Hadronizer") << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
177  return false;
178  }
179  }
180  int evtnum = lheEvent()->evtnum();
181  if (evtnum == -1) {
182  edm::LogError("Generator|Herwig7Hadronizer") << "Event number not set in lhe file, needed for correctly aligning Herwig and LHE events!";
183  return false;
184  } else if (thepegEvent->number() == evtnum) {
185  edm::LogError("Herwig7 interface") << "Herwig does not seem to be generating events in order, did you set /Herwig/EventHandlers/FxFxLHReader:AllowedToReOpen Yes?";
186  return false;
187  } else if (thepegEvent->number() == evtnum) {
188  haveEvt = false;
189  if (!thepegEvent) {
190  edm::LogWarning("Generator|Herwig7Hadronizer") << "thepegEvent not initialized";
191  return false;
192  }
193 
195  if (!event().get()) {
196  edm::LogWarning("Generator|Herwig7Hadronizer") << "genEvent not initialized";
197  return false;
198  }
199  return true;
200  }
201  edm::LogWarning("Generator|Herwig7Hadronizer") << "Event " << evtnum << " not generated (likely skipped in merging)";
202  return false;
203 }
204 
205 std::unique_ptr<GenLumiInfoHeader> Herwig7Hadronizer::getGenLumiInfoHeader() const {
206  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
207 
208  if (thepegEvent)
209  {
210  int weights_number = thepegEvent->optionalWeights().size();
211 
212  if(weights_number > 1){
213  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
214  genLumiInfoHeader->weightNames().push_back("nominal");
215  std::map<std::string,double> weights_map = thepegEvent->optionalWeights();
216  for (std::map<std::string,double>::iterator it = weights_map.begin(); it != weights_map.end(); it++)
217  {
218  genLumiInfoHeader->weightNames().push_back(it->first);
219  }
220  }
221  }
222 
223  return genLumiInfoHeader;
224 }
225 
226 void Herwig7Hadronizer::randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine)
227 {
228  BaseHadronizer::randomizeIndex(lumi, rengine);
229 
230  if (firstLumiBlock==0)
231  {
233  }
235 
236 }
237 
239 {
240  eventInfo().reset(new GenEventInfoProduct(event().get()));
241  eventInfo()->setBinningValues(
242  std::vector<double>(1, pthat(thepegEvent)));
243 
244  if (eventsToPrint) {
245  eventsToPrint--;
246  event()->print();
247  }
248 
249  if (iobc_.get())
250  iobc_->write_event(event().get());
251 
252  edm::LogInfo("Generator|Herwig7Hadronizer") << "Event produced";
253 }
254 
256 {
257  return true;
258 }
259 
261 {
262  return true;
263 }
264 
266 
269 
LuminosityBlockID id() const
~Herwig7Hadronizer() override
static double pthat(const ThePEG::EventPtr &event)
void initRepository(const edm::ParameterSet &params)
unsigned int firstLumiBlock
std::unique_ptr< HepMC::IO_BaseClass > iobc_
void setInternalXSec(const XSec &xsec)
GenRunInfoProduct & runInfo()
ThePEG::EGPtr eg_
bool declareSpecialSettings(const std::vector< std::string >)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
lhef::LHEEvent * lheEvent()
edm::GeneratorFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7GeneratorFilter
unsigned int eventsToPrint
static std::unique_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
edm::ParameterSet paramSettings
edm::HadronizerFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7HadronizerFilter
bool initializeForInternalPartons()
std::unique_ptr< HepMC::GenEvent > & event()
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::shared_ptr< lhef::LHEProxy > proxy_
std::unique_ptr< GenEventInfoProduct > & eventInfo()
LuminosityBlockNumber_t luminosityBlock() const
unsigned int currentLumiBlock
Herwig7Hadronizer(const edm::ParameterSet &params)
ThePEG::EventPtr thepegEvent
const char * classname() const
int evtnum() const
Definition: LHEEvent.h:58
std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const override
const std::string handlerDirectory_
const std::string runFileName
bool declareStableParticles(const std::vector< int > &pdgIds)
Definition: event.py:1
void randomizeIndex(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine)