From a3507958471a86d154ae166c935710fc4dc59f7f Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 17 Nov 2023 11:56:01 +0100 Subject: [PATCH] Merge PodioDataSvc into k4DataSvc - Move k4DataSvc to core library - Make PodioDataSvc an alias for now --- k4FWCore/CMakeLists.txt | 8 +- k4FWCore/components/PodioInput.h | 4 +- k4FWCore/components/PodioOutput.h | 4 +- k4FWCore/components/k4DataSvc.cpp | 13 +- k4FWCore/components/k4DataSvc.h | 33 ----- k4FWCore/include/k4FWCore/PodioDataSvc.h | 118 +--------------- k4FWCore/include/k4FWCore/k4DataSvc.h | 133 ++++++++++++++++++ .../src/{PodioDataSvc.cpp => k4DataSvc.cpp} | 47 ++++--- 8 files changed, 171 insertions(+), 189 deletions(-) delete mode 100644 k4FWCore/components/k4DataSvc.h create mode 100644 k4FWCore/include/k4FWCore/k4DataSvc.h rename k4FWCore/src/{PodioDataSvc.cpp => k4DataSvc.cpp} (81%) diff --git a/k4FWCore/CMakeLists.txt b/k4FWCore/CMakeLists.txt index 6a33b8f6..038b670d 100644 --- a/k4FWCore/CMakeLists.txt +++ b/k4FWCore/CMakeLists.txt @@ -29,10 +29,10 @@ gaudi_install(PYTHON) gaudi_add_library(k4FWCore - SOURCES src/PodioDataSvc.cpp - src/KeepDropSwitch.cpp - LINK Gaudi::GaudiKernel podio::podioRootIO ROOT::Core ROOT::RIO ROOT::Tree - ) + SOURCES src/PodioDataSvc.cpp + src/KeepDropSwitch.cpp + LINK Gaudi::GaudiKernel podio::podioRootIO ROOT::Core ROOT::RIO ROOT::Tree +) target_include_directories(k4FWCore PUBLIC $ $) diff --git a/k4FWCore/components/PodioInput.h b/k4FWCore/components/PodioInput.h index db70b69f..0fb2a1fb 100644 --- a/k4FWCore/components/PodioInput.h +++ b/k4FWCore/components/PodioInput.h @@ -26,7 +26,7 @@ #include #include -class PodioDataSvc; +class k4DataSvc; /** @class PodioInput * @@ -51,7 +51,7 @@ class PodioInput final : public Gaudi::Functional::Consumer Gaudi::Property> m_collectionNames{ this, "collections", {}, "Collections that should be read (default all)"}; // Data service: needed to register objects and get collection IDs. Just an observing pointer. - PodioDataSvc* m_podioDataSvc; + k4DataSvc* m_podioDataSvc; mutable std::map> m_readers; }; diff --git a/k4FWCore/components/PodioOutput.h b/k4FWCore/components/PodioOutput.h index 510ef297..1ece7089 100644 --- a/k4FWCore/components/PodioOutput.h +++ b/k4FWCore/components/PodioOutput.h @@ -35,7 +35,7 @@ namespace podio { #include // forward declarations -class PodioDataSvc; +class k4DataSvc; class PodioOutput : public Gaudi::Algorithm { public: @@ -62,7 +62,7 @@ class PodioOutput : public Gaudi::Algorithm { "An optional file path to copy the outputfile to."}; /// Switch for keeping or dropping outputs KeepDropSwitch m_switch; - PodioDataSvc* m_podioDataSvc; + k4DataSvc* m_podioDataSvc; /// The actual ROOT frame writer std::unique_ptr m_framewriter; /// The stored collections diff --git a/k4FWCore/components/k4DataSvc.cpp b/k4FWCore/components/k4DataSvc.cpp index e7c7bf0d..d7b1a63c 100644 --- a/k4FWCore/components/k4DataSvc.cpp +++ b/k4FWCore/components/k4DataSvc.cpp @@ -16,19 +16,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "k4DataSvc.h" +#include "k4FWCore/k4DataSvc.h" // Instantiation of a static factory class used by clients to create // instances of this service DECLARE_COMPONENT(k4DataSvc) - -/// Standard Constructor -k4DataSvc::k4DataSvc(const std::string& name, ISvcLocator* svc) : PodioDataSvc(name, svc) { - declareProperty("inputs", m_filenames = {}, "Names of the files to read"); - declareProperty("input", m_filename = "", "Name of the file to read"); - declareProperty("FirstEventEntry", m_1stEvtEntry = 0, "First event to read"); -} - -/// Standard Destructor -k4DataSvc::~k4DataSvc() {} diff --git a/k4FWCore/components/k4DataSvc.h b/k4FWCore/components/k4DataSvc.h deleted file mode 100644 index b8f89943..00000000 --- a/k4FWCore/components/k4DataSvc.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2014-2024 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 K4FWCORE_K4DATASVC_H -#define K4FWCORE_K4DATASVC_H - -#include "k4FWCore/PodioDataSvc.h" - -class k4DataSvc : public PodioDataSvc { -public: - /// Standard Constructor - k4DataSvc(const std::string& name, ISvcLocator* svc); - - /// Standard Destructor - virtual ~k4DataSvc(); -}; -#endif diff --git a/k4FWCore/include/k4FWCore/PodioDataSvc.h b/k4FWCore/include/k4FWCore/PodioDataSvc.h index 7ce04986..05eda0e2 100644 --- a/k4FWCore/include/k4FWCore/PodioDataSvc.h +++ b/k4FWCore/include/k4FWCore/PodioDataSvc.h @@ -16,119 +16,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef FWCORE_PODIODATASVC_H -#define FWCORE_PODIODATASVC_H +#ifndef K4FWCORE_PODIODATASVC_H +#define K4FWCORE_PODIODATASVC_H -#include "GaudiKernel/DataSvc.h" -#include "GaudiKernel/IConversionSvc.h" -// PODIO -#include -#include "podio/CollectionBase.h" -#include "podio/CollectionIDTable.h" -#include "podio/Frame.h" -#include "podio/podioVersion.h" -#if PODIO_BUILD_VERSION >= PODIO_VERSION(0, 99, 0) -#include "podio/ROOTReader.h" -#else -#include "podio/ROOTFrameReader.h" -namespace podio { - using ROOTReader = podio::ROOTFrameReader; -} -#endif -// Forward declarations -#include "k4FWCore/DataWrapper.h" -class DataWrapperBase; -class PodioOutput; -template class MetaDataHandle; - -/** @class PodioEvtSvc EvtDataSvc.h - * - * An EvtDataSvc for PODIO classes - * - * @author B. Hegner - */ -class PodioDataSvc : public DataSvc { - template friend class MetaDataHandle; - friend class PodioOutput; - friend class Lcio2EDM4hepTool; - -public: - typedef std::vector> CollRegistry; - - StatusCode initialize() final; - StatusCode reinitialize() final; - StatusCode finalize() final; - StatusCode clearStore() final; - StatusCode i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) final; - StatusCode i_setRoot(std::string root_path, DataObject* pRootObj) final; - - /// Standard Constructor - PodioDataSvc(const std::string& name, ISvcLocator* svc); - - /// Standard Destructor - virtual ~PodioDataSvc(); +#include "k4FWCore/k4DataSvc.h" - // Use DataSvc functionality except where we override - using DataSvc::registerObject; - /// Overriding standard behaviour of evt service - /// Register object with the data store. - virtual StatusCode registerObject(std::string_view parentPath, std::string_view fullPath, - DataObject* pObject) override final; +using PodioDataSvc [[deprecated("Use k4DataSvc instead")]] = k4DataSvc; - const std::string_view getCollectionType(const std::string& collName); - - template StatusCode readCollection(const std::string& collName) { - DataObject* objectPtr = nullptr; - if (DataSvc::findObject("/Event", "/" + collName, objectPtr)) { - debug() << "Collection " << collName << " already read, not reading it again" << endmsg; - return StatusCode::SUCCESS; - } - const T* collection(nullptr); - collection = static_cast(m_eventframe.get(collName)); - if (collection == nullptr) { - error() << "Collection " << collName << " does not exist." << endmsg; - } - auto wrapper = new DataWrapper; - wrapper->setData(collection); - m_podio_datawrappers.push_back(wrapper); - return DataSvc::registerObject("/Event", "/" + collName, wrapper); - } - - const podio::Frame& getEventFrame() const { return m_eventframe; } - - /// Resets caches of reader and event store, increases event counter - void endOfRead(); - - /// TODO: Make this private again after conversions have been properly solved - podio::Frame& getMetaDataFrame() { return m_metadataframe; } - -private: - /// PODIO reader for ROOT files - podio::ROOTReader m_reader; - /// PODIO Frame, used to initialise collections - podio::Frame m_eventframe; - /// PODIO Frame, used to store metadata - podio::Frame m_metadataframe; - /// Counter of the event number - int m_eventNum{0}; - /// Number of events in the file / to process - int m_numAvailableEvents{-1}; - int m_requestedEventMax{-1}; - /// Whether reading from file at all - bool m_reading_from_file{false}; - - SmartIF m_cnvSvc; - - // Registry of data wrappers; needed for memory management - std::vector m_podio_datawrappers; - -protected: - /// ROOT file name the input is read from. Set by option filename - std::vector m_filenames; - std::string m_filename; - /// Jump to nth events at the beginning. Set by option FirstEventEntry - /// This option is helpful when we want to debug an event in the middle of a file - unsigned m_1stEvtEntry{0}; - bool m_bounds_check_needed{true}; -}; -#endif // CORE_PODIODATASVC_H +#endif diff --git a/k4FWCore/include/k4FWCore/k4DataSvc.h b/k4FWCore/include/k4FWCore/k4DataSvc.h new file mode 100644 index 00000000..737a6569 --- /dev/null +++ b/k4FWCore/include/k4FWCore/k4DataSvc.h @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2014-2024 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 K4FWCORE_PODIODATASVC_H +#define K4FWCORE_PODIODATASVC_H + +#include "GaudiKernel/DataSvc.h" +#include "GaudiKernel/IConversionSvc.h" +// PODIO +#include +#include "podio/CollectionBase.h" +#include "podio/Frame.h" +#include "podio/podioVersion.h" +#if PODIO_BUILD_VERSION >= PODIO_VERSION(0, 99, 0) +#include "podio/ROOTReader.h" +#else +#include "podio/ROOTFrameReader.h" +namespace podio { + using ROOTReader = podio::ROOTFrameReader; +} +#endif +// Forward declarations +#include "k4FWCore/DataWrapper.h" +class DataWrapperBase; +class PodioOutput; +template class MetaDataHandle; + +/** @class k4DataSvc + * + * An EvtDataSvc for PODIO classes + * + * @author B. Hegner + */ +class k4DataSvc : public DataSvc { + template friend class MetaDataHandle; + friend class PodioOutput; + friend class Lcio2EDM4hepTool; + +public: + typedef std::vector> CollRegistry; + + StatusCode initialize() final; + StatusCode reinitialize() final; + StatusCode finalize() final; + StatusCode clearStore() final; + StatusCode i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) final; + StatusCode i_setRoot(std::string root_path, DataObject* pRootObj) final; + + /// Standard Constructor + k4DataSvc(const std::string& name, ISvcLocator* svc); + + /// Standard Destructor + virtual ~k4DataSvc(); + + // Use DataSvc functionality except where we override + using DataSvc::registerObject; + /// Overriding standard behaviour of evt service + /// Register object with the data store. + virtual StatusCode registerObject(std::string_view parentPath, std::string_view fullPath, + DataObject* pObject) override final; + + const std::string_view getCollectionType(const std::string& collName); + + template StatusCode readCollection(const std::string& collName) { + DataObject* objectPtr = nullptr; + if (DataSvc::findObject("/Event", "/" + collName, objectPtr)) { + debug() << "Collection " << collName << " already read, not reading it again" << endmsg; + return StatusCode::SUCCESS; + } + const T* collection(nullptr); + collection = static_cast(m_eventframe.get(collName)); + if (collection == nullptr) { + error() << "Collection " << collName << " does not exist." << endmsg; + } + auto wrapper = new DataWrapper; + wrapper->setData(collection); + m_podio_datawrappers.push_back(wrapper); + return DataSvc::registerObject("/Event", "/" + collName, wrapper); + } + + const podio::Frame& getEventFrame() const { return m_eventframe; } + + /// Resets caches of reader and event store, increases event counter + void endOfRead(); + + /// TODO: Make this private again after conversions have been properly solved + podio::Frame& getMetaDataFrame() { return m_metadataframe; } + +private: + /// PODIO reader for ROOT files + podio::ROOTReader m_reader; + /// PODIO Frame, used to initialise collections + podio::Frame m_eventframe; + /// PODIO Frame, used to store metadata + podio::Frame m_metadataframe; + /// Counter of the event number + int m_eventNum{0}; + /// Number of events in the file / to process + int m_numAvailableEvents{-1}; + int m_requestedEventMax{-1}; + /// Whether reading from file at all + bool m_reading_from_file{false}; + + SmartIF m_cnvSvc; + + // Registry of data wrappers; needed for memory management + std::vector m_podio_datawrappers; + +protected: + /// ROOT file name the input is read from. Set by option filename + std::vector m_filenames; + std::string m_filename; + /// Jump to nth events at the beginning. Set by option FirstEventEntry + /// This option is helpful when we want to debug an event in the middle of a file + unsigned m_1stEvtEntry{0}; + bool m_bounds_check_needed{true}; +}; +#endif // CORE_PODIODATASVC_H diff --git a/k4FWCore/src/PodioDataSvc.cpp b/k4FWCore/src/k4DataSvc.cpp similarity index 81% rename from k4FWCore/src/PodioDataSvc.cpp rename to k4FWCore/src/k4DataSvc.cpp index d318d531..3ca08ee2 100644 --- a/k4FWCore/src/PodioDataSvc.cpp +++ b/k4FWCore/src/k4DataSvc.cpp @@ -16,20 +16,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "k4FWCore/PodioDataSvc.h" -#include -#include "GaudiKernel/IConversionSvc.h" -#include "GaudiKernel/IEventProcessor.h" -#include "GaudiKernel/IProperty.h" -#include "GaudiKernel/ISvcLocator.h" + +#include "k4FWCore/k4DataSvc.h" #include "k4FWCore/DataWrapper.h" -#include "podio/CollectionBase.h" +#include +#include +#include +#include +#include -#include "TTree.h" +/// Standard Constructor +k4DataSvc::k4DataSvc(const std::string& name, ISvcLocator* svc) : DataSvc(name, svc) { + declareProperty("inputs", m_filenames = {}, "Names of the files to read"); + declareProperty("input", m_filename = "", "Name of the file to read"); + declareProperty("FirstEventEntry", m_1stEvtEntry = 0, "First event to read"); +} /// Service initialisation -StatusCode PodioDataSvc::initialize() { +StatusCode k4DataSvc::initialize() { // Nothing to do: just call base class initialisation StatusCode status = DataSvc::initialize(); ISvcLocator* svc_loc = serviceLocator(); @@ -80,19 +85,21 @@ StatusCode PodioDataSvc::initialize() { return status; } + /// Service reinitialisation -StatusCode PodioDataSvc::reinitialize() { +StatusCode k4DataSvc::reinitialize() { // Do nothing for this service return StatusCode::SUCCESS; } + /// Service finalization -StatusCode PodioDataSvc::finalize() { +StatusCode k4DataSvc::finalize() { m_cnvSvc = 0; // release DataSvc::finalize().ignore(); return StatusCode::SUCCESS; } -StatusCode PodioDataSvc::clearStore() { +StatusCode k4DataSvc::clearStore() { // as the frame takes care of the ownership of the podio::Collections, // make sure the DataWrappers don't cause a double delete for (auto wrapper : m_podio_datawrappers) { @@ -104,7 +111,7 @@ StatusCode PodioDataSvc::clearStore() { return StatusCode::SUCCESS; } -StatusCode PodioDataSvc::i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) { +StatusCode k4DataSvc::i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) { // create a new frame if (m_reading_from_file) { m_eventframe = podio::Frame(m_reader.readEntry("events", m_eventNum + m_1stEvtEntry)); @@ -114,7 +121,7 @@ StatusCode PodioDataSvc::i_setRoot(std::string root_path, IOpaqueAddress* pRootA return DataSvc::i_setRoot(root_path, pRootAddr); } -StatusCode PodioDataSvc::i_setRoot(std::string root_path, DataObject* pRootObj) { +StatusCode k4DataSvc::i_setRoot(std::string root_path, DataObject* pRootObj) { // create a new frame if (m_reading_from_file) { m_eventframe = podio::Frame(m_reader.readEntry("events", m_eventNum + m_1stEvtEntry)); @@ -124,7 +131,7 @@ StatusCode PodioDataSvc::i_setRoot(std::string root_path, DataObject* pRootObj) return DataSvc::i_setRoot(root_path, pRootObj); } -void PodioDataSvc::endOfRead() { +void k4DataSvc::endOfRead() { m_eventNum++; if (!m_bounds_check_needed) { @@ -143,13 +150,7 @@ void PodioDataSvc::endOfRead() { // todo: figure out sthg to do with sc (added to silence -Wunused-result) } -/// Standard Constructor -PodioDataSvc::PodioDataSvc(const std::string& name, ISvcLocator* svc) : DataSvc(name, svc) {} - -/// Standard Destructor -PodioDataSvc::~PodioDataSvc() {} - -const std::string_view PodioDataSvc::getCollectionType(const std::string& collName) { +const std::string_view k4DataSvc::getCollectionType(const std::string& collName) { const auto coll = m_eventframe.get(collName); if (coll == nullptr) { error() << "Collection " << collName << " does not exist." << endmsg; @@ -158,7 +159,7 @@ const std::string_view PodioDataSvc::getCollectionType(const std::string& collNa return coll->getTypeName(); } -StatusCode PodioDataSvc::registerObject(std::string_view parentPath, std::string_view fullPath, DataObject* pObject) { +StatusCode k4DataSvc::registerObject(std::string_view parentPath, std::string_view fullPath, DataObject* pObject) { DataWrapperBase* wrapper = dynamic_cast(pObject); if (wrapper != nullptr) { podio::CollectionBase* coll = wrapper->collectionBase();