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 {
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>
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>
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(std::ostream& str, const DataMap& map, bool onlyFilled = false)
97{
98 for (const auto& [linkInfo, data] : map) {
99 if (onlyFilled) {
100 if (!std::accumulate(data.begin(), data.end(), uint32_t(0))) {
101 continue;
102 }
103 }
104 std::string values;
105 for (const auto& val : data) {
106 if (values.size()) {
107 values += ",";
108 }
109 values += fmt::format("{}", val);
110 }
111
112 str << linkInfo.cru << " "
113 << linkInfo.globalLinkID << " "
114 << values << "\n";
115 }
116}
117
118template <typename DataMap>
119void writeValues(const std::string_view fileName, const DataMap& map, bool onlyFilled = false)
120{
121 std::ofstream str(fileName.data(), std::ofstream::out);
122 writeValues(str, map, onlyFilled);
123}
124
125template <class T>
126struct is_map {
127 static constexpr bool value = false;
128};
129
130template <class Key, class Value>
131struct is_map<std::map<Key, Value>> {
132 static constexpr bool value = true;
133};
134
137template <typename DataMap, uint32_t SignificantBitsT = 0>
138typename std::enable_if_t<is_map<DataMap>::value, void>
139 fillCalPad(CalDet<float>& calPad, const DataMap& map)
140{
141 using namespace o2::tpc;
142 const auto& mapper = Mapper::instance();
143
144 for (const auto& [linkInfo, data] : map) {
145 const CRU cru(linkInfo.cru);
146 const PartitionInfo& partInfo = mapper.getMapPartitionInfo()[cru.partition()];
147 const int nFECs = partInfo.getNumberOfFECs();
148 const int fecOffset = (nFECs + 1) / 2;
149 const int fecInPartition = (linkInfo.globalLinkID < fecOffset) ? linkInfo.globalLinkID : fecOffset + linkInfo.globalLinkID % 12;
150
151 int hwChannel{0};
152 for (const auto& val : data) {
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);
157 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
158 } else {
159 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(val)));
160 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
161 }
162 ++hwChannel;
163 }
164 }
165}
166
168template <uint32_t SignificantBitsT = 2>
169int fillCalPad(CalDet<float>& calPad, std::istream& infile)
170{
171 using namespace o2::tpc;
172 const auto& mapper = Mapper::instance();
173
174 int cruID{0};
175 int globalLinkID{0};
176 int sampaOnFEC{0};
177 int channelOnSAMPA{0};
178 std::string values;
179 int nLines{0};
180
181 std::string line;
182 while (std::getline(infile, line)) {
183 ++nLines;
184 std::stringstream streamLine(line);
185 streamLine >> cruID >> globalLinkID >> values;
186
187 const CRU cru(cruID);
188 const PartitionInfo& partInfo = mapper.getMapPartitionInfo()[cru.partition()];
189 const int nFECs = partInfo.getNumberOfFECs();
190 const int fecOffset = (nFECs + 1) / 2;
191 const int fecInPartition = (globalLinkID < fecOffset) ? globalLinkID : fecOffset + globalLinkID % 12;
192
193 int hwChannel{0};
194 for (const auto& val : utils::tokenize(values, ",")) {
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);
199 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
200 } else {
201 const float set = fixedSizeToFloat<SignificantBitsT>(uint32_t(std::stoi(val)));
202 calPad.getCalArray(padROCPos.getROC()).setValue(padROCPos.getRow(), padROCPos.getPad(), set);
203 }
204 ++hwChannel;
205 }
206 }
207
208 return nLines;
209}
210
212template <uint32_t SignificantBitsT = 2>
213int fillCalPad(CalDet<float>& calPad, gsl::span<const char> data)
214{
215 struct membuf : std::streambuf {
216 membuf(char* base, std::ptrdiff_t n)
217 {
218 this->setg(base, base, base + n);
219 }
220 };
221 membuf sbuf((char*)data.data(), data.size());
222 std::istream in(&sbuf);
223
224 return fillCalPad<SignificantBitsT>(calPad, in);
225}
226
231template <uint32_t SignificantBitsT = 2>
232o2::tpc::CalDet<float> getCalPad(const std::string_view fileName, const std::string_view outputFile = "", std::string_view calPadName = "")
233{
234 using namespace o2::tpc;
235
236 if (!calPadName.size()) {
237 calPadName = fs::path(fileName.data()).stem().c_str();
238 }
239 CalDet<float> calPad(calPadName);
240
241 std::ifstream infile(fileName.data(), std::ifstream::in);
242 if (!infile.is_open()) {
243 LOGP(error, "could not open file {}", fileName);
244 return calPad;
245 }
246
247 fillCalPad<SignificantBitsT>(calPad, infile);
248
249 if (outputFile.size()) {
250 TFile f(outputFile.data(), "recreate");
251 f.WriteObject(&calPad, calPadName.data());
252 }
253 return calPad;
254}
255
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);
259
260DataMapU32 getDataMap(const CalPad& calPad);
261} // namespace o2::tpc::cru_calib_helpers
262
263#endif
unsigned int uint32_t
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
configStandalone Reconstruction rec_tpc float
char error[CURL_ERROR_SIZE]
unsigned char partition() const
Definition CRU.h:63
void setValue(const size_t channel, const T &value)
Definition CalArray.h:100
const CalType & getCalArray(size_t position) const
Definition CalDet.h:69
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
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)
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)
Definition Utils.cxx:40
Global TPC definitions and constants.
bool operator<(const LinkInfo &other) const
VectorOfTObjectPtrs other
ArrayADC adc
const std::string str