Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:41:04

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/Topology/JArrow.h>
0007 #include <JANA/JEventUnfolder.h>
0008 
0009 class JUnfoldArrow : public JArrow {
0010 public:
0011     enum PortIndex {PARENT_IN=0, CHILD_IN=1, CHILD_OUT=2, REJECTED_PARENT_OUT=3};
0012 
0013 private:
0014     JEventUnfolder* m_unfolder = nullptr;
0015     JEvent* m_parent_event = nullptr;
0016     JEvent* m_child_event = nullptr;
0017 
0018 public:
0019     JUnfoldArrow(std::string name, JEventUnfolder* unfolder) : m_unfolder(unfolder) {
0020         set_name(name);
0021         create_ports(2, 2);
0022         m_next_input_port = PARENT_IN;
0023     }
0024 
0025     void initialize() final {
0026         m_unfolder->DoInit();
0027         LOG_INFO(m_logger) << "Initialized JEventUnfolder '" << m_unfolder->GetTypeName() << "'" << LOG_END;
0028     }
0029 
0030     void finalize() final {
0031         m_unfolder->DoFinish();
0032         LOG_INFO(m_logger) << "Finalized JEventUnfolder '" << m_unfolder->GetTypeName() << "'" << LOG_END;
0033     }
0034 
0035     void fire(JEvent* event, OutputData& outputs, size_t& output_count, JArrow::FireResult& status) final {
0036 
0037         // Take whatever we were given
0038         if (this->m_next_input_port == PARENT_IN) {
0039             assert(m_parent_event == nullptr);
0040             m_parent_event = event;
0041             m_parent_event->TakeRefToSelf();
0042         }
0043         else if (this->m_next_input_port == CHILD_IN) {
0044             assert(m_child_event == nullptr);
0045             m_child_event = event;
0046         }
0047         else {
0048             throw JException("Invalid input port for JEventUnfolder!");
0049         }
0050 
0051         // Check if we should exit early because we don't have a parent event
0052         if (m_parent_event == nullptr) {
0053             m_next_input_port = PARENT_IN;
0054             output_count = 0;
0055             status = JArrow::FireResult::KeepGoing;
0056             return;
0057         }
0058 
0059         // Check if we should exit early because we don't have a child event
0060         if (m_child_event == nullptr) {
0061             m_next_input_port = CHILD_IN;
0062             output_count = 0;
0063             status = JArrow::FireResult::KeepGoing;
0064             return;
0065         }
0066 
0067         // At this point we know we have both inputs, so we can run the unfolder. First we validate 
0068         // that the events we received are at the correct level for the unfolder. Hopefully the only 
0069         // way to end up here is to override the JTopologyBuilder wiring and do it wrong
0070 
0071         if (m_parent_event->GetLevel() != m_unfolder->GetLevel()) {
0072             throw JException("JUnfolder: Expected parent with level %s, got %s", toString(m_unfolder->GetLevel()).c_str(), toString(m_parent_event->GetLevel()).c_str());
0073         }
0074 
0075         if (m_child_event->GetLevel() != m_unfolder->GetChildLevel()) {
0076             throw JException("JUnfolder: Expected child with level %s, got %s", toString(m_unfolder->GetChildLevel()).c_str(), toString(m_child_event->GetLevel()).c_str());
0077         }
0078 
0079         auto result = m_unfolder->DoUnfold(*m_parent_event, *m_child_event);
0080         LOG_DEBUG(m_logger) << "Unfold succeeded: Parent event = " << m_parent_event->GetEventNumber() << ", child event = " << m_child_event->GetEventNumber() << LOG_END;
0081 
0082         if (result == JEventUnfolder::Result::KeepChildNextParent) {
0083             // KeepChildNextParent is a little more complicated because we have to handle the case of the parent having no children.
0084             // In this case the parent obviously doesn't get shared among any children, and instead it is sent to the REJECTED_PARENT_OUT port.
0085             int child_count = m_parent_event->ReleaseRefToSelf(); // Decrement the reference count so that this can be recycled
0086             LOG_DEBUG(m_logger) << "Unfold finished with parent event = " << m_parent_event->GetEventNumber() << " (" << child_count << " children emitted)";
0087 
0088             if (child_count > 0) {
0089                 // Parent DOES have children even though this particular child isn't one of them
0090                 m_parent_event = nullptr;
0091                 output_count = 0;
0092                 m_next_input_port = PARENT_IN;
0093                 status = JArrow::FireResult::KeepGoing;
0094                 return;
0095             }
0096             else {
0097                 // Parent has NO children
0098                 output_count = 1;
0099                 outputs[0] = {m_parent_event, REJECTED_PARENT_OUT};
0100                 m_parent_event = nullptr;
0101                 m_next_input_port = PARENT_IN;
0102                 status = JArrow::FireResult::KeepGoing;
0103                 return;
0104             }
0105         }
0106         else if (result == JEventUnfolder::Result::NextChildKeepParent) {
0107             m_child_event->SetParent(m_parent_event);
0108             outputs[0] = {m_child_event, CHILD_OUT};
0109             output_count = 1;
0110             m_child_event = nullptr;
0111             m_next_input_port = CHILD_IN;
0112             status = JArrow::FireResult::KeepGoing;
0113             return;
0114         }
0115         else if (result == JEventUnfolder::Result::NextChildNextParent) {
0116             m_child_event->SetParent(m_parent_event);
0117             m_parent_event->ReleaseRefToSelf(); // Decrement the reference count so that this can be recycled
0118             outputs[0] = {m_child_event, CHILD_OUT};
0119             output_count = 1;
0120             LOG_DEBUG(m_logger) << "Unfold finished with parent event = " << m_parent_event->GetEventNumber() << LOG_END;
0121             m_child_event = nullptr;
0122             m_parent_event = nullptr;
0123             m_next_input_port = PARENT_IN;
0124             status = JArrow::FireResult::KeepGoing;
0125             return;
0126         }
0127         else {
0128             throw JException("Unsupported (corrupt?) JEventUnfolder::Result");
0129         }
0130     }
0131 };
0132 
0133