Skip to content

Commit

Permalink
Deep terminology cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Sep 23, 2024
1 parent da316c7 commit 36ebc7e
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

#pragma once

#include <JANA/Components/JStorage.h>
#include <JANA/Components/JDataBundle.h>
#include <JANA/JObject.h>
#include <JANA/Utils/JTypeInfo.h>

#ifdef JANA2_HAVE_ROOT
#include <TObject.h>
#endif

class JBasicStorage : public JStorage {
class JBasicDataBundle : public JDataBundle {
bool m_is_persistent = false;
bool m_not_object_owner = false;
bool m_write_to_output = true;

public:
JBasicStorage() = default;
~JBasicStorage() override = default;
JBasicDataBundle() = default;
~JBasicDataBundle() override = default;
void SetPersistentFlag(bool persistent) { m_is_persistent = persistent; }
void SetNotOwnerFlag(bool not_owner) { m_not_object_owner = not_owner; }
void SetWriteToOutputFlag(bool write_to_output) { m_write_to_output = write_to_output; }
Expand All @@ -29,12 +29,12 @@ class JBasicStorage : public JStorage {


template <typename T>
class JBasicStorageT : public JBasicStorage {
class JBasicDataBundleT : public JBasicDataBundle {
private:
std::vector<T*> m_data;

public:
JBasicStorageT();
JBasicDataBundleT();
void ClearData() override;
size_t GetSize() const override { return m_data.size();}

Expand All @@ -55,7 +55,7 @@ class JBasicStorageT : public JBasicStorage {
// Template definitions

template <typename T>
JBasicStorageT<T>::JBasicStorageT() {
JBasicDataBundleT<T>::JBasicDataBundleT() {
SetTypeName(JTypeInfo::demangle<T>());
EnableGetAs<T>();
EnableGetAs<JObject>( std::is_convertible<T,JObject>() ); // Automatically add JObject if this can be converted to it
Expand All @@ -65,7 +65,7 @@ JBasicStorageT<T>::JBasicStorageT() {
}

template <typename T>
void JBasicStorageT<T>::ClearData() {
void JBasicDataBundleT<T>::ClearData() {

// ClearData won't do anything if Init() hasn't been called
if (GetStatus() == Status::Empty) {
Expand All @@ -87,7 +87,7 @@ void JBasicStorageT<T>::ClearData() {

template<typename T>
template<typename S>
void JBasicStorageT<T>::EnableGetAs() {
void JBasicDataBundleT<T>::EnableGetAs() {

auto upcast_lambda = [this]() {
std::vector<S*> results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

class JFactory;

class JStorage {
class JDataBundle {
public:
// Typedefs
enum class Status { Empty, Created, Inserted, InsertedViaGetObjects };

private:
// Fields
Status m_status = Status::Empty;
std::string m_collection_name;
JOptional<std::string> m_collection_tag;
std::string m_unique_name;
JOptional<std::string> m_short_name;
std::string m_type_name;
JFactory* m_factory = nullptr;
JOptional<std::type_index> m_inner_type_index;
Expand All @@ -37,28 +37,28 @@ class JStorage {

public:
// Interface
JStorage() = default;
virtual ~JStorage() = default;
JDataBundle() = default;
virtual ~JDataBundle() = default;
virtual size_t GetSize() const = 0;
virtual void ClearData() = 0;

// Getters
Status GetStatus() const { return m_status; }
std::string GetCollectionName() const { return m_collection_name; }
JOptional<std::string> GetCollectionTag() const { return m_collection_tag; }
std::string GetUniqueName() const { return m_unique_name; }
JOptional<std::string> GetShortName() const { return m_short_name; }
std::string GetTypeName() const { return m_type_name; }
JOptional<std::type_index> GetTypeIndex() const { return m_inner_type_index; }
JCallGraphRecorder::JDataOrigin GetInsertOrigin() const { return m_insert_origin; } ///< If objects were placed here by JEvent::Insert() this records whether that call was made from a source or factory.
JFactory* GetFactory() const { return m_factory; }

// Setters
void SetStatus(Status s) { m_status = s;}
void SetCollectionName(std::string collection_name) { m_collection_name = collection_name; }
void SetCollectionTag(std::string collection_tag) { m_collection_tag = collection_tag; }
void SetUniqueName(std::string unique_name) { m_unique_name = unique_name; }
void SetShortName(std::string short_name) { m_short_name = short_name; }
void SetTypeName(std::string type_name) { m_type_name = type_name; }
void SetInsertOrigin(JCallGraphRecorder::JDataOrigin origin) { m_insert_origin = origin; } ///< Called automatically by JEvent::Insert() to records whether that call was made by a source or factory.
void SetFactory(JFactory* fac) { m_factory = fac; }

// Templates
//
/// Generically access the encapsulated data, performing an upcast if necessary. This is useful for extracting data from
Expand Down Expand Up @@ -86,7 +86,7 @@ class JStorage {
// 3. The size of the vtable is expected to be very small (<10 elements, most likely 2)

template<typename S>
std::vector<S*> JStorage::GetAs() {
std::vector<S*> JDataBundle::GetAs() {
std::vector<S*> results;
auto ti = std::type_index(typeid(S));
auto search = mUpcastVTable.find(ti);
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/JANA/Components/JHasFactoryOutputs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#pragma once
#include <JANA/Components/JStorage.h>
#include <JANA/Components/JDataBundle.h>
#include <memory>

class JEvent;
Expand All @@ -12,11 +12,11 @@ class JHasFactoryOutputs {
public:
struct OutputBase {
protected:
std::vector<std::unique_ptr<JStorage>> m_collections;
std::vector<std::unique_ptr<JDataBundle>> m_databundles;
bool m_is_variadic = false;
public:
const std::vector<std::unique_ptr<JStorage>>& GetCollections() const { return m_collections;}
virtual void PutCollections(const JEvent& event) = 0;
const std::vector<std::unique_ptr<JDataBundle>>& GetDataBundles() const { return m_databundles; }
virtual void StoreData(const JEvent& event) = 0;
virtual void Reset() = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#pragma once

#include <JANA/Components/JStorage.h>
#include <JANA/Components/JDataBundle.h>
#include <podio/CollectionBase.h>
#include <podio/podioVersion.h>


class JPodioStorage : public JStorage {
class JPodioDataBundle : public JDataBundle {

private:
const podio::CollectionBase* m_collection = nullptr;
Expand All @@ -23,7 +23,7 @@ class JPodioStorage : public JStorage {

virtual void ClearData() override {
m_collection = nullptr;
SetStatus(JStorage::Status::Empty);
SetStatus(JDataBundle::Status::Empty);
// Podio clears the data itself when the frame is destroyed.
// Until then, the collection is immutable.
//
Expand Down
69 changes: 34 additions & 35 deletions src/libraries/JANA/Components/JPodioOutput.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <JANA/Components/JPodioStorage.h>
#include <JANA/Components/JPodioDataBundle.h>
#include <JANA/Utils/JTypeInfo.h>
#include <JANA/JEvent.h>
#include <JANA/Components/JHasFactoryOutputs.h>
Expand All @@ -13,29 +13,27 @@ namespace jana::components {
template <typename PodioT>
class PodioOutput : public JHasFactoryOutputs::OutputBase {
private:
std::unique_ptr<typename PodioT::collection_type> m_data;
JPodioStorage* m_podio_storage;
std::unique_ptr<typename PodioT::collection_type> m_transient_collection;
JPodioDataBundle* m_podio_databundle;
public:
PodioOutput(JHasFactoryOutputs* owner, std::string default_collection_name="") {
owner->RegisterOutput(this);
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>());
auto bundle = std::make_unique<JPodioDataBundle>();
bundle->SetUniqueName(default_collection_name);
bundle->SetTypeName(JTypeInfo::demangle<PodioT>());
m_podio_databundle = bundle.get();
m_databundles.push_back(std::move(bundle));
m_transient_collection = std::move(std::make_unique<typename PodioT::collection_type>());
}

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

const JStorage* GetCollection() const { return m_podio_storage; }
JPodioDataBundle* GetDataBundle() const { return m_podio_databundle; }


protected:
//void CreateCollections() override {
//}

void PutCollections(const JEvent& event) override {
void StoreData(const JEvent& event) override {
podio::Frame* frame;
try {
frame = const_cast<podio::Frame*>(event.GetSingle<podio::Frame>());
Expand All @@ -49,39 +47,40 @@ class PodioOutput : public JHasFactoryOutputs::OutputBase {
event.Insert<podio::Frame>(frame);
}

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_podio_storage->SetCollection(moved);
frame->put(std::move(m_transient_collection), m_podio_databundle->GetUniqueName());
const auto* moved = &frame->template get<typename PodioT::collection_type>(m_podio_databundle->GetUniqueName());
m_transient_collection = nullptr;
m_podio_databundle->SetCollection(moved);
}
void Reset() override {
m_data = std::move(std::make_unique<typename PodioT::collection_type>());
m_transient_collection = std::move(std::make_unique<typename PodioT::collection_type>());
}
};


template <typename PodioT>
class VariadicPodioOutput : public JHasFactoryOutputs::OutputBase {
private:
std::vector<std::unique_ptr<typename PodioT::collection_type>> m_data;
std::vector<std::unique_ptr<typename PodioT::collection_type>> m_collections;
std::vector<JPodioDataBundle*> m_databundles;

public:
VariadicPodioOutput(JHasFactoryOutputs* owner, std::vector<std::string> default_collection_names={}) {
owner->RegisterOutput(this);
this->m_is_variadic = true;
for (const std::string& name : default_collection_names) {
auto coll = std::make_unique<JPodioStorage>();
coll->SetCollectionName(name);
auto coll = std::make_unique<JPodioDataBundle>();
coll->SetUniqueName(name);
coll->SetTypeName(JTypeInfo::demangle<PodioT>());
m_collections.push_back(std::move(coll));
}
for (auto& coll_name : this->collection_names) {
m_data.push_back(std::make_unique<typename PodioT::collection_type>());
m_collections.push_back(std::make_unique<typename PodioT::collection_type>());
}
}
void PutCollections(const JEvent& event) override {
if (m_data.size() != this->collection_names.size()) {
throw JException("VariadicPodioOutput InsertCollection failed: Declared %d collections, but provided %d.", this->collection_names.size(), m_data.size());
void StoreData(const JEvent& event) override {
if (m_collections.size() != this->collection_names.size()) {
throw JException("VariadicPodioOutput InsertCollection failed: Declared %d collections, but provided %d.", this->collection_names.size(), m_collections.size());
}

podio::Frame* frame;
Expand All @@ -98,22 +97,22 @@ class VariadicPodioOutput : public JHasFactoryOutputs::OutputBase {
}

size_t i = 0;
for (auto& datum : m_data) {
frame->put(std::move(std::move(datum)), m_collections[i]->GetCollectionName());
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.SetCollection(moved);
for (auto& collection : m_collections) {
frame->put(std::move(std::move(collection)), m_databundles[i]->GetUniqueName());
const auto* moved = &frame->template get<typename PodioT::collection_type>(m_databundles[i]->GetUniqueName());
collection = nullptr;
const auto &databundle = dynamic_cast<JPodioDataBundle*>(m_databundles[i]);
databundle->SetCollection(moved);
i += 1;
}
}
void Reset() override {
m_data.clear();
for (auto& coll : this->m_collections) {
m_collections.clear();
for (auto& coll : this->m_databundles) {
coll->ClearData();
}
for (auto& coll_name : this->collection_names) {
m_data.push_back(std::make_unique<typename PodioT::collection_type>());
m_collections.push_back(std::make_unique<typename PodioT::collection_type>());
}
}
};
Expand Down
Loading

0 comments on commit 36ebc7e

Please sign in to comment.