Skip to content

Commit

Permalink
Remove templating from JPodioStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Sep 23, 2024
1 parent 4b5c0a7 commit da316c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 63 deletions.
25 changes: 13 additions & 12 deletions src/libraries/JANA/Components/JPodioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <JANA/Utils/JTypeInfo.h>
#include <JANA/JEvent.h>
#include <JANA/Components/JHasFactoryOutputs.h>
#include <podio/Frame.h>
#include <memory>


Expand All @@ -13,21 +14,21 @@ template <typename PodioT>
class PodioOutput : public JHasFactoryOutputs::OutputBase {
private:
std::unique_ptr<typename PodioT::collection_type> m_data;
JPodioStorage* m_collection;
JPodioStorage* m_podio_storage;
public:
PodioOutput(JHasFactoryOutputs* owner, std::string default_collection_name="") {
owner->RegisterOutput(this);
auto coll = std::make_unique<JPodioStorage>();
coll->SetCollectionName(default_collection_name);
coll->SetTypeName(JTypeInfo::demangle<PodioT>());
m_collection = coll.get();
m_collections.push_back(std::move(coll));
auto storage = std::make_unique<JPodioStorage>();
storage->SetCollectionName(default_collection_name);
storage->SetTypeName(JTypeInfo::demangle<PodioT>());
m_podio_storage = storage.get();
m_collections.push_back(std::move(storage));
m_data = std::move(std::make_unique<typename PodioT::collection_type>());
}

std::unique_ptr<typename PodioT::collection_type>& operator()() { return m_data; }

const JStorage* GetCollection() const { return m_collection; }
const JStorage* GetCollection() const { return m_podio_storage; }


protected:
Expand All @@ -48,11 +49,10 @@ class PodioOutput : public JHasFactoryOutputs::OutputBase {
event.Insert<podio::Frame>(frame);
}

frame->put(std::move(m_data), m_collection->GetCollectionName());
const auto* moved = &frame->template get<typename PodioT::collection_type>(m_collection->GetCollectionName());
frame->put(std::move(m_data), m_podio_storage->GetCollectionName());
const auto* moved = &frame->template get<typename PodioT::collection_type>(m_podio_storage->GetCollectionName());
m_data = nullptr;
m_collection->SetCollectionAlreadyInFrame<PodioT>(moved);
//m_collection->SetFrame(frame); // We might not need this!
m_podio_storage->SetCollection(moved);
}
void Reset() override {
m_data = std::move(std::make_unique<typename PodioT::collection_type>());
Expand Down Expand Up @@ -103,7 +103,8 @@ class VariadicPodioOutput : public JHasFactoryOutputs::OutputBase {
const auto* moved = &frame->template get<typename PodioT::collection_type>(m_collections[i]->GetCollectionName());
datum = nullptr;
const auto &coll = dynamic_cast<JPodioStorage>(m_collections[i]);
coll.SetCollectionAlreadyInFrame<PodioT>(moved);
coll.SetCollection(moved);
i += 1;
}
}
void Reset() override {
Expand Down
50 changes: 2 additions & 48 deletions src/libraries/JANA/Components/JPodioStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@

#pragma once

#include "JANA/Utils/JTypeInfo.h"
#include <JANA/Components/JStorage.h>
#include <podio/CollectionBase.h>
#include <podio/podioVersion.h>
#include <podio/Frame.h>


class JPodioStorage : public JStorage {
class JPodioStorage : public JStorage {

private:
const podio::CollectionBase* m_collection = nullptr;
podio::Frame* m_frame = nullptr;

public:
size_t GetSize() const override {
Expand All @@ -26,7 +23,6 @@ class JPodioStorage : public JStorage {

virtual void ClearData() override {
m_collection = nullptr;
m_frame = nullptr;
SetStatus(JStorage::Status::Empty);
// Podio clears the data itself when the frame is destroyed.
// Until then, the collection is immutable.
Expand All @@ -39,50 +35,8 @@ class JPodioStorage : public JStorage {
// it might also prevent the user from accessing frames directly.
}

// Getters
const podio::CollectionBase* GetCollection() const { return m_collection; }

template <typename T> const typename T::collection_type* GetCollection();


// Setters
void SetFrame(podio::Frame* frame) { m_frame = frame; }

template <typename T>
void SetCollection(std::unique_ptr<typename T::collection_type> collection);

template <typename T>
void SetCollectionAlreadyInFrame(const typename T::collection_type* collection);
void SetCollection(const podio::CollectionBase* collection) { m_collection = collection; }
};

template <typename T>
const typename T::collection_type* JPodioStorage::GetCollection() {
assert(JTypeInfo::demangle<T>() == this->GetTypeName());
return dynamic_cast<const typename T::collection_type*>(m_collection);
}


template <typename T>
void JPodioStorage::SetCollection(std::unique_ptr<typename T::collection_type> 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->m_frame == nullptr) {
throw JException("JPodioStorage: Unable to add collection to frame as frame is missing!");
}
this->m_frame->put(std::move(collection), this->GetCollectionName());
const auto* moved = &this->m_frame->template get<typename T::collection_type>(this->GetCollectionName());
this->m_collection = moved;

this->SetTypeName(JTypeInfo::demangle<T>());
this->SetStatus(Status::Inserted);
}

template <typename T>
void JPodioStorage::SetCollectionAlreadyInFrame(const typename T::collection_type* collection) {
m_collection = collection;
this->SetTypeName(JTypeInfo::demangle<T>());
SetStatus(Status::Inserted);
}


12 changes: 9 additions & 3 deletions src/libraries/JANA/JEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#if JANA2_HAVE_PODIO
#include <JANA/Components/JPodioStorage.h>
#include <podio/Frame.h>
#endif

class JApplication;
Expand Down Expand Up @@ -578,7 +579,12 @@ const typename T::collection_type* JEvent::GetCollection(std::string name, bool
throw JException("Not a podio collection: %s", name.c_str());
}
else {
return podio_coll->GetCollection<T>();
auto coll = podio_coll->GetCollection();
auto typed_coll = dynamic_cast<const typename T::collection_type*>(coll);
if (typed_coll == nullptr) {
throw JException("Unable to cast Podio collection to %s", JTypeInfo::demangle<typename T::collection_type>().c_str());
}
return typed_coll;
}
}
else if (throw_on_missing) {
Expand Down Expand Up @@ -637,7 +643,7 @@ JPodioStorage* JEvent::InsertCollectionAlreadyInFrame(const podio::CollectionBas
coll->SetTypeName(JTypeInfo::demangle<PodioT>());
coll->SetStatus(JStorage::Status::Inserted);
coll->SetInsertOrigin(mCallGraph.GetInsertDataOrigin());
coll->SetCollectionAlreadyInFrame<PodioT>(typed_collection);
coll->SetCollection(typed_collection);
mFactorySet->Add(coll);
return coll;
}
Expand All @@ -648,7 +654,7 @@ JPodioStorage* JEvent::InsertCollectionAlreadyInFrame(const podio::CollectionBas
throw JException("Collections can only be inserted once!");
}
auto typed_storage = dynamic_cast<JPodioStorage*>(storage);
typed_storage->SetCollectionAlreadyInFrame<PodioT>(typed_collection);
typed_storage->SetCollection(typed_collection);
typed_storage->SetStatus(JStorage::Status::Inserted);
typed_storage->SetInsertOrigin(mCallGraph.GetInsertDataOrigin());
return typed_storage;
Expand Down

0 comments on commit da316c7

Please sign in to comment.