Track.cxx
Go to the documentation of this file.
1 //
2 // Track.cpp
3 // garsoft-mrb
4 //
5 // Created by Brian Rebel on 10/6/16.
6 // Copyright 2016 Brian Rebel. All rights reserved. Unless he was working
7 // for FRA in 2016 :) !
8 // Modifications and additions by Tom Junk, 2018; Leo Bellantoni, 2019; etc.
9 //
10 
13 #include "nug4/MagneticFieldServices/MagneticFieldService.h"
14 #include "CoreUtils/ServiceUtil.h"
15 
16 #include "TMath.h"
17 
18 namespace gar {
19  namespace rec {
20 
21  //--------------------------------------------------------------------------
23  {
24  // The default constructor is used e.g. by art::DataViewImpl::getHandle
25  // Make sure all Track objects are numbered, lest art deep-copy uninitialized
26  // Track instances and then operator==() evaluates meaninglessly true.
29  return;
30  }
31 
32 
33 
34  bool Track::operator==(const Track& rhs) const {
35  return (this->fIDnumero == rhs.fIDnumero);
36  }
37 
38  bool Track::operator!=(const Track& rhs) const {
39  return (this->fIDnumero != rhs.fIDnumero);
40  }
41 
43 
44 
45 
46  //--------------------------------------------------------------------------
47  // Track constructor with no errors -- to be called by the Track Cheater
48 
49  Track::Track(const float length,
50  const float momentum_beg,
51  const float momentum_end,
52  const float *vtx,
53  const float *end,
54  const float *vtxDir,
55  const float *endDir,
56  const size_t nhits,
57  const int , //charge,
58  const double time)
59  : fLengthforwards (length )
60  , fLengthbackwards (length )
61  , fMomentum_beg(momentum_beg)
62  , fMomentum_end(momentum_end)
63  , fChisqForward(0)
64  , fChisqBackward(0)
65  , fNHits(nhits)
66  , fTime(time)
67  {
70 
71  auto const* magFieldService = gar::providerFrom<mag::MagneticFieldService>();
72  G4ThreeVector zerovec(0,0,0);
73  G4ThreeVector magfield = magFieldService->FieldAtPoint(zerovec);
74 
75  fVertex[0] = vtx[0];
76  fVertex[1] = vtx[1];
77  fVertex[2] = vtx[2];
78 
79  fVtxDir[0] = vtxDir[0];
80  fVtxDir[1] = vtxDir[1];
81  fVtxDir[2] = vtxDir[2];
82 
83  fEnd[0] = end[0];
84  fEnd[1] = end[1];
85  fEnd[2] = end[2];
86 
87  fEndDir[0] = endDir[0];
88  fEndDir[1] = endDir[1];
89  fEndDir[2] = endDir[2];
90 
91  for (size_t i=0; i<15; ++i)
92  {
93  fCovMatBeg[i] = 0;
94  fCovMatEnd[i] = 0;
95  }
96 
97  // calculate track parameters from information we have
98 
99  fTrackParBeg[0] = vtx[1];
100  fTrackParBeg[1] = vtx[2];
101  float Pt_beg = momentum_beg * TMath::Sqrt(std::max(1E-6,1-TMath::Sq(vtxDir[0])));
102  if (momentum_beg > 0)
103  {
104  fTrackParBeg[2] = (1.0E-2)*0.3*magfield[0]/Pt_beg;
105  }
106  else
107  {
108  fTrackParBeg[2] = 0;
109  }
110  fTrackParBeg[3] = TMath::ATan2(vtxDir[1],vtxDir[2]);
111 
112  if (vtxDir[1] != 0 || vtxDir[2] != 0)
113  {
114  fTrackParBeg[4] = TMath::ATan(vtxDir[0]/TMath::Sqrt(vtxDir[1]*vtxDir[1] + vtxDir[2]*vtxDir[2]));
115  }
116  else
117  {
118  fTrackParBeg[4] = -TMath::Pi()/2.0;
119  }
120 
121  fTrackParEnd[0] = end[1];
122  fTrackParEnd[1] = end[2];
123  float Pt_end = momentum_end * TMath::Sqrt(std::max(1E-6,1-TMath::Sq(endDir[0])));
124  if (momentum_end > 0)
125  {
126  fTrackParEnd[2] = (1.0E-2)*0.3*magfield[0]/Pt_end;
127  }
128  else
129  {
130  fTrackParEnd[2] = 0;
131  }
132  fTrackParEnd[3] = TMath::ATan2(endDir[1],endDir[2]);
133 
134  if (endDir[1] != 0 || endDir[2] != 0)
135  {
136  fTrackParEnd[4] = TMath::ATan(endDir[0]/TMath::Sqrt(endDir[1]*endDir[1] + endDir[2]*endDir[2]));
137  }
138  else
139  {
140  fTrackParEnd[4] = -TMath::Pi()/2.0;
141  }
142 
143  return;
144  }
145 
146 
147  Track::Track(const float lengthforwards,
148  const float lengthbackwards,
149  const size_t nhits,
150  const float xbeg, // x location at beginning of track in cm
151  const float *trackparbeg, // y, z, curvature, phi, slope -- 5-parameter track (cm, cm, cm-1, radians, dy,z/dx)
152  const float *covmatbeg, // covariance matrix at beginning of track -- symmetric 5x5
153  const float chisqforward, // chisquared of forwards fit
154  const float xend, // x location at end of track
155  const float *trackparend, // y, z, curvature, phi, slope -- 5-parameter track (cm, cm, cm-1, radians, dy,z/dx)
156  const float *covmatend, // covariance matrix at beginning of track -- symmetric 5x5
157  const float chisqbackward, // chisquared of forwards fit
158  const double time) // timestamp
159  : fLengthforwards(lengthforwards)
160  , fLengthbackwards(lengthbackwards)
161  , fChisqForward(chisqforward)
162  , fChisqBackward(chisqbackward)
163  , fNHits(nhits)
164  , fTime(time)
165  {
168 
169  auto const* magFieldService = gar::providerFrom<mag::MagneticFieldService>();
170  G4ThreeVector zerovec(0,0,0);
171  G4ThreeVector magfield = magFieldService->FieldAtPoint(zerovec);
172 
173  size_t icov = 0;
174  for (size_t i=0; i< 5; ++i)
175  {
176  fTrackParBeg[i] = trackparbeg[i];
177  fTrackParEnd[i] = trackparend[i];
178  for (size_t j=i; j<5; ++j)
179  {
180  fCovMatBeg[icov] = covmatbeg[i+5*j];
181  fCovMatEnd[icov] = covmatend[i+5*j];
182  ++icov;
183  }
184  }
185 
186  fVertex[0] = xbeg;
187  fVertex[1] = trackparbeg[0];
188  fVertex[2] = trackparbeg[1];
189 
190  FindDirectionFromTrackParameters(trackparbeg, xbeg,xend, fVtxDir);
191 
192  if (trackparbeg[2] != 0)
193  {
194  float poverpt = 1.0/TMath::Sqrt(std::max(1E-6,1.0-TMath::Sq(fVtxDir[0])));
195  fMomentum_beg = poverpt * TMath::Abs((1.0E-2)*0.3*magfield[0]/trackparbeg[2]); // check constant (kG or T?)
196  }
197  else
198  {
199  fMomentum_beg = 0;
200  }
201 
202  fEnd[0] = xend;
203  fEnd[1] = trackparend[0];
204  fEnd[2] = trackparend[1];
205 
206  FindDirectionFromTrackParameters(trackparend, xend,xbeg, fEndDir);
207 
208  if (trackparend[2] != 0)
209  {
210  float poverpt = 1.0/TMath::Sqrt(std::max(1E-6,1.0-TMath::Sq(fEndDir[0])));
211  fMomentum_end = poverpt * TMath::Abs((1.0E-2)*0.3*magfield[0]/trackparend[2]); // check constant (kG or T?)
212  }
213  else
214  {
215  fMomentum_end = 0;
216  }
217 
218  }
219 
220 
221 
222  // Charge is d(phi)/d(time in flight). We don't know if it went from fVertex
223  // to fEnd or the otherway round; but under either assumption we can find
224  // if phi is increasing or decreasing. All we need is the x component of
225  // the cross product of the direction with the lever arm (position of track
226  // end in the (z,y) plane relative to (Zc, Yc).
227  // N.b. this has no concept of charge +-2 or other values than abs(charge)=1
228 
229  int Track::ChargeBeg() const {
230  float Zlever = +TMath::Sin(fTrackParBeg[3]) / fTrackParBeg[2];
231  float Ylever = -TMath::Cos(fTrackParBeg[3]) / fTrackParBeg[2];
232  float Xcross = Ylever * fVtxDir[2] - Zlever * fVtxDir[1];
233  return ( Xcross>0 ? -1 : +1 );
234  }
235 
236  int Track::ChargeEnd() const {
237  float Zlever = +TMath::Sin(fTrackParEnd[3]) / fTrackParEnd[2];
238  float Ylever = -TMath::Cos(fTrackParEnd[3]) / fTrackParEnd[2];
239  float Xcross = Ylever * fEndDir[2] - Zlever * fEndDir[1];
240  return ( Xcross>0 ? -1 : +1 );
241  }
242 
243 
244 
245  // recover symmetric covariance matrices. Assume the caller owns the memory to a 5x5 array
246  void Track::CovMatBegSymmetric(float *cmb) const {
247  size_t ic=0;
248  for (size_t i=0; i<5; ++i) {
249  for (size_t j=i; j<5; ++j) {
250  cmb[i + 5*j] = fCovMatBeg[ic];
251  cmb[j + 5*i] = fCovMatBeg[ic];
252  ++ic;
253  }
254  }
255  }
256 
257  void Track::CovMatEndSymmetric(float *cmb) const {
258  size_t ic=0;
259  for (size_t i=0; i<5; ++i) {
260  for (size_t j=i; j<5; ++j) {
261  cmb[i + 5*j] = fCovMatEnd[ic];
262  cmb[j + 5*i] = fCovMatEnd[ic];
263  ++ic;
264  }
265  }
266  }
267 
268 
269  void FindDirectionFromTrackParameters(const float *tparms,
270  const float thisXend, const float farXend, float *dir) {
271  // Direction is either +1 or -1 times d (position) / d(phi)
272  // from the track fit equations; the sign is determined by the
273  // x position of the far end.
274  dir[0] = TMath::Tan(tparms[4]);
275  dir[1] = TMath::Sin(tparms[3]);
276  dir[2] = TMath::Cos(tparms[3]);
277 
278  int sigh = +1;
279  if ( dir[0]>0 && farXend<thisXend ) sigh = -1;
280  if ( dir[0]<0 && farXend>thisXend ) sigh = -1;
281  float norm = sigh * TMath::Sqrt( 1.0 + dir[0]*dir[0]);
282 
283  dir[0] /= norm;
284  dir[1] /= norm;
285  dir[2] /= norm;
286  }
287 
288 
289 
290  } // rec namespace
291 } // gar namespace
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
float fChisqBackward
chisquared backward fit
Definition: Track.h:59
bool operator!=(const Track &rhs) const
Definition: Track.cxx:38
rec
Definition: tracks.py:88
float fChisqForward
chisquared forward fit
Definition: Track.h:58
float fCovMatBeg[15]
covariance matrix at beginning of track – packed in a 1D array, assuming symmetry ...
Definition: Track.h:68
float fMomentum_beg
momentum of the track at the vertex in GeV/c
Definition: Track.h:52
float fCovMatEnd[15]
covariance matrix at end of track
Definition: Track.h:69
double fTime
time in ns from trigger
Definition: Track.h:61
float fTrackParBeg[5]
Track parameters at beginning of track y, z, curvature, phi, lambda – 5-parameter track (cm...
Definition: Track.h:65
float fLengthforwards
length of the track in cm from forwards fit
Definition: Track.h:50
string dir
static gar::rec::IDNumber const FirstNumber
Definition: Track.h:47
float fLengthbackwards
length of the track in cm from backwards fit
Definition: Track.h:51
size_t fNHits
number of hits
Definition: Track.h:60
static IDNumberGen * create(IDNumber iniValue=std::numeric_limits< IDNumber >::max())
Definition: IDNumberGen.cxx:18
float fMomentum_end
momentum of the track at the end in GeV/c
Definition: Track.h:53
int ChargeEnd() const
Definition: Track.cxx:236
int ChargeBeg() const
Definition: Track.cxx:229
float fVertex[3]
track vertex position in cm – == "beginning" of track. Arbitrary choice made by patrec ...
Definition: Track.h:54
static int max(int a, int b)
bool operator==(const Track &rhs) const
Definition: Track.cxx:34
auto norm(Vector const &v)
Return norm of the specified vector.
float fEndDir[3]
track end direction
Definition: Track.h:57
float fTrackParEnd[5]
Track parameters at end of track y, z, curvature, phi, lambda – 5-parameter track (cm...
Definition: Track.h:66
General GArSoft Utilities.
float fEnd[3]
track end position in cm
Definition: Track.h:55
float fVtxDir[3]
track vertex direction
Definition: Track.h:56
void FindDirectionFromTrackParameters(const float *tparms, const float thisXend, const float farXend, float *dir)
Definition: Track.cxx:269
gar::rec::IDNumber fIDnumero
Definition: Track.h:48
gar::rec::IDNumber getIDNumber() const
Definition: Track.cxx:42
void CovMatBegSymmetric(float *cmb) const
Definition: Track.cxx:246
void CovMatEndSymmetric(float *cme) const
Definition: Track.cxx:257
size_t IDNumber
Definition: IDNumberGen.h:71