File indexing completed on 2025-07-09 08:51:53
0001
0002
0003
0004 #pragma once
0005
0006 #include <JANA/Components/JComponent.h>
0007 #include <JANA/Components/JHasInputs.h>
0008 #include <JANA/Components/JHasOutputs.h>
0009 #include <JANA/Components/JHasRunCallbacks.h>
0010 #include <JANA/JEvent.h>
0011
0012 class JApplication;
0013 class JEventFolder : public jana::components::JComponent,
0014 public jana::components::JHasRunCallbacks,
0015 public jana::components::JHasInputs,
0016 public jana::components::JHasOutputs {
0017
0018 private:
0019 int32_t m_last_run_number = -1;
0020 JEventLevel m_child_level;
0021 bool m_call_preprocess_upstream = true;
0022
0023
0024 public:
0025
0026 JEventFolder() = default;
0027 virtual ~JEventFolder() {};
0028
0029 virtual void Init() {};
0030
0031 virtual void Preprocess(const JEvent& ) const {};
0032
0033 virtual void Fold(const JEvent& , JEvent& , int ) {
0034 throw JException("Not implemented yet!");
0035 };
0036
0037 virtual void Finish() {};
0038
0039
0040
0041
0042 void SetParentLevel(JEventLevel level) { m_level = level; }
0043
0044 void SetChildLevel(JEventLevel level) { m_child_level = level; }
0045
0046 void SetCallPreprocessUpstream(bool call_upstream) { m_call_preprocess_upstream = call_upstream; }
0047
0048 JEventLevel GetChildLevel() { return m_child_level; }
0049
0050
0051 public:
0052
0053
0054 void DoInit() {
0055 std::lock_guard<std::mutex> lock(m_mutex);
0056 if (m_status != Status::Uninitialized) {
0057 throw JException("JEventFolder: Attempting to initialize twice or from an invalid state");
0058 }
0059
0060 for (auto* parameter : m_parameters) {
0061 parameter->Init(*(m_app->GetJParameterManager()), m_prefix);
0062 }
0063 for (auto* service : m_services) {
0064 service->Fetch(m_app);
0065 }
0066 CallWithJExceptionWrapper("JEventFolder::Init", [&](){Init();});
0067 m_status = Status::Initialized;
0068 }
0069
0070 void DoPreprocess(const JEvent& child) {
0071 {
0072 std::lock_guard<std::mutex> lock(m_mutex);
0073 if (m_status != Status::Initialized) {
0074 throw JException("JEventFolder: Component needs to be initialized and not finalized before Fold can be called");
0075 }
0076 }
0077 for (auto* input : m_inputs) {
0078 input->PrefetchCollection(child);
0079 }
0080 for (auto* variadic_input : m_variadic_inputs) {
0081 variadic_input->PrefetchCollection(child);
0082 }
0083 if (m_callback_style != CallbackStyle::DeclarativeMode) {
0084 CallWithJExceptionWrapper("JEventFolder::Preprocess", [&](){
0085 Preprocess(child);
0086 });
0087 }
0088 }
0089
0090 void DoFold(const JEvent& child, JEvent& parent) {
0091 std::lock_guard<std::mutex> lock(m_mutex);
0092 if (m_status != Status::Initialized) {
0093 throw JException("Component needs to be initialized and not finalized before Fold() can be called");
0094 }
0095 if (!m_call_preprocess_upstream) {
0096 CallWithJExceptionWrapper("JEventFolder::Preprocess", [&](){
0097 Preprocess(child);
0098 });
0099 }
0100 if (m_last_run_number != parent.GetRunNumber()) {
0101 for (auto* resource : m_resources) {
0102 resource->ChangeRun(parent.GetRunNumber(), m_app);
0103 }
0104 if (m_callback_style == CallbackStyle::DeclarativeMode) {
0105 CallWithJExceptionWrapper("JEventFolder::ChangeRun", [&](){
0106 ChangeRun(parent.GetRunNumber());
0107 });
0108 }
0109 else {
0110 CallWithJExceptionWrapper("JEventFolder::ChangeRun", [&](){
0111 ChangeRun(parent);
0112 });
0113 }
0114 m_last_run_number = parent.GetRunNumber();
0115 }
0116 for (auto* input : m_inputs) {
0117 input->GetCollection(child);
0118 }
0119 for (auto* variadic_input : m_variadic_inputs) {
0120 variadic_input->GetCollection(child);
0121 }
0122 for (auto* output : m_outputs) {
0123 output->Reset();
0124 }
0125 auto child_number = child.GetEventIndex();
0126 CallWithJExceptionWrapper("JEventFolder::Fold", [&](){
0127 Fold(child, parent, child_number);
0128 });
0129
0130 for (auto* output : m_outputs) {
0131 output->InsertCollection(parent);
0132 }
0133 }
0134
0135 void DoFinish() {
0136 std::lock_guard<std::mutex> lock(m_mutex);
0137 if (m_status != Status::Finalized) {
0138 CallWithJExceptionWrapper("JEventFolder::Finish", [&](){
0139 Finish();
0140 });
0141 m_status = Status::Finalized;
0142 }
0143 }
0144
0145 void Summarize(JComponentSummary& summary) const override {
0146 auto* us = new JComponentSummary::Component(
0147 "Folder", GetPrefix(), GetTypeName(), GetLevel(), GetPluginName());
0148
0149 for (const auto* input : m_inputs) {
0150 us->AddInput(new JComponentSummary::Collection("", input->GetDatabundleName(), input->GetTypeName(), input->GetLevel()));
0151 }
0152 for (const auto* input : m_variadic_inputs) {
0153 size_t subinput_count = input->GetRequestedDatabundleNames().size();
0154 for (size_t i=0; i<subinput_count; ++i) {
0155 us->AddInput(new JComponentSummary::Collection("", input->GetRequestedDatabundleNames().at(i), input->GetTypeName(), input->GetLevel()));
0156 }
0157 }
0158 for (const auto* output : m_outputs) {
0159 size_t suboutput_count = output->collection_names.size();
0160 for (size_t i=0; i<suboutput_count; ++i) {
0161 us->AddOutput(new JComponentSummary::Collection("", output->collection_names[i], output->type_name, GetLevel()));
0162 }
0163 }
0164 summary.Add(us);
0165 }
0166
0167 };
0168
0169