Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
EventIDBase.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_EVENTIDBASE_H
2 #define GAUDIKERNEL_EVENTIDBASE_H 1
3 
16 #include <cstdint>
17 #include <iomanip>
18 #include <iostream>
19 #include <tuple>
20 
21 #include "GaudiKernel/compose.h"
22 namespace details {
23  template <typename lambda>
24  struct arg_helper : public arg_helper<decltype( &lambda::operator() )> {};
25  template <typename T, typename Ret, typename Arg>
26  struct arg_helper<Ret ( T::* )( Arg ) const> {
27  using type = Arg;
28  };
29 
30  // given a unary lambda whose argument is of type Arg_t,
31  // argument_t<lambda> will be equal to Arg_t
32  template <typename lambda>
34 
35  template <typename Fun>
36  auto add_deref( Fun f ) {
37  return compose( f, [=]( auto*... p ) { return f( *p... ); } );
38  }
39 
40  template <typename Proj, typename Cmp = std::greater<>>
41  auto make_cmp( Proj p, Cmp cmp = {} ) {
42  static_assert( std::is_reference<argument_t<Proj>>::value, "must be a reference" );
43  static_assert( std::is_const<std::remove_reference_t<argument_t<Proj>>>::value, "must be const" );
44  return [=]( argument_t<Proj> lhs, argument_t<Proj> rhs ) { return cmp( p( lhs ), p( rhs ) ); };
45  }
46 } // namespace details
47 
56 class EventIDBase {
57 public:
58  typedef unsigned int number_type;
60 
61  static const number_type UNDEFNUM;
62  static const event_number_t UNDEFEVT;
63 
64  friend class EventIDRange;
65 
66 public:
68 
70  EventIDBase( number_type run_number, event_number_t event_number, number_type time_stamp = UNDEFNUM,
71  number_type time_stamp_ns_offset = 0, number_type lumi_block = UNDEFNUM,
72  number_type bunch_crossing_id = 0 );
73 
75  std::tuple<number_type, number_type> time_stamp, number_type bunch_crossing_id );
76 
77  // Use default copy constructor.
78  virtual ~EventIDBase() = default;
80 
82  number_type run_number() const { return m_run_number; }
83 
85  event_number_t event_number() const { return m_event_number; }
86 
88  number_type time_stamp() const { return m_time_stamp; }
89 
91  number_type time_stamp_ns_offset() const { return m_time_stamp_ns_offset; }
92 
94  number_type lumi_block() const { return m_lumi_block; }
95 
97  number_type bunch_crossing_id() const { return m_bunch_crossing_id; }
98 
100  void set_run_number( number_type runNumber ) {
101  m_run_number = runNumber;
102  if ( m_event_number != UNDEFEVT ) setRE();
103  if ( m_lumi_block != UNDEFNUM ) setRL();
104  }
105 
107  void set_event_number( event_number_t eventNumber ) {
108  m_event_number = eventNumber;
109  if ( m_run_number != UNDEFNUM ) setRE();
110  if ( m_lumi_block != UNDEFNUM ) setLE();
111  }
112 
114  void set_time_stamp( number_type timeStamp ) {
115  m_time_stamp = timeStamp;
116  setTS();
117  }
118 
120  void set_time_stamp_ns_offset( number_type timeStampNs ) { m_time_stamp_ns_offset = timeStampNs; }
121 
123  void set_lumi_block( number_type lumiBlock ) {
124  m_lumi_block = lumiBlock;
125  if ( m_run_number != UNDEFNUM ) setRL();
126  if ( m_event_number != UNDEFEVT ) setLE();
127  }
128 
130  void set_bunch_crossing_id( number_type bcid ) { m_bunch_crossing_id = bcid; }
131 
133  friend bool operator==( const EventIDBase& lhs, const EventIDBase& rhs );
134  friend bool operator<( const EventIDBase& lhs, const EventIDBase& rhs );
135  friend bool operator>( const EventIDBase& lhs, const EventIDBase& rhs ) { return rhs < lhs; }
136  friend bool operator!=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs == rhs ); }
137  friend bool operator<=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs > rhs ); }
138  friend bool operator>=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs < rhs ); }
139 
140  friend EventIDBase min( const EventIDBase& lhs, const EventIDBase& rhs );
141  friend EventIDBase max( const EventIDBase& lhs, const EventIDBase& rhs );
142 
143  bool isRunEvent() const { return m_type & RunEvent; }
144  bool isTimeStamp() const { return m_type & TimeStamp; }
145  bool isLumiEvent() const { return m_type & LumiEvent; }
146  bool isRunLumi() const { return m_type & RunLumi; }
147  bool isValid() const { return m_type != Invalid; }
148 
150  friend std::ostream& operator<<( std::ostream& os, const EventIDBase& rhs );
151 
152  static auto SortByTimeStamp() {
154  []( const EventIDBase& e ) { return std::tie( e.m_time_stamp, e.m_time_stamp_ns_offset ); } ) );
155  };
156 
157  static auto SortByRunEvent() {
159  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_event_number ); } ) );
160  };
161 
162  static auto SortByLumiEvent() {
164  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_lumi_block, e.m_event_number ); } ) );
165  };
166 
167  static auto SortByRunLumi() {
169  ::details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_lumi_block ); } ) );
170  };
171 
172 private:
173  enum Type { Invalid = 0, RunEvent = 1 << 1, TimeStamp = 1 << 2, LumiEvent = 1 << 3, RunLumi = 1 << 4 };
174 
175  unsigned m_type{Invalid};
176 
177  void setRE() { m_type |= RunEvent; }
178  void setTS() { m_type |= TimeStamp; }
179  void setLE() { m_type |= LumiEvent; }
180  void setRL() { m_type |= RunLumi; }
181 
183  number_type m_run_number{UNDEFNUM};
184 
186  event_number_t m_event_number{UNDEFEVT};
187 
189  number_type m_time_stamp{UNDEFNUM};
190 
192  number_type m_time_stamp_ns_offset{UNDEFNUM};
193 
196  number_type m_lumi_block{UNDEFNUM};
197 
199  number_type m_bunch_crossing_id{UNDEFNUM};
200 };
201 
202 inline EventIDBase min( const EventIDBase& lhs, const EventIDBase& rhs ) {
203 
204  //"min" is easy b/c the numbers denoting invalidty for TS or Run/Event/LB are the
205  // largest possible numbers, so naturally larger than any valid number
206 
211  lhs.bunch_crossing_id() // bcid doesn't really matter here
212  );
213 }
214 
215 inline EventIDBase max( const EventIDBase& lhs, const EventIDBase& rhs ) {
216 
217  //"max" is much trickier because we need to handle invalid number explicilty by
218  // checking if a EventIDBase is TS or Run/Lumi
219 
222 
223  if ( lhs.isTimeStamp() && rhs.isTimeStamp() ) { // both time-stamp, compare them
224  time_stamp = std::max( std::tie( lhs.m_time_stamp, lhs.m_time_stamp_ns_offset ),
226  } else if ( lhs.isTimeStamp() ) { // only lhs time-stamp: Use it
227  time_stamp = std::tie( lhs.m_time_stamp, lhs.m_time_stamp_ns_offset );
228  } else { // otherwise use rhs time-stamp which might be UNDEFNUM (in case both input values are Run-Lumi only)
229  time_stamp = std::tie( rhs.m_time_stamp, rhs.m_time_stamp_ns_offset );
230  }
231 
232  if ( lhs.isRunLumi() && rhs.isRunLumi() ) { // both run-lumi, compare them
233  run_lumi_ev = std::max( std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number ),
235 
236  } else if ( lhs.isRunLumi() ) { // only lhs run-lumi: Use it
237  run_lumi_ev = std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number );
238  } else { // otherwise use rhs run-lumi which might be UNDEFNUM (in case both input values are TS-only)
239  run_lumi_ev = std::tie( rhs.m_run_number, rhs.m_lumi_block, rhs.m_event_number );
240  }
241 
242  return EventIDBase( run_lumi_ev, time_stamp, lhs.bunch_crossing_id() );
243 }
244 
245 inline bool operator<( const EventIDBase& lhs, const EventIDBase& rhs ) {
246  // first try ordering by timestamp if both are non-zero
247  // then try ordering by run/lumi/event
248  // this assumes that both EventIDBase have the same set of values defined.
249 
250  if ( lhs.isTimeStamp() && rhs.isTimeStamp() ) {
251  return lhs.m_time_stamp < rhs.m_time_stamp;
252  } else {
253  return std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number ) <
255  }
256 }
257 
258 inline bool operator==( const EventIDBase& lhs, const EventIDBase& rhs ) {
259  // We assume that equality via run/event/lumi numbers is sufficient
260  return ( lhs.m_run_number == rhs.m_run_number && lhs.m_event_number == rhs.m_event_number &&
261  lhs.m_lumi_block == rhs.m_lumi_block );
262 }
263 
264 inline std::ostream& operator<<( std::ostream& os, const EventIDBase& rhs ) {
265  if ( rhs.m_type == EventIDBase::Invalid ) return os << "[INVALID]";
266 
267  const char* separator = "";
268  os << "[";
269  if ( rhs.m_run_number != EventIDBase::UNDEFNUM ) {
270  os << rhs.m_run_number;
271  separator = ",";
272  }
273 
274  if ( rhs.m_event_number != EventIDBase::UNDEFEVT ) {
275  os << separator << rhs.m_event_number;
276  separator = ",";
277  }
278 
279  if ( rhs.isTimeStamp() ) {
280  os << separator << "t:" << rhs.m_time_stamp;
281  if ( rhs.m_time_stamp_ns_offset != 0 ) {
282  os << "." << std::setfill( '0' ) << std::setw( 9 ) << rhs.m_time_stamp_ns_offset;
283  }
284  separator = ",";
285  }
286 
287  if ( rhs.isLumiEvent() || rhs.isRunLumi() ) {
288  os << separator << "l:" << rhs.m_lumi_block;
289  separator = ",";
290  }
291 
292  if ( rhs.m_bunch_crossing_id != 0 ) { os << separator << "b:" << rhs.m_bunch_crossing_id; }
293  os << "]";
294  return os;
295 }
296 
297 #endif // EVENTINFO_EVENTID_H
void set_time_stamp(number_type timeStamp)
set time stamp
Definition: EventIDBase.h:114
static const number_type UNDEFNUM
Definition: EventIDBase.h:61
std::ostream & operator<<(std::ostream &os, const EventIDBase &rhs)
Definition: EventIDBase.h:264
void set_time_stamp_ns_offset(number_type timeStampNs)
set time stamp in ns
Definition: EventIDBase.h:120
void setLE()
Definition: EventIDBase.h:179
unsigned m_type
Definition: EventIDBase.h:175
static auto SortByLumiEvent()
Definition: EventIDBase.h:162
void set_lumi_block(number_type lumiBlock)
set luminosity block identifier
Definition: EventIDBase.h:123
void set_run_number(number_type runNumber)
set run number
Definition: EventIDBase.h:100
number_type bunch_crossing_id() const
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:97
static auto SortByRunEvent()
Definition: EventIDBase.h:157
T tie(T...args)
static auto SortByTimeStamp()
Definition: EventIDBase.h:152
number_type m_time_stamp_ns_offset
time stamp ns - ns time offset for time_stamp, 32 bit unsigned
Definition: EventIDBase.h:192
bool isRunLumi() const
Definition: EventIDBase.h:146
friend bool operator<=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:137
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:202
void set_bunch_crossing_id(number_type bcid)
set bunch crossing ID
Definition: EventIDBase.h:130
auto add_deref(Fun f)
Definition: EventIDBase.h:36
unsigned long long uint64_t
Definition: instrset.h:143
number_type m_bunch_crossing_id
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:199
void setRE()
Definition: EventIDBase.h:177
number_type lumi_block() const
luminosity block identifier, 32 bit unsigned
Definition: EventIDBase.h:94
event_number_t m_event_number
event number
Definition: EventIDBase.h:186
number_type time_stamp() const
time stamp - posix time in seconds from 1970, 32 bit unsigned
Definition: EventIDBase.h:88
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:215
void setRL()
Definition: EventIDBase.h:180
T setw(T...args)
number_type m_time_stamp
posix time in seconds since 1970/01/01
Definition: EventIDBase.h:189
T min(T...args)
number_type m_lumi_block
luminosity block number: the number which uniquely tags a luminosity block within a run ...
Definition: EventIDBase.h:196
bool isValid() const
Definition: EventIDBase.h:147
typename arg_helper< lambda >::type argument_t
Definition: EventIDBase.h:33
Event ID Range object.
Definition: EventIDRange.h:23
friend bool operator!=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:136
T setfill(T...args)
number_type m_run_number
run number
Definition: EventIDBase.h:183
void set_event_number(event_number_t eventNumber)
set event number
Definition: EventIDBase.h:107
number_type run_number() const
run number - 32 bit unsigned
Definition: EventIDBase.h:82
bool operator==(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:258
T max(T...args)
bool isRunEvent() const
Definition: EventIDBase.h:143
friend bool operator>(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:135
bool isTimeStamp() const
Definition: EventIDBase.h:144
number_type time_stamp_ns_offset() const
time stamp ns - ns time offset for time_stamp, 32 bit unsigned
Definition: EventIDBase.h:91
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:245
static auto SortByRunLumi()
Definition: EventIDBase.h:167
uint64_t event_number_t
Definition: EventIDBase.h:59
bool isLumiEvent() const
Definition: EventIDBase.h:145
auto make_cmp(Proj p, Cmp cmp={})
Definition: EventIDBase.h:41
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:56
auto compose(lambda_ts &&...lambdas)
Definition: compose.h:70
unsigned int number_type
Definition: EventIDBase.h:58
friend bool operator>=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:138
STL class.
static const event_number_t UNDEFEVT
Definition: EventIDBase.h:62
void setTS()
Definition: EventIDBase.h:178
event_number_t event_number() const
event number - 64 bit unsigned
Definition: EventIDBase.h:85