Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidying up of GenAlg #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 82 additions & 32 deletions k4Gen/src/components/GenAlg.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,110 @@

// GenAlg
#include "GenAlg.h"

// Gaudi
#include "GaudiKernel/IEventProcessor.h"
#include "GaudiKernel/IIncidentSvc.h"
#include "GaudiKernel/Incident.h"

// HepMC3
#include "HepMC3/GenEvent.h"

DECLARE_COMPONENT(GenAlg)

GenAlg::GenAlg(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc) {
declareProperty("SignalProvider", m_signalProvider,
"Signal events provider tool");
declareProperty("PileUpTool", m_pileUpTool, "Pileup tool");
declareProperty("PileUpProvider", m_pileUpProvider,
"Pileup events provider tool");
declareProperty("VertexSmearingTool", m_vertexSmearingTool,
"Vertex smearing tool");
declareProperty("HepMCMergeTool", m_HepMCMergeTool, "Event merge tool");
declareProperty("hepmc", m_hepmcHandle, "HepMC event handle (output)");
}

StatusCode GenAlg::initialize() {
{
StatusCode sc = GaudiAlgorithm::initialize();
if (!sc.isSuccess()) return sc;
}

declareProperty("PileUpTool", m_pileUpTool);
if (!m_signalProvider) {
error() << "Signal event provider is missing!" << endmsg;
return StatusCode::FAILURE;
}

declareProperty("VertexSmearingTool", m_vertexSmearingTool);
if (!m_vertexSmearingTool) {
error() << "Vertex smearing tool is missing!" << endmsg;
return StatusCode::FAILURE;
}

declareProperty("HepMCMergeTool", m_HepMCMergeTool);
if (!m_pileUpTool) {
error() << "Pileup tool is missing!" << endmsg;
return StatusCode::FAILURE;
}

declareProperty("SignalProvider", m_signalProvider);
declareProperty("PileUpProvider", m_pileUpProvider);
if (!m_pileUpProvider) {
error() << "Pileup provider is missing!" << endmsg;
return StatusCode::FAILURE;
}

declareProperty("hepmc", m_hepmchandle, "HepMC event handle (output)");
}
if (!m_HepMCMergeTool) {
error() << "Event merge tool is missing!" << endmsg;
return StatusCode::FAILURE;
}

StatusCode GenAlg::initialize() {
StatusCode sc = GaudiAlgorithm::initialize();
if (!sc.isSuccess()) return sc;
return sc;
return StatusCode::SUCCESS;
}

StatusCode GenAlg::execute() {
HepMC3::GenEvent* theEvent = m_hepmchandle.createAndPut();
// Create empty event
auto theEvent = m_hepmcHandle.createAndPut();
theEvent->set_units(HepMC3::Units::GEV, HepMC3::Units::MM);

// Get the event from the signal provider
{
StatusCode sc = m_signalProvider->getNextEvent(*theEvent);
if (!sc.isSuccess()) return sc;
}

// Smear vertex
{
StatusCode sc = m_vertexSmearingTool->smearVertex(*theEvent);
if (!sc.isSuccess()) return sc;
}

// Get number of pileup events
const unsigned int numPileUp = m_pileUpTool->numberOfPileUp();
std::vector<HepMC3::GenEvent> eventVector;
eventVector.reserve(numPileUp + 1);
StatusCode sc;
if (!m_signalProvider.empty()) {
sc = m_signalProvider->getNextEvent(*theEvent);
}
if (StatusCode::SUCCESS != sc) {
return sc;
}
m_vertexSmearingTool->smearVertex(*theEvent).ignore();
if (!m_pileUpProvider.empty()) {
for (unsigned int i_pileUp = 0; i_pileUp < numPileUp; ++i_pileUp) {
auto puEvt = HepMC3::GenEvent();
sc = m_pileUpProvider->getNextEvent(puEvt);
if (StatusCode::SUCCESS != sc) {
return sc;
debug() << "Number of pileup events: " << numPileUp << endmsg;

// Merge in pileup
if (numPileUp > 0) {
std::vector<HepMC3::GenEvent> eventVector;
eventVector.reserve(numPileUp + 1);

if (!m_pileUpProvider.empty()) {
for (unsigned int i_pileUp = 0; i_pileUp < numPileUp; ++i_pileUp) {
auto puEvt = HepMC3::GenEvent();
StatusCode sc = m_pileUpProvider->getNextEvent(puEvt);
if (!sc.isSuccess()) return sc;

m_vertexSmearingTool->smearVertex(puEvt).ignore();
eventVector.push_back(std::move(puEvt));
}
m_vertexSmearingTool->smearVertex(puEvt).ignore();
eventVector.push_back(std::move(puEvt));
}

StatusCode sc = m_HepMCMergeTool->merge(*theEvent, eventVector);
if (!sc.isSuccess()) return sc;
}
return m_HepMCMergeTool->merge(*theEvent, eventVector);

debug() << "Event number: " << theEvent->event_number() << endmsg;
debug() << "Number of particles in the event: " << theEvent->particles().size()
<< endmsg;
debug() << "Number of vertices in the event: " << theEvent->vertices().size()
<< endmsg;

return StatusCode::SUCCESS;
}

StatusCode GenAlg::finalize() { return GaudiAlgorithm::finalize(); }
32 changes: 17 additions & 15 deletions k4Gen/src/components/GenAlg.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

#ifndef GENERATION_GENALG_H
#define GENERATION_GENALG_H

// Gaudi
#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiKernel/ToolHandle.h"

// k4FWCore
#include "k4FWCore/DataHandle.h"

// k4Gen
#include "Generation/IHepMCMergeTool.h"
#include "Generation/IHepMCProviderTool.h"
#include "Generation/IPileUpTool.h"
#include "Generation/IVertexSmearingTool.h"


#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiKernel/ToolHandle.h"


#include "k4FWCore/DataHandle.h"
namespace HepMC3 {
class GenEvent;
class GenEvent;
}

class GenAlg : public GaudiAlgorithm {
Expand All @@ -30,17 +31,18 @@ class GenAlg : public GaudiAlgorithm {
virtual StatusCode finalize();

private:
/// Tools to handle input from HepMC-file
/// Tool to provide signal event
ToolHandle<IHepMCProviderTool> m_signalProvider{"MomentumRangeParticleGun/HepMCProviderTool", this};
ToolHandle<IHepMCProviderTool> m_pileUpProvider{"MomentumRangeParticleGun/HepMCProviderTool", this};
/// Tool to determine number of pileup events
ToolHandle<IPileUpTool> m_pileUpTool{"ConstPileUp/PileUpTool", this};

/// Tool to provide pile up event(s)
ToolHandle<IHepMCProviderTool> m_pileUpProvider{"MomentumRangeParticleGun/HepMCProviderTool", this};
// Tool to smear vertex
ToolHandle<IVertexSmearingTool> m_vertexSmearingTool{"FlatSmearVertex/VertexSmearingTool", this};
/// Tool to merge HepMC events
ToolHandle<IHepMCMergeTool> m_HepMCMergeTool{"HepMCSimpleMerge/HepMCMergeTool", this};
// Tool to smear vertices
ToolHandle<IVertexSmearingTool> m_vertexSmearingTool{"FlatSmearVertex/VertexSmearingTool", this};
// output handle for finished event
DataHandle<HepMC3::GenEvent> m_hepmchandle{"hepmc", Gaudi::DataHandle::Writer, this};
// Output handle for finished event
DataHandle<HepMC3::GenEvent> m_hepmcHandle{"hepmc", Gaudi::DataHandle::Writer, this};
};

#endif // GENERATION_GENALG_H
4 changes: 2 additions & 2 deletions k4Gen/src/components/HepMCToEDMConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ StatusCode HepMCToEDMConverter::execute() {
const HepMC3::GenEvent* evt = m_hepmchandle.get();
edm4hep::MCParticleCollection* particles = new edm4hep::MCParticleCollection();


std::unordered_map<unsigned int, edm4hep::MutableMCParticle> _map;
for (auto _p:evt->particles()) {
debug() << "Converting hepmc particle with Pdg_ID \t" << _p->pdg_id() << "and id \t" << _p->id() << endmsg;
verbose() << "Converting hepmc particle with Pdg_ID " << _p->pdg_id()
<< " and id " << _p->id() << endmsg;
if (_map.find(_p->id()) == _map.end()) {
edm4hep::MutableMCParticle edm_particle = convert(_p);
_map.insert({_p->id(), edm_particle});
Expand Down
Loading