CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ThePEGInterface.cc
Go to the documentation of this file.
1 
7 #include <iostream>
8 #include <sstream>
9 #include <cstdlib>
10 #include <memory>
11 #include <cmath>
12 #include <stdlib.h>
13 
14 #include <boost/shared_ptr.hpp>
15 #include <boost/filesystem.hpp>
16 
17 #include <HepMC/GenEvent.h>
18 #include <HepMC/PdfInfo.h>
19 #include <HepMC/IO_GenEvent.h>
20 
21 #include <ThePEG/Utilities/DynamicLoader.h>
22 #include <ThePEG/Repository/Repository.h>
23 #include <ThePEG/Handlers/EventHandler.h>
24 #include <ThePEG/Handlers/XComb.h>
25 #include <ThePEG/EventRecord/Event.h>
26 #include <ThePEG/EventRecord/Particle.h>
27 #include <ThePEG/EventRecord/Collision.h>
28 #include <ThePEG/EventRecord/TmpTransform.h>
29 #include <ThePEG/Config/ThePEG.h>
30 #include <ThePEG/PDF/PartonExtractor.h>
31 #include <ThePEG/PDF/PDFBase.h>
32 #include <ThePEG/Utilities/UtilityBase.h>
33 #include <ThePEG/Vectors/HepMCConverter.h>
34 
35 
37 
39 
43 
44 using namespace std;
45 using namespace gen;
46 
48  randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
49  dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
50  generator_(pset.getParameter<string>("generatorModule")),
51  run_(pset.getParameter<string>("run")),
52  dumpConfig_(pset.getUntrackedParameter<string>("dumpConfig", "")),
53  skipEvents_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0))
54 {
55  // Write events in hepmc ascii format for debugging purposes
56  string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
57  if (!dumpEvents.empty()) {
58  iobc_.reset(new HepMC::IO_GenEvent(dumpEvents.c_str(), ios::out));
59  edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
60  }
61  // Clear dumpConfig target
62  if (!dumpConfig_.empty())
63  ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
64 }
65 
67 {
68  if (eg_)
69  eg_->finalize();
70  edm::LogInfo("ThePEGInterface") << "Event generator finalized";
71 }
72 
73 void ThePEGInterface::setPEGRandomEngine(CLHEP::HepRandomEngine* v) {
74  randomEngineGlueProxy_->setRandomEngine(v);
76  if(rnd) {
77  rnd->setRandomEngine(v);
78  }
79 }
80 
81 string ThePEGInterface::dataFile(const string &fileName) const
82 {
83  if (fileName.empty() || fileName[0] == '/')
84  return fileName;
85  else
86  return dataLocation_ + "/" + fileName;
87 }
88 
90  const string &paramName) const
91 {
92  const edm::Entry &entry = pset.retrieve(paramName);
93  if (entry.typeCode() == 'F')
94  return entry.getFileInPath().fullPath();
95  else
96  return dataFile(entry.getString());
97 }
98 
100 {
101  /* Initialize the repository from
102  * 1. the repository file (default values)
103  * 2. the ThePEG config files
104  * 3. the CMSSW config blocks
105  */
106  stringstream logstream;
107 
108  // Read the repository of serialized default values
109  string repository = dataFile(pset, "repository");
110  if (!repository.empty()) {
111  edm::LogInfo("ThePEGInterface") << "Loading repository (" << repository << ")";
112  ThePEG::Repository::load(repository);
113  }
114 
115  if (!getenv("ThePEG_INSTALL_PATH")) {
116  vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
117  for(vector<string>::const_iterator libdir = libdirlist.begin();
118  libdir < libdirlist.end(); ++libdir) {
119  if (libdir->empty() || (*libdir)[0] != '/')
120  continue;
121  if (boost::filesystem::exists(*libdir +
122  "/../../share/ThePEG/PDFsets.index")) {
123  setenv("ThePEG_INSTALL_PATH",
124  libdir->c_str(), 0);
125  break;
126  }
127  }
128  }
129 
130  // Read ThePEG config files to read
131  vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
132 
133  // Loop over the config files
134  for(vector<string>::const_iterator iter = configFiles.begin();
135  iter != configFiles.end(); ++iter) {
136  edm::LogInfo("ThePEGInterface") << "Reading config file (" << *iter << ")";
137  ThePEG::Repository::read(dataFile(*iter), logstream);
138  edm::LogInfo("ThePEGSource") << logstream.str();
139  }
140 
141  // Read CMSSW config file parameter sets starting from "parameterSets"
142 
143  ofstream cfgDump;
144  ParameterCollector collector(pset);
146  if (!dumpConfig_.empty()) {
147  cfgDump.open(dumpConfig_.c_str(), ios_base::app);
148  iter = collector.begin(cfgDump);
149  } else
150  iter = collector.begin();
151 
152  for(; iter != collector.end(); ++iter) {
153  string out = ThePEG::Repository::exec(*iter, logstream);
154  if (!out.empty()) {
155  edm::LogInfo("ThePEGInterface") << *iter << " => " << out;
156  cerr << "Error in ThePEG configuration!\n"
157  "\tLine: " << *iter << "\n" << out << endl;
158  }
159  }
160 
161  if (!dumpConfig_.empty()) {
162  cfgDump << "saverun " << run_ << " " << generator_ << endl;
163  cfgDump.close();
164  }
165 
166  // write the ProxyID for the RandomEngineGlue to fill its pointer in
167  ostringstream ss;
168  ss << randomEngineGlueProxy_->getID();
169  ThePEG::Repository::exec("set " + generator_ +
170  ":RandomNumberGenerator:ProxyID " + ss.str(),
171  logstream);
172 
173  // Print the directories where ThePEG looks for libs
174  vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
175  for(vector<string>::const_iterator libdir = libdirlist.begin();
176  libdir < libdirlist.end(); ++libdir)
177  edm::LogInfo("ThePEGInterface") << "DynamicLoader path = " << *libdir << endl;
178 
179  // Output status information about the repository
180  ThePEG::Repository::stats(logstream);
181  edm::LogInfo("ThePEGInterface") << logstream.str();
182 }
183 
185 {
186  // Get generator from the repository and initialize it
187  ThePEG::BaseRepository::CheckObjectDirectory(generator_);
188  ThePEG::EGPtr tmp = ThePEG::BaseRepository::GetObject<ThePEG::EGPtr>(generator_);
189  if (tmp) {
190  eg_ = ThePEG::Repository::makeRun(tmp, run_);
191  eg_->initialize();
192  edm::LogInfo("ThePEGInterface") << "EventGenerator initialized";
193  } else
194  throw cms::Exception("ThePEGInterface")
195  << "EventGenerator could not be initialized!" << endl;
196 
197  // Skip events
198  for (unsigned int i = 0; i < skipEvents_; i++) {
200  eg_->shoot();
201  edm::LogInfo("ThePEGInterface") << "Event discarded";
202  }
203 }
204 
206 {
207  ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
208 
209  if (!rnd)
210  edm::LogWarning("ProxyMissing")
211  << "ThePEG not initialised with RandomEngineGlue.";
212  else
213  rnd->flush();
214 }
215 
216 auto_ptr<HepMC::GenEvent> ThePEGInterface::convert(
217  const ThePEG::EventPtr &event)
218 {
219  return std::auto_ptr<HepMC::GenEvent>(
221 }
222 
223 void ThePEGInterface::clearAuxiliary(HepMC::GenEvent *hepmc,
224  HepMC::PdfInfo *pdf)
225 {
226  if (hepmc) {
227  hepmc->set_event_scale(-1.0);
228  hepmc->set_alphaQCD(-1.0);
229  hepmc->set_alphaQED(-1.0);
230  }
231  if (pdf) {
232  pdf->set_id1(-100);
233  pdf->set_id2(-100);
234  pdf->set_x1(-1.0);
235  pdf->set_x2(-1.0);
236  pdf->set_scalePDF(-1.0);
237  pdf->set_pdf1(-1.0);
238  pdf->set_pdf2(-1.0);
239  }
240 }
241 
242 void ThePEGInterface::fillAuxiliary(HepMC::GenEvent *hepmc,
243  HepMC::PdfInfo *pdf,
244  const ThePEG::EventPtr &event)
245 {
246  if (!event->primaryCollision())
247  return;
248 
249  ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
250  event->primaryCollision()->handler());
251  double scale = eh->lastScale();
252 
253  if (hepmc) {
254  if (hepmc->event_scale() < 0 && scale > 0)
255  hepmc->set_event_scale(std::sqrt(scale) / ThePEG::GeV);
256 
257  if (hepmc->alphaQCD() < 0)
258  hepmc->set_alphaQCD(eh->lastAlphaS());
259  if (hepmc->alphaQED() < 0)
260  hepmc->set_alphaQED(eh->lastAlphaEM());
261  }
262 
263  if (pdf) {
264  const ThePEG::PPair &beams = eh->lastParticles();
265  const ThePEG::PPair &partons = eh->lastPartons();
266  ThePEG::tcPDFPtr pdf1 = eh->lastExtractor()->getPDF(
267  beams.first->dataPtr());
268  ThePEG::tcPDFPtr pdf2 = eh->lastExtractor()->getPDF(
269  beams.second->dataPtr());
270  double x1, x2;
271 
272  if (pdf->id1() == -100) {
273  int id = partons.first->id();
274  pdf->set_id1(id == 21 ? 0 : id);
275  }
276  if (pdf->id2() == -100) {
277  int id = partons.second->id();
278  pdf->set_id2(id == 21 ? 0 : id);
279  }
280 
281  if (pdf->scalePDF() < 0)
282  pdf->set_scalePDF(std::sqrt(scale) / ThePEG::GeV);
283  else
284  scale = ThePEG::sqr(pdf->scalePDF()) * ThePEG::GeV;
285 
286  if (pdf->x1() < 0) {
287  x1 = eh->lastX1();
288  pdf->set_x1(x1);
289  } else
290  x1 = pdf->x1();
291 
292  if (pdf->x2() < 0) {
293  x2 = eh->lastX2();
294  pdf->set_x2(x2);
295  } else
296  x2 = pdf->x2();
297 
298  if (pdf1 && pdf->pdf1() < 0) {
299  double v = pdf1->xfx(beams.first->dataPtr(),
300  partons.first->dataPtr(),
301  scale, x1);
302  if (v > 0 && x1 > 0)
303  pdf->set_pdf1(v / x1);
304  else
305  pdf->set_pdf2(-1.0);
306  }
307  if (pdf2 && pdf->pdf2() < 0) {
308  double v = pdf2->xfx(beams.first->dataPtr(),
309  partons.first->dataPtr(),
310  scale, x2);
311  if (v > 0 && x2 > 0)
312  pdf->set_pdf2(v / x2);
313  else
314  pdf->set_pdf2(-1.0);
315  }
316  }
317 
318 }
319 
320 double ThePEGInterface::pthat(const ThePEG::EventPtr &event)
321 {
322  using namespace ThePEG;
323 
324  if (!event->primaryCollision())
325  return -1.0;
326 
327  tSubProPtr sub = event->primaryCollision()->primarySubProcess();
328  TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(
329  sub->incoming()));
330 
331  double pthat = (*sub->outgoing().begin())->momentum().perp();
332  for(PVector::const_iterator it = sub->outgoing().begin();
333  it != sub->outgoing().end(); ++it)
334  pthat = std::min(pthat, (*it)->momentum().perp());
335 
336  return pthat / ThePEG::GeV;
337 }
T getParameter(std::string const &) const
Entry const & retrieve(char const *) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void initRepository(const edm::ParameterSet &params) const
const double GeV
Definition: MathUtil.h:16
ThePEGInterface(const edm::ParameterSet &params)
static double pthat(const ThePEG::EventPtr &event)
std::string dataFile(const std::string &fileName) const
const std::string run_
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
T sqrt(T t)
Definition: SSEVec.h:18
def load
Definition: svgfig.py:546
void flushRandomNumberGenerator()
std::auto_ptr< HepMC::IO_BaseClass > iobc_
ThePEG::EGPtr eg_
T min(T a, T b)
Definition: MathUtil.h:58
const std::string dumpConfig_
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
const std::string generator_
FileInPath getFileInPath() const
Definition: Entry.cc:787
virtual ~ThePEGInterface()
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
const unsigned int skipEvents_
const std::string dataLocation_
Square< F >::type sqr(const F &f)
Definition: Square.h:13
const_iterator end() const
void setPEGRandomEngine(CLHEP::HepRandomEngine *)
list entry
Definition: mps_splice.py:62
const_iterator begin() const
char typeCode() const
Definition: Entry.h:175
void setRandomEngine(CLHEP::HepRandomEngine *v)
std::string fullPath() const
Definition: FileInPath.cc:184
boost::shared_ptr< ThePEG::RandomEngineGlue::Proxy > randomEngineGlueProxy_
static void clearAuxiliary(HepMC::GenEvent *hepmc, HepMC::PdfInfo *pdf)
std::string getString() const
Definition: Entry.cc:764
static void fillAuxiliary(HepMC::GenEvent *hepmc, HepMC::PdfInfo *pdf, const ThePEG::EventPtr &event)