9#include <framework/datastore/DataStore.h>
10#include <framework/database/DBStore.h>
11#include <framework/dbobjects/BeamParameters.h>
12#include <framework/utilities/CalcMeanCov.h>
13#include <generators/utilities/InitialParticleGeneration.h>
15#include <gtest/gtest.h>
24 class InitialParticleGenerationTests :
public ::testing::Test {
31 generator.initialize();
35 beamparams.setHER(7.004, 0.0415, 0, {2.63169e-05, 1e-5, 1e-5});
36 beamparams.setLER(4.002, -0.0415, 0, {5.64063e-06, 1e-5, 1e-5});
37 beamparams.setVertex({0, 1, 2}, {4.10916e-07, 0, -2.64802e-06, 0, 1.7405e-11, 0, -2.64802e-06, 0, 0.000237962});
41 virtual void TearDown()
52 TEST_F(InitialParticleGenerationTests, TestCMSGeneration)
54 beamparams.setGenerationFlags(BeamParameters::c_generateCMS);
56 auto initialCMS = generator.generate();
58 EXPECT_EQ(initialCMS.getLabToCMS(), ROOT::Math::LorentzRotation());
60 EXPECT_NEAR(initialCMS.getHER().Theta(), 0, 1e-15);
62 EXPECT_NEAR(initialCMS.getLER().Theta(), M_PI, 1e-15);
64 EXPECT_NEAR(initialCMS.getHER().E(), initialCMS.getLER().E(), 1e-15);
66 EXPECT_EQ(initialCMS.getMass(), beamparams.getMass());
69 beamparams.setGenerationFlags(0);
71 auto initialLAB = generator.generate();
73 EXPECT_EQ(initialLAB.getMass(), beamparams.getMass());
75 EXPECT_EQ(initialLAB.getHER(), beamparams.getHER());
76 EXPECT_EQ(initialLAB.getLER(), beamparams.getLER());
78 EXPECT_EQ(initialCMS.getHER(), initialLAB.getLabToCMS() * initialLAB.getHER());
79 EXPECT_EQ(initialCMS.getLER(), initialLAB.getLabToCMS() * initialLAB.getLER());
83 TEST_F(InitialParticleGenerationTests, TestEnergySmear)
88 beamparams.setHER(10, 0, 0, {0.01});
89 beamparams.setLER(10, M_PI, 0, {0});
90 beamparams.setGenerationFlags(BeamParameters::c_smearBeamEnergy);
96 for (
int i = 0; i < 100000; ++i) {
97 auto& initial = generator.generate();
98 ASSERT_TRUE(initial.hasGenerationFlags(BeamParameters::c_smearBeamEnergy));
99 mean.add(initial.getMass());
102 EXPECT_NEAR(mean.getStddev(), 0.1, 0.0005);
103 EXPECT_NEAR(mean.getMean(), 20, 0.001);
107 beamparams.setLER(10, M_PI, 0, {0.01});
108 beamparams.setGenerationFlags(BeamParameters::c_smearBeamEnergy);
112 for (
int i = 0; i < 100000; ++i) {
113 auto& initial = generator.generate();
114 ASSERT_TRUE(initial.hasGenerationFlags(BeamParameters::c_smearBeamEnergy));
115 mean.add(initial.getMass());
117 EXPECT_NEAR(mean.getStddev(), std::sqrt(2) * 0.1, 0.0005);
118 EXPECT_NEAR(mean.getMean(), 20, 0.001);
126 TEST_F(InitialParticleGenerationTests, TestVertexSmear)
128 beamparams.setGenerationFlags(BeamParameters::c_smearVertex);
132 for (
int i = 0; i < 100000; ++i) {
133 auto& initial = generator.generate();
134 mean.add(initial.getVertex().X(), initial.getVertex().Y(), initial.getVertex().Z());
136 auto cov = beamparams.getCovVertex();
137 auto pos = beamparams.getVertex();
138 EXPECT_NEAR(mean.getMean(0), pos.x(), 1e-4);
139 EXPECT_NEAR(mean.getMean(1), pos.y(), 1e-4);
140 EXPECT_NEAR(mean.getMean(2), pos.z(), 1e-4);
141 for (
int i = 0; i < 3; ++i) {
142 for (
int j = 0; j < 3; ++j) {
143 EXPECT_NEAR(mean.getCovariance(i, j), cov(i, j), 1e-6);
156 TEST_F(InitialParticleGenerationTests, TestFlags)
159 for (
int flag = 0; flag <= BeamParameters::c_smearALL; ++flag) {
161 beamparams.setGenerationFlags(flag);
166 for (
int i = 0; i < 5; ++i) {
167 auto& initial = generator.generate();
168 EXPECT_EQ(flag, initial.getGenerationFlags());
170 const std::string flags = initial.getGenerationFlagString();
171 if (flag & BeamParameters::c_smearBeam) {
174 EXPECT_NE(initial.getHER(), beamparams.getHER()) << flags <<
" " << i;
175 EXPECT_NE(initial.getLER(), beamparams.getLER()) << flags <<
" " << i;
176 EXPECT_NE(initial.getHER(), last.
getHER()) << flags <<
" " << i;
177 EXPECT_NE(initial.getLER(), last.
getLER()) << flags <<
" " << i;
178 }
else if (!(flag & BeamParameters::c_generateCMS)) {
180 EXPECT_EQ(initial.getHER(), beamparams.getHER()) << flags <<
" " << i;
181 EXPECT_EQ(initial.getLER(), beamparams.getLER()) << flags <<
" " << i;
182 EXPECT_EQ(initial.getHER(), last.
getHER()) << flags <<
" " << i;
183 EXPECT_EQ(initial.getLER(), last.
getLER()) << flags <<
" " << i;
188 beamparams.setGenerationFlags(0);
189 EXPECT_EQ(initial.getHER(), beamparams.getLabToCMS() * beamparams.getHER()) << flags <<
" " << i;
190 EXPECT_EQ(initial.getLER(), beamparams.getLabToCMS() * beamparams.getLER()) << flags <<
" " << i;
194 EXPECT_EQ(initial.getHER(), last.
getHER()) << flags <<
" " << i;
195 EXPECT_EQ(initial.getLER(), last.
getLER()) << flags <<
" " << i;
198 if (flag & BeamParameters::c_smearVertex) {
200 EXPECT_NE(initial.getVertex(), beamparams.getVertex());
201 EXPECT_NE(initial.getVertex(), last.
getVertex());
204 EXPECT_EQ(initial.getVertex(), beamparams.getVertex());
205 EXPECT_EQ(initial.getVertex(), last.
getVertex());
213 TEST_F(InitialParticleGenerationTests, TestValidFlag)
215 beamparams.setGenerationFlags(0);
219 ROOT::Math::PxPyPzEVector her = initial.
getHER();
220 ROOT::Math::PxPyPzEVector ler = initial.
getLER();
221 ROOT::Math::XYZVector vertex = initial.
getVertex();
222 for (
int i = 0; i < 10; ++i) {
223 initial = generator.generate();
224 EXPECT_EQ(her, initial.
getHER());
225 EXPECT_EQ(ler, initial.
getLER());
228 beamparams.setGenerationFlags(BeamParameters::c_smearALL);
230 for (
int i = 0; i < 10; ++i) {
231 initial = generator.generate();
232 EXPECT_NE(her, initial.
getHER());
233 EXPECT_NE(ler, initial.
getLER());
241 TEST_F(InitialParticleGenerationTests, UpdateVertex)
243 beamparams.setGenerationFlags(BeamParameters::c_smearBeam);
246 ROOT::Math::XYZVector shift = generator.updateVertex();
247 EXPECT_EQ(shift, ROOT::Math::XYZVector(0, 1, 2));
251 shift = generator.updateVertex();
252 EXPECT_EQ(shift, ROOT::Math::XYZVector(0, 0, 0));
254 beamparams.setGenerationFlags(BeamParameters::c_smearALL);
256 shift = generator.updateVertex();
257 EXPECT_NE(shift, ROOT::Math::XYZVector(0, 0, 0));
258 EXPECT_EQ(nominal + shift, initial.
getVertex());
260 shift = generator.updateVertex();
261 EXPECT_EQ(shift, ROOT::Math::XYZVector(0, 0, 0));
264 shift = generator.updateVertex(
true);
265 EXPECT_NE(shift, ROOT::Math::XYZVector(0, 0, 0));
266 EXPECT_EQ(previous + shift, initial.
getVertex());
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
Class to calculate mean and and covariance between a number of parameters on running data without sto...
static DataStore & Instance()
Instance of singleton Store.
void setInitializeActive(bool active)
Setter for m_initializeActive.
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
This class contains the initial state for the given event.
const ROOT::Math::PxPyPzEVector & getHER() const
Get 4vector of the high energy beam.
const ROOT::Math::PxPyPzEVector & getLER() const
Get 4vector of the low energy beam.
const ROOT::Math::XYZVector & getVertex() const
Get the position of the collision.
void reset(bool keepEntries=false)
Invalidate all payloads.
static DBStore & Instance()
Instance of a singleton DBStore.
void addConstantOverride(const std::string &name, TObject *obj, bool oneRun=false)
Add constant override payload.
Abstract base class for different kinds of events.