Belle II Software light-2406-ragdoll
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
19using namespace Belle2;
20using namespace std;
21
22/*** The implementation of the Unit class defined in Unit.h starts here ***/
23
24namespace 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);
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
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");
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
183{
184 while (1) {
185 m_SetBit = m_SetBit << 1;
186 if (m_SetBit >= 0x1000) {
187 m_SetBit = invalidDetector;
188 return *this;
189 }
190 if ((m_DetectorSetBits & m_SetBit) != 0)
191 break;
192 }
193 m_Index++;
194 return *this;
195}
196
198 const Const::DetectorSet::Iterator& iterator)
199{
200 return m_SetBit == iterator.m_SetBit;
201}
202
204 const Const::DetectorSet::Iterator& iterator)
205{
206 return m_SetBit != iterator.m_SetBit;
207}
208
210{
211 uint16_t setBit = 1;
212 while ((m_bits & setBit) == 0) {
213 setBit = setBit << 1;
214 if (setBit >= 0x1000)
215 return Const::DetectorSet::Iterator(0, m_bits, invalidDetector);
216 }
217 return Const::DetectorSet::Iterator(0, m_bits, setBit);
218}
219
221{
222 return Const::DetectorSet::Iterator(0, m_bits, invalidDetector);
223}
224
226{
227 Const::DetectorSet set(firstSet);
228 set += secondSet;
229 return set;
230}
231
233{
234 Const::DetectorSet set(firstSet);
235 set -= secondSet;
236 return set;
237}
238
240{
241 Const::DetectorSet set(firstDet);
242 set += secondDet;
243 return set;
244}
245
247{
248 if (det == 0 || det > 0x100) {
249 B2ERROR("Const::DetectorSet::getBit() called for an invalid detector "
250 "identifier (possibly a subdetector):" << det);
251 return 0;
252 }
253 if (det > TEST) {
254 B2ERROR("Const::DetectorSet::getBit(): Invalid detector ID");
255 }
256 return (1 << (det - 1));
257}
258
260{
261 switch (bit) {
262 case 0x0001: return PXD;
263 case 0x0002: return SVD;
264 case 0x0004: return CDC;
265 case 0x0008: return TOP;
266 case 0x0010: return ARICH;
267 case 0x0020: return ECL;
268 case 0x0040: return KLM;
269 case 0x0080: return IR;
270 case 0x0100: return TRG;
271 case 0x0200: return DAQ;
272 case 0x0400: return BEAST;
273 case 0x0800: return TEST;
274 default: return invalidDetector;
275 }
276}
277
279{
280 unsigned short bit = getBit(det);
281 if ((m_bits & bit) == 0) return -1;
282 int index = 0;
283 for (unsigned short setBit = 1; setBit < bit; setBit *= 2) {
284 if ((m_bits & setBit) != 0) ++index;
285 }
286 return index;
287}
288
290{
291 int size = 0;
292 for (unsigned short setBit = 1; setBit < 0x1000; setBit *= 2) {
293 if ((m_bits & setBit) != 0) ++size;
294 }
295 return size;
296}
297
299{
300 std::string result = "<set: ";
301 const std::string detectorNames[] = {"invalidDetector", "PXD", "SVD", "CDC", "TOP", "ARICH", "ECL", "KLM", "IR", "TRG", "DAQ", "BEAST", "TEST"};
302 for (size_t index = 1; index <= Const::TEST; index++) {
303 if (contains(EDetector(index))) {
304 if (result.size() > 6) {
305 result += ",";
306 }
307 result += detectorNames[index];
308 }
309 }
310 result += ">";
311 return result;
312}
313
314
315const Const::DetectorSet Const::VXDDetectors::c_set = Const::PXD + Const::SVD;
316
317const Const::DetectorSet Const::TrackingDetectors::c_set = Const::PXD + Const::SVD + Const::CDC;
318
319const Const::DetectorSet Const::PIDDetectors::c_set = Const::SVD + Const::CDC + Const::TOP + Const::ARICH + Const::ECL + Const::KLM;
320
321
322const Const::DetectorSet Const::allDetectors = Const::IR + Const::PXD + Const::SVD + Const::CDC +
323 Const::TOP + Const::ARICH + Const::ECL + Const::KLM +
324 Const::TRG + Const::DAQ + Const::BEAST + Const::TEST;
325
326const Const::DetectorSet Const::ClusterDetectors::c_set = Const::ECL + Const::KLM;
327
329{
330 return m_pdgCode < other.m_pdgCode;
331}
332
334{
335 if (!m_set) {
336 *this = invalidParticle;
337 } else {
338 m_pdgCode = m_set->at(++m_index).getPDGCode();
339 }
340
341 return *this;
342}
343
345{
346 Const::ParticleType p = *this;
347 ++(*this);
348 return p;
349}
350
351const TParticlePDG* Const::ParticleType::getParticlePDG() const
352{
353 return EvtGenDatabasePDG::Instance()->GetParticle(m_pdgCode);
354}
355
357{
358 return getParticlePDG()->Mass();
359}
360
362{
363 std::string result = "<type: ";
364 result += getParticlePDG()->GetName();
365 result += ">";
366 return result;
367}
368
372
376
377
384
391
400const Const::ParticleType Const::invalidParticle = Const::ParticleType(9900000); // codes beginning with 99... are reserved
401const Const::ParticleType Const::unspecifiedParticle = Const::ParticleType(9900001); // codes beginning with 99... are reserved
402
406
408const double Const::muonMass = Const::muon.getMass();
409const double Const::pionMass = Const::pion.getMass();
410const double Const::kaonMass = Const::kaon.getMass();
413const double Const::pi0Mass = Const::pi0.getMass();
415const double Const::K0Mass = Const::Kshort.getMass();
416
418{
419 if (contains(p))
420 return;
421 m_particles.emplace_back(p.getPDGCode(), this, m_particles.size());
422}
423
425{
426 return (std::find(m_particles.begin(), m_particles.end(), p) != m_particles.end());
427}
428
430{
431 Const::ParticleSet result(firstSet);
432 for (Const::ParticleType pdgIter = secondSet.begin(); pdgIter != secondSet.end(); ++pdgIter) {
433 result.add(pdgIter);
434 }
435 return result;
436}
437
438
439const double Const::speedOfLight = 29.9792458;
440const double Const::kBoltzmann = 8.617343 * 1.0e-5 * Unit::eV / Unit::K;
441const double Const::ehEnergy = 3.65 * Unit::eV;
442const double Const::fineStrConst = 1.0 / 137.036;
443const double Const::permSi = 11.9 * 8.8542 * 1e-18 * Unit::C / Unit::V / Unit::um;
444const double Const::uTherm = 0.026 * Unit::V;
445const double Const::eMobilitySi = 1415 * Unit::cm2 / Unit::V / Unit::s;
446
447const double Const::doubleNaN = std::numeric_limits<double>::quiet_NaN();
448const float Const::floatNaN = std::numeric_limits<float>::quiet_NaN();
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:589
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:388
Provides a type-safe way to pass members of the clusterSet set.
Definition: Const.h:626
uint16_t m_DetectorSetBits
Bits in DetectorSet.
Definition: Const.h:171
bool operator==(const Iterator &iterator)
Operator ==.
Definition: UnitConst.cc:197
bool operator!=(const Iterator &iterator)
Operator !=.
Definition: UnitConst.cc:203
Iterator & operator++()
Operator ++.
Definition: UnitConst.cc:182
The DetectorSet class for sets of detector IDs in the form of EDetector values.
Definition: Const.h:80
std::string __str__() const
String for printing in python.
Definition: UnitConst.cc:298
size_t size() const
Getter for number of detector IDs in this set.
Definition: UnitConst.cc:289
int getIndex(EDetector det) const
Getter for the index of a given detector in this set.
Definition: UnitConst.cc:278
Iterator end() const
Ending iterator.
Definition: UnitConst.cc:220
uint16_t m_bits
The internal representation of the set as bit pattern.
Definition: Const.h:286
static uint16_t getBit(EDetector det)
Conversion of detector ID to bit pattern.
Definition: UnitConst.cc:246
bool contains(const DetectorSet &set) const
Check whether this set contains another set.
Definition: Const.h:235
static EDetector getDetector(uint16_t bit)
Conversion of bit pattern to detector ID.
Definition: UnitConst.cc:259
Iterator begin() const
Beginning iterator.
Definition: UnitConst.cc:209
static const DetectorSet c_set
The set of valid PID detectors.
Definition: Const.h:375
A set of ParticleType objects, with defined order.
Definition: Const.h:517
ParticleType begin() const
Returns first particle.
Definition: Const.h:557
bool contains(const ParticleType &p) const
Returns true if and only if the set contains 'p'.
Definition: UnitConst.cc:424
ParticleType end() const
Returns an invalid particle to check if iteration should be stopped.
Definition: Const.h:565
void add(const ParticleType &p)
Add a copy of the given ParticleType to this set.
Definition: UnitConst.cc:417
const ParticleType & find(int pdg) const
Returns particle in set with given PDG code, or invalidParticle if not found.
Definition: Const.h:571
The ParticleType class for identifying different particle types.
Definition: Const.h:408
int getPDGCode() const
PDG code.
Definition: Const.h:473
std::string __repr__() const
String for printing in python.
Definition: UnitConst.cc:361
bool operator<(const ParticleType &other) const
Comparison operator to be usable in sets.
Definition: UnitConst.cc:328
int m_pdgCode
PDG code of the particle.
Definition: Const.h:487
const TParticlePDG * getParticlePDG() const
Accessor for ROOT TParticlePDG object.
Definition: UnitConst.cc:351
ParticleType & operator++()
Prefix increment.
Definition: UnitConst.cc:333
double getMass() const
Particle mass.
Definition: UnitConst.cc:356
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:363
static const DetectorSet c_set
The set of valid tracking detectors.
Definition: Const.h:351
static const ParticleType neutron
neutron particle
Definition: Const.h:675
static const ParticleType unspecifiedParticle
Unspecified particle, used when no other particle type fits.
Definition: Const.h:683
static const ParticleType pi0
neutral pion particle
Definition: Const.h:674
static const Cluster clustermuon
muon cluster
Definition: Const.h:670
static const ParticleType Lambda
Lambda particle.
Definition: Const.h:679
static const DetectorSet allDetectors
The set of all detectors.
Definition: Const.h:398
static const double eMobilitySi
Electron mobility in intrinsic Silicon at room temperature.
Definition: Const.h:701
static const ParticleSet clusterSet
set of cluster particles
Definition: Const.h:655
static const ChargedStable muon
muon particle
Definition: Const.h:660
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:618
static const float floatNaN
quiet_NaN
Definition: Const.h:704
static const double kBoltzmann
Boltzmann constant in GeV/K.
Definition: Const.h:696
static const Cluster clusterelectron
electron cluster
Definition: Const.h:668
static const double kaonMass
charged kaon mass
Definition: Const.h:688
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition: Const.h:42
static const double K0Mass
neutral kaon mass
Definition: Const.h:693
static const Cluster clusterpion
charged hadron cluster
Definition: Const.h:669
static const ChargedStable pion
charged pion particle
Definition: Const.h:661
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:687
static const ParticleType Klong
K^0_L particle.
Definition: Const.h:678
static const ParticleSet finalStateParticlesSet
set of final set particles that can be created by the ParticleLoader
Definition: Const.h:657
static const double permSi
Permittivity of Silicon.
Definition: Const.h:699
static const double speedOfLight
[cm/ns]
Definition: Const.h:695
static const double electronMass
electron mass
Definition: Const.h:685
static const double neutronMass
neutron mass
Definition: Const.h:692
static const Cluster clusterKlong
K^0_L cluster.
Definition: Const.h:667
static const ParticleType antiLambda
Anti-Lambda particle.
Definition: Const.h:680
static const double deuteronMass
deuteron mass
Definition: Const.h:690
static const ChargedStable proton
proton particle
Definition: Const.h:663
static const ParticleType invalidParticle
Invalid particle, used internally.
Definition: Const.h:681
static const double ehEnergy
Energy needed to create an electron-hole pair in Si at std.
Definition: Const.h:697
static const ParticleType Kshort
K^0_S particle.
Definition: Const.h:677
static const double protonMass
proton mass
Definition: Const.h:689
static const double doubleNaN
quiet_NaN
Definition: Const.h:703
static const ParticleType antiNeutron
Anti-neutron particle.
Definition: Const.h:676
static const double fineStrConst
The fine structure constant.
Definition: Const.h:698
static const Cluster clusterjunk
junk cluster
Definition: Const.h:671
static const double uTherm
Thermal Voltage at room temperature.
Definition: Const.h:700
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:662
static const ParticleType photon
photon particle
Definition: Const.h:673
static const double pi0Mass
neutral pion mass
Definition: Const.h:691
static const Cluster clusterphoton
photon cluster
Definition: Const.h:666
static const double muonMass
muon mass
Definition: Const.h:686
static const ChargedStable electron
electron particle
Definition: Const.h:659
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:664
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
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
B2Vector3< DataType > operator-(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for substracting a TVector3 from a B2Vector3
Definition: B2Vector3.h:551
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129
#define DEFINE_UNIT_NAME(var, value, name)
Define a new Unit given a name for the conversion table.
Definition: UnitConst.cc:33
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition: B2Vector3.h:544
#define DEFINE_UNIT(var, value)
Define a new Unit and take the variable name as name for the conversion table.
Definition: UnitConst.cc:35
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24
STL namespace.