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