Belle II Software  release-05-01-25
VxdID.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <vxd/dataobjects/VxdID.h>
12 #include <sstream>
13 
14 using namespace std;
15 
16 namespace Belle2 {
22  namespace {
28  int getPart(istream& in)
29  {
30  if (!in.eof()) {
31  //Get next char, if it is a dot, ignore it and get the next one
32  int next = in.get();
33  if (next == '.') next = in.get();
34  //If it is a wildcard we return 0 as id, otherwise we put it back in the stream
35  if (next == '*' or in.eof()) {
36  return 0;
37  } else {
38  in.unget();
39  }
40  //If it is the segment separator, we assume the remaining parts to be missing, so return 0
41  if (next == '#') return 0;
42 
43  //Now get the actual value out of the stream. If this fails something is wrong and it is not
44  //a valid id
45  int value(0);
46  in >> value;
47  if (in.fail() && !in.eof()) {
48  throw runtime_error("Failed to parse Number");
49  }
50  return value;
51  }
52  return 0;
53  }
54  }
55 
56  VxdID::VxdID(const std::string& sensor)
57  {
58  //We parse the Id from string, so set it to 0 first
59  m_id.id = 0;
60  //create a stream from the string
61  istringstream in(sensor);
62  try {
63  //Get all the parts
64  m_id.parts.layer = getPart(in);
65  m_id.parts.ladder = getPart(in);
66  m_id.parts.sensor = getPart(in);
67  //Check if we also have a segment specified, if so get it
68  if (in.peek() == '#') {
69  in.get();
70  m_id.parts.segment = getPart(in);
71  }
72  } catch (runtime_error&) {
73  //Something went wrong parsing the parts
74  m_id.id = 0;
75  throw invalid_argument("Could not parse VxdID: '" + sensor + "'");
76  }
77  //There is stuff left we also throw an exception as we cannot warn the user
78  //without the logging system
79  if (!in.eof()) {
80  string rest;
81  //Get the remainder: get everything in the stream until the next NULL
82  //character which should only occur at the end of the string.
83  getline(in, rest, '\0');
84  throw invalid_argument("Trailing characters after VxdID " + (string)*this + ": '" + rest + "'");
85  }
86  }
87 
88  VxdID::operator string() const
89  {
90  stringstream out;
91  if (m_id.parts.layer) {
92  out << m_id.parts.layer;
93  } else {
94  out << "*";
95  }
96  if (m_id.parts.ladder || m_id.parts.sensor) {
97  out << ".";
98  if (m_id.parts.ladder) {
99  out << m_id.parts.ladder;
100  } else {
101  out << "*";
102  }
103  }
104  if (m_id.parts.sensor) {
105  out << "." << m_id.parts.sensor;
106  }
107  if (m_id.parts.segment) {
108  out << "#" << m_id.parts.segment;
109  }
110  return out.str();
111  }
112 
113  std::ostream& operator<<(std::ostream& out, const VxdID& id)
114  {
115  out << ((string)id);
116  return out;
117  }
118 
120 }
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
Belle2::operator<<
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
Definition: IntervalOfValidity.cc:196
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19