Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-03 08:40:33

0001 // Copyright 2024, 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 #include <JANA/JEvent.h>
0007 
0008 
0009 namespace jana::components {
0010 
0011 struct JHasInputs {
0012 protected:
0013 
0014     struct InputBase;
0015     std::vector<InputBase*> m_inputs;
0016 
0017     void RegisterInput(InputBase* input) {
0018         m_inputs.push_back(input);
0019     }
0020     
0021     struct InputOptions {
0022         std::string name {""};
0023         JEventLevel level {JEventLevel::None};
0024         bool is_optional {false};
0025         // bool is_shortcircuiting {false};
0026         // bool contains_single_item {false};
0027     };
0028 
0029     struct VariadicInputOptions {
0030         std::vector<std::string> names {""};
0031         JEventLevel level {JEventLevel::None};
0032         bool is_optional {false};
0033         // bool is_shortcircuiting {false};
0034         // bool contains_single_item {false};
0035     };
0036 
0037     struct InputBase {
0038         std::string type_name;
0039         std::vector<std::string> names;
0040         JEventLevel level = JEventLevel::None;
0041         bool is_variadic = false;
0042         bool is_optional = false;
0043         //bool is_shortcircuiting = false;
0044         //bool contains_single_item = false;
0045 
0046         void SetOptional(bool isOptional) {
0047             this->is_optional = isOptional;
0048         }
0049 
0050         void SetLevel(JEventLevel level) {
0051             this->level = level;
0052         }
0053 
0054         void Configure(const InputOptions& options) {
0055             this->names.clear();
0056             this->names.push_back(options.name);
0057             this->level = options.level;
0058             this->is_optional = options.is_optional;
0059             // this->is_shortcircuiting = options.is_shortcircuiting;
0060             // this->contains_single_item = options.contains_single_item;
0061         }
0062 
0063         void ConfigureVariadic(const VariadicInputOptions& options) {
0064             if (!is_variadic) { throw JException("Setting variadic options on non-variadic input"); }
0065             this->names = options.names;
0066             this->level = options.level;
0067             this->is_optional = options.is_optional;
0068             // this->is_shortcircuiting = options.is_shortcircuiting;
0069             // this->contains_single_item = options.contains_single_item;
0070         }
0071 
0072         virtual void GetCollection(const JEvent& event) = 0;
0073         virtual void PrefetchCollection(const JEvent& event) = 0;
0074     };
0075 
0076     template <typename T>
0077     class Input : public InputBase {
0078 
0079         std::vector<const T*> m_data;
0080 
0081     public:
0082 
0083         Input(JHasInputs* owner) {
0084             owner->RegisterInput(this);
0085             this->type_name = JTypeInfo::demangle<T>();
0086             this->names.push_back("");
0087             // For non-PODIO inputs, these are technically tags for now, not names
0088             this->level = JEventLevel::None;
0089         }
0090 
0091         Input(JHasInputs* owner, const InputOptions& options) {
0092             owner->RegisterInput(this);
0093             this->type_name = JTypeInfo::demangle<T>();
0094             Configure(options);
0095         }
0096 
0097         void SetTag(std::string tag) {
0098             this->names.clear();
0099             this->names.push_back(tag);
0100         }
0101 
0102         const std::vector<const T*>& operator()() { return m_data; }
0103         const std::vector<const T*>& operator*() { return m_data; }
0104         const std::vector<const T*>* operator->() { return &m_data; }
0105 
0106 
0107     private:
0108         friend class JComponentT;
0109 
0110         void GetCollection(const JEvent& event) {
0111             auto& level = this->level;
0112             m_data.clear();
0113             if (level == event.GetLevel() || level == JEventLevel::None) {
0114                 event.Get<T>(m_data, this->names[0], !this->is_optional);
0115             }
0116             else {
0117                 if (this->is_optional && !event.HasParent(level)) return;
0118                 event.GetParent(level).template Get<T>(m_data, this->names[0], !this->is_optional);
0119             }
0120         }
0121         void PrefetchCollection(const JEvent& event) {
0122             auto& level = this->level;
0123             auto& name = this->names[0];
0124             if (level == event.GetLevel() || level == JEventLevel::None) {
0125                 event.GetFactory<T>(name, !this->is_optional)->Create(event.shared_from_this());
0126             }
0127             else {
0128                 if (this->is_optional && !event.HasParent(level)) return;
0129                 event.GetParent(level).template GetFactory<T>(name, !this->is_optional)->Create(event.shared_from_this());
0130             }
0131         }
0132     };
0133 
0134 #if JANA2_HAVE_PODIO
0135     template <typename PodioT>
0136     class PodioInput : public InputBase {
0137 
0138         const typename PodioT::collection_type* m_data;
0139 
0140     public:
0141 
0142         PodioInput(JHasInputs* owner) {
0143             owner->RegisterInput(this);
0144             this->type_name = JTypeInfo::demangle<PodioT>();
0145             this->names.push_back(this->type_name);
0146             this->level = JEventLevel::None;
0147         }
0148 
0149         PodioInput(JHasInputs* owner, const InputOptions& options) {
0150             owner->RegisterInput(this);
0151             this->type_name = JTypeInfo::demangle<PodioT>();
0152             Configure(options);
0153         }
0154 
0155         const typename PodioT::collection_type* operator()() {
0156             return m_data;
0157         }
0158         const typename PodioT::collection_type& operator*() {
0159             return *m_data;
0160         }
0161         const typename PodioT::collection_type* operator->() {
0162             return m_data;
0163         }
0164 
0165         void SetCollectionName(std::string name) {
0166             this->names.clear();
0167             this->names.push_back(name);
0168         }
0169 
0170         void SetTag(std::string tag) {
0171             this->names.clear();
0172             this->names.push_back(type_name + ":" + tag);
0173         }
0174 
0175         void GetCollection(const JEvent& event) {
0176             auto& level = this->level;
0177             auto& name = this->names[0];
0178             if (level == event.GetLevel() || level == JEventLevel::None) {
0179                 m_data = event.GetCollection<PodioT>(name, !this->is_optional);
0180             }
0181             else {
0182                 if (this->is_optional && !event.HasParent(level)) return;
0183                 m_data = event.GetParent(level).template GetCollection<PodioT>(name, !this->is_optional);
0184             }
0185         }
0186 
0187         void PrefetchCollection(const JEvent& event) {
0188             auto& level = this->level;
0189             auto& name = this->names[0];
0190             if (level == event.GetLevel() || level == JEventLevel::None) {
0191                 event.GetCollection<PodioT>(name, !this->is_optional);
0192             }
0193             else {
0194                 if (this->is_optional && !event.HasParent(level)) return;
0195                 event.GetParent(level).template GetCollection<PodioT>(name, !this->is_optional);
0196             }
0197         }
0198     };
0199 
0200 
0201     template <typename PodioT>
0202     class VariadicPodioInput : public InputBase {
0203 
0204         std::vector<const typename PodioT::collection_type*> m_data;
0205 
0206     public:
0207 
0208         VariadicPodioInput(JHasInputs* owner) {
0209             owner->RegisterInput(this);
0210             this->type_name = JTypeInfo::demangle<PodioT>();
0211             this->is_variadic = true;
0212         }
0213 
0214         VariadicPodioInput(JHasInputs* owner, const VariadicInputOptions& options) {
0215             owner->RegisterInput(this);
0216             this->type_name = JTypeInfo::demangle<PodioT>();
0217             this->is_variadic = true;
0218             ConfigureVariadic(options);
0219         }
0220 
0221         const std::vector<const typename PodioT::collection_type*> operator()() {
0222             return m_data;
0223         }
0224 
0225         void SetCollectionNames(std::vector<std::string> names) {
0226             this->names = std::move(names);
0227         }
0228 
0229         void GetCollection(const JEvent& event) {
0230             m_data.clear();
0231             for (auto& coll_name : names) {
0232                 if (level == event.GetLevel() || level == JEventLevel::None) {
0233                     m_data.push_back(event.GetCollection<PodioT>(coll_name, !this->is_optional));
0234                 }
0235                 else {
0236                     if (this->is_optional && !event.HasParent(level)) return;
0237                     m_data.push_back(event.GetParent(level).template GetCollection<PodioT>(coll_name, !this->is_optional));
0238                 }
0239             }
0240         }
0241 
0242         void PrefetchCollection(const JEvent& event) {
0243             for (auto& coll_name : names) {
0244                 if (level == event.GetLevel() || level == JEventLevel::None) {
0245                     event.GetCollection<PodioT>(coll_name, !this->is_optional);
0246                 }
0247                 else {
0248                     if (this->is_optional && !event.HasParent(level)) return;
0249                     event.GetParent(level).template GetCollection<PodioT>(coll_name, !this->is_optional);
0250                 }
0251             }
0252         }
0253     };
0254 #endif
0255 };
0256 
0257 } // namespace jana::components
0258