Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions DataFormats/Detectors/FIT/FT0/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ o2_add_library(DataFormatsFT0
src/CTF.cxx
src/LookUpTable.cxx
src/SlewingCoef.cxx
src/PMLookupTable.cxx

PUBLIC_LINK_LIBRARIES O2::FT0Base
O2::DataFormatsFIT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.

#ifndef O2_FT0_PMLOOKUPTABLE_H_
#define O2_FT0_PMLOOKUPTABLE_H_

#include <array>
#include <cstddef>
#include <cstdint>
#include <map>

#include "FT0Base/Constants.h"

namespace o2::ft0
{

/// Cached mapping between FT0 channels and PM modules.
///
/// The mapping is constructed once from SingleLUT and can then be reused by
/// digitization, reconstruction, trigger and QA code without reparsing the LUT.
class PMLookupTable
{
public:
using ChannelID = uint16_t;
using PMHash = uint8_t;
using PMMap = std::map<PMHash, bool>; // PM hash -> true for A side, false for C side

/// Return the process-wide immutable lookup table.
static const PMLookupTable& Instance();

/// Return the PM hash assigned to a detector channel.
PMHash getPMHash(ChannelID channelID) const;

/// Return true for an A-side PM and false for a C-side PM.
bool isASide(PMHash pmHash) const;

/// Return all PM hashes and their sides. TCM entries are not included.
const PMMap& getPMs() const noexcept { return mPMHash2IsASide; }

private:
PMLookupTable();

std::array<PMHash, Constants::sNCHANNELS_PM> mChannelID2PMHash{};
std::array<bool, Constants::sNCHANNELS_PM> mChannelIsMapped{};
PMMap mPMHash2IsASide;
};

} // namespace o2::ft0

#endif // O2_FT0_PMLOOKUPTABLE_H_
109 changes: 109 additions & 0 deletions DataFormats/Detectors/FIT/FT0/src/PMLookupTable.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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.

#include "DataFormatsFT0/PMLookupTable.h"

#include "DataFormatsFT0/LookUpTable.h"
#include "Framework/Logger.h"

#include <algorithm>
#include <charconv>
#include <map>
#include <limits>
#include <string>
#include <system_error>

namespace o2::ft0
{

const PMLookupTable& PMLookupTable::Instance()
{
static const PMLookupTable table;
return table;
}

PMLookupTable::PMLookupTable()
{
std::map<std::string, PMHash> moduleName2Hash;

auto lut = SingleLUT::Instance().getVecMetadataFEE();
std::sort(lut.begin(), lut.end(), [](const auto& lhs, const auto& rhs) {
return lhs.mModuleName < rhs.mModuleName;
});

unsigned int nextHash = 0;

for (const auto& entry : lut) {
const auto& moduleName = entry.mModuleName;
const auto& moduleType = entry.mModuleType;
const auto& channelString = entry.mChannelID;

if (nextHash > static_cast<unsigned int>(std::numeric_limits<PMHash>::max())) {
LOG(fatal) << "Too many FT0 FEE modules to represent with PMHash";
}

auto [moduleIt, inserted] = moduleName2Hash.emplace(moduleName, static_cast<PMHash>(nextHash));

if (inserted) {
const auto moduleHash = moduleIt->second;
if (moduleName.find("PMA") != std::string::npos) {
mPMHash2IsASide.emplace(moduleHash, true);
} else if (moduleName.find("PMC") != std::string::npos) {
mPMHash2IsASide.emplace(moduleHash, false);
} else if (moduleType != "TCM") {
LOG(fatal) << "Unknown FT0 module in LUT: " << moduleName
<< " (type " << moduleType << ")";
}
++nextHash;
}

int channelID = -1;
const char* begin = channelString.data();
const char* end = begin + channelString.size();
const auto [ptr, error] = std::from_chars(begin, end, channelID);
const bool isNumericChannel = error == std::errc{} && ptr == end;

if (isNumericChannel) {
if (channelID < 0 || channelID >= Constants::sNCHANNELS_PM) {
LOG(fatal) << "Incorrect FT0 LUT entry: channel " << channelString
<< " | module " << moduleName;
}

mChannelID2PMHash[channelID] = moduleIt->second;
mChannelIsMapped[channelID] = true;
} else if (moduleType != "TCM") {
LOG(fatal) << "Non-TCM FT0 module without numerical channel ID: channel "
<< channelString << " | module " << moduleName;
}
}
}

PMLookupTable::PMHash PMLookupTable::getPMHash(ChannelID channelID) const
{
if (channelID >= Constants::sNCHANNELS_PM) {
LOG(fatal) << "FT0 channel ID outside valid range: " << channelID;
}
if (!mChannelIsMapped[channelID]) {
LOG(fatal) << "FT0 channel is not mapped to a PM: " << channelID;
}
return mChannelID2PMHash[channelID];
}

bool PMLookupTable::isASide(PMHash pmHash) const
{
const auto it = mPMHash2IsASide.find(pmHash);
if (it == mPMHash2IsASide.end()) {
LOG(fatal) << "Unknown FT0 PM hash: " << static_cast<unsigned int>(pmHash);
}
return it->second;
}

} // namespace o2::ft0
Loading