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