diff --git a/k4ProjectTemplate/options/createExampleEventData.py b/k4ProjectTemplate/options/createExampleEventData.py index e742d07..ced4f05 100644 --- a/k4ProjectTemplate/options/createExampleEventData.py +++ b/k4ProjectTemplate/options/createExampleEventData.py @@ -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, + ) diff --git a/k4ProjectTemplate/options/eventcounter.py b/k4ProjectTemplate/options/eventcounter.py deleted file mode 100644 index cbc7ec3..0000000 --- a/k4ProjectTemplate/options/eventcounter.py +++ /dev/null @@ -1,36 +0,0 @@ -# -# Copyright (c) 2020-2023 Key4hep-Project. -# -# This file is part of Key4hep. -# See https://key4hep.github.io/key4hep-doc/ for further info. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from Gaudi.Configuration import * - -from Configurables import k4DataSvc -podioevent = k4DataSvc("EventDataSvc") - -from Configurables import EventCounterExample -eventcounter = EventCounterExample() - - -from Configurables import ApplicationMgr -ApplicationMgr( TopAlg=[eventcounter], - EvtSel="NONE", - EvtMax=100, - ExtSvc=[podioevent], - OutputLevel=DEBUG, - ) - - diff --git a/k4ProjectTemplate/options/readExampleEventData.py b/k4ProjectTemplate/options/readExampleEventData.py index 9875758..4296e26 100644 --- a/k4ProjectTemplate/options/readExampleEventData.py +++ b/k4ProjectTemplate/options/readExampleEventData.py @@ -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, diff --git a/k4ProjectTemplate/src/components/CreateExampleEventData.cpp b/k4ProjectTemplate/src/components/CreateExampleEventData.cpp index 4b5a402..7052739 100644 --- a/k4ProjectTemplate/src/components/CreateExampleEventData.cpp +++ b/k4ProjectTemplate/src/components/CreateExampleEventData.cpp @@ -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 - 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 { + 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 m_exampleEnergy{this, "ExampleEnergy", 3, "Example int to be produced"}; +}; -StatusCode CreateExampleEventData::finalize() { return GaudiAlgorithm::finalize(); } +DECLARE_COMPONENT(CreateExampleEventData) diff --git a/k4ProjectTemplate/src/components/CreateExampleEventData.h b/k4ProjectTemplate/src/components/CreateExampleEventData.h deleted file mode 100644 index cd46c84..0000000 --- a/k4ProjectTemplate/src/components/CreateExampleEventData.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2020-2023 Key4hep-Project. - * - * This file is part of Key4hep. - * See https://key4hep.github.io/key4hep-doc/ for further info. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef TESTFWCORE_CREATEEXAMPLEEVENTDATA -#define TESTFWCORE_CREATEEXAMPLEEVENTDATA - -// GAUDI -#include "GaudiAlg/GaudiAlgorithm.h" - -#include "podio/UserDataCollection.h" - -// edm4hep -#include "TTree.h" -#include "k4FWCore/DataHandle.h" - -// datamodel -namespace edm4hep { - class MCParticleCollection; - class SimTrackerHitCollection; - class SimCaloHit; -} // namespace edm4hep - -/** @class CreateExampleEventData - * Lightweight producer for edm data for tests that do not depend on the actual - * data content and therefore do not need the simulation machinery. - * Fills data members with increasing integers, together with some offset so that different - * events can be easily distinguished. - * - */ -class CreateExampleEventData : public GaudiAlgorithm { -public: - explicit CreateExampleEventData(const std::string&, ISvcLocator*); - virtual ~CreateExampleEventData(); - /** Initialize. - * @return status code - */ - virtual StatusCode initialize() final; - /** Execute. - * @return status code - */ - virtual StatusCode execute() final; - /** Finalize. - * @return status code - */ - virtual StatusCode finalize() final; - -private: - /// integer to add to the dummy values written to the edm - Gaudi::Property m_magicNumberOffset{this, "magicNumberOffset", 0, - "Integer to add to the dummy values written to the edm"}; - /// Handle for the genparticles to be written - DataHandle m_mcParticleHandle{"MCParticles", Gaudi::DataHandle::Writer, this}; - /// Handle for the genvertices to be written - DataHandle m_simTrackerHitHandle{"SimTrackerHit", Gaudi::DataHandle::Writer, this}; - - DataHandle> m_vectorFloatHandle{"VectorFloat", Gaudi::DataHandle::Writer, this}; -}; -#endif /* TESTFWCORE_CREATEEXAMPLEEVENTDATA */ diff --git a/k4ProjectTemplate/src/components/EmptyAlg.h b/k4ProjectTemplate/src/components/ExampleConsumer.cpp similarity index 59% rename from k4ProjectTemplate/src/components/EmptyAlg.h rename to k4ProjectTemplate/src/components/ExampleConsumer.cpp index 61b3bd0..b3367bb 100644 --- a/k4ProjectTemplate/src/components/EmptyAlg.h +++ b/k4ProjectTemplate/src/components/ExampleConsumer.cpp @@ -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 + +struct ExampleConsumer final : Gaudi::Functional::Consumer { + 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) diff --git a/k4ProjectTemplate/src/components/EmptyAlg.cpp b/k4ProjectTemplate/src/components/ExampleTransformer.cpp similarity index 52% rename from k4ProjectTemplate/src/components/EmptyAlg.cpp rename to k4ProjectTemplate/src/components/ExampleTransformer.cpp index 9527a68..5654b43 100644 --- a/k4ProjectTemplate/src/components/EmptyAlg.cpp +++ b/k4ProjectTemplate/src/components/ExampleTransformer.cpp @@ -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 -StatusCode EmptyAlg::initialize() { return StatusCode::SUCCESS; } +struct ExampleTransformer final : Gaudi::Functional::Transformer { + 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) diff --git a/k4ProjectTemplate/src/components/HelloWorldAlg.cpp b/k4ProjectTemplate/src/components/HelloWorldAlg.cpp index d4948df..dbefa66 100644 --- a/k4ProjectTemplate/src/components/HelloWorldAlg.cpp +++ b/k4ProjectTemplate/src/components/HelloWorldAlg.cpp @@ -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; @@ -41,4 +41,4 @@ StatusCode HelloWorldAlg::execute() { return StatusCode::SUCCESS; } -StatusCode HelloWorldAlg::finalize() { return GaudiAlgorithm::finalize(); } +StatusCode HelloWorldAlg::finalize() { return Gaudi::Algorithm::finalize(); } diff --git a/k4ProjectTemplate/src/components/HelloWorldAlg.h b/k4ProjectTemplate/src/components/HelloWorldAlg.h index c18a51b..e9ffe82 100644 --- a/k4ProjectTemplate/src/components/HelloWorldAlg.h +++ b/k4ProjectTemplate/src/components/HelloWorldAlg.h @@ -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 theMessage{this, "PerEventPrintMessage", "Hello ", "The message to printed for each Event"}; }; - -#endif /* TESTFWCORE_HELLOWORLDALG */