Skip to content

Restrict datahandle #319

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

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_

find_package(ROOT COMPONENTS RIO Tree REQUIRED)
find_package(Gaudi REQUIRED)
find_package(podio 1.2.99 REQUIRED)
find_package(podio 1.3 REQUIRED)
find_package(EDM4HEP 0.99 REQUIRED)
find_package(fmt REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions k4FWCore/components/EventHeaderCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class EventHeaderCreator : public Gaudi::Algorithm {
"Event number offset, eventNumber will be filled with 'event_index + eventNumberOffset'"};

// datahandle for the EventHeader
mutable DataHandle<edm4hep::EventHeaderCollection> m_headerCol{edm4hep::labels::EventHeader,
Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::EventHeaderCollection> m_headerCol{edm4hep::labels::EventHeader,
Gaudi::DataHandle::Writer, this};
};

#endif
31 changes: 17 additions & 14 deletions k4FWCore/include/k4FWCore/DataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef K4FWCORE_DATAHANDLE_H
#define K4FWCORE_DATAHANDLE_H

#include <podio/utilities/TypeHelpers.h>

#include "k4FWCore/DataWrapper.h"
#include "k4FWCore/PodioDataSvc.h"

Expand All @@ -28,16 +30,13 @@
#include <stdexcept>
#include <type_traits>

namespace k4FWCore {
/**
* Specialisation of the Gaudi DataHandle
* for use with podio collections.
*/
template <typename T>
template <podio::CollectionType T>
class DataHandle : public DataObjectHandle<DataWrapper<T>> {
public:
friend class Algorithm;
friend class AlgTool;

public:
DataHandle();
~DataHandle() override;
Expand Down Expand Up @@ -70,7 +69,7 @@ class DataHandle : public DataObjectHandle<DataWrapper<T>> {
T* m_dataPtr;
};

template <typename T>
template <podio::CollectionType T>
DataHandle<T>::~DataHandle() {
// release memory allocated for primitive types (see comments in ctor)
if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>) {
Expand All @@ -79,11 +78,11 @@ DataHandle<T>::~DataHandle() {
}

//---------------------------------------------------------------------------
template <typename T>
template <podio::CollectionType T>
DataHandle<T>::DataHandle(DataObjID& descriptor, Gaudi::DataHandle::Mode a, IDataHandleHolder* fatherAlg)
: DataObjectHandle<DataWrapper<T>>(descriptor, a, fatherAlg), m_eds("EventDataSvc", "DataHandle") {}

template <typename T>
template <podio::CollectionType T>
DataHandle<T>::DataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a, IDataHandleHolder* fatherAlg)
: DataObjectHandle<DataWrapper<T>>(descriptor, a, fatherAlg), m_eds("EventDataSvc", "DataHandle") {
if (a == Gaudi::DataHandle::Writer) {
Expand All @@ -105,7 +104,7 @@ DataHandle<T>::DataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode
* If this is not the first time we cast and the cast worked, just use the
* static cast: we do not need the checks of the dynamic cast for every access!
*/
template <typename T>
template <podio::CollectionType T>
const T* DataHandle<T>::get() {
DataObject* dataObjectp;
auto sc = m_eds->retrieveObject(DataObjectHandle<DataWrapper<T>>::fullKey().key(), dataObjectp);
Expand All @@ -132,7 +131,7 @@ const T* DataHandle<T>::get() {
}

//---------------------------------------------------------------------------
template <typename T>
template <podio::CollectionType T>
void DataHandle<T>::put(T* objectp) {
std::unique_ptr<DataWrapper<T>> dw = std::make_unique<DataWrapper<T>>();
// in case T is of primitive type, we must not change the pointer address
Expand All @@ -147,7 +146,7 @@ void DataHandle<T>::put(T* objectp) {
}

//---------------------------------------------------------------------------
template <typename T>
template <podio::CollectionType T>
T* DataHandle<T>::put(std::unique_ptr<T> objectp) {
put(objectp.get());
return objectp.release();
Expand All @@ -159,19 +158,23 @@ T* DataHandle<T>::put(std::unique_ptr<T> objectp) {
* pointer to the data. Call this function if you create a collection and
* want to save it.
*/
template <typename T>
template <podio::CollectionType T>
T* DataHandle<T>::createAndPut() {
T* objectp = new T();
this->put(objectp);
return objectp;
}
} // namespace k4FWCore

template <podio::CollectionType T>
using DataHandle [[deprecated("Use k4FWCore::DataHandle instead")]] = k4FWCore::DataHandle<T>;

// temporary to allow property declaration
namespace Gaudi {
template <class T>
class Property<::DataHandle<T>&> : public ::DataHandleProperty {
class Property<k4FWCore::DataHandle<T>&> : public ::DataHandleProperty {
public:
Property(const std::string& name, ::DataHandle<T>& value) : ::DataHandleProperty(name, value) {}
Property(const std::string& name, k4FWCore::DataHandle<T>& value) : ::DataHandleProperty(name, value) {}
};
} // namespace Gaudi

Expand Down
10 changes: 7 additions & 3 deletions k4FWCore/include/k4FWCore/DataWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
#include <type_traits>

#include "GaudiKernel/DataObject.h"

#include "podio/CollectionBase.h"
#include "podio/utilities/TypeHelpers.h"

// forward declaration
template <typename T>
namespace k4FWCore {
template <podio::CollectionType T>
class DataHandle;
}

class GAUDI_API DataWrapperBase : public DataObject {
public:
Expand All @@ -39,8 +43,8 @@ class GAUDI_API DataWrapperBase : public DataObject {
template <class T>
class GAUDI_API DataWrapper : public DataWrapperBase {
public:
template <class T2>
friend class DataHandle;
template <podio::CollectionType T2>
friend class k4FWCore::DataHandle;

public:
DataWrapper() : m_data(nullptr) {}
Expand Down
6 changes: 6 additions & 0 deletions k4FWCore/include/k4FWCore/MetaDataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "k4FWCore/MetadataUtils.h"
#include "k4FWCore/PodioDataSvc.h"

namespace k4FWCore {

template <typename T>
class MetaDataHandle {
public:
Expand Down Expand Up @@ -164,5 +166,9 @@ void MetaDataHandle<T>::checkPodioDataSvc() {
std::cout << "Warning: MetaDataHandles require the PodioDataSvc or for compatibility the MetadataSvc" << std::endl;
}
}
} // namespace k4FWCore

template <typename T>
using MetaDataHandle [[deprecated("Use k4FWCore::MetaDataHandle instead")]] = k4FWCore::MetaDataHandle<T>;

#endif
4 changes: 3 additions & 1 deletion k4FWCore/include/k4FWCore/PodioDataSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#include "k4FWCore/DataWrapper.h"
class DataWrapperBase;
class PodioOutput;
namespace k4FWCore {
template <typename T>
class MetaDataHandle;
}

/** @class PodioEvtSvc EvtDataSvc.h
*
Expand All @@ -42,7 +44,7 @@ class MetaDataHandle;
*/
class PodioDataSvc : public DataSvc {
template <typename T>
friend class MetaDataHandle;
friend class k4FWCore::MetaDataHandle;
friend class PodioOutput;
friend class Lcio2EDM4hepTool;

Expand Down
19 changes: 8 additions & 11 deletions test/k4FWCoreTest/src/components/k4FWCoreTest_AlgorithmWithTFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@

#include "podio/UserDataCollection.h"

// datamodel
namespace edm4hep {
class MCParticleCollection;
class SimTrackerHitCollection;
class SimCaloHit;
} // namespace edm4hep
#include "edm4hep/MCParticleCollection.h"
#include "edm4hep/SimTrackerHitCollection.h"

/** @class k4FWCoreTest_AlgorithmWithTFile
* Test producer to check that data can still be written to
Expand Down Expand Up @@ -62,13 +58,14 @@ class k4FWCoreTest_AlgorithmWithTFile : public Gaudi::Algorithm {
Gaudi::Property<int> m_magicNumberOffset{this, "magicNumberOffset", 0,
"Integer to add to the dummy values written to the edm"};
/// Handle for the genparticles to be written
mutable DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles", Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles",
Gaudi::DataHandle::Writer, this};
/// Handle for the genvertices to be written
mutable DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitHandle{"SimTrackerHit", Gaudi::DataHandle::Writer,
this};
mutable k4FWCore::DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitHandle{"SimTrackerHit",
Gaudi::DataHandle::Writer, this};

mutable DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat", Gaudi::DataHandle::Writer,
this};
mutable k4FWCore::DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat",
Gaudi::DataHandle::Writer, this};

/// for testing: write a second TFile by user in an algorithm
mutable Float_t m_value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

#include "podio/UserDataCollection.h"

// datamodel
namespace edm4hep {
class MCParticleCollection;
} // namespace edm4hep
#include "edm4hep/MCParticleCollection.h"

class k4FWCoreTest_CheckExampleEventData : public Gaudi::Algorithm {
public:
Expand All @@ -47,9 +44,10 @@ class k4FWCoreTest_CheckExampleEventData : public Gaudi::Algorithm {
Gaudi::Property<bool> m_keepEventNumberZero{this, "keepEventNumberZero", false,
"Don't add the event number to the dummy values written"};
/// Handle for the MCParticles to be written
mutable DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles", Gaudi::DataHandle::Reader, this};
mutable DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat", Gaudi::DataHandle::Reader,
this};
mutable k4FWCore::DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles",
Gaudi::DataHandle::Reader, this};
mutable k4FWCore::DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat",
Gaudi::DataHandle::Reader, this};

mutable int m_event{0};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ class k4FWCoreTest_CreateExampleEventData : public Gaudi::Algorithm {
/// integer to add to the dummy values written to the edm
Gaudi::Property<int> m_magicNumberOffset{this, "magicNumberOffset", 0,
"Integer to add to the dummy values written to the edm"};
mutable DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles", Gaudi::DataHandle::Writer, this};
mutable DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitHandle{"SimTrackerHits",
Gaudi::DataHandle::Writer, this};
mutable DataHandle<edm4hep::TrackerHit3DCollection> m_TrackerHitHandle{"TrackerHits", Gaudi::DataHandle::Writer,
this};
mutable k4FWCore::DataHandle<edm4hep::MCParticleCollection> m_mcParticleHandle{"MCParticles",
Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitHandle{"SimTrackerHits",
Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::TrackerHit3DCollection> m_TrackerHitHandle{"TrackerHits",
Gaudi::DataHandle::Writer, this};

mutable DataHandle<edm4hep::TrackCollection> m_trackHandle{"Tracks", Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::TrackCollection> m_trackHandle{"Tracks", Gaudi::DataHandle::Writer, this};

mutable DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat", Gaudi::DataHandle::Writer,
this};
mutable DataHandle<edm4hep::ReconstructedParticleCollection> m_recoHandle{"RecoParticles", Gaudi::DataHandle::Writer,
this};
mutable DataHandle<edm4hep::RecoMCParticleLinkCollection> m_linkHandle{"Links", Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<podio::UserDataCollection<float>> m_vectorFloatHandle{"VectorFloat",
Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::ReconstructedParticleCollection> m_recoHandle{"RecoParticles",
Gaudi::DataHandle::Writer, this};
mutable k4FWCore::DataHandle<edm4hep::RecoMCParticleLinkCollection> m_linkHandle{"Links", Gaudi::DataHandle::Writer,
this};

mutable int m_event{0};
};
Expand Down
8 changes: 4 additions & 4 deletions test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class k4FWCoreTest_cellID_reader : public Gaudi::Algorithm {

private:
/// Handle for the SimTrackerHits to be read
mutable DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitReaderHandle{"SimTrackerHits",
Gaudi::DataHandle::Reader, this};
mutable MetaDataHandle<std::string> m_cellIDHandle{m_simTrackerHitReaderHandle, edm4hep::labels::CellIDEncoding,
Gaudi::DataHandle::Reader};
mutable k4FWCore::DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitReaderHandle{
"SimTrackerHits", Gaudi::DataHandle::Reader, this};
mutable k4FWCore::MetaDataHandle<std::string> m_cellIDHandle{
m_simTrackerHitReaderHandle, edm4hep::labels::CellIDEncoding, Gaudi::DataHandle::Reader};
};
#endif /* K4FWCORE_K4FWCORETEST_CELLID */
8 changes: 4 additions & 4 deletions test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class k4FWCoreTest_cellID_writer : public Gaudi::Algorithm {

private:
/// Handle for the SimTrackerHits to be written
mutable DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitWriterHandle{"SimTrackerHits",
Gaudi::DataHandle::Writer, this};
MetaDataHandle<std::string> m_cellIDHandle{m_simTrackerHitWriterHandle, edm4hep::labels::CellIDEncoding,
Gaudi::DataHandle::Writer};
mutable k4FWCore::DataHandle<edm4hep::SimTrackerHitCollection> m_simTrackerHitWriterHandle{
"SimTrackerHits", Gaudi::DataHandle::Writer, this};
k4FWCore::MetaDataHandle<std::string> m_cellIDHandle{m_simTrackerHitWriterHandle, edm4hep::labels::CellIDEncoding,
Gaudi::DataHandle::Writer};

// Some properties for the configuration metadata
Gaudi::Property<int> m_intProp{this, "intProp", 42, "An integer property"};
Expand Down
Loading