Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:50

0001 #include <algorithm>
0002 #include <functional>
0003 #include <iostream>
0004 #include <string>
0005 #include <memory>
0006 
0007 #include "FWCore/Framework/interface/InputSourceMacros.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/EventPrincipal.h"
0010 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0011 #include "FWCore/Framework/interface/RunPrincipal.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/Utilities/interface/TypeID.h"
0015 
0016 #include "DataFormats/Common/interface/OrphanHandle.h"
0017 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0018 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0019 #include "DataFormats/Provenance/interface/Timestamp.h"
0020 
0021 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
0022 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
0023 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
0024 
0025 #include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
0026 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
0027 #include "GeneratorInterface/LHEInterface/interface/LHEReader.h"
0028 
0029 #include "LHESource.h"
0030 
0031 using namespace lhef;
0032 
0033 LHESource::LHESource(const edm::ParameterSet& params, const edm::InputSourceDescription& desc)
0034     : ProducerSourceFromFiles(params, desc, false),
0035       reader_(new LHEReader(fileNames(0), params.getUntrackedParameter<unsigned int>("skipEvents", 0))),
0036       lheProvenanceHelper_(edm::TypeID(typeid(LHEEventProduct)),
0037                            edm::TypeID(typeid(LHERunInfoProduct)),
0038                            productRegistryUpdate(),
0039                            *branchIDListHelper()),
0040       phid_() {
0041   nextEvent();
0042   lheProvenanceHelper_.lheAugment(nullptr);
0043   // Initialize metadata, and save the process history ID for use every event.
0044   phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
0045 
0046   // These calls are not wanted, because the principals are used for putting the products.
0047   //produces<LHEEventProduct>();
0048   //produces<LHERunInfoProduct, edm::InRun>();
0049 }
0050 
0051 LHESource::~LHESource() {}
0052 
0053 void LHESource::endJob() { reader_.reset(); }
0054 
0055 void LHESource::nextEvent() {
0056   if (partonLevel_) {
0057     return;
0058   }
0059 
0060   bool newFileOpened;
0061   do {
0062     newFileOpened = false;
0063     partonLevel_ = reader_->next(&newFileOpened);
0064     if (newFileOpened) {
0065       incrementFileIndex();
0066     }
0067   } while (newFileOpened && !partonLevel_);
0068 
0069   if (!partonLevel_) {
0070     return;
0071   }
0072 
0073   auto runInfoThis = partonLevel_->getRunInfo();
0074   if (runInfoThis != runInfoLast_) {
0075     runInfoLast_ = runInfoThis;
0076     std::unique_ptr<LHERunInfoProduct> product = std::make_unique<LHERunInfoProduct>(*runInfoThis->getHEPRUP());
0077     fillRunInfoProduct(*runInfoThis, *product);
0078 
0079     if (runInfoProductLast_) {
0080       if (!runInfoProductLast_->mergeProduct(*product)) {
0081         //cannot be merged so must start new Run
0082         runInfoProductLast_ = std::move(product);
0083         lheProvenanceHelper_.lheAugment(runInfoThis.get());
0084         // Initialize metadata, and save the process history ID for use every event.
0085         phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
0086         resetRunAuxiliary();
0087       }
0088     } else {
0089       runInfoProductLast_ = std::move(product);
0090     }
0091   }
0092 }
0093 
0094 void LHESource::fillRunInfoProduct(lhef::LHERunInfo const& iInfo, LHERunInfoProduct& oProduct) {
0095   for (auto const& h : iInfo.getHeaders()) {
0096     oProduct.addHeader(h);
0097   }
0098   for (auto const& c : iInfo.getComments()) {
0099     oProduct.addComment(c);
0100   }
0101 }
0102 
0103 void LHESource::readRun_(edm::RunPrincipal& runPrincipal) {
0104   runAuxiliary()->setProcessHistoryID(phid_);
0105   runPrincipal.fillRunPrincipal(processHistoryRegistryForUpdate());
0106 
0107   putRunInfoProduct(runPrincipal);
0108 }
0109 
0110 void LHESource::readLuminosityBlock_(edm::LuminosityBlockPrincipal& lumiPrincipal) {
0111   luminosityBlockAuxiliary()->setProcessHistoryID(phid_);
0112   lumiPrincipal.fillLuminosityBlockPrincipal(
0113       processHistoryRegistry().getMapped(lumiPrincipal.aux().processHistoryID()));
0114 }
0115 
0116 void LHESource::putRunInfoProduct(edm::RunPrincipal& iRunPrincipal) {
0117   if (runInfoProductLast_) {
0118     auto product = std::make_unique<LHERunInfoProduct>(*runInfoProductLast_);
0119     std::unique_ptr<edm::WrapperBase> rdp = std::make_unique<edm::Wrapper<LHERunInfoProduct>>(std::move(product));
0120     iRunPrincipal.putOrMerge(lheProvenanceHelper_.runProductBranchDescription_, std::move(rdp));
0121   }
0122 }
0123 
0124 bool LHESource::setRunAndEventInfo(edm::EventID&, edm::TimeValue_t&, edm::EventAuxiliary::ExperimentType&) {
0125   nextEvent();
0126   if (!partonLevel_) {
0127     // We just finished an input file. See if there is another.
0128     nextEvent();
0129     if (!partonLevel_) {
0130       // No more input files.
0131       return false;
0132     }
0133   }
0134   return true;
0135 }
0136 
0137 void LHESource::readEvent_(edm::EventPrincipal& eventPrincipal) {
0138   assert(eventCached() || processingMode() != RunsLumisAndEvents);
0139   edm::EventAuxiliary aux(eventID(), processGUID(), edm::Timestamp(presentTime()), false);
0140   aux.setProcessHistoryID(phid_);
0141   eventPrincipal.fillEventPrincipal(aux,
0142                                     processHistoryRegistry().getMapped(aux.processHistoryID()),
0143                                     edm::EventSelectionIDVector(),
0144                                     lheProvenanceHelper_.branchListIndexes_);
0145 
0146   std::unique_ptr<LHEEventProduct> product =
0147       std::make_unique<LHEEventProduct>(*partonLevel_->getHEPEUP(), partonLevel_->originalXWGTUP());
0148   if (partonLevel_->getPDF()) {
0149     product->setPDF(*partonLevel_->getPDF());
0150   }
0151   std::for_each(partonLevel_->weights().begin(),
0152                 partonLevel_->weights().end(),
0153                 std::bind(&LHEEventProduct::addWeight, product.get(), std::placeholders::_1));
0154   product->setScales(partonLevel_->scales());
0155   product->setNpLO(partonLevel_->npLO());
0156   product->setNpNLO(partonLevel_->npNLO());
0157   std::for_each(partonLevel_->getComments().begin(),
0158                 partonLevel_->getComments().end(),
0159                 std::bind(&LHEEventProduct::addComment, product.get(), std::placeholders::_1));
0160 
0161   std::unique_ptr<edm::WrapperBase> edp = std::make_unique<edm::Wrapper<LHEEventProduct>>(std::move(product));
0162   eventPrincipal.put(lheProvenanceHelper_.eventProductBranchDescription_,
0163                      std::move(edp),
0164                      lheProvenanceHelper_.eventProductProvenance_);
0165 
0166   partonLevel_.reset();
0167 
0168   resetEventCached();
0169 }
0170 
0171 std::shared_ptr<edm::RunAuxiliary> LHESource::readRunAuxiliary_() {
0172   edm::Timestamp ts = edm::Timestamp(presentTime());
0173   resetNewRun();
0174   auto aux = std::make_shared<edm::RunAuxiliary>(eventID().run(), ts, edm::Timestamp::invalidTimestamp());
0175   aux->setProcessHistoryID(phid_);
0176   return aux;
0177 }
0178 
0179 std::shared_ptr<edm::LuminosityBlockAuxiliary> LHESource::readLuminosityBlockAuxiliary_() {
0180   if (processingMode() == Runs)
0181     return std::shared_ptr<edm::LuminosityBlockAuxiliary>();
0182   edm::Timestamp ts = edm::Timestamp(presentTime());
0183   resetNewLumi();
0184   auto aux = std::make_shared<edm::LuminosityBlockAuxiliary>(
0185       eventID().run(), eventID().luminosityBlock(), ts, edm::Timestamp::invalidTimestamp());
0186   aux->setProcessHistoryID(phid_);
0187   return aux;
0188 }
0189 
0190 void LHESource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0191   edm::ParameterSetDescription desc;
0192   desc.setComment("A source which reads LHE files.");
0193   edm::ProducerSourceFromFiles::fillDescription(desc);
0194   desc.addUntracked<unsigned int>("skipEvents", 0U)->setComment("Skip the first 'skipEvents' events.");
0195   descriptions.add("source", desc);
0196 }
0197 
0198 DEFINE_FWK_INPUT_SOURCE(LHESource);