Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-26 09:04:11

0001 // Copyright 2023, 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/JEventFolder.h>
0007 #include <JANA/Topology/JArrow.h>
0008 
0009 class JFoldArrow : public JArrow {
0010 public:
0011     enum PortIndex {CHILD_IN=0, CHILD_OUT=1, PARENT_OUT=2};
0012 
0013 private:
0014     JEventFolder* m_folder = nullptr;
0015 
0016     JEventLevel m_parent_level;
0017     JEventLevel m_child_level;
0018 
0019 public:
0020     JFoldArrow(
0021         std::string name,
0022         JEventLevel parent_level,
0023         JEventLevel child_level)
0024 
0025       : m_parent_level(parent_level),
0026         m_child_level(child_level)
0027     {
0028         set_name(name);
0029         create_ports(1, 2);
0030         m_next_input_port = CHILD_IN;
0031         m_ports[CHILD_IN].enforces_ordering = true;
0032     }
0033 
0034     void set_folder(JEventFolder* folder) {
0035         m_folder = folder;
0036     }
0037 
0038     void initialize() final {
0039         if (m_folder != nullptr) {
0040             m_folder->DoInit();
0041             LOG_INFO(m_logger) << "Initialized JEventFolder '" << m_folder->GetTypeName() << "'" << LOG_END;
0042         }
0043         else {
0044             LOG_INFO(m_logger) << "Initialized JEventFolder (trivial)" << LOG_END;
0045         }
0046     }
0047 
0048     void finalize() final {
0049         if (m_folder != nullptr) {
0050             m_folder->DoFinish();
0051             LOG_INFO(m_logger) << "Finalized JEventFolder '" << m_folder->GetTypeName() << "'" << LOG_END;
0052         }
0053         else {
0054             LOG_INFO(m_logger) << "Finalized JEventFolder (trivial)" << LOG_END;
0055         }
0056     }
0057 
0058     void fire(JEvent* event, OutputData& outputs, size_t& output_count, JArrow::FireResult& status) final {
0059 
0060         assert(m_next_input_port == CHILD_IN);
0061 
0062         // Check that child is at the correct event level
0063         if (event->GetLevel() != m_child_level) {
0064             throw JException("JFoldArrow received a child with the wrong event level");
0065         }
0066 
0067         if (m_folder != nullptr) {
0068             auto parent = const_cast<JEvent*>(&event->GetParent(m_parent_level));
0069             m_folder->DoFold(*event, *parent);
0070         }
0071 
0072         status = JArrow::FireResult::KeepGoing;
0073         outputs[0] = {event, CHILD_OUT};
0074         output_count = 1;
0075 
0076         auto* released_parent = event->ReleaseParent(m_parent_level);
0077         if (released_parent != nullptr) {
0078             // JEvent::ReleaseParent() returns nullptr if there are remaining references
0079             // to the parent event. If non-null, we are completely done with the parent
0080             // and are free to return it to the pool. In the future we could have the pool
0081             // itself handle the logic for releasing parents, in which case we could avoid
0082             // trivial JEventFolders.
0083 
0084             outputs[1] = {released_parent, PARENT_OUT};
0085             output_count = 2;
0086         }
0087     }
0088 
0089 };
0090 
0091