Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-08 23:11:13

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