RooParticle
Particle scattering simulator with ROOT
Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1#include "TROOT.h"
2#include "TVector3.h"
3
4#include "path.hpp"
5#include "list"
6#include "iostream"
7
8#include <math.h>
9
10using namespace std;
11
14
15path::path(const path& path_){
16 std::vector<TVector3>::const_iterator i1;
17 std::vector<Double_t>::const_iterator i2;
18
19 for(i1=path_.pos.begin();i1!=path_.pos.end();++i1){
20 pos.push_back(*i1);
21 }
22
23 for(i1=path_.vel.begin();i1!=path_.vel.end();++i1){
24 vel.push_back(*i1);
25 }
26
27 for(i2=path_.t.begin();i2!=path_.t.end();++i2){
28 t.push_back(*i2);
29 }
30}
31
33 pos.clear();
34 vel.clear();
35 t.clear();
36
37 // pos.shrink_to_fit();
38 // vel.shrink_to_fit();
39 // t.shrink_to_fit();
40}
41
42void path::AddRecord(TVector3 x_, TVector3 v_, Double_t time_=-1.){
43 pos.push_back(x_);
44 vel.push_back(v_);
45 t.push_back(time_);
46}
47
48TVector3 path::GetRecordX(Int_t i){
49 // std::cout<<"CALL RECORDX ("<<i<<")"<<std::endl;
50 if (pos.size()==0 || pos.size()<=i){
51 std::cerr<<"Path::GetRecordX : NO ANY VALUES, RETURN NULL VECTOR"<<std::endl;
52 return TVector3();
53 }
54 return pos[i];
55}
56
57TVector3 path::GetRecordV(Int_t i){
58 if (vel.size()==0 || vel.size()<=i){
59 std::cerr<<"Path::GetRecordV : NO ANY VALUES, RETURN NULL VECTOR"<<std::endl;
60 return TVector3();
61 }
62 return vel[i];
63}
64
65Double_t path::GetRecordT(Int_t i){
66 if (t.size()==0 || t.size()<=i){
67 std::cerr<<"Path::GetRecordT : NO ANY VALUES, RETURN NULL VALUE"<<std::endl;
68 return Double_t(0.);
69 }
70 return t[i];
71}
72
73void path::GetRecord(Int_t i,
74 TVector3 & x_, TVector3 & v_,
75 Double_t & t_){
76 x_ = GetRecordX(i);
77 v_ = GetRecordV(i);
78 t_ = GetRecordT(i);
79 return;
80}
81
82TVector3 path::GetInitX(){
83 if (pos.size()==0){
84 std::cerr<<"Path::GetInitX : RETURN NULL VECTOR"<<std::endl;
85 return TVector3();
86 }
87 return pos.front();
88}
89
90TVector3 path::GetInitV(){
91 if (vel.size()==0){
92 std::cerr<<"Path::GetInitV : NO ANY VALUES, RETURN NULL VECTOR"<<std::endl;
93 return TVector3();
94 }
95 return vel.front();
96}
97
98Double_t path::GetInitT(){
99 if (t.size()==0){
100 std::cerr<<"Path::GetInitT : NO ANY VALUES, RETURN ZERO VALUE"<<std::endl;
101 return Double_t(0.);
102 }
103 return t.front();
104}
105
106void path::GetInit(TVector3 & x_, TVector3 & v_,
107 Double_t & t_){
108 x_ = GetInitX();
109 v_ = GetInitV();
110 t_ = GetInitT();
111 return;
112}
113
114TVector3 path::GetLastX(){
115 if (pos.size()==0){
116 std::cerr<<"Path::GetLastX : NO ANY VALUES, RETURN NULL VECTOR"<<std::endl;
117 return TVector3();
118 }
119 return pos.back();
120}
121
122TVector3 path::GetLastV(){
123 if (vel.size()==0){
124 std::cerr<<"Path::GetLastV : NO ANY VALUES, RETURN NULL VECTOR"<<std::endl;
125 return TVector3();
126 }
127 return vel.back();
128}
129
130Double_t path::GetLastT(){
131 if (t.size()==0){
132 std::cerr<<"Path::GetLastT : NO ANY VALUES, RETURN ZERO VALUE"<<std::endl;
133 return Double_t(0.);
134 }
135 return t.back();
136}
137
138void path::GetLast(TVector3 & x_, TVector3 & v_,
139 Double_t & t_){
140 x_ = GetLastX();
141 v_ = GetLastV();
142 t_ = GetLastT();
143 return;
144}
145
146// Sentinel written to DCA and outtime when no distance can be computed
147// (empty path, length/time mismatch, null pointer).
148static const Double_t kPathDCAInvalid = -1.;
149
150// Refine a discrete minimum of the squared distance d2[] at index k by a
151// parabola through samples k-1, k, k+1 (t need not be uniform). The recorded
152// steps are coarse, so the true closest approach usually lies between two
153// of them. Keeps the discrete values at the path ends, for non-increasing
154// time, or when the parabola has no interior minimum (degenerate fit).
155static void RefineMinimum(const std::vector<Double_t> & d2,
156 const std::vector<Double_t> & t,
157 Int_t k, Double_t & DCA, Double_t & outtime){
158 DCA = sqrt(d2[k]);
159 outtime = t[k];
160 if(k<=0 || k>=(Int_t)d2.size()-1) return;
161
162 Double_t s0 = t[k-1]-t[k];
163 Double_t s2 = t[k+1]-t[k];
164 if(!(s0<0. && s2>0.)) return;
165
166 Double_t y1 = d2[k];
167 Double_t q0 = (d2[k-1]-y1)/s0;
168 Double_t q2 = (d2[k+1]-y1)/s2;
169 Double_t a = (q0-q2)/(s0-s2);
170 if(!(a>0.)) return;
171 Double_t b = q2-a*s2;
172 Double_t sm = -b/(2.*a);
173 if(sm<s0 || sm>s2) return;
174
175 Double_t dmin2 = y1-b*b/(4.*a);
176 if(dmin2<0.) dmin2 = 0.; // rounding only; distance squared cannot be negative
177 DCA = sqrt(dmin2);
178 outtime = t[k]+sm;
179}
180
181void path::GetDCA(const TVector3 pos_, Double_t & DCA, Double_t & outtime, Bool_t verbose, Bool_t interpolate){
182 DCA = kPathDCAInvalid;
183 outtime = kPathDCAInvalid;
184
185 if(pos.empty() || pos.size()!=t.size()){
186 std::cerr<<"path::GetDCA : EMPTY OR INCONSISTENT PATH, RETURN DCA=-1, TIME=-1"<<std::endl;
187 return;
188 }
189
190 std::vector<Double_t> d2(pos.size());
191 Int_t resultTI=0;
192 for(Int_t i=0; i<(Int_t)pos.size(); i++){
193 // distance to the fixed point (not to the path itself)
194 d2[i] = (pos[i]-pos_).Mag2();
195 if(d2[i]<d2[resultTI]) resultTI = i;
196 }
197
198 if(interpolate){
199 RefineMinimum(d2, t, resultTI, DCA, outtime);
200 }
201 else{
202 DCA = sqrt(d2[resultTI]);
203 outtime = t[resultTI];
204 }
205 if(verbose){
206 std::cerr<<"path::GetDCA : index "<<resultTI<<" DCA "<<DCA<<" time "<<outtime<<std::endl;
207 }
208}
209
210void path::GetDCA(path* timepath, Double_t & DCA, Double_t & outtime, Bool_t verbose, Bool_t interpolate){
211 DCA = kPathDCAInvalid;
212 outtime = kPathDCAInvalid;
213
214 if(timepath==nullptr){
215 std::cerr<<"path::GetDCA : NULL PATH, RETURN DCA=-1, TIME=-1"<<std::endl;
216 return;
217 }
218 if(!timeChk(timepath)){
219 std::cerr<<"path::GetDCA : PATHS DO NOT SHARE TIME GRID, RETURN DCA=-1, TIME=-1"<<std::endl;
220 return;
221 }
222 if(pos.empty() || pos.size()!=t.size() || timepath->pos.size()!=pos.size()){
223 std::cerr<<"path::GetDCA : EMPTY OR INCONSISTENT PATH, RETURN DCA=-1, TIME=-1"<<std::endl;
224 return;
225 }
226
227 std::vector<Double_t> d2(pos.size());
228 Int_t resultTI=0;
229 for(Int_t i=0; i<(Int_t)pos.size(); ++i){
230 d2[i] = (timepath->pos[i]-pos[i]).Mag2();
231 if(d2[i]<d2[resultTI]) resultTI = i;
232 }
233
234 if(interpolate){
235 RefineMinimum(d2, t, resultTI, DCA, outtime);
236 }
237 else{
238 DCA = sqrt(d2[resultTI]);
239 outtime = t[resultTI];
240 }
241 if(verbose){
242 std::cerr<<"path::GetDCA : index "<<resultTI<<" DCA "<<DCA<<" time "<<outtime<<std::endl;
243 }
244}
245
246Bool_t path::lengthChk(path* target){
247 if(GetMaxNumber()!=target->GetMaxNumber()){
248 std::cerr<<"path::lengthChk : LENGTH CHK ERROR (NOT SAME)"<<std::endl;
249 return kFALSE;
250 }
251 else{
252 return kTRUE;
253 }
254}
255
256Bool_t path::timeChk(path* target){
257 if(!lengthChk(target)){
258 return kFALSE;
259 }
260 Int_t i;
261 for(i=0; i<target->GetMaxNumber(); i++){
262 if(target->GetRecordT(i)!=GetRecordT(i)){
263 std::cerr<<"path::timeChk : TIME CHK ERROR: "<< i << std::endl;
264 return false;
265 }
266 }
267 return true;
268}
Time-ordered list of (position, velocity, time) samples of one particle.
Definition path.hpp:37
Bool_t lengthChk(path *target)
True if target has the same number of records; otherwise prints a message on std::cerr and returns fa...
Definition path.cpp:246
void GetInit(TVector3 &x_, TVector3 &v_, Double_t &t_)
First record as position (fm), velocity (c) and time (fm/c).
Definition path.cpp:106
Double_t GetLastT()
Time of the last stored record (fm/c); 0 plus message if empty.
Definition path.cpp:130
TVector3 GetRecordV(Int_t i)
Velocity of record i (c); same failure behaviour as GetRecordX().
Definition path.cpp:57
std::vector< TVector3 > pos
positions (fm)
Definition path.hpp:163
TVector3 GetRecordX(Int_t i)
Position of record i (fm).
Definition path.cpp:48
path()
Create an empty path.
Definition path.cpp:12
std::vector< TVector3 > vel
velocities (c)
Definition path.hpp:164
std::vector< Double_t > t
times (fm/c)
Definition path.hpp:165
Bool_t timeChk(path *target)
True if target has the same length and exactly equal times in every record; prints a message on std::...
Definition path.cpp:256
void GetDCA(const TVector3 pos_, Double_t &DCA, Double_t &outtime, Bool_t verbose=false, Bool_t interpolate=true)
Distance of closest approach of this path to a fixed point.
Definition path.cpp:181
TVector3 GetLastV()
Velocity of the last stored record (c); null vector plus message if empty.
Definition path.cpp:122
void GetRecord(Int_t i, TVector3 &x_, TVector3 &v_, Double_t &t_)
Position, velocity and time of record i in one call.
Definition path.cpp:73
TVector3 GetLastX()
Position of the last stored record (fm); null vector plus message if empty.
Definition path.cpp:114
void GetLast(TVector3 &x_, TVector3 &v_, Double_t &t_)
Last stored record as position (fm), velocity (c) and time (fm/c).
Definition path.cpp:138
Long64_t GetMaxNumber()
Number of stored time stamps (as a 64-bit integer); equals GetRecordSize().
Definition path.hpp:152
TVector3 GetInitX()
Position of the first record (fm); null vector plus message if the path is empty.
Definition path.cpp:82
void AddRecord(TVector3 x_, TVector3 v_, Double_t time_)
Append one record at the end.
Definition path.cpp:42
~path()
Destructor: clears the records.
Definition path.cpp:32
Double_t GetRecordT(Int_t i)
Time of record i (fm/c).
Definition path.cpp:65
TVector3 GetInitV()
Velocity of the first record (c); null vector plus message if the path is empty.
Definition path.cpp:90
Double_t GetInitT()
Time of the first record (fm/c); 0 plus message if the path is empty.
Definition path.cpp:98
static const Double_t kPathDCAInvalid
Definition path.cpp:148
static void RefineMinimum(const std::vector< Double_t > &d2, const std::vector< Double_t > &t, Int_t k, Double_t &DCA, Double_t &outtime)
Definition path.cpp:155
Recorded trajectory of one particle (path) and distance-of-closest-approach helpers.