Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DTrackLSFitter.cc
Go to the documentation of this file.
1 // DTrackLSFitter.cc
2 //
3 /*******************************************************************************
4 To Do:
5 
6 Allow for multiple tracks, either by declaring this as a one track
7 class, or by handling multiple hit lists.
8 
9 Distinquish actions between Monte Carlo and real data.
10 
11 *******************************************************************************/
12 
13 #include <iostream>
14 #include <vector>
15 #include <string>
16 #include <CLHEP/Matrix/Matrix.h>
17 #include <CLHEP/Matrix/Vector.h>
18 
19 #include <FDC/DFDCHit.h>
20 #include <FDC/DFDCPseudo_factory.h>
22 #include <TRACKING/DMCTrackHit.h>
24 #include <DANA/DApplication.h>
25 
26 #include "MyTrajectory.h"
27 #include "MyTrajectoryBfield.h"
28 #include "chisqMin.h"
29 #include "combinedResidFunc.h"
30 #include "globalGslFuncs.h"
31 #include "DTrackLSFitter.h"
32 #include "DFactoryGeneratorLSLM.h"
33 
34 #define TARGET_POSITION 65.0
35 #define THETA_FORWARD_CUT 1.178097
36 #define THETA_BACKWARD_CUT 1.963495
37 #define THREEPIOVER4 2.356194
38 #define MAX_TRAJS 1000
39 #define MAX_POINTS 10000
40 #define MAX_TRACKS 200
41 #define PI 3.141592653589793238
42 
43 double radialDist2(const DCDCTrackHit *trkhit) {
44  double x, y, r2;
45  const DCDCWire *wire;
46  wire = trkhit->wire;
47  x = wire->origin.X();
48  y = wire->origin.Y();
49  r2 = x*x + y*y;
50  return r2;
51 }
52 
54 public:
55  int operator() (const DCDCTrackHit* const &hit1, const DCDCTrackHit* const &hit2) const
56  {return radialDist2(hit1) < radialDist2(hit2);}
57 };
58 
59 // Routine used If we're a plugin
60 extern "C"{
61 void InitPlugin(JApplication *app){
62  InitJANAPlugin(app);
63  app->AddFactoryGenerator(new DFactoryGeneratorLSLM());
64 }
65 } // "C"
66 
67 
68 //------------------------------------------------------------------
69 // DTrackLSFitter
70 //------------------------------------------------------------------
71 DTrackLSFitter::DTrackLSFitter(JEventLoop *loop):DTrackFitter(loop),debug_level(1), ppEnd(5)
72 {
73  // Get the DApplication pointer so we can get pointers to the Lorentz deflections
74  // table for the FDC and the DFDCSegment factory pointer.
75  // NOTE: The bfield member is supplied by the DTrackFitter base class and
76  // set in the DTrackFitter(loop) constructor.
77  DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
78  if(!dapp){
79  _DBG_<<"Cannot get DApplication from JEventLoop! (are you using a JApplication based program?)"<<endl;
80  return;
81  }
83  JFactory_base *base = loop->GetFactory("DFDCSegment");
84  segment_factory = dynamic_cast<DFDCSegment_factory*>(base);
85 }
86 
87 //------------------------------------------------------------------
88 // ~DTrackLSFitter
89 //------------------------------------------------------------------
91 {
92  cout << "DTrackLSFitter destructor called\n";
93 }
94 
95 //------------------------------------------------------------------
96 // ChiSq
97 //------------------------------------------------------------------
98 double DTrackLSFitter::ChiSq(fit_type_t fit_type, DReferenceTrajectory *rt, double *chisq_ptr, int *dof_ptr, vector<pull_t> *pulls_ptr)
99 {
100  // This will need to be filled in by Mark
101  if(chisq_ptr)*chisq_ptr=0.0;
102  if(dof_ptr)*dof_ptr=0.0;
103  return 0.0; // return the chisq/Ndof
104 }
105 
106 //------------------------------------------------------------------
107 // FitTrack
108 //------------------------------------------------------------------
110 {
111 
112  // Initialize result to indicate fit in progress
113  this->fit_status = kFitNotDone;
114 
115  status = DTRACKLSFITTER_UNDEFINED; // mark status as undefined
116 
117  fitPtr = NULL;
118 
119  // The CDC and FDC hits are already filled out by the DTrackFitter base class
120  // This code used different names though so we just set a couple of aliases.
121  vector<const DCDCTrackHit*> &trackhits = cdchits;
122  vector<const DFDCPseudo*> &pseudopoints = fdchits;
123 
124  //
125  // find the starting parameters
126  //
128 
129 #ifdef GRKUTA
131 #else
133 #endif
134 
135  // instantiate the residual function class
136  combinedResidFunc prf(&pseudopoints, &trackhits, &trajectory, lorentz_def,
137  debug_level);
138 
139  residFuncPtr = &prf; // set the global residual point to refer to this class
140  try {
141 
142  fitPtr = new chisqMin(&prf, debug_level);
143  HepVector ppStart(5), paramTrue(5);
144  //
145  // swim using MC truth points
146  //
147 
148 // trajectory.swimMC(mctrackhits);
149  trajectory.clear();
150 
151 
152  //
153  // swim with MC parameters
154  //
155 // paramTrue(1) = 0.0;
156 // paramTrue(2) = TARGET_POSITION;
157 // paramTrue(3) = thrown[0]->momentum().Theta();
158 // paramTrue(4) = thrown[0]->momentum().Phi();
159 // paramTrue(5) = 1.0/thrown[0]->momentum().Pt();
160 // trajectory.swim(paramTrue);
161 
162 // trajectory.clear();
163 
164  prf.clearDetails();
165 
166  //
167  // swim with initial parameters
168  //
169  ppStart(1) = xpInitial;
170  ppStart(2) = zInitial;
171  ppStart(3) = thetaInitial;
172  ppStart(4) = phiInitial;
173  ppStart(5) = ptinvInitial;
174  trajectory.swim(ppStart); // swim with initial parameters
175 
176  trajectory.clear();
177 
178  //
179  // do the fit
180  //
181  for (double df = 0.0; df < 1.0001; df += 1.0) {
182  prf.setInnerResidFrac(df);
183  fitPtr->setStartParams(ppStart);
184  fitPtr->fit();
186  // cout << df << ' ' << fitPtr->getChi2() << endl;
187  ppStart = ppEnd;
188  }
189  //
190  // store the results
191  //
192  trajectory.swim(ppEnd); // swim with final parameters
193 
194  /*
195  *signatureFile << eventnumber << ' '
196  << size_fdc << ' '
197  << size_cdc << ' '
198  << thetaInitial << ' '
199  << phiInitial << ' '
200  << ppEnd(1) << ' '
201  << ppEnd(2) << ' '
202  << ppEnd(3) << ' '
203  << ppEnd(4) << ' '
204  << ppEnd(5) << ' '
205  << fitPtr->getChi2() << endl;
206  */
207 
208  // get details of hits on track
209 
211  } catch (int code) {
212 
213  cout << "==========fit error = " << code << "===========" << endl;
215  cout << "= at event " << loop->GetJEvent().GetEventNumber() << endl;
216  if (debug_level >= 3) {
217  cout << "= trajectory" << endl;
218  trajectory.print();
219  }
220  this->fit_status = kFitFailed;
221 
222  }
223 
224  // clean up
225  prf.clearDetails();
226  prf.setStoreDetails(false);
227  trajectory.clear();
228 
229  if (debug_level >= 3) {
230  cout << "FDCHitDetails: " << FDCHitDetails::getInstanceCount() << endl;
231  cout << "CDCHitDetails: " << CDCHitDetails::getInstanceCount() << endl;
232  }
233 
235 
236  // Copy results into DTrackFitter data members
237  double r0 = ppEnd(1);
238  double z0 = ppEnd(2);
239  double theta = ppEnd(3);
240  double phi = ppEnd(4);
241  double ptot = fabs(1.0/ppEnd(5)/sin(theta));
242 
243  double x0 = r0*cos(phi);
244  double y0 = r0*sin(phi);
245  DVector3 pos(x0, y0, z0);
246  DVector3 mom;
247  mom.SetMagThetaPhi(ptot, theta, phi);
248  fit_params.setMomentum(mom);
249  fit_params.setPosition(pos);
250  fit_params.setCharge(ppEnd(5)>=0.0 ? 1.0:-1.0);
251 
252  this->chisq = fitPtr->getChi2();
253  this->Ndof = fitPtr->getN() - fitPtr->getP();
254  if(this->fit_status==kFitNotDone)this->fit_status = kFitSuccess; // honor it if fit_status has already been set (e.g. to kFitFailed)
257  }
258 
259  if (fitPtr != NULL) {
260  delete fitPtr;
261  fitPtr = NULL;
262  }
263 
264  return this->fit_status;
265 
266 }
267 
269  return ppEnd;
270 }
271 
273  double chisq = -1.0;
274  if (fitPtr) chisq = fitPtr->getChi2();
275  return chisq;
276 }
277 
279  return size_fdc;
280 }
281 
283  return size_cdc;
284 }
285 
287  xpInitial = input_params.position().Perp(); // distance from beam line at point of closest approach
289  thetaInitial = input_params.momentum().Theta();
290  phiInitial = input_params.momentum().Phi();
291  ptinvInitial = 1.0/input_params.momentum().Perp();
292 
293 #if 0
295  ptinvInitial = 0.0;
296  if (size_fdc > 1) {
297  double xfirst, yfirst, zfirst, xlast, ylast, zlast;
298  xfirst = (pseudopoints[0])->x;
299  yfirst = (pseudopoints[0])->y;
300  zfirst = (pseudopoints[0])->wire->origin(2);
301  xlast = (pseudopoints[size_fdc - 1])->x;
302  ylast = (pseudopoints[size_fdc - 1])->y;
303  zlast = (pseudopoints[size_fdc - 1])->wire->origin(2);
304  double rfirst = sqrt(xfirst*xfirst + yfirst*yfirst);
305  double delta_z = zlast - zfirst;
306  double rPerp = sqrt((xlast - xfirst)*(xlast - xfirst) + (ylast - yfirst)*(ylast - yfirst));
307  thetaInitial = atan2(rPerp, delta_z);
308  phiInitial = atan2(ylast - yfirst, xlast - xfirst);
309  xpInitial = (xfirst - yfirst/tan(phiInitial))*sin(phiInitial);
310  zInitial = zfirst - sqrt(rfirst*rfirst - xpInitial*xpInitial)/tan(thetaInitial);
311  } else if (size_cdc > 0) {
312 
313  float thetaInitialTrue = thrown[0]->momentum().Theta();
314  if (thetaInitialTrue < PIOVER2) {
315  thetaInitial = thetaInitialTrue - 5*PI/180;
316  } else {
317  thetaInitial = thetaInitialTrue + 5*PI/180;
318  }
319 
320  const DCDCTrackHit *trkhit_0 = trackhits[0];
321  const DCDCWire *wire_0 = trkhit_0->wire;
322  const DCDCTrackHit *trkhit_n = trackhits[size_cdc - 1];
323  const DCDCWire *wire_n = trkhit_n->wire;
324  double deltaX = wire_n->origin.X() - wire_0->origin.X();
325  double deltaY = wire_n->origin.Y() - wire_0->origin.Y();
326  phiInitial = atan2(deltaY, deltaX);
327  double alpha = phiInitial - PIOVER2;
328  xpInitial = wire_0->origin.X()*cos(alpha) + wire_0->origin.Y()*sin(alpha);
329  } else {
330  int code = 32;
331  throw code;
332  }
333 #endif
334 }
335 
336 
338  return status;
339 }
340 
void setMomentum(const DVector3 &aMomentum)
DApplication * dapp
#define DTRACKLSFITTER_NOMINAL
int getP()
Definition: chisqMin.cc:98
TFile * df
Definition: ST_slices_eff.C:17
const DCDCWire * wire
Definition: DCDCTrackHit.h:37
The DTrackFitter class is a base class for different charged track fitting algorithms. It does not actually fit the track itself, but provides the interface and some common support features most algorthims will need to implement.
Definition: DTrackFitter.h:61
TVector3 DVector3
Definition: DVector3.h:14
fit_status_t FitTrack(void)
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
double radialDist2(const DCDCTrackHit *trkhit)
#define y
void fit()
Definition: chisqMin.cc:76
const DVector3 & position(void) const
double ChiSq(fit_type_t fit_type, DReferenceTrajectory *rt, double *chisq_ptr=NULL, int *dof_ptr=NULL, vector< pull_t > *pulls_ptr=NULL)
#define PIOVER2
#define PI
DLorentzDeflections * GetLorentzDeflections(unsigned int run_number=1)
vector< const DFDCPseudo * > fdchits_used_in_fit
Definition: DTrackFitter.h:242
void swim(const HepVector &param)
void setStoreDetails(bool storeDetailsValue)
DTrackLSFitter(JEventLoop *loop)
HepVector getParams()
const DMagneticFieldMap * bfield
Definition: DTrackFitter.h:228
const DLorentzDeflections * lorentz_def
int operator()(const DCDCTrackHit *const &hit1, const DCDCTrackHit *const &hit2) const
vector< const DCDCTrackHit * > cdchits
Definition: DTrackFitter.h:224
const double alpha
InitPlugin_t InitPlugin
fit_status_t fit_status
Definition: DTrackFitter.h:240
int getN()
Definition: chisqMin.cc:102
double getChiSquared()
#define _DBG_
Definition: HDEVIO.h:12
residFunc * residFuncPtr
Definition: globalGslFuncs.h:4
vector< const DCDCTrackHit * > cdchits_used_in_fit
Definition: DTrackFitter.h:241
double sqrt(double)
vector< const DFDCPseudo * > fdchits
Definition: DTrackFitter.h:225
void setInnerResidFrac(double innerResidFracIn)
double sin(double)
#define DTRACKLSFITTER_UNDEFINED
Example program for a Hall-D analyzer which uses DANA.
#define TARGET_POSITION
const DVector3 & momentum(void) const
JEventLoop * loop
Definition: DTrackFitter.h:231
DTrackingData input_params
Definition: DTrackFitter.h:226
void setStartParams(HepVector &x)
Definition: chisqMin.cc:39
double getChi2()
Definition: chisqMin.cc:71
#define DTRACKLSFITTER_EXCEPTION_THROWN
chisqMin * fitPtr
static int getInstanceCount()
Definition: hitDetails.cc:16
class DFDCSegment_factory: definition for a JFactory that produces space points from pseudopoints...
void setPosition(const DVector3 &aPosition)
void setFitterStartParams()
DFDCSegment_factory * segment_factory
DTrackingData fit_params
Definition: DTrackFitter.h:234
void getParams(HepVector &x)
Definition: chisqMin.cc:65
static int getInstanceCount()
Definition: hitDetails.cc:30
HepVector ppEnd