File indexing completed on 2025-01-18 10:17:33
0001
0002
0003
0004
0005 #pragma once
0006
0007 #include <JANA/JEvent.h>
0008
0009 namespace jana::components {
0010
0011
0012
0013 struct JHasOutputs {
0014 protected:
0015 struct OutputBase;
0016 std::vector<OutputBase*> m_outputs;
0017
0018 void RegisterOutput(OutputBase* output) {
0019 m_outputs.push_back(output);
0020 }
0021
0022 struct OutputBase {
0023 std::string type_name;
0024 std::vector<std::string> collection_names;
0025 bool is_variadic = false;
0026
0027 virtual void InsertCollection(JEvent& event) = 0;
0028 virtual void Reset() = 0;
0029 };
0030
0031 template <typename T>
0032 class Output : public OutputBase {
0033 std::vector<T*> m_data;
0034 bool is_not_owner = false;
0035
0036 public:
0037 Output(JHasOutputs* owner, std::string default_tag_name="") {
0038 owner->RegisterOutput(this);
0039 this->collection_names.push_back(default_tag_name);
0040 this->type_name = JTypeInfo::demangle<T>();
0041 }
0042
0043 std::vector<T*>& operator()() { return m_data; }
0044
0045 protected:
0046 void InsertCollection(JEvent& event) override {
0047 auto fac = event.Insert(m_data, this->collection_names[0]);
0048 fac->SetIsNotOwnerFlag(is_not_owner);
0049 }
0050 void Reset() override { }
0051
0052 void SetIsNotOwnerFlag(bool not_owner=true) { is_not_owner = not_owner; }
0053 };
0054
0055
0056 #if JANA2_HAVE_PODIO
0057 template <typename PodioT>
0058 class PodioOutput : public OutputBase {
0059
0060 std::unique_ptr<typename PodioT::collection_type> m_data;
0061
0062 public:
0063
0064 PodioOutput(JHasOutputs* owner, std::string default_collection_name="") {
0065 owner->RegisterOutput(this);
0066 this->collection_names.push_back(default_collection_name);
0067 this->type_name = JTypeInfo::demangle<PodioT>();
0068 }
0069
0070 std::unique_ptr<typename PodioT::collection_type>& operator()() { return m_data; }
0071
0072 protected:
0073 void InsertCollection(JEvent& event) override {
0074 event.InsertCollection<PodioT>(std::move(*m_data), this->collection_names[0]);
0075 }
0076 void Reset() override {
0077 m_data = std::move(std::make_unique<typename PodioT::collection_type>());
0078 }
0079 };
0080
0081
0082 template <typename PodioT>
0083 class VariadicPodioOutput : public OutputBase {
0084
0085 std::vector<std::unique_ptr<typename PodioT::collection_type>> m_data;
0086
0087 public:
0088
0089 VariadicPodioOutput(JHasOutputs* owner, std::vector<std::string> default_collection_names={}) {
0090 owner->RegisterOutput(this);
0091 this->collection_names = default_collection_names;
0092 this->type_name = JTypeInfo::demangle<PodioT>();
0093 this->is_variadic = true;
0094 }
0095
0096 std::vector<std::unique_ptr<typename PodioT::collection_type>>& operator()() { return m_data; }
0097
0098 protected:
0099 void InsertCollection(JEvent& event) override {
0100 if (m_data.size() != this->collection_names.size()) {
0101 throw JException("VariadicPodioOutput InsertCollection failed: Declared %d collections, but provided %d.", this->collection_names.size(), m_data.size());
0102
0103 }
0104 size_t i = 0;
0105 for (auto& coll_name : this->collection_names) {
0106 event.InsertCollection(std::move(*(m_data[i++])), coll_name);
0107 }
0108 }
0109
0110 void Reset() override {
0111 m_data.clear();
0112 for (auto& coll_name : this->collection_names) {
0113 m_data.push_back(std::make_unique<typename PodioT::collection_type>());
0114 }
0115 }
0116 };
0117 #endif
0118
0119 };
0120
0121
0122 }