Belle II Software  release-06-00-14
ECLChannelMapper.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 //
9 #include <ecl/utility/ECLChannelMapper.h>
10 #include <rawdata/dataobjects/RawCOPPERFormat.h>
11 #include <framework/database/DBObjPtr.h>
12 #include <framework/utilities/FileSystem.h>
13 //
14 #include <fstream>
15 #include <string>
16 
17 using namespace Belle2;
18 using namespace std;
19 using namespace ECL;
20 
22 {
23  int i;
24  for (i = 0; i < ECL_BARREL_CRATES * ECL_BARREL_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER; i++)
25  convertArrayBarrel[i] = 0;
26  for (i = 0; i < ECL_FWD_CRATES * ECL_FWD_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER; i++)
27  convertArrayFWD[i] = 0;
28  for (i = 0; i < ECL_BKW_CRATES * ECL_BKW_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER; i++)
29  convertArrayBKW[i] = 0;
30  for (i = 0; i < ECL_TOTAL_CHANNELS; i++)
31  for (int j = 0; j < 3; j++)
32  convertArrayInv[i][j] = 0;
33 
34  isInitialized = false;
35 
36 }
37 
39 {
40  std::string filePath = FileSystem::findFile("ecl/data/ecl_channels_map.txt");
41  return initFromFile(filePath.c_str());
42 }
43 
44 bool ECLChannelMapper::initFromFile(const char* eclMapFileName)
45 {
46  B2WARNING("Reading possibly outdated ECLChannelMap from " << eclMapFileName);
47 
48  ifstream mapFile(eclMapFileName);
49  if (mapFile.is_open()) {
50 
51  float iCrate, iShaper, iChannel, thetaID, phiID, cellID;
52  int arrayIndex = 0;
53  int arrayCount = 0;
54  while (mapFile.good()) {
55 
56  // Ignoring commented lines
57  char ch = mapFile.get();
58  if (ch == '#') {
59  mapFile.ignore(256, '\n');
60  continue;
61  } else if (ch == '\n') {
62  continue;
63  } else {
64  mapFile.unget();
65  }
66 
67  mapFile >> iCrate >> iShaper >> iChannel >> thetaID >> phiID >> cellID;
68 
69  if (cellID > ECL_TOTAL_CHANNELS) {
70  B2ERROR("ECLChannelMapper:: wrong cellID in the init file " << eclMapFileName);
71  return false;
72  }
73 
74  if (cellID > 0) {
75  convertArrayInv[(int)cellID - 1][0] = (int)iCrate;
76  convertArrayInv[(int)cellID - 1][1] = (int)iShaper;
77  convertArrayInv[(int)cellID - 1][2] = (int)iChannel;
78  }
79 
80  // Barrel
81  if (iCrate >= 1 && iCrate <= 36) {
82  arrayIndex = arrayCount;
83  convertArrayBarrel[arrayIndex] = (int)cellID;
84  }
85 
86  // Forward endcap
87  if (iCrate > 36 && iCrate < 45) {
88  arrayIndex = arrayCount - 36 * 12 * 16;
89 
90  if (arrayIndex >= 0 && arrayIndex < ECL_FWD_CRATES * ECL_FWD_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER)
91  convertArrayFWD[arrayIndex] = (int)cellID;
92  }
93 
94  // Backward endcap
95  if (iCrate > 44) {
96  arrayIndex = arrayCount - 36 * 12 * 16 - 8 * 10 * 16;
97  if (arrayIndex >= 0 && arrayIndex < ECL_BKW_CRATES * ECL_BKW_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER)
98  convertArrayBKW[arrayIndex] = (int)cellID;
99  }
100  arrayCount++;
101  }
102  } else {
103  B2ERROR("ERROR:: file " << eclMapFileName << " doesn't found");
104  return false;
105  }
106 
107  isInitialized = true;
108 
109  return true;
110 }
111 
113 {
114  DBObjPtr<Belle2::ECLChannelMap> channelMap("ECLChannelMap");
115  // Re-initialize only if interval of validity was changed
116  if (isInitialized && !channelMap.hasChanged()) {
117  return true;
118  }
119 
120  if (!channelMap.isValid()) {
121  B2FATAL("ECLChannelMapper:: Could not get ECLChannelMap from the database.");
122  }
123 
124  B2INFO("ECLChannelMapper:: loaded ECLChannelMap from the database"
125  << LogVar("IoV", channelMap.getIoV()));
126 
127  const auto& mappingBAR = channelMap->getMappingBAR();
128  const auto& mappingFWD = channelMap->getMappingFWD();
129  const auto& mappingBWD = channelMap->getMappingBWD();
130 
131  int cellID;
132  // Loop variables
133  int iCrate = 1, iShaper = 1, iChannel = 1;
134  // Index in currently used array (converArrayBarrel,*FWD,*BKW)
135  int arrayIndex = 0;
136  int iMaxShapers = ECL_BARREL_SHAPERS_IN_CRATE;
137 
138  while (iCrate <= ECL_CRATES) {
139  if (iCrate <= ECL_BARREL_CRATES) {
140  // Barrel
141  cellID = mappingBAR[arrayIndex];
142  convertArrayBarrel[arrayIndex] = cellID;
143  } else if (iCrate <= ECL_BARREL_CRATES + ECL_FWD_CRATES) {
144  // Forward endcap
145  cellID = mappingFWD[arrayIndex];
146  convertArrayFWD[arrayIndex] = cellID;
147  } else {
148  // Backward endcap
149  cellID = mappingBWD[arrayIndex];
150  convertArrayBKW[arrayIndex] = cellID;
151  }
152 
153  if (cellID > ECL_TOTAL_CHANNELS) {
154  B2FATAL("ECLChannelMapper:: wrong cellID (" << cellID << ") in the database payload");
155  return false;
156  }
157 
158  if (cellID > 0) {
159  convertArrayInv[cellID - 1][0] = iCrate;
160  convertArrayInv[cellID - 1][1] = iShaper;
161  convertArrayInv[cellID - 1][2] = iChannel;
162  }
163 
164  arrayIndex++;
165  // Increment all indices, accounting for hierarchical
166  // structure of channels <- shapers <- crates
167  iChannel++;
168  if (iChannel > ECL_CHANNELS_IN_SHAPER) {
169  iChannel = 1;
170  iShaper++;
171  if (iShaper > iMaxShapers) {
172  iShaper = 1;
173  if (iCrate == ECL_BARREL_CRATES) {
174  arrayIndex = 0;
175  iMaxShapers = getNShapersInCrate(iCrate + 1);
176  } else if (iCrate == ECL_BARREL_CRATES + ECL_FWD_CRATES) {
177  arrayIndex = 0;
178  iMaxShapers = getNShapersInCrate(iCrate + 1);
179  }
180  iCrate++;
181  }
182  }
183  }
184 
185  isInitialized = true;
186 
187  return true;
188 }
189 
191 {
192  ECLChannelMap map;
193 
194  if (!isInitialized) {
195  B2FATAL("ECLChannelMapper:: Tried to generate dbobject before initialization");
196  }
197 
198  int mappingBAR_size = ECL_BARREL_CRATES * ECL_BARREL_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER;
199  int mappingFWD_size = ECL_FWD_CRATES * ECL_FWD_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER;
200  int mappingBWD_size = ECL_BKW_CRATES * ECL_BKW_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER;
201 
202  std::vector<int> mappingBAR(mappingBAR_size);
203  std::vector<int> mappingFWD(mappingFWD_size);
204  std::vector<int> mappingBWD(mappingBWD_size);
205 
206  for (int i = 0; i < mappingBAR_size; i++)
207  mappingBAR[i] = convertArrayBarrel[i];
208  for (int i = 0; i < mappingFWD_size; i++)
209  mappingFWD[i] = convertArrayFWD[i];
210  for (int i = 0; i < mappingBWD_size; i++)
211  mappingBWD[i] = convertArrayBKW[i];
212 
213  map.setMappingVectors(mappingBAR, mappingFWD, mappingBWD);
214 
215  return map;
216 }
217 
218 int ECLChannelMapper::getCrateID(int iCOPPERNode, int iFINESSE)
219 {
220  int iCrate;
221 
222  if (iFINESSE > ECL_FINESSES_IN_COPPER - 1) {
223  B2ERROR("ECLChannelMapper::ERROR:: wrong FINESSE " << iFINESSE);
224  return -1;
225  }
226 
227  if ((iCOPPERNode & BECL_ID) == BECL_ID) {
228 
229  iCrate = (iCOPPERNode - BECL_ID - 1) * ECL_FINESSES_IN_COPPER + iFINESSE + 1;
230 
231  } else if ((iCOPPERNode & EECL_ID) == EECL_ID) {
232 
233  iCrate = ECL_BARREL_CRATES + iFINESSE * ECL_FWD_CRATES + (iCOPPERNode - EECL_ID - 1) + 1;
234 
235  } else {
236 
237  B2ERROR("ECLChannelMapper::ERROR:: wrong COPPER NodeID 0x" << std::hex << iCOPPERNode << " BECL_ID 0x" << BECL_ID << " EECL_ID 0x"
238  << EECL_ID);
239  return -1;
240  }
241 // B2DEBUG(100,"ECLChannelMapper:: " << std::hex << "0x"<<iCOPPERNode << " " << iFINESSE << " iCrate = " << std::dec << iCrate);
242  if (iCrate > ECL_CRATES || iCrate < 1) {
243  B2ERROR("ECLChannelMapper::getCrateID::ERROR:: wrong crate number " << iCrate << " return -1");
244  return -1;
245  }
246 
247  return iCrate;
248 
249 }
250 
251 int ECLChannelMapper::getCellId(int iCrate, int iShaper, int iChannel)
252 {
253  // iCrate = 1 - 36 -- Barrel
254  // 37 - 44 -- Forward
255  // 45 - 52 -- Backward
256  int cellID = 0;
257  int arrayIndex;
258 
259  if (iCrate < 1 || iCrate > 52) return -1;
260  if (iShaper < 1 || iShaper > 12) return -1;
261  if (iChannel < 1 || iChannel > 16) return -1;
262 
263  if (iCrate >= 1 && iCrate <= 36) {
264  //int thetaID = 1 + (iShaper - 1) * 4 + (iChannel - 1) % 4;
265  //int phiID = 1 + (iCrate - 1) * 4 + (iChannel - 1) / 4;
266  //cellID = 1 + ECL_FWD_CHANNELS + (phiID - 1) + (thetaID - 1) * 144;
267 
268  arrayIndex = (iCrate - 1) * ECL_BARREL_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER
269  + (iShaper - 1) * ECL_CHANNELS_IN_SHAPER + (iChannel - 1);
270  cellID = convertArrayBarrel[arrayIndex];
271 
272  }
273 
274  if (iCrate > 36 && iCrate < 45) {
275  arrayIndex = (iCrate - 37) * ECL_FWD_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER
276  + (iShaper - 1) * ECL_CHANNELS_IN_SHAPER + (iChannel - 1);
277  cellID = convertArrayFWD[arrayIndex];
278  }
279 
280  if (iCrate > 44) {
281  arrayIndex = (iCrate - 45) * ECL_BKW_SHAPERS_IN_CRATE * ECL_CHANNELS_IN_SHAPER
282  + (iShaper - 1) * ECL_CHANNELS_IN_SHAPER + (iChannel - 1);
283  cellID = convertArrayBKW[arrayIndex];
284  }
285 
286 
287  return cellID;
288 }
290 {
291  if (cellID > 0 && cellID <= ECL_TOTAL_CHANNELS) {
292  return convertArrayInv[cellID - 1 ][0];
293  } else return -1;
294 }
295 
297 {
298  if (cellID > 0 && cellID <= ECL_TOTAL_CHANNELS) {
299  return convertArrayInv[cellID - 1][1];
300  } else return -1;
301 }
302 
304 {
305  if (cellID > 0 && cellID <= ECL_TOTAL_CHANNELS) {
306  return convertArrayInv[cellID - 1][2];
307  } else return -1;
308 }
309 
311 {
312  int systemID, iNode, iCOPPERNode;
313 
314  if (iCrate < 1 || iCrate > ECL_CRATES) return -1;
315  systemID = (iCrate <= ECL_BARREL_CRATES) * BECL_ID + (iCrate > ECL_BARREL_CRATES) * EECL_ID;
316  iNode = (iCrate <= ECL_BARREL_CRATES) ? (iCrate - 1) / 2 : (iCrate - ECL_BARREL_CRATES - 1) % 8;
317  iCOPPERNode = systemID + iNode + 1;
318 
319  return iCOPPERNode;
320 }
321 
323 {
324  if (iCrate < 1 || iCrate > ECL_CRATES) return -1;
325 
326  return (iCrate <= ECL_BARREL_CRATES) ? (iCrate - 1) % 2 : (iCrate - ECL_BARREL_CRATES - 1) / 8;
327 }
328 
330 {
331  if (iCrate <= ECL_BARREL_CRATES) return 0;
332  if (ECL_BARREL_CRATES < iCrate && iCrate <= ECL_BARREL_CRATES + ECL_FWD_CRATES)
333  return 1;
334  if (ECL_BARREL_CRATES + ECL_FWD_CRATES < iCrate && iCrate <= ECL_CRATES) return 2;
335  return -1;
336 }
337 
bool isValid() const
Check whether a valid object was obtained from the database.
IntervalOfValidity getIoV() const
Return current IoV of the object.
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
DB object to store correspondence table of type (Crate id, ShaperDSP id, Channel id) <-> (ECL CellID)
Definition: ECLChannelMap.h:44
void setMappingVectors(const std::vector< int > &mappingBAR, const std::vector< int > &mappingFWD, const std::vector< int > &mappingBWD)
Set three vectors of map entries.
Definition: ECLChannelMap.h:93
const std::vector< int > & getMappingBAR() const
Get vector of map entries for ECL barrel.
Definition: ECLChannelMap.h:52
const std::vector< int > & getMappingBWD() const
Get vector of map entries for ECL backward endcap.
Definition: ECLChannelMap.h:56
const std::vector< int > & getMappingFWD() const
Get vector of map entries for ECL forward endcap.
Definition: ECLChannelMap.h:54
int getSubSystem(int iCrate)
get ECL subsystem ID by given crate number: 0 – barrel, 1 – forward. 2 – backward endcap
bool initFromDB()
Initialize channel mapper from the conditions database.
ECLChannelMap getDBObject()
Convert internal data to ECLChannelMap database object.
int getCrateID(int iCOPPERNode, int iFINESSE)
get crate number by given COPPER node number and FINESSE number
bool initFromFile()
Initialize channel mapper using data stored in default location.
int getFINESSE(int iCrate)
get number of FINESSE (0/1) in COPPER by given crate number
ECLChannelMapper()
Default constructor.
int getCellId(int iCrate, int iShaper, int iChannel)
get CellId by given crate number, shaper position in the crate and DSP channel number in the shaper
int getShaperChannel(int cellID)
get number of DSP channel in the shaper by given number of CellId
int getShaperPosition(int cellID)
get position of the shaper in the crate by given CellId
int getCOPPERNode(int iCrate)
get number of COPPER node by given crate number
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:145
Class to store variables with their name which were sent to the logging service.
Abstract base class for different kinds of events.