Belle II Software  release-06-01-15
UnitConst.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 #include <framework/gearbox/Unit.h>
10 #include <framework/gearbox/Const.h>
11 #include <framework/logging/Logger.h>
12 #include <framework/particledb/EvtGenDatabasePDG.h>
13 
14 #include <TMath.h>
15 
16 #include <algorithm>
17 #include <fstream>
18 
19 using namespace Belle2;
20 using namespace std;
21 
22 /*** The implementation of the Unit class defined in Unit.h starts here ***/
23 
24 namespace Belle2 {
30  map<string, double> Unit::s_conversionFactors;
31 
33 #define DEFINE_UNIT_NAME(var,value,name) const double Unit::var = Unit::registerConversion(name,value)
35 #define DEFINE_UNIT(var,value) DEFINE_UNIT_NAME(var,value,#var)
36 
40  DEFINE_UNIT(cm , 1.);
41  DEFINE_UNIT(ns , 1.);
42  DEFINE_UNIT(GHz , 1.);
43  DEFINE_UNIT(rad , 1.);
44  DEFINE_UNIT(GeV , 1.);
45  DEFINE_UNIT(K , 1.);
46  DEFINE_UNIT(e , 1.);
53  DEFINE_UNIT_NAME(g_cm3 , 1., "g/cm3");
59 // length units
60  DEFINE_UNIT(km , Unit::cm * 1e5);
61  DEFINE_UNIT(m , Unit::cm * 1e2);
62  DEFINE_UNIT(mm , Unit::m * 1e-3);
63  DEFINE_UNIT(um , Unit::m * 1e-6);
64  DEFINE_UNIT(nm , Unit::m * 1e-9);
65  DEFINE_UNIT(pm , Unit::m * 1e-12);
66  DEFINE_UNIT(fm , Unit::m * 1e-15);
68 // area units
73  DEFINE_UNIT(b , Unit::m2 * 1e-28);
74  DEFINE_UNIT(mb , Unit::b * 1e-3);
75  DEFINE_UNIT(ub , Unit::b * 1e-6);
76  DEFINE_UNIT(nb , Unit::b * 1e-9);
77  DEFINE_UNIT(pb , Unit::b * 1e-12);
78  DEFINE_UNIT(fb , Unit::b * 1e-15);
79  DEFINE_UNIT(ab , Unit::b * 1e-18);
81 // volume units
86 // time units
87  DEFINE_UNIT(s , Unit::ns * 1e9);
88  DEFINE_UNIT(ms , Unit::s * 1e-3);
89  DEFINE_UNIT(us , Unit::s * 1e-6);
90  DEFINE_UNIT(ps , Unit::s * 1e-12);
91  DEFINE_UNIT(fs , Unit::s * 1e-15);
93 // frequency units
94  DEFINE_UNIT(Hz, Unit::GHz * 1e-9);
95  DEFINE_UNIT(kHz, Unit::GHz * 1e-6);
96  DEFINE_UNIT(MHz, Unit::GHz * 1e-3);
97  DEFINE_UNIT(mHz, Unit::GHz * 1e-12);
99 // angle units
100  DEFINE_UNIT(mrad , Unit::rad * 1e-3);
101  DEFINE_UNIT(deg , TMath::DegToRad());
103 // energy units
104  DEFINE_UNIT(eV , Unit::GeV * 1e-9);
105  DEFINE_UNIT(keV , Unit::eV * 1e3);
106  DEFINE_UNIT(MeV , Unit::eV * 1e6);
107  DEFINE_UNIT(TeV , Unit::eV * 1e12);
108  DEFINE_UNIT(J , 1.0 / TMath::Qe() * Unit::eV);
111 // charge units
112  DEFINE_UNIT(C , 1.0 / TMath::Qe() * Unit::e);
113  DEFINE_UNIT(fC , Unit::C / 1.0e15);
115 // magnetic field units
117  DEFINE_UNIT(mT , Unit::T * 1e-3);
118  DEFINE_UNIT(Gauss , Unit::T * 1e-4);
119  DEFINE_UNIT(kGauss , Unit::Gauss * 1e3);
121 // density units
122  DEFINE_UNIT_NAME(mg_cm3 , Unit::g_cm3 * 1e-3, "mg/cm3");
123  DEFINE_UNIT_NAME(kg_cm3 , Unit::g_cm3 * 1e3, "kg/cm3");
124  DEFINE_UNIT_NAME(g_mm3 , Unit::g_cm3 / Unit::mm3, "g/mm3");
125  DEFINE_UNIT_NAME(mg_mm3 , Unit::mg_cm3 / Unit::mm3, "mg/mm3");
126  DEFINE_UNIT_NAME(kg_mm3 , Unit::kg_cm3 / Unit::mm3, "kg/mm3");
129  double Unit::convertValue(double value, const std::string& unitString)
130  {
131  auto it = s_conversionFactors.find(unitString);
132  if (it == s_conversionFactors.end()) {
133  B2ERROR("Could not find conversion factor for unit " << unitString << ", assuming 1.0");
134  return value;
135  }
136  return it->second * value;
137  }
138 
139  double Unit::convertValueToUnit(double value, const std::string& unitString)
140  {
141  auto it = s_conversionFactors.find(unitString);
142  if (it == s_conversionFactors.end()) {
143  B2ERROR("Could not find conversion factor for unit " << unitString << ", assuming 1.0");
144  return value;
145  }
146  return value / it->second;
147  }
148 
149  double Unit::registerConversion(const string& name, double value)
150  {
151  if (!s_conversionFactors.insert(make_pair(name, value)).second) {
152  B2ERROR("Conversion factor for unit " << name << " already exists, ignoring");
153  }
154  return value;
155  }
156 
158 }
159 
160 /*** The implementation of the Const class defined in Const.h starts here ***/
161 
163 {
164  if (det == Const::PXD) return "PXD";
165  else if (det == Const::SVD) return "SVD";
166  else if (det == Const::CDC) return "CDC";
167  else if (det == Const::TOP) return "TOP";
168  else if (det == Const::ARICH) return "ARICH";
169  else if (det == Const::ECL) return "ECL";
170  else if (det == Const::KLM) return "KLM";
171  else if (det == Const::BKLM) return "BKLM";
172  else if (det == Const::EKLM) return "EKLM";
173  else if (det == Const::IR) return "IR";
174  else if (det == Const::TRG) return "TRG";
175  else if (det == Const::DAQ) return "DAQ";
176  else if (det == Const::BEAST) return "BEAST";
177  else if (det == Const::TEST) return "TEST";
178  else B2FATAL("Unknown detector component: " << det);
179  return "INVALID";
180 }
181 
182 Const::DetectorSet operator + (const Const::DetectorSet& firstSet, const Const::DetectorSet& secondSet)
183 {
184  Const::DetectorSet set(firstSet);
185  set += secondSet;
186  return set;
187 }
188 
189 Const::DetectorSet operator - (const Const::DetectorSet& firstSet, const Const::DetectorSet& secondSet)
190 {
191  Const::DetectorSet set(firstSet);
192  set -= secondSet;
193  return set;
194 }
195 
197 {
198  Const::DetectorSet set(firstDet);
199  set += secondDet;
200  return set;
201 }
202 
204 {
205  if (det == 0 || det > 0x100) {
206  B2ERROR("Const::DetectorSet::getBit() called for an invalid detector "
207  "identifier (possibly a subdetector):" << det);
208  return 0;
209  }
210  if (det > TEST) {
211  B2ERROR("Const::DetectorSet::getBit(): Invalid detector ID");
212  }
213  return (1 << (det - 1));
214 }
215 
217 {
218  switch (bit) {
219  case 0x0001: return PXD;
220  case 0x0002: return SVD;
221  case 0x0004: return CDC;
222  case 0x0008: return TOP;
223  case 0x0010: return ARICH;
224  case 0x0020: return ECL;
225  case 0x0040: return KLM;
226  case 0x0080: return IR;
227  case 0x0100: return TRG;
228  case 0x0200: return DAQ;
229  case 0x0400: return BEAST;
230  case 0x0800: return TEST;
231  default: return invalidDetector;
232  }
233 }
234 
236 {
237  unsigned short bit = getBit(det);
238  if ((m_bits & bit) == 0) return -1;
239  int index = 0;
240  for (unsigned short setBit = 1; setBit < bit; setBit *= 2) {
241  if ((m_bits & setBit) != 0) ++index;
242  }
243  return index;
244 }
245 
247 {
248  if (index < 0) return Const::invalidDetector;
249  for (unsigned short setBit = 1; setBit < 0x1000; setBit *= 2) {
250  if ((m_bits & setBit) != 0) --index;
251  if (index < 0) return getDetector(setBit);
252  }
253  return Const::invalidDetector;
254 }
255 
257 {
258  int size = 0;
259  for (unsigned short setBit = 1; setBit < 0x1000; setBit *= 2) {
260  if ((m_bits & setBit) != 0) ++size;
261  }
262  return size;
263 }
264 
265 std::string Const::DetectorSet::__repr__() const
266 {
267  std::string result = "<set: ";
268  std::string detectorNames[] = {"invalidDetector", "PXD", "SVD", "CDC", "TOP", "ARICH", "ECL", "KLM", "IR", "TRG", "DAQ", "BEAST", "TEST"};
269  for (size_t index = 1; index <= Const::TEST; index++) {
270  if (contains(EDetector(index))) {
271  if (result.size() > 6) {
272  result += ",";
273  }
274  result += detectorNames[index];
275  }
276  }
277  result += ">";
278  return result;
279 }
280 
281 
282 const Const::DetectorSet Const::VXDDetectors::c_set = Const::PXD + Const::SVD;
283 
284 const Const::DetectorSet Const::TrackingDetectors::c_set = Const::PXD + Const::SVD + Const::CDC;
285 
286 const Const::DetectorSet Const::PIDDetectors::c_set = Const::SVD + Const::CDC + Const::TOP + Const::ARICH + Const::ECL + Const::KLM;
287 
288 
289 const Const::DetectorSet Const::allDetectors = Const::IR + Const::PXD + Const::SVD + Const::CDC +
290  Const::TOP + Const::ARICH + Const::ECL + Const::KLM +
291  Const::TRG + Const::DAQ + Const::BEAST + Const::TEST;
292 
293 const Const::DetectorSet Const::ClusterDetectors::c_set = Const::ECL + Const::KLM;
294 
296 {
297  return m_pdgCode < other.m_pdgCode;
298 }
299 
301 {
302  if (!m_set) {
303  *this = invalidParticle;
304  } else {
305  m_pdgCode = m_set->at(++m_index).getPDGCode();
306  }
307 
308  return *this;
309 }
310 
312 {
313  Const::ParticleType p = *this;
314  ++(*this);
315  return p;
316 }
317 
318 const TParticlePDG* Const::ParticleType::getParticlePDG() const
319 {
320  return EvtGenDatabasePDG::Instance()->GetParticle(m_pdgCode);
321 }
322 
324 {
325  return getParticlePDG()->Mass();
326 }
327 
328 std::string Const::ParticleType::__repr__() const
329 {
330  std::string result = "<type: ";
331  result += getParticlePDG()->GetName();
332  result += ">";
333  return result;
334 }
335 
338  + Const::ParticleType(321) + Const::ParticleType(2212) + Const::ParticleType(1000010020);
339 
343 
344 
351 
358 
366 const Const::ParticleType Const::invalidParticle = Const::ParticleType(9900000); // codes beginning with 99... are reserved
367 const Const::ParticleType Const::unspecifiedParticle = Const::ParticleType(9900001); // codes beginning with 99... are reserved
368 
372 
374 const double Const::muonMass = Const::muon.getMass();
375 const double Const::pionMass = Const::pion.getMass();
376 const double Const::kaonMass = Const::kaon.getMass();
377 const double Const::protonMass = Const::proton.getMass();
379 const double Const::pi0Mass = Const::pi0.getMass();
380 const double Const::neutronMass = Const::neutron.getMass();
381 const double Const::K0Mass = Const::Kshort.getMass();
382 
384 {
385  if (contains(p))
386  return;
387  m_particles.emplace_back(p.getPDGCode(), this, m_particles.size());
388 }
389 
391 {
392  return (std::find(m_particles.begin(), m_particles.end(), p) != m_particles.end());
393 }
394 
395 Const::ParticleSet operator + (const Const::ParticleSet& firstSet, const Const::ParticleSet& secondSet)
396 {
397  Const::ParticleSet result(firstSet);
398  for (Const::ParticleType pdgIter = secondSet.begin(); pdgIter != secondSet.end(); ++pdgIter) {
399  result.add(pdgIter);
400  }
401  return result;
402 }
403 
404 
405 const double Const::speedOfLight = 29.9792458;
406 const double Const::kBoltzmann = 8.617343 * 1.0e-5 * Unit::eV / Unit::K;
407 const double Const::ehEnergy = 3.65 * Unit::eV;
408 const double Const::fineStrConst = 1.0 / 137.036;
409 const double Const::permSi = 11.9 * 8.8542 * 1e-18 * Unit::C / Unit::V / Unit::um;
410 const double Const::uTherm = 0.026 * Unit::V;
411 const double Const::eMobilitySi = 1415 * Unit::cm2 / Unit::V / Unit::s;
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:470
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:269
Provides a type-safe way to pass members of the clusterSet set.
Definition: Const.h:507
The DetectorSet class for sets of detector IDs in the form of EDetector values.
Definition: Const.h:71
size_t size() const
Getter for number of detector IDs in this set.
Definition: UnitConst.cc:256
static EDetector getDetector(unsigned short bit)
Conversion of bit pattern to detector ID.
Definition: UnitConst.cc:216
int getIndex(EDetector det) const
Getter for the index of a given detector in this set.
Definition: UnitConst.cc:235
static unsigned short getBit(EDetector det)
Conversion of detector ID to bit pattern.
Definition: UnitConst.cc:203
std::string __repr__() const
String for printing in python.
Definition: UnitConst.cc:265
EDetector operator[](int index) const
Accessor for a detector ID in this set.
Definition: UnitConst.cc:246
static const DetectorSet c_set
The set of valid PID detectors.
Definition: Const.h:256
A set of ParticleType objects, with defined order.
Definition: Const.h:398
ParticleType begin() const
Returns first particle.
Definition: Const.h:438
bool contains(const ParticleType &p) const
Returns true if and only if the set contains 'p'.
Definition: UnitConst.cc:390
ParticleType end() const
Returns an invalid particle to check if iteration should be stopped.
Definition: Const.h:446
void add(const ParticleType &p)
Add a copy of the given ParticleType to this set.
Definition: UnitConst.cc:383
const ParticleType & find(int pdg) const
Returns particle in set with given PDG code, or invalidParticle if not found.
Definition: Const.h:452
The ParticleType class for identifying different particle types.
Definition: Const.h:289
int getPDGCode() const
PDG code.
Definition: Const.h:354
std::string __repr__() const
String for printing in python.
Definition: UnitConst.cc:328
bool operator<(const ParticleType &other) const
Comparison operator to be usable in sets.
Definition: UnitConst.cc:295
int m_pdgCode
PDG code of the particle.
Definition: Const.h:368
const TParticlePDG * getParticlePDG() const
Accessor for ROOT TParticlePDG object.
Definition: UnitConst.cc:318
ParticleType & operator++()
Prefix increment.
Definition: UnitConst.cc:300
double getMass() const
Particle mass.
Definition: UnitConst.cc:323
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:244
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:232
static const ParticleType neutron
neutron particle
Definition: Const.h:556
static const ParticleType unspecifiedParticle
Unspecified particle, used when no other particle type fits.
Definition: Const.h:563
static const ParticleType pi0
neutral pion particle
Definition: Const.h:555
static const Cluster clustermuon
muon cluster
Definition: Const.h:551
static const ParticleType Lambda
Lambda particle.
Definition: Const.h:559
static const DetectorSet allDetectors
The set of all detectors.
Definition: Const.h:279
static const double eMobilitySi
Electron mobility in intrinsic Silicon at room temperature.
Definition: Const.h:581
static const ParticleSet clusterSet
set of cluster particles
Definition: Const.h:536
static const ChargedStable muon
muon particle
Definition: Const.h:541
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:499
static const double kBoltzmann
Boltzmann constant in GeV/K.
Definition: Const.h:576
static const Cluster clusterelectron
electron cluster
Definition: Const.h:549
static const double kaonMass
charged kaon mass
Definition: Const.h:568
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition: Const.h:42
static const double K0Mass
neutral kaon mass
Definition: Const.h:573
static const Cluster clusterpion
charged hadron cluster
Definition: Const.h:550
static const ChargedStable pion
charged pion particle
Definition: Const.h:542
static std::string parseDetectors(EDetector det)
Converts Const::EDetector object to string.
Definition: UnitConst.cc:162
static const double pionMass
charged pion mass
Definition: Const.h:567
static const ParticleType Klong
K^0_L particle.
Definition: Const.h:558
static const ParticleSet finalStateParticlesSet
set of final set particles that can be created by the ParticleLoader
Definition: Const.h:538
static const double permSi
Permittivity of Silicon.
Definition: Const.h:579
static const double speedOfLight
[cm/ns]
Definition: Const.h:575
static const double electronMass
electron mass
Definition: Const.h:565
static const double neutronMass
neutron mass
Definition: Const.h:572
static const Cluster clusterKlong
K^0_L cluster.
Definition: Const.h:548
static const ParticleType antiLambda
Anti-Lambda particle.
Definition: Const.h:560
static const double deuteronMass
deuteron mass
Definition: Const.h:570
static const ChargedStable proton
proton particle
Definition: Const.h:544
static const ParticleType invalidParticle
Invalid particle, used internally.
Definition: Const.h:561
static const double ehEnergy
Energy needed to create an electron-hole pair in Si at std.
Definition: Const.h:577
static const ParticleType Kshort
K^0_S particle.
Definition: Const.h:557
static const double protonMass
proton mass
Definition: Const.h:569
static const double fineStrConst
The fine structure constant.
Definition: Const.h:578
static const Cluster clusterjunk
junk cluster
Definition: Const.h:552
static const double uTherm
Thermal Voltage at room temperature.
Definition: Const.h:580
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:543
static const ParticleType photon
photon particle
Definition: Const.h:554
static const double pi0Mass
neutral pion mass
Definition: Const.h:571
static const Cluster clusterphoton
photon cluster
Definition: Const.h:547
static const double muonMass
muon mass
Definition: Const.h:566
static const ChargedStable electron
electron particle
Definition: Const.h:540
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:545
static EvtGenDatabasePDG * Instance()
Instance method that loads the EvtGen table.
static const double mm
[millimeters]
Definition: Unit.h:70
static const double e
Standard of [electric charge].
Definition: Unit.h:53
static const double mm3
[cubic millimeters]
Definition: Unit.h:92
static const double GHz
Standard of [frequency].
Definition: Unit.h:49
static const double rad
Standard of [angle].
Definition: Unit.h:50
static const double kg_cm3
[kg/cm^3]
Definition: Unit.h:131
static const double Gauss
[gauss]
Definition: Unit.h:122
static const double um
[micrometers]
Definition: Unit.h:71
static const double V
[voltage]
Definition: Unit.h:117
static const double eV
[electronvolt]
Definition: Unit.h:112
static const double m2
[square meters]
Definition: Unit.h:77
static const double b
[barn]
Definition: Unit.h:81
static const double m
[meters]
Definition: Unit.h:69
static const double C
[coulomb]
Definition: Unit.h:126
static const double cm
Standard units with the value = 1.
Definition: Unit.h:47
static const double K
Standard of [temperature].
Definition: Unit.h:52
static const double g_cm3
Practical units with the value set at 1.
Definition: Unit.h:60
static const double mg_cm3
[mg/cm^3]
Definition: Unit.h:130
static const double ns
Standard of [time].
Definition: Unit.h:48
static const double s
[second]
Definition: Unit.h:95
static const double GeV
Standard of [energy, momentum, mass].
Definition: Unit.h:51
static const double T
[tesla]
Definition: Unit.h:120
static const double cm2
[square centimeters]
Definition: Unit.h:78
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
static std::map< std::string, double > s_conversionFactors
Map to be able to convert between units using string representations of the unit name.
Definition: Unit.h:163
static double registerConversion(const std::string &name, double value)
registers the name and value of a conversion in s_conversionFactors.
Definition: UnitConst.cc:149
static double convertValueToUnit(double value, const std::string &unitString)
Converts a floating point value from the standard framework unit to the given unit.
Definition: UnitConst.cc:139
DEFINE_UNIT(cm, 1.)
Standard units with the value = 1.
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129
DEFINE_UNIT_NAME(g_cm3, 1., "g/cm3")
Practical units with the value = 1.
B2Vector3< DataType > operator-(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for substracting a TVector3 from a B2Vector3
Definition: B2Vector3.h:528
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition: B2Vector3.h:521
Abstract base class for different kinds of events.