Skip to content

Commit 22ea0b1

Browse files
committed
Cleaned up vector algebra. implemented ThreeVector tests. Using unique
pointers in ParticleFactory
1 parent a3dd239 commit 22ea0b1

File tree

9 files changed

+110
-52
lines changed

9 files changed

+110
-52
lines changed

include/Constants.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ constexpr auto ThreeAxis_X = ThreeVector<double>({1, 0, 0});
6262
constexpr auto ThreeAxis_Y = ThreeVector<double>({0, 1, 0});
6363

6464
/// Z axis (ThreeVector)
65-
constexpr auto ThreeAxis_Z = ThreeVector<double>({0, 1, 0});
65+
constexpr auto ThreeAxis_Z = ThreeVector<double>({0, 0, 1});
6666

6767
/// Standard 3D coordinate system
6868
constexpr auto ThreeAxes = CoordinateSystem<double, 3> {ThreeAxis_X, ThreeAxis_Y, ThreeAxis_Z};

include/FourVector.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,18 @@ template <typename T>
3939
class FourVector : public Vector<T, 4>
4040
{
4141
public:
42-
/// Default constructor
43-
constexpr FourVector() noexcept : Vector<T, 4> () {}
44-
4542
/// intializer_list constructor
4643
constexpr FourVector(const std::array<T, 4>& l) noexcept : Vector<T, 4>(l) {}
4744

48-
// /// initializer with individual elements
49-
// constexpr FourVector(const T& E, const T& P0, const T& P1, const T& P2) : FourVector({E, P0, P1, P2}) {}
50-
5145
/// energy + ThreeVector constructor
5246
/// \param E 0th component
5347
/// \param P #ThreeVector component
5448
constexpr FourVector(const T& E, const ThreeVector<T>& P) noexcept : Vector<T, 4>( {E, P[0], P[1], P[2]}) {}
5549

56-
// /// Vector<T, 4> constructor
57-
// constexpr FourVector(const Vector<T, 4>&& V) : Vector<T, 4>(V) {}
50+
/// Default constructor
51+
FourVector() = default;
5852

59-
/// using assignment with rhs = brace-enclosed list
53+
/// Use std::array's assignment operators
6054
using Vector<T, 4>::operator=;
6155

6256
/// \return inner (dot) product for 4-vectors

include/ParticleFactory.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ class ParticleFactory
6565

6666
/// Create a FinalStateParticle from a PDG code
6767
/// \param PDG PDG code of particle to create
68-
/// \return shared pointer to new final state particle
69-
std::shared_ptr<FinalStateParticle> createFinalStateParticle(int PDG);
68+
/// \return unique pointer to new final state particle
69+
std::unique_ptr<FinalStateParticle> createFinalStateParticle(int PDG);
7070

7171
/// Create an InitialStateParticle from a PDG code and a MassShape
7272
/// \param PDG PDG code of particle to create
7373
/// \param radialSize radial size of particle to create [GeV^-1]
74-
/// \return pointer to new InitialStateParticle object
75-
std::shared_ptr<InitialStateParticle> createInitialStateParticle(int PDG, double radialSize);
74+
/// \return unique pointer to new InitialStateParticle object
75+
std::unique_ptr<InitialStateParticle> createInitialStateParticle(int PDG, double radialSize);
7676

7777
/// Create a Resonance from a PDG code and a MassShape
7878
/// \param PDG PDG code of particle to create
7979
/// \param radialSize Radial size of particle to create [GeV^-1]
8080
/// \param massShape Pointer to MassShape object describing resonance
81-
/// \return pointer to new Resonance object
82-
std::shared_ptr<Resonance> createResonance(int PDG, double radialSize, std::unique_ptr<MassShape>&& massShape);
81+
/// \return unique pointer to new Resonance object
82+
std::unique_ptr<Resonance> createResonance(int PDG, double radialSize, std::unique_ptr<MassShape>&& massShape);
8383

8484
/// \name Particle table access
8585
/// @{

include/Vector.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <cassert>
3030
#include <cmath>
3131
#include <numeric>
32+
#include <ostream>
3233
#include <stdexcept>
3334
#include <string>
3435

@@ -42,19 +43,19 @@ template <typename T, size_t N>
4243
class Vector : public std::array<T, N>
4344
{
4445
public:
45-
/// Default Constructor
46-
constexpr Vector() noexcept : std::array<T, N>() {}
47-
4846
/// Constructor
49-
constexpr Vector(const std::array<T, N>& list) noexcept : std::array<T, N>(list) {}
47+
constexpr Vector(const std::array<T, N>& v) noexcept : std::array<T, N>(v) {}
48+
49+
/// Default constructor
50+
Vector() = default;
51+
52+
/// Use std::array's assignment operators
53+
using std::array<T, N>::operator=;
5054

5155
/// inner (dot) product of #Vector's
5256
virtual constexpr T operator*(const Vector<T, N>& B) const
5357
{ return std::inner_product(this->begin(), this->end(), B.begin(), T(0)); }
5458

55-
/// using assignment with rhs = brace-enclosed list
56-
using std::array<T, N>::operator=;
57-
5859
/// unary minus
5960
constexpr virtual Vector<T, N> operator-() const
6061
{ return T(-1) * *(this); }
@@ -73,6 +74,10 @@ std::string to_string(const Vector<T, N>& V)
7374
return s;
7475
}
7576

77+
template <typename T, size_t N>
78+
std::ostream& operator<<(std::ostream& os, const Vector<T, N>& V)
79+
{ os << to_string(V); return os; }
80+
7681
/// addition assignment
7782
template <typename T, size_t N>
7883
Vector<T, N>& operator+=(Vector<T, N>& A, const Vector<T, N>& B)

programs/D3piTest.cxx

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,42 @@ int main( int argc, char** argv)
2424
yap::ParticleFactory factory((::getenv("YAPDIR") ? (std::string)::getenv("YAPDIR") : ".") + "/evt.pdl");
2525

2626
// initial state particle
27-
auto D = factory.createInitialStateParticle(factory.pdgCode("D+"), radialSize);
27+
std::shared_ptr<yap::InitialStateParticle> D = factory.createInitialStateParticle(factory.pdgCode("D+"), radialSize);
2828

2929
// final state particles
30-
auto piPlus = factory.createFinalStateParticle(211);
31-
auto piMinus = factory.createFinalStateParticle(-211);
30+
std::shared_ptr<yap::FinalStateParticle> piPlus = factory.createFinalStateParticle(211);
31+
std::shared_ptr<yap::FinalStateParticle> piMinus = factory.createFinalStateParticle(-211);
3232

3333
// set final state
3434
D->setFinalStateParticles({piPlus, piMinus, piPlus});
3535

3636
// rho
37-
auto rho = std::make_shared<yap::Resonance>(factory.quantumNumbers("rho0"), 0.775, "rho", radialSize, std::make_unique<yap::BreitWigner>());
37+
std::shared_ptr<yap::Resonance> rho = std::make_shared<yap::Resonance>(factory.quantumNumbers("rho0"), 0.775, "rho", radialSize, std::make_unique<yap::BreitWigner>());
3838
static_cast<yap::BreitWigner&>(rho->massShape()).width()->setValue(0.149);
3939
rho->addChannels(piPlus, piMinus, max2L);
4040

4141
// f_2(1270)
42-
auto f_2 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_2"), 1.275, "f_2", radialSize, std::make_unique<yap::BreitWigner>());
42+
std::shared_ptr<yap::Resonance> f_2 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_2"), 1.275, "f_2", radialSize, std::make_unique<yap::BreitWigner>());
4343
static_cast<yap::BreitWigner&>(f_2->massShape()).width()->setValue(0.185);
4444
f_2->addChannels(piPlus, piMinus, max2L);
4545

4646
// f_0(980)
47-
auto f_0_980 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 0.980, "f_0_980", radialSize, std::make_unique<yap::BreitWigner>());
47+
std::shared_ptr<yap::Resonance> f_0_980 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 0.980, "f_0_980", radialSize, std::make_unique<yap::BreitWigner>());
4848
static_cast<yap::BreitWigner&>(f_0_980->massShape()).width()->setValue(0.329);
4949
f_0_980->addChannels(piPlus, piMinus, max2L);
5050

5151
// f_0(1370)
52-
auto f_0_1370 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 1.350, "f_0_1370", radialSize, std::make_unique<yap::BreitWigner>());
52+
std::shared_ptr<yap::Resonance> f_0_1370 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 1.350, "f_0_1370", radialSize, std::make_unique<yap::BreitWigner>());
5353
static_cast<yap::BreitWigner&>(f_0_1370->massShape()).width()->setValue(0.250);
5454
f_0_1370->addChannels(piPlus, piMinus, max2L);
5555

5656
// f_0(1500)
57-
auto f_0_1500 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 1.507, "f_0_1500", radialSize, std::make_unique<yap::BreitWigner>());
57+
std::shared_ptr<yap::Resonance> f_0_1500 = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 1.507, "f_0_1500", radialSize, std::make_unique<yap::BreitWigner>());
5858
static_cast<yap::BreitWigner&>(f_0_1500->massShape()).width()->setValue(0.109);
5959
f_0_1500->addChannels(piPlus, piMinus, max2L);
6060

6161
// sigma a.k.a. f_0(500)
62-
auto sigma = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 0.800, "sigma", radialSize, std::make_unique<yap::BreitWigner>());
62+
std::shared_ptr<yap::Resonance> sigma = std::make_shared<yap::Resonance>(factory.quantumNumbers("f_0"), 0.800, "sigma", radialSize, std::make_unique<yap::BreitWigner>());
6363
static_cast<yap::BreitWigner&>(sigma->massShape()).width()->setValue(0.800);
6464
sigma->addChannels(piPlus, piMinus, max2L);
6565

programs/D4piTest.cxx

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <assert.h>
1919
#include <iostream>
20+
#include <memory>
2021
#include <string>
2122

2223
//#include <callgrind.h>
@@ -35,23 +36,23 @@ int main( int argc, char** argv)
3536

3637
// initial state particle
3738
double radialSize = 1.;
38-
auto D = factory.createInitialStateParticle(421, radialSize);
39+
std::shared_ptr<yap::InitialStateParticle> D = factory.createInitialStateParticle(421, radialSize);
3940

4041
// final state particles
41-
auto piPlus = factory.createFinalStateParticle(211);
42-
auto piMinus = factory.createFinalStateParticle(-211);
42+
std::shared_ptr<yap::FinalStateParticle> piPlus = factory.createFinalStateParticle(211);
43+
std::shared_ptr<yap::FinalStateParticle> piMinus = factory.createFinalStateParticle(-211);
4344

4445
// Set final-state particles
4546
D->setFinalStateParticles({piPlus, piMinus, piPlus, piMinus});
4647

4748
// rho rho
48-
auto rho = factory.createResonance(113, radialSize, std::make_unique<yap::BreitWigner>());
49+
std::shared_ptr<yap::Resonance> rho = factory.createResonance(113, radialSize, std::make_unique<yap::BreitWigner>());
4950
rho->addChannels(piPlus, piMinus, max2L);
5051

5152
D->addChannels(rho, rho, max2L);
5253

5354
// omega omega
54-
auto omega = factory.createResonance(223, radialSize, std::make_unique<yap::BreitWigner>());
55+
std::shared_ptr<yap::Resonance> omega = factory.createResonance(223, radialSize, std::make_unique<yap::BreitWigner>());
5556
omega->addChannels(piPlus, piMinus, max2L);
5657

5758
D->addChannels(omega, omega, max2L);
@@ -60,10 +61,10 @@ int main( int argc, char** argv)
6061
D->addChannels(rho, omega, max2L);
6162

6263
// a_1 channels
63-
auto sigma = factory.createResonance(9000221, radialSize, std::make_unique<yap::BreitWigner>());
64+
std::shared_ptr<yap::Resonance> sigma = factory.createResonance(9000221, radialSize, std::make_unique<yap::BreitWigner>());
6465
sigma->addChannels(piPlus, piMinus, max2L);
6566

66-
auto a_1 = factory.createResonance(20213, radialSize, std::make_unique<yap::BreitWigner>());
67+
std::shared_ptr<yap::Resonance> a_1 = factory.createResonance(20213, radialSize, std::make_unique<yap::BreitWigner>());
6768
a_1->addChannels(sigma, piPlus, max2L);
6869

6970
a_1->addChannels(rho, piPlus, max2L);

programs/DKKpiTest.cxx

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ int main( int argc, char** argv)
2424
yap::ParticleFactory factory((::getenv("YAPDIR") ? (std::string)::getenv("YAPDIR") : ".") + "/evt.pdl");
2525

2626
// create final state particles
27-
auto kPlus = factory.createFinalStateParticle(+321);
28-
auto kMinus = factory.createFinalStateParticle(-321);
29-
auto piPlus = factory.createFinalStateParticle(+211);
27+
std::shared_ptr<yap::FinalStateParticle> kPlus = factory.createFinalStateParticle(+321);
28+
std::shared_ptr<yap::FinalStateParticle> kMinus = factory.createFinalStateParticle(-321);
29+
std::shared_ptr<yap::FinalStateParticle> piPlus = factory.createFinalStateParticle(+211);
3030

3131
// create initial state particle and set final state
32-
auto D = factory.createInitialStateParticle(factory.pdgCode("D+"), radialSize);
32+
std::shared_ptr<yap::InitialStateParticle> D = factory.createInitialStateParticle(factory.pdgCode("D+"), radialSize);
3333
D->setFinalStateParticles({kPlus, kMinus, piPlus});
3434

3535
// create a phi

src/ParticleFactory.cxx

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "FinalStateParticle.h"
55
#include "InitialStateParticle.h"
66
#include "logging.h"
7+
#include "make_unique.h"
78
#include "Resonance.h"
89
#include "SpinAmplitude.h"
910

@@ -42,32 +43,32 @@ ParticleFactory::ParticleFactory(const std::string pdlFile)
4243
}
4344

4445
//-------------------------
45-
std::shared_ptr<FinalStateParticle> ParticleFactory::createFinalStateParticle(int PDG)
46+
std::unique_ptr<FinalStateParticle> ParticleFactory::createFinalStateParticle(int PDG)
4647
{
47-
const ParticleTableEntry& p = particleTableEntry(PDG);
48+
const auto& p = particleTableEntry(PDG);
4849
DEBUG("make FinalStateParticle " << p.Name_ << " with quantum numbers " << p);
49-
return std::make_shared<FinalStateParticle>(p, p.Mass_, p.Name_);
50+
return std::make_unique<FinalStateParticle>(p, p.Mass_, p.Name_);
5051
}
5152

5253
//-------------------------
53-
std::shared_ptr<InitialStateParticle> ParticleFactory::createInitialStateParticle(int PDG, double radialSize)
54+
std::unique_ptr<InitialStateParticle> ParticleFactory::createInitialStateParticle(int PDG, double radialSize)
5455
{
55-
const ParticleTableEntry& p = particleTableEntry(PDG);
56+
const auto& p = particleTableEntry(PDG);
5657

5758
if (p.twoJ() != 0)
5859
LOG(ERROR) << "InitialStateParticle has spin != 0. ";
5960

6061
DEBUG("make InitialStateParticle " << p.Name_ << " with quantum numbers " << p);
61-
return std::make_shared<InitialStateParticle>(p, p.Mass_, p.Name_, radialSize);
62+
return std::make_unique<InitialStateParticle>(p, p.Mass_, p.Name_, radialSize);
6263
}
6364

6465
//-------------------------
65-
std::shared_ptr<Resonance> ParticleFactory::createResonance(int PDG, double radialSize, std::unique_ptr<MassShape>&& massShape)
66+
std::unique_ptr<Resonance> ParticleFactory::createResonance(int PDG, double radialSize, std::unique_ptr<MassShape>&& massShape)
6667
{
67-
const ParticleTableEntry& p = particleTableEntry(PDG);
68+
const auto& p = particleTableEntry(PDG);
6869
DEBUG("make Resonance " << p.Name_ << " with quantum numbers " << p);
6970
massShape->setParameters(p);
70-
return std::make_shared<Resonance>(p, p.Mass_, p.Name_, radialSize, std::move(massShape));
71+
return std::make_unique<Resonance>(p, p.Mass_, p.Name_, radialSize, std::move(massShape));
7172
}
7273

7374
//-------------------------

test/test_Vector.cxx

+57
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <Constants.h>
44
#include <FourVector.h>
5+
#include <ThreeVector.h>
56
#include <logging.h>
67

78
#include <cmath>
@@ -12,6 +13,62 @@ TEST_CASE( "Vector" )
1213
// disable logs in text
1314
yap::disableLogs(el::Level::Global);
1415

16+
SECTION( "ThreeVector" ) {
17+
18+
auto v1 = yap::ThreeVector<double>({1, 2, 3});
19+
auto v2 = yap::ThreeVector<double>({4, 5, 6});
20+
21+
SECTION( "addition-assignment" ) {
22+
v1 += v2;
23+
REQUIRE( v1 == yap::ThreeVector<double>({5, 7, 9}) );
24+
}
25+
26+
SECTION( "subtraction-assignment" ) {
27+
v1 -= v2;
28+
REQUIRE( v1 == yap::ThreeVector<double>({ -3, -3, -3}) );
29+
}
30+
31+
SECTION( "multiplication-assignment" ) {
32+
v1 *= 3.;
33+
REQUIRE( v1 == yap::ThreeVector<double>({3, 6, 9}) );
34+
}
35+
36+
SECTION( "arithmetic operations" ) {
37+
38+
// +
39+
REQUIRE( v1 + v2 == yap::ThreeVector<double>({5, 7, 9}) );
40+
41+
// -
42+
REQUIRE( v1 - v2 == yap::ThreeVector<double>({ -3, -3, -3}) );
43+
44+
// inner product
45+
REQUIRE( v1 * v2 == 32 );
46+
47+
// norm
48+
REQUIRE( norm(v1) == 14 );
49+
REQUIRE( norm(v2) == 77 );
50+
51+
// abs
52+
REQUIRE( abs(v1) == sqrt(14) );
53+
REQUIRE( abs(v2) == sqrt(77) );
54+
55+
// cross product
56+
auto x = yap::ThreeVector<double>({1, 0, 0});
57+
auto y = yap::ThreeVector<double>({0, 1, 0});
58+
auto z = yap::ThreeVector<double>({0, 0, 1});
59+
REQUIRE( cross(x, y) == z );
60+
61+
}
62+
63+
SECTION( "constants" ) {
64+
65+
// axes:
66+
REQUIRE( cross(yap::ThreeAxis_X, yap::ThreeAxis_Y) == yap::ThreeAxis_Z );
67+
REQUIRE( isRightHanded(yap::ThreeAxes) );
68+
}
69+
70+
}
71+
1572
SECTION( "FourVector" ) {
1673

1774
auto v1 = yap::FourVector<double>({4, 3, 2, 1});

0 commit comments

Comments
 (0)