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) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef BDSFOURVECTOR_H
19#define BDSFOURVECTOR_H
20#include "BDSException.hh"
21
22#include "globals.hh" // geant4 types / globals
23
24#include <ostream>
25#include <vector>
26
33template<typename Type>
35{
36public:
39 {values[0] = 0; values[1] = 0; values[2] = 0; values[3] = 0;}
40
42 BDSFourVector(Type xIn, Type yIn, Type zIn, Type tIn)
43 {values[0] = xIn; values[1] = yIn; values[2] = zIn; values[3] = tIn;}
44
47 {
48 for (const auto& i : indices)
49 {values[i] = other.values[i];}
50 }
51
54 {
55 if (this == &rhs)
56 {return *this;}
57 for (const G4int& i : indices)
58 {values[i] = rhs.values[i];}
59 return *this;
60 }
61
63 Type& operator[](const G4int index)
64 {
65 if (index > (G4int)indices.size()-1)
66 {throw BDSException("BDSFourVector::operator[]> index outside array");}
67 return values[index];
68 }
69
71 const Type& operator[](const G4int index) const
72 {
73 if (index > (G4int)indices.size()-1)
74 {throw BDSException("BDSFourVector::operator[]> index outside array");}
75 return values[index];
76 }
77
79 inline const Type& x() const {return values[0];}
80 inline const Type& y() const {return values[1];}
81 inline const Type& z() const {return values[2];}
82 inline const Type& t() const {return values[3];}
84
86 friend std::ostream& operator<< (std::ostream& out, BDSFourVector const &v)
87 {
88 out << "("
89 << v.values[0] << ", "
90 << v.values[1] << ", "
91 << v.values[2] << ", "
92 << v.values[3]
93 << ")";
94 return out;
95 }
96
97private:
99 Type values[4];
100
101 std::vector<G4int> indices = {0,1,2,3};
102};
103
104#endif
General exception with possible name of object and message.
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.