Belle II Software development
particlegun.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 <generators/particlegun/ParticleGun.h>
9#include <gtest/gtest.h>
10#include <iostream>
11using namespace std;
12
13namespace Belle2 {
19 class ParticleGunTest : public ::testing::Test {
20 protected:
22 virtual void SetUp()
23 {
24 parameters.momentumParams = {1, 1};
25 parameters.xVertexParams = {1, 1};
26 parameters.yVertexParams = {1, 1};
27 parameters.zVertexParams = {1, 1};
28 parameters.thetaParams = {1, 1};
29 parameters.phiParams = {1, 1};
30 }
31
35
37 void checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
38 std::vector<double>& pars);
39 };
40
41 void ParticleGunTest::checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
42 std::vector<double>& pars)
43 {
45 parameters.pdgCodes = {11};
46 for (auto item : allowed) {
47 dist = (ParticleGun::EDistribution) item.first;
48 //We need at least for parameters for some distributions.
49 pars = {1, 1, 1, 1};
50 //Check if distribution is accepted
51 ASSERT_EQ(item.second, pgun.setParameters(parameters)) << name << ": " << item.first << ", " << item.second;
52 if (item.second) {
53 //And if it is accepted, check number of parameters
54 for (int i = 0; i < 10; ++i) {
55 pars.resize(i, 1);
56 int minpar = (item.first == ParticleGun::c_fixedValue) ? 1 : 2;
57 //Polyline needs at least two points, so four parameters
59 || dist == ParticleGun::c_polylineCosDistribution) minpar = 4;
60 bool valid = i >= minpar;
61 //discrete and polyline need even number of parameters
64 valid &= i % 2 == 0;
65 }
66 //Check if numberof parameters is accepted
67 ASSERT_EQ(valid, pgun.setParameters(parameters)) << name << " " << pars.size();
68 }
69 }
70 //Check that zeros are forbidden for inversePt spectrum
71 if (item.first == ParticleGun::c_inversePtDistribution && item.second) {
72 pars = {1, 1};
73 ASSERT_TRUE(pgun.setParameters(parameters));
74 pars = {1, 0};
75 ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
76 parameters.momentumParams[1];
77 pars = {0, 1};
78 ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
79 parameters.momentumParams[1];
80 }
81 //Check polyline stuff
83 || dist == ParticleGun::c_polylineCosDistribution) && item.second) {
84 pars = {0, 1, 0, 1};
85 ASSERT_TRUE(pgun.setParameters(parameters));
86 //x needs to be ascending
87 pars = {1, 0, 0, 1};
88 ASSERT_FALSE(pgun.setParameters(parameters));
89 //at least one positive y value
90 pars = {0, 1, 0, 0};
91 ASSERT_FALSE(pgun.setParameters(parameters));
92 //no negative y values
93 pars = {0, 1, 1, -1};
94 ASSERT_FALSE(pgun.setParameters(parameters));
95 }
96 //Check discrete spectrum has non-negative weights
98 pars = {0, 1, 0, 1};
99 ASSERT_TRUE(pgun.setParameters(parameters));
100 //at least one weight must be positive
101 pars = {0, 1, 0, 0};
102 ASSERT_FALSE(pgun.setParameters(parameters));
103 //no negative weights
104 pars = {0, 1, 1, -1};
105 ASSERT_FALSE(pgun.setParameters(parameters));
106 }
107 }
108 std::cout << std::endl;
109 }
110
112 TEST_F(ParticleGunTest, MomentumDistributions)
113 {
114 std::map<int, bool> distributions = {
127 };
128 checkVariable("momentum", distributions, parameters.momentumDist, parameters.momentumParams);
129 }
130
132 TEST_F(ParticleGunTest, VertexDistributions)
133 {
134 std::map<int, bool> distributions = {
147 };
148 checkVariable("xvertex", distributions, parameters.xVertexDist, parameters.xVertexParams);
149 checkVariable("yvertex", distributions, parameters.yVertexDist, parameters.yVertexParams);
150 checkVariable("zvertex", distributions, parameters.zVertexDist, parameters.zVertexParams);
151 }
152
154 TEST_F(ParticleGunTest, AngularDistributions)
155 {
156 std::map<int, bool> distributions = {
169 };
170 checkVariable("theta", distributions, parameters.thetaDist, parameters.thetaParams);
171 checkVariable("phi", distributions, parameters.phiDist, parameters.phiParams);
172 }
173
174} // namespace
The fixture for testing the Particlegun.
ParticleGun::Parameters parameters
Variable parameters.
virtual void SetUp()
Set up parameters which work for all distributions.
ParticleGun pgun
Variable of pgun.
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
@ c_polylineDistribution
Distribution given as list of (x,y) points.
Definition ParticleGun.h:57
@ c_normalDistribution
Normal distribution, parameters are mean and sigma.
Definition ParticleGun.h:43
@ c_normalCosDistribution
Normal distribution of the cosinge, parameters are mean and sigma.
Definition ParticleGun.h:47
@ c_uniformPtDistribution
Uniform distribution of Pt, parameters are min and max value.
Definition ParticleGun.h:35
@ c_polylinePtDistribution
Same as polylineDistribution but for the transverse momentum.
Definition ParticleGun.h:59
@ c_uniformDistribution
Uniform distribution, parameters are min and max value.
Definition ParticleGun.h:33
@ c_discreteSpectrum
Discrete spectrum, parameters are first the values and then the weights (non-negative) for each value...
Definition ParticleGun.h:49
@ c_fixedValue
Fixed value, no random generation at all, 1 parameter.
Definition ParticleGun.h:31
@ c_normalPtDistribution
Normal distribution of Pt, parameters are mean and sigma.
Definition ParticleGun.h:45
@ c_uniformCosDistribution
Uniform distribution of the cosine of the values, parameters are min and max value.
Definition ParticleGun.h:37
@ c_inversePtDistribution
Distribution uniform in the inverse pt distribution, that is uniform in track curvature.
Definition ParticleGun.h:53
@ c_polylineCosDistribution
Same as polylineDistribution but for the cosine of the angle.
Definition ParticleGun.h:61
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
void checkVariable(const std::string &name, const std::map< int, bool > &allowed, ParticleGun::EDistribution &dist, std::vector< double > &pars)
check one of the variables given a list of allowed and excluded distributions
Abstract base class for different kinds of events.
STL namespace.
Struct to keep all necessary parameters for the particle gun.
Definition ParticleGun.h:65