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