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(std::ostream&
str,
const DataMap& map,
bool onlyFilled =
false)
98 for (
const auto& [linkInfo,
data] : map) {
100 if (!std::accumulate(
data.begin(),
data.end(), uint32_t(0))) {
112 str << linkInfo.cru <<
" "
113 << linkInfo.globalLinkID <<
" "
118template <
typename DataMap>
119void writeValues(
const std::string_view fileName,
const DataMap& map,
bool onlyFilled =
false)
121 std::ofstream
str(fileName.data(), std::ofstream::out);
127 static constexpr bool value =
false;
130template <
class Key,
class Value>
137template <
typename DataMap, u
int32_t SignificantBitsT = 0>
138typename std::enable_if_t<is_map<DataMap>::value,
void>
144 for (
const auto& [linkInfo,
data] : map) {
145 const CRU cru(linkInfo.cru);
148 const int fecOffset = (nFECs + 1) / 2;
149 const int fecInPartition = (linkInfo.globalLinkID < fecOffset) ? linkInfo.globalLinkID : fecOffset + linkInfo.globalLinkID % 12;
153 const auto& [sampaOnFEC, channelOnSAMPA] =
getSampaInfo(hwChannel, cru);
154 const PadROCPos padROCPos = mapper.padROCPos(cru, fecInPartition, sampaOnFEC, channelOnSAMPA);
155 if constexpr (SignificantBitsT == 0) {
156 const float set = std::stof(
val);
159 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(
val)));
168template <u
int32_t SignificantBitsT = 2>
177 int channelOnSAMPA{0};
182 while (std::getline(infile, line)) {
184 std::stringstream streamLine(line);
185 streamLine >> cruID >> globalLinkID >>
values;
187 const CRU cru(cruID);
190 const int fecOffset = (nFECs + 1) / 2;
191 const int fecInPartition = (globalLinkID < fecOffset) ? globalLinkID : fecOffset + globalLinkID % 12;
195 const auto& [sampaOnFEC, channelOnSAMPA] =
getSampaInfo(hwChannel, cru);
196 const PadROCPos padROCPos = mapper.padROCPos(cru, fecInPartition, sampaOnFEC, channelOnSAMPA);
197 if constexpr (SignificantBitsT == 0) {
198 const float set = std::stof(
val);
201 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(
val)));
212template <u
int32_t SignificantBitsT = 2>
215 struct membuf : std::streambuf {
216 membuf(
char* base, std::ptrdiff_t
n)
218 this->setg(base, base, base +
n);
221 membuf sbuf((
char*)
data.data(),
data.size());
222 std::istream in(&sbuf);
224 return fillCalPad<SignificantBitsT>(calPad, in);
231template <u
int32_t SignificantBitsT = 2>
236 if (!calPadName.size()) {
237 calPadName = fs::path(fileName.data()).stem().c_str();
241 std::ifstream infile(fileName.data(), std::ifstream::in);
242 if (!infile.is_open()) {
243 LOGP(error,
"could not open file {}", fileName);
247 fillCalPad<SignificantBitsT>(calPad, infile);
249 if (outputFile.size()) {
250 TFile
f(outputFile.data(),
"recreate");
251 f.WriteObject(&calPad, calPadName.data());
258std::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)
DataMapU32 getDataMap(const CalPad &calPad)
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)
void writeValues(std::ostream &str, const DataMap &map, bool onlyFilled=false)
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
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