Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 08:38:26

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         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         void SetTag(std::string tag) {
0044             this->collection_names.clear();
0045             this->collection_names.push_back(tag);
0046         }
0047 
0048         std::vector<T*>& operator()() { return m_data; }
0049 
0050     protected:
0051         void InsertCollection(JEvent& event) override {
0052             auto fac = event.Insert(m_data, this->collection_names[0]);
0053             fac->SetIsNotOwnerFlag(is_not_owner);
0054         }
0055         void Reset() override { }
0056 
0057         void SetIsNotOwnerFlag(bool not_owner=true) { is_not_owner = not_owner; }
0058     };
0059 
0060 
0061 #if JANA2_HAVE_PODIO
0062     template <typename PodioT>
0063     class PodioOutput : public OutputBase {
0064 
0065         std::unique_ptr<typename PodioT::collection_type> m_data;
0066 
0067     public:
0068 
0069         PodioOutput(JHasOutputs* owner, std::string default_collection_name="") {
0070             owner->RegisterOutput(this);
0071             this->collection_names.push_back(default_collection_name);
0072             this->type_name = JTypeInfo::demangle<PodioT>();
0073         }
0074 
0075         std::unique_ptr<typename PodioT::collection_type>& operator()() { return m_data; }
0076 
0077         void SetCollectionName(std::string name) {
0078             this->collection_names.clear();
0079             this->collection_names.push_back(name);
0080         }
0081 
0082     protected:
0083         void InsertCollection(JEvent& event) override {
0084             event.InsertCollection<PodioT>(std::move(*m_data), this->collection_names[0]);
0085         }
0086         void Reset() override {
0087             m_data = std::move(std::make_unique<typename PodioT::collection_type>());
0088         }
0089     };
0090 
0091 
0092     template <typename PodioT>
0093     class VariadicPodioOutput : public OutputBase {
0094 
0095         std::vector<std::unique_ptr<typename PodioT::collection_type>> m_data;
0096 
0097     public:
0098 
0099         VariadicPodioOutput(JHasOutputs* owner, std::vector<std::string> default_collection_names={}) {
0100             owner->RegisterOutput(this);
0101             this->collection_names = default_collection_names;
0102             this->type_name = JTypeInfo::demangle<PodioT>();
0103             this->is_variadic = true;
0104         }
0105 
0106         std::vector<std::unique_ptr<typename PodioT::collection_type>>& operator()() { return m_data; }
0107 
0108         void SetCollectionNames(std::vector<std::string> names) {
0109             this->collection_names = names;
0110         }
0111 
0112     protected:
0113         void InsertCollection(JEvent& event) override {
0114             if (m_data.size() != this->collection_names.size()) {
0115                 throw JException("VariadicPodioOutput InsertCollection failed: Declared %d collections, but provided %d.", this->collection_names.size(), m_data.size());
0116                 // Otherwise this leads to a PODIO segfault
0117             }
0118             size_t i = 0;
0119             for (auto& coll_name : this->collection_names) {
0120                 event.InsertCollection<PodioT>(std::move(*(m_data[i++])), coll_name);
0121             }
0122         }
0123 
0124         void Reset() override {
0125             m_data.clear();
0126             for (auto& coll_name : this->collection_names) {
0127                 m_data.push_back(std::make_unique<typename PodioT::collection_type>());
0128             }
0129         }
0130     };
0131 #endif
0132 
0133 };
0134 
0135 
0136 } // namespace jana::components