Belle II Software  release-08-01-10
particlegun.cc
1 #include <generators/particlegun/ParticleGun.h>
2 #include <gtest/gtest.h>
3 #include <iostream>
4 using namespace std;
5 
6 namespace Belle2 {
12  class ParticleGunTest : public ::testing::Test {
13  protected:
15  virtual void SetUp()
16  {
17  parameters.momentumParams = {1, 1};
18  parameters.xVertexParams = {1, 1};
19  parameters.yVertexParams = {1, 1};
20  parameters.zVertexParams = {1, 1};
21  parameters.thetaParams = {1, 1};
22  parameters.phiParams = {1, 1};
23  }
28 
30  void checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
31  std::vector<double>& pars);
32  };
33 
34  void ParticleGunTest::checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
35  std::vector<double>& pars)
36  {
37  parameters = ParticleGun::Parameters();
38  parameters.pdgCodes = {11};
39  for (auto item : allowed) {
40  dist = (ParticleGun::EDistribution) item.first;
41  //We need at least for parameters for some distributions.
42  pars = {1, 1, 1, 1};
43  //Check if distribution is accepted
44  ASSERT_EQ(item.second, pgun.setParameters(parameters)) << name << ": " << item.first << ", " << item.second;
45  if (item.second) {
46  //And if it is accepted, check number of parameters
47  for (int i = 0; i < 10; ++i) {
48  pars.resize(i, 1);
49  int minpar = (item.first == ParticleGun::c_fixedValue) ? 1 : 2;
50  //Polyline needs at least two points, so four parameters
51  if (dist == ParticleGun::c_polylineDistribution || dist == ParticleGun::c_polylinePtDistribution
52  || dist == ParticleGun::c_polylineCosDistribution) minpar = 4;
53  bool valid = i >= minpar;
54  //discrete and polyline need even number of parameters
55  if (dist == ParticleGun::c_discreteSpectrum || dist == ParticleGun::c_polylineDistribution ||
56  dist == ParticleGun::c_polylinePtDistribution || dist == ParticleGun::c_polylineCosDistribution) {
57  valid &= i % 2 == 0;
58  }
59  //Check if numberof parameters is accepted
60  ASSERT_EQ(valid, pgun.setParameters(parameters)) << name << " " << pars.size();
61  }
62  }
63  //Check that zeros are forbidden for inversePt spectrum
64  if (item.first == ParticleGun::c_inversePtDistribution && item.second) {
65  pars = {1, 1};
66  ASSERT_TRUE(pgun.setParameters(parameters));
67  pars = {1, 0};
68  ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
69  parameters.momentumParams[1];
70  pars = {0, 1};
71  ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
72  parameters.momentumParams[1];
73  }
74  //Check polyline stuff
75  if ((dist == ParticleGun::c_polylineDistribution || dist == ParticleGun::c_polylinePtDistribution
76  || dist == ParticleGun::c_polylineCosDistribution) && item.second) {
77  pars = {0, 1, 0, 1};
78  ASSERT_TRUE(pgun.setParameters(parameters));
79  //x needs to be ascending
80  pars = {1, 0, 0, 1};
81  ASSERT_FALSE(pgun.setParameters(parameters));
82  //at least one positive y value
83  pars = {0, 1, 0, 0};
84  ASSERT_FALSE(pgun.setParameters(parameters));
85  //no negative y values
86  pars = {0, 1, 1, -1};
87  ASSERT_FALSE(pgun.setParameters(parameters));
88  }
89  //Check discrete spectrum has non-negative weights
90  if (dist == ParticleGun::c_discreteSpectrum) {
91  pars = {0, 1, 0, 1};
92  ASSERT_TRUE(pgun.setParameters(parameters));
93  //at least one weight must be positive
94  pars = {0, 1, 0, 0};
95  ASSERT_FALSE(pgun.setParameters(parameters));
96  //no negative weights
97  pars = {0, 1, 1, -1};
98  ASSERT_FALSE(pgun.setParameters(parameters));
99  }
100  }
101  std::cout << std::endl;
102  }
103 
105  TEST_F(ParticleGunTest, MomentumDistributions)
106  {
107  std::map<int, bool> distributions = {
108  {ParticleGun::c_fixedValue, true},
109  {ParticleGun::c_uniformDistribution, true},
110  {ParticleGun::c_uniformPtDistribution, true},
111  {ParticleGun::c_uniformCosDistribution, false},
112  {ParticleGun::c_normalDistribution, true},
113  {ParticleGun::c_normalPtDistribution, true},
114  {ParticleGun::c_normalCosDistribution, false},
115  {ParticleGun::c_discreteSpectrum, true},
116  {ParticleGun::c_inversePtDistribution, true},
117  {ParticleGun::c_polylineDistribution, true},
118  {ParticleGun::c_polylinePtDistribution, true},
119  {ParticleGun::c_polylineCosDistribution, false}
120  };
121  checkVariable("momentum", distributions, parameters.momentumDist, parameters.momentumParams);
122  }
123 
125  TEST_F(ParticleGunTest, VertexDistributions)
126  {
127  std::map<int, bool> distributions = {
128  {ParticleGun::c_fixedValue, true},
129  {ParticleGun::c_uniformDistribution, true},
130  {ParticleGun::c_uniformPtDistribution, false},
131  {ParticleGun::c_uniformCosDistribution, false},
132  {ParticleGun::c_normalDistribution, true},
133  {ParticleGun::c_normalPtDistribution, false},
134  {ParticleGun::c_normalCosDistribution, false},
135  {ParticleGun::c_discreteSpectrum, true},
136  {ParticleGun::c_inversePtDistribution, false},
137  {ParticleGun::c_polylineDistribution, true},
138  {ParticleGun::c_polylinePtDistribution, false},
139  {ParticleGun::c_polylineCosDistribution, false}
140  };
141  checkVariable("xvertex", distributions, parameters.xVertexDist, parameters.xVertexParams);
142  checkVariable("yvertex", distributions, parameters.yVertexDist, parameters.yVertexParams);
143  checkVariable("zvertex", distributions, parameters.zVertexDist, parameters.zVertexParams);
144  }
145 
147  TEST_F(ParticleGunTest, AngularDistributions)
148  {
149  std::map<int, bool> distributions = {
150  {ParticleGun::c_fixedValue, true},
151  {ParticleGun::c_uniformDistribution, true},
152  {ParticleGun::c_uniformPtDistribution, false},
153  {ParticleGun::c_uniformCosDistribution, true},
154  {ParticleGun::c_normalDistribution, true},
155  {ParticleGun::c_normalPtDistribution, false},
156  {ParticleGun::c_normalCosDistribution, true},
157  {ParticleGun::c_discreteSpectrum, true},
158  {ParticleGun::c_inversePtDistribution, false},
159  {ParticleGun::c_polylineDistribution, true},
160  {ParticleGun::c_polylinePtDistribution, false},
161  {ParticleGun::c_polylineCosDistribution, true}
162  };
163  checkVariable("theta", distributions, parameters.thetaDist, parameters.thetaParams);
164  checkVariable("phi", distributions, parameters.phiDist, parameters.phiParams);
165  }
167 } // namespace
The fixture for testing the Particlegun.
Definition: particlegun.cc:12
ParticleGun::Parameters parameters
Variable parameters.
Definition: particlegun.cc:27
virtual void SetUp()
Set up parameters which work for all distributions.
Definition: particlegun.cc:15
ParticleGun pgun
Variable of pgun.
Definition: particlegun.cc:25
Class to generate tracks in the particle gun and store them in a MCParticle graph.
Definition: ParticleGun.h:26
EDistribution
enum containing all known distributions available for generation of values
Definition: ParticleGun.h:29
TEST_F(ParticleGunTest, AngularDistributions)
Tests angular generation parameters.
Definition: particlegun.cc:147
Abstract base class for different kinds of events.
Struct to keep all necessary parameters for the particle gun.
Definition: ParticleGun.h:65