Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:38

0001 // Copyright 2024, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
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& /*parent*/) const {};
0032 
0033     virtual void Fold(const JEvent& /*child*/, JEvent& /*parent*/, int /*item_nr*/) {
0034         throw JException("Not implemented yet!");
0035     };
0036 
0037     virtual void Finish() {};
0038 
0039 
0040     // Configuration
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     // Backend
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         // TODO: Obtain overrides of collection names from param manager
0060         for (auto* parameter : m_parameters) {
0061             parameter->Configure(*(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                 // TODO: Consider calling Initialize(with_lock=false) like we do elsewhere
0076             }
0077         }
0078         for (auto* input : m_inputs) {
0079             input->PrefetchCollection(child);
0080         }
0081         if (m_callback_style != CallbackStyle::DeclarativeMode) {
0082             CallWithJExceptionWrapper("JEventFolder::Preprocess", [&](){
0083                 Preprocess(child);
0084             });
0085         }
0086     }
0087 
0088     void DoFold(const JEvent& child, JEvent& parent) {
0089         std::lock_guard<std::mutex> lock(m_mutex);
0090         if (m_status != Status::Initialized) {
0091             throw JException("Component needs to be initialized and not finalized before Fold() can be called");
0092         }
0093         if (!m_call_preprocess_upstream) {
0094             CallWithJExceptionWrapper("JEventFolder::Preprocess", [&](){
0095                 Preprocess(parent);
0096             });
0097         }
0098         if (m_last_run_number != parent.GetRunNumber()) {
0099             for (auto* resource : m_resources) {
0100                 resource->ChangeRun(parent.GetRunNumber(), m_app);
0101             }
0102             if (m_callback_style == CallbackStyle::DeclarativeMode) {
0103                 CallWithJExceptionWrapper("JEventFolder::ChangeRun", [&](){
0104                     ChangeRun(parent.GetRunNumber());
0105                 });
0106             }
0107             else {
0108                 CallWithJExceptionWrapper("JEventFolder::ChangeRun", [&](){
0109                     ChangeRun(parent);
0110                 });
0111             }
0112             m_last_run_number = parent.GetRunNumber();
0113         }
0114         for (auto* input : m_inputs) {
0115             input->GetCollection(parent);
0116             // TODO: This requires that all inputs come from the parent.
0117             //       However, eventually we will want to support inputs 
0118             //       that come from the child.
0119         }
0120         for (auto* output : m_outputs) {
0121             output->Reset();
0122         }
0123         auto child_number = child.GetEventIndex();
0124         CallWithJExceptionWrapper("JEventFolder::Fold", [&](){
0125             Fold(child, parent, child_number);
0126         });
0127 
0128         for (auto* output : m_outputs) {
0129             output->InsertCollection(parent);
0130         }
0131     }
0132 
0133     void DoFinish() {
0134         std::lock_guard<std::mutex> lock(m_mutex);
0135         if (m_status != Status::Finalized) {
0136             CallWithJExceptionWrapper("JEventFolder::Finish", [&](){
0137                 Finish();
0138             });
0139             m_status = Status::Finalized;
0140         }
0141     }
0142 
0143     void Summarize(JComponentSummary& summary) const override {
0144         auto* us = new JComponentSummary::Component( 
0145             "Folder", GetPrefix(), GetTypeName(), GetLevel(), GetPluginName());
0146 
0147         for (const auto* input : m_inputs) {
0148             size_t subinput_count = input->names.size();
0149             for (size_t i=0; i<subinput_count; ++i) {
0150                 us->AddInput(new JComponentSummary::Collection("", input->names[i], input->type_name, input->levels[i]));
0151             }
0152         }
0153         for (const auto* output : m_outputs) {
0154             size_t suboutput_count = output->collection_names.size();
0155             for (size_t i=0; i<suboutput_count; ++i) {
0156                 us->AddOutput(new JComponentSummary::Collection("", output->collection_names[i], output->type_name, GetLevel()));
0157             }
0158         }
0159         summary.Add(us);
0160     }
0161 
0162 };
0163 
0164