Skip to content

Commit

Permalink
Merge pull request #37689 from mkirsano/hepmc3-addtogencore
Browse files Browse the repository at this point in the history
Add HepMC3 products and methods to Generator base clases
  • Loading branch information
cmsbuild authored Jun 10, 2022
2 parents f1b0cbe + 50e9acb commit 979f025
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 18 deletions.
1 change: 1 addition & 0 deletions GeneratorInterface/Core/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<use name="heppdt"/>
<use name="boost"/>
<use name="hepmc"/>
<use name="hepmc3"/>
<use name="clhep"/>
<use name="lhapdf"/>
<use name="f77compiler"/>
Expand Down
13 changes: 13 additions & 0 deletions GeneratorInterface/Core/interface/BaseHadronizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include <memory>

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

#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct3.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"

#include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
Expand Down Expand Up @@ -51,12 +53,16 @@ namespace gen {
// GenRunInfo and GenEvent passing
GenRunInfoProduct& getGenRunInfo() { return genRunInfo_; }
std::unique_ptr<HepMC::GenEvent> getGenEvent() { return std::move(genEvent_); }
std::unique_ptr<HepMC3::GenEvent> getGenEvent3() { return std::move(genEvent3_); }
std::unique_ptr<GenEventInfoProduct> getGenEventInfo() { return std::move(genEventInfo_); }
std::unique_ptr<GenEventInfoProduct3> getGenEventInfo3() { return std::move(genEventInfo3_); }
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 resetEvent3(std::unique_ptr<HepMC3::GenEvent> event3) { genEvent3_ = std::move(event3); }
void resetEventInfo(std::unique_ptr<GenEventInfoProduct> eventInfo) { genEventInfo_ = std::move(eventInfo); }
void resetEventInfo3(std::unique_ptr<GenEventInfoProduct3> eventInfo) { genEventInfo3_ = std::move(eventInfo); }

// LHERunInfo and LHEEvent passing
const std::shared_ptr<lhef::LHERunInfo>& getLHERunInfo() const { return lheRunInfo_; }
Expand All @@ -80,11 +86,16 @@ namespace gen {
void randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine);
void generateLHE(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine, unsigned int ncpu);
void cleanLHE();
unsigned int getVHepMC() { return ivhepmc; }

protected:
unsigned int ivhepmc = 2;
GenRunInfoProduct& runInfo() { return genRunInfo_; }
std::unique_ptr<HepMC::GenEvent>& event() { return genEvent_; }
std::unique_ptr<GenEventInfoProduct>& eventInfo() { return genEventInfo_; }
//HepMC3:
std::unique_ptr<HepMC3::GenEvent>& event3() { return genEvent3_; }
std::unique_ptr<GenEventInfoProduct3>& eventInfo3() { return genEventInfo3_; }

lhef::LHEEvent* lheEvent() { return lheEvent_.get(); }
lhef::LHERunInfo* lheRunInfo() { return lheRunInfo_.get(); }
Expand All @@ -98,7 +109,9 @@ namespace gen {

GenRunInfoProduct genRunInfo_;
std::unique_ptr<HepMC::GenEvent> genEvent_;
std::unique_ptr<HepMC3::GenEvent> genEvent3_;
std::unique_ptr<GenEventInfoProduct> genEventInfo_;
std::unique_ptr<GenEventInfoProduct3> genEventInfo3_;

std::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
std::unique_ptr<lhef::LHEEvent> lheEvent_;
Expand Down
80 changes: 62 additions & 18 deletions GeneratorInterface/Core/interface/GeneratorFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <string>
#include <vector>

#include "HepMC3/GenEvent.h"

#include "FWCore/Concurrency/interface/SharedResourceNames.h"
#include "FWCore/Framework/interface/one/EDFilter.h"
#include "FWCore/Framework/interface/Event.h"
Expand All @@ -35,6 +37,7 @@
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct3.h"

namespace edm {
template <class HAD, class DEC>
Expand Down Expand Up @@ -71,6 +74,7 @@ namespace edm {
unsigned int nEventsInLumiBlock_ = 0;
unsigned int nThreads_{1};
bool initialized_ = false;
unsigned int ivhepmc = 2;
};

//------------------------------------------------------------------------
Expand All @@ -93,6 +97,8 @@ namespace edm {
// other maybe added as needs be
//

ivhepmc = hadronizer_.getVHepMC();

std::vector<std::string> const& sharedResources = hadronizer_.sharedResources();
for (auto const& resource : sharedResources) {
usesResource(resource);
Expand All @@ -115,8 +121,13 @@ namespace edm {
usesResource(edm::uniqueSharedResourceName());
}

produces<edm::HepMCProduct>("unsmeared");
produces<GenEventInfoProduct>();
if (ivhepmc == 2) {
produces<edm::HepMCProduct>("unsmeared");
produces<GenEventInfoProduct>();
} else if (ivhepmc == 3) {
//produces<edm::HepMC3Product>("unsmeared");
//produces<GenEventInfoProduct3>();
}
produces<GenLumiInfoHeader, edm::Transition::BeginLuminosityBlock>();
produces<GenLumiInfoProduct, edm::Transition::EndLuminosityBlock>();
produces<GenRunInfoProduct, edm::Transition::EndRun>();
Expand All @@ -142,9 +153,11 @@ namespace edm {

bool passEvtGenSelector = false;
std::unique_ptr<HepMC::GenEvent> event(nullptr);
std::unique_ptr<HepMC3::GenEvent> event3(nullptr);

while (!passEvtGenSelector) {
event.reset();
event3.reset();
hadronizer_.setEDMEvent(ev);

if (!hadronizer_.generatePartonsAndHadronize())
Expand All @@ -160,19 +173,24 @@ namespace edm {
return false;

event = hadronizer_.getGenEvent();
if (!event.get())
event3 = hadronizer_.getGenEvent3();
if (ivhepmc == 2 && !event.get())
return false;
if (ivhepmc == 3 && !event3.get())
return false;

// The external decay driver is being added to the system,
// it should be called here
//
if (decayer_) {
if (decayer_) { // handle only HepMC2 for the moment
auto t = decayer_->decay(event.get());
if (t != event.get()) {
event.reset(t);
}
}
if (!event.get())
if (ivhepmc == 2 && !event.get())
return false;
if (ivhepmc == 3 && !event3.get())
return false;

passEvtGenSelector = hadronizer_.select(event.get());
Expand All @@ -182,7 +200,10 @@ namespace edm {
//
// fisrt of all, put back modified event tree (after external decay)
//
hadronizer_.resetEvent(std::move(event));
if (ivhepmc == 2)
hadronizer_.resetEvent(std::move(event));
else if (ivhepmc == 3)
hadronizer_.resetEvent3(std::move(event3));

//
// now run residual decays
Expand All @@ -193,25 +214,48 @@ namespace edm {
hadronizer_.finalizeEvent();

event = hadronizer_.getGenEvent();
if (!event.get())
return false;
event3 = hadronizer_.getGenEvent3();
if (ivhepmc == 2) { // HepMC
if (!event.get())
return false;
event->set_event_number(ev.id().event());

event->set_event_number(ev.id().event());
} else if (ivhepmc == 3) { // HepMC3
if (!event3.get())
return false;
event3->set_event_number(ev.id().event());
}

//
// tutto bene - finally, form up EDM products !
//
auto genEventInfo = hadronizer_.getGenEventInfo();
if (!genEventInfo.get()) {
// create GenEventInfoProduct from HepMC event in case hadronizer didn't provide one
genEventInfo.reset(new GenEventInfoProduct(event.get()));
}
if (ivhepmc == 2) { // HepMC
auto genEventInfo = hadronizer_.getGenEventInfo();
if (!genEventInfo.get()) {
// create GenEventInfoProduct from HepMC event in case hadronizer didn't provide one
genEventInfo.reset(new GenEventInfoProduct(event.get()));
}

ev.put(std::move(genEventInfo));

ev.put(std::move(genEventInfo));
std::unique_ptr<HepMCProduct> bare_product(new HepMCProduct());
bare_product->addHepMCData(event.release());
ev.put(std::move(bare_product), "unsmeared");

} else if (ivhepmc == 3) { // HepMC3
auto genEventInfo3 = hadronizer_.getGenEventInfo3();
if (!genEventInfo3.get()) {
// create GenEventInfoProduct3 from HepMC3 event in case hadronizer didn't provide one
genEventInfo3.reset(new GenEventInfoProduct3(event3.get()));
}

//ev.put(std::move(genEventInfo3));

//std::unique_ptr<HepMCProduct3> bare_product(new HepMCProduct3());
//bare_product->addHepMCData(event3.release());
//ev.put(std::move(bare_product), "unsmeared");
}

std::unique_ptr<HepMCProduct> bare_product(new HepMCProduct());
bare_product->addHepMCData(event.release());
ev.put(std::move(bare_product), "unsmeared");
nEventsInLumiBlock_++;
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions GeneratorInterface/Core/test/FailingGeneratorFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// system include files

// user include files
#include "GeneratorInterface/Core/interface/BaseHadronizer.h"
#include "GeneratorInterface/Core/interface/GeneratorFilter.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Utilities/interface/Exception.h"
Expand Down Expand Up @@ -41,13 +42,17 @@ namespace test {
}
bool decay() const { return true; }

unsigned int getVHepMC() { return ivhepmc; }
std::unique_ptr<HepMC::GenEvent> getGenEvent() { return std::move(event_); }
std::unique_ptr<HepMC3::GenEvent> getGenEvent3() { return std::move(event3_); }

bool select(HepMC::GenEvent*) const { return true; }
void resetEvent(std::unique_ptr<HepMC::GenEvent> iEvent) { event_ = std::move(iEvent); }
void resetEvent3(std::unique_ptr<HepMC3::GenEvent> iEvent) { event3_ = std::move(iEvent); }
bool residualDecay() const { return true; }
void finalizeEvent() const {}
std::unique_ptr<GenEventInfoProduct> getGenEventInfo() const { return std::make_unique<GenEventInfoProduct>(); }
std::unique_ptr<GenEventInfoProduct3> getGenEventInfo3() const { return std::make_unique<GenEventInfoProduct3>(); }

//caled at endRunProduce, endLumiProduce
void statistics() const {}
Expand Down Expand Up @@ -102,8 +107,10 @@ namespace test {
}
}
std::unique_ptr<HepMC::GenEvent> event_;
std::unique_ptr<HepMC3::GenEvent> event3_;
int failAt_;
int failureType_;
unsigned int ivhepmc = 2;
};

class DummyDec {
Expand Down

0 comments on commit 979f025

Please sign in to comment.