Project
Loading...
Searching...
No Matches
CRUCalibHelpers.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef AliceO2_TPC_CRUCalibHelpers_H_
13#define AliceO2_TPC_CRUCalibHelpers_H_
14
15#include <unordered_map>
16#include <string>
17#include <array>
18#include <map>
19#include <cassert>
20#include <gsl/span>
21#include <filesystem>
22#include <type_traits>
23#include <vector>
24namespace fs = std::filesystem;
25
26#include "Rtypes.h"
27#include "TFile.h"
28
29#include "TPCBase/CalDet.h"
30#include "TPCBase/Utils.h"
31
33{
34
35struct LinkInfo {
36 LinkInfo(int cru, int link) : cru(cru), globalLinkID(link) {}
37 int cru{0};
39
40 bool operator<(const LinkInfo& other) const
41 {
42 if (cru < other.cru) {
43 return true;
44 }
45 if ((cru == other.cru) && (globalLinkID < other.globalLinkID)) {
46 return true;
47 }
48 return false;
49 }
50
52};
53
54using ValueArrayU32 = std::array<uint32_t, 80>;
55using DataMapU32 = std::map<LinkInfo, ValueArrayU32>;
56
57using ValueArrayF = std::array<float, 80>;
58using DataMapF = std::map<LinkInfo, ValueArrayF>;
59
60void debugDiff(std::string_view file1, std::string_view file2, std::string_view objName);
61void testChannelMapping(int cruID = 0);
62
64int getHWChannel(int sampa, int channel, int regionIter);
65
67std::tuple<int, int> getSampaInfo(int hwChannel, int cruID);
68
70template <uint32_t DataBitSizeT = 12, uint32_t SignificantBitsT = 2>
71constexpr uint32_t floatToFixedSize(float value)
72{
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);
77
78 const auto adc = uint32_t((value + 0.5f * FloatConversion) / FloatConversion) & BitMask;
79 assert(std::abs(value - adc * FloatConversion) <= 0.5f * FloatConversion);
80
81 return adc;
82}
83
84template <uint32_t SignificantBitsT = 2>
85constexpr float fixedSizeToFloat(uint32_t value)
86{
87 constexpr uint32_t SignificantBits = SignificantBitsT;
88 constexpr float FloatConversion = 1.f / float(1 << SignificantBits);
89
90 return float(value) * FloatConversion;
91}
92
95template <typename DataMap>
96void writeValues(const std::string_view fileName, const DataMap& map, bool onlyFilled = false)
97{
98 std::ofstream str(fileName.data(), std::ofstream::out);
99
100 for (const auto& [linkInfo, data] : map) {
101 if (onlyFilled) {
102 if (!std::accumulate(data.begin(), data.end(), uint32_t(0))) {
103 continue;
104 }
105 }
106 std::string values;
107 for (const auto& val : data) {
108 if (values.size()) {
109 values += ",";
110 }
111 values += fmt::format("{}", val);
112 }
113
114 str << linkInfo.cru << " "
115 << linkInfo.globalLinkID << " "
116 << values << "\n";
117 }
118}
119
120template <class T>
121struct is_map {
122 static constexpr bool value = false;
123};
124
125template <class Key, class Value>
126struct is_map<std::map<Key, Value>> {
127 static constexpr bool value = true;
128};
131template <typename DataMap, uint32_t SignificantBitsT = 0>
132typename std::enable_if_t<is_map<DataMap>::value, void>
133 fillCalPad(CalDet<float>& calPad, const DataMap& map)
134{
135 using namespace o2::tpc;
136 const auto& mapper = Mapper::instance();
137
138 for (const auto& [linkInfo, data] : map) {
139 const CRU cru(linkInfo.cru);
140 const PartitionInfo& partInfo = mapper.getMapPartitionInfo()[cru.partition()];
141 const int nFECs = partInfo.getNumberOfFECs();
142 const int fecOffset = (nFECs + 1) / 2;
143 const int fecInPartition = (linkInfo.globalLinkID < fecOffset) ? linkInfo.globalLinkID : fecOffset + linkInfo.globalLinkID % 12;
144
145 int hwChannel{0};
146 for (const auto& val : data) {
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);
151 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
152 } else {
153 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(val)));
154 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
155 }
156 ++hwChannel;
157 }
158 }
159}
160
162template <uint32_t SignificantBitsT = 2>
163int fillCalPad(CalDet<float>& calPad, std::istream& infile)
164{
165 using namespace o2::tpc;
166 const auto& mapper = Mapper::instance();
167
168 int cruID{0};
169 int globalLinkID{0};
170 int sampaOnFEC{0};
171 int channelOnSAMPA{0};
172 std::string values;
173 int nLines{0};
174
175 std::string line;
176 while (std::getline(infile, line)) {
177 ++nLines;
178 std::stringstream streamLine(line);
179 streamLine >> cruID >> globalLinkID >> values;
180
181 const CRU cru(cruID);
182 const PartitionInfo& partInfo = mapper.getMapPartitionInfo()[cru.partition()];
183 const int nFECs = partInfo.getNumberOfFECs();
184 const int fecOffset = (nFECs + 1) / 2;
185 const int fecInPartition = (globalLinkID < fecOffset) ? globalLinkID : fecOffset + globalLinkID % 12;
186
187 int hwChannel{0};
188 for (const auto& val : utils::tokenize(values, ",")) {
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);
193 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
194 } else {
195 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(val)));
196 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
197 }
198 ++hwChannel;
199 }
200 }
201
202 return nLines;
203}
204
206template <uint32_t SignificantBitsT = 2>
207int fillCalPad(CalDet<float>& calPad, gsl::span<const char> data)
208{
209 struct membuf : std::streambuf {
210 membuf(char* base, std::ptrdiff_t n)
211 {
212 this->setg(base, base, base + n);
213 }
214 };
215 membuf sbuf((char*)data.data(), data.size());
216 std::istream in(&sbuf);
217
218 return fillCalPad<SignificantBitsT>(calPad, in);
219}
220
225template <uint32_t SignificantBitsT = 2>
226o2::tpc::CalDet<float> getCalPad(const std::string_view fileName, const std::string_view outputFile = "", std::string_view calPadName = "")
227{
228 using namespace o2::tpc;
229
230 if (!calPadName.size()) {
231 calPadName = fs::path(fileName.data()).stem().c_str();
232 }
233 CalDet<float> calPad(calPadName);
234
235 std::ifstream infile(fileName.data(), std::ifstream::in);
236 if (!infile.is_open()) {
237 LOGP(error, "could not open file {}", fileName);
238 return calPad;
239 }
240
241 fillCalPad<SignificantBitsT>(calPad, infile);
242
243 if (outputFile.size()) {
244 TFile f(outputFile.data(), "recreate");
245 f.WriteObject(&calPad, calPadName.data());
246 }
247 return calPad;
248}
249
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);
253
254} // namespace o2::tpc::cru_calib_helpers
255
256#endif
unsigned char partition() const
Definition CRU.h:63
void setValue(const size_t channel, const T &value)
Definition CalArray.h:95
const CalType & getCalArray(size_t position) const
Definition CalDet.h:63
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
Pad and row inside a ROC.
Definition PadROCPos.h:37
const ROC & getROC() const
Definition PadROCPos.h:55
int getPad() const
Definition PadROCPos.h:79
int getRow() const
Definition PadROCPos.h:75
unsigned char getNumberOfFECs() const
GLdouble n
Definition glcorearb.h:1982
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLenum GLsizei GLsizei GLint * values
Definition glcorearb.h:1576
GLboolean * data
Definition glcorearb.h:298
GLuint GLfloat * val
Definition glcorearb.h:1582
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)
Definition Utils.cxx:40
Global TPC definitions and constants.
Definition SimTraits.h:167
Defining DataPointCompositeObject explicitly as copiable.
bool operator<(const LinkInfo &other) const
VectorOfTObjectPtrs other
ArrayADC adc
const std::string str