Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
combinedResidFunc.cc
Go to the documentation of this file.
1 #include <CLHEP/Matrix/Matrix.h>
2 #include <CLHEP/Matrix/Vector.h>
3 
4 #define SPEED_OF_LIGHT 29.9792548
5 
6 using namespace std;
7 
9 #include "MyTrajectory.h"
10 #include "residFunc.h"
11 #include "combinedResidFunc.h"
13 
14 const double velDrift = 55.e-4; // cm/ns
15 const double c = 29.9792548; // speed of light, cm/ns
16 
17 // constructor, takes pointer to vector of pseudopoints and a pointer
18 // to a trajectory
19 combinedResidFunc::combinedResidFunc(vector<const DFDCPseudo*> *pseudopoints,
20  vector<const DCDCTrackHit*> *trackHits,
21  MyTrajectory *trajectory,
22  const DLorentzDeflections *lorentz_def_in,
23  int level) :
24  n_fdc(pseudopoints->size()), n_cdc(trackHits->size()), ppPtr(pseudopoints),
25  trkhitPtr(trackHits), trajPtr(trajectory), delta(trajPtr->getDelta()),
26  debug_level(level), lorentz_def(lorentz_def_in), storeDetails(false),
27  innerResidFrac(1.0), rCDC(trackHits, trajectory, level),
28  rFDC(pseudopoints, trajectory, lorentz_def_in, level)
29 {}
30 
31 void combinedResidFunc::resid(const HepVector *x, void *data, HepVector *f){
32 // input parameters
33 // x: pointer to a vector of fit parameters
34 // data: ???
35 // output parameters
36 // f: pointer to vector of residuals
37  if (debug_level > 2) {
38  cout << "combinedResidFunc::resid: resid called\n";
39  cout << " params: " << *x;
40  }
41  // do a swim with the input parameters
42  trajPtr->swim(*x);
43  // populate f vector with residuals
44 
45  double thisChiSquared = 0.0;
46  double thisResid;
47 
48  // get info from residFDC class
49 
50  rFDC.calcResids();
51  vector<double> residsF;
52  rFDC.getResids(residsF);
53  FDCHitDetails *FDCHitDetailsPtr;
54  vector<HepVector> pointF;
55  vector<double> docasF, errorsF;
56  vector<HepLorentzVector> pocasF;
57  rFDC.getDetails(pointF, docasF, errorsF, pocasF);
58  for (unsigned int ir = 0; ir < n_fdc; ir++) {
59  thisResid = residsF[ir];
60  (*f)(ir + 1) = thisResid;
61  if (storeDetails) {
62  thisChiSquared += thisResid*thisResid;
63  FDCHitDetailsPtr = new FDCHitDetails();
64  FDCHitDetailsPtr->doca = docasF[ir];
65  FDCHitDetailsPtr->poca = pocasF[ir];
66  FDCHitDetailsPtr->rCorr = pointF[ir];
67  FDCDetails.push_back(FDCHitDetailsPtr);
68  }
69  }
70 
71  // get info from CDC residual class
72 
74  rCDC.calcResids();
75  vector<double> residsC;
76  rCDC.getResids(residsC);
77  CDCHitDetails *CDCHitDetailsPtr;
78  vector<double> docasC, distsC, errorsC;
79  vector<HepLorentzVector> pocasC;
80  vector<HepVector> posWiresC;
81  rCDC.getDetails(docasC, distsC, errorsC, pocasC, posWiresC);
82  for (unsigned int ir = 0; ir < n_cdc; ir++) {
83  thisResid = residsC[ir];
84  (*f)(n_fdc + ir + 1) = thisResid;
85  if (storeDetails) {
86  thisChiSquared += thisResid*thisResid;
87  CDCHitDetailsPtr = new CDCHitDetails();
88  CDCHitDetailsPtr->doca = docasC[ir];
89  CDCHitDetailsPtr->poca = pocasC[ir];
90  CDCHitDetailsPtr->dist = distsC[ir];
91  CDCHitDetailsPtr->posWire = posWiresC[ir];
92  CDCDetails.push_back(CDCHitDetailsPtr);
93  }
94  }
95 
96  if (debug_level > 2) cout << "combinedResidFunc::resid: resids:" << *f;
97  if (storeDetails) chiSquared = thisChiSquared;
98 
99  // clear the trajectory
100  trajPtr->clear();
101 
102 };
103 
104 void combinedResidFunc::residAndDeriv(const HepVector *x, void *data, HepVector *f,
105  HepMatrix *J){};
106 
108  double x;
109  double y;
110  double ct;
111  double z = ppoint.wire->origin(2);
112  trajPtr->getXYT(z, x, y, ct); // on trajectory
113  double delta_x = 0.0, delta_y = 0.0;
114  getCorrectionValue(ppoint, x, y, z, ct, delta_x, delta_y);
115  bool ispos = getCorrectionSign(ppoint, x, y, delta_x, delta_y);
116  HepVector point(3);
117  if (ispos) {
118  point(1) = ppoint.xy.X() + delta_x;
119  point(2) = ppoint.xy.Y() + delta_y;
120  } else {
121  point(1) = ppoint.xy.X() - delta_x;
122  point(2) = ppoint.xy.Y() - delta_y;
123  }
124  point(3) = z;
125  if (debug_level >= 4) {
126  cout << "combinedResidFunc::pseudo2HepVector, x = " << x << " y = " << y << " z = " << z << " ct = " << ct << " delta_x = " << delta_x << " delta_y = " << delta_y << " ispos = " << ispos << " point = " << point << endl;
127  }
128  return point;
129 }
130 
131 bool combinedResidFunc::getCorrectionSign(const DFDCPseudo &ppoint, double x, double y, double delta_x, double delta_y) {
132  double xWire = ppoint.wire->udir(0);
133  double yWire = ppoint.wire->udir(1);
134  double wireCrossTraj = xWire*(y - ppoint.xy.Y()) - yWire*(x - ppoint.xy.X());
135  double wireCrossDelta = xWire*delta_y - yWire*delta_x;
136  bool isposTraj = wireCrossTraj > 0?true:false;
137  bool isposDelta = wireCrossDelta > 0?true:false;
138  bool ispos = !(isposTraj ^ isposDelta);
139  if (debug_level > 3) cout << setprecision(14)
140  << "combinedResidFunc::getCorrectionSign,"
141  << " x = " << x
142  << " y = " << y
143  << " ppx = " << ppoint.xy.X()
144  << " ppy = " << ppoint.xy.Y()
145  << " dx = " << x - ppoint.xy.X()
146  << " dy = " << y - ppoint.xy.Y()
147  << " delta_x = " << delta_x
148  << " delta_y = " << delta_y
149  << " xWire = " << xWire
150  << " yWire = " << yWire
151  << " wireCrossTraj = " << wireCrossTraj
152  << " wireCrossDelta = " << wireCrossDelta
153  << " isposTraj = " << isposTraj
154  << " isposDelta = " << isposDelta
155  << " ispos = " << ispos
156  << endl;
157  return ispos;
158 }
159 
160 void combinedResidFunc::getCorrectionValue(const DFDCPseudo &ppoint, double x, double y, double z, double ct, double &delta_x, double &delta_y) {
161  double driftDist = (ppoint.time - ct/c)*DRIFT_VELOCITY;
162  //double driftDist = ppoint.time*DRIFT_VELOCITY;
163  double cosangle = ppoint.wire->udir(1);
164  double sinangle= ppoint.wire->udir(0);
165  double alpha = 0.0; // for now
166  if (debug_level > 3) cout << "x, y, z, ct = " << x << ' ' << y << ' ' << z << ' ' << ct << " time, ct/c = " << ppoint.time << " " << ct/c << " driftdist = " << driftDist << endl;
167  double lorentzShift = lorentz_def->GetLorentzCorrection(x, y, z, alpha, driftDist);
168  double ds = -lorentzShift;
169  double dw = driftDist;
170  if (debug_level > 3) cout << "ds, dw" << ds << " " << dw << endl;
171  delta_x = dw*cosangle + ds*sinangle;
172  delta_y = -dw*sinangle + ds*cosangle;
173  return;
174 }
175 
177  const DCDCWire *wire = trkhit.wire;
178  double x = wire->origin.X();
179  double y = wire->origin.Y();
180  double z = wire->origin.Z();
181  // double phi_wire = atan2(y, x);
182  // double phi_naive = phi_wire + PIOVER2;
183  // double theta_naive = wire->stereo;
184  double theta = acos(wire->udir.z());
185  double phi = atan2(wire->udir.y(), wire->udir.x());
186  /*
187  cout << "theta " << theta_naive << " " << theta << " phi " << phi_naive
188  << " "<< phi << endl;
189  */
190  DLine line(x, y, z, theta, phi);
191  return line;
192 }
193 
194 FDCHitDetails combinedResidFunc::getDetails(const DFDCPseudo *ppoint, HepVector point) {
195  FDCHitDetails details;
196  details.doca = trajPtr->doca(point, details.poca);
197  details.rCorr = point;
198  return details;
199 }
200 
202  HepLorentzVector poca;
203  details.doca = trajPtr->doca(line, poca);
204  details.poca = poca;
205  details.dist = velDrift*(trackhit->tdrift - poca.getT()/c);
206  details.posWire = line.poca(poca);
207  return details;
208 }
209 
211  storeDetails = value;
212  return;
213 }
214 
216  for (unsigned int i = 0; i < FDCDetails.size(); i++) {delete FDCDetails[i];}
217  FDCDetails.clear();
218  for (unsigned int i = 0; i < CDCDetails.size(); i++) {delete CDCDetails[i];}
219  CDCDetails.clear();
220 }
221 
222 void combinedResidFunc::setInnerResidFrac(double innerResidFracIn) {
223  innerResidFrac = innerResidFracIn;
224 }
225 
226 void combinedResidFunc::getResidsBoth(vector<double> &residsBoth) {
227 
229  rCDC.calcResids();
230  vector<double> residsC;
231  rCDC.getResids(residsC);
232 
233  rFDC.calcResids();
234  vector<double> residsF;
235  rFDC.getResids(residsF);
236 
237  residsBoth.reserve(n_fdc + n_cdc); // reserve slots for FDC and CDC residuals
238 
239  for (unsigned int i = 0; i < n_fdc; i++) {
240  residsBoth[i] = residsF[i];
241  }
242 
243  for (unsigned int i = 0; i < n_cdc; i++) {
244  residsBoth[n_fdc + i] = residsC[i];
245  }
246 
247  return;
248 
249 }
250 
251 void combinedResidFunc::deriv(const HepVector *params, void *data, HepMatrix *Jacobian) {
252  HepVector paramsCentral = *params, paramsThis;
253  int nResids = n_fdc + n_cdc;
254  unsigned int nParams = trajPtr->getNumberOfParams();
255  int iHep = 0, jHep = 0;
256  if (debug_level > 2){
257  for (unsigned int j = 0; j < nParams; j++) {
258  jHep = j + 1;
259  cout << "central params: jHep = " << jHep << ", values:" << paramsCentral(jHep) << endl;
260  }
261  }
262  // do central swim
263  trajPtr->swim(paramsCentral);
264  // save central residuals
265  vector<double> residsCentral, residsThis;
266  getResidsBoth(residsCentral);
267  if (debug_level >=3) {
268  cout << "combinedResidFunc::deriv2: central resids, ";
269  for (int k = 0; k < nResids; k++) {
270  cout << k << "=" << residsCentral[k] << " ";
271  }
272  cout << endl;
273  }
274  trajPtr->clear();
275  // calculate the Jacobian matrix
276  for (unsigned int j = 0; j < nParams; j++) {
277  jHep = j + 1;
278  // prepare perturbed parameters
279  paramsThis = paramsCentral;
280  paramsThis(jHep) = paramsCentral(jHep) + delta[j];
281  if (debug_level > 2) cout << "perturbed params: jHep = " << jHep << ", values:" << paramsThis << endl;
282  // do the perturbed swim
283  trajPtr->swim(paramsThis);
284  // get the perturbed residuals
285  getResidsBoth(residsThis);
286  if (debug_level >=3) {
287  cout << "combinedResidFunc::deriv2: resids, ";
288  for (int k = 0; k < nResids; k++) {
289  cout << k << "=" << residsThis[k] << " ";
290  }
291  cout << endl;
292  }
293  // calculate the derivatives
294  for (int i = 0; i < nResids; i++) {
295  iHep = i + 1;
296  (*Jacobian)(iHep, jHep) = (residsThis[i] - residsCentral[i])/delta[j];
297  }
298  trajPtr->clear();
299  }
300  return;
301 }
int getXYT(double z, double &x, double &y, double &ct) const
DVector2 xy
rough x,y coordinates in lab coordinate system
Definition: DFDCPseudo.h:100
vector< FDCHitDetails * > FDCDetails
HepVector posWire
Definition: hitDetails.h:27
const DCDCWire * wire
Definition: DCDCTrackHit.h:37
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define c
double dist
Definition: hitDetails.h:25
#define y
const DFDCWire * wire
DFDCWire for this wire.
Definition: DFDCPseudo.h:92
void getDetails(vector< HepVector > &points, vector< double > &docasRef, vector< double > &errorsRef, vector< HepLorentzVector > &pocasRef)
Definition: residFDC.cc:121
double doca(C &spaceObject, HepLorentzVector &poca) const
Definition: MyTrajectory.h:48
class DFDCPseudo: definition for a reconstructed point in the FDC
Definition: DFDCPseudo.h:74
void setStoreDetails(bool storeDetailsValue)
HepLorentzVector poca
Definition: hitDetails.h:15
bool getCorrectionSign(const DFDCPseudo &pseudopoint, double x, double y, double deltaX, double deltaY)
TF1 * f
Definition: FitGains.C:21
void getCorrectionValue(const DFDCPseudo &pseudopoint, double x, double y, double z, double t, double &delta_x, double &delta_y)
void getResids(vector< double > &residsRef)
Definition: residFDC.cc:116
HepVector poca(HepVector point)
Definition: DLine.cc:37
Definition: DLine.h:8
void getResidsBoth(vector< double > &residsBoth)
vector< double > delta
void deriv(const HepVector *x, void *data, HepMatrix *J)
const double alpha
void calcResids()
Definition: residCDC.cc:20
DLine trackhit2line(const DCDCTrackHit &trackhit)
vector< CDCHitDetails * > CDCDetails
void calcResids()
Definition: residFDC.cc:22
double doca
Definition: hitDetails.h:13
void residAndDeriv(const HepVector *x, void *data, HepVector *f, HepMatrix *J)
virtual unsigned int getNumberOfParams()
Definition: MyTrajectory.cc:21
double time
time corresponding to this pseudopoint.
Definition: DFDCPseudo.h:93
void resid(const HepVector *x, void *data, HepVector *f)
double GetLorentzCorrection(double x, double y, double z, double alpha, double dx) const
void setInnerResidFrac(double innerResidFracIn)
virtual void swim(HepVector startingPoint, double theta, double phi)
Definition: MyTrajectory.cc:37
void setInnerResidFrac(double innerResidFracIn)
Definition: residCDC.cc:66
double chiSquared
Definition: residFunc.h:14
void getDetails(vector< double > &docasRef, vector< double > &distsRef, vector< double > &errorsRef, vector< HepLorentzVector > &pocasRef, vector< HepVector > &posWiresRef)
Definition: residCDC.cc:75
HepVector pseudo2HepVector(const DFDCPseudo &pseudopoint)
FDCHitDetails getDetails(const DFDCPseudo *ppoint, HepVector point)
void getResids(vector< double > &residsRef)
Definition: residCDC.cc:70
const DLorentzDeflections * lorentz_def
#define DRIFT_VELOCITY
HepLorentzVector poca
Definition: hitDetails.h:26
const double velDrift
HepVector rCorr
Definition: hitDetails.h:14
combinedResidFunc(vector< const DFDCPseudo * > *pseudopoints, vector< const DCDCTrackHit * > *trackhits, MyTrajectory *trajectory, const DLorentzDeflections *lorentz_def, int level=1)
MyTrajectory * trajPtr
double doca
Definition: hitDetails.h:24