Skip to content

Commit

Permalink
Add templates of some functional algorithms (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell authored Oct 8, 2023
1 parent 39f2c79 commit d9f9a3b
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 210 deletions.
24 changes: 11 additions & 13 deletions k4ProjectTemplate/options/createExampleEventData.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from Gaudi.Configuration import *

from Gaudi.Configuration import INFO
from Configurables import CreateExampleEventData
from Configurables import PodioOutput
from Configurables import ApplicationMgr
from Configurables import k4DataSvc

podioevent = k4DataSvc("EventDataSvc")

from Configurables import CreateExampleEventData
producer = CreateExampleEventData()

from Configurables import PodioOutput
out = PodioOutput("out")
out.filename = "output_k4test_exampledata.root"
out.outputCommands = ["keep *"]

from Configurables import ApplicationMgr
ApplicationMgr( TopAlg=[producer, out],
EvtSel="NONE",
EvtMax=100,
ExtSvc=[podioevent],
OutputLevel=INFO,
)


ApplicationMgr(TopAlg=[producer, out],
EvtSel="NONE",
EvtMax=100,
ExtSvc=[podioevent],
OutputLevel=INFO,
)
36 changes: 0 additions & 36 deletions k4ProjectTemplate/options/eventcounter.py

This file was deleted.

12 changes: 6 additions & 6 deletions k4ProjectTemplate/options/readExampleEventData.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from Gaudi.Configuration import *

from Gaudi.Configuration import DEBUG
from Configurables import k4DataSvc
from Configurables import CreateExampleEventData
from Configurables import PodioInput
from Configurables import ApplicationMgr

podioevent = k4DataSvc("EventDataSvc")
podioevent.input = "output_k4test_exampledata.root"
from Configurables import CreateExampleEventData
producer = CreateExampleEventData()

from Configurables import PodioInput
inp = PodioInput("InputReader")
inp.collections = ["MCParticles", "SimTrackerHit"]
inp.collections = ["ExampleParticles"]

from Configurables import ApplicationMgr
ApplicationMgr( TopAlg=[inp],
EvtSel="NONE",
EvtMax=100,
Expand Down
57 changes: 19 additions & 38 deletions k4ProjectTemplate/src/components/CreateExampleEventData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CreateExampleEventData.h"

// datamodel
#include "edm4hep/MCParticleCollection.h"
#include "edm4hep/SimTrackerHitCollection.h"

DECLARE_COMPONENT(CreateExampleEventData)

CreateExampleEventData::CreateExampleEventData(const std::string& aName, ISvcLocator* aSvcLoc)
: GaudiAlgorithm(aName, aSvcLoc) {
declareProperty("mcparticles", m_mcParticleHandle, "Dummy Particle collection (output)");
declareProperty("trackhits", m_simTrackerHitHandle, "Dummy Hit collection (output)");
}

CreateExampleEventData::~CreateExampleEventData() {}
#include "Gaudi/Property.h"
#include "GaudiAlg/Producer.h"

StatusCode CreateExampleEventData::initialize() {
if (GaudiAlgorithm::initialize().isFailure()) {
return StatusCode::FAILURE;
}
return StatusCode::SUCCESS;
}

StatusCode CreateExampleEventData::execute() {
auto* floatVector = m_vectorFloatHandle.createAndPut();
floatVector->push_back(125.);
floatVector->push_back(25.);
// Define BaseClass_t
#include "k4FWCore/BaseClass.h"

edm4hep::MCParticleCollection* particles = m_mcParticleHandle.createAndPut();
#include "edm4hep/MCParticleCollection.h"

auto particle = particles->create();
#include <string>

auto& p4 = particle.momentum();
p4.x = m_magicNumberOffset + 5;
p4.y = m_magicNumberOffset + 6;
p4.z = m_magicNumberOffset + 7;
particle.setMass(m_magicNumberOffset + 8);
struct CreateExampleEventData final : Gaudi::Functional::Producer<edm4hep::MCParticleCollection(), BaseClass_t> {
CreateExampleEventData(const std::string& name, ISvcLocator* svcLoc)
: Producer(name, svcLoc, KeyValue("OutputLocation", "ExampleParticles")) {}

auto* hits = m_simTrackerHitHandle.createAndPut();
auto hit = hits->create();
hit.setPosition({3, 4, 5});
edm4hep::MCParticleCollection operator()() const override {
auto coll = edm4hep::MCParticleCollection();
auto particle = coll.create();
particle.setMass(m_exampleEnergy);
info() << "Producing a particle with mass = " << m_exampleEnergy << " GeV" << endmsg;
return coll;
}

return StatusCode::SUCCESS;
}
Gaudi::Property<double> m_exampleEnergy{this, "ExampleEnergy", 3, "Example int to be produced"};
};

StatusCode CreateExampleEventData::finalize() { return GaudiAlgorithm::finalize(); }
DECLARE_COMPONENT(CreateExampleEventData)
73 changes: 0 additions & 73 deletions k4ProjectTemplate/src/components/CreateExampleEventData.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

// GAUDI
#include "Gaudi/Property.h"
#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiAlg/Consumer.h"

class EmptyAlg : public GaudiAlgorithm {
public:
explicit EmptyAlg(const std::string&, ISvcLocator*);
virtual ~EmptyAlg();
/** Initialize.
* @return status code
*/
virtual StatusCode initialize() final;
/** Execute.
* @return status code
*/
virtual StatusCode execute() final;
/** Finalize.
* @return status code
*/
virtual StatusCode finalize() final;
// Define BaseClass_t
#include "k4FWCore/BaseClass.h"

private:
// member variable
int m_member = 0;
#include <string>

struct ExampleConsumer final : Gaudi::Functional::Consumer<void(const int&), BaseClass_t> {
ExampleConsumer(const std::string& name, ISvcLocator* svcLoc)
: Consumer(name, svcLoc, KeyValue("ExampleConsumerInputLocation", "/ExampleInt")) {}

void operator()(const int& input) const override { info() << "ExampleInt = " << input << endmsg; }
};

DECLARE_COMPONENT(ExampleConsumer)
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "EmptyAlg.h"

DECLARE_COMPONENT(EmptyAlg)
#include "Gaudi/Property.h"
#include "GaudiAlg/Transformer.h"

EmptyAlg::EmptyAlg(const std::string& aName, ISvcLocator* aSvcLoc) : GaudiAlgorithm(aName, aSvcLoc) {}
// Define BaseClass_t
#include "k4FWCore/BaseClass.h"

EmptyAlg::~EmptyAlg() {}
#include <string>

StatusCode EmptyAlg::initialize() { return StatusCode::SUCCESS; }
struct ExampleTransformer final : Gaudi::Functional::Transformer<int(const int&), BaseClass_t> {
ExampleTransformer(const std::string& name, ISvcLocator* svcLoc)
: Transformer(name, svcLoc, KeyValue("ExampleTransformerInputLocation", "/InputExampleInt"),
{KeyValue("ExampleTransformerOutputLocation", "/OutputExampleInt")}) {}

StatusCode EmptyAlg::execute() { return StatusCode::SUCCESS; }
int operator()(const int& input) const override {
info() << "ExampleInt = " << input << endmsg;
return input + 1;
}
};

StatusCode EmptyAlg::finalize() { return StatusCode::SUCCESS; }
DECLARE_COMPONENT(ExampleTransformer)
8 changes: 4 additions & 4 deletions k4ProjectTemplate/src/components/HelloWorldAlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@

DECLARE_COMPONENT(HelloWorldAlg)

HelloWorldAlg::HelloWorldAlg(const std::string& aName, ISvcLocator* aSvcLoc) : GaudiAlgorithm(aName, aSvcLoc) {}
HelloWorldAlg::HelloWorldAlg(const std::string& aName, ISvcLocator* aSvcLoc) : Gaudi::Algorithm(aName, aSvcLoc) {}

HelloWorldAlg::~HelloWorldAlg() {}

StatusCode HelloWorldAlg::initialize() {
if (GaudiAlgorithm::initialize().isFailure()) {
if (Gaudi::Algorithm::initialize().isFailure()) {
return StatusCode::FAILURE;
}
return StatusCode::SUCCESS;
}

StatusCode HelloWorldAlg::execute() {
StatusCode HelloWorldAlg::execute(const EventContext& ctx) const {
info() << endmsg;
info() << endmsg;
info() << theMessage << endmsg;
Expand All @@ -41,4 +41,4 @@ StatusCode HelloWorldAlg::execute() {
return StatusCode::SUCCESS;
}

StatusCode HelloWorldAlg::finalize() { return GaudiAlgorithm::finalize(); }
StatusCode HelloWorldAlg::finalize() { return Gaudi::Algorithm::finalize(); }
19 changes: 8 additions & 11 deletions k4ProjectTemplate/src/components/HelloWorldAlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TESTFWCORE_HELLOWORLDALG
#define TESTFWCORE_HELLOWORLDALG

#pragma once
// Note that in most cases it's better to use the functional
// approach than Gaudi::Algorithm

// GAUDI
#include "Gaudi/Algorithm.h"
#include "Gaudi/Property.h"
#include "GaudiAlg/GaudiAlgorithm.h"

class HelloWorldAlg : public GaudiAlgorithm {
class HelloWorldAlg : public Gaudi::Algorithm {
public:
explicit HelloWorldAlg(const std::string&, ISvcLocator*);
HelloWorldAlg(const std::string&, ISvcLocator*);
virtual ~HelloWorldAlg();
/** Initialize.
* @return status code
*/
virtual StatusCode initialize() final;
StatusCode initialize() override;
/** Execute.
* @return status code
*/
virtual StatusCode execute() final;
StatusCode execute(const EventContext&) const override;
/** Finalize.
* @return status code
*/
virtual StatusCode finalize() final;
StatusCode finalize() override;

private:
// member variable
Gaudi::Property<std::string> theMessage{this, "PerEventPrintMessage", "Hello ",
"The message to printed for each Event"};
};

#endif /* TESTFWCORE_HELLOWORLDALG */

0 comments on commit d9f9a3b

Please sign in to comment.