Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:22:16

0001 
0002 #pragma once
0003 #include <JANA/Components/JDatabundle.h>
0004 #include <JANA/Components/JComponentSummary.h>
0005 #include <JANA/Utils/JEventLevel.h>
0006 
0007 class JFactorySet;
0008 
0009 namespace jana::components {
0010 
0011 
0012 class JHasOutputs {
0013 public:
0014 
0015     class OutputBase {
0016     private:
0017         JDatabundle* m_databundle;
0018         JEventLevel m_level = JEventLevel::None; // By default we inherit this from the component that owns this
0019 
0020     public:
0021         virtual ~OutputBase() = default;
0022         JDatabundle* GetDatabundle() { return m_databundle; }
0023         JEventLevel GetLevel() const { return m_level; }
0024 
0025         void SetDatabundle(JDatabundle* databundle) { m_databundle = databundle; }
0026         void SetLevel(JEventLevel level) { m_level = level; }
0027         void SetShortName(std::string short_name) { m_databundle->SetShortName(short_name); }
0028         void SetUniqueName(std::string unique_name) { m_databundle->SetUniqueName(unique_name); }
0029 
0030         virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0031         virtual void EulerianStore(JFactorySet&) {}
0032 
0033         void ClearData() { m_databundle->ClearData(); }
0034     };
0035 
0036 
0037     class VariadicOutputBase {
0038     private:
0039         std::vector<JDatabundle*> m_databundles;
0040         JEventLevel m_level = JEventLevel::PhysicsEvent;
0041 
0042     public:
0043         virtual ~VariadicOutputBase() = default;
0044         std::vector<JDatabundle*>& GetDatabundles() { return m_databundles; }
0045         JEventLevel GetLevel() const { return m_level; }
0046 
0047         void SetLevel(JEventLevel level) { m_level = level; }
0048         virtual void SetShortNames(std::vector<std::string>) {}
0049         virtual void SetUniqueNames(std::vector<std::string>) {}
0050 
0051         std::vector<std::string> GetUniqueNames() {
0052             std::vector<std::string> results;
0053             for (auto* databundle: m_databundles) {
0054                 results.push_back(databundle->GetUniqueName());
0055             }
0056             return results;
0057         };
0058 
0059         virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0060         virtual void EulerianStore(JFactorySet&) {}
0061 
0062         void ClearData() {
0063             for (auto* databundle : m_databundles) {
0064                 databundle->ClearData();
0065             }
0066         }
0067     };
0068 
0069 private:
0070     std::vector<OutputBase*> m_outputs;;
0071     std::vector<VariadicOutputBase*> m_variadic_outputs;
0072     std::vector<std::pair<OutputBase*, VariadicOutputBase*>> m_ordered_outputs;
0073 
0074 public:
0075 
0076     const std::vector<OutputBase*>& GetOutputs() const {
0077         return m_outputs;
0078     }
0079 
0080     const std::vector<VariadicOutputBase*>& GetVariadicOutputs() const {
0081         return m_variadic_outputs;
0082     }
0083 
0084     JDatabundle* GetFirstDatabundle() const {
0085         if (m_outputs.size() > 0) {
0086             return m_outputs.at(0)->GetDatabundle();
0087         }
0088         return m_variadic_outputs.at(0)->GetDatabundles().at(0);
0089         // TODO: This will except if our first databundle is an empty variadic one
0090     }
0091 
0092     void RegisterOutput(OutputBase* output) {
0093         m_outputs.push_back(output);
0094         m_ordered_outputs.push_back({output, nullptr});
0095     }
0096 
0097     void RegisterOutput(VariadicOutputBase* output) {
0098         m_variadic_outputs.push_back(output);
0099         m_ordered_outputs.push_back({nullptr, output});
0100     }
0101 
0102     void SummarizeOutputs(JComponentSummary::Component& summary) const {
0103 
0104         for (auto* output : m_outputs) {
0105             auto* databundle = output->GetDatabundle();
0106             summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(), 
0107                                                                   databundle->GetUniqueName(), 
0108                                                              databundle->GetTypeName(), 
0109                                                                  output->GetLevel()));
0110         }
0111 
0112         for (auto* output : m_variadic_outputs) {
0113             for (auto* databundle : output->GetDatabundles()) {
0114                 summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(), 
0115                                                                       databundle->GetUniqueName(), 
0116                                                                  databundle->GetTypeName(), 
0117                                                                      output->GetLevel()));
0118             }
0119         }
0120     }
0121 
0122     void WireOutputs(JEventLevel component_level,
0123                                const std::vector<std::string>& single_output_databundle_names,
0124                                const std::vector<std::vector<std::string>>& variadic_output_databundle_names,
0125                                bool use_short_names) {
0126 
0127         if (m_variadic_outputs.size() == 1 && variadic_output_databundle_names.size() == 0) {
0128             // Obtain variadic databundle names from excess single-output databundle names
0129             int variadic_databundle_count = single_output_databundle_names.size() - m_outputs.size();
0130             int current_databundle_index = 0;
0131 
0132             for (auto& pair : m_ordered_outputs) {
0133                 auto* single_output = pair.first;
0134                 auto* variadic_output = pair.second;
0135 
0136                 if (variadic_output != nullptr) {
0137 
0138                     variadic_output->SetLevel(component_level);
0139                     std::vector<std::string> variadic_names;
0140                     for (int i=0; i<variadic_databundle_count; ++i) {
0141                         variadic_names.push_back(single_output_databundle_names.at(current_databundle_index+i));
0142                     }
0143                     if (use_short_names) {
0144                         variadic_output->SetShortNames(variadic_names);
0145                     } else {
0146                         variadic_output->SetUniqueNames(variadic_names);
0147                     }
0148                     current_databundle_index += variadic_databundle_count;
0149                 }
0150                 else {
0151                     single_output->SetLevel(component_level);
0152                     if (use_short_names) {
0153                         single_output->SetShortName(single_output_databundle_names.at(current_databundle_index));
0154                     }
0155                     else {
0156                         single_output->SetUniqueName(single_output_databundle_names.at(current_databundle_index));
0157                     }
0158                     current_databundle_index += 1;
0159                 }
0160             }
0161         }
0162         else {
0163             // Do the obvious, sensible thing instead
0164             size_t i = 0;
0165             for (auto* output : m_outputs) {
0166                 output->SetLevel(component_level);
0167                 if (use_short_names) {
0168                     output->SetShortName(single_output_databundle_names.at(i));
0169                 }
0170                 else {
0171                     output->SetUniqueName(single_output_databundle_names.at(i));
0172                 }
0173                 i += 1;
0174             }
0175             i = 0;
0176             for (auto* variadic_output : m_variadic_outputs) {
0177                 variadic_output->SetLevel(component_level);
0178                 if (use_short_names) {
0179                     variadic_output->SetShortNames(variadic_output_databundle_names.at(i));
0180                 }
0181                 else {
0182                     variadic_output->SetUniqueNames(variadic_output_databundle_names.at(i));
0183                 }
0184                 i += 1;
0185             }
0186         }
0187     }
0188 };
0189 
0190 } // namespace jana::components
0191