ComPWA
Common Partial-Wave-Analysis Framework
Properties.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 #include "Properties.hpp"
6 
7 #include "Core/Logging.hpp"
8 #include "Core/Utils.hpp"
9 
10 #include "boost/property_tree/xml_parser.hpp"
11 
12 namespace ComPWA {
13 
14 ParticleProperties::ParticleProperties(boost::property_tree::ptree pt) {
15  Name = pt.get<std::string>("<xmlattr>.Name");
16  Id = pt.get<pid>("Pid");
17  for (const auto &v : pt.get_child("")) {
18  if (v.first == "QuantumNumber") {
19  // QuantumNumbers which can be of type int or ComPWA::Spin
20  std::string type = v.second.get<std::string>("<xmlattr>.Type");
21 
22  // We have to distinguish between spin and integer quantum numbers
23  if (v.second.get<std::string>("<xmlattr>.Class") == "Spin") {
24  auto value = v.second.get<double>("<xmlattr>.Value");
25  try { // Projection of spin is optional (e.g. (I,I3))
26  double valueZ = v.second.get<double>("<xmlattr>.Projection");
27  RealQuantumNumbers.insert(
28  std::make_pair(type + ".Projection", valueZ));
29  } catch (std::exception &ex) {
30  }
31  RealQuantumNumbers.insert(std::make_pair(type, value));
32  } else if (v.second.get<std::string>("<xmlattr>.Class") == "Int") {
33  auto value = v.second.get<int>("<xmlattr>.Value");
34  IntQuantumNumbers.insert(std::make_pair(type, value));
35  } else {
36  throw BadParameter(
37  "ParticleProperties::ParticleProperties() | "
38  "QuantumNumber is neither of type 'Spin' nor of type "
39  "'Int'!");
40  }
41  } else if (v.first == "Parameter") {
42  // Parameter (e.g. Mass)
43  if (v.second.get<std::string>("<xmlattr>.Type") != "Mass")
44  continue;
45  Mass.Name = v.second.get<std::string>("<xmlattr>.Name");
46  Mass.Value = v.second.get<double>("Value");
47 
48  auto error = v.second.get_optional<double>("Error");
49  if (error) {
50  Mass.Error = std::make_pair(error.get(), error.get());
51  } else {
52  auto errorlow = v.second.get_optional<double>("ErrorLow");
53  auto errorhigh = v.second.get_optional<double>("ErrorHigh");
54  if (errorlow && errorhigh) {
55  Mass.Error = std::make_pair(errorlow.get(), errorhigh.get());
56  } else if (errorlow) {
57  throw std::runtime_error("ParticleProperties | Error of Parameter "
58  "Mass not properly set (missing ErrorHigh)");
59  } else if (errorhigh) {
60  throw std::runtime_error("ParticleProperties | Error of Parameter "
61  "Mass not properly set (missing ErrorLow)");
62  }
63  }
64 
65  auto fix = v.second.get_optional<bool>("Fix");
66  if (fix)
67  Mass.IsFixed = fix.get();
68  else
69  Mass.IsFixed = true;
70 
71  auto min = v.second.get_optional<double>("Min");
72  auto max = v.second.get_optional<double>("Max");
73  if (min && max) {
74  Mass.HasBounds = true;
75  Mass.Bounds = std::make_pair(min.get(), max.get());
76  } else if (min || max) { // Bounds not completely specified
77  throw std::runtime_error(
78  "FitParameterFactory() | Parameter bounds not properly set!");
79  }
80  } else {
81  }
82  }
83 
84  // Info on the particle decay is stored as it is as property_tree and later
85  // used by AbstractDynamicalFunctions (e.g. RelativisticBreitWigner).
86  auto decayInfo = pt.get_child_optional("DecayInfo");
87  if (decayInfo) {
88  DecayInfo = decayInfo.get();
89  } else {
90  DecayInfo.put("<xmlattr>.Type", "Stable");
91  }
92 }
93 
96  const boost::property_tree::ptree &pt) {
97  auto particleTree = pt.get_child_optional("ParticleList");
98  if (!particleTree)
99  return;
100  for (auto const &v : particleTree.get()) {
101  if (v.first != "Particle")
102  continue;
103  ParticleProperties tmp(v.second);
104  auto result = list.insert(tmp);
105 
106  if (!result.second) {
107  LOG(INFO) << " Particle " << tmp.getName() << " with identical ID "
108  << tmp.getId() << " already exists in list with the name "
109  << result.first->getName() << " and ID "
110  << result.first->getId()
111  << ". Particle properties will be overwritten!";
112  list.erase(result.first);
113  result = list.insert(tmp);
114  }
115  tmp = *result.first;
116 
117  std::stringstream ss;
118  ss << " Particle " << tmp.getName() << " (id=" << tmp.getId()
119  << ") J(PC)=" << tmp.getQuantumNumber<double>("Spin") << "(";
120  // parities are optional
121  try {
122  int parity = tmp.getQuantumNumber<int>("Parity");
123  ss << parity;
124  } catch (std::exception &ex) {
125  }
126  try {
127  int cparity = tmp.getQuantumNumber<int>("Cparity");
128  ss << cparity;
129  } catch (std::exception &ex) {
130  }
131  ss << ") mass=" << tmp.getMass().Value
132  << " decayType=" << tmp.getDecayType();
133  LOG(DEBUG) << ss.str();
134  }
135 
136  return;
137 }
138 
139 void insertParticles(ParticleList &list, std::stringstream &Stream) {
140  boost::property_tree::ptree tree;
141  boost::property_tree::xml_parser::read_xml(Stream, tree);
142  insertParticles(list, tree);
143 }
144 
145 void insertParticles(ParticleList &list, std::string FileName) {
146  boost::property_tree::ptree tree;
147  boost::property_tree::xml_parser::read_xml(FileName, tree);
148  insertParticles(list, tree);
149 }
150 
151 ParticleList readParticles(std::stringstream &Stream) {
152  boost::property_tree::ptree tree;
153  boost::property_tree::xml_parser::read_xml(Stream, tree);
154  return readParticles(tree);
155 }
156 
157 ParticleList readParticles(std::string FileName) {
158  boost::property_tree::ptree tree;
159  boost::property_tree::xml_parser::read_xml(FileName, tree);
160  return readParticles(tree);
161 }
162 
163 ParticleList readParticles(boost::property_tree::ptree &tree) {
164  ParticleList list;
165  insertParticles(list, tree);
166  return list;
167 }
168 
169 } // namespace ComPWA
std::string getDecayType() const
Definition: Properties.hpp:41
Parameter not existing.
Definition: Exceptions.hpp:62
ParticleProperties(boost::property_tree::ptree pt)
Definition: Properties.cpp:14
std::pair< T, T > Error
T getQuantumNumber(std::string type) const
ComPWA::FitParameter< double > Mass
Definition: Properties.hpp:53
ComPWA::FitParameter< double > getMass() const
Definition: Properties.hpp:35
ParticleList readParticles(std::stringstream &Stream)
Read list of particles from a stringstream For some reason the boost xml parser needs a non-const ref...
Definition: Properties.cpp:151
std::set< ParticleProperties > ParticleList
Definition: Properties.hpp:84
void insertParticles(ParticleList &list, const boost::property_tree::ptree &pt)
Read list of particles from a boost::property_tree.
Definition: Properties.cpp:95
std::string getName() const
Definition: Properties.hpp:31
int pid
Particle ID.
Definition: Properties.hpp:22
std::map< std::string, double > RealQuantumNumbers
Definition: Properties.hpp:55
boost::property_tree::ptree DecayInfo
Store decay info in property_tree.
Definition: Properties.hpp:59
std::pair< T, T > Bounds
std::map< std::string, int > IntQuantumNumbers
Definition: Properties.hpp:54