Back to home page

EIC code displayed by LXR

 
 

    


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

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 #include <JANA/Utils/JEventLevel.h>
0006 #include <vector>
0007 
0008 class JEventKey {
0009     struct Entry { JEventLevel level, 
0010                    uint64_t absolute_number, // Starts at zero. For each event emitted at this level, increases by 1
0011                    uint64_t relative_number, // Starts at zero. For each event emitted at this level BELONGING TO THIS PARENT, increases by 1
0012                    uint64_t user_number }    // User can set this using whatever convention they like. However it should probably increase monotonically.
0013                                              // Defaults to absolute_number.
0014 
0015     std::vector<Entry> m_entries; // store in REVERSE order: [self, parent, grandparent, ...]
0016     int64_t m_run_number = -1;  
0017     // Set to -1 by default (we need to know when we don't have a run number so that the calibrations db can respond accordingly)
0018     // User can set this to whatever they want
0019     // Run number will automatically be propagated to any children
0020 
0021 public:
0022 
0023     JEventKey() {
0024         m_entries.push_back({JEventLevel::None, 0, 0, 0});
0025     }
0026 
0027     Configure(JEventLevel level, uint64_t absolute_number) {
0028         m_entries.clear();
0029         m_entries.push_back({level, absolute_number, absolute_number, absolute_number});
0030     }
0031 
0032     AddParent(const JEventKey& parent_key, uint64_t relative_number) {
0033         if (m_entries.size() > 1) {
0034             throw JException("Event already has a parent");
0035         }
0036         for (const auto& entry : parent_key.m_entries) {
0037             m_entries.push_back(entry);
0038             // Store a copy of all parent entries locally so that we can more reliably detect refcount/recycling issues
0039             // This may change as we figure out how to track non-parent ancestors and siblings
0040         }
0041         m_entries[0].relative_number = relative_number;
0042         if (m_run_number == -1) {
0043             m_run_number = parent_key.m_run_number;
0044         }
0045     }
0046 
0047     const std::vector<Entry>& GetEntries() const {
0048         return m_entries;
0049     }
0050 
0051     void SetUserEventNumber(uint64_t user_number) {
0052         // m_entries always has at least one entry
0053         if (m_entries.size() == 0) throw JException("")
0054         m_entries.back().user_number = user_number;
0055     }
0056 
0057     void SetRunNumber(int64_t run_number) {
0058         m_run_number = run_number;
0059     }
0060 
0061     std::string toString() {
0062         std::ostringstream ss;
0063         ss << *this;
0064         return ss.str();
0065     }
0066 
0067     JEventLevel GetLevel() {
0068         return m_entries[0].level;
0069     }
0070 
0071     uint64_t GetAbsoluteEventNumber() {
0072         return m_entries[0].absolute_number;
0073     }
0074 
0075     uint64_t GetRelativeEventNumber() {
0076         return m_entries[0].relative_number;
0077     }
0078 
0079     uint64_t GetUserEventNumber() {
0080         return m_entries[0].user_number;
0081     }
0082 }
0083 
0084 inline std::ostream& operator<<(std::ostream& in, const JEventKey& key) {
0085     // Timeslice 34.10.19 (absolute #1234, user #5678)"
0086     in << key.GetLevel() << " ";
0087     auto s = m_entries.size();
0088     for (size_t i=m_entries.size(); i>0; --i) {
0089         in << m_entries[s-i-1].relative_number << ".";
0090     }
0091     in << key.GetRelativeEventNumber();
0092     if (key.GetRelativeEventNumber() != key.GetAbsoluteEventNumber()) {
0093         in << "(absolute #" << key.GetAbsoluteEventNumber();
0094 
0095     }
0096     if (key.GetUserEventNumber() != key.GetAbsoluteEventNumber()) {
0097         in << ", user #" << key.GetUserNumber();
0098     }
0099     in << ")";
0100     return in;
0101 }