The Gaudi Framework  v30r3 (a5ef0a68)
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 {
24  template <typename lambda>
25  struct arg_helper : public arg_helper<decltype( &lambda::operator() )> {
26  };
27  template <typename T, typename Ret, typename Arg>
28  struct arg_helper<Ret ( T::* )( Arg ) const> {
29  using type = Arg;
30  };
31 
32  // given a unary lambda whose argument is of type Arg_t,
33  // argument_t<lambda> will be equal to Arg_t
34  template <typename lambda>
36 
37  template <typename Fun>
38  auto add_deref( Fun f )
39  {
40  return compose( f, [=]( auto*... p ) { return f( *p... ); } );
41  }
42 
43  template <typename Proj, typename Cmp = std::greater<>>
44  auto make_cmp( Proj p, Cmp cmp = {} )
45  {
46  static_assert( std::is_reference<argument_t<Proj>>::value, "must be a reference" );
47  static_assert( std::is_const<std::remove_reference_t<argument_t<Proj>>>::value, "must be const" );
48  return [=]( argument_t<Proj> lhs, argument_t<Proj> rhs ) { return cmp( p( lhs ), p( rhs ) ); };
49  }
50 }
51 
61 {
62 public:
63  typedef unsigned int number_type;
65 
66  static const number_type UNDEFNUM;
67  static const event_number_t UNDEFEVT;
68 
69 public:
71 
73  EventIDBase( number_type run_number, event_number_t event_number, number_type time_stamp = UNDEFNUM,
74  number_type time_stamp_ns_offset = 0, number_type lumi_block = UNDEFNUM,
75  number_type bunch_crossing_id = 0 );
76  // Use default copy constructor.
77  virtual ~EventIDBase() = default;
79 
81  number_type run_number() const { return m_run_number; }
82 
84  event_number_t event_number() const { return m_event_number; }
85 
87  number_type time_stamp() const { return m_time_stamp; }
88 
90  number_type time_stamp_ns_offset() const { return m_time_stamp_ns_offset; }
91 
93  number_type lumi_block() const { return m_lumi_block; }
94 
96  number_type bunch_crossing_id() const { return m_bunch_crossing_id; }
97 
99  void set_run_number( number_type runNumber )
100  {
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  {
109  m_event_number = eventNumber;
110  if ( m_run_number != UNDEFNUM ) setRE();
111  if ( m_lumi_block != UNDEFNUM ) setLE();
112  }
113 
115  void set_time_stamp( number_type timeStamp )
116  {
117  m_time_stamp = timeStamp;
118  setTS();
119  }
120 
122  void set_time_stamp_ns_offset( number_type timeStampNs ) { m_time_stamp_ns_offset = timeStampNs; }
123 
125  void set_lumi_block( number_type lumiBlock )
126  {
127  m_lumi_block = lumiBlock;
128  if ( m_run_number != UNDEFNUM ) setRL();
129  if ( m_event_number != UNDEFEVT ) setLE();
130  }
131 
133  void set_bunch_crossing_id( number_type bcid ) { m_bunch_crossing_id = bcid; }
134 
136  friend bool operator==( const EventIDBase& lhs, const EventIDBase& rhs );
137  friend bool operator<( const EventIDBase& lhs, const EventIDBase& rhs );
138  friend bool operator>( const EventIDBase& lhs, const EventIDBase& rhs ) { return rhs < lhs; }
139  friend bool operator!=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs == rhs ); }
140  friend bool operator<=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs > rhs ); }
141  friend bool operator>=( const EventIDBase& lhs, const EventIDBase& rhs ) { return !( lhs < 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()
153  {
155  []( const EventIDBase& e ) { return std::tie( e.m_time_stamp, e.m_time_stamp_ns_offset ); } ) );
156  };
157 
158  static auto SortByRunEvent()
159  {
160  return details::add_deref(
161  details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_event_number ); } ) );
162  };
163 
164  static auto SortByLumiEvent()
165  {
166  return details::add_deref(
167  details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_lumi_block, e.m_event_number ); } ) );
168  };
169 
170  static auto SortByRunLumi()
171  {
172  return details::add_deref(
173  details::make_cmp( []( const EventIDBase& e ) { return std::tie( e.m_run_number, e.m_lumi_block ); } ) );
174  };
175 
176 private:
177  enum Type { Invalid = 0, RunEvent = 1 << 1, TimeStamp = 1 << 2, LumiEvent = 1 << 3, RunLumi = 1 << 4 };
178 
179  unsigned m_type{Invalid};
180 
181  void setRE() { m_type |= RunEvent; }
182  void setTS() { m_type |= TimeStamp; }
183  void setLE() { m_type |= LumiEvent; }
184  void setRL() { m_type |= RunLumi; }
185 
187  number_type m_run_number{UNDEFNUM};
188 
190  event_number_t m_event_number{UNDEFEVT};
191 
193  number_type m_time_stamp{UNDEFNUM};
194 
196  number_type m_time_stamp_ns_offset{UNDEFNUM};
197 
200  number_type m_lumi_block{UNDEFNUM};
201 
203  number_type m_bunch_crossing_id{UNDEFNUM};
204 };
205 
206 inline bool operator<( const EventIDBase& lhs, const EventIDBase& rhs )
207 {
208  // first try ordering by timestamp if both are non-zero
209  // then try ordering by run/lumi/event
210  // this assumes that both EventIDBase have the same set of values defined.
211 
212  if ( lhs.isTimeStamp() && rhs.isTimeStamp() ) {
213  return lhs.m_time_stamp < rhs.m_time_stamp;
214  } else {
215  return std::tie( lhs.m_run_number, lhs.m_lumi_block, lhs.m_event_number ) <
217  }
218 }
219 
220 inline bool operator==( const EventIDBase& lhs, const EventIDBase& rhs )
221 {
222  // We assume that equality via run/event/lumi numbers is sufficient
223  return ( lhs.m_run_number == rhs.m_run_number && lhs.m_event_number == rhs.m_event_number &&
224  lhs.m_lumi_block == rhs.m_lumi_block );
225 }
226 
228 {
229  if ( rhs.m_type == EventIDBase::Invalid ) return os << "[INVALID]";
230 
231  const char* separator = "";
232  os << "[";
233  if ( rhs.m_run_number != EventIDBase::UNDEFNUM ) {
234  os << rhs.m_run_number;
235  separator = ",";
236  }
237 
238  if ( rhs.m_event_number != EventIDBase::UNDEFEVT ) {
239  os << separator << rhs.m_event_number;
240  separator = ",";
241  }
242 
243  if ( rhs.isTimeStamp() ) {
244  os << separator << "t:" << rhs.m_time_stamp;
245  if ( rhs.m_time_stamp_ns_offset != 0 ) {
246  os << "." << std::setfill( '0' ) << std::setw( 9 ) << rhs.m_time_stamp_ns_offset;
247  }
248  separator = ",";
249  }
250 
251  if ( rhs.isLumiEvent() || rhs.isRunLumi() ) {
252  os << separator << "l:" << rhs.m_lumi_block;
253  separator = ",";
254  }
255 
256  if ( rhs.m_bunch_crossing_id != 0 ) {
257  os << separator << "b:" << rhs.m_bunch_crossing_id;
258  }
259  os << "]";
260  return os;
261 }
262 
263 #endif // EVENTINFO_EVENTID_H
void set_time_stamp(number_type timeStamp)
set time stamp
Definition: EventIDBase.h:115
static const number_type UNDEFNUM
Definition: EventIDBase.h:66
std::ostream & operator<<(std::ostream &os, const EventIDBase &rhs)
Definition: EventIDBase.h:227
void set_time_stamp_ns_offset(number_type timeStampNs)
set time stamp in ns
Definition: EventIDBase.h:122
void setLE()
Definition: EventIDBase.h:183
unsigned m_type
Definition: EventIDBase.h:179
static auto SortByLumiEvent()
Definition: EventIDBase.h:164
void set_lumi_block(number_type lumiBlock)
set luminosity block identifier
Definition: EventIDBase.h:125
void set_run_number(number_type runNumber)
set run number
Definition: EventIDBase.h:99
number_type bunch_crossing_id() const
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:96
static auto SortByRunEvent()
Definition: EventIDBase.h:158
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:196
bool isRunLumi() const
Definition: EventIDBase.h:146
friend bool operator<=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:140
void set_bunch_crossing_id(number_type bcid)
set bunch crossing ID
Definition: EventIDBase.h:133
auto add_deref(Fun f)
Definition: EventIDBase.h:38
unsigned long long uint64_t
Definition: instrset.h:143
number_type m_bunch_crossing_id
bunch crossing ID, 32 bit unsigned
Definition: EventIDBase.h:203
void setRE()
Definition: EventIDBase.h:181
number_type lumi_block() const
luminosity block identifier, 32 bit unsigned
Definition: EventIDBase.h:93
event_number_t m_event_number
event number
Definition: EventIDBase.h:190
number_type time_stamp() const
time stamp - posix time in seconds from 1970, 32 bit unsigned
Definition: EventIDBase.h:87
void setRL()
Definition: EventIDBase.h:184
T setw(T...args)
number_type m_time_stamp
posix time in seconds since 1970/01/01
Definition: EventIDBase.h:193
number_type m_lumi_block
luminosity block number: the number which uniquely tags a luminosity block within a run ...
Definition: EventIDBase.h:200
bool isValid() const
Definition: EventIDBase.h:147
typename arg_helper< lambda >::type argument_t
Definition: EventIDBase.h:35
friend bool operator!=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:139
T setfill(T...args)
number_type m_run_number
run number
Definition: EventIDBase.h:187
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:81
bool operator==(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:220
bool isRunEvent() const
Definition: EventIDBase.h:143
friend bool operator>(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:138
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:90
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:206
static auto SortByRunLumi()
Definition: EventIDBase.h:170
uint64_t event_number_t
Definition: EventIDBase.h:64
bool isLumiEvent() const
Definition: EventIDBase.h:145
auto make_cmp(Proj p, Cmp cmp={})
Definition: EventIDBase.h:44
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:60
auto compose(lambda_ts &&...lambdas)
Definition: compose.h:77
unsigned int number_type
Definition: EventIDBase.h:63
friend bool operator>=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:141
STL class.
static const event_number_t UNDEFEVT
Definition: EventIDBase.h:67
void setTS()
Definition: EventIDBase.h:182
event_number_t event_number() const
event number - 64 bit unsigned
Definition: EventIDBase.h:84