Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:32:09

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 
0013 #include <GaudiKernel/EventIDBase.h>
0014 #include <any>
0015 #include <cstddef>
0016 #include <fmt/format.h>
0017 #include <iostream>
0018 #include <limits>
0019 
0020 /** @class EventContext EventContext.h GaudiKernel/EventContext.h
0021  *
0022  * This class represents an entry point to all the event specific data.
0023  * It is needed to make the algorithm "aware" of the event it is operating on.
0024  * This was not needed in the serial version of Gaudi where the assumption
0025  * of 1-event-at-the-time processing was implicit.
0026  *
0027  * This class has nothing to do with the AlgContextSvc or the Context of
0028  * the EvtSelector!
0029  *
0030  * @author Danilo Piparo, Charles Leggett
0031  * @date 2012
0032  **/
0033 
0034 class EventContext {
0035 public:
0036   using ContextID_t  = size_t;
0037   using ContextEvt_t = size_t;
0038 
0039   static constexpr ContextID_t  INVALID_CONTEXT_ID  = std::numeric_limits<ContextID_t>::max();
0040   static constexpr ContextEvt_t INVALID_CONTEXT_EVT = std::numeric_limits<ContextEvt_t>::max();
0041 
0042   EventContext() = default;
0043 
0044   explicit EventContext( const ContextEvt_t e, const ContextID_t s = INVALID_CONTEXT_ID,
0045                          const ContextID_t subSlot = INVALID_CONTEXT_ID )
0046       : m_evt_num( e ), m_evt_slot( s ), m_sub_slot( subSlot ) {
0047     m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
0048   }
0049 
0050   ContextEvt_t       evt() const { return m_evt_num; }
0051   ContextID_t        slot() const { return m_evt_slot; }
0052   ContextID_t        subSlot() const { return m_sub_slot; }
0053   bool               usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
0054   bool               valid() const { return m_valid; }
0055   const EventIDBase& eventID() const { return m_eid; }
0056 
0057   void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
0058             const ContextID_t subSlot = INVALID_CONTEXT_ID ) {
0059     m_valid    = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
0060     m_evt_num  = e;
0061     m_evt_slot = s;
0062     m_sub_slot = subSlot;
0063   }
0064 
0065   void setEvt( const ContextEvt_t e ) {
0066     if ( e == INVALID_CONTEXT_EVT ) setValid( false );
0067     m_evt_num = e;
0068   }
0069 
0070   void setSlot( const ContextID_t s ) {
0071     if ( s == INVALID_CONTEXT_ID ) setValid( false );
0072     m_evt_slot = s;
0073   }
0074 
0075   void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
0076 
0077   void setValid( const bool b = true ) {
0078     m_valid = b;
0079     if ( !m_valid ) {
0080       m_evt_slot = INVALID_CONTEXT_ID;
0081       m_evt_num  = INVALID_CONTEXT_EVT;
0082     }
0083   }
0084 
0085   void setEventID( const EventIDBase& e ) { m_eid = e; }
0086 
0087   template <typename ValueType, typename... Args>
0088   auto& emplaceExtension( Args&&... args ) {
0089     return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
0090   }
0091 
0092   template <typename T>
0093   auto& setExtension( T&& t ) {
0094     m_extension = std::forward<T>( t );
0095     return getExtension<T>();
0096   }
0097 
0098   void resetExtension() { m_extension.reset(); }
0099 
0100   std::any detachExtension() { return std::move( m_extension ); }
0101 
0102   template <typename T>
0103   auto& getExtension() {
0104     return std::any_cast<std::decay_t<T>&>( m_extension );
0105   }
0106 
0107   template <typename T>
0108   const auto& getExtension() const {
0109     return std::any_cast<std::decay_t<T> const&>( m_extension );
0110   }
0111 
0112   template <typename T>
0113   T* tryGetExtension() noexcept {
0114     return std::any_cast<T>( &m_extension );
0115   }
0116 
0117   template <typename T>
0118   const T* tryGetExtension() const noexcept {
0119     return std::any_cast<T>( &m_extension );
0120   }
0121 
0122   bool hasExtension() const { return m_extension.has_value(); }
0123 
0124   template <typename T>
0125   bool hasExtension() const {
0126     return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
0127   }
0128 
0129   const std::type_info& getExtensionType() const { return m_extension.type(); }
0130 
0131   friend std::ostream& operator<<( std::ostream& os, const EventContext& ctx );
0132   friend std::ostream& operator<<( std::ostream& os, const EventContext* ctx );
0133 
0134 private:
0135   EventIDBase  m_eid{};
0136   ContextEvt_t m_evt_num{ INVALID_CONTEXT_EVT };
0137   ContextID_t  m_evt_slot{ INVALID_CONTEXT_ID };
0138   ContextID_t  m_sub_slot{ INVALID_CONTEXT_ID };
0139   bool         m_valid{ false };
0140 
0141   std::any m_extension;
0142 };
0143 
0144 template <>
0145 struct fmt::formatter<EventContext> : formatter<string_view> {
0146   auto format( const EventContext& ec, format_context& ctx ) const {
0147     if ( ec.valid() ) {
0148       std::string out;
0149       if ( ec.usesSubSlot() ) {
0150         out = fmt::format( "s: {}  e: {} sub: {}", ec.slot(), ec.evt(), ec.subSlot() );
0151       } else {
0152         out = fmt::format( "s: {}  e: {}", ec.slot(), ec.evt() );
0153       }
0154       return formatter<string_view>::format( out, ctx );
0155     } else {
0156       return formatter<string_view>::format( "INVALID", ctx );
0157     }
0158   }
0159 };