Belle II Software  release-05-02-19
CompactSecIDs.h
1 /********************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Eugenio Paoloni *
7  * *
8  * This software is provided "as is" without any warranty. *
9  *******************************************************************************/
10 #pragma once
11 
12 #include "tracking/dataobjects/FullSecID.h"
13 #include <tracking/trackFindingVXD/filterMap/map/SectorsOnSensor.h>
14 #include <framework/core/FrameworkExceptions.h>
15 #include <framework/logging/Logger.h>
16 #include <cstdint>
17 #include <vector>
18 
19 namespace Belle2 {
25  class CompactSecIDs {
31 
32  public:
33 
34  // uint_16_t upper limit is UINT16_MAX
35 
37  typedef uint16_t sectorID_t ;
38 
40  typedef uint32_t secPairID_t ;
41 
43  typedef uint64_t secTripletID_t ;
44 
46  typedef uint64_t secQuadrupletID_t;
47 
50 
52  ~CompactSecIDs() {}
53 
55  int getSize() const {return (int)m_sectorCounter ; }
56 
58  unsigned int getAvailableSlots() const
59  {
60  return UINT16_MAX - (getSize());
61  }
62 
63 
71  int addSectors(const std::vector< double>& normalizedUsup,
72  const std::vector< double>& normalizedVsup,
73  const std::vector< std::vector< FullSecID >>& fullSecIDs)
74  {
75  // First let us check that there is space to store all the sectors
76  if (getAvailableSlots() < (normalizedUsup.size() + 1) *
77  (normalizedVsup.size() + 1))
78  return 0;
79 
80  // Second let us check that the array of fullSecIDs is rectangular
81  // and that its size matches the size of the coordinates.
82  if (fullSecIDs.size() != normalizedUsup.size() + 1 ||
83  fullSecIDs[0].size() != normalizedVsup.size() + 1)
84  return 0;
85 
86  // Further sanity checks
87 
88  auto layer = fullSecIDs[0][0].getLayerID();
89  auto ladder = fullSecIDs[0][0].getLadderID();
90  auto sensor = fullSecIDs[0][0].getVxdID().getSensorNumber();
91 
92  for (auto fullSecIDrow : fullSecIDs) {
93  // Check that the fullSecIDs vector of vector is rectangular
94  if (fullSecIDrow.size() != normalizedVsup.size() + 1)
95  return 0;
96 
97  // Check that all the fullSectorIDs are on the same physical sensor
98  for (auto fullSecID : fullSecIDrow)
99  if (layer != fullSecID.getLayerID() ||
100  ladder != fullSecID.getLadderID() ||
101  sensor != fullSecID.getVxdID().getSensorNumber())
102  return 0;
103  }
104 
106  normalizedUsup, normalizedVsup,
107  fullSecIDs,
108  layer, ladder, sensor);
109 
110  }
111 
112 
116  sectorID_t getCompactID(const FullSecID& fullID) const
117  {
118 
120  fullID.getLayerID(),
121  fullID.getLadderID(),
122  fullID.getVxdID().getSensorNumber(),
123  fullID.getSecID());
124  }
125 
133  getSectorsOnSensor(unsigned layer, unsigned ladder, unsigned sensor)
134  const
135  {
136  try {
137  return m_compactSectorsIDMap.at(layer).at(ladder).at(sensor);
138  } catch (...)
139  { return SectorsOnSensor<sectorID_t>(); }
140 
141  }
142 
144  BELLE2_DEFINE_EXCEPTION(unboundedNormalizedU,
145  "On layer:%1% ladder:%2% sensor:%3% abnormal U=%4$");
146 
148  BELLE2_DEFINE_EXCEPTION(unboundedNormalizedV,
149  "On layer:%1% ladder:%2% sensor:%3% abnormal V=%4$");
150 
153  FullSecID getFullSecID(VxdID aSensorID,
154  double normalizedU, double normalizedV) const
155  {
156 
157  auto layer = aSensorID.getLayerNumber() ;
158  auto ladder = aSensorID.getLadderNumber();
159  auto sensor = aSensorID.getSensorNumber();
160 
161  auto sectorsOnSensor =
162  m_compactSectorsIDMap.at(layer).at(ladder).at(sensor);
163 
164 
165  if (normalizedU < 0. || 1. < normalizedU)
166  B2WARNING("CompactSecIDs: U not normalized! This may lead to undefined behavior!");
167  if (normalizedV < 0. || 1. < normalizedV)
168  B2WARNING("CompactSecIDs: V not normalized! This may lead to undefined behavior!");
169 
170  return sectorsOnSensor(normalizedU, normalizedV);
171 
172 
173  }
174 
178  sectorID_t operator [](const FullSecID& fullID) const
179  {
180  return getCompactID(fullID);
181  }
182 
185  secPairID_t getCompactID(const FullSecID& id1, const FullSecID& id2) const
186  {
187 
188  auto i1 = (secPairID_t)getCompactID(id1);
189  if (i1 == 0)
190  return 0;
191 
192  auto i2 = (secPairID_t)getCompactID(id2);
193  if (i2 == 0)
194  return 0;
195 
196  return i1 + (i2 << 16);
197 
198  }
199 
202  static void extractCompactID(secPairID_t pair_id, sectorID_t& id1, sectorID_t& id2)
203  {
204  id1 = pair_id & 0xffff;
205  id2 = (pair_id >> 16) & 0xffff;
206  }
207 
208 
209 
212  secTripletID_t getCompactID(const FullSecID& id1, const FullSecID& id2 ,
213  const FullSecID& id3) const
214  {
215 
216  auto i1 = (secTripletID_t)getCompactID(id1);
217  if (i1 == 0)
218  return 0;
219 
220  auto i2 = (secTripletID_t)getCompactID(id2);
221  if (i2 == 0)
222  return 0;
223 
224  auto i3 = (secTripletID_t)getCompactID(id3);
225  if (i3 == 0)
226  return 0;
227 
228  return i1 + (i2 << 16) + (i3 << 32);
229 
230  }
231 
234  static void extractCompactID(secTripletID_t triplet_id, sectorID_t& id1, sectorID_t& id2, sectorID_t& id3)
235  {
236  id1 = triplet_id & 0xffff;
237  id2 = (triplet_id >> 16) & 0xffff;
238  id3 = (triplet_id >> 32) & 0xffff;
239  }
240 
243  secQuadrupletID_t getCompactID(const FullSecID& id1, const FullSecID& id2 ,
244  const FullSecID& id3, const FullSecID& id4)
245  const
246  {
247 
248  auto i1 = (secQuadrupletID_t)getCompactID(id1);
249  if (i1 == 0)
250  return 0;
251 
252  auto i2 = (secQuadrupletID_t)getCompactID(id2);
253  if (i2 == 0)
254  return 0;
255 
256  auto i3 = (secQuadrupletID_t)getCompactID(id3);
257  if (i3 == 0)
258  return 0;
259 
260  auto i4 = (secQuadrupletID_t)getCompactID(id4);
261  if (i4 == 0)
262  return 0;
263 
264  return i1 + (i2 << 16) + (i3 << 32) + (i4 << 48) ;
265 
266  }
267 
271  {
272 
273  auto layer = fullID.getLayerID();
274  auto ladder = fullID.getLadderID();
275  auto sensor = fullID.getVxdID().getSensorNumber();
276  auto sector = fullID.getSecID();
277 
278  return m_compactSectorsIDMap[layer][ladder][sensor][sector];
279 
280  }
281 
282 
283 
285  bool areCoordinatesValid(VxdID aSensorID, double normalizedU, double normalizedV) const
286  {
287  auto layer = aSensorID.getLayerNumber() ;
288  auto ladder = aSensorID.getLadderNumber();
289  auto sensor = aSensorID.getSensorNumber();
290 
291  // check sensor:
292  if (!(layer < m_compactSectorsIDMap.size())) return false;
293  if (!(ladder < m_compactSectorsIDMap[layer].size())) return false;
294  if (!(sensor < m_compactSectorsIDMap[layer][ladder].size())) return false;
295 
296  // check u and v:
297  return m_compactSectorsIDMap[layer][ladder][sensor].areCoordinatesValid(normalizedU, normalizedV);
298  }
299 
301  typedef std::vector<SectorsOnSensor<sectorID_t> > SensorsOnLadder_t;
303  typedef std::vector<SensorsOnLadder_t> LaddersOnLayer_t;
305  typedef std::vector<LaddersOnLayer_t> LayersLookUpTable_t;
306 
307 
310 
312  int nOfLayers(void) const
313  { return m_compactSectorsIDMap.size(); }
314 
317  int nOfLadders(int layer) const
318  {
319  if (layer < 0 || layer >= nOfLayers())
320  return 0;
321  return m_compactSectorsIDMap[layer].size();
322  }
323 
326  int nOfSensors(int layer, int ladder) const
327  {
328  if (ladder < 0 || ladder >= nOfLadders(layer))
329  return 0; // Note: the check on the layer is implicitely made
330 
331  return m_compactSectorsIDMap[layer][ladder].size();
332  }
333 
337  bool setSubLayerID(FullSecID& sector, int sublayer)
338  {
339  // cross check if sector is registered in the map, compactID will be 0 if not
340  sectorID_t compactID = getCompactID(sector);
341  if (compactID == 0) return false;
342 
343  // now update the SectorsOnSensor object
344  return m_compactSectorsIDMap[sector.getLayerID()][sector.getLadderID()]
345  [sector.getVxdID().getSensorNumber()].updateSubLayerID(sector, sublayer);
346  }
347 
348 
349  private:
352 
355 
359  template< class TContainer,
360  class ... Indexes >
361  int privateAddSectors(TContainer& container,
362  const std::vector< double >& normalizedUsup,
363  const std::vector< double >& normalizedVsup,
364  const std::vector< std::vector< FullSecID > >& fullSecIDs,
365  short unsigned int index, Indexes ... indexes)
366  {
367  try {
368  if ((int) container.size() < (int)index + 1)
369  container.resize(index + 1);
370  } catch (...) { return 0; }
371  return privateAddSectors(container[ index ],
372  normalizedUsup, normalizedVsup,
373  fullSecIDs, indexes...);
374  }
375 
376 
380  int privateAddSectors(SectorsOnSensor<sectorID_t>& sectors,
381  const std::vector< double>& normalizedUsup,
382  const std::vector< double>& normalizedVsup,
383  const std::vector< std::vector< FullSecID > >& fullSecIDs)
384  {
385  if (sectors.size() != 0)
386  return 0;
387 
388  sectors = SectorsOnSensor< sectorID_t >(normalizedUsup,
389  normalizedVsup, fullSecIDs);
390 
391  int addedSectors = 0;
392 
393  for (auto sectorRow : fullSecIDs)
394  for (auto sector : sectorRow) {
395  auto secID = sector.getSecID();
396  try {
397  if ((int) sectors.size() < (int) secID + 1)
398  sectors.resize(secID + 1);
399  } catch (...) { return addedSectors; }
400 
401  sectors[ secID ] = ++m_sectorCounter ;
402  addedSectors++;
403  }
404  return addedSectors;
405 
406  }
407 
412  template< class TContainer,
413  class ... Indexes >
414  sectorID_t privateGetCompactID(const TContainer& container,
415  short unsigned int index, Indexes ... indexes) const
416  {
417  if ((int) container.size() < (int) index + 1)
418  return 0;
419 
420  return privateGetCompactID(container[ index ], indexes...);
421  }
422 
423 
427  template< class TContainer >
428  sectorID_t privateGetCompactID(const TContainer& container,
429  short unsigned int index) const
430  {
431  if ((int) container.size() < (int) index + 1)
432  return 0;
433 
434  return container[ index ];
435  }
436 
437  };
438 
440 }
Belle2::CompactSecIDs::BELLE2_DEFINE_EXCEPTION
BELLE2_DEFINE_EXCEPTION(unboundedNormalizedU, "On layer:%1% ladder:%2% sensor:%3% abnormal U=%4$")
Exception for normalized coordinate U out of bound [0,1].
Belle2::CompactSecIDs::m_compactSectorsIDMap
LayersLookUpTable_t m_compactSectorsIDMap
Lookup table containing all sectorIDs.
Definition: CompactSecIDs.h:362
Belle2::CompactSecIDs::m_sectorCounter
sectorID_t m_sectorCounter
Counter for sectors.
Definition: CompactSecIDs.h:359
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
Belle2::CompactSecIDs::nOfSensors
int nOfSensors(int layer, int ladder) const
get the number of sensors on
Definition: CompactSecIDs.h:334
Belle2::CompactSecIDs::nOfLadders
int nOfLadders(int layer) const
get the number of ladders on
Definition: CompactSecIDs.h:325
Belle2::CompactSecIDs::sectorID_t
uint16_t sectorID_t
Typedef of the compact Id for a single sector.
Definition: CompactSecIDs.h:45
Belle2::CompactSecIDs::extractCompactID
static void extractCompactID(secPairID_t pair_id, sectorID_t &id1, sectorID_t &id2)
Sets the two compact Sector id.
Definition: CompactSecIDs.h:210
Belle2::VxdID::getLadderNumber
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:108
Belle2::CompactSecIDs::nOfLayers
int nOfLayers(void) const
get the number of layers in this CompactSecIDs
Definition: CompactSecIDs.h:320
Belle2::FullSecID::getVxdID
VxdID getVxdID() const
returns VxdID of sensor.
Definition: FullSecID.h:148
Belle2::CompactSecIDs::SensorsOnLadder_t
std::vector< SectorsOnSensor< sectorID_t > > SensorsOnLadder_t
Typedef for vector of IDs of sectors on a sensors.
Definition: CompactSecIDs.h:309
Belle2::CompactSecIDs::getCompactID
sectorID_t getCompactID(const FullSecID &fullID) const
Returns the compact id of the FullSecID.
Definition: CompactSecIDs.h:124
Belle2::CompactSecIDs::~CompactSecIDs
~CompactSecIDs()
The destructor is quite trivial: nothing special to delete.
Definition: CompactSecIDs.h:60
Belle2::CompactSecIDs::setSubLayerID
bool setSubLayerID(FullSecID &sector, int sublayer)
set the SublayerID of the sector
Definition: CompactSecIDs.h:345
Belle2::CompactSecIDs::getAvailableSlots
unsigned int getAvailableSlots() const
Returns the available number of sector that can be defined now.
Definition: CompactSecIDs.h:66
Belle2::FullSecID
Class to identify a sector inside of the VXD.
Definition: FullSecID.h:43
Belle2::CompactSecIDs::getSectorsOnSensor
SectorsOnSensor< sectorID_t > getSectorsOnSensor(unsigned layer, unsigned ladder, unsigned sensor) const
Getter for IDs of all sectors on a sensor.
Definition: CompactSecIDs.h:141
Belle2::CompactSecIDs::areCoordinatesValid
bool areCoordinatesValid(VxdID aSensorID, double normalizedU, double normalizedV) const
JKL: returns true if operator() will not throw an exception.
Definition: CompactSecIDs.h:293
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CompactSecIDs::LaddersOnLayer_t
std::vector< SensorsOnLadder_t > LaddersOnLayer_t
Typedef for vector of vector of IDs of sectors on a Ladder.
Definition: CompactSecIDs.h:311
Belle2::CompactSecIDs::secTripletID_t
uint64_t secTripletID_t
Typedef of the compact Id for a 3 sectors combination.
Definition: CompactSecIDs.h:51
Belle2::FullSecID::getSecID
short int getSecID() const
returns SecID of current FullSecID (only unique for each sensor).
Definition: FullSecID.h:156
Belle2::CompactSecIDs::getSize
int getSize() const
Returns the number of sectors defined so far.
Definition: CompactSecIDs.h:63
Belle2::SectorsOnSensor
This class associates to an ordered pairs of normalized local coordinates a compact sector id.
Definition: SectorsOnSensor.h:36
Belle2::VxdID::getSensorNumber
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:110
Belle2::CompactSecIDs::addSectors
int addSectors(const std::vector< double > &normalizedUsup, const std::vector< double > &normalizedVsup, const std::vector< std::vector< FullSecID >> &fullSecIDs)
This method defines all the sectors on a given sensor.
Definition: CompactSecIDs.h:79
Belle2::CompactSecIDs::getCompactIDFastAndDangerous
sectorID_t getCompactIDFastAndDangerous(const FullSecID &fullID) const
Fast (and potentially dangerous) equivalent of getCompactID.
Definition: CompactSecIDs.h:278
Belle2::CompactSecIDs::privateGetCompactID
sectorID_t privateGetCompactID(const TContainer &container, short unsigned int index, Indexes ... indexes) const
The hidden private method that recursively descend the layer, ladder, sensor stack and returns the co...
Definition: CompactSecIDs.h:422
Belle2::CompactSecIDs::getCompactSectorsIDMap
const LayersLookUpTable_t & getCompactSectorsIDMap() const
Get access to the whole map.
Definition: CompactSecIDs.h:317
Belle2::CompactSecIDs::operator[]
sectorID_t operator[](const FullSecID &fullID) const
Returns the compact id of the FullSecID.
Definition: CompactSecIDs.h:186
Belle2::CompactSecIDs::secQuadrupletID_t
uint64_t secQuadrupletID_t
Typedef of the compact Id for a 4 sectors combination.
Definition: CompactSecIDs.h:54
Belle2::VxdID::getLayerNumber
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:106
Belle2::CompactSecIDs::CompactSecIDs
CompactSecIDs()
The constructor have just to set the internal counter to 0.
Definition: CompactSecIDs.h:57
Belle2::FullSecID::getLadderID
int getLadderID() const
returns LadderID compatible with basf2 standards
Definition: FullSecID.h:136
Belle2::FullSecID::getLayerID
short int getLayerID() const
returns LayerID compatible with basf2 standards.
Definition: FullSecID.h:128
Belle2::CompactSecIDs::getFullSecID
FullSecID getFullSecID(VxdID aSensorID, double normalizedU, double normalizedV) const
Returns a fullSecID for given sensor and pair of coordinates.
Definition: CompactSecIDs.h:161
Belle2::CompactSecIDs::getCompactID
secTripletID_t getCompactID(const FullSecID &id1, const FullSecID &id2, const FullSecID &id3) const
Returns the compact id of the triplet of FullSecID id1 id2 id3 If any of the id1, id2 or id3 is undef...
Definition: CompactSecIDs.h:220
Belle2::CompactSecIDs::LayersLookUpTable_t
std::vector< LaddersOnLayer_t > LayersLookUpTable_t
Typedef for vector of vector of vector of IDs of sectors on a layer.
Definition: CompactSecIDs.h:313
Belle2::CompactSecIDs::privateAddSectors
int privateAddSectors(TContainer &container, const std::vector< double > &normalizedUsup, const std::vector< double > &normalizedVsup, const std::vector< std::vector< FullSecID > > &fullSecIDs, short unsigned int index, Indexes ... indexes)
The hidden private method that recursively manage the size of everything.
Definition: CompactSecIDs.h:369
Belle2::CompactSecIDs::secPairID_t
uint32_t secPairID_t
Typedef of the compact Id for a 2 sectors combination.
Definition: CompactSecIDs.h:48