CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/GeneratorInterface/LHEInterface/plugins/LHESource.cc

Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <functional>
00003 #include <iostream>
00004 #include <string>
00005 #include <memory>
00006 
00007 #include <boost/bind.hpp>
00008 #include <boost/ptr_container/ptr_deque.hpp>
00009 
00010 #include "FWCore/Framework/interface/InputSourceMacros.h"
00011 #include "FWCore/Framework/interface/MakerMacros.h"
00012 #include "FWCore/Framework/interface/EventPrincipal.h"
00013 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
00014 #include "FWCore/Framework/interface/RunPrincipal.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 #include "FWCore/Utilities/interface/TypeID.h"
00018 
00019 #include "DataFormats/Common/interface/OrphanHandle.h"
00020 
00021 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
00022 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00023 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00024 
00025 #include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
00026 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00027 #include "GeneratorInterface/LHEInterface/interface/LHEReader.h"
00028 
00029 #include "LHESource.h"
00030 
00031 using namespace lhef;
00032 
00033 LHESource::LHESource(const edm::ParameterSet &params,
00034                      const edm::InputSourceDescription &desc) :
00035         ProducerSourceFromFiles(params, desc, false),
00036         reader(new LHEReader(fileNames(), params.getUntrackedParameter<unsigned int>("skipEvents", 0))),
00037         wasMerged(false),
00038         lheProvenanceHelper_(edm::TypeID(typeid(LHEEventProduct)), edm::TypeID(typeid(LHERunInfoProduct))),
00039         phid_(),
00040         runPrincipal_()
00041 {
00042         nextEvent();
00043         lheProvenanceHelper_.lheAugment(runInfo.get());
00044         // Initialize metadata, and save the process history ID for use every event.
00045         phid_ = lheProvenanceHelper_.lheInit(productRegistryUpdate());
00046 
00047         // These calls are not wanted, because the principals are used for putting the products.
00048         //produces<LHEEventProduct>();
00049         //produces<LHERunInfoProduct, edm::InRun>();
00050 }
00051 
00052 LHESource::~LHESource()
00053 {
00054 }
00055 
00056 void LHESource::endJob()
00057 {
00058         reader.reset();
00059 }
00060 
00061 void LHESource::nextEvent()
00062 {
00063         if (partonLevel) {
00064                 return;
00065         }
00066 
00067         bool newFileOpened = false;
00068         partonLevel = reader->next(&newFileOpened);
00069         if(newFileOpened) incrementFileIndex();
00070         if (!partonLevel) {
00071                 return;
00072         }
00073 
00074         boost::shared_ptr<LHERunInfo> runInfoThis = partonLevel->getRunInfo();
00075         if (runInfoThis != runInfoLast) {
00076                 runInfo = runInfoThis;
00077                 runInfoLast = runInfoThis;
00078         }
00079 }
00080 
00081 // This is the only way we can now access the run principal.
00082 boost::shared_ptr<edm::RunPrincipal>
00083 LHESource::readRun_(boost::shared_ptr<edm::RunPrincipal> runPrincipal) {
00084   runAuxiliary()->setProcessHistoryID(phid_);
00085   runPrincipal->fillRunPrincipal();
00086   runPrincipal_ = runPrincipal;
00087   return runPrincipal;
00088 }
00089 
00090 boost::shared_ptr<edm::LuminosityBlockPrincipal>
00091 LHESource::readLuminosityBlock_(boost::shared_ptr<edm::LuminosityBlockPrincipal> lumiPrincipal) {
00092   luminosityBlockAuxiliary()->setProcessHistoryID(phid_);
00093   lumiPrincipal->fillLuminosityBlockPrincipal();
00094   return lumiPrincipal;
00095 }
00096 
00097 void LHESource::beginRun(edm::Run&)
00098 {
00099         if (runInfoLast) {
00100                 runInfo = runInfoLast;
00101 
00102                 std::auto_ptr<LHERunInfoProduct> product(
00103                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00104                 std::for_each(runInfo->getHeaders().begin(),
00105                               runInfo->getHeaders().end(),
00106                               boost::bind(
00107                                 &LHERunInfoProduct::addHeader,
00108                                 product.get(), _1));
00109                 std::for_each(runInfo->getComments().begin(),
00110                               runInfo->getComments().end(),
00111                               boost::bind(&LHERunInfoProduct::addComment,
00112                                 product.get(), _1));
00113 
00114                 // keep a copy around in case of merging
00115                 runInfoProducts.push_back(new LHERunInfoProduct(*product));
00116                 wasMerged = false;
00117 
00118                 edm::WrapperOwningHolder rdp(new edm::Wrapper<LHERunInfoProduct>(product), edm::Wrapper<LHERunInfoProduct>::getInterface());
00119                 runPrincipal_->put(lheProvenanceHelper_.runProductBranchDescription_, rdp);
00120 
00121                 runInfo.reset();
00122         }
00123 }
00124 
00125 void LHESource::endRun(edm::Run&)
00126 {
00127         if (!runInfoProducts.empty()) {
00128                 std::auto_ptr<LHERunInfoProduct> product(
00129                                         runInfoProducts.pop_front().release());
00130                 edm::WrapperOwningHolder rdp(new edm::Wrapper<LHERunInfoProduct>(product), edm::Wrapper<LHERunInfoProduct>::getInterface());
00131                 runPrincipal_->put(lheProvenanceHelper_.runProductBranchDescription_, rdp);
00132         }
00133         runPrincipal_.reset();
00134 }
00135 
00136 bool LHESource::setRunAndEventInfo(edm::EventID&, edm::TimeValue_t&)
00137 {
00138         nextEvent();
00139         if (!partonLevel) {
00140                 return false;
00141         }
00142         return true;
00143 }
00144 
00145 edm::EventPrincipal*
00146 LHESource::readEvent_(edm::EventPrincipal& eventPrincipal) {
00147         assert(eventCached() || processingMode() != RunsLumisAndEvents);
00148         EventSourceSentry sentry(*this);
00149         edm::EventAuxiliary aux(eventID(), processGUID(), edm::Timestamp(presentTime()), false);
00150         aux.setProcessHistoryID(phid_);
00151         eventPrincipal.fillEventPrincipal(aux);
00152 
00153         std::auto_ptr<LHEEventProduct> product(
00154                         new LHEEventProduct(*partonLevel->getHEPEUP()));
00155         if (partonLevel->getPDF()) {
00156                 product->setPDF(*partonLevel->getPDF());
00157         }
00158         std::for_each(partonLevel->getComments().begin(),
00159                       partonLevel->getComments().end(),
00160                       boost::bind(&LHEEventProduct::addComment,
00161                                   product.get(), _1));
00162 
00163         edm::WrapperOwningHolder edp(new edm::Wrapper<LHEEventProduct>(product), edm::Wrapper<LHEEventProduct>::getInterface());
00164         eventPrincipal.put(lheProvenanceHelper_.eventProductBranchDescription_, edp, lheProvenanceHelper_.eventProductProvenance_);
00165 
00166         if (runInfo) {
00167                 std::auto_ptr<LHERunInfoProduct> product(
00168                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00169                 std::for_each(runInfo->getHeaders().begin(),
00170                               runInfo->getHeaders().end(),
00171                               boost::bind(
00172                                 &LHERunInfoProduct::addHeader,
00173                                 product.get(), _1));
00174                 std::for_each(runInfo->getComments().begin(),
00175                               runInfo->getComments().end(),
00176                               boost::bind(&LHERunInfoProduct::addComment,
00177                                 product.get(), _1));
00178 
00179                 if (!runInfoProducts.empty()) {
00180                         runInfoProducts.front().mergeProduct(*product);
00181                         if (!wasMerged) {
00182                                 runInfoProducts.pop_front();
00183                                 runInfoProducts.push_front(product);
00184                                 wasMerged = true;
00185                         }
00186                 }
00187 
00188                 runInfo.reset();
00189         }
00190 
00191         partonLevel.reset();
00192 
00193         resetEventCached();
00194         return &eventPrincipal;
00195 }
00196 
00197 DEFINE_FWK_INPUT_SOURCE(LHESource);