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:
38  Herwig7Hadronizer(const edm::ParameterSet &params);
39  ~Herwig7Hadronizer() override;
40 
41  bool readSettings( int ) { return true; }
42  bool initializeForInternalPartons();
43  bool initializeForExternalPartons();
44  bool declareStableParticles(const std::vector<int> &pdgIds);
45  bool declareSpecialSettings( const std::vector<std::string> ) { return true; }
46 
47  void statistics();
48 
49  bool generatePartonsAndHadronize();
50  bool hadronize();
51  bool decay();
52  bool residualDecay();
53  void finalizeEvent();
54 
55  const char *classname() const { return "Herwig7Hadronizer"; }
56  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 
69  std::shared_ptr<lhef::LHEProxy> proxy_;
73 
74  unsigned int firstLumiBlock=0;
75  unsigned int currentLumiBlock=0;
76 };
77 
79  Herwig7Interface(pset),
80  BaseHadronizer(pset),
81  eventsToPrint(pset.getUntrackedParameter<unsigned int>("eventsToPrint", 0)),
82  handlerDirectory_(pset.getParameter<std::string>("eventHandlers")),
83  runFileName(pset.getParameter<std::string>("run"))
84 {
85  initRepository(pset);
87 }
88 
90 {
91 }
92 
94 {
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 {
114  edm::LogError("Herwig7 interface") << "Read in of LHE files is not supported in this way. You can read them manually if necessary.";
115  return false;
116 }
117 
118 bool Herwig7Hadronizer::declareStableParticles(const std::vector<int> &pdgIds)
119 {
120  return false;
121 }
122 
124 {
125  if(eg_){
127  eg_->integratedXSec() / ThePEG::picobarn,
128  eg_->integratedXSecErr() / ThePEG::picobarn));
129  }
130 }
131 
133 {
134  edm::LogInfo("Generator|Herwig7Hadronizer") << "Start production";
135 
136 
137  try {
138  thepegEvent = eg_->shoot();
139  } catch (std::exception& exc) {
140  edm::LogWarning("Generator|Herwig7Hadronizer") << "EGPtr::shoot() thrown an exception, event skipped: " << exc.what();
141  return false;
142  }
143 
144  if (!thepegEvent) {
145  edm::LogWarning("Generator|Herwig7Hadronizer") << "thepegEvent not initialized";
146  return false;
147  }
148 
150  if (!event().get()) {
151  edm::LogWarning("Generator|Herwig7Hadronizer") << "genEvent not initialized";
152  return false;
153  }
154 
155  return true;
156 }
157 
159 {
160 
161  edm::LogError("Herwig7 interface") << "Read in of LHE files is not supported in this way. You can read them manually if necessary.";
162  return false;
163 }
164 
166 {
167  eventInfo().reset(new GenEventInfoProduct(event().get()));
168  eventInfo()->setBinningValues(
169  std::vector<double>(1, pthat(thepegEvent)));
170 
171  if (eventsToPrint) {
172  eventsToPrint--;
173  event()->print();
174  }
175 
176  if (iobc_.get())
177  iobc_->write_event(event().get());
178 
179  edm::LogInfo("Generator|Herwig7Hadronizer") << "Event produced";
180 }
181 
183 {
184  return true;
185 }
186 
188 {
189  return true;
190 }
191 
193  GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
194 
195  if (thepegEvent)
196  {
197  int weights_number = thepegEvent->optionalWeights().size();
198 
199  if(weights_number > 1){
200  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
201  genLumiInfoHeader->weightNames().push_back("nominal");
202  std::map<std::string,double> weights_map = thepegEvent->optionalWeights();
203  for (std::map<std::string,double>::iterator it = weights_map.begin(); it != weights_map.end(); it++)
204  {
205  genLumiInfoHeader->weightNames().push_back(it->first);
206  }
207  }
208  }
209 
210  return genLumiInfoHeader;
211 }
212 
213 void Herwig7Hadronizer::randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine)
214 {
215  BaseHadronizer::randomizeIndex(lumi, rengine);
216 
217  if (firstLumiBlock==0)
218  {
220  }
222 
223 }
224 
225 
227 
230 
LuminosityBlockID id() const
~Herwig7Hadronizer() override
static double pthat(const ThePEG::EventPtr &event)
void initRepository(const edm::ParameterSet &params)
GenLumiInfoHeader * getGenLumiInfoHeader() const override
unsigned int firstLumiBlock
std::auto_ptr< HepMC::IO_BaseClass > iobc_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const std::vector< std::string > & weightNames() const
void setInternalXSec(const XSec &xsec)
std::auto_ptr< HepMC::GenEvent > & event()
GenRunInfoProduct & runInfo()
ThePEG::EGPtr eg_
bool declareSpecialSettings(const std::vector< std::string >)
edm::GeneratorFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7GeneratorFilter
unsigned int eventsToPrint
edm::ParameterSet paramSettings
edm::HadronizerFilter< Herwig7Hadronizer, gen::ExternalDecayDriver > Herwig7HadronizerFilter
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
bool initializeForInternalPartons()
std::auto_ptr< GenEventInfoProduct > & eventInfo()
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::shared_ptr< lhef::LHEProxy > proxy_
LuminosityBlockNumber_t luminosityBlock() const
unsigned int currentLumiBlock
Herwig7Hadronizer(const edm::ParameterSet &params)
ThePEG::EventPtr thepegEvent
const char * classname() const
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)