Skip to content

Commit

Permalink
Merge pull request #25632 from Dr15Jones/modernMemoryHadronizer
Browse files Browse the repository at this point in the history
Modernized the memory handling of Hadronizers
  • Loading branch information
cmsbuild committed Jan 23, 2019
2 parents 8b6f399 + f186735 commit 7074981
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 59 deletions.
23 changes: 12 additions & 11 deletions GeneratorInterface/Core/interface/BaseHadronizer.h
Expand Up @@ -14,7 +14,7 @@
#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>
#include <memory>

#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"

Expand Down Expand Up @@ -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<HepMC::GenEvent> getGenEvent() { return std::move(genEvent_); }
std::unique_ptr<GenEventInfoProduct> getGenEventInfo() { return std::move(genEventInfo_); }
virtual std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const;
std::unique_ptr<lhef::LHEEvent> getLHEEvent() { return std::move(lheEvent_);}

void resetEvent(std::unique_ptr<HepMC::GenEvent> event) { genEvent_ = std::move(event); }
void resetEventInfo(std::unique_ptr<GenEventInfoProduct> eventInfo) { genEventInfo_ = std::move(eventInfo); }

// LHERunInfo and LHEEvent passing
const boost::shared_ptr<lhef::LHERunInfo> &getLHERunInfo() const { return lheRunInfo_; }
const std::shared_ptr<lhef::LHERunInfo> &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<lhef::LHERunInfo> runInfo) { lheRunInfo_ = std::move(runInfo); }
void setLHEEvent(std::unique_ptr<lhef::LHEEvent> event) { lheEvent_ = std::move(event); }

// interface for accessing the EDM information from the hadronizer
void setEDMEvent(edm::Event &event) { edmEvent_ = &event; }
Expand Down Expand Up @@ -101,7 +102,7 @@ namespace gen {
std::unique_ptr<HepMC::GenEvent> genEvent_;
std::unique_ptr<GenEventInfoProduct> genEventInfo_;

boost::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
std::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
std::unique_ptr<lhef::LHEEvent> lheEvent_;

edm::Event *edmEvent_;
Expand Down
8 changes: 4 additions & 4 deletions GeneratorInterface/Core/interface/GeneratorFilter.h
Expand Up @@ -158,7 +158,7 @@ namespace edm
//
if ( !hadronizer_.decay() ) return false;

event = std::unique_ptr<HepMC::GenEvent>(hadronizer_.getGenEvent());
event = hadronizer_.getGenEvent();
if ( !event.get() ) return false;

// The external decay driver is being added to the system,
Expand All @@ -181,7 +181,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
Expand All @@ -190,15 +190,15 @@ namespace edm

hadronizer_.finalizeEvent();

event.reset( hadronizer_.getGenEvent() );
event = hadronizer_.getGenEvent();
if ( !event.get() ) return false;

event->set_event_number( ev.id().event() );

//
// tutto bene - finally, form up EDM products !
//
std::unique_ptr<GenEventInfoProduct> genEventInfo(hadronizer_.getGenEventInfo());
auto genEventInfo = hadronizer_.getGenEventInfo();
if (!genEventInfo.get())
{
// create GenEventInfoProduct from HepMC event in case hadronizer didn't provide one
Expand Down
14 changes: 7 additions & 7 deletions GeneratorInterface/Core/interface/HadronizerFilter.h
Expand Up @@ -199,9 +199,7 @@ namespace edm

for (unsigned int itry = 0; itry<nAttempts_; ++itry) {

lhef::LHEEvent *lheEvent =
new lhef::LHEEvent(hadronizer_.getLHERunInfo(), *product);
hadronizer_.setLHEEvent( lheEvent );
hadronizer_.setLHEEvent(std::make_unique<lhef::LHEEvent>(hadronizer_.getLHERunInfo(), *product) );

// hadronizer_.generatePartons();
if ( !hadronizer_.hadronize() ) continue ;
Expand All @@ -222,23 +220,25 @@ 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;

// 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() );
Expand Down Expand Up @@ -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<lhef::LHERunInfo>(*lheRunInfoProduct) );
lhef::LHERunInfo* lheRunInfo = hadronizer_.getLHERunInfo().get();
lheRunInfo->initLumi();

Expand Down
4 changes: 2 additions & 2 deletions GeneratorInterface/Core/src/BaseHadronizer.cc
Expand Up @@ -44,9 +44,9 @@ const std::vector<std::string> BaseHadronizer::theSharedResources;

}

GenLumiInfoHeader *BaseHadronizer::getGenLumiInfoHeader() const {
std::unique_ptr<GenLumiInfoHeader> BaseHadronizer::getGenLumiInfoHeader() const {

GenLumiInfoHeader *genLumiInfoHeader = new GenLumiInfoHeader();
auto genLumiInfoHeader = std::make_unique<GenLumiInfoHeader>();

//fill information on randomized configs for parameter scans
genLumiInfoHeader->setRandomConfigIndex(randomIndex_);
Expand Down
14 changes: 6 additions & 8 deletions GeneratorInterface/LHEInterface/interface/LHEEvent.h
Expand Up @@ -7,8 +7,6 @@
#include <vector>
#include <string>

#include <boost/shared_ptr.hpp>

#include "HepMC/GenEvent.h"
#include "HepMC/GenVertex.h"
#include "HepMC/PdfInfo.h"
Expand All @@ -24,22 +22,22 @@ namespace lhef {

class LHEEvent {
public:
LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
std::istream &in);
LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const HEPEUP &hepeup);
LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const HEPEUP &hepeup,
const LHEEventProduct::PDF *pdf,
const std::vector<std::string> &comments);
LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const LHEEventProduct &product);
~LHEEvent();

typedef LHEEventProduct::PDF PDF;
typedef LHEEventProduct::WGT WGT;

const boost::shared_ptr<LHERunInfo> &getRunInfo() const { return runInfo; }
const std::shared_ptr<LHERunInfo> &getRunInfo() const { return runInfo; }
const HEPEUP *getHEPEUP() const { return &hepeup; }
const HEPRUP *getHEPRUP() const { return runInfo->getHEPRUP(); }
const PDF *getPDF() const { return pdf.get(); }
Expand Down Expand Up @@ -85,7 +83,7 @@ class LHEEvent {
static bool checkHepMCTree(const HepMC::GenEvent *event);
HepMC::GenParticle *makeHepMCParticle(unsigned int i) const;

const boost::shared_ptr<LHERunInfo> runInfo;
const std::shared_ptr<LHERunInfo> runInfo;

HEPEUP hepeup;
std::unique_ptr<PDF> pdf;
Expand Down
6 changes: 2 additions & 4 deletions GeneratorInterface/LHEInterface/interface/LHEReader.h
Expand Up @@ -5,8 +5,6 @@
#include <vector>
#include <memory>

#include <boost/shared_ptr.hpp>

#include "FWCore/ParameterSet/interface/ParameterSet.h"

namespace lhef {
Expand All @@ -24,7 +22,7 @@ class LHEReader {
unsigned int skip = 0);
~LHEReader();

boost::shared_ptr<LHEEvent> next(bool* newFileOpened = nullptr);
std::shared_ptr<LHEEvent> next(bool* newFileOpened = nullptr);

private:
class Source;
Expand All @@ -41,7 +39,7 @@ class LHEReader {

std::unique_ptr<Source> curSource;
std::unique_ptr<XMLDocument> curDoc;
boost::shared_ptr<LHERunInfo> curRunInfo;
std::shared_ptr<LHERunInfo> curRunInfo;
std::unique_ptr<XMLHandler> handler;
std::shared_ptr<void> platform;
};
Expand Down
Expand Up @@ -97,9 +97,9 @@ class ExternalLHEProducer : public edm::one::EDProducer<edm::BeginRunProducer,
std::string outputContents_;

std::unique_ptr<lhef::LHEReader> reader_;
boost::shared_ptr<lhef::LHERunInfo> runInfoLast;
boost::shared_ptr<lhef::LHERunInfo> runInfo;
boost::shared_ptr<lhef::LHEEvent> partonLevel;
std::shared_ptr<lhef::LHERunInfo> runInfoLast;
std::shared_ptr<lhef::LHERunInfo> runInfo;
std::shared_ptr<lhef::LHEEvent> partonLevel;
boost::ptr_deque<LHERunInfoProduct> runInfoProducts;
bool wasMerged;

Expand Down Expand Up @@ -513,7 +513,7 @@ void ExternalLHEProducer::nextEvent()
if (!partonLevel)
return;

boost::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel->getRunInfo();
std::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel->getRunInfo();
if (runInfoThis != runInfoLast) {
runInfo = runInfoThis;
runInfoLast = runInfoThis;
Expand Down
4 changes: 2 additions & 2 deletions GeneratorInterface/LHEInterface/plugins/LHESource.h
Expand Up @@ -54,8 +54,8 @@ class LHESource : public edm::ProducerSourceFromFiles {

std::unique_ptr<lhef::LHEReader> reader_;

boost::shared_ptr<lhef::LHERunInfo> runInfoLast_;
boost::shared_ptr<lhef::LHEEvent> partonLevel_;
std::shared_ptr<lhef::LHERunInfo> runInfoLast_;
std::shared_ptr<lhef::LHEEvent> partonLevel_;

std::unique_ptr<LHERunInfoProduct> runInfoProductLast_;
edm::LHEProvenanceHelper lheProvenanceHelper_;
Expand Down
8 changes: 4 additions & 4 deletions GeneratorInterface/LHEInterface/src/LHEEvent.cc
Expand Up @@ -33,7 +33,7 @@ static int skipWhitespace(std::istream &in)

namespace lhef {

LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
std::istream &in) :
runInfo(runInfo), weights_(0), counted(false),
readAttemptCounter(0), npLO_(-99), npNLO_(-99)
Expand Down Expand Up @@ -113,14 +113,14 @@ LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
" content after event data." << std::endl;
}

LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const HEPEUP &hepeup) :
runInfo(runInfo), hepeup(hepeup), counted(false), readAttemptCounter(0),
npLO_(-99), npNLO_(-99)
{
}

LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const HEPEUP &hepeup,
const LHEEventProduct::PDF *pdf,
const std::vector<std::string> &comments) :
Expand All @@ -130,7 +130,7 @@ LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
{
}

LHEEvent::LHEEvent(const boost::shared_ptr<LHERunInfo> &runInfo,
LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
const LHEEventProduct &product) :
runInfo(runInfo), hepeup(product.hepeup()),
pdf(product.pdf() ? new PDF(*product.pdf()) : nullptr),
Expand Down
10 changes: 5 additions & 5 deletions GeneratorInterface/LHEInterface/src/LHEReader.cc
Expand Up @@ -487,7 +487,7 @@ LHEReader::~LHEReader()
curSource.release();
}

boost::shared_ptr<LHEEvent> LHEReader::next(bool* newFileOpened)
std::shared_ptr<LHEEvent> LHEReader::next(bool* newFileOpened)
{
while(curDoc.get() || curIndex < fileURLs.size() || (fileURLs.empty() && !strName.empty() ) ) {
if (!curDoc.get()) {
Expand Down Expand Up @@ -520,7 +520,7 @@ LHEReader::~LHEReader()
if (!curDoc->parse()) {
curDoc.reset();
logFileAction(" Closed LHE file ", fileURLs[curIndex - 1]);
return boost::shared_ptr<LHEEvent>();
return std::shared_ptr<LHEEvent>();
}
break;

Expand Down Expand Up @@ -559,15 +559,15 @@ LHEReader::~LHEReader()
}

if (maxEvents == 0)
return boost::shared_ptr<LHEEvent>();
return std::shared_ptr<LHEEvent>();
else if (maxEvents > 0)
maxEvents--;

std::istringstream data;
data.str(handler->buffer);
handler->buffer.clear();

boost::shared_ptr<LHEEvent> lheevent;
std::shared_ptr<LHEEvent> lheevent;
lheevent.reset(new LHEEvent(curRunInfo, data));
const XMLHandler::wgt_info& info = handler->weightsinevent;
for( size_t i=0; i< info.size(); ++i ) {
Expand All @@ -586,7 +586,7 @@ LHEReader::~LHEReader()
}
}

return boost::shared_ptr<LHEEvent>();
return std::shared_ptr<LHEEvent>();
}

} // namespace lhef
Expand Down
Expand Up @@ -93,7 +93,7 @@ class Pythia8Hadronizer : public Py8InterfaceBase {

const char *classname() const override { return "Pythia8Hadronizer"; }

GenLumiInfoHeader *getGenLumiInfoHeader() const override;
std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const override;

private:

Expand Down Expand Up @@ -944,8 +944,8 @@ void Pythia8Hadronizer::finalizeEvent()
}
}

GenLumiInfoHeader *Pythia8Hadronizer::getGenLumiInfoHeader() const {
GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
std::unique_ptr<GenLumiInfoHeader> Pythia8Hadronizer::getGenLumiInfoHeader() const {
auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();

//fill lhe headers
//*FIXME* initrwgt header is corrupt due to pythia bug
Expand Down
10 changes: 5 additions & 5 deletions GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc
Expand Up @@ -52,7 +52,7 @@ class SherpaHadronizer : public gen::BaseHadronizer {
bool rearrangeWeights;
bool residualDecay();
void finalizeEvent();
GenLumiInfoHeader *getGenLumiInfoHeader() const override;
std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const override;
const char *classname() const { return "SherpaHadronizer"; }


Expand Down Expand Up @@ -261,7 +261,7 @@ bool SherpaHadronizer::generatePartonsAndHadronize()
}
if (rc) {
//convert it to HepMC2
HepMC::GenEvent* evt = new HepMC::GenEvent();
auto evt = std::make_unique<HepMC::GenEvent>();
Generator->FillHepMCEvent(*evt);

// in case of unweighted events sherpa puts the max weight as event weight.
Expand Down Expand Up @@ -317,7 +317,7 @@ bool SherpaHadronizer::generatePartonsAndHadronize()
if(unweighted){
evt->weights()[0]/=weight_normalization;
}
resetEvent(evt);
resetEvent(std::move(evt));
return true;
}
else {
Expand Down Expand Up @@ -365,8 +365,8 @@ double CMS_SHERPA_RNG::Get() {
return randomEngine->flat();

}
GenLumiInfoHeader *SherpaHadronizer::getGenLumiInfoHeader() const {
GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
std::unique_ptr<GenLumiInfoHeader> SherpaHadronizer::getGenLumiInfoHeader() const {
auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();

if(rearrangeWeights){
edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
Expand Down

0 comments on commit 7074981

Please sign in to comment.