File indexing completed on 2021-03-29 04:38:07
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_(
0037 edm::TypeID(typeid(LHEEventProduct)), edm::TypeID(typeid(LHERunInfoProduct)), productRegistryUpdate()),
0038 phid_() {
0039 nextEvent();
0040 lheProvenanceHelper_.lheAugment(nullptr);
0041
0042 phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
0043
0044
0045
0046
0047 }
0048
0049 LHESource::~LHESource() {}
0050
0051 void LHESource::endJob() { reader_.reset(); }
0052
0053 void LHESource::nextEvent() {
0054 if (partonLevel_) {
0055 return;
0056 }
0057
0058 bool newFileOpened;
0059 do {
0060 newFileOpened = false;
0061 partonLevel_ = reader_->next(&newFileOpened);
0062 if (newFileOpened) {
0063 incrementFileIndex();
0064 }
0065 } while (newFileOpened && !partonLevel_);
0066
0067 if (!partonLevel_) {
0068 return;
0069 }
0070
0071 auto runInfoThis = partonLevel_->getRunInfo();
0072 if (runInfoThis != runInfoLast_) {
0073 runInfoLast_ = runInfoThis;
0074 std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfoThis->getHEPRUP()));
0075 fillRunInfoProduct(*runInfoThis, *product);
0076
0077 if (runInfoProductLast_) {
0078 if (!runInfoProductLast_->mergeProduct(*product)) {
0079
0080 runInfoProductLast_ = std::move(product);
0081 lheProvenanceHelper_.lheAugment(runInfoThis.get());
0082
0083 phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
0084 resetRunAuxiliary();
0085 }
0086 } else {
0087 runInfoProductLast_ = std::move(product);
0088 }
0089 }
0090 }
0091
0092 void LHESource::fillRunInfoProduct(lhef::LHERunInfo const& iInfo, LHERunInfoProduct& oProduct) {
0093 for (auto const& h : iInfo.getHeaders()) {
0094 oProduct.addHeader(h);
0095 }
0096 for (auto const& c : iInfo.getComments()) {
0097 oProduct.addComment(c);
0098 }
0099 }
0100
0101 void LHESource::readRun_(edm::RunPrincipal& runPrincipal) {
0102 runAuxiliary()->setProcessHistoryID(phid_);
0103 runPrincipal.fillRunPrincipal(processHistoryRegistryForUpdate());
0104
0105 putRunInfoProduct(runPrincipal);
0106 }
0107
0108 void LHESource::readLuminosityBlock_(edm::LuminosityBlockPrincipal& lumiPrincipal) {
0109 luminosityBlockAuxiliary()->setProcessHistoryID(phid_);
0110 lumiPrincipal.fillLuminosityBlockPrincipal(
0111 processHistoryRegistry().getMapped(lumiPrincipal.aux().processHistoryID()));
0112 }
0113
0114 void LHESource::putRunInfoProduct(edm::RunPrincipal& iRunPrincipal) {
0115 if (runInfoProductLast_) {
0116 auto product = std::make_unique<LHERunInfoProduct>(*runInfoProductLast_);
0117 std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHERunInfoProduct>(std::move(product)));
0118 iRunPrincipal.putOrMerge(lheProvenanceHelper_.runProductBranchDescription_, std::move(rdp));
0119 }
0120 }
0121
0122 bool LHESource::setRunAndEventInfo(edm::EventID&, edm::TimeValue_t&, edm::EventAuxiliary::ExperimentType&) {
0123 nextEvent();
0124 if (!partonLevel_) {
0125
0126 nextEvent();
0127 if (!partonLevel_) {
0128
0129 return false;
0130 }
0131 }
0132 return true;
0133 }
0134
0135 void LHESource::readEvent_(edm::EventPrincipal& eventPrincipal) {
0136 assert(eventCached() || processingMode() != RunsLumisAndEvents);
0137 edm::EventAuxiliary aux(eventID(), processGUID(), edm::Timestamp(presentTime()), false);
0138 aux.setProcessHistoryID(phid_);
0139 eventPrincipal.fillEventPrincipal(aux, processHistoryRegistry().getMapped(aux.processHistoryID()));
0140
0141 std::unique_ptr<LHEEventProduct> product(
0142 new LHEEventProduct(*partonLevel_->getHEPEUP(), partonLevel_->originalXWGTUP()));
0143 if (partonLevel_->getPDF()) {
0144 product->setPDF(*partonLevel_->getPDF());
0145 }
0146 std::for_each(partonLevel_->weights().begin(),
0147 partonLevel_->weights().end(),
0148 std::bind(&LHEEventProduct::addWeight, product.get(), std::placeholders::_1));
0149 product->setScales(partonLevel_->scales());
0150 product->setNpLO(partonLevel_->npLO());
0151 product->setNpNLO(partonLevel_->npNLO());
0152 std::for_each(partonLevel_->getComments().begin(),
0153 partonLevel_->getComments().end(),
0154 std::bind(&LHEEventProduct::addComment, product.get(), std::placeholders::_1));
0155
0156 std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<LHEEventProduct>(std::move(product)));
0157 eventPrincipal.put(lheProvenanceHelper_.eventProductBranchDescription_,
0158 std::move(edp),
0159 lheProvenanceHelper_.eventProductProvenance_);
0160
0161 partonLevel_.reset();
0162
0163 resetEventCached();
0164 }
0165
0166 std::shared_ptr<edm::RunAuxiliary> LHESource::readRunAuxiliary_() {
0167 edm::Timestamp ts = edm::Timestamp(presentTime());
0168 resetNewRun();
0169 auto aux = std::make_shared<edm::RunAuxiliary>(eventID().run(), ts, edm::Timestamp::invalidTimestamp());
0170 aux->setProcessHistoryID(phid_);
0171 return aux;
0172 }
0173
0174 std::shared_ptr<edm::LuminosityBlockAuxiliary> LHESource::readLuminosityBlockAuxiliary_() {
0175 if (processingMode() == Runs)
0176 return std::shared_ptr<edm::LuminosityBlockAuxiliary>();
0177 edm::Timestamp ts = edm::Timestamp(presentTime());
0178 resetNewLumi();
0179 auto aux = std::make_shared<edm::LuminosityBlockAuxiliary>(
0180 eventID().run(), eventID().luminosityBlock(), ts, edm::Timestamp::invalidTimestamp());
0181 aux->setProcessHistoryID(phid_);
0182 return aux;
0183 }
0184
0185 void LHESource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0186 edm::ParameterSetDescription desc;
0187 desc.setComment("A source which reads LHE files.");
0188 edm::ProducerSourceFromFiles::fillDescription(desc);
0189 desc.addUntracked<unsigned int>("skipEvents", 0U)->setComment("Skip the first 'skipEvents' events.");
0190 descriptions.add("source", desc);
0191 }
0192
0193 DEFINE_FWK_INPUT_SOURCE(LHESource);