Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:27

0001 // Copyright 2023, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 // Created by Nathan Brei
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 
0035     public:
0036         Output(JHasOutputs* owner, std::string default_tag_name="") {
0037             owner->RegisterOutput(this);
0038             this->collection_names.push_back(default_tag_name);
0039             this->type_name = JTypeInfo::demangle<T>();
0040         }
0041 
0042         std::vector<T*>& operator()() { return m_data; }
0043 
0044     protected:
0045         void InsertCollection(JEvent& event) override {
0046             event.Insert(m_data, this->collection_names[0]);
0047         }
0048         void Reset() override { }
0049     };
0050 
0051 
0052 #if JANA2_HAVE_PODIO
0053     template <typename PodioT>
0054     class PodioOutput : public OutputBase {
0055 
0056         std::unique_ptr<typename PodioT::collection_type> m_data;
0057 
0058     public:
0059 
0060         PodioOutput(JHasOutputs* owner, std::string default_collection_name="") {
0061             owner->RegisterOutput(this);
0062             this->collection_names.push_back(default_collection_name);
0063             this->type_name = JTypeInfo::demangle<PodioT>();
0064         }
0065 
0066         std::unique_ptr<typename PodioT::collection_type>& operator()() { return m_data; }
0067 
0068     protected:
0069         void InsertCollection(JEvent& event) override {
0070             event.InsertCollection<PodioT>(std::move(*m_data), this->collection_names[0]);
0071         }
0072         void Reset() override {
0073             m_data = std::move(std::make_unique<typename PodioT::collection_type>());
0074         }
0075     };
0076 
0077 
0078     template <typename PodioT>
0079     class VariadicPodioOutput : public OutputBase {
0080 
0081         std::vector<std::unique_ptr<typename PodioT::collection_type>> m_data;
0082 
0083     public:
0084 
0085         VariadicPodioOutput(JHasOutputs* owner, std::vector<std::string> default_collection_names={}) {
0086             owner->RegisterOutput(this);
0087             this->collection_names = default_collection_names;
0088             this->type_name = JTypeInfo::demangle<PodioT>();
0089             this->is_variadic = true;
0090         }
0091 
0092         std::vector<std::unique_ptr<typename PodioT::collection_type>>& operator()() { return m_data; }
0093 
0094     protected:
0095         void InsertCollection(JEvent& event) override {
0096             if (m_data.size() != this->collection_names.size()) {
0097                 throw JException("VariadicPodioOutput InsertCollection failed: Declared %d collections, but provided %d.", this->collection_names.size(), m_data.size());
0098                 // Otherwise this leads to a PODIO segfault
0099             }
0100             size_t i = 0;
0101             for (auto& coll_name : this->collection_names) {
0102                 event.InsertCollection(std::move(*(m_data[i++])), coll_name);
0103             }
0104         }
0105 
0106         void Reset() override {
0107             m_data.clear();
0108             for (auto& coll_name : this->collection_names) {
0109                 m_data.push_back(std::make_unique<typename PodioT::collection_type>());
0110             }
0111         }
0112     };
0113 #endif
0114 
0115 };
0116 
0117 
0118 } // namespace jana::components