Belle II Software  release-08-01-10
KLMElectronicsMapImporter.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 /* Own header. */
10 #include <klm/calibration/KLMElectronicsMapImporter.h>
11 
12 /* KLM headers. */
13 #include <klm/dataobjects/KLMChannelIndex.h>
14 #include <klm/dbobjects/KLMElectronicsMap.h>
15 
16 /* Basf2 headers. */
17 #include <framework/database/DBImportObjPtr.h>
18 #include <framework/database/IntervalOfValidity.h>
19 #include <framework/logging/Logger.h>
20 #include <rawdata/dataobjects/RawCOPPERFormat.h>
21 
22 using namespace Belle2;
23 
25  m_ElementNumbers(&(KLMElementNumbers::Instance()))
26 {
27 }
28 
30 {
31 }
32 
33 void KLMElectronicsMapImporter::setIOV(int experimentLow, int runLow,
34  int experimentHigh, int runHigh)
35 {
36  m_ExperimentLow = experimentLow;
37  m_RunLow = runLow;
38  m_ExperimentHigh = experimentHigh;
39  m_RunHigh = runHigh;
40 }
41 
43 {
44  m_ChannelMap.clear();
45 }
46 
48 {
49  const int minimalVersion = 1;
50  const int maximalVersion = 2;
51  if (version < minimalVersion || version > maximalVersion) {
52  B2FATAL("Incorrect version (" << version << ") of BKLM electronics map. "
53  "It must be from " << minimalVersion << " to " << maximalVersion);
54  }
55  int copperId = 0;
56  int slotId = 0;
57  int laneId;
58  int axisId = 0;
60  for (KLMChannelIndex bklmPlane = bklmPlanes.beginBKLM();
61  bklmPlane != bklmPlanes.endBKLM(); ++bklmPlane) {
62  int section = bklmPlane.getSection();
63  int sector = bklmPlane.getSector();
64  int layer = bklmPlane.getLayer();
65  int plane = bklmPlane.getPlane();
66 
67  if (section == BKLMElementNumbers::c_ForwardSection) {
68  if (sector == 3 || sector == 4 || sector == 5 || sector == 6)
69  copperId = 1 + BKLM_ID;
70  if (sector == 1 || sector == 2 || sector == 7 || sector == 8)
71  copperId = 2 + BKLM_ID;
72  }
74  if (sector == 3 || sector == 4 || sector == 5 || sector == 6)
75  copperId = 3 + BKLM_ID;
76  if (sector == 1 || sector == 2 || sector == 7 || sector == 8)
77  copperId = 4 + BKLM_ID;
78  }
79 
80  if (sector == 3 || sector == 4 || sector == 5 || sector == 6)
81  slotId = sector - 2;
82  if (sector == 1 || sector == 2)
83  slotId = sector + 2;
84  if (sector == 7 || sector == 8)
85  slotId = sector - 6;
86 
88  laneId = layer + 5;
89  axisId = plane;
90  } else {
91  laneId = layer;
92  if (plane == BKLMElementNumbers::c_ZPlane)
93  axisId = 1;
94  if (plane == BKLMElementNumbers::c_PhiPlane)
95  axisId = 0;
96  }
97 
98  int MaxiChannel = BKLMElementNumbers::getNStrips(
99  section, sector, layer, plane);
100 
101  bool dontFlip = false;
102  if (section == BKLMElementNumbers::c_ForwardSection &&
103  (sector == 7 || sector == 8 || sector == 1 || sector == 2))
104  dontFlip = true;
105  if (section == BKLMElementNumbers::c_BackwardSection &&
106  (sector == 4 || sector == 5 || sector == 6 || sector == 7))
107  dontFlip = true;
108 
109  for (int iStrip = 1; iStrip <= MaxiChannel; iStrip++) {
110  int channelId = iStrip;
111 
112  if (!(dontFlip && layer >= BKLMElementNumbers::c_FirstRPCLayer && plane == BKLMElementNumbers::c_PhiPlane))
113  channelId = MaxiChannel - iStrip + 1;
114 
115  if (plane == BKLMElementNumbers::c_PhiPlane) {
116  if (layer == 1)
117  channelId += 4;
118  if (layer == 2)
119  channelId += 2;
120  }
121 
122  if (plane == BKLMElementNumbers::c_ZPlane) {
124  int channelCheck = channelId;
127  if (layer == 1) {
128  if (version == 1) {
129  if (channelCheck > 0 && channelCheck < 9)
130  channelId = 9 - channelId;
131  if (channelCheck > 8 && channelCheck < 24)
132  channelId = 54 - channelId;
133  if (channelCheck > 23 && channelCheck < 39)
134  channelId = 54 - channelId;
135  } else {
136  if (channelCheck > 0 && channelCheck < 9)
137  channelId = 9 - channelId; // 8 : 1
138  if (channelCheck > 8 && channelCheck < 24)
139  channelId = 39 - channelId; // 30 : 16
140  if (channelCheck > 23 && channelCheck < 39)
141  channelId = 69 - channelId; // 45 : 31
142  }
143  }
144  if (layer == 2) {
145  if (channelCheck > 0 && channelCheck < 10)
146  channelId = 10 - channelId;
147  if (channelCheck > 9 && channelCheck < 24)
148  channelId = 40 - channelId;
149  if (channelCheck > 23 && channelCheck < 39)
150  channelId = 69 - channelId;
151  }
152  } else { // All the sectors except the chimney one
153  if (channelCheck > 0 && channelCheck < 10)
154  channelId = 10 - channelId;
155  if (channelCheck > 9 && channelCheck < 25)
156  channelId = 40 - channelId;
157  if (channelCheck > 24 && channelCheck < 40)
158  channelId = 70 - channelId;
159  if (channelCheck > 39 && channelCheck < 55)
160  channelId = 100 - channelId;
161  }
162  }
163  }
164 
166  section, sector, layer, plane, iStrip);
167  m_ChannelMap.insert(
168  std::pair<KLMChannelNumber, KLMElectronicsChannel>(
169  detectorChannel,
170  KLMElectronicsChannel(copperId, slotId, laneId, axisId, channelId)));
171  }
172  }
173 }
174 
176 {
177  int segment, strip;
178  /* Segment is 0-based here. */
179  segment = (stripSoftware - 1) / EKLMElementNumbers::getNStripsSegment();
180  /* Order of segment readout boards in the firmware is opposite. */
181  segment = 4 - segment;
182  strip = segment * EKLMElementNumbers::getNStripsSegment() +
183  (stripSoftware - 1) % EKLMElementNumbers::getNStripsSegment() + 1;
184  return strip;
185 }
186 
188  int section, int sector, int layer, int copper, int slot, int lane)
189 {
190  for (int plane = 1; plane <= EKLMElementNumbers::getMaximalPlaneNumber(); ++plane) {
191  for (int strip = 1; strip <= EKLMElementNumbers::getMaximalStripNumber(); ++strip) {
192  int axis = plane - 1;
193  int channel = getEKLMStripFirmwareBySoftware(strip);
195  section, sector, layer, plane, strip);
196  m_ChannelMap.insert(
197  std::pair<KLMChannelNumber, KLMElectronicsChannel>(
198  detectorChannel,
199  KLMElectronicsChannel(EKLM_ID + copper, slot, lane, axis, channel)));
200  }
201  }
202 }
203 
205  int section, int sector, int layer, int plane, int segment,
206  int firmwareSegment)
207 {
208  std::map<KLMChannelNumber, KLMElectronicsChannel>:: iterator it;
209  for (int strip = 1; strip <= EKLMElementNumbers::getNStripsSegment(); ++strip) {
210  int stripPlane = strip + EKLMElementNumbers::getNStripsSegment() * (segment - 1);
212  section, sector, layer, plane, stripPlane);
213  it = m_ChannelMap.find(detectorChannel);
214  if (it == m_ChannelMap.end())
215  B2FATAL("The KLM electronics map is not loaded or incomplete.");
216  int channel = (firmwareSegment - 1) * EKLMElementNumbers::getNStripsSegment() +
217  strip;
218  it->second.setChannel(channel);
219  }
220 }
221 
223 {
224  const int minimalVersion = 1;
225  const int maximalVersion = 2;
226  if (version < minimalVersion || version > maximalVersion) {
227  B2FATAL("Incorrect version (" << version << ") of EKLM electronics map. "
228  "It must be from " << minimalVersion << " to " << maximalVersion);
229  }
230  /* Backward section. */
231  /* Sector 1. */
241  /* Wrong connection was fixed between phase 2 and phase 3. */
242  if (mc || (version >= 2)) {
245  } else {
248  }
250  /* Sector 2. */
251  if (version == 1) {
264  } else {
277  }
278  /* Sector 3. */
279  if (version == 1) {
292  } else {
305  }
306  /* Sector 4. */
319  /* Forward section. */
320  /* Sector 1. */
335  /* Sector 2. */
350  /* Sector 3. */
365  /* Sector 4. */
380  /* Switch of internal cables. */
381  if (!mc) {
382  setChannelsEKLMSegment(1, 1, 5, 1, 1, 4);
383  setChannelsEKLMSegment(1, 1, 5, 1, 2, 5);
384  setChannelsEKLMSegment(1, 1, 5, 2, 1, 4);
385  setChannelsEKLMSegment(1, 1, 5, 2, 2, 5);
386  setChannelsEKLMSegment(2, 1, 10, 2, 3, 2);
387  setChannelsEKLMSegment(2, 1, 10, 2, 4, 3);
388  }
389 }
390 
392  int subdetector, int section, int sector, int layer, int lane)
393 {
394  std::map<KLMChannelNumber, KLMElectronicsChannel>::iterator it;
395  int minimalPlane = m_ElementNumbers->getMinimalPlaneNumber(subdetector);
396  KLMChannelIndex klmChannel(subdetector, section, sector, layer, minimalPlane, 1);
397  KLMChannelIndex klmModule(klmChannel);
399  KLMChannelIndex klmNextModule(klmModule);
400  ++klmNextModule;
401  for (; klmChannel != klmNextModule; ++klmChannel) {
402  KLMChannelNumber channel = klmChannel.getKLMChannelNumber();
403  it = m_ChannelMap.find(channel);
404  if (it == m_ChannelMap.end())
405  B2FATAL("The KLM electronics map is not loaded or incomplete.");
406  it->second.setLane(lane);
407  }
408 }
409 
411  int subdetector, int section, int sector, int layer, int plane, int lane)
412 {
413  std::map<KLMChannelNumber, KLMElectronicsChannel>::iterator it;
414  KLMChannelIndex klmChannel(subdetector, section, sector, layer, plane, 1);
415  KLMChannelIndex klmPlane(klmChannel);
417  KLMChannelIndex klmNextPlane(klmPlane);
418  ++klmNextPlane;
419  for (; klmChannel != klmNextPlane; ++klmChannel) {
420  KLMChannelNumber channel = klmChannel.getKLMChannelNumber();
421  it = m_ChannelMap.find(channel);
422  if (it == m_ChannelMap.end())
423  B2FATAL("The KLM electronics map is not loaded or incomplete.");
424  it->second.setLane(lane);
425  }
426 }
427 
429 {
430  DBImportObjPtr<KLMElectronicsMap> electronicsMap;
431  electronicsMap.construct();
432  std::map<KLMChannelNumber, KLMElectronicsChannel>::iterator it;
433  for (it = m_ChannelMap.begin(); it != m_ChannelMap.end(); ++it) {
434  electronicsMap->addChannel(
435  it->first,
436  it->second.getCopper(),
437  it->second.getSlot(),
438  it->second.getLane(),
439  it->second.getAxis(),
440  it->second.getChannel());
441  }
444  electronicsMap.import(iov);
445 }
@ c_ChimneySector
Chimney sector: BB3 in 1-based notation; BB2 in 0-based notation.
@ c_FirstRPCLayer
First RPC layer.
static int getNStrips(int section, int sector, int layer, int plane)
Get number of strips.
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:36
Class for importing a single object to the database.
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
static constexpr int getMaximalStripNumber()
Get maximal strip number.
static constexpr int getNStripsSegment()
Get number of strips in a segment.
static constexpr int getMaximalPlaneNumber()
Get maximal plane number.
A class that describes the interval of experiments/runs for which an object in the database is valid.
KLM channel index.
KLMChannelIndex beginBKLM()
First channel for BKLM.
KLMChannelNumber getKLMChannelNumber() const
Get KLM channel number.
void setIndexLevel(enum IndexLevel indexLevel)
Set index level.
KLMChannelIndex & endBKLM()
Last channel for BKLM.
BKLM electronics channel.
const KLMElementNumbers * m_ElementNumbers
Element numbers.
void setChannelsEKLMSegment(int section, int sector, int layer, int plane, int segment, int firmwareSegment)
Set channels for EKLM segment.
void setIOV(int experimentLow, int runLow, int experimentHigh, int runHigh)
Set interval of validity.
void clearElectronicsMap()
Clear electronics map (to be able to import its multiple versions).
void addEKLMLane(int section, int sector, int layer, int copper, int slot, int lane)
Add EKLM electronics map lane.
void setLane(int subdetector, int section, int sector, int layer, int lane)
Set non-default lane for all channels in a module.
void loadEKLMElectronicsMap(int version, bool mc)
Load EKLM electronics map.
void loadBKLMElectronicsMap(int version)
Load BKLM electronics map.
std::map< KLMChannelNumber, KLMElectronicsChannel > m_ChannelMap
Data for creation of the electronics map.
void importElectronicsMap()
Import electronics map.
int getEKLMStripFirmwareBySoftware(int stripSoftware) const
Get EKLM firmware strip number by software strip number.
KLM element numbers.
KLMChannelNumber channelNumberBKLM(int section, int sector, int layer, int plane, int strip) const
Get channel number for BKLM.
int getMinimalPlaneNumber(int subdetector) const
Get minimal plane number.
KLMChannelNumber channelNumberEKLM(int section, int sector, int layer, int plane, int strip) const
Get channel number for EKLM.
uint16_t KLMChannelNumber
Channel number.
Abstract base class for different kinds of events.