From f1867357c576690f45d03859e9db4a8abc58c55b Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Fri, 11 Jan 2019 16:16:52 -0600 Subject: [PATCH] Modernized the memory handling of Hadronizers -Switched from boost::shared_ptr to std::shared_ptr -Use std::unique_ptr directly to transfer memory ownership --- .../Core/interface/BaseHadronizer.h | 23 ++++++++++--------- .../Core/interface/GeneratorFilter.h | 8 +++---- .../Core/interface/HadronizerFilter.h | 14 +++++------ GeneratorInterface/Core/src/BaseHadronizer.cc | 4 ++-- .../LHEInterface/interface/LHEEvent.h | 14 +++++------ .../LHEInterface/interface/LHEReader.h | 6 ++--- .../plugins/ExternalLHEProducer.cc | 8 +++---- .../LHEInterface/plugins/LHESource.h | 4 ++-- .../LHEInterface/src/LHEEvent.cc | 8 +++---- .../LHEInterface/src/LHEReader.cc | 10 ++++---- .../plugins/Pythia8Hadronizer.cc | 6 ++--- .../SherpaInterface/src/SherpaHadronizer.cc | 10 ++++---- 12 files changed, 56 insertions(+), 59 deletions(-) diff --git a/GeneratorInterface/Core/interface/BaseHadronizer.h b/GeneratorInterface/Core/interface/BaseHadronizer.h index eb8fcc86cc4f4..87f2ae07c2be1 100644 --- a/GeneratorInterface/Core/interface/BaseHadronizer.h +++ b/GeneratorInterface/Core/interface/BaseHadronizer.h @@ -14,7 +14,7 @@ #include #include -#include +#include #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h" @@ -51,18 +51,19 @@ namespace gen { // GenRunInfo and GenEvent passing GenRunInfoProduct &getGenRunInfo() { return genRunInfo_; } - HepMC::GenEvent *getGenEvent() { return genEvent_.release(); } - GenEventInfoProduct *getGenEventInfo() { return genEventInfo_.release(); } - virtual GenLumiInfoHeader *getGenLumiInfoHeader() const; - - void resetEvent(HepMC::GenEvent *event) { genEvent_.reset(event); } - void resetEventInfo(GenEventInfoProduct *eventInfo) { genEventInfo_.reset(eventInfo); } + std::unique_ptr getGenEvent() { return std::move(genEvent_); } + std::unique_ptr getGenEventInfo() { return std::move(genEventInfo_); } + virtual std::unique_ptr getGenLumiInfoHeader() const; + std::unique_ptr getLHEEvent() { return std::move(lheEvent_);} + + void resetEvent(std::unique_ptr event) { genEvent_ = std::move(event); } + void resetEventInfo(std::unique_ptr eventInfo) { genEventInfo_ = std::move(eventInfo); } // LHERunInfo and LHEEvent passing - const boost::shared_ptr &getLHERunInfo() const { return lheRunInfo_; } + const std::shared_ptr &getLHERunInfo() const { return lheRunInfo_; } - void setLHERunInfo(lhef::LHERunInfo *runInfo) { lheRunInfo_.reset(runInfo); } - void setLHEEvent(lhef::LHEEvent *event) { lheEvent_.reset(event); } + void setLHERunInfo(std::unique_ptr runInfo) { lheRunInfo_ = std::move(runInfo); } + void setLHEEvent(std::unique_ptr event) { lheEvent_ = std::move(event); } // interface for accessing the EDM information from the hadronizer void setEDMEvent(edm::Event &event) { edmEvent_ = &event; } @@ -101,7 +102,7 @@ namespace gen { std::unique_ptr genEvent_; std::unique_ptr genEventInfo_; - boost::shared_ptr lheRunInfo_; + std::shared_ptr lheRunInfo_; std::unique_ptr lheEvent_; edm::Event *edmEvent_; diff --git a/GeneratorInterface/Core/interface/GeneratorFilter.h b/GeneratorInterface/Core/interface/GeneratorFilter.h index 7deb56b80485b..449836117f815 100644 --- a/GeneratorInterface/Core/interface/GeneratorFilter.h +++ b/GeneratorInterface/Core/interface/GeneratorFilter.h @@ -157,7 +157,7 @@ namespace edm // if ( !hadronizer_.decay() ) return false; - event = std::unique_ptr(hadronizer_.getGenEvent()); + event = hadronizer_.getGenEvent(); if ( !event.get() ) return false; // The external decay driver is being added to the system, @@ -180,7 +180,7 @@ namespace edm // // fisrt of all, put back modified event tree (after external decay) // - hadronizer_.resetEvent( event.release() ); + hadronizer_.resetEvent( std::move(event) ); // // now run residual decays @@ -189,7 +189,7 @@ namespace edm hadronizer_.finalizeEvent(); - event.reset( hadronizer_.getGenEvent() ); + event = hadronizer_.getGenEvent(); if ( !event.get() ) return false; event->set_event_number( ev.id().event() ); @@ -197,7 +197,7 @@ namespace edm // // tutto bene - finally, form up EDM products ! // - std::unique_ptr genEventInfo(hadronizer_.getGenEventInfo()); + auto genEventInfo = hadronizer_.getGenEventInfo(); if (!genEventInfo.get()) { // create GenEventInfoProduct from HepMC event in case hadronizer didn't provide one diff --git a/GeneratorInterface/Core/interface/HadronizerFilter.h b/GeneratorInterface/Core/interface/HadronizerFilter.h index 92c9b884b99a4..41987651612eb 100644 --- a/GeneratorInterface/Core/interface/HadronizerFilter.h +++ b/GeneratorInterface/Core/interface/HadronizerFilter.h @@ -199,9 +199,7 @@ namespace edm for (unsigned int itry = 0; itry(hadronizer_.getLHERunInfo(), *product) ); // hadronizer_.generatePartons(); if ( !hadronizer_.hadronize() ) continue ; @@ -222,10 +220,12 @@ namespace edm // if ( decayer_ ) { - auto t = decayer_->decay( event.get(),lheEvent ); + auto lheEvent = hadronizer_.getLHEEvent(); + auto t = decayer_->decay( event.get(),lheEvent.get() ); if(t != event.get()) { event.reset(t); } + hadronizer_.setLHEEvent(std::move(lheEvent)); } if ( !event.get() ) continue; @@ -233,12 +233,12 @@ namespace edm // check and perform if there're any unstable particles after // running external decay packges // - hadronizer_.resetEvent( event.release() ); + hadronizer_.resetEvent( std::move(event) ); if ( !hadronizer_.residualDecay() ) continue; hadronizer_.finalizeEvent(); - event.reset( hadronizer_.getGenEvent() ); + event = hadronizer_.getGenEvent(); if ( !event.get() ) continue; event->set_event_number( ev.id().event() ); @@ -308,7 +308,7 @@ namespace edm //TODO: fix so that this actually works with getByToken commented below... //run.getByToken(runInfoProductToken_, lheRunInfoProduct); - hadronizer_.setLHERunInfo( new lhef::LHERunInfo(*lheRunInfoProduct) ); + hadronizer_.setLHERunInfo( std::make_unique(*lheRunInfoProduct) ); lhef::LHERunInfo* lheRunInfo = hadronizer_.getLHERunInfo().get(); lheRunInfo->initLumi(); diff --git a/GeneratorInterface/Core/src/BaseHadronizer.cc b/GeneratorInterface/Core/src/BaseHadronizer.cc index 534ec728fb54b..9817e55491105 100644 --- a/GeneratorInterface/Core/src/BaseHadronizer.cc +++ b/GeneratorInterface/Core/src/BaseHadronizer.cc @@ -44,9 +44,9 @@ const std::vector BaseHadronizer::theSharedResources; } - GenLumiInfoHeader *BaseHadronizer::getGenLumiInfoHeader() const { + std::unique_ptr BaseHadronizer::getGenLumiInfoHeader() const { - GenLumiInfoHeader *genLumiInfoHeader = new GenLumiInfoHeader(); + auto genLumiInfoHeader = std::make_unique(); //fill information on randomized configs for parameter scans genLumiInfoHeader->setRandomConfigIndex(randomIndex_); diff --git a/GeneratorInterface/LHEInterface/interface/LHEEvent.h b/GeneratorInterface/LHEInterface/interface/LHEEvent.h index 235cdd21bbe5b..eb7dff16ff932 100644 --- a/GeneratorInterface/LHEInterface/interface/LHEEvent.h +++ b/GeneratorInterface/LHEInterface/interface/LHEEvent.h @@ -7,8 +7,6 @@ #include #include -#include - #include "HepMC/GenEvent.h" #include "HepMC/GenVertex.h" #include "HepMC/PdfInfo.h" @@ -24,22 +22,22 @@ namespace lhef { class LHEEvent { public: - LHEEvent(const boost::shared_ptr &runInfo, + LHEEvent(const std::shared_ptr &runInfo, std::istream &in); - LHEEvent(const boost::shared_ptr &runInfo, + LHEEvent(const std::shared_ptr &runInfo, const HEPEUP &hepeup); - LHEEvent(const boost::shared_ptr &runInfo, + LHEEvent(const std::shared_ptr &runInfo, const HEPEUP &hepeup, const LHEEventProduct::PDF *pdf, const std::vector &comments); - LHEEvent(const boost::shared_ptr &runInfo, + LHEEvent(const std::shared_ptr &runInfo, const LHEEventProduct &product); ~LHEEvent(); typedef LHEEventProduct::PDF PDF; typedef LHEEventProduct::WGT WGT; - const boost::shared_ptr &getRunInfo() const { return runInfo; } + const std::shared_ptr &getRunInfo() const { return runInfo; } const HEPEUP *getHEPEUP() const { return &hepeup; } const HEPRUP *getHEPRUP() const { return runInfo->getHEPRUP(); } const PDF *getPDF() const { return pdf.get(); } @@ -85,7 +83,7 @@ class LHEEvent { static bool checkHepMCTree(const HepMC::GenEvent *event); HepMC::GenParticle *makeHepMCParticle(unsigned int i) const; - const boost::shared_ptr runInfo; + const std::shared_ptr runInfo; HEPEUP hepeup; std::unique_ptr pdf; diff --git a/GeneratorInterface/LHEInterface/interface/LHEReader.h b/GeneratorInterface/LHEInterface/interface/LHEReader.h index 086556a680040..b79b4dd3f6525 100644 --- a/GeneratorInterface/LHEInterface/interface/LHEReader.h +++ b/GeneratorInterface/LHEInterface/interface/LHEReader.h @@ -5,8 +5,6 @@ #include #include -#include - #include "FWCore/ParameterSet/interface/ParameterSet.h" namespace lhef { @@ -24,7 +22,7 @@ class LHEReader { unsigned int skip = 0); ~LHEReader(); - boost::shared_ptr next(bool* newFileOpened = nullptr); + std::shared_ptr next(bool* newFileOpened = nullptr); private: class Source; @@ -41,7 +39,7 @@ class LHEReader { std::unique_ptr curSource; std::unique_ptr curDoc; - boost::shared_ptr curRunInfo; + std::shared_ptr curRunInfo; std::unique_ptr handler; std::shared_ptr platform; }; diff --git a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc index 3bf7e83e14b2a..d91bf20146616 100644 --- a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc +++ b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc @@ -97,9 +97,9 @@ class ExternalLHEProducer : public edm::one::EDProducer reader_; - boost::shared_ptr runInfoLast; - boost::shared_ptr runInfo; - boost::shared_ptr partonLevel; + std::shared_ptr runInfoLast; + std::shared_ptr runInfo; + std::shared_ptr partonLevel; boost::ptr_deque runInfoProducts; bool wasMerged; @@ -513,7 +513,7 @@ void ExternalLHEProducer::nextEvent() if (!partonLevel) return; - boost::shared_ptr runInfoThis = partonLevel->getRunInfo(); + std::shared_ptr runInfoThis = partonLevel->getRunInfo(); if (runInfoThis != runInfoLast) { runInfo = runInfoThis; runInfoLast = runInfoThis; diff --git a/GeneratorInterface/LHEInterface/plugins/LHESource.h b/GeneratorInterface/LHEInterface/plugins/LHESource.h index 898e16569b78c..ad679810dc518 100644 --- a/GeneratorInterface/LHEInterface/plugins/LHESource.h +++ b/GeneratorInterface/LHEInterface/plugins/LHESource.h @@ -54,8 +54,8 @@ class LHESource : public edm::ProducerSourceFromFiles { std::unique_ptr reader_; - boost::shared_ptr runInfoLast_; - boost::shared_ptr partonLevel_; + std::shared_ptr runInfoLast_; + std::shared_ptr partonLevel_; std::unique_ptr runInfoProductLast_; edm::LHEProvenanceHelper lheProvenanceHelper_; diff --git a/GeneratorInterface/LHEInterface/src/LHEEvent.cc b/GeneratorInterface/LHEInterface/src/LHEEvent.cc index 0639d11c0e237..1912c0f7f4de4 100644 --- a/GeneratorInterface/LHEInterface/src/LHEEvent.cc +++ b/GeneratorInterface/LHEInterface/src/LHEEvent.cc @@ -33,7 +33,7 @@ static int skipWhitespace(std::istream &in) namespace lhef { -LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, +LHEEvent::LHEEvent(const std::shared_ptr &runInfo, std::istream &in) : runInfo(runInfo), weights_(0), counted(false), readAttemptCounter(0), npLO_(-99), npNLO_(-99) @@ -113,14 +113,14 @@ LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, " content after event data." << std::endl; } -LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, +LHEEvent::LHEEvent(const std::shared_ptr &runInfo, const HEPEUP &hepeup) : runInfo(runInfo), hepeup(hepeup), counted(false), readAttemptCounter(0), npLO_(-99), npNLO_(-99) { } -LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, +LHEEvent::LHEEvent(const std::shared_ptr &runInfo, const HEPEUP &hepeup, const LHEEventProduct::PDF *pdf, const std::vector &comments) : @@ -130,7 +130,7 @@ LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, { } -LHEEvent::LHEEvent(const boost::shared_ptr &runInfo, +LHEEvent::LHEEvent(const std::shared_ptr &runInfo, const LHEEventProduct &product) : runInfo(runInfo), hepeup(product.hepeup()), pdf(product.pdf() ? new PDF(*product.pdf()) : nullptr), diff --git a/GeneratorInterface/LHEInterface/src/LHEReader.cc b/GeneratorInterface/LHEInterface/src/LHEReader.cc index c3e9b13abfc79..0d07f80c11cfb 100644 --- a/GeneratorInterface/LHEInterface/src/LHEReader.cc +++ b/GeneratorInterface/LHEInterface/src/LHEReader.cc @@ -487,7 +487,7 @@ LHEReader::~LHEReader() curSource.release(); } - boost::shared_ptr LHEReader::next(bool* newFileOpened) + std::shared_ptr LHEReader::next(bool* newFileOpened) { while(curDoc.get() || curIndex < fileURLs.size() || (fileURLs.empty() && !strName.empty() ) ) { if (!curDoc.get()) { @@ -520,7 +520,7 @@ LHEReader::~LHEReader() if (!curDoc->parse()) { curDoc.reset(); logFileAction(" Closed LHE file ", fileURLs[curIndex - 1]); - return boost::shared_ptr(); + return std::shared_ptr(); } break; @@ -559,7 +559,7 @@ LHEReader::~LHEReader() } if (maxEvents == 0) - return boost::shared_ptr(); + return std::shared_ptr(); else if (maxEvents > 0) maxEvents--; @@ -567,7 +567,7 @@ LHEReader::~LHEReader() data.str(handler->buffer); handler->buffer.clear(); - boost::shared_ptr lheevent; + std::shared_ptr lheevent; lheevent.reset(new LHEEvent(curRunInfo, data)); const XMLHandler::wgt_info& info = handler->weightsinevent; for( size_t i=0; i< info.size(); ++i ) { @@ -586,7 +586,7 @@ LHEReader::~LHEReader() } } - return boost::shared_ptr(); + return std::shared_ptr(); } } // namespace lhef diff --git a/GeneratorInterface/Pythia8Interface/plugins/Pythia8Hadronizer.cc b/GeneratorInterface/Pythia8Interface/plugins/Pythia8Hadronizer.cc index 890272f9fc2fc..20dbb25981997 100644 --- a/GeneratorInterface/Pythia8Interface/plugins/Pythia8Hadronizer.cc +++ b/GeneratorInterface/Pythia8Interface/plugins/Pythia8Hadronizer.cc @@ -93,7 +93,7 @@ class Pythia8Hadronizer : public Py8InterfaceBase { const char *classname() const override { return "Pythia8Hadronizer"; } - GenLumiInfoHeader *getGenLumiInfoHeader() const override; + std::unique_ptr getGenLumiInfoHeader() const override; private: @@ -944,8 +944,8 @@ void Pythia8Hadronizer::finalizeEvent() } } -GenLumiInfoHeader *Pythia8Hadronizer::getGenLumiInfoHeader() const { - GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader(); +std::unique_ptr Pythia8Hadronizer::getGenLumiInfoHeader() const { + auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader(); //fill lhe headers //*FIXME* initrwgt header is corrupt due to pythia bug diff --git a/GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc b/GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc index ca6f3a6aae713..1a5ee1755ea39 100644 --- a/GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc +++ b/GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc @@ -52,7 +52,7 @@ class SherpaHadronizer : public gen::BaseHadronizer { bool rearrangeWeights; bool residualDecay(); void finalizeEvent(); - GenLumiInfoHeader *getGenLumiInfoHeader() const override; + std::unique_ptr getGenLumiInfoHeader() const override; const char *classname() const { return "SherpaHadronizer"; } @@ -261,7 +261,7 @@ bool SherpaHadronizer::generatePartonsAndHadronize() } if (rc) { //convert it to HepMC2 - HepMC::GenEvent* evt = new HepMC::GenEvent(); + auto evt = std::make_unique(); Generator->FillHepMCEvent(*evt); // in case of unweighted events sherpa puts the max weight as event weight. @@ -317,7 +317,7 @@ bool SherpaHadronizer::generatePartonsAndHadronize() if(unweighted){ evt->weights()[0]/=weight_normalization; } - resetEvent(evt); + resetEvent(std::move(evt)); return true; } else { @@ -365,8 +365,8 @@ double CMS_SHERPA_RNG::Get() { return randomEngine->flat(); } -GenLumiInfoHeader *SherpaHadronizer::getGenLumiInfoHeader() const { - GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader(); +std::unique_ptr SherpaHadronizer::getGenLumiInfoHeader() const { + auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader(); if(rearrangeWeights){ edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;