00001 #include <memory> 00002 00003 #include <HepMC/GenEvent.h> 00004 #include <HepMC/IO_BaseClass.h> 00005 00006 #include <ThePEG/Repository/Repository.h> 00007 #include <ThePEG/EventRecord/Event.h> 00008 #include <ThePEG/Config/ThePEG.h> 00009 00010 #include "FWCore/Framework/interface/Event.h" 00011 #include "FWCore/Framework/interface/Run.h" 00012 #include "FWCore/Framework/interface/GeneratedInputSource.h" 00013 #include "FWCore/Framework/interface/InputSourceMacros.h" 00014 #include "FWCore/Framework/interface/MakerMacros.h" 00015 #include "FWCore/Utilities/interface/RandomNumberGenerator.h" 00016 #include "FWCore/MessageLogger/interface/MessageLogger.h" 00017 #include "FWCore/ServiceRegistry/interface/Service.h" 00018 #include "FWCore/ParameterSet/interface/ParameterSet.h" 00019 00020 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h" 00021 #include "SimDataFormats/HepMCProduct/interface/GenInfoProduct.h" 00022 00023 #include "GeneratorInterface/ThePEGInterface/interface/ThePEGInterface.h" 00024 00025 class ThePEGSource : public edm::GeneratedInputSource, public ThePEGInterface { 00026 public: 00027 ThePEGSource(const edm::ParameterSet ¶ms, 00028 const edm::InputSourceDescription &desc); 00029 virtual ~ThePEGSource(); 00030 00031 private: 00032 virtual void beginRun(edm::Run &run); 00033 virtual void endRun(edm::Run &run); 00034 virtual bool produce(edm::Event &event); 00035 00036 unsigned int eventsToPrint; 00037 00038 const double extCrossSect; 00039 const double extFilterEff; 00040 }; 00041 00042 ThePEGSource::ThePEGSource(const edm::ParameterSet &pset, 00043 edm::InputSourceDescription const &desc) : 00044 edm::GeneratedInputSource(pset, desc), 00045 ThePEGInterface(pset), 00046 eventsToPrint(pset.getUntrackedParameter<unsigned int>("eventsToPrint", 0)), 00047 extCrossSect(pset.getUntrackedParameter<double>("crossSection", -1.0)), 00048 extFilterEff(pset.getUntrackedParameter<double>("filterEfficiency", -1.0)) 00049 { 00050 initRepository(pset); 00051 00052 produces<edm::HepMCProduct>(); 00053 produces<edm::GenInfoProduct, edm::InRun>(); 00054 } 00055 00056 ThePEGSource::~ThePEGSource() 00057 { 00058 } 00059 00060 void ThePEGSource::beginRun(edm::Run &run) 00061 { 00062 initGenerator(); 00063 00064 edm::Service<edm::RandomNumberGenerator> rng; 00065 eg_->setSeed(rng->mySeed()); 00066 } 00067 00068 void ThePEGSource::endRun(edm::Run &run) 00069 { 00070 std::auto_ptr<edm::GenInfoProduct> genInfoProd(new edm::GenInfoProduct); 00071 genInfoProd->set_cross_section(eg_->integratedXSec() / ThePEG::picobarn); 00072 genInfoProd->set_external_cross_section(extCrossSect); 00073 genInfoProd->set_filter_efficiency(extFilterEff); 00074 run.put(genInfoProd); 00075 } 00076 00077 bool ThePEGSource::produce(edm::Event &event) 00078 { 00079 edm::LogInfo("Generator|ThePEGSource") << "Start production"; 00080 00081 ThePEG::EventPtr thepegEvent = eg_->shoot(); 00082 if (!thepegEvent) { 00083 edm::LogWarning("Generator|ThePEGSource") << "thepegEvent not initialized"; 00084 return false; 00085 } 00086 00087 std::auto_ptr<HepMC::GenEvent> hepmcEvent = convert(thepegEvent); 00088 if (!hepmcEvent.get()) { 00089 edm::LogWarning("Generator|ThePEGSource") << "hepmcEvent not initialized"; 00090 return false; 00091 } 00092 00093 HepMC::PdfInfo pdf; 00094 clearAuxiliary(hepmcEvent.get(), &pdf); 00095 hepmcEvent->set_event_number(numberEventsInRun() - 00096 remainingEvents() - 1); 00097 fillAuxiliary(hepmcEvent.get(), &pdf, thepegEvent); 00098 hepmcEvent->set_pdf_info(pdf); 00099 00100 if (eventsToPrint) { 00101 eventsToPrint--; 00102 hepmcEvent->print(); 00103 } 00104 00105 if (iobc_.get()) 00106 iobc_->write_event(hepmcEvent.get()); 00107 00108 std::auto_ptr<edm::HepMCProduct> result(new edm::HepMCProduct()); 00109 result->addHepMCData(hepmcEvent.release()); 00110 event.put(result); 00111 edm::LogInfo("Generator|ThePEGSource") << "Event produced"; 00112 00113 return true; 00114 } 00115 00116 DEFINE_ANOTHER_FWK_INPUT_SOURCE(ThePEGSource);