The Gaudi Framework  master (da3d77e1)
EventIDBase.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_EVENTIDBASE_H
12 #define GAUDIKERNEL_EVENTIDBASE_H 1
13 
26 #include <cstdint>
27 #include <iomanip>
28 #include <iostream>
29 #include <tuple>
30 
31 #include <GaudiKernel/compose.h>
32 namespace details {
33  template <typename lambda>
34  struct arg_helper : public arg_helper<decltype( &lambda::operator() )> {};
35  template <typename T, typename Ret, typename Arg>
36  struct arg_helper<Ret ( T::* )( Arg ) const> {
37  using type = Arg;
38  };
39 
40  // given a unary lambda whose argument is of type Arg_t,
41  // argument_t<lambda> will be equal to Arg_t
42  template <typename lambda>
44 
45  template <typename Fun>
46  auto add_deref( Fun f ) {
47  return compose( f, [=]( auto*... p ) { return f( *p... ); } );
48  }
49 
50  template <typename Proj, typename Cmp = std::greater<>>
51  auto make_cmp( Proj p, Cmp cmp = {} ) {
52  static_assert( std::is_reference_v<argument_t<Proj>>, "must be a reference" );
53  static_assert( std::is_const_v<std::remove_reference_t<argument_t<Proj>>>, "must be const" );
54  return [=]( argument_t<Proj> lhs, argument_t<Proj> rhs ) { return cmp( p( lhs ), p( rhs ) ); };
55  }
56 } // namespace details
57 
66 class EventIDBase {
67 public:
68  typedef unsigned int number_type;
69  typedef uint64_t event_number_t;
70 
71  static const number_type UNDEFNUM;
72  static const event_number_t UNDEFEVT;
73 
74  friend class EventIDRange;
75 
76 public:
78 
83 
86 
87  // Use default copy constructor.
88  virtual ~EventIDBase() = default;
90 
92  number_type run_number() const { return m_run_number; }
93 
96 
98  number_type time_stamp() const { return m_time_stamp; }
99 
102 
104  number_type lumi_block() const { return m_lumi_block; }
105 
108 
110  void set_run_number( number_type runNumber ) {
111  m_run_number = runNumber;
112  if ( m_event_number != UNDEFEVT ) setRE();
113  if ( m_lumi_block != UNDEFNUM ) setRL();
114  }
115 
117  void set_event_number( event_number_t eventNumber ) {
118  m_event_number = eventNumber;
119  if ( m_run_number != UNDEFNUM ) setRE();
120  if ( m_lumi_block != UNDEFNUM ) setLE();
121  }
122 
124  void set_time_stamp( number_type timeStamp ) {
125  m_time_stamp = timeStamp;
126  setTS();
127  }
128 
130  void set_time_stamp_ns_offset( number_type timeStampNs ) { m_time_stamp_ns_offset = timeStampNs; }
131 
133  void set_lumi_block( number_type lumiBlock ) {
134  m_lumi_block = lumiBlock;
135  if ( m_run_number != UNDEFNUM ) setRL();
136  if ( m_event_number != UNDEFEVT ) setLE();
137  }
138 
141 
143  friend bool operator==( const EventIDBase& lhs, const EventIDBase& rhs );
144  friend bool operator<( const EventIDBase& lhs, const EventIDBase& rhs );
145  friend bool operator>( const EventIDBase& lhs, const EventIDBase& rhs ) { return rhs < lhs; }
146  friend bool operator!=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs == rhs ); }
147  friend bool operator<=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs > rhs ); }
148  friend bool operator>=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs < rhs ); }
149 
150  friend EventIDBase min( const EventIDBase& lhs, const EventIDBase& rhs );
151  friend EventIDBase max( const EventIDBase& lhs, const EventIDBase& rhs );
152 
153  bool isRunEvent() const { return m_type & RunEvent; }
154  bool isTimeStamp() const { return m_type & TimeStamp; }
155  bool isLumiEvent() const { return m_type & LumiEvent; }
156  bool isRunLumi() const { return m_type & RunLumi; }
157  bool isValid() const { return m_type != Invalid; }
158 
160  friend std::ostream& operator<<( std::ostream& os, const EventIDBase& rhs );
161 
162  static auto SortByTimeStamp() {
164  []( const EventIDBase& e ) { return std::tie( e.m_time_stamp, e.m_time_stamp_ns_offset ); } ) );
165  }
166 
167  static auto SortByRunEvent() {
169  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_event_number ); } ) );
170  }
171 
172  static auto SortByLumiEvent() {
174  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_lumi_block, e.m_event_number ); } ) );
175  }
176 
177  static auto SortByRunLumi() {
179  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_lumi_block ); } ) );
180  }
181 
182 private:
183  enum Type { Invalid = 0, RunEvent = 1 << 1, TimeStamp = 1 << 2, LumiEvent = 1 << 3, RunLumi = 1 << 4 };
184 
185  unsigned m_type{ Invalid };
186 
187  void setRE() { m_type |= RunEvent; }
188  void setTS() { m_type |= TimeStamp; }
189  void setLE() { m_type |= LumiEvent; }
190  void setRL() { m_type |= RunLumi; }
191 
194 
197 
200 
203 
207 
210 
211  friend EventIDBase min( const EventIDBase& lhs, const EventIDBase& rhs ) {
212 
213  //"min" is easy b/c the numbers denoting invalidty for TS or Run/Event/LB are the
214  // largest possible numbers, so naturally larger than any valid number
215 
220  lhs.bunch_crossing_id() // bcid doesn't really matter here
221  );
222  }
223 
224  friend EventIDBase max( const EventIDBase& lhs, const EventIDBase& rhs ) {
225 
226  //"max" is much trickier because we need to handle invalid number explicilty by
227  // checking if a EventIDBase is TS or Run/Lumi
228 
231 
232  if ( lhs.isTimeStamp() && rhs.isTimeStamp() ) { // both time-stamp, compare them
235  } else if ( lhs.isTimeStamp() ) { // only lhs time-stamp: Use it
237  } else { // otherwise use rhs time-stamp which might be UNDEFNUM (in case both input values are Run-Lumi only)
239  }
240 
241  if ( lhs.isRunLumi() && rhs.isRunLumi() ) { // both run-lumi, compare them
242  run_lumi_ev = std::max( std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number ),
244 
245  } else if ( lhs.isRunLumi() ) { // only lhs run-lumi: Use it
246  run_lumi_ev = std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number );
247  } else { // otherwise use rhs run-lumi which might be UNDEFNUM (in case both input values are TS-only)
248  run_lumi_ev = std::tie( rhs.m_run_number, rhs.m_lumi_block, rhs.m_event_number );
249  }
250 
251  return EventIDBase( run_lumi_ev, time_stamp, lhs.bunch_crossing_id() );
252  }
253 
254  friend bool operator<( const EventIDBase& lhs, const EventIDBase& rhs ) {
255  // first try ordering by timestamp if both are non-zero
256  // then try ordering by run/lumi/event
257  // this assumes that both EventIDBase have the same set of values defined.
258 
259  if ( lhs.isTimeStamp() && rhs.isTimeStamp() ) {
260  return lhs.m_time_stamp < rhs.m_time_stamp;
261  } else {
262  return std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number ) <
264  }
265  }
266 
267  friend bool operator==( const EventIDBase& lhs, const EventIDBase& rhs ) {
268  // We assume that equality via run/event/lumi numbers is sufficient
269  return ( lhs.m_run_number == rhs.m_run_number && lhs.m_event_number == rhs.m_event_number &&
270  lhs.m_lumi_block == rhs.m_lumi_block );
271  }
272 
273  friend std::ostream& operator<<( std::ostream& os, const EventIDBase& rhs ) {
274  if ( rhs.m_type == EventIDBase::Invalid ) return os << "[INVALID]";
275 
276  const char* separator = "";
277  os << "[";
278  if ( rhs.m_run_number != EventIDBase::UNDEFNUM ) {
279  os << rhs.m_run_number;
280  separator = ",";
281  }
282 
283  if ( rhs.m_event_number != EventIDBase::UNDEFEVT ) {
284  os << separator << rhs.m_event_number;
285  separator = ",";
286  }
287 
288  if ( rhs.isTimeStamp() ) {
289  os << separator << "t:" << rhs.m_time_stamp;
290  if ( rhs.m_time_stamp_ns_offset != 0 ) {
291  os << "." << std::setfill( '0' ) << std::setw( 9 ) << rhs.m_time_stamp_ns_offset;
292  }
293  separator = ",";
294  }
295 
296  if ( rhs.isLumiEvent() || rhs.isRunLumi() ) {
297  os << separator << "l:" << rhs.m_lumi_block;
298  separator = ",";
299  }
300 
301  if ( rhs.m_bunch_crossing_id != 0 ) { os << separator << "b:" << rhs.m_bunch_crossing_id; }
302  os << "]";
303  return os;
304  }
305 };
306 
307 #endif // EVENTINFO_EVENTID_H
EventIDBase::event_number_t
uint64_t event_number_t
Definition: EventIDBase.h:69
EventIDBase::operator!=
friend bool operator!=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:146
EventIDBase::m_time_stamp_ns_offset
number_type m_time_stamp_ns_offset
time stamp ns - ns time offset for time_stamp, 32 bit unsigned
Definition: EventIDBase.h:202
EventIDBase::SortByRunLumi
static auto SortByRunLumi()
Definition: EventIDBase.h:177
EventIDRange
Event ID Range object. Holds two EventIDBases (start and stop)
Definition: EventIDRange.h:33
EventIDBase::m_run_number
number_type m_run_number
run number
Definition: EventIDBase.h:193
EventIDBase::lumi_block
number_type lumi_block() const
luminosity block identifier, 32 bit unsigned
Definition: EventIDBase.h:104
EventIDBase::m_bunch_crossing_id
number_type m_bunch_crossing_id
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:209
EventIDBase::set_time_stamp_ns_offset
void set_time_stamp_ns_offset(number_type timeStampNs)
set time stamp in ns
Definition: EventIDBase.h:130
EventIDBase::setLE
void setLE()
Definition: EventIDBase.h:189
EventIDBase::EventIDBase
EventIDBase()
Definition: EventIDBase.h:79
EventIDBase::RunLumi
@ RunLumi
Definition: EventIDBase.h:183
EventIDBase::bunch_crossing_id
number_type bunch_crossing_id() const
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:107
std::tuple
EventIDBase::UNDEFEVT
static const event_number_t UNDEFEVT
Definition: EventIDBase.h:72
std::setfill
T setfill(T... args)
details::add_deref
auto add_deref(Fun f)
Definition: EventIDBase.h:46
EventIDBase::UNDEFNUM
static const number_type UNDEFNUM
Definition: EventIDBase.h:71
EventIDBase::m_lumi_block
number_type m_lumi_block
luminosity block number: the number which uniquely tags a luminosity block within a run
Definition: EventIDBase.h:206
EventIDBase::~EventIDBase
virtual ~EventIDBase()=default
EventIDBase::operator<=
friend bool operator<=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:147
details::arg_helper< Ret(T::*)(Arg) const >::type
Arg type
Definition: EventIDBase.h:37
EventIDBase::operator>
friend bool operator>(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:145
std::tie
T tie(T... args)
EventIDBase::set_run_number
void set_run_number(number_type runNumber)
set run number
Definition: EventIDBase.h:110
EventIDBase::isValid
bool isValid() const
Definition: EventIDBase.h:157
EventIDBase::m_event_number
event_number_t m_event_number
event number
Definition: EventIDBase.h:196
details
Definition: AnyDataWrapper.h:19
EventIDBase::RunEvent
@ RunEvent
Definition: EventIDBase.h:183
std::ostream
STL class.
EventIDBase::operator<<
friend std::ostream & operator<<(std::ostream &os, const EventIDBase &rhs)
Extraction operators.
EventIDBase::LumiEvent
@ LumiEvent
Definition: EventIDBase.h:183
details::argument_t
typename arg_helper< lambda >::type argument_t
Definition: EventIDBase.h:43
EventIDBase::isLumiEvent
bool isLumiEvent() const
Definition: EventIDBase.h:155
EventIDBase::Type
Type
Definition: EventIDBase.h:183
EventIDBase::operator<
friend bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
compose.h
EventIDBase::number_type
unsigned int number_type
Definition: EventIDBase.h:68
details::make_cmp
auto make_cmp(Proj p, Cmp cmp={})
Definition: EventIDBase.h:51
EventIDBase::isRunEvent
bool isRunEvent() const
Definition: EventIDBase.h:153
EventIDBase::TimeStamp
@ TimeStamp
Definition: EventIDBase.h:183
std::min
T min(T... args)
EventIDBase::setRL
void setRL()
Definition: EventIDBase.h:190
EventIDBase::Invalid
@ Invalid
Definition: EventIDBase.h:183
EventIDBase::isTimeStamp
bool isTimeStamp() const
Definition: EventIDBase.h:154
compose
auto compose(lambda_ts &&... lambdas)
Definition: compose.h:46
EventIDBase::set_time_stamp
void set_time_stamp(number_type timeStamp)
set time stamp
Definition: EventIDBase.h:124
EventIDBase::time_stamp
number_type time_stamp() const
time stamp - posix time in seconds from 1970, 32 bit unsigned
Definition: EventIDBase.h:98
EventIDBase::run_number
number_type run_number() const
run number - 32 bit unsigned
Definition: EventIDBase.h:92
EventIDBase::SortByLumiEvent
static auto SortByLumiEvent()
Definition: EventIDBase.h:172
EventIDBase::operator>=
friend bool operator>=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:148
EventIDBase::set_bunch_crossing_id
void set_bunch_crossing_id(number_type bcid)
set bunch crossing ID
Definition: EventIDBase.h:140
EventIDBase::max
friend EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
EventIDBase::isRunLumi
bool isRunLumi() const
Definition: EventIDBase.h:156
EventIDBase::set_lumi_block
void set_lumi_block(number_type lumiBlock)
set luminosity block identifier
Definition: EventIDBase.h:133
EventIDBase::setRE
void setRE()
Definition: EventIDBase.h:187
EventIDBase
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:66
EventIDBase::SortByTimeStamp
static auto SortByTimeStamp()
Definition: EventIDBase.h:162
EventIDBase::operator==
friend bool operator==(const EventIDBase &lhs, const EventIDBase &rhs)
Comparison operators.
EventIDBase::time_stamp_ns_offset
number_type time_stamp_ns_offset() const
time stamp ns - ns time offset for time_stamp, 32 bit unsigned
Definition: EventIDBase.h:101
std::setw
T setw(T... args)
EventIDBase::setTS
void setTS()
Definition: EventIDBase.h:188
std::max
T max(T... args)
EventIDBase::min
friend EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
EventIDBase::event_number
event_number_t event_number() const
event number - 64 bit unsigned
Definition: EventIDBase.h:95
EventIDBase::set_event_number
void set_event_number(event_number_t eventNumber)
set event number
Definition: EventIDBase.h:117
EventIDBase::m_type
unsigned m_type
Definition: EventIDBase.h:185
EventIDBase::m_time_stamp
number_type m_time_stamp
posix time in seconds since 1970/01/01
Definition: EventIDBase.h:199
EventIDBase::SortByRunEvent
static auto SortByRunEvent()
Definition: EventIDBase.h:167
details::arg_helper
Definition: EventIDBase.h:34