Belle II Software  release-05-01-25
SVDOnlineToOfflineMap.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Giulia Casarosa, Eugenio Paoloni, Jarek Wiechczynski *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include "svd/online/SVDOnlineToOfflineMap.h"
12 #include <boost/property_tree/xml_parser.hpp>
13 #include <framework/logging/Logger.h>
14 #include <framework/utilities/FileSystem.h>
15 #include <vxd/geometry/GeoCache.h>
16 
17 using namespace Belle2;
18 using namespace std;
19 using boost::property_tree::ptree;
20 
21 
22 SVDOnlineToOfflineMap::SVDOnlineToOfflineMap(const string& xmlFilename): m_MapUniqueName("")
23 {
24 
25  // Create an empty property tree object
26 
27  ptree propertyTree;
28 
29  // Load the XML file into the property tree. If reading fails
30  // (cannot open file, parse error), an exception is thrown.
31  string xmlFullPath = FileSystem::findFile(xmlFilename);
32 
33  if (! FileSystem::fileExists(xmlFullPath)) {
34  B2ERROR("The xml filename: " << xmlFilename << endl <<
35  "resolved to: " << xmlFullPath << endl <<
36  "by FileSystem::findFile does not exist." << endl <<
37  "SVD online to offline map cannot be initialized." << endl <<
38  "Be aware: no SVDShaperDigit will be produced by this module." << endl
39  );
40  return;
41  }
42 
43  try {
44  read_xml(xmlFullPath, propertyTree);
45  } catch (std::exception const& ex) {
46  B2ERROR("STD excpetion rised during xml parsing " << ex.what() << endl <<
47  "SVD online to offline map cannot be initialized." << endl <<
48  "Be aware: no SVDShaperDigits will be produced by this module." << endl);
49  return;
50  } catch (...) {
51  B2ERROR("Unknown excpetion rised during xml parsing "
52  "SVD online to offline map cannot be initialized." << endl <<
53  "Be aware: no SVDShaperDigits will be produced by this module." << endl);
54  return;
55  }
56 
57  try {
58  // traverse pt: let us navigate through the daughters of <SVD>
59  for (ptree::value_type const& v : propertyTree.get_child("SVD")) {
60  if (v.first == "unique") {
61  m_MapUniqueName = v.second.get<string>("<xmlattr>.name");
62  B2INFO("Loading the offline -> online SVD map named " << m_MapUniqueName);
63  }
64  // if the daughter is a <layer> then read it!
65  if (v.first == "layer")
66  ReadLayer(v.second.get<int>("<xmlattr>.n"), v.second);
67  }
68  } catch (...) {
69  B2ERROR("Unknown excpetion rised during map initialization! "
70  "SVD online to offline map corrupted." << endl <<
71  "Be aware: the SVDShaperDigits will be unreliable." << endl);
72  // To Do: rise an exception so that the calling module will skip the
73  // SVDShaperDigits filling
74  return;
75  }
76 }
77 
78 const SVDOnlineToOfflineMap::SensorInfo& SVDOnlineToOfflineMap::getSensorInfo(unsigned char FADC, unsigned char APV25)
79 {
80 
81  ChipID id(FADC, APV25);
82  auto sensorIter = m_sensors.find(id);
83 
84  if (sensorIter == m_sensors.end()) {
86 
87  if (!(nBadMappingErrors % m_errorRate)) B2ERROR("Combination not found in the SVD On-line to Off-line map:" << LogVar("FADC",
88  int(FADC)) << LogVar("APV", int(APV25)));
89 
93  return m_currentSensorInfo;
94  }
95  m_currentSensorInfo = sensorIter->second;
96  return m_currentSensorInfo;
97 }
98 
99 
100 
101 const SVDOnlineToOfflineMap::ChipInfo& SVDOnlineToOfflineMap::getChipInfo(unsigned short layer, unsigned short ladder,
102  unsigned short dssd, bool side, unsigned short strip)
103 {
104  SensorID id(layer, ladder, dssd, side);
105  auto chipIter = m_chips.find(id);
106 
107  if (chipIter == m_chips.end()) B2WARNING(" The following combination: sensorID: " << layer << "." << ladder << "." << dssd <<
108  ", isU=" << side << ", strip=" << strip <<
109  " - is not found in the SVD Off-line to On-line map! The payload retrieved from database may be wrong! ");
110 
111 
112  vector<ChipInfo> vecChipInfo = chipIter->second;
113 
114  ChipInfo info = {0, 0, 0, 0, 0};
115  ChipInfo* pinfo = &info;
116 
117  for (std::vector<ChipInfo>::iterator it = vecChipInfo.begin() ; it != vecChipInfo.end(); ++it) {
118  ChipInfo& chipInfo = *it;
119  unsigned short channelFirst = min(chipInfo.stripFirst, chipInfo.stripLast);
120  unsigned short channelLast = max(chipInfo.stripFirst, chipInfo.stripLast);
121 
122  if (strip >= channelFirst and strip <= channelLast) {
123  pinfo = &chipInfo;
124  pinfo->apvChannel = abs(strip - (pinfo->stripFirst));
125  }
126  }
127  if (pinfo->fadc == 0) B2WARNING("The strip number " << strip << " is not found in the SVDOnlineToOfflineMap for sensor " << layer <<
128  "." << ladder << "." << dssd << " on side " << (side ? "u" : "v") << "! Related APV chip is excluded in the hardware mapping.");
129 
130  m_currentChipInfo = *pinfo;
131  return m_currentChipInfo;
132 }
133 
134 bool SVDOnlineToOfflineMap::isAPVinMap(unsigned short layer, unsigned short ladder,
135  unsigned short dssd, bool side, unsigned short strip)
136 {
137  SensorID id(layer, ladder, dssd, side);
138  auto chipIter = m_chips.find(id);
139 
140  if (chipIter == m_chips.end()) return false;
141 
142  vector<ChipInfo> vecChipInfo = chipIter->second;
143 
144  ChipInfo info = {0, 0, 0, 0, 0};
145  ChipInfo* pinfo = &info;
146 
147  for (std::vector<ChipInfo>::iterator it = vecChipInfo.begin() ; it != vecChipInfo.end(); ++it) {
148  ChipInfo& chipInfo = *it;
149  unsigned short channelFirst = min(chipInfo.stripFirst, chipInfo.stripLast);
150  unsigned short channelLast = max(chipInfo.stripFirst, chipInfo.stripLast);
151 
152  if (strip >= channelFirst and strip <= channelLast) {
153  pinfo = &chipInfo;
154  pinfo->apvChannel = abs(strip - (pinfo->stripFirst));
155  }
156  }
157  if (pinfo->fadc == 0) return false;
158 
159  return true;
160 }
161 
162 bool SVDOnlineToOfflineMap::isAPVinMap(VxdID sensorID, bool side, unsigned short strip)
163 {
164  return isAPVinMap(sensorID.getLayerNumber(), sensorID.getLadderNumber(), sensorID.getSensorNumber(), side, strip);
165 }
166 
167 
169  unsigned char APV25, unsigned char channel, short samples[6], float time, SVDModeByte mode)
170 {
171  // Issue a warning, we'll be sending out a null pointer.
172  if (channel > 127) {
173  B2WARNING(" channel out of range (0-127):" << LogVar("channel", int(channel)));
174  return NULL;
175  }
176  const SensorInfo& info = getSensorInfo(FADC, APV25);
177  short strip = getStripNumber(channel, info);
178 
180  copy(samples, samples + SVDShaperDigit::c_nAPVSamples, rawSamples.begin());
181 
182  // create SVDShaperDigit only for existing sensor
183  if (info.m_sensorID) {
184  return new SVDShaperDigit(info.m_sensorID, info.m_uSide, strip, rawSamples, time, mode);
185  } else {
186  return NULL;
187  }
188 }
189 
190 
191 void
192 SVDOnlineToOfflineMap::ReadLayer(int nlayer, ptree const& xml_layer)
193 {
194  // traverse xml_layer: let us navigate through the daughters of <layer>
195  for (ptree::value_type const& v : xml_layer) {
196  // if the daughter is a <ladder> then read it!
197  if (v.first == "ladder") {
198  ReadLadder(nlayer, v.second.get<int>("<xmlattr>.n") , v.second);
199  }
200  }
201 }
202 
203 void
204 SVDOnlineToOfflineMap::ReadLadder(int nlayer, int nladder, ptree const& xml_ladder)
205 {
206  // traverse xml_ladder: let us navigate through the daughters of <ladder>
207  for (ptree::value_type const& v : xml_ladder) {
208  // if the daughter is a <sensor> then read it!
209  if (v.first == "sensor") {
210  ReadSensor(nlayer, nladder, v.second.get<int>("<xmlattr>.n") , v.second);
211  }
212  }
213 }
214 
215 void
216 SVDOnlineToOfflineMap::ReadSensor(int nlayer, int nladder, int nsensor, ptree const& xml_sensor)
217 {
218  // traverse xml_sensor: let us navigate through the daughters of <sensor>
219  for (ptree::value_type const& v : xml_sensor) {
220  // if the daughter is one side <> then read it!
221  if (v.first == "side") {
222  std::string tagSide = v.second.get<std::string>("<xmlattr>.side");
223 
224  bool isOnSideU = (tagSide == "U" || tagSide == "u");
225  bool isOnSideV = (tagSide == "V" || tagSide == "v");
226 
227  if ((! isOnSideU) && (! isOnSideV)) {
228  B2ERROR("Side '" << tagSide << "' on layer " << nlayer
229  << " ladder " << nladder << " sensor " << nsensor
230  << " is neither 'U' nor 'V'");
231  }
232 
233  ReadSensorSide(nlayer, nladder, nsensor, isOnSideU, v.second);
234  }
235  }
236 
237 }
238 
239 void
240 SVDOnlineToOfflineMap::ReadSensorSide(int nlayer, int nladder, int nsensor, bool isU,
241  ptree const& xml_side)
242 {
243 
244  // traverse xml_sensor: let us navigate through the daughters of <side>
245 
246 
247  vector<ChipInfo> vecInfo; // for packer
248  SensorID sid(nlayer, nladder, nsensor, isU);
249 
250  for (ptree::value_type const& v : xml_side) {
251  // if the daughter is a <chip>
252 
253  if (v.first == "chip") {
254  auto tags = v.second;
255  unsigned char chipN = tags.get<unsigned char>("<xmlattr>.n");
256  unsigned char FADCn = tags.get<unsigned char>("<xmlattr>.FADCn");
257 
258  //storing info on FADC numbers and related APV chips
259  FADCnumbers.insert(FADCn);
260  APVforFADCmap.insert(std::pair<unsigned char, unsigned char>(FADCn, chipN));
261 
262  ChipID cid(FADCn, chipN);
263 
264  auto sensorIter = m_sensors.find(cid);
265 
266  if (sensorIter != m_sensors.end()) {
267  B2WARNING("Repeated insertion for FADC " << FADCn << " and APV "
268  << chipN << ", layer/ladder/sensor " << nlayer << "/" << nladder
269  << "/" << nsensor << ", side " << (isU ? "u" : "v"));
270  }
271  unsigned short stripNumberCh0 = tags.get<unsigned short>("<xmlattr>.strip_number_of_ch0");
272  unsigned short stripNumberCh127 = tags.get<unsigned short>("<xmlattr>.strip_number_of_ch127");
273 
274 
275  SensorInfo sinfo;
276  sinfo.m_sensorID = VxdID(nlayer, nladder, nsensor);
277  sinfo.m_uSide = isU;
278  sinfo.m_parallel = (stripNumberCh127 > stripNumberCh0);
279  sinfo.m_channel0 = stripNumberCh0;
280  sinfo.m_channel127 = stripNumberCh127;
281 
282  m_sensors[cid] = sinfo;
283 
284  // for packer
285  ChipInfo cinfo;
286  cinfo.fadc = FADCn;
287  cinfo.apv = chipN;
288  cinfo.stripFirst = stripNumberCh0;
289  cinfo.stripLast = stripNumberCh127;
290 
291  vecInfo.push_back(cinfo);
292 
293  } //chip
294 
295  } // for daughters
296 
297  m_chips[sid] = vecInfo; // for packer
298 }
299 
301 {
302  unsigned short it = 0;
303 
304  for (auto ifadc = FADCnumbers.begin(); ifadc != FADCnumbers.end(); ++ifadc) {
305  map2[it] = *ifadc;
306  map1[*ifadc] = it++;
307  }
308 }
309 
311 {
312 
313 
315 
316  for (auto layer : geoCache.getLayers(VXD::SensorInfoBase::SVD))
317  for (auto ladder : geoCache.getLadders(layer))
318  for (Belle2::VxdID sensor : geoCache.getSensors(ladder))
319  for (int view = 0; view < 2; view++) {
320 
321  int nAPVs = 6;
322  if (layer.getLayerNumber() != 3 && view == 0)
323  nAPVs = 4;
324 
325  //loop on all APVs of the side
326  for (int apv = 0; apv < nAPVs; apv++) {
327  B2DEBUG(29, "checking " << sensor.getLayerNumber() << "." << sensor.getLadderNumber() << "." << sensor.getSensorNumber() <<
328  ", view = " << view << ", apv = " << apv);
329  if (! isAPVinMap(sensor, view, apv * 128 + 63.5)) {
330  missingAPV tmp_missingAPV;
331  tmp_missingAPV.m_sensorID = sensor;
332  tmp_missingAPV.m_isUSide = view;
333  tmp_missingAPV.m_halfStrip = apv * 128 + 63.5;
334 
335  m_missingAPVs.push_back(tmp_missingAPV);
336  B2DEBUG(29, "FOUND MISSING APV: " << sensor << ", " << view << ", " << apv);
337  }
338 
339  }
340 
341  }
342 
343 }
Belle2::SVDOnlineToOfflineMap::m_sensors
std::unordered_map< ChipID::baseType, SensorInfo > m_sensors
m_sensors[ChipID(FADC,APV25)] gives the SensorInfo for the given APV25 on the given FADC (Unpacker)
Definition: SVDOnlineToOfflineMap.h:297
Belle2::SVDOnlineToOfflineMap::APVforFADCmap
std::unordered_multimap< unsigned char, unsigned char > APVforFADCmap
map containing FADC numbers assigned to multiple APVs, from xml file
Definition: SVDOnlineToOfflineMap.h:258
Belle2::SVDOnlineToOfflineMap::FADCnumbers
std::unordered_set< unsigned char > FADCnumbers
container for FADC numbers from current mapping file
Definition: SVDOnlineToOfflineMap.h:255
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
Belle2::SVDOnlineToOfflineMap::ReadLayer
void ReadLayer(int nLayer, boost::property_tree::ptree const &xml_layer)
Read from the ptree v in the xml file the layer nLayer.
Definition: SVDOnlineToOfflineMap.cc:192
Belle2::SVDOnlineToOfflineMap::ReadSensor
void ReadSensor(int nLayer, int nLadder, int nSensor, boost::property_tree::ptree const &xml_sensor)
Read from the ptree xml_sensor the sensor nSensor in ladder nLadder in layer nLayer.
Definition: SVDOnlineToOfflineMap.cc:216
Belle2::SVDOnlineToOfflineMap::SensorInfo::m_channel127
unsigned short m_channel127
Strip corresponding to channel 127.
Definition: SVDOnlineToOfflineMap.h:151
Belle2::VxdID::getLadderNumber
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:108
Belle2::SVDOnlineToOfflineMap::ChipInfo
Struct to hold data about an APV25 chip.
Definition: SVDOnlineToOfflineMap.h:155
Belle2::SVDOnlineToOfflineMap::prepareListOfMissingAPVs
void prepareListOfMissingAPVs()
prepares the list of the missing APVs using the channel mapping
Definition: SVDOnlineToOfflineMap.cc:310
Belle2::SVDOnlineToOfflineMap::SensorInfo::m_sensorID
VxdID m_sensorID
Sensor ID.
Definition: SVDOnlineToOfflineMap.h:147
Belle2::SVDModeByte
Class to store SVD mode information.
Definition: SVDModeByte.h:79
Belle2::SVDOnlineToOfflineMap::missingAPV::m_sensorID
VxdID m_sensorID
Sensor ID.
Definition: SVDOnlineToOfflineMap.h:219
Belle2::SVDOnlineToOfflineMap::missingAPV::m_isUSide
bool m_isUSide
True if u-side of the sensor.
Definition: SVDOnlineToOfflineMap.h:220
Belle2::SVDOnlineToOfflineMap::getStripNumber
short getStripNumber(unsigned char channel, const SensorInfo &info) const
Convert APV channel number to a strip number using a ChipInfo object.
Definition: SVDOnlineToOfflineMap.h:250
Belle2::SVDOnlineToOfflineMap::ReadLadder
void ReadLadder(int nLayer, int nLadder, boost::property_tree::ptree const &xml_ladder)
Read from the ptree xml_ladde the ladder nLadder in layer nLayer.
Definition: SVDOnlineToOfflineMap.cc:204
Belle2::SVDOnlineToOfflineMap::SensorInfo::m_channel0
unsigned short m_channel0
Strip corresponding to channel 0.
Definition: SVDOnlineToOfflineMap.h:150
Belle2::SVDOnlineToOfflineMap::missingAPV::m_halfStrip
float m_halfStrip
floating strip in the middle of the APV
Definition: SVDOnlineToOfflineMap.h:221
Belle2::SVDShaperDigit
The SVD ShaperDigit class.
Definition: SVDShaperDigit.h:46
Belle2::SVDOnlineToOfflineMap::ChipInfo::stripFirst
unsigned short stripFirst
first strip number
Definition: SVDOnlineToOfflineMap.h:158
Belle2::SVDOnlineToOfflineMap::missingAPV
struct to hold missing APVs informations
Definition: SVDOnlineToOfflineMap.h:218
Belle2::SVDOnlineToOfflineMap::m_errorRate
int m_errorRate
The suppression factor of BadMapping ERRORs messages to be shown.
Definition: SVDOnlineToOfflineMap.h:304
Belle2::SVDOnlineToOfflineMap::isAPVinMap
bool isAPVinMap(unsigned short layer, unsigned short ladder, unsigned short dssd, bool side, unsigned short strip)
is the APV of the strips in the map? for a given layer/ladder/dssd/side/strip combination.
Definition: SVDOnlineToOfflineMap.cc:134
Belle2::SVDOnlineToOfflineMap::SensorInfo
Struct to hold data about a sensor.
Definition: SVDOnlineToOfflineMap.h:146
Belle2::VXD::GeoCache::getInstance
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:215
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::SVDOnlineToOfflineMap::ChipInfo::stripLast
unsigned short stripLast
last strip number
Definition: SVDOnlineToOfflineMap.h:159
Belle2::SVDOnlineToOfflineMap::m_currentSensorInfo
SensorInfo m_currentSensorInfo
current sensor info
Definition: SVDOnlineToOfflineMap.h:323
Belle2::SVDOnlineToOfflineMap::ChipInfo::fadc
unsigned short fadc
fadc number
Definition: SVDOnlineToOfflineMap.h:156
Belle2::SVDOnlineToOfflineMap::getChipInfo
const ChipInfo & getChipInfo(unsigned short layer, unsigned short ladder, unsigned short dssd, bool side, unsigned short strip)
Get ChipInfo for a given layer/ladder/dssd/side/strip combination.
Definition: SVDOnlineToOfflineMap.cc:101
Belle2::SVDOnlineToOfflineMap::ChipID
Class to hold FADC+APV25 numbers.
Definition: SVDOnlineToOfflineMap.h:54
Belle2::VXD::SensorInfoBase::SVD
@ SVD
SVD Sensor.
Definition: SensorInfoBase.h:45
Belle2::SVDOnlineToOfflineMap::ReadSensorSide
void ReadSensorSide(int nLayer, int nLadder, int nSensor, bool isU, boost::property_tree::ptree const &xml_side)
Read from the ptree xml_side the U-side, if isU, (the V-side otherwise) of the sensor nSensor in ladd...
Definition: SVDOnlineToOfflineMap.cc:240
Belle2::SVDOnlineToOfflineMap::FADCmap
std::unordered_map< unsigned short, unsigned short > FADCmap
FADC map typedef.
Definition: SVDOnlineToOfflineMap.h:261
Belle2::VxdID::getSensorNumber
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:110
Belle2::SVDOnlineToOfflineMap::NewShaperDigit
SVDShaperDigit * NewShaperDigit(unsigned char FADC, unsigned char APV25, unsigned char channel, short samples[6], float time=0.0, SVDModeByte mode=SVDModeByte())
Return a pointer to a new SVDShpaerDigit whose VxdID, isU and cellID is set.
Definition: SVDOnlineToOfflineMap.cc:168
Belle2::SVDOnlineToOfflineMap::SVDOnlineToOfflineMap
SVDOnlineToOfflineMap()=delete
No default constructor.
Belle2::FileSystem::fileExists
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:33
Belle2::SVDOnlineToOfflineMap::ChipInfo::apvChannel
unsigned char apvChannel
apv channel
Definition: SVDOnlineToOfflineMap.h:160
Belle2::VXD::GeoCache
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:41
Belle2::SVDOnlineToOfflineMap::m_missingAPVs
std::vector< missingAPV > m_missingAPVs
list of the missing APVs
Definition: SVDOnlineToOfflineMap.h:226
Belle2::SVDOnlineToOfflineMap::ChipInfo::apv
unsigned char apv
apv number
Definition: SVDOnlineToOfflineMap.h:157
Belle2::SVDShaperDigit::c_nAPVSamples
static const std::size_t c_nAPVSamples
Number of APV samples stored.
Definition: SVDShaperDigit.h:51
Belle2::SVDOnlineToOfflineMap::m_chips
std::unordered_map< SensorID::baseType, std::vector< ChipInfo > > m_chips
needed for the packer, map of VxdID to chips
Definition: SVDOnlineToOfflineMap.h:298
Belle2::SVDShaperDigit::APVRawSamples
std::array< APVRawSampleType, c_nAPVSamples > APVRawSamples
array of APVRawSamplesType objects
Definition: SVDShaperDigit.h:58
Belle2::VxdID::getLayerNumber
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:106
Belle2::SVDOnlineToOfflineMap::m_MapUniqueName
std::string m_MapUniqueName
Human readable unique name of this map.
Definition: SVDOnlineToOfflineMap.h:292
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::SVDOnlineToOfflineMap::getSensorInfo
const SensorInfo & getSensorInfo(unsigned char FADC, unsigned char APV25)
Get SensorInfo for a given FADC/APV combination.
Definition: SVDOnlineToOfflineMap.cc:78
Belle2::SVDOnlineToOfflineMap::nBadMappingErrors
unsigned int nBadMappingErrors
Counter of the BadMapping errors.
Definition: SVDOnlineToOfflineMap.h:301
Belle2::SVDOnlineToOfflineMap::m_currentChipInfo
ChipInfo m_currentChipInfo
internal instance of chipinfo used by the getter
Definition: SVDOnlineToOfflineMap.h:322
Belle2::SVDOnlineToOfflineMap::prepFADCmaps
void prepFADCmaps(FADCmap &, FADCmap &)
function that maps FADC numbers as 0-(nFADCboards-1) from FADCnumbers unordered_set
Definition: SVDOnlineToOfflineMap.cc:300
Belle2::SVDOnlineToOfflineMap::SensorID
Class to hold numbers related to sensor.
Definition: SVDOnlineToOfflineMap.h:109