BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFourVector.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef BDSFOURVECTOR_H
20#define BDSFOURVECTOR_H
21#include "BDSException.hh"
22
23#include "globals.hh" // geant4 types / globals
24
25#include <ostream>
26#include <vector>
27
34template<typename Type>
36{
37public:
40 {values[0] = 0; values[1] = 0; values[2] = 0; values[3] = 0;}
41
43 BDSFourVector(Type xIn, Type yIn, Type zIn, Type tIn)
44 {values[0] = xIn; values[1] = yIn; values[2] = zIn; values[3] = tIn;}
45
48 {
49 for (const auto& i : indices)
50 {values[i] = other.values[i];}
51 }
52
55 {
56 if (this == &rhs)
57 {return *this;}
58 for (const G4int& i : indices)
59 {values[i] = rhs.values[i];}
60 return *this;
61 }
62
64 Type& operator[](const G4int index)
65 {
66 if (index > (G4int)indices.size()-1)
67 {throw BDSException("BDSFourVector::operator[]> index outside array");}
68 return values[index];
69 }
70
72 const Type& operator[](const G4int index) const
73 {
74 if (index > (G4int)indices.size()-1)
75 {throw BDSException("BDSFourVector::operator[]> index outside array");}
76 return values[index];
77 }
78
80 inline const Type& x() const {return values[0];}
81 inline const Type& y() const {return values[1];}
82 inline const Type& z() const {return values[2];}
83 inline const Type& t() const {return values[3];}
85
87 friend std::ostream& operator<< (std::ostream& out, BDSFourVector const &v)
88 {
89 out << "("
90 << v.values[0] << ", "
91 << v.values[1] << ", "
92 << v.values[2] << ", "
93 << v.values[3]
94 << ")";
95 return out;
96 }
97
98private:
100 Type values[4];
101
102 std::vector<G4int> indices = {0,1,2,3};
103};
104
105#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
A simple templated four vector class.
const Type & y() const
Accessor by name.
const Type & z() const
Accessor by name.
BDSFourVector(Type xIn, Type yIn, Type zIn, Type tIn)
Alternate constructor.
friend std::ostream & operator<<(std::ostream &out, BDSFourVector const &v)
Output stream.
Type & operator[](const G4int index)
Access / set a single element.
const Type & x() const
Accessor by name.
const Type & operator[](const G4int index) const
Access a single element.
BDSFourVector()
Default constructor.
BDSFourVector & operator=(const BDSFourVector &rhs)
Assignment operator.
Type values[4]
Member data x,y,z,t.
const Type & t() const
Accessor by name.
BDSFourVector(const BDSFourVector &other)
Copy constructor.