CMS 3D CMS Logo

ModuleMethods.h
Go to the documentation of this file.
1 #ifndef RecoTracker_LSTCore_src_ModuleMethods_h
2 #define RecoTracker_LSTCore_src_ModuleMethods_h
3 
4 #include <map>
5 #include <iostream>
6 
14 
17 
18 namespace lst {
19  struct ModuleMetaData {
20  std::map<unsigned int, uint16_t> detIdToIndex;
21  std::map<unsigned int, float> module_x;
22  std::map<unsigned int, float> module_y;
23  std::map<unsigned int, float> module_z;
24  std::map<unsigned int, unsigned int> module_type; // 23 : Ph2PSP, 24 : Ph2PSS, 25 : Ph2SS
25  // https://github.com/cms-sw/cmssw/blob/5e809e8e0a625578aa265dc4b128a93830cb5429/Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h#L29
26  };
27 
28  bool parseIsLower(bool isInvertedx, unsigned int detId) { return (isInvertedx) ? !(detId & 1) : (detId & 1); }
29 
30  unsigned int parsePartnerModuleId(unsigned int detId, bool isLowerx, bool isInvertedx) {
31  return isLowerx ? (isInvertedx ? detId - 1 : detId + 1) : (isInvertedx ? detId + 1 : detId - 1);
32  }
33 
34  bool parseIsInverted(short subdet, short side, short module, short layer) {
35  if (subdet == Endcap) {
36  if (side == NegZ) {
37  return module % 2 == 1;
38  } else if (side == PosZ) {
39  return module % 2 == 0;
40  } else {
41  return false;
42  }
43  } else if (subdet == Barrel) {
44  if (side == Center) {
45  if (layer <= 3) {
46  return module % 2 == 1;
47  } else if (layer >= 4) {
48  return module % 2 == 0;
49  } else {
50  return false;
51  }
52  } else if (side == NegZ or side == PosZ) {
53  if (layer <= 2) {
54  return module % 2 == 1;
55  } else if (layer == 3) {
56  return module % 2 == 0;
57  } else {
58  return false;
59  }
60  } else {
61  return false;
62  }
63  } else {
64  return false;
65  }
66  }
67 
68  inline std::tuple<unsigned int,
69  std::vector<unsigned int>,
70  unsigned int,
71  std::vector<unsigned int>,
72  unsigned int,
73  std::vector<unsigned int>>
74  getConnectedPixels(uint16_t nModules, unsigned int& nPixels, PixelMap& pixelMapping, MapPLStoLayer const& pLStoLayer) {
75  std::vector<unsigned int> connectedModuleDetIds;
76  std::vector<unsigned int> connectedModuleDetIds_pos;
77  std::vector<unsigned int> connectedModuleDetIds_neg;
78 
79  unsigned int totalSizes = 0;
80  unsigned int totalSizes_pos = 0;
81  unsigned int totalSizes_neg = 0;
82  for (unsigned int isuperbin = 0; isuperbin < size_superbins; isuperbin++) {
83  int sizes = 0;
84  for (auto const& mCM_pLS : pLStoLayer[0]) {
85  std::vector<unsigned int> connectedModuleDetIds_pLS =
86  mCM_pLS.getConnectedModuleDetIds(isuperbin + size_superbins);
87  connectedModuleDetIds.insert(
88  connectedModuleDetIds.end(), connectedModuleDetIds_pLS.begin(), connectedModuleDetIds_pLS.end());
89  sizes += connectedModuleDetIds_pLS.size();
90  }
91  pixelMapping.connectedPixelsIndex[isuperbin] = totalSizes;
92  pixelMapping.connectedPixelsSizes[isuperbin] = sizes;
93  totalSizes += sizes;
94 
95  int sizes_pos = 0;
96  for (auto const& mCM_pLS : pLStoLayer[1]) {
97  std::vector<unsigned int> connectedModuleDetIds_pLS_pos = mCM_pLS.getConnectedModuleDetIds(isuperbin);
98  connectedModuleDetIds_pos.insert(connectedModuleDetIds_pos.end(),
99  connectedModuleDetIds_pLS_pos.begin(),
100  connectedModuleDetIds_pLS_pos.end());
101  sizes_pos += connectedModuleDetIds_pLS_pos.size();
102  }
103  pixelMapping.connectedPixelsIndexPos[isuperbin] = totalSizes_pos;
104  pixelMapping.connectedPixelsSizesPos[isuperbin] = sizes_pos;
105  totalSizes_pos += sizes_pos;
106 
107  int sizes_neg = 0;
108  for (auto const& mCM_pLS : pLStoLayer[2]) {
109  std::vector<unsigned int> connectedModuleDetIds_pLS_neg = mCM_pLS.getConnectedModuleDetIds(isuperbin);
110  connectedModuleDetIds_neg.insert(connectedModuleDetIds_neg.end(),
111  connectedModuleDetIds_pLS_neg.begin(),
112  connectedModuleDetIds_pLS_neg.end());
113  sizes_neg += connectedModuleDetIds_pLS_neg.size();
114  }
115  pixelMapping.connectedPixelsIndexNeg[isuperbin] = totalSizes_neg;
116  pixelMapping.connectedPixelsSizesNeg[isuperbin] = sizes_neg;
117  totalSizes_neg += sizes_neg;
118  }
119 
120  nPixels = totalSizes + totalSizes_pos + totalSizes_neg;
121 
122  return {totalSizes,
123  connectedModuleDetIds,
124  totalSizes_pos,
125  connectedModuleDetIds_pos,
126  totalSizes_neg,
127  connectedModuleDetIds_neg};
128  }
129 
131  ModuleMetaData const& mmd,
132  ModuleConnectionMap const& moduleConnectionMap) {
133  Params_Modules::ArrayU16xMaxConnected* moduleMap = modules.moduleMap();
134  uint16_t* nConnectedModules = modules.nConnectedModules();
135 
136  for (auto it = mmd.detIdToIndex.begin(); it != mmd.detIdToIndex.end(); ++it) {
137  unsigned int detId = it->first;
138  uint16_t index = it->second;
139  auto& connectedModules = moduleConnectionMap.getConnectedModuleDetIds(detId);
140  nConnectedModules[index] = connectedModules.size();
141  for (uint16_t i = 0; i < nConnectedModules[index]; i++) {
142  moduleMap[index][i] = mmd.detIdToIndex.at(connectedModules[i]);
143  }
144  }
145  }
146 
148  uint16_t* mapIdx = modules.mapIdx();
149  unsigned int* mapdetId = modules.mapdetId();
150 
151  unsigned int counter = 0;
152  for (auto it = mmd.detIdToIndex.begin(); it != mmd.detIdToIndex.end(); ++it) {
153  unsigned int detId = it->first;
154  unsigned int index = it->second;
155  mapIdx[counter] = index;
156  mapdetId[counter] = detId;
157  counter++;
158  }
159  }
160 
161  inline void setDerivedQuantities(unsigned int detId,
162  unsigned short& layer,
163  unsigned short& ring,
164  unsigned short& rod,
165  unsigned short& module,
166  unsigned short& subdet,
167  unsigned short& side,
168  float m_x,
169  float m_y,
170  float m_z,
171  float& eta,
172  float& r) {
173  subdet = (detId & (7 << 25)) >> 25;
174  side = (subdet == Endcap) ? (detId & (3 << 23)) >> 23 : (detId & (3 << 18)) >> 18;
175  layer = (subdet == Endcap) ? (detId & (7 << 18)) >> 18 : (detId & (7 << 20)) >> 20;
176  ring = (subdet == Endcap) ? (detId & (15 << 12)) >> 12 : 0;
177  module = (detId & (127 << 2)) >> 2;
178  rod = (subdet == Endcap) ? 0 : (detId & (127 << 10)) >> 10;
179 
180  r = std::sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
181  eta = ((m_z > 0) - (m_z < 0)) * std::acosh(r / std::sqrt(m_x * m_x + m_y * m_y));
182  }
183 
184  inline void loadCentroidsFromFile(const char* filePath, ModuleMetaData& mmd, uint16_t& nModules) {
185  std::ifstream ifile(filePath, std::ios::binary);
186  if (!ifile.is_open()) {
187  throw std::runtime_error("Unable to open file: " + std::string(filePath));
188  }
189 
190  uint16_t counter = 0;
191  while (!ifile.eof()) {
192  unsigned int temp_detId;
193  float module_x, module_y, module_z;
194  int module_type;
195 
196  ifile.read(reinterpret_cast<char*>(&temp_detId), sizeof(temp_detId));
197  ifile.read(reinterpret_cast<char*>(&module_x), sizeof(module_x));
198  ifile.read(reinterpret_cast<char*>(&module_y), sizeof(module_y));
199  ifile.read(reinterpret_cast<char*>(&module_z), sizeof(module_z));
200  ifile.read(reinterpret_cast<char*>(&module_type), sizeof(module_type));
201 
202  if (ifile) {
203  mmd.detIdToIndex[temp_detId] = counter;
204  mmd.module_x[temp_detId] = module_x;
205  mmd.module_y[temp_detId] = module_y;
206  mmd.module_z[temp_detId] = module_z;
207  mmd.module_type[temp_detId] = module_type;
208  counter++;
209  } else {
210  if (!ifile.eof()) {
211  throw std::runtime_error("Failed to read data for detId: " + std::to_string(temp_detId));
212  }
213  }
214  }
215 
216  mmd.detIdToIndex[1] = counter; //pixel module is the last module in the module list
217  counter++;
218  nModules = counter;
219  }
220 
221  inline std::shared_ptr<ModulesHostCollection> loadModulesFromFile(MapPLStoLayer const& pLStoLayer,
222  const char* moduleMetaDataFilePath,
223  uint16_t& nModules,
224  uint16_t& nLowerModules,
225  unsigned int& nPixels,
226  PixelMap& pixelMapping,
227  const EndcapGeometry& endcapGeometry,
228  const TiltedGeometry& tiltedGeometry,
229  const ModuleConnectionMap& moduleConnectionMap) {
230  ModuleMetaData mmd;
231 
232  loadCentroidsFromFile(moduleMetaDataFilePath, mmd, nModules);
233 
234  // TODO: this whole section could use some refactoring
235  auto [totalSizes,
236  connectedModuleDetIds,
237  totalSizes_pos,
238  connectedModuleDetIds_pos,
239  totalSizes_neg,
240  connectedModuleDetIds_neg] = getConnectedPixels(nModules, nPixels, pixelMapping, pLStoLayer);
241 
242  std::array<int, 2> const modules_sizes{{static_cast<int>(nModules), static_cast<int>(nPixels)}};
243 
244  auto modulesHC = std::make_shared<ModulesHostCollection>(modules_sizes, cms::alpakatools::host());
245 
246  auto modules_view = modulesHC->view<ModulesSoA>();
247 
248  // Getting the underlying data pointers
249  unsigned int* host_detIds = modules_view.detIds();
250  short* host_layers = modules_view.layers();
251  short* host_rings = modules_view.rings();
252  short* host_rods = modules_view.rods();
253  short* host_modules = modules_view.modules();
254  short* host_subdets = modules_view.subdets();
255  short* host_sides = modules_view.sides();
256  float* host_eta = modules_view.eta();
257  float* host_r = modules_view.r();
258  bool* host_isInverted = modules_view.isInverted();
259  bool* host_isLower = modules_view.isLower();
260  bool* host_isAnchor = modules_view.isAnchor();
261  ModuleType* host_moduleType = modules_view.moduleType();
262  ModuleLayerType* host_moduleLayerType = modules_view.moduleLayerType();
263  float* host_dxdys = modules_view.dxdys();
264  float* host_drdzs = modules_view.drdzs();
265  uint16_t* host_nModules = &modules_view.nModules();
266  uint16_t* host_nLowerModules = &modules_view.nLowerModules();
267  uint16_t* host_partnerModuleIndices = modules_view.partnerModuleIndices();
268  int* host_lstLayers = modules_view.lstLayers();
269 
270  //reassign detIdToIndex indices here
271  nLowerModules = (nModules - 1) / 2;
272  uint16_t lowerModuleCounter = 0;
273  uint16_t upperModuleCounter = nLowerModules + 1;
274  //0 to nLowerModules - 1 => only lower modules, nLowerModules - pixel module, nLowerModules + 1 to nModules => upper modules
275  for (auto it = mmd.detIdToIndex.begin(); it != mmd.detIdToIndex.end(); it++) {
276  unsigned int detId = it->first;
277  float m_x = mmd.module_x[detId];
278  float m_y = mmd.module_y[detId];
279  float m_z = mmd.module_z[detId];
280  unsigned int m_t = mmd.module_type[detId];
281 
282  float eta, r;
283 
284  uint16_t index;
285  unsigned short layer, ring, rod, module, subdet, side;
286  bool isInverted, isLower;
287  if (detId == 1) {
288  layer = 0;
289  ring = 0;
290  rod = 0;
291  module = 0;
292  subdet = 0;
293  side = 0;
294  isInverted = false;
295  isLower = false;
296  eta = 0;
297  r = 0;
298  } else {
299  setDerivedQuantities(detId, layer, ring, rod, module, subdet, side, m_x, m_y, m_z, eta, r);
300  isInverted = parseIsInverted(subdet, side, module, layer);
301  isLower = parseIsLower(isInverted, detId);
302  }
303  if (isLower) {
304  index = lowerModuleCounter;
305  lowerModuleCounter++;
306  } else if (detId != 1) {
307  index = upperModuleCounter;
308  upperModuleCounter++;
309  } else {
310  index = nLowerModules; //pixel
311  }
312  //reassigning indices!
313  mmd.detIdToIndex[detId] = index;
314  host_detIds[index] = detId;
315  host_layers[index] = layer;
316  host_rings[index] = ring;
317  host_rods[index] = rod;
318  host_modules[index] = module;
319  host_subdets[index] = subdet;
320  host_sides[index] = side;
321  host_eta[index] = eta;
322  host_r[index] = r;
323  host_isInverted[index] = isInverted;
324  host_isLower[index] = isLower;
325 
326  //assigning other variables!
327  if (detId == 1) {
328  host_moduleType[index] = PixelModule;
329  host_moduleLayerType[index] = lst::InnerPixelLayer;
330  host_dxdys[index] = 0;
331  host_drdzs[index] = 0;
332  host_isAnchor[index] = false;
333  } else {
334  host_moduleType[index] = (m_t == 25 ? lst::TwoS : lst::PS);
335  host_moduleLayerType[index] = (m_t == 23 ? lst::Pixel : lst::Strip);
336 
337  if (host_moduleType[index] == lst::PS and host_moduleLayerType[index] == lst::Pixel) {
338  host_isAnchor[index] = true;
339  } else if (host_moduleType[index] == lst::TwoS and host_isLower[index]) {
340  host_isAnchor[index] = true;
341  } else {
342  host_isAnchor[index] = false;
343  }
344 
345  host_dxdys[index] = (subdet == Endcap) ? endcapGeometry.getdxdy_slope(detId) : tiltedGeometry.getDxDy(detId);
346  host_drdzs[index] = (subdet == Barrel) ? tiltedGeometry.getDrDz(detId) : 0;
347  }
348 
349  host_lstLayers[index] =
350  layer + 6 * (subdet == lst::Endcap) + 5 * (subdet == lst::Endcap and host_moduleType[index] == lst::TwoS);
351  }
352 
353  //partner module stuff, and slopes and drdz move around
354  for (auto it = mmd.detIdToIndex.begin(); it != mmd.detIdToIndex.end(); it++) {
355  auto& detId = it->first;
356  auto& index = it->second;
357  if (detId != 1) {
358  host_partnerModuleIndices[index] =
359  mmd.detIdToIndex[parsePartnerModuleId(detId, host_isLower[index], host_isInverted[index])];
360  //add drdz and slope importing stuff here!
361  if (host_drdzs[index] == 0) {
362  host_drdzs[index] = host_drdzs[host_partnerModuleIndices[index]];
363  }
364  if (host_dxdys[index] == 0) {
365  host_dxdys[index] = host_dxdys[host_partnerModuleIndices[index]];
366  }
367  }
368  }
369 
370  *host_nModules = nModules;
371  *host_nLowerModules = nLowerModules;
372 
373  // Fill pixel part
374  pixelMapping.pixelModuleIndex = mmd.detIdToIndex.at(1);
375 
376  auto modulesPixel_view = modulesHC->view<ModulesPixelSoA>();
377  auto connectedPixels =
378  cms::alpakatools::make_host_view(modulesPixel_view.connectedPixels(), modulesPixel_view.metadata().size());
379  for (unsigned int icondet = 0; icondet < totalSizes; icondet++) {
380  connectedPixels[icondet] = mmd.detIdToIndex.at(connectedModuleDetIds[icondet]);
381  }
382  for (unsigned int icondet = 0; icondet < totalSizes_pos; icondet++) {
383  connectedPixels[icondet + totalSizes] = mmd.detIdToIndex.at(connectedModuleDetIds_pos[icondet]);
384  }
385  for (unsigned int icondet = 0; icondet < totalSizes_neg; icondet++) {
386  connectedPixels[icondet + totalSizes + totalSizes_pos] = mmd.detIdToIndex.at(connectedModuleDetIds_neg[icondet]);
387  }
388 
389  fillConnectedModuleArrayExplicit(modules_view, mmd, moduleConnectionMap);
390  fillMapArraysExplicit(modules_view, mmd);
391 
392  return modulesHC;
393  }
394 } // namespace lst
395 #endif
ModuleType
Definition: ModulesSoA.h:15
std::shared_ptr< ModulesHostCollection > loadModulesFromFile(MapPLStoLayer const &pLStoLayer, const char *moduleMetaDataFilePath, uint16_t &nModules, uint16_t &nLowerModules, unsigned int &nPixels, PixelMap &pixelMapping, const EndcapGeometry &endcapGeometry, const TiltedGeometry &tiltedGeometry, const ModuleConnectionMap &moduleConnectionMap)
void loadCentroidsFromFile(const char *filePath, ModuleMetaData &mmd, uint16_t &nModules)
std::vector< unsigned int > connectedPixelsIndexNeg
Definition: PixelMap.h:17
Definition: Common.h:15
std::map< unsigned int, uint16_t > detIdToIndex
Definition: ModuleMethods.h:20
unsigned int parsePartnerModuleId(unsigned int detId, bool isLowerx, bool isInvertedx)
Definition: ModuleMethods.h:30
bool parseIsInverted(short subdet, short side, short module, short layer)
Definition: ModuleMethods.h:34
std::map< unsigned int, unsigned int > module_type
Definition: ModuleMethods.h:24
std::vector< unsigned int > connectedPixelsIndex
Definition: PixelMap.h:13
static std::string to_string(const XMLCh *ch)
std::vector< unsigned int > connectedPixelsSizes
Definition: PixelMap.h:14
void fillConnectedModuleArrayExplicit(Modules modules, ModuleMetaData const &mmd, ModuleConnectionMap const &moduleConnectionMap)
std::map< unsigned int, float > module_y
Definition: ModuleMethods.h:22
T sqrt(T t)
Definition: SSEVec.h:23
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
ModuleLayerType
Definition: ModulesSoA.h:17
std::map< unsigned int, float > module_z
Definition: ModuleMethods.h:23
alpaka::DevCpu const & host()
Definition: host.h:14
const std::vector< unsigned int > & getConnectedModuleDetIds(unsigned int detid) const
float getdxdy_slope(unsigned int detid) const
std::map< unsigned int, float > module_x
Definition: ModuleMethods.h:21
void setDerivedQuantities(unsigned int detId, unsigned short &layer, unsigned short &ring, unsigned short &rod, unsigned short &module, unsigned short &subdet, unsigned short &side, float m_x, float m_y, float m_z, float &eta, float &r)
std::tuple< unsigned int, std::vector< unsigned int >, unsigned int, std::vector< unsigned int >, unsigned int, std::vector< unsigned int > > getConnectedPixels(uint16_t nModules, unsigned int &nPixels, PixelMap &pixelMapping, MapPLStoLayer const &pLStoLayer)
Definition: ModuleMethods.h:74
std::vector< unsigned int > connectedPixelsSizesNeg
Definition: PixelMap.h:18
std::array< std::array< ModuleConnectionMap, 4 >, 3 > MapPLStoLayer
void fillMapArraysExplicit(Modules modules, ModuleMetaData const &mmd)
uint16_t pixelModuleIndex
Definition: PixelMap.h:11
static std::atomic< unsigned int > counter
std::vector< unsigned int > connectedPixelsIndexPos
Definition: PixelMap.h:15
std::enable_if_t< not std::is_array_v< T >, host_view< T > > make_host_view(T &data)
Definition: memory.h:153
bool parseIsLower(bool isInvertedx, unsigned int detId)
Definition: ModuleMethods.h:28
float getDrDz(unsigned int detid) const
constexpr unsigned int size_superbins
Definition: Common.h:41
float getDxDy(unsigned int detid) const
std::vector< unsigned int > connectedPixelsSizesPos
Definition: PixelMap.h:16