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>
12 
16 
20 
24 
26 
28 
29 #include <Herwig/API/HerwigAPI.h>
30 #include "CLHEP/Random/RandomEngine.h"
31 
32 namespace CLHEP {
33  class HepRandomEngine;
34 }
35 
37 public:
39  ~Herwig7Hadronizer() override;
40 
41  bool readSettings(int) { return true; }
44  bool declareStableParticles(const std::vector<int>& pdgIds);
45  bool declareSpecialSettings(const std::vector<std::string>) { return true; }
46 
47  void statistics();
48 
50  bool hadronize();
51  bool decay();
52  bool residualDecay();
53  void finalizeEvent();
54 
55  const char* classname() const { return "Herwig7Hadronizer"; }
56  std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const override;
58  void randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine);
59 
60 private:
61  void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { setPEGRandomEngine(v); }
62 
63  unsigned int eventsToPrint;
64 
65  ThePEG::EventPtr thepegEvent;
66 
67  std::shared_ptr<lhef::LHEProxy> proxy_;
71 
72  unsigned int firstLumiBlock = 0;
73  unsigned int currentLumiBlock = 0;
74 };
75 
78  BaseHadronizer(pset),
79  eventsToPrint(pset.getUntrackedParameter<unsigned int>("eventsToPrint", 0)),
80  handlerDirectory_(pset.getParameter<std::string>("eventHandlers")),
81  runFileName(pset.getParameter<std::string>("run")) {
84 }
85 
87 
90  std::ifstream runFile(runFileName + ".run");
91  if (runFile.fail()) //required for showering of LHE files
92  {
94  }
95  if (!initGenerator()) {
96  edm::LogInfo("Generator|Herwig7Hadronizer") << "No run step for Herwig chosen. Program will be aborted.";
97  exit(0);
98  }
99  }
100  return true;
101 }
102 
104  edm::LogError("Herwig7 interface")
105  << "Read in of LHE files is not supported in this way. You can read them manually if necessary.";
106  return false;
107 }
108 
109 bool Herwig7Hadronizer::declareStableParticles(const std::vector<int>& pdgIds) { return false; }
110 
112  if (eg_) {
114  GenRunInfoProduct::XSec(eg_->integratedXSec() / ThePEG::picobarn, eg_->integratedXSecErr() / ThePEG::picobarn));
115  }
116 }
117 
119  edm::LogInfo("Generator|Herwig7Hadronizer") << "Start production";
120 
121  try {
122  thepegEvent = eg_->shoot();
123  } catch (std::exception& exc) {
124  edm::LogWarning("Generator|Herwig7Hadronizer")
125  << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
126  return false;
127  }
128 
129  if (!thepegEvent) {
130  edm::LogWarning("Generator|Herwig7Hadronizer") << "thepegEvent not initialized";
131  return false;
132  }
133 
135  if (!event().get()) {
136  edm::LogWarning("Generator|Herwig7Hadronizer") << "genEvent not initialized";
137  return false;
138  }
139 
140  return true;
141 }
142 
144  edm::LogError("Herwig7 interface")
145  << "Read in of LHE files is not supported in this way. You can read them manually if necessary.";
146  return false;
147 }
148 
150  eventInfo() = std::make_unique<GenEventInfoProduct>(event().get());
151  eventInfo()->setBinningValues(std::vector<double>(1, pthat(thepegEvent)));
152 
153  if (eventsToPrint) {
154  eventsToPrint--;
155  event()->print();
156  }
157 
158  if (iobc_.get())
159  iobc_->write_event(event().get());
160 
161  edm::LogInfo("Generator|Herwig7Hadronizer") << "Event produced";
162 }
163 
164 bool Herwig7Hadronizer::decay() { return true; }
165 
166 bool Herwig7Hadronizer::residualDecay() { return true; }
167 
168 std::unique_ptr<GenLumiInfoHeader> Herwig7Hadronizer::getGenLumiInfoHeader() const {
169  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
170 
171  if (thepegEvent) {
172  int weights_number = thepegEvent->optionalWeights().size();
173 
174  if (weights_number > 1) {
175  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
176  genLumiInfoHeader->weightNames().push_back("nominal");
177  std::map<std::string, double> weights_map = thepegEvent->optionalWeights();
178  for (std::map<std::string, double>::iterator it = weights_map.begin(); it != weights_map.end(); it++) {
179  genLumiInfoHeader->weightNames().push_back(it->first);
180  }
181  }
182  }
183 
184  return genLumiInfoHeader;
185 }
186 
187 void Herwig7Hadronizer::randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine) {
188  BaseHadronizer::randomizeIndex(lumi, rengine);
189 
190  if (firstLumiBlock == 0) {
191  firstLumiBlock = lumi.id().luminosityBlock();
192  }
193  currentLumiBlock = lumi.id().luminosityBlock();
194 }
195 
197 
200 
~Herwig7Hadronizer() override
static double pthat(const ThePEG::EventPtr &event)
void initRepository(const edm::ParameterSet &params)
unsigned int firstLumiBlock
void setPEGRandomEngine(CLHEP::HepRandomEngine *)
std::auto_ptr< HepMC::IO_BaseClass > iobc_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Error, false > LogError
void setInternalXSec(const XSec &xsec)
GenRunInfoProduct & runInfo()
ThePEG::EGPtr eg_
bool declareSpecialSettings(const std::vector< std::string >)
const char * classname() const
edm::GeneratorFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7GeneratorFilter
unsigned int eventsToPrint
std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const override
edm::ParameterSet paramSettings
edm::HadronizerFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7HadronizerFilter
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
bool initializeForInternalPartons()
std::unique_ptr< HepMC::GenEvent > & event()
Log< level::Info, false > LogInfo
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::shared_ptr< lhef::LHEProxy > proxy_
std::unique_ptr< GenEventInfoProduct > & eventInfo()
unsigned int currentLumiBlock
Herwig7Hadronizer(const edm::ParameterSet &params)
ThePEG::EventPtr thepegEvent
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
const std::string handlerDirectory_
const std::string runFileName
bool declareStableParticles(const std::vector< int > &pdgIds)
Log< level::Warning, false > LogWarning
Definition: event.py:1
void randomizeIndex(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine)
def exit(msg="")