CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/GeneratorInterface/ThePEGInterface/src/ThePEGInterface.cc

Go to the documentation of this file.
00001 
00008 #include <iostream>
00009 #include <sstream>
00010 #include <cstdlib>
00011 #include <memory>
00012 #include <cmath>
00013 #include <stdlib.h>
00014 
00015 #include <boost/shared_ptr.hpp>
00016 #include <boost/filesystem.hpp>
00017 
00018 #include <HepMC/GenEvent.h>
00019 #include <HepMC/PdfInfo.h>
00020 #include <HepMC/IO_GenEvent.h>
00021 
00022 #include <ThePEG/Utilities/DynamicLoader.h>
00023 #include <ThePEG/Repository/Repository.h>
00024 #include <ThePEG/Handlers/EventHandler.h>
00025 #include <ThePEG/Handlers/XComb.h>
00026 #include <ThePEG/EventRecord/Event.h>
00027 #include <ThePEG/EventRecord/Particle.h> 
00028 #include <ThePEG/EventRecord/Collision.h>
00029 #include <ThePEG/EventRecord/TmpTransform.h>
00030 #include <ThePEG/Config/ThePEG.h>
00031 #include <ThePEG/PDF/PartonExtractor.h>
00032 #include <ThePEG/PDF/PDFBase.h>
00033 #include <ThePEG/Utilities/UtilityBase.h>
00034 
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 
00037 #include "GeneratorInterface/Core/interface/ParameterCollector.h"
00038 
00039 #include "GeneratorInterface/ThePEGInterface/interface/Proxy.h"
00040 #include "GeneratorInterface/ThePEGInterface/interface/RandomEngineGlue.h"
00041 #include "GeneratorInterface/ThePEGInterface/interface/HepMCConverter.h"
00042 #include "GeneratorInterface/ThePEGInterface/interface/ThePEGInterface.h"
00043 
00044 using namespace std;
00045 using namespace gen;
00046 
00047 ThePEGInterface::ThePEGInterface(const edm::ParameterSet &pset) :
00048         randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
00049         dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
00050         generator_(pset.getParameter<string>("generatorModule")),
00051         run_(pset.getParameter<string>("run")),
00052         dumpConfig_(pset.getUntrackedParameter<string>("dumpConfig", "")),
00053         skipEvents_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0))
00054 {
00055         // Write events in hepmc ascii format for debugging purposes
00056         string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
00057         if (!dumpEvents.empty()) {
00058                 iobc_.reset(new HepMC::IO_GenEvent(dumpEvents.c_str(), ios::out));
00059                 edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
00060         }
00061         // Clear dumpConfig target
00062         if (!dumpConfig_.empty())
00063                 ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
00064 }
00065 
00066 ThePEGInterface::~ThePEGInterface()
00067 {
00068         if (eg_)
00069                 eg_->finalize();
00070         edm::LogInfo("ThePEGInterface") << "Event generator finalized";
00071 }
00072 
00073 string ThePEGInterface::dataFile(const string &fileName) const
00074 {
00075         if (fileName.empty() || fileName[0] == '/')
00076                 return fileName;
00077         else
00078                 return dataLocation_ + "/" + fileName;
00079 }
00080 
00081 string ThePEGInterface::dataFile(const edm::ParameterSet &pset,
00082                                  const string &paramName) const
00083 {
00084         const edm::Entry &entry = pset.retrieve(paramName);
00085         if (entry.typeCode() == 'F')
00086                 return entry.getFileInPath().fullPath();
00087         else
00088                 return dataFile(entry.getString());
00089 }
00090 
00091 void ThePEGInterface::initRepository(const edm::ParameterSet &pset) const
00092 {
00093         /* Initialize the repository from
00094          * 1. the repository file (default values)
00095          * 2. the ThePEG config files
00096          * 3. the CMSSW config blocks
00097          */
00098         stringstream logstream;
00099 
00100         // Read the repository of serialized default values
00101         string repository = dataFile(pset, "repository");
00102         if (!repository.empty()) {
00103                 edm::LogInfo("ThePEGInterface") << "Loading repository (" << repository << ")";
00104                 ThePEG::Repository::load(repository);
00105         }
00106 
00107         if (!getenv("ThePEG_INSTALL_PATH")) {
00108                 vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
00109                 for(vector<string>::const_iterator libdir = libdirlist.begin();
00110                     libdir < libdirlist.end(); ++libdir) {
00111                         if (libdir->empty() || (*libdir)[0] != '/')
00112                                 continue;
00113                         if (boost::filesystem::exists(*libdir +
00114                                         "/../../share/ThePEG/PDFsets.index")) {
00115                                 setenv("ThePEG_INSTALL_PATH",
00116                                        libdir->c_str(), 0);
00117                                 break;
00118                         }
00119                 }
00120         }
00121 
00122         // Read ThePEG config files to read
00123         vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
00124 
00125         // Loop over the config files
00126         for(vector<string>::const_iterator iter = configFiles.begin();
00127             iter != configFiles.end(); ++iter) {
00128                 edm::LogInfo("ThePEGInterface") << "Reading config file (" << *iter << ")";
00129                 ThePEG::Repository::read(dataFile(*iter), logstream);
00130                 edm::LogInfo("ThePEGSource") << logstream.str();
00131         }
00132 
00133         // Read CMSSW config file parameter sets starting from "parameterSets"
00134 
00135         ofstream cfgDump;
00136         ParameterCollector collector(pset);
00137         ParameterCollector::const_iterator iter;
00138         if (!dumpConfig_.empty()) {
00139                 cfgDump.open(dumpConfig_.c_str(), ios_base::app);
00140                 iter = collector.begin(cfgDump);
00141         } else
00142                 iter = collector.begin();
00143 
00144         for(; iter != collector.end(); ++iter) {
00145                 string out = ThePEG::Repository::exec(*iter, logstream);
00146                 if (!out.empty()) {
00147                         edm::LogInfo("ThePEGInterface") << *iter << " => " << out;
00148                         cerr << "Error in ThePEG configuration!\n"
00149                                 "\tLine: " << *iter << "\n" << out << endl;
00150                 }
00151         }
00152 
00153         if (!dumpConfig_.empty()) {
00154                 cfgDump << "saverun " << run_ << " " << generator_ << endl;
00155                 cfgDump.close();
00156         }
00157 
00158         // write the ProxyID for the RandomEngineGlue to fill its pointer in
00159         ostringstream ss;
00160         ss << randomEngineGlueProxy_->getID();
00161         ThePEG::Repository::exec("set " + generator_ +
00162                                  ":RandomNumberGenerator:ProxyID " + ss.str(),
00163                                  logstream);
00164 
00165         // Print the directories where ThePEG looks for libs
00166         vector<string> libdirlist = ThePEG::DynamicLoader::allPaths();
00167         for(vector<string>::const_iterator libdir = libdirlist.begin();
00168             libdir < libdirlist.end(); ++libdir)
00169                 edm::LogInfo("ThePEGInterface") << "DynamicLoader path = " << *libdir << endl;
00170 
00171         // Output status information about the repository
00172         ThePEG::Repository::stats(logstream);
00173         edm::LogInfo("ThePEGInterface") << logstream.str();
00174 }
00175 
00176 void ThePEGInterface::initGenerator()
00177 {
00178         // Get generator from the repository and initialize it
00179         ThePEG::BaseRepository::CheckObjectDirectory(generator_);
00180         ThePEG::EGPtr tmp = ThePEG::BaseRepository::GetObject<ThePEG::EGPtr>(generator_);
00181         if (tmp) {
00182                 eg_ = ThePEG::Repository::makeRun(tmp, run_);
00183                 eg_->initialize();
00184                 edm::LogInfo("ThePEGInterface") << "EventGenerator initialized";
00185         } else
00186                 throw cms::Exception("ThePEGInterface")
00187                         << "EventGenerator could not be initialized!" << endl;
00188 
00189         // Skip events
00190         for (unsigned int i = 0; i < skipEvents_; i++) {
00191                 flushRandomNumberGenerator();
00192                 eg_->shoot();
00193                 edm::LogInfo("ThePEGInterface") << "Event discarded";
00194         }
00195 }
00196 
00197 void ThePEGInterface::flushRandomNumberGenerator()
00198 {
00199         ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
00200 
00201         if (!rnd)
00202                 edm::LogWarning("ProxyMissing")
00203                         << "ThePEG not initialised with RandomEngineGlue.";
00204         else
00205                 rnd->flush();
00206 }
00207 
00208 auto_ptr<HepMC::GenEvent> ThePEGInterface::convert(
00209                                         const ThePEG::EventPtr &event)
00210 {
00211         return std::auto_ptr<HepMC::GenEvent>(
00212                 ThePEG::HepMCConverter<HepMC::GenEvent>::convert(*event));
00213 }
00214 
00215 void ThePEGInterface::clearAuxiliary(HepMC::GenEvent *hepmc,
00216                                      HepMC::PdfInfo *pdf)
00217 {
00218         if (hepmc) {
00219                 hepmc->set_event_scale(-1.0);
00220                 hepmc->set_alphaQCD(-1.0);
00221                 hepmc->set_alphaQED(-1.0);
00222         }
00223         if (pdf) {
00224                 pdf->set_id1(-100);
00225                 pdf->set_id2(-100);
00226                 pdf->set_x1(-1.0);
00227                 pdf->set_x2(-1.0);
00228                 pdf->set_scalePDF(-1.0);
00229                 pdf->set_pdf1(-1.0);
00230                 pdf->set_pdf2(-1.0);
00231         }
00232 }
00233 
00234 void ThePEGInterface::fillAuxiliary(HepMC::GenEvent *hepmc,
00235                                     HepMC::PdfInfo *pdf,
00236                                     const ThePEG::EventPtr &event)
00237 {
00238         if (!event->primaryCollision())
00239                 return;
00240 
00241         ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
00242                                 event->primaryCollision()->handler());
00243         double scale = eh->lastScale();
00244 
00245         if (hepmc) {
00246                 if (hepmc->event_scale() < 0 && scale > 0)
00247                         hepmc->set_event_scale(std::sqrt(scale) / ThePEG::GeV);
00248 
00249                 if (hepmc->alphaQCD() < 0)
00250                         hepmc->set_alphaQCD(eh->lastAlphaS());
00251                 if (hepmc->alphaQED() < 0)
00252                         hepmc->set_alphaQED(eh->lastAlphaEM());
00253         }
00254 
00255         if (pdf) {
00256                 const ThePEG::PPair &beams = eh->lastParticles();
00257                 const ThePEG::PPair &partons = eh->lastPartons();
00258                 ThePEG::tcPDFPtr pdf1 = eh->lastExtractor()->getPDF(
00259                                                 beams.first->dataPtr());
00260                 ThePEG::tcPDFPtr pdf2 = eh->lastExtractor()->getPDF(
00261                                                 beams.second->dataPtr());
00262                 double x1, x2;
00263 
00264                 if (pdf->id1() == -100) {
00265                         int id = partons.first->id();
00266                         pdf->set_id1(id == 21 ? 0 : id);
00267                 }
00268                 if (pdf->id2() == -100) {
00269                         int id = partons.second->id();
00270                         pdf->set_id2(id == 21 ? 0 : id);
00271                 }
00272 
00273                 if (pdf->scalePDF() < 0)
00274                         pdf->set_scalePDF(std::sqrt(scale) / ThePEG::GeV);
00275                 else
00276                         scale = ThePEG::sqr(pdf->scalePDF()) * ThePEG::GeV;
00277 
00278                 if (pdf->x1() < 0) {
00279                         x1 = eh->lastX1();
00280                         pdf->set_x1(x1);
00281                 } else
00282                         x1 = pdf->x1();
00283 
00284                 if (pdf->x2() < 0) {
00285                         x2 = eh->lastX2();
00286                         pdf->set_x2(x2);
00287                 } else
00288                         x2 = pdf->x2();
00289 
00290                 if (pdf1 && pdf->pdf1() < 0) {
00291                         double v = pdf1->xfx(beams.first->dataPtr(),
00292                                              partons.first->dataPtr(),
00293                                              scale, x1);
00294                         if (v > 0 && x1 > 0)
00295                                 pdf->set_pdf1(v / x1);
00296                         else
00297                                 pdf->set_pdf2(-1.0);
00298                 }
00299                 if (pdf2 && pdf->pdf2() < 0) {
00300                         double v = pdf2->xfx(beams.first->dataPtr(),
00301                                              partons.first->dataPtr(),
00302                                              scale, x2);
00303                         if (v > 0 && x2 > 0)
00304                                 pdf->set_pdf2(v / x2);
00305                         else
00306                                 pdf->set_pdf2(-1.0);
00307                 }
00308         }
00309 
00310 }
00311 
00312 double ThePEGInterface::pthat(const ThePEG::EventPtr &event)
00313 {
00314         using namespace ThePEG;
00315 
00316         if (!event->primaryCollision())
00317                 return -1.0;
00318 
00319         tSubProPtr sub = event->primaryCollision()->primarySubProcess();
00320         TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(
00321                                                         sub->incoming()));
00322 
00323         double pthat = (*sub->outgoing().begin())->momentum().perp();
00324         for(PVector::const_iterator it = sub->outgoing().begin();
00325             it != sub->outgoing().end(); ++it)
00326                 pthat = std::min(pthat, (*it)->momentum().perp());
00327 
00328         return pthat / ThePEG::GeV;
00329 }