Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GlobalTrackID sources masks and their manipulations
  • Loading branch information
shahor02 committed Feb 28, 2021
commit 2f3ad165b91a58e6eff3e3bc7ab800edf1b2da11
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DetID
static constexpr int nDetectors = Last + 1; ///< number of defined detectors

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

typedef std::bitset<nDetectors> mask_t;

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

static std::string getNames(mask_t mask);
static std::string getNames(mask_t mask, char delimiter = ',');

// we need default c-tor only for root persistency, code must use c-tor with argument
DetID() : mID(First) {}
Expand Down
34 changes: 19 additions & 15 deletions DataFormats/Detectors/Common/src/DetID.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,27 @@ constexpr int DetID::nDetectors;
DetID::mask_t DetID::getMask(const std::string_view detList)
{
DetID::mask_t mask;
std::string ss(detList);
auto pos = ss.find(NONE);
if (pos != std::string::npos) {
ss.replace(pos, NONE.size(), "");
} else {
std::string ss(detList), sname{};
if (ss.find(NONE) != std::string::npos) {
return mask;
}
if (ss.find(ALL) != std::string::npos) {
mask.set();
return mask;
}
std::replace(ss.begin(), ss.end(), ' ', ',');
std::stringstream sss(ss);
while (getline(sss, sname, ',')) {
for (auto id = DetID::First; id <= DetID::Last; id++) {
auto pos = ss.find(DetID::getName(id));
if (pos != std::string::npos) {
if (sname == getName(id)) {
mask.set(id);
ss.replace(pos, strlen(DetID::getName(id)), "");
sname = "";
break;
}
}
}
// make sure that no names are left in the input strings
if (std::count_if(ss.begin(), ss.end(), [](unsigned char c) { return std::isalnum(c); })) {
LOG(ERROR) << "Ill-formed detectors list " << std::string(detList);
throw std::runtime_error("Ill-formed detectors list");
if (!sname.empty()) {
throw std::runtime_error(fmt::format("Wrong entry {:s} in detectors list {:s}", sname, detList));
}
}
return mask;
}
Expand All @@ -68,14 +72,14 @@ DetID::DetID(const char* name) : mID(nameToID(name, First))
}

//_______________________________
std::string DetID::getNames(DetID::mask_t mask)
std::string DetID::getNames(DetID::mask_t mask, char delimiter)
{
// construct from the name
std::string ns;
for (auto id = DetID::First; id <= DetID::Last; id++) {
if (mask[id]) {
if (!ns.empty()) {
ns += ',';
ns += delimiter;
}
ns += getName(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <array>
#include <string_view>
#include <bitset>

namespace o2
{
Expand Down Expand Up @@ -59,15 +60,33 @@ class GlobalTrackID : public AbstractRef<25, 5, 2>
NSources
};

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

static const auto getSourceMask(int i) { return DetectorMasks[i]; }
static auto getSourceName(int i) { return DetID::getNames(getSourceMask(i)); }

static const std::array<DetID::mask_t, NSources> SourceDetectorsMasks; // RS cannot be made constexpr since operator| is not constexpr
static constexpr std::string_view NONE{"none"}; ///< keywork for no sources
static constexpr std::string_view ALL{"all"}; ///< keywork for all sources
typedef std::bitset<NSources> mask_t;

static constexpr std::array<mask_t, NSources> sMasks = ///< detectot masks
{math_utils::bit2Mask(ITS), math_utils::bit2Mask(TPC), math_utils::bit2Mask(TRD), math_utils::bit2Mask(TOF), math_utils::bit2Mask(PHS),
math_utils::bit2Mask(CPV), math_utils::bit2Mask(EMC), math_utils::bit2Mask(HMP), math_utils::bit2Mask(MFT), math_utils::bit2Mask(MCH),
math_utils::bit2Mask(MID), math_utils::bit2Mask(ZDC), math_utils::bit2Mask(FT0), math_utils::bit2Mask(FV0), math_utils::bit2Mask(FDD),
math_utils::bit2Mask(ITSTPC), math_utils::bit2Mask(TPCTOF), math_utils::bit2Mask(TPCTRD), math_utils::bit2Mask(ITSTPCTRD),
math_utils::bit2Mask(ITSTPCTOF), math_utils::bit2Mask(TPCTRDTOF), math_utils::bit2Mask(ITSTPCTRDTOF)};

// methods for detector level manipulations
static const auto getSourceDetectorsMask(int i) { return SourceDetectorsMasks[i]; }
static bool includesDet(DetID id, GlobalTrackID::mask_t srcm);
auto getSourceDetectorsMask() const { return getSourceDetectorsMask(getSource()); }
bool includesDet(DetID id) const { return (getSourceDetectorsMask() & DetID::getMask(id)).any(); }

// methods for source level manipulations
static auto getSourceName(int s) { return DetID::getNames(getSourceDetectorsMask(s), '-'); }
constexpr mask_t getSourceMask(int s) const { return sMasks[s]; }
mask_t getSourceMask() const { return getSourceMask(getSource()); }
auto getSourceName() const { return getSourceName(getSource()); }
auto getSourceMask() const { return getSourceMask(getSource()); }
bool includesDet(DetID id) const { return (getSourceMask() & DetID::getMask(id)).any(); }
static mask_t getSourcesMask(const std::string_view srcList);
static bool includesSource(int s, mask_t srcm) { return srcm[s]; }

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

Expand Down
40 changes: 39 additions & 1 deletion DataFormats/Reconstruction/src/GlobalTrackID.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using namespace o2::dataformats;
using DetID = o2::detectors::DetID;

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

GlobalTrackID::mask_t GlobalTrackID::getSourcesMask(const std::string_view srcList)
{
mask_t mask;
std::string ss(srcList), sname{};
if (ss.find(NONE) != std::string::npos) {
return mask;
}
if (ss.find(ALL) != std::string::npos) {
mask.set();
return mask;
}
std::replace(ss.begin(), ss.end(), ' ', ',');
std::stringstream sss(ss);
while (getline(sss, sname, ',')) {
for (auto id = 0; id < NSources; id++) {
if (sname == getSourceName(id)) {
mask.set(id);
sname = "";
break;
}
}
if (!sname.empty()) {
throw std::runtime_error(fmt::format("Wrong entry {:s} in reco-sources list {:s}", sname, srcList));
}
}
return mask;
}

bool GlobalTrackID::includesDet(DetID id, GlobalTrackID::mask_t srcm)
{
for (int i = 0; i < NSources; i++) {
if (includesSource(i, srcm) && getSourceDetectorsMask(i) == id.getMask()) {
return true;
}
}
return false;
}

std::ostream& o2::dataformats::operator<<(std::ostream& os, const o2::dataformats::GlobalTrackID& v)
{
// stream itself
Expand Down