Project
Loading...
Searching...
No Matches
MCEventLabel.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef ALICEO2_MCVEVENTLABEL_H
13#define ALICEO2_MCVEVENTLABEL_H
14
15#include "GPUCommonRtypes.h"
16#include <cmath>
17#include <cassert>
18#include <string>
19
20namespace o2
21{
22// Composed Label to encode MC event and the source (file) + weight
23
25{
26 private:
27 static constexpr uint32_t Dummy = 0xffffffff;
28 uint32_t mLabel = Dummy;
29
30 public:
31 static constexpr int nbitsEvID = 16; // number of bits reserved for MC event ID
32 static constexpr int nbitsSrcID = 7; // number of bits reserved for MC source ID
33 static constexpr int nbitsCorrW = sizeof(mLabel) * 8 - nbitsEvID - nbitsSrcID - 1;
34 static constexpr uint32_t NotSet = (0x1 << (nbitsEvID + nbitsSrcID)) - 1;
35
36 // Mask to extract MC event ID
37 static constexpr uint32_t MaskEvID = (0x1 << nbitsEvID) - 1;
38 // Mask to extract MC source ID
39 static constexpr uint32_t MaskSrcID = (0x1 << nbitsSrcID) - 1;
40 // Mask to extract MC source and event ID only
41 static constexpr uint32_t MaskSrcEvID = (MaskSrcID << nbitsEvID) | MaskEvID;
42 // Mask to extract MC correct contribution weight
43 static constexpr uint32_t MaskCorrW = (0x1 << nbitsCorrW) - 1;
44 static constexpr float WeightNorm = 1. / float(MaskCorrW);
45
46 MCEventLabel(int evID, int srcID, float corrw = 1.0) { set(evID, srcID, corrw); }
47 MCEventLabel() = default;
48 ~MCEventLabel() = default;
49
50 // check if label was assigned
51 bool isSet() const { return (mLabel & NotSet) != NotSet; }
52 // check if label was not assigned
53 bool isEmpty() const { return (mLabel & NotSet) == NotSet; }
54
55 // conversion operator
56 operator uint32_t() const { return mLabel; }
57 // allow to retrieve bare label
58 uint32_t getRawValue() const { return mLabel; }
59
60 // get only combined identifier, discarding weight info
61 uint32_t getIDOnly() const { return mLabel & MaskSrcEvID; }
62
63 // compare
64 bool compare(const MCEventLabel& other, bool strict = false) const
65 {
66 return strict ? (getRawValue() == other.getRawValue()) : (getIDOnly() == other.getIDOnly());
67 }
68
69 // comparison operator, compares only label, not eventual weight or correctness info
70 bool operator==(const MCEventLabel& other) const { return compare(other); }
71
72 // invalidate
73 void unset() { mLabel = NotSet; }
74
76 void set(int evID, int srcID, float corrW)
77 {
78 uint32_t iw = static_cast<uint32_t>(std::round(corrW * MaskCorrW));
79 assert(iw <= MaskCorrW);
80 mLabel = (iw << (nbitsEvID + nbitsSrcID)) | ((MaskSrcID & static_cast<uint32_t>(srcID)) << nbitsEvID) | (MaskEvID & static_cast<uint32_t>(evID));
81 }
82 void setCorrWeight(float corrW)
83 {
84 uint32_t iw = static_cast<uint32_t>(std::round(corrW * MaskCorrW));
85 assert(iw <= MaskCorrW);
86 mLabel = (mLabel & ((MaskSrcID << nbitsEvID) | MaskEvID)) | (iw << (nbitsEvID + nbitsSrcID));
87 }
88
89 int getEventID() const
90 {
91 auto res = mLabel & MaskEvID;
92 return res <= MaxEventID() ? res : -1;
93 }
94
95 int getSourceID() const
96 {
97 auto res = (mLabel >> nbitsEvID) & MaskSrcID;
98 return res <= MaxSourceID() ? res : -1;
99 }
100
101 float getCorrWeight() const { return ((mLabel >> (nbitsEvID + nbitsSrcID)) & MaskCorrW) * WeightNorm; }
102
103 void get(int& evID, int& srcID, float& corrW)
104 {
106 evID = getEventID();
107 srcID = getSourceID();
108 corrW = getCorrWeight();
109 }
110
111 void print() const;
112 std::string asString() const;
113
114 static constexpr uint32_t MaxSourceID() { return MaskSrcID - 1; }
115 static constexpr uint32_t MaxEventID() { return MaskEvID - 1; }
116 static constexpr float WeightPrecision() { return WeightNorm; }
118};
119} // namespace o2
120
121std::ostream& operator<<(std::ostream& os, const o2::MCEventLabel& c);
122
123namespace std
124{
125// defining std::hash for MCEventLabel in order to be used with unordered_maps
126template <>
127struct hash<o2::MCEventLabel> {
128 public:
129 size_t operator()(o2::MCEventLabel const& label) const
130 {
131 return static_cast<uint32_t>(label);
132 }
133};
134} // namespace std
135
136#endif
std::ostream & operator<<(std::ostream &os, const o2::MCEventLabel &c)
uint32_t res
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
ClassDefNV(MCEventLabel, 1)
static constexpr int nbitsSrcID
static constexpr uint32_t MaskCorrW
bool isSet() const
void set(int evID, int srcID, float corrW)
compose label
void get(int &evID, int &srcID, float &corrW)
static constexpr int nbitsCorrW
static constexpr uint32_t MaskSrcID
static constexpr uint32_t NotSet
static constexpr int nbitsEvID
bool compare(const MCEventLabel &other, bool strict=false) const
void print() const
static constexpr uint32_t MaxEventID()
uint32_t getIDOnly() const
~MCEventLabel()=default
static constexpr uint32_t MaskSrcEvID
std::string asString() const
static constexpr float WeightNorm
static constexpr float WeightPrecision()
uint32_t getRawValue() const
static constexpr uint32_t MaxSourceID()
MCEventLabel(int evID, int srcID, float corrw=1.0)
void setCorrWeight(float corrW)
float getCorrWeight() const
bool isEmpty() const
static constexpr uint32_t MaskEvID
bool operator==(const MCEventLabel &other) const
int getSourceID() const
int getEventID() const
MCEventLabel()=default
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.
size_t operator()(o2::MCEventLabel const &label) const
VectorOfTObjectPtrs other