File indexing completed on 2025-10-31 09:22:26
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;
0019
0020 public:
0021 virtual ~OutputBase() {
0022 delete m_databundle;
0023 }
0024 JDatabundle* GetDatabundle() { return m_databundle; }
0025 JEventLevel GetLevel() const { return m_level; }
0026
0027 void SetDatabundle(JDatabundle* databundle) { m_databundle = databundle; }
0028 void SetLevel(JEventLevel level) { m_level = level; }
0029 void SetShortName(std::string short_name) { m_databundle->SetShortName(short_name); }
0030 void SetUniqueName(std::string unique_name) { m_databundle->SetUniqueName(unique_name); }
0031
0032 virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0033 virtual void EulerianStore(JFactorySet&) {}
0034
0035 void ClearData() { m_databundle->ClearData(); }
0036 };
0037
0038
0039 class VariadicOutputBase {
0040 private:
0041 std::vector<JDatabundle*> m_databundles;
0042 JEventLevel m_level = JEventLevel::PhysicsEvent;
0043
0044 public:
0045 virtual ~VariadicOutputBase() {
0046 for (auto* db : m_databundles) {
0047 delete db;
0048 }
0049 }
0050 std::vector<JDatabundle*>& GetDatabundles() { return m_databundles; }
0051 JEventLevel GetLevel() const { return m_level; }
0052
0053 void SetLevel(JEventLevel level) { m_level = level; }
0054 virtual void SetShortNames(std::vector<std::string>) {}
0055 virtual void SetUniqueNames(std::vector<std::string>) {}
0056
0057 std::vector<std::string> GetUniqueNames() {
0058 std::vector<std::string> results;
0059 for (auto* databundle: m_databundles) {
0060 results.push_back(databundle->GetUniqueName());
0061 }
0062 return results;
0063 };
0064
0065 virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0066 virtual void EulerianStore(JFactorySet&) {}
0067
0068 void ClearData() {
0069 for (auto* databundle : m_databundles) {
0070 databundle->ClearData();
0071 }
0072 }
0073 };
0074
0075 private:
0076 std::vector<OutputBase*> m_outputs;;
0077 std::vector<VariadicOutputBase*> m_variadic_outputs;
0078 std::vector<std::pair<OutputBase*, VariadicOutputBase*>> m_ordered_outputs;
0079
0080 public:
0081
0082 const std::vector<OutputBase*>& GetOutputs() const {
0083 return m_outputs;
0084 }
0085
0086 const std::vector<VariadicOutputBase*>& GetVariadicOutputs() const {
0087 return m_variadic_outputs;
0088 }
0089
0090 JDatabundle* GetFirstDatabundle() const {
0091 if (m_outputs.size() > 0) {
0092 return m_outputs.at(0)->GetDatabundle();
0093 }
0094 return m_variadic_outputs.at(0)->GetDatabundles().at(0);
0095
0096 }
0097
0098 void RegisterOutput(OutputBase* output) {
0099 m_outputs.push_back(output);
0100 m_ordered_outputs.push_back({output, nullptr});
0101 }
0102
0103 void RegisterOutput(VariadicOutputBase* output) {
0104 m_variadic_outputs.push_back(output);
0105 m_ordered_outputs.push_back({nullptr, output});
0106 }
0107
0108 void SummarizeOutputs(JComponentSummary::Component& summary) const {
0109
0110 for (auto* output : m_outputs) {
0111 auto* databundle = output->GetDatabundle();
0112 summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(),
0113 databundle->GetUniqueName(),
0114 databundle->GetTypeName(),
0115 output->GetLevel()));
0116 }
0117
0118 for (auto* output : m_variadic_outputs) {
0119 for (auto* databundle : output->GetDatabundles()) {
0120 summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(),
0121 databundle->GetUniqueName(),
0122 databundle->GetTypeName(),
0123 output->GetLevel()));
0124 }
0125 }
0126 }
0127
0128 void WireOutputs(JEventLevel component_level,
0129 const std::vector<std::string>& single_output_databundle_names,
0130 const std::vector<std::vector<std::string>>& variadic_output_databundle_names,
0131 bool use_short_names) {
0132
0133 if (m_variadic_outputs.size() == 1 && variadic_output_databundle_names.size() == 0) {
0134
0135 int variadic_databundle_count = single_output_databundle_names.size() - m_outputs.size();
0136 int current_databundle_index = 0;
0137
0138 for (auto& pair : m_ordered_outputs) {
0139 auto* single_output = pair.first;
0140 auto* variadic_output = pair.second;
0141
0142 if (variadic_output != nullptr) {
0143
0144 variadic_output->SetLevel(component_level);
0145 std::vector<std::string> variadic_names;
0146 for (int i=0; i<variadic_databundle_count; ++i) {
0147 variadic_names.push_back(single_output_databundle_names.at(current_databundle_index+i));
0148 }
0149 if (use_short_names) {
0150 variadic_output->SetShortNames(variadic_names);
0151 } else {
0152 variadic_output->SetUniqueNames(variadic_names);
0153 }
0154 current_databundle_index += variadic_databundle_count;
0155 }
0156 else {
0157 single_output->SetLevel(component_level);
0158 if (use_short_names) {
0159 single_output->SetShortName(single_output_databundle_names.at(current_databundle_index));
0160 }
0161 else {
0162 single_output->SetUniqueName(single_output_databundle_names.at(current_databundle_index));
0163 }
0164 current_databundle_index += 1;
0165 }
0166 }
0167 }
0168 else {
0169
0170 size_t i = 0;
0171 for (auto* output : m_outputs) {
0172 output->SetLevel(component_level);
0173 if (use_short_names) {
0174 output->SetShortName(single_output_databundle_names.at(i));
0175 }
0176 else {
0177 output->SetUniqueName(single_output_databundle_names.at(i));
0178 }
0179 i += 1;
0180 }
0181 i = 0;
0182 for (auto* variadic_output : m_variadic_outputs) {
0183 variadic_output->SetLevel(component_level);
0184 if (use_short_names) {
0185 variadic_output->SetShortNames(variadic_output_databundle_names.at(i));
0186 }
0187 else {
0188 variadic_output->SetUniqueNames(variadic_output_databundle_names.at(i));
0189 }
0190 i += 1;
0191 }
0192 }
0193 }
0194 };
0195
0196 }
0197