Belle II Software  release-05-01-25
const.cc
1 #include <framework/gearbox/Const.h>
2 
3 #include <TParticlePDG.h>
4 
5 #include <gtest/gtest.h>
6 
7 using namespace std;
8 using namespace Belle2;
9 
10 namespace {
12  TEST(ConstTest, ParticleBasics)
13  {
14  //construction
21  EXPECT_THROW(Const::ChargedStable(Const::ParticleType(22)), std::runtime_error);
22  EXPECT_THROW(Const::ChargedStable(Const::ParticleType(-211)), std::runtime_error);
23 
24  const Const::ParticleSet emptyset;
25  EXPECT_FALSE(emptyset.contains(Const::Klong));
26 
27  //check indices of some defined particles
28  EXPECT_EQ(0, Const::electron.getIndex());
29  EXPECT_EQ(1, Const::muon.getIndex());
30  EXPECT_EQ(2, Const::pion.getIndex());
31  EXPECT_EQ(3, Const::kaon.getIndex());
32  EXPECT_EQ(4, Const::proton.getIndex());
33  EXPECT_EQ(5, Const::deuteron.getIndex());
34 
35  //and after a copy
36  Const::ChargedStable c = Const::muon;
37  EXPECT_EQ(1, c.getIndex());
38  Const::ParticleType p = Const::muon;
39  EXPECT_EQ(1, p.getIndex());
40 
41  //and after construction from PDG code
42  EXPECT_EQ(1, Const::ChargedStable(13).getIndex());
43 
44  //not in any set
45  EXPECT_EQ(-1, Const::invalidParticle.getIndex());
46  EXPECT_EQ(-1, Const::Klong.getIndex());
47  EXPECT_EQ(-1, Const::photon.getIndex());
48  }
49 
51  TEST(ConstTest, ParticleIteration)
52  {
53  const Const::ParticleSet emptyset;
54  EXPECT_FALSE(emptyset.contains(Const::Klong));
55 
56  const Const::ParticleSet set = Const::chargedStableSet;
57  Const::ParticleType prefix = set.begin();
58  EXPECT_EQ(1, (++prefix).getIndex());
59  Const::ParticleType postfix = set.begin();
60  EXPECT_EQ(0, (postfix++).getIndex());
61 
62  int size = 0;
63  //note: iterating over the restricted type (ParticleType would work, too)
64  for (const auto& c : set) {
65 
66  int pdg = c.getPDGCode();
67  unsigned int index = c.getIndex();
68 
69  switch (index) {
70  case 0:
71  EXPECT_EQ(11, pdg);
72  break;
73  case 1:
74  EXPECT_EQ(13, pdg);
75  break;
76  case 2:
77  EXPECT_EQ(211, pdg);
78  break;
79  case 3:
80  EXPECT_EQ(321, pdg);
81  break;
82  case 4:
83  EXPECT_EQ(2212, pdg);
84  break;
85  case 5:
86  EXPECT_EQ(1000010020, pdg);
87  break;
88  default:
89  EXPECT_TRUE(false) << "Index >5 encountered?";
90  }
91  size++;
92  }
93  int setSize = Const::ChargedStable::c_SetSize;
94  EXPECT_EQ(setSize, size);
95 
96  size = 0;
97  for (Const::ParticleType pdgIter = set.begin(); pdgIter != set.end(); ++pdgIter) {
98  size++;
99  }
100  EXPECT_EQ(6, size);
101 
102  Const::ChargedStable c = Const::kaon;
103  EXPECT_TRUE(Const::chargedStableSet.contains(c));
104  EXPECT_EQ(3, c.getIndex());
105  ++c;
106  ++c;
107  EXPECT_EQ(5, c.getIndex());
108  EXPECT_EQ(1000010020, c.getPDGCode());
109  }
110 
112  TEST(ConstTest, ParticleCombination)
113  {
114  //no-op
115  const Const::ParticleSet set = Const::chargedStableSet + Const::chargedStableSet;
116  int size = 0;
117  for (Const::ParticleType pdgIter = set.begin(); pdgIter != set.end(); ++pdgIter) {
118  size++;
119  }
120  EXPECT_EQ(6, size);
121 
122  const Const::ParticleSet kaonSet = Const::kaon + Const::Klong + Const::Kshort;
123  size = 0;
124  for (Const::ParticleType pdgIter = kaonSet.begin(); pdgIter != kaonSet.end(); ++pdgIter) {
125  size++;
126  }
127  EXPECT_EQ(3, size);
128 
129  const Const::ParticleSet mergedSet = set + kaonSet;
130  size = 0;
131  for (Const::ParticleType pdgIter = mergedSet.begin(); pdgIter != mergedSet.end(); ++pdgIter) {
132  size++;
133  }
134  EXPECT_EQ(size, 8); //kaon should be removed
135  }
136 
137  TEST(ConstTest, FindInParticleSet)
138  {
139  for (const auto& c : Const::chargedStableSet) {
140  int pdg = c.getPDGCode();
141  EXPECT_EQ(pdg, Const::chargedStableSet.find(pdg).getPDGCode());
142  }
143  EXPECT_TRUE(Const::chargedStableSet.find(12356467) == Const::invalidParticle);
144  }
145 
147  TEST(ConstTest, TDatabasePDG)
148  {
149  EXPECT_DOUBLE_EQ(Const::Klong.getParticlePDG()->Charge(), 0);
150  EXPECT_EQ(Const::Klong.getParticlePDG()->PdgCode(), 130);
151 
152  EXPECT_DOUBLE_EQ(Const::proton.getParticlePDG()->Charge(), 3);
153  EXPECT_EQ(Const::proton.getParticlePDG()->PdgCode(), 2212);
154 
155  Const::ParticleType antiproton(-2212);
156  EXPECT_DOUBLE_EQ(antiproton.getParticlePDG()->Charge(), -3);
157  EXPECT_EQ(antiproton.getParticlePDG()->PdgCode(), -2212);
158 
159  //Spin and lifetime are added manually to TDatabasePDG, test this
160  EXPECT_DOUBLE_EQ(1.0, Const::photon.getParticlePDG()->Spin());
161  EXPECT_DOUBLE_EQ(0.5, Const::proton.getParticlePDG()->Spin());
162  EXPECT_DOUBLE_EQ(0.5, Const::electron.getParticlePDG()->Spin());
163 
164  EXPECT_DOUBLE_EQ(0.0, Const::proton.getParticlePDG()->Lifetime());
165  //lifetime (about 881s for neutron, 2.197e-6s for muon)
166  EXPECT_TRUE(Const::neutron.getParticlePDG()->Lifetime() > 800);
167  EXPECT_TRUE(Const::neutron.getParticlePDG()->Lifetime() < 900);
168  EXPECT_TRUE(Const::muon.getParticlePDG()->Lifetime() < 2.2e-6);
169  EXPECT_TRUE(Const::muon.getParticlePDG()->Lifetime() > 2.1e-6);
170 
171  //AntiParticle is non-const...
172  auto* protonnonconst = const_cast<TParticlePDG*>(Const::proton.getParticlePDG());
173  auto* photonnonconst = const_cast<TParticlePDG*>(Const::photon.getParticlePDG());
174 
175  //test that AntiParticle() works as expected (not the case for previous implementation)
176  EXPECT_DOUBLE_EQ(protonnonconst->AntiParticle()->Charge(), -3);
177  EXPECT_EQ(protonnonconst->AntiParticle()->PdgCode(), -2212);
178 
179  EXPECT_TRUE(photonnonconst->AntiParticle() == photonnonconst);
180 
181 
182  EXPECT_TRUE(Const::invalidParticle.getParticlePDG() == nullptr);
183  EXPECT_TRUE(Const::unspecifiedParticle.getParticlePDG() == nullptr);
184  }
185 
187  TEST(ConstTest, DetectorSet)
188  {
189  Const::DetectorSet set(Const::IR);
190  EXPECT_EQ(set, Const::IR);
191  set += Const::PXD;
192  EXPECT_EQ(set, Const::IR + Const::PXD);
193  set += Const::PXD;
194  EXPECT_EQ(set, Const::IR + Const::PXD);
195  EXPECT_TRUE(set == Const::IR + Const::PXD);
196  EXPECT_FALSE(set == Const::IR);
197  set -= Const::IR;
198  EXPECT_EQ(set, Const::PXD);
199  EXPECT_TRUE(set.contains(Const::PXD));
200  EXPECT_FALSE(set.contains(Const::IR));
201  set += Const::SVD + Const::TEST;
202  EXPECT_EQ(set.getIndex(Const::IR), -1);
203  EXPECT_EQ(set.getIndex(Const::PXD), 0);
204  EXPECT_EQ(set.getIndex(Const::TEST), 2);
205  EXPECT_EQ(set[0], Const::PXD);
206  EXPECT_EQ(set[2], Const::TEST);
207  EXPECT_EQ(set[3], Const::invalidDetector);
208  EXPECT_EQ(set.size(), (size_t)3);
209  EXPECT_EQ(Const::allDetectors.size(), (size_t)12);
210  }
211 
213  TEST(ConstTest, RestrictedDetectorSet)
214  {
215  //note: cannot use EXPECT_EQ() here because c_size is only declared in header, but EXPECT_EQ wants a const&, leading to an undefined reference
216  EXPECT_TRUE(Const::PIDDetectors::c_set.size() == Const::PIDDetectors::c_size);
217  }
218 
219 } // namespace
Belle2::Const::DetectorSet
The DetectorSet class for sets of detector IDs in the form of EDetector values.
Definition: Const.h:66
Belle2::Const::ParticleSet
A set of ParticleType objects, with defined order.
Definition: Const.h:393
Belle2::Const::ParticleSet::contains
bool contains(const ParticleType &p) const
Returns true if and only if the set contains 'p'.
Definition: UnitConst.cc:372
Belle2::Const::ParticleSet::begin
ParticleType begin() const
Returns first particle.
Definition: Const.h:433
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::Const::ParticleSet::end
ParticleType end() const
Returns an invalid particle to check if iteration should be stopped.
Definition: Const.h:441
Belle2::Const::ParticleSet::size
unsigned int size() const
Returns number of particles in this set.
Definition: Const.h:422
Belle2::Const::ParticleType
The ParticleType class for identifying different particle types.
Definition: Const.h:284
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465