Skip to content

Commit

Permalink
services/io/podio: rely on ::collection_type feature from recent PODIO (
Browse files Browse the repository at this point in the history
#1108)

This takes advantage of AIDASoft/podio#465 to
start deprecating one of the two responsibilities of the
`make_datamodel_glue.py`. A similar change to JANA2 will be done
independently. I suggest to keep the `#if` pragmas for now, in case we
want to bisect possible issues with the latest PODIO.
  • Loading branch information
veprbl authored Nov 19, 2023
1 parent fdc79ac commit 9f247b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
21 changes: 13 additions & 8 deletions src/services/io/podio/JFactoryPodioT.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <JANA/JFactoryT.h>
#include <JANA/JEvent.h>
#include <podio/podioVersion.h>
#include <podio/Frame.h>
#include "datamodel_glue.h"

Expand All @@ -16,7 +17,11 @@ namespace eicrecon {
template <typename T>
class JFactoryPodioT : public JFactoryT<T>, public JFactoryPodio {
public:
#if podio_VERSION >= PODIO_VERSION(0, 17, 0)
using CollectionT = typename T::collection_type;
#else
using CollectionT = typename PodioTypeMap<T>::collection_t;
#endif
private:
// mCollection is owned by the frame.
// mFrame is owned by the JFactoryT<podio::Frame>.
Expand Down Expand Up @@ -65,7 +70,7 @@ JFactoryPodioT<T>::~JFactoryPodioT() {
}

template <typename T>
void JFactoryPodioT<T>::SetCollection(typename PodioTypeMap<T>::collection_t&& collection) {
void JFactoryPodioT<T>::SetCollection(CollectionT&& collection) {
/// Provide a PODIO collection. Note that PODIO assumes ownership of this collection, and the
/// collection pointer should be assumed to be invalid after this call

Expand All @@ -85,15 +90,15 @@ void JFactoryPodioT<T>::SetCollection(typename PodioTypeMap<T>::collection_t&& c


template <typename T>
void JFactoryPodioT<T>::SetCollection(std::unique_ptr<typename PodioTypeMap<T>::collection_t> collection) {
void JFactoryPodioT<T>::SetCollection(std::unique_ptr<CollectionT> collection) {
/// Provide a PODIO collection. Note that PODIO assumes ownership of this collection, and the
/// collection pointer should be assumed to be invalid after this call

if (this->mFrame == nullptr) {
throw JException("JFactoryPodioT: Unable to add collection to frame as frame is missing!");
}
this->mFrame->put(std::move(collection), this->GetTag());
const auto* moved = &this->mFrame->template get<typename PodioTypeMap<T>::collection_t>(this->GetTag());
const auto* moved = &this->mFrame->template get<CollectionT>(this->GetTag());
this->mCollection = moved;

for (const T& item : *moved) {
Expand Down Expand Up @@ -177,12 +182,12 @@ void JFactoryPodioT<T>::Create(const std::shared_ptr<const JEvent>& event) {
}
catch(...) {
if (mCollection == nullptr) {
SetCollection(typename PodioTypeMap<T>::collection_t());
SetCollection(CollectionT());
}
throw;
}
if (mCollection == nullptr) {
SetCollection(typename PodioTypeMap<T>::collection_t());
SetCollection(CollectionT());
// If calling Process() didn't result in a call to Set() or SetCollection(), we create an empty collection
// so that podio::ROOTFrameWriter doesn't segfault on the null mCollection pointer
}
Expand All @@ -193,7 +198,7 @@ void JFactoryPodioT<T>::Create(const std::shared_ptr<const JEvent>& event) {

template <typename T>
void JFactoryPodioT<T>::Set(const std::vector<T*>& aData) {
typename PodioTypeMap<T>::collection_t collection;
CollectionT collection;
if (mIsSubsetCollection) collection.setSubsetCollection(true);
for (T* item : aData) {
collection.push_back(*item);
Expand All @@ -203,7 +208,7 @@ void JFactoryPodioT<T>::Set(const std::vector<T*>& aData) {

template <typename T>
void JFactoryPodioT<T>::Set(std::vector<T*>&& aData) {
typename PodioTypeMap<T>::collection_t collection;
CollectionT collection;
if (mIsSubsetCollection) collection.setSubsetCollection(true);
for (T* item : aData) {
collection.push_back(*item);
Expand All @@ -213,7 +218,7 @@ void JFactoryPodioT<T>::Set(std::vector<T*>&& aData) {

template <typename T>
void JFactoryPodioT<T>::Insert(T* aDatum) {
typename PodioTypeMap<T>::collection_t collection;
CollectionT collection;
if (mIsSubsetCollection) collection->setSubsetCollection(true);
collection->push_back(*aDatum);
SetCollection(std::move(collection));
Expand Down
10 changes: 9 additions & 1 deletion src/services/io/podio/make_datamodel_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def AddCollections(datamodelName, collectionfiles):
type_map.append(' class ' + basename + 'Collection;')
type_map.append(' class Mutable' + basename + ';')
type_map.append('};')
type_map.append('#if podio_VERSION < PODIO_VERSION(0, 17, 0)')
type_map.append('template <> struct PodioTypeMap<' + datamodelName + '::' + basename + '> {')
type_map.append(' using collection_t = ' + datamodelName + '::' + basename + 'Collection;')
type_map.append(' using mutable_t = ' + datamodelName + '::Mutable' + basename + ';')
type_map.append('};')
type_map.append('#endif')

visitor.append(' if (podio_typename == "' + datamodelName + '::' + basename + 'Collection") {')
visitor.append(' return visitor(*reinterpret_cast<const ' + datamodelName + '::' + basename + 'Collection*>(&collection));')
Expand Down Expand Up @@ -112,10 +114,16 @@ def AddCollections(datamodelName, collectionfiles):
f.write('#pragma once\n')
f.write('\n')
f.write('#include <stdexcept>\n')
f.write('#include <podio/podioVersion.h>\n')
f.write('#include <podio/CollectionBase.h>\n')
f.write('\n')

f.write('\ntemplate <typename T> struct PodioTypeMap {};')
f.write('\ntemplate <typename T> struct PodioTypeMap {')
f.write('\n#if podio_VERSION >= PODIO_VERSION(0, 17, 0)')
f.write('\n using collection_t = typename T::collection_type;')
f.write('\n using mutable_t = typename T::mutable_type;')
f.write('\n#endif')
f.write('\n};')
f.write('\n\n')
f.write('\n'.join(type_map))
f.write('\n')
Expand Down

0 comments on commit 9f247b3

Please sign in to comment.