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 
35 
37 
42 
43 using namespace std;
44 using namespace gen;
45 
47  randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
48  dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
49  generator_(pset.getParameter<string>("generatorModule")),
50  run_(pset.getParameter<string>("run")),
51  dumpConfig_(pset.getUntrackedParameter<string>("dumpConfig", "")),
52  skipEvents_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0))
53 {
54  // Write events in hepmc ascii format for debugging purposes
55  string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
56  if (!dumpEvents.empty()) {
57  iobc_.reset(new HepMC::IO_GenEvent(dumpEvents.c_str(), ios::out));
58  edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
59  }
60  // Clear dumpConfig target
61  if (!dumpConfig_.empty())
62  ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
63 }
64 
66 {
67  if (eg_)
68  eg_->finalize();
69  edm::LogInfo("ThePEGInterface") << "Event generator finalized";
70 }
71 
72 void ThePEGInterface::setPEGRandomEngine(CLHEP::HepRandomEngine* v) {
73  randomEngineGlueProxy_->setRandomEngine(v);
75  if(rnd) {
76  rnd->setRandomEngine(v);
77  }
78 }
79 
80 string ThePEGInterface::dataFile(const string &fileName) const
81 {
82  if (fileName.empty() || fileName[0] == '/')
83  return fileName;
84  else
85  return dataLocation_ + "/" + fileName;
86 }
87 
89  const string &paramName) const
90 {
91  const edm::Entry &entry = pset.retrieve(paramName);
92  if (entry.typeCode() == 'F')
93  return entry.getFileInPath().fullPath();
94  else
95  return dataFile(entry.getString());
96 }
97 
99 {
100  /* Initialize the repository from
101  * 1. the repository file (default values)
102  * 2. the ThePEG config files
103  * 3. the CMSSW config blocks
104  */
105  stringstream logstream;
106 
107  // Read the repository of serialized default values
108  string repository = dataFile(pset, "repository");
109  if (!repository.empty()) {
110  edm::LogInfo("ThePEGInterface") << "Loading repository (" << repository << ")";
111  ThePEG::Repository::load(repository);
112  }
113 
114  if (!getenv("ThePEG_INSTALL_PATH")) {
115  vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
116  for(vector<string>::const_iterator libdir = libdirlist.begin();
117  libdir < libdirlist.end(); ++libdir) {
118  if (libdir->empty() || (*libdir)[0] != '/')
119  continue;
120  if (boost::filesystem::exists(*libdir +
121  "/../../share/ThePEG/PDFsets.index")) {
122  setenv("ThePEG_INSTALL_PATH",
123  libdir->c_str(), 0);
124  break;
125  }
126  }
127  }
128 
129  // Read ThePEG config files to read
130  vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
131 
132  // Loop over the config files
133  for(vector<string>::const_iterator iter = configFiles.begin();
134  iter != configFiles.end(); ++iter) {
135  edm::LogInfo("ThePEGInterface") << "Reading config file (" << *iter << ")";
136  ThePEG::Repository::read(dataFile(*iter), logstream);
137  edm::LogInfo("ThePEGSource") << logstream.str();
138  }
139 
140  // Read CMSSW config file parameter sets starting from "parameterSets"
141 
142  ofstream cfgDump;
143  ParameterCollector collector(pset);
145  if (!dumpConfig_.empty()) {
146  cfgDump.open(dumpConfig_.c_str(), ios_base::app);
147  iter = collector.begin(cfgDump);
148  } else
149  iter = collector.begin();
150 
151  for(; iter != collector.end(); ++iter) {
152  string out = ThePEG::Repository::exec(*iter, logstream);
153  if (!out.empty()) {
154  edm::LogInfo("ThePEGInterface") << *iter << " => " << out;
155  cerr << "Error in ThePEG configuration!\n"
156  "\tLine: " << *iter << "\n" << out << endl;
157  }
158  }
159 
160  if (!dumpConfig_.empty()) {
161  cfgDump << "saverun " << run_ << " " << generator_ << endl;
162  cfgDump.close();
163  }
164 
165  // write the ProxyID for the RandomEngineGlue to fill its pointer in
166  ostringstream ss;
167  ss << randomEngineGlueProxy_->getID();
168  ThePEG::Repository::exec("set " + generator_ +
169  ":RandomNumberGenerator:ProxyID " + ss.str(),
170  logstream);
171 
172  // Print the directories where ThePEG looks for libs
173  vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
174  for(vector<string>::const_iterator libdir = libdirlist.begin();
175  libdir < libdirlist.end(); ++libdir)
176  edm::LogInfo("ThePEGInterface") << "DynamicLoader path = " << *libdir << endl;
177 
178  // Output status information about the repository
179  ThePEG::Repository::stats(logstream);
180  edm::LogInfo("ThePEGInterface") << logstream.str();
181 }
182 
184 {
185  // Get generator from the repository and initialize it
186  ThePEG::BaseRepository::CheckObjectDirectory(generator_);
187  ThePEG::EGPtr tmp = ThePEG::BaseRepository::GetObject<ThePEG::EGPtr>(generator_);
188  if (tmp) {
189  eg_ = ThePEG::Repository::makeRun(tmp, run_);
190  eg_->initialize();
191  edm::LogInfo("ThePEGInterface") << "EventGenerator initialized";
192  } else
193  throw cms::Exception("ThePEGInterface")
194  << "EventGenerator could not be initialized!" << endl;
195 
196  // Skip events
197  for (unsigned int i = 0; i < skipEvents_; i++) {
199  eg_->shoot();
200  edm::LogInfo("ThePEGInterface") << "Event discarded";
201  }
202 }
203 
205 {
206  ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
207 
208  if (!rnd)
209  edm::LogWarning("ProxyMissing")
210  << "ThePEG not initialised with RandomEngineGlue.";
211  else
212  rnd->flush();
213 }
214 
215 auto_ptr<HepMC::GenEvent> ThePEGInterface::convert(
216  const ThePEG::EventPtr &event)
217 {
218  return std::auto_ptr<HepMC::GenEvent>(
220 }
221 
222 void ThePEGInterface::clearAuxiliary(HepMC::GenEvent *hepmc,
223  HepMC::PdfInfo *pdf)
224 {
225  if (hepmc) {
226  hepmc->set_event_scale(-1.0);
227  hepmc->set_alphaQCD(-1.0);
228  hepmc->set_alphaQED(-1.0);
229  }
230  if (pdf) {
231  pdf->set_id1(-100);
232  pdf->set_id2(-100);
233  pdf->set_x1(-1.0);
234  pdf->set_x2(-1.0);
235  pdf->set_scalePDF(-1.0);
236  pdf->set_pdf1(-1.0);
237  pdf->set_pdf2(-1.0);
238  }
239 }
240 
241 void ThePEGInterface::fillAuxiliary(HepMC::GenEvent *hepmc,
242  HepMC::PdfInfo *pdf,
243  const ThePEG::EventPtr &event)
244 {
245  if (!event->primaryCollision())
246  return;
247 
248  ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
249  event->primaryCollision()->handler());
250  double scale = eh->lastScale();
251 
252  if (hepmc) {
253  if (hepmc->event_scale() < 0 && scale > 0)
254  hepmc->set_event_scale(std::sqrt(scale) / ThePEG::GeV);
255 
256  if (hepmc->alphaQCD() < 0)
257  hepmc->set_alphaQCD(eh->lastAlphaS());
258  if (hepmc->alphaQED() < 0)
259  hepmc->set_alphaQED(eh->lastAlphaEM());
260  }
261 
262  if (pdf) {
263  const ThePEG::PPair &beams = eh->lastParticles();
264  const ThePEG::PPair &partons = eh->lastPartons();
265  ThePEG::tcPDFPtr pdf1 = eh->lastExtractor()->getPDF(
266  beams.first->dataPtr());
267  ThePEG::tcPDFPtr pdf2 = eh->lastExtractor()->getPDF(
268  beams.second->dataPtr());
269  double x1, x2;
270 
271  if (pdf->id1() == -100) {
272  int id = partons.first->id();
273  pdf->set_id1(id == 21 ? 0 : id);
274  }
275  if (pdf->id2() == -100) {
276  int id = partons.second->id();
277  pdf->set_id2(id == 21 ? 0 : id);
278  }
279 
280  if (pdf->scalePDF() < 0)
281  pdf->set_scalePDF(std::sqrt(scale) / ThePEG::GeV);
282  else
283  scale = ThePEG::sqr(pdf->scalePDF()) * ThePEG::GeV;
284 
285  if (pdf->x1() < 0) {
286  x1 = eh->lastX1();
287  pdf->set_x1(x1);
288  } else
289  x1 = pdf->x1();
290 
291  if (pdf->x2() < 0) {
292  x2 = eh->lastX2();
293  pdf->set_x2(x2);
294  } else
295  x2 = pdf->x2();
296 
297  if (pdf1 && pdf->pdf1() < 0) {
298  double v = pdf1->xfx(beams.first->dataPtr(),
299  partons.first->dataPtr(),
300  scale, x1);
301  if (v > 0 && x1 > 0)
302  pdf->set_pdf1(v / x1);
303  else
304  pdf->set_pdf2(-1.0);
305  }
306  if (pdf2 && pdf->pdf2() < 0) {
307  double v = pdf2->xfx(beams.first->dataPtr(),
308  partons.first->dataPtr(),
309  scale, x2);
310  if (v > 0 && x2 > 0)
311  pdf->set_pdf2(v / x2);
312  else
313  pdf->set_pdf2(-1.0);
314  }
315  }
316 
317 }
318 
319 double ThePEGInterface::pthat(const ThePEG::EventPtr &event)
320 {
321  using namespace ThePEG;
322 
323  if (!event->primaryCollision())
324  return -1.0;
325 
326  tSubProPtr sub = event->primaryCollision()->primarySubProcess();
327  TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(
328  sub->incoming()));
329 
330  double pthat = (*sub->outgoing().begin())->momentum().perp();
331  for(PVector::const_iterator it = sub->outgoing().begin();
332  it != sub->outgoing().end(); ++it)
333  pthat = std::min(pthat, (*it)->momentum().perp());
334 
335  return pthat / ThePEG::GeV;
336 }
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_
T sqrt(T t)
Definition: SSEVec.h:48
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)
static GenEvent * convert(const Event &ev, bool nocopies=false, Energy eunit=Traits::defaultEnergyUnit(), Length lunit=Traits::defaultLengthUnit())
tuple out
Definition: dbtoconf.py:99
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 *)
const_iterator begin() const
char typeCode() const
Definition: Entry.h:175
void setRandomEngine(CLHEP::HepRandomEngine *v)
std::string fullPath() const
Definition: FileInPath.cc:165
boost::shared_ptr< ThePEG::RandomEngineGlue::Proxy > randomEngineGlueProxy_
SurfaceDeformation * create(int type, const std::vector< double > &params)
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)