12#ifndef AliceO2_TPC_CRUCalibHelpers_H_
13#define AliceO2_TPC_CRUCalibHelpers_H_
15#include <unordered_map>
24namespace fs = std::filesystem;
58using DataMapF = std::map<LinkInfo, ValueArrayF>;
60void debugDiff(std::string_view file1, std::string_view file2, std::string_view objName);
67std::tuple<int, int>
getSampaInfo(
int hwChannel,
int cruID);
70template <u
int32_t DataBitSizeT = 12, u
int32_t SignificantBitsT = 2>
73 constexpr uint32_t DataBitSize = DataBitSizeT;
74 constexpr uint32_t SignificantBits = SignificantBitsT;
75 constexpr uint64_t BitMask = ((uint64_t(1) << DataBitSize) - 1);
76 constexpr float FloatConversion = 1.f / float(1 << SignificantBits);
78 const auto adc = uint32_t((
value + 0.5f * FloatConversion) / FloatConversion) & BitMask;
79 assert(std::abs(
value -
adc * FloatConversion) <= 0.5f * FloatConversion);
84template <u
int32_t SignificantBitsT = 2>
87 constexpr uint32_t SignificantBits = SignificantBitsT;
88 constexpr float FloatConversion = 1.f / float(1 << SignificantBits);
90 return float(
value) * FloatConversion;
95template <
typename DataMap>
96void writeValues(
const std::string_view fileName,
const DataMap& map,
bool onlyFilled =
false)
98 std::ofstream
str(fileName.data(), std::ofstream::out);
100 for (
const auto& [linkInfo,
data] : map) {
102 if (!std::accumulate(
data.begin(),
data.end(), uint32_t(0))) {
114 str << linkInfo.cru <<
" "
115 << linkInfo.globalLinkID <<
" "
122 static constexpr bool value =
false;
125template <
class Key,
class Value>
131template <
typename DataMap, u
int32_t SignificantBitsT = 0>
132typename std::enable_if_t<is_map<DataMap>::value,
void>
138 for (
const auto& [linkInfo,
data] : map) {
139 const CRU cru(linkInfo.cru);
142 const int fecOffset = (nFECs + 1) / 2;
143 const int fecInPartition = (linkInfo.globalLinkID < fecOffset) ? linkInfo.globalLinkID : fecOffset + linkInfo.globalLinkID % 12;
147 const auto& [sampaOnFEC, channelOnSAMPA] =
getSampaInfo(hwChannel, cru);
148 const PadROCPos padROCPos = mapper.padROCPos(cru, fecInPartition, sampaOnFEC, channelOnSAMPA);
149 if constexpr (SignificantBitsT == 0) {
150 const float set = std::stof(
val);
153 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(
val)));
162template <u
int32_t SignificantBitsT = 2>
171 int channelOnSAMPA{0};
176 while (std::getline(infile, line)) {
178 std::stringstream streamLine(line);
179 streamLine >> cruID >> globalLinkID >>
values;
181 const CRU cru(cruID);
184 const int fecOffset = (nFECs + 1) / 2;
185 const int fecInPartition = (globalLinkID < fecOffset) ? globalLinkID : fecOffset + globalLinkID % 12;
189 const auto& [sampaOnFEC, channelOnSAMPA] =
getSampaInfo(hwChannel, cru);
190 const PadROCPos padROCPos = mapper.padROCPos(cru, fecInPartition, sampaOnFEC, channelOnSAMPA);
191 if constexpr (SignificantBitsT == 0) {
192 const float set = std::stof(
val);
195 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(
val)));
206template <u
int32_t SignificantBitsT = 2>
209 struct membuf : std::streambuf {
210 membuf(
char* base, std::ptrdiff_t
n)
212 this->setg(base, base, base +
n);
215 membuf sbuf((
char*)
data.data(),
data.size());
216 std::istream in(&sbuf);
218 return fillCalPad<SignificantBitsT>(calPad, in);
225template <u
int32_t SignificantBitsT = 2>
230 if (!calPadName.size()) {
231 calPadName = fs::path(fileName.data()).stem().c_str();
235 std::ifstream infile(fileName.data(), std::ifstream::in);
236 if (!infile.is_open()) {
237 LOGP(error,
"could not open file {}", fileName);
241 fillCalPad<SignificantBitsT>(calPad, infile);
243 if (outputFile.size()) {
244 TFile
f(outputFile.data(),
"recreate");
245 f.WriteObject(&calPad, calPadName.data());
252std::unordered_map<std::string, CalPad>
preparePedestalFiles(
const CalPad& pedestals,
const CalPad& noise, std::vector<float> sigmaNoiseROCType = {3, 3, 3, 3}, std::vector<float> minADCROCType = {2, 2, 2, 2},
float pedestalOffset = 0,
bool onlyFilled =
false,
bool maskBad =
true,
float noisyChannelThreshold = 1.5,
float sigmaNoiseNoisyChannels = 4,
float badChannelThreshold = 6,
bool fixedSize =
false);
unsigned char partition() const
void setValue(const size_t channel, const T &value)
const CalType & getCalArray(size_t position) const
static Mapper & instance(const std::string mappingDir="")
Pad and row inside a ROC.
const ROC & getROC() const
unsigned char getNumberOfFECs() const
GLsizei const GLfloat * value
GLenum GLsizei GLsizei GLint * values
int getHWChannel(int sampa, int channel, int regionIter)
return the hardware channel number as mapped in the CRU
std::unordered_map< std::string, CalPad > preparePedestalFiles(const CalPad &pedestals, const CalPad &noise, std::vector< float > sigmaNoiseROCType={3, 3, 3, 3}, std::vector< float > minADCROCType={2, 2, 2, 2}, float pedestalOffset=0, bool onlyFilled=false, bool maskBad=true, float noisyChannelThreshold=1.5, float sigmaNoiseNoisyChannels=4, float badChannelThreshold=6, bool fixedSize=false)
std::array< float, 80 > ValueArrayF
std::enable_if_t< is_map< DataMap >::value, void > fillCalPad(CalDet< float > &calPad, const DataMap &map)
std::tuple< int, int > getSampaInfo(int hwChannel, int cruID)
convert HW mapping to sampa and channel number
constexpr uint32_t floatToFixedSize(float value)
convert float to integer with fixed precision and max number of digits
void testChannelMapping(int cruID=0)
std::map< LinkInfo, ValueArrayF > DataMapF
constexpr float fixedSizeToFloat(uint32_t value)
o2::tpc::CalDet< float > getCalPad(const std::string_view fileName, const std::string_view outputFile="", std::string_view calPadName="")
void debugDiff(std::string_view file1, std::string_view file2, std::string_view objName)
debug differences between two cal pad objects
void writeValues(const std::string_view fileName, const DataMap &map, bool onlyFilled=false)
std::map< LinkInfo, ValueArrayU32 > DataMapU32
std::array< uint32_t, 80 > ValueArrayU32
const std::vector< std::string > tokenize(const std::string_view input, const std::string_view pattern)
Global TPC definitions and constants.
Defining DataPointCompositeObject explicitly as copiable.
bool operator<(const LinkInfo &other) const
LinkInfo(int cru, int link)
VectorOfTObjectPtrs other