Skip to content

Commit c00777e

Browse files
committed
GlobalTrackID sources masks and their manipulations
1 parent f3794b8 commit c00777e

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-23
lines changed

DataFormats/Detectors/Common/include/DetectorsCommonDataFormats/DetID.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class DetID
8080
static constexpr int nDetectors = Last + 1; ///< number of defined detectors
8181

8282
static constexpr std::string_view NONE{"none"}; ///< keywork for no-detector
83+
static constexpr std::string_view ALL{"all"}; ///< keywork for all detectors
8384

8485
typedef std::bitset<nDetectors> mask_t;
8586

@@ -112,7 +113,7 @@ class DetID
112113
// detector masks from any non-alpha-num delimiter-separated list (empty if NONE is supplied)
113114
static mask_t getMask(const std::string_view detList);
114115

115-
static std::string getNames(mask_t mask);
116+
static std::string getNames(mask_t mask, char delimiter = ',');
116117

117118
// we need default c-tor only for root persistency, code must use c-tor with argument
118119
DetID() : mID(First) {}

DataFormats/Detectors/Common/src/DetID.cxx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,27 @@ constexpr int DetID::nDetectors;
3939
DetID::mask_t DetID::getMask(const std::string_view detList)
4040
{
4141
DetID::mask_t mask;
42-
std::string ss(detList);
43-
auto pos = ss.find(NONE);
44-
if (pos != std::string::npos) {
45-
ss.replace(pos, NONE.size(), "");
46-
} else {
42+
std::string ss(detList), sname{};
43+
if (ss.find(NONE) != std::string::npos) {
44+
return mask;
45+
}
46+
if (ss.find(ALL) != std::string::npos) {
47+
mask.set();
48+
return mask;
49+
}
50+
std::replace(ss.begin(), ss.end(), ' ', ',');
51+
std::stringstream sss(ss);
52+
while (getline(sss, sname, ',')) {
4753
for (auto id = DetID::First; id <= DetID::Last; id++) {
48-
auto pos = ss.find(DetID::getName(id));
49-
if (pos != std::string::npos) {
54+
if (sname == getName(id)) {
5055
mask.set(id);
51-
ss.replace(pos, strlen(DetID::getName(id)), "");
56+
sname = "";
57+
break;
5258
}
5359
}
54-
}
55-
// make sure that no names are left in the input strings
56-
if (std::count_if(ss.begin(), ss.end(), [](unsigned char c) { return std::isalnum(c); })) {
57-
LOG(ERROR) << "Ill-formed detectors list " << std::string(detList);
58-
throw std::runtime_error("Ill-formed detectors list");
60+
if (!sname.empty()) {
61+
throw std::runtime_error(fmt::format("Wrong entry {:s} in detectors list {:s}", sname, detList));
62+
}
5963
}
6064
return mask;
6165
}
@@ -68,14 +72,14 @@ DetID::DetID(const char* name) : mID(nameToID(name, First))
6872
}
6973

7074
//_______________________________
71-
std::string DetID::getNames(DetID::mask_t mask)
75+
std::string DetID::getNames(DetID::mask_t mask, char delimiter)
7276
{
7377
// construct from the name
7478
std::string ns;
7579
for (auto id = DetID::First; id <= DetID::Last; id++) {
7680
if (mask[id]) {
7781
if (!ns.empty()) {
78-
ns += ',';
82+
ns += delimiter;
7983
}
8084
ns += getName(id);
8185
}

DataFormats/Reconstruction/include/ReconstructionDataFormats/GlobalTrackID.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222
#include <array>
2323
#include <string_view>
24+
#include <bitset>
2425

2526
namespace o2
2627
{
@@ -59,15 +60,33 @@ class GlobalTrackID : public AbstractRef<25, 5, 2>
5960
NSources
6061
};
6162

62-
static const std::array<DetID::mask_t, NSources> DetectorMasks; // RS cannot be made constexpr since operator| is not constexpr
6363
using AbstractRef<25, 5, 2>::AbstractRef;
6464

65-
static const auto getSourceMask(int i) { return DetectorMasks[i]; }
66-
static auto getSourceName(int i) { return DetID::getNames(getSourceMask(i)); }
67-
65+
static const std::array<DetID::mask_t, NSources> SourceDetectorsMasks; // RS cannot be made constexpr since operator| is not constexpr
66+
static constexpr std::string_view NONE{"none"}; ///< keywork for no sources
67+
static constexpr std::string_view ALL{"all"}; ///< keywork for all sources
68+
typedef std::bitset<NSources> mask_t;
69+
70+
static constexpr std::array<mask_t, NSources> sMasks = ///< detectot masks
71+
{math_utils::bit2Mask(ITS), math_utils::bit2Mask(TPC), math_utils::bit2Mask(TRD), math_utils::bit2Mask(TOF), math_utils::bit2Mask(PHS),
72+
math_utils::bit2Mask(CPV), math_utils::bit2Mask(EMC), math_utils::bit2Mask(HMP), math_utils::bit2Mask(MFT), math_utils::bit2Mask(MCH),
73+
math_utils::bit2Mask(MID), math_utils::bit2Mask(ZDC), math_utils::bit2Mask(FT0), math_utils::bit2Mask(FV0), math_utils::bit2Mask(FDD),
74+
math_utils::bit2Mask(ITSTPC), math_utils::bit2Mask(TPCTOF), math_utils::bit2Mask(TPCTRD), math_utils::bit2Mask(ITSTPCTRD),
75+
math_utils::bit2Mask(ITSTPCTOF), math_utils::bit2Mask(TPCTRDTOF), math_utils::bit2Mask(ITSTPCTRDTOF)};
76+
77+
// methods for detector level manipulations
78+
static const auto getSourceDetectorsMask(int i) { return SourceDetectorsMasks[i]; }
79+
static bool includesDet(DetID id, GlobalTrackID::mask_t srcm);
80+
auto getSourceDetectorsMask() const { return getSourceDetectorsMask(getSource()); }
81+
bool includesDet(DetID id) const { return (getSourceDetectorsMask() & DetID::getMask(id)).any(); }
82+
83+
// methods for source level manipulations
84+
static auto getSourceName(int s) { return DetID::getNames(getSourceDetectorsMask(s), '-'); }
85+
constexpr mask_t getSourceMask(int s) const { return sMasks[s]; }
86+
mask_t getSourceMask() const { return getSourceMask(getSource()); }
6887
auto getSourceName() const { return getSourceName(getSource()); }
69-
auto getSourceMask() const { return getSourceMask(getSource()); }
70-
bool includesDet(DetID id) const { return (getSourceMask() & DetID::getMask(id)).any(); }
88+
static mask_t getSourcesMask(const std::string_view srcList);
89+
static bool includesSource(int s, mask_t srcm) { return srcm[s]; }
7190

7291
operator int() const { return int(getIndex()); }
7392

DataFormats/Reconstruction/src/GlobalTrackID.cxx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
using namespace o2::dataformats;
2222
using DetID = o2::detectors::DetID;
2323

24-
const std::array<DetID::mask_t, GlobalTrackID::NSources> GlobalTrackID::DetectorMasks = {
24+
const std::array<DetID::mask_t, GlobalTrackID::NSources> GlobalTrackID::SourceDetectorsMasks = {
2525
DetID::getMask(DetID::ITS),
2626
DetID::getMask(DetID::TPC),
2727
DetID::getMask(DetID::TRD),
@@ -52,6 +52,44 @@ std::string GlobalTrackID::asString() const
5252
return fmt::format("[{:s}/{:d}/{:s}]", getSourceName(), getIndex(), bits.to_string());
5353
}
5454

55+
GlobalTrackID::mask_t GlobalTrackID::getSourcesMask(const std::string_view srcList)
56+
{
57+
mask_t mask;
58+
std::string ss(srcList), sname{};
59+
if (ss.find(NONE) != std::string::npos) {
60+
return mask;
61+
}
62+
if (ss.find(ALL) != std::string::npos) {
63+
mask.set();
64+
return mask;
65+
}
66+
std::replace(ss.begin(), ss.end(), ' ', ',');
67+
std::stringstream sss(ss);
68+
while (getline(sss, sname, ',')) {
69+
for (auto id = 0; id < NSources; id++) {
70+
if (sname == getSourceName(id)) {
71+
mask.set(id);
72+
sname = "";
73+
break;
74+
}
75+
}
76+
if (!sname.empty()) {
77+
throw std::runtime_error(fmt::format("Wrong entry {:s} in reco-sources list {:s}", sname, srcList));
78+
}
79+
}
80+
return mask;
81+
}
82+
83+
bool GlobalTrackID::includesDet(DetID id, GlobalTrackID::mask_t srcm)
84+
{
85+
for (int i = 0; i < NSources; i++) {
86+
if (includesSource(i, srcm) && getSourceDetectorsMask(i) == id.getMask()) {
87+
return true;
88+
}
89+
}
90+
return false;
91+
}
92+
5593
std::ostream& o2::dataformats::operator<<(std::ostream& os, const o2::dataformats::GlobalTrackID& v)
5694
{
5795
// stream itself

0 commit comments

Comments
 (0)