Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyTrajectory.h
Go to the documentation of this file.
1 #ifndef _MYTRAJECTORY_H_
2 #define _MYTRAJECTORY_H_
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <CLHEP/Vector/LorentzVector.h>
7 #include "TRACKING/DMCTrackHit.h"
8 #include "DLine.h"
9 
10 #define DIST_BIG 10000.0
11 #define MAX_ITERATIONS 100000
12 
13 using namespace std;
14 
15 class MyTrajectory {
16  public:
17  MyTrajectory(int level = 1);
18  virtual ~MyTrajectory();
19  void clear();
20  virtual void swim(HepVector startingPoint, double theta, double phi);
21  virtual void swim(const HepVector &startingVector);
22  void swim(const vector<double> &startingStdVector);
23  void swimMC(vector<const DMCTrackHit*> &mctrackhits);
24  void print();
25  vector<HepLorentzVector*>* getTrajectory();
26  template<class C> double doca(C& spaceObject, HepLorentzVector &poca) const;
27  void checkClear();
28  virtual unsigned int getNumberOfParams();
29  double dist(HepVector& point, int trajIndex) const;
30  double dist(DLine& line, int trajIndex) const;
31  void para_min(double yMinus, double yZero, double yPlus, double &xMinFrac,
32  double &yMin) const;
33  virtual vector<double> getDelta() {
34  return delta;
35  }
36  int getXYT(double z, double &x, double &y, double &ct) const;
37  void dump_ascii(ostream *trajFile, int tag);
38 
39  protected:
40  vector<HepLorentzVector*> traj;
41 
42  private:
43  unsigned int nparams; // number of parameters for trajectory
44  vector<double> delta;
46 };
47 
48 template<class C> double MyTrajectory::doca(C& spaceObject, HepLorentzVector &poca) const {
49  unsigned int ilo, ihi, imid;
50  ilo = 0;
51  ihi = traj.size() - 1;
52  double distlo, disthi, distmid, distdown, distup;
53  distlo = dist(spaceObject, ilo);
54  disthi = dist(spaceObject, ihi);
55  if (distlo > DIST_BIG || disthi > DIST_BIG) {
56  cout << "MyTrajectory::doca: end point of trajectory too far away " << distlo << " " << disthi << endl;
57  int ierror = 3;
58  throw ierror;
59  }
60  if (distlo < disthi) {
61  imid = ilo + 1;
62  } else {
63  imid = ihi - 1;
64  }
65  distmid = dist(spaceObject, imid);
66  if (debug_level >= 4) cout << setprecision(14) << "MyTrajectory::doca: initialize " << ilo << " " << imid << " " << ihi << " " << distlo << " " << distmid << " " << disthi << endl;
67  if (isnan(distmid)) {
68  cout << "MyTrajectory::doca: distmid is not a number: " << ilo << " " << imid << " " << ihi << " " << distlo << " " << distmid << " " << disthi << endl;
69  int ierror = 2;
70  throw ierror;
71  }
72  if (distmid >= distlo || distmid >= disthi) {
73  cout << "MyTrajectory::doca: bad initialization of doca search: " << ilo << " " << imid << " " << ihi << " " << distlo << " " << distmid << " " << disthi << endl;
74  int ierror = 1;
75  throw ierror;
76  }
77  double x1, x2, y1, y2, xnew, distnew = 0;
78  unsigned int inew;
79  int iterations = 0;
80  while (imid - ilo > 1 || ihi - imid > 1) {
81  x1 = (double)(imid - ilo);
82  x2 = -(double)(ihi - imid); // unsigned int's cannot be negative
83  y1 = distmid - distlo;
84  y2 = distmid - disthi;
85  xnew = (double)imid
86  - 0.5*(
87  (x1*x1*y2 - x2*x2*y1)
88  /
89  (x1*y2 - x2*y1)
90  );
91  inew = (unsigned int)(xnew + 0.5);
92  if (inew < ilo || inew > ihi) { // jumped out of the bracket
93  cout << "MyTrajectory::doca: jumped out of the miminum bracket: " << ilo << " " << inew << " " << ihi << endl;
94  int ierror = 4;
95  throw ierror;
96  }
97  distnew = dist(spaceObject, inew);
98  if (debug_level >= 4) cout << "MyTrajectory::doca: xnew = " << xnew << " inew = " << inew << " distnew = " << distnew << endl;
99  if (inew == imid) {
100  // Look on either side for minimum bracket
101  distup = dist(spaceObject, imid + 1);
102  distdown = dist(spaceObject, imid - 1);
103  if (distup > distmid && distdown > distmid) {
104  ihi = imid + 1;
105  disthi = distup;
106  ilo = imid - 1;
107  distlo = distdown;
108  } else if (distup > distmid) { // higher on the up side
109  if (ihi - imid > 1) { // room on the up side
110  ihi = imid + 1;
111  disthi = dist(spaceObject, ihi);
112  } else { // no room on the up side
113  imid--;
114  distmid = dist(spaceObject, imid);
115  }
116  } else { // higher on the down side
117  if (imid - ilo > 1) { // room on the down side
118  ilo = imid - 1;
119  distlo = dist(spaceObject, ilo);
120  } else { // no room on the down side
121  imid++;
122  distmid = dist(spaceObject, imid);
123  }
124  }
125  } else {
126 
127  if (distnew < distmid) { // new point is the new lowest point
128  if (inew < imid) { // if new index less than old mid point index
129  ihi = imid; // new high point is the old mid point
130  disthi = distmid;
131  } else { // new index is greater than the old mid point index
132  ilo = imid; // new low point is the old mid point
133  distlo = distmid;
134  }
135  imid = inew; // new point is the new mid point
136  distmid = distnew;
137  } else { // old mid-point is still the lowest point
138  if (inew < imid) { // if new index less than old mid point index
139  ilo = inew; // new low point is the new point
140  distlo = distnew;
141  } else { // new index is greater than the old mid point index
142  ihi = inew; // new high point is the new point
143  disthi = distnew;
144  }
145  }
146 
147  }
148  if (debug_level >= 4) cout << "MyTrajectory::doca, after housekeeping: " << ilo << " " << imid << " " << ihi << " " << distlo << " " << distmid << " " << disthi << endl;
149  iterations++;
150  if (iterations > MAX_ITERATIONS) {
151  cout << "MyTrajectory::doca: too many iterations " << ilo << " " << imid << " " << ihi << " " << distlo << " " << distmid << " " << disthi << endl;
152  int ierror = 4;
153  throw ierror;
154  }
155  }
156  double dlon, dmidn, dhin, dinterp;
157  dlon = dist(spaceObject, ilo);
158  dmidn = dist(spaceObject, imid);
159  dhin = dist(spaceObject, ihi);
160  double xMinFrac, yMin;
161  para_min(dlon*dlon, dmidn*dmidn, dhin*dhin, xMinFrac, yMin);
162  dinterp = sqrt(yMin);
163  if (xMinFrac > 0.0) {
164  poca = xMinFrac*(*traj[ihi]) + (1.0 - xMinFrac)*(*traj[imid]);
165  } else {
166  poca = -xMinFrac*(*traj[ilo]) + (1.0 + xMinFrac)*(*traj[imid]);
167  }
168  if (debug_level >= 4) cout << "doca final calc: ilo,imid,ihi = " << ilo << ',' << imid << ',' << ihi << " dists = " << dlon << "," << dmidn << "," << dhin << " dinterp = " << dinterp << endl;
169  return dinterp;
170 
171 }
172 
173 #endif // _MYTRAJECTORY_H_
vector< double > delta
Definition: MyTrajectory.h:44
#define MAX_ITERATIONS
Definition: MyTrajectory.h:11
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define y
double doca(C &spaceObject, HepLorentzVector &poca) const
Definition: MyTrajectory.h:48
vector< HepLorentzVector * > traj
Definition: MyTrajectory.h:40
Definition: DLine.h:8
virtual vector< double > getDelta()
Definition: MyTrajectory.h:33
double sqrt(double)
#define DIST_BIG
Definition: MyTrajectory.h:10
unsigned int nparams
Definition: MyTrajectory.h:43