Project
Loading...
Searching...
No Matches
Mapping.cxx
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
14
15#include <fstream>
16#include "TSystem.h"
17#include <fairlogger/Logger.h>
18#include "PHOSBase/Mapping.h"
19#include "PHOSBase/Geometry.h"
20
21using namespace o2::phos;
22Mapping* Mapping::sMapping = nullptr;
23//_______________________________________________________
24Mapping::Mapping(std::basic_string_view<char> path) : mPath(path),
25 mInitialized(false)
26{
27}
28//_______________________________________________________
30{
31 if (sMapping) {
32 return sMapping;
33 } else {
34 sMapping = new Mapping();
35 sMapping->setMapping();
36 return sMapping;
37 }
38}
39//_______________________________________________________
40Mapping* Mapping::Instance(std::basic_string_view<char> path)
41{
42 if (sMapping) {
43 if (sMapping->mPath == path) {
44 return sMapping;
45 } else {
46 delete sMapping;
47 }
48 }
49 sMapping = new Mapping(path);
50 sMapping->setMapping();
51 return sMapping;
52}
53//_______________________________________________________
54Mapping::ErrorStatus Mapping::hwToAbsId(short ddl, short hwAddr, short& absId, CaloFlag& caloFlag) const
55{
56
57 if (!mInitialized) {
58 LOG(error) << "Mapping not initialized";
59 return kNotInitialized;
60 }
61
62 if (ddl < 0 || ddl > 14) {
63 return kWrongDDL;
64 }
65
66 if ((hwAddr >= 112 && hwAddr < 128) || (hwAddr >= 2159 && hwAddr < 2176)) { // TRU flags
67 caloFlag = kTRU;
68 absId = -1;
69 return kOK;
70 }
71 if (hwAddr < 0 || hwAddr >= NMaxHWAddress) {
72 return kWrongHWAddress;
73 }
74
75 // transform
76 absId = mAbsId[ddl][hwAddr];
77 caloFlag = mCaloFlag[ddl][hwAddr];
78
79 if (caloFlag == 2) {
80 absId += NCHANNELS;
81 }
82 if (caloFlag < 2 && (absId > NCHANNELS || absId <= 1792)) {
83 absId = 0;
84 return kWrongHWAddress;
85 }
86 return kOK;
87}
88//_______________________________________________________
89Mapping::ErrorStatus Mapping::absIdTohw(short absId, short caloFlag, short& ddl, short& hwAddr) const
90{
91
92 if (caloFlag < 0 || caloFlag > 2) {
93 ddl = 0;
94 hwAddr = 0;
95 return kWrongCaloFlag;
96 }
97 if (caloFlag < 2) {
98 if (absId <= 1792 || absId > NCHANNELS) {
99 ddl = 0;
100 hwAddr = 0;
101 return kWrongAbsId;
102 }
103 } else { // TRU: absId goes after readout ones
104 absId -= NCHANNELS;
105 if (absId < 1 || absId > NTRUReadoutChannels) {
106 ddl = 0;
107 hwAddr = 0;
108 return kWrongAbsId;
109 }
110 }
111
112 if (!mInitialized) {
113 LOG(error) << "Mapping not initialized";
114 return kNotInitialized;
115 }
116
117 ddl = mAbsToHW[absId - 1][caloFlag][0];
118 hwAddr = mAbsToHW[absId - 1][caloFlag][1];
119 return kOK;
120}
121//_______________________________________________________
123{
124 // Read mapping from data files a-la Run2
125
126 std::string p;
127 if (mPath.empty()) { // use default path
128 p = gSystem->Getenv("O2_ROOT");
129 p += "/share/Detectors/PHOS/files";
130 } else {
131 p = mPath.data();
132 }
133
134 for (short m = 0; m < 4; m++) { // modules
135 for (short i = 0; i < 4; i++) { // RCU
136 if (m == 0 && (i < 2)) {
137 continue; // half of module: only RCU 2,3
138 }
139
140 short numberOfChannels = 0;
141 short maxHWAddress = 0;
142 std::string fname = fmt::format("{:s}/Mod{:d}RCU{:d}.data", p, m, i);
143 std::ifstream fIn(fname);
144 if (!fIn.is_open()) {
145 LOG(fatal) << "Missing mapping file " << p << "/Mod" << m << "RCU" << i << ".data";
146 return kNotInitialized;
147 }
148 if (!(fIn >> numberOfChannels)) {
149 LOG(fatal) << "Syntax of mapping file " << p << "/Mod" << m << "RCU" << i << ".data is wrong: no numberOfChannels";
150 return kNotInitialized;
151 }
152 if (numberOfChannels != NHWPERDDL) {
153 LOG(fatal) << "Unexpected number of channels: " << numberOfChannels << " expecting " << NHWPERDDL << " file " << p << "/Mod" << m << "RCU" << i << ".data is wrong: no numberOfChannels";
154 return kNotInitialized;
155 }
156 if (!(fIn >> maxHWAddress)) {
157 LOG(fatal) << "Syntax of mapping file " << p << "/Mod" << m << "RCU" << i << ".data is wrong: no maxHWAddress";
158 return kNotInitialized;
159 }
160 if (maxHWAddress > NMaxHWAddress) {
161 LOG(fatal) << "Maximal HW address in file " << maxHWAddress << "larger than array size " << NMaxHWAddress << "for /Mod" << m << "RCU" << i << ".data is wrong: no maxHWAddress";
162 return kNotInitialized;
163 }
164 for (short ich = 0; ich < numberOfChannels; ich++) { // 1792 = 2*896 channels connected to each RCU
165 int hwAddress;
166 if (!(fIn >> hwAddress)) {
167 LOG(fatal) << "Syntax of mapping file " << p << "/Mod" << m << "RCU" << i << ".data is wrong: no HWadd for ch " << ich;
168 return kNotInitialized;
169 }
170 if (hwAddress > maxHWAddress) {
171 LOG(fatal) << "Hardware (ALTRO) adress (" << hwAddress << ") outside the range (0 -> " << maxHWAddress << ") !";
172 return kNotInitialized;
173 }
174 int row, col, caloFlag;
175 if (!(fIn >> row >> col >> caloFlag)) {
176 LOG(fatal) << "Syntax of mapping file " << p << "/Mod" << m << "RCU" << i << ".data is wrong: no (raw col caloFlag)";
177 return kNotInitialized;
178 }
179
180 if (caloFlag < 0 || caloFlag > 2) {
181 LOG(fatal) << "Wrong CaloFlag value found (" << caloFlag << "). Should be 0, 1, 2 !";
182 return kNotInitialized;
183 }
184
185 // convert ddl, col,raw caloFlag to AbsId
186 // Converts the absolute numbering into the following array
187 // relid[0] = PHOS Module number
188 // relid[1] = Row number inside a PHOS module (Phi coordinate)
189 // relid[2] = Column number inside a PHOS module (Z coordinate)
190 short ddl = 4 * m + i - 2;
191 if (ddl < 0 || ddl >= NDDL) {
192 LOG(fatal) << "Wrong ddl address found (" << ddl << "). Module= " << m << " RCU =" << i;
193 return kNotInitialized;
194 }
195
196 short absId;
197 if (caloFlag < 2) { // readout channels
198 char relid[3] = {static_cast<char>(m + 1), static_cast<char>(row + 1), static_cast<char>(col + 1)};
199 Geometry::relToAbsNumbering(relid, absId);
200 } else { // TRU channels: internal storage of TRU channesl absId-NCHANNELS
201 if (isTRUReadoutchannel(hwAddress)) {
202 if (hwAddress < 2048) { // branch 28<=z<56
203 absId = 1 + ddl * 2 * NTRUBranchReadoutChannels + hwAddress;
204 } else { // branch 0<=z<28
205 absId = 1 + (ddl * 2 + 1) * NTRUBranchReadoutChannels + hwAddress - 2048;
206 }
207 } else { // TRU flag channels, no absId
208 continue;
209 }
210 }
211
212 mAbsId[ddl][hwAddress] = absId;
213 mCaloFlag[ddl][hwAddress] = (CaloFlag)caloFlag;
214 mAbsToHW[absId - 1][caloFlag][0] = ddl;
215 mAbsToHW[absId - 1][caloFlag][1] = hwAddress;
216 }
217 fIn.close();
218 } // RCU
219 } // module
220 mInitialized = true;
221 return kOK;
222}
int32_t i
uint32_t col
Definition RawData.h:4
static bool relToAbsNumbering(const char *RelId, short &AbsId)
Definition Geometry.cxx:207
static constexpr short NDDL
Total number of DDLs.
Definition Mapping.h:45
static constexpr short NMaxHWAddress
Maximal HW address (size of array)
Definition Mapping.h:44
static Mapping * Instance()
Definition Mapping.cxx:29
static constexpr short NTRUBranchReadoutChannels
Number of TRU readout channels per branch.
Definition Mapping.h:46
static bool isTRUReadoutchannel(short hwAddress)
Definition Mapping.h:112
static constexpr short NHWPERDDL
Number of HW addressed per DDL.
Definition Mapping.h:43
ErrorStatus setMapping()
Definition Mapping.cxx:122
static constexpr short NCHANNELS
Number of channels starting from 1.
Definition Mapping.h:42
ErrorStatus hwToAbsId(short ddl, short hw, short &absId, CaloFlag &caloFlag) const
convert hardware address to absId and caloFlag
Definition Mapping.cxx:54
ErrorStatus absIdTohw(short absId, short caloFlag, short &ddl, short &hwAddr) const
convert absId and caloflag to hardware address and ddl
Definition Mapping.cxx:89
static constexpr short NTRUReadoutChannels
Total number of TRU readout channels.
Definition Mapping.h:47
const GLfloat * m
Definition glcorearb.h:4066
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< int > row