-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathInteractionRecord.h
More file actions
385 lines (328 loc) · 9.75 KB
/
InteractionRecord.h
File metadata and controls
385 lines (328 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @brief Interaction record encoding BC, orbit, time
#ifndef ALICEO2_INTERACTIONRECORD_H
#define ALICEO2_INTERACTIONRECORD_H
#include "GPUCommonRtypes.h"
#ifndef GPUCA_GPUCODE_DEVICE
#include <iosfwd>
#include <cstdint>
#endif
#include <cmath>
#include "CommonConstants/LHCConstants.h"
namespace o2
{
struct InteractionRecord {
// information about bunch crossing and orbit
static constexpr uint16_t DummyBC = 0xffff;
static constexpr uint32_t DummyOrbit = 0xffffffff;
static constexpr double DummyTime = DummyBC * o2::constants::lhc::LHCBunchSpacingNS + DummyOrbit * o2::constants::lhc::LHCOrbitNS;
static constexpr int64_t MaxGlobalBCs = (int64_t(DummyOrbit) * o2::constants::lhc::LHCMaxBunches) + (o2::constants::lhc::LHCMaxBunches - 1);
static constexpr InteractionRecord getIRMaxBC() { return {o2::constants::lhc::LHCMaxBunches - 1, DummyOrbit}; }
uint16_t bc = DummyBC; ///< bunch crossing ID of interaction
uint32_t orbit = DummyOrbit; ///< LHC orbit
constexpr InteractionRecord() = default;
InteractionRecord(double tNS)
{
setFromNS(tNS);
}
constexpr InteractionRecord(uint16_t b, uint32_t orb) : bc(b), orbit(orb)
{
}
InteractionRecord(const InteractionRecord& src) = default;
InteractionRecord& operator=(const InteractionRecord& src) = default;
void clear()
{
bc = 0xffff;
orbit = 0xffffffff;
}
bool isDummy() const
{
return bc > o2::constants::lhc::LHCMaxBunches;
}
void setFromNS(double ns)
{
bc = ns2bc(ns, orbit);
}
static double bc2ns(int bc, unsigned int orbit)
{
return bc * o2::constants::lhc::LHCBunchSpacingNS + orbit * o2::constants::lhc::LHCOrbitNS;
}
static int ns2bc(double ns, unsigned int& orb)
{
long nb = std::round(ns / o2::constants::lhc::LHCBunchSpacingNS);
orb = nb / o2::constants::lhc::LHCMaxBunches;
return nb % o2::constants::lhc::LHCMaxBunches;
}
double bc2ns() const
{
return bc2ns(bc, orbit);
}
bool operator==(const InteractionRecord& other) const
{
return (bc == other.bc) && (orbit == other.orbit);
}
bool operator!=(const InteractionRecord& other) const
{
return (bc != other.bc) || (orbit != other.orbit);
}
int64_t differenceInBC(const InteractionRecord& other) const
{
// return difference in bunch-crossings
int64_t diffBC = int(bc) - other.bc;
if (orbit != other.orbit) {
diffBC += (int64_t(orbit) - other.orbit) * o2::constants::lhc::LHCMaxBunches;
}
return diffBC;
}
float differenceInBCNS(const InteractionRecord& other) const
{
// return difference in bunch-crossings in ns
return differenceInBC(other) * o2::constants::lhc::LHCBunchSpacingNS;
}
float differenceInBCMUS(const InteractionRecord& other) const
{
// return difference in bunch-crossings in us
return differenceInBC(other) * o2::constants::lhc::LHCBunchSpacingMUS;
}
int64_t toLong() const
{
// return as single long number
return (int64_t(orbit) * o2::constants::lhc::LHCMaxBunches) + bc;
}
void setFromLong(int64_t l)
{
// set from long BC counter
bc = l % o2::constants::lhc::LHCMaxBunches;
orbit = l / o2::constants::lhc::LHCMaxBunches;
}
static InteractionRecord long2IR(int64_t l)
{
// set from long BC counter
return {uint16_t(l % o2::constants::lhc::LHCMaxBunches), uint32_t(l / o2::constants::lhc::LHCMaxBunches)};
}
bool operator>(const InteractionRecord& other) const
{
return (orbit == other.orbit) ? (bc > other.bc) : (orbit > other.orbit);
}
bool operator>=(const InteractionRecord& other) const
{
return !((*this) < other);
}
bool operator<(const InteractionRecord& other) const
{
return (orbit == other.orbit) ? (bc < other.bc) : (orbit < other.orbit);
}
bool operator<=(const InteractionRecord& other) const
{
return !((*this) > other);
}
InteractionRecord operator--()
{
// prefix decrement operator
if (!bc--) {
orbit--;
bc = o2::constants::lhc::LHCMaxBunches - 1;
if (orbit == DummyOrbit) { // wrapped?
orbit = 0;
bc = 0;
}
}
return InteractionRecord(*this);
}
InteractionRecord operator--(int)
{
// postfix decrement operator, no check for orbit wrap
InteractionRecord tmp(*this);
if (!bc--) {
orbit--;
bc = o2::constants::lhc::LHCMaxBunches - 1;
if (orbit == DummyOrbit) { // wrapped?
orbit = 0;
bc = 0;
}
}
return tmp;
}
InteractionRecord operator++()
{
// prefix increment operator
if ((++bc) == o2::constants::lhc::LHCMaxBunches) {
orbit++;
bc = 0;
if (orbit == 0) { // wrapped?
orbit = DummyOrbit;
bc = o2::constants::lhc::LHCMaxBunches - 1;
}
}
return InteractionRecord(*this);
}
InteractionRecord operator++(int)
{
// postfix increment operator
InteractionRecord tmp(*this);
if ((++bc) == o2::constants::lhc::LHCMaxBunches) {
orbit++;
bc = 0;
if (orbit == 0) { // wrapped?
orbit = DummyOrbit;
bc = o2::constants::lhc::LHCMaxBunches - 1;
}
}
return tmp;
}
InteractionRecord& operator+=(int64_t dbc)
{
// bc self-addition operator, avoid wrapping
auto l = toLong();
if (dbc >= 0) {
if (MaxGlobalBCs - dbc < l) {
l = MaxGlobalBCs;
dbc = 0;
}
} else {
if (l < -dbc) {
l = 0;
dbc = 0;
}
}
l += dbc;
bc = l % o2::constants::lhc::LHCMaxBunches;
orbit = l / o2::constants::lhc::LHCMaxBunches;
return *this;
}
InteractionRecord& operator-=(int64_t dbc)
{
// bc self-subtraction operator
return operator+=(-dbc);
}
InteractionRecord& operator+=(const InteractionRecord& add)
{
// InteractionRecord self-addition operator
return operator+=(add.toLong());
}
InteractionRecord& operator-=(const InteractionRecord& add)
{
// InteractionRecord self-subtraction operator
return operator-=(add.toLong());
}
InteractionRecord operator+(int64_t dbc) const
{
// bc addition operator
InteractionRecord tmp(*this);
tmp += dbc;
return tmp;
}
InteractionRecord operator-(int64_t dbc) const
{
// bc subtraction operator
InteractionRecord tmp(*this);
tmp -= dbc;
return tmp;
}
InteractionRecord operator+(const InteractionRecord& add) const
{
// InteractionRecord addition operator, no check for orbit wrap
InteractionRecord tmp(*this);
tmp += add;
return tmp;
}
InteractionRecord operator-(const InteractionRecord& add) const
{
// InteractionRecord subtraction operator, no check for orbit wrap
InteractionRecord tmp(*this);
tmp -= add;
return tmp;
}
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
void print() const;
std::string asString() const;
friend std::ostream& operator<<(std::ostream& stream, InteractionRecord const& ir);
#endif
ClassDefNV(InteractionRecord, 3);
};
struct InteractionTimeRecord : public InteractionRecord {
double timeInBCNS = 0.; ///< time in NANOSECONDS relative to orbit/bc
InteractionTimeRecord() = default;
/// create from the interaction record and time in the bunch (in ns)
InteractionTimeRecord(const InteractionRecord& ir, double t_in_bc) : InteractionRecord(ir), timeInBCNS(t_in_bc)
{
}
/// create from the abs. (since orbit=0/bc=0) time in NS
InteractionTimeRecord(double tNS) : InteractionRecord(tNS)
{
timeInBCNS = tNS - bc2ns();
}
/// set the from the abs. (since orbit=0/bc=0) time in NS
void setFromNS(double tNS)
{
InteractionRecord::setFromNS(tNS);
timeInBCNS = tNS - bc2ns();
}
void clear()
{
InteractionRecord::clear();
timeInBCNS = 0.;
}
double getTimeOffsetWrtBC() const
{
return timeInBCNS;
}
/// get time in ns from orbit=0/bc=0
double getTimeNS() const
{
return timeInBCNS + bc2ns();
}
bool operator==(const InteractionTimeRecord& other) const
{
return this->InteractionRecord::operator==(other) && (timeInBCNS == other.timeInBCNS);
}
bool operator!=(const InteractionTimeRecord& other) const
{
return this->InteractionRecord::operator!=(other) || (timeInBCNS != other.timeInBCNS);
}
bool operator>(const InteractionTimeRecord& other) const
{
return (this->InteractionRecord::operator>(other)) || (this->InteractionRecord::operator==(other) && (timeInBCNS > other.timeInBCNS));
}
bool operator>=(const InteractionTimeRecord& other) const
{
return !((*this) < other);
}
bool operator<(const InteractionTimeRecord& other) const
{
return (this->InteractionRecord::operator<(other)) || (this->InteractionRecord::operator==(other) && (timeInBCNS < other.timeInBCNS));
}
bool operator<=(const InteractionTimeRecord& other) const
{
return !((*this) > other);
}
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
void print() const;
std::string asString() const;
friend std::ostream& operator<<(std::ostream& stream, InteractionTimeRecord const& ir);
#endif
ClassDefNV(InteractionTimeRecord, 1);
};
} // namespace o2
namespace std
{
// defining std::hash for InteractionRecord to be used with std containers
template <>
struct hash<o2::InteractionRecord> {
public:
size_t operator()(const o2::InteractionRecord& ir) const
{
return ir.toLong();
}
};
} // namespace std
#endif