CGEM BOSS 6.6.5.g
BESIII Offline Software System
Loading...
Searching...
No Matches
T3DLineFitter.cxx
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// $Id: T3DLineFitter.cxx,v 1.10 2011/12/05 04:49:50 maoh Exp $
3//-----------------------------------------------------------------------------
4// Filename : T3DLineFitter.cc
5// Section : Tracking
6// Owner : Kenji Inami
7// Email : [email protected]
8//-----------------------------------------------------------------------------
9// Description : A class to fit a TTrackBase object to a 3D line.
10// See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
11//-----------------------------------------------------------------------------
12
13#ifdef TRKRECO_DEBUG_DETAIL
14#ifndef TRKRECO_DEBUG
15#define TRKRECO_DEBUG
16#endif
17#endif
18#include <iostream>
19#include <float.h>
21#include "TrkReco/T3DLine.h"
22#define HEP_SHORT_NAMES
23//#include "panther/panther.h"
24//#include MDC_H
25#include "MdcTables/MdcTables.h"
26#include "CLHEP/Matrix/Vector.h"
27#include "CLHEP/Matrix/SymMatrix.h"
28#include "CLHEP/Matrix/Matrix.h"
29#include "TrkReco/TMLink.h"
30#include "TrkReco/TTrackBase.h"
31
34
35#include "GaudiKernel/MsgStream.h"
36#include "GaudiKernel/AlgFactory.h"
37#include "GaudiKernel/ISvcLocator.h"
38#include "GaudiKernel/IMessageSvc.h"
39#include "GaudiKernel/IDataProviderSvc.h"
40#include "GaudiKernel/IDataManagerSvc.h"
41#include "GaudiKernel/SmartDataPtr.h"
42#include "GaudiKernel/IDataProviderSvc.h"
43#include "GaudiKernel/PropertyMgr.h"
44#include "GaudiKernel/IJobOptionsSvc.h"
45#include "GaudiKernel/IMessageSvc.h"
46#include "GaudiKernel/Bootstrap.h"
47#include "GaudiKernel/StatusCode.h"
48
49#include "GaudiKernel/ContainedObject.h"
50#include "GaudiKernel/SmartRef.h"
51#include "GaudiKernel/SmartRefVector.h"
52#include "GaudiKernel/ObjectVector.h"
54
56
57using CLHEP::HepVector;
58using CLHEP::HepSymMatrix;
59using CLHEP::HepMatrix;
60
61#define T3DLine2DFit 2
62
63/* ignore fortran codes ... zangsl 04/10/26
64extern "C"
65void
66calcdc_driftdist_(int *,
67 int *,
68 int *,
69 float[3],
70 float[3],
71 float *,
72 float *,
73 float *);
74
75extern "C"
76void
77calcdc_tof2_(int *, float *, float *, float *);
78*/
79
80T3DLineFitter::T3DLineFitter(const std::string& name)
81 : TMFitter(name),
82// _sag(true),_propagation(1),_tof(false){
83 _sag(false),_propagation(0),_tof(true){ //Liuqg, tmp
84
85}
86T3DLineFitter::T3DLineFitter(const std::string& name,
87 bool m_sag,int m_prop,bool m_tof)
88 : TMFitter(name),
89 _sag(m_sag),_propagation(m_prop),_tof(m_tof){
90}
91
93}
94
95void T3DLineFitter::sag(bool _in){
96 _sag = _in;
97}
99 _propagation = _in;
100}
101void T3DLineFitter::tof(bool _in){
102 _tof = _in;
103}
104
105void T3DLineFitter::drift(const T3DLine& t,const TMLink& l,
106 float t0Offset,
107 double& distance,double& err) const{
108
109 //read t0 from TDS-------------------------------------//
110 double _t0_bes = -1.;
111 IMessageSvc *msgSvc;
112 Gaudi::svcLocator()->service("MessageSvc", msgSvc);
113 MsgStream log(msgSvc, "TCosmicFitter");
114
115 IDataProviderSvc* eventSvc = NULL;
116 Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
117
118 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
119 if (aevtimeCol) {
120 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
121 _t0_bes = (*iter_evt)->getTest();
122 }else{
123 log << MSG::WARNING << "Could not find RecEsTimeCol" << endreq;
124 }
125 //----------------------------------------------------//
126
127 const TMDCWireHit& h = *l.hit();
128 const HepPoint3D& onTrack = l.positionOnTrack();
129 const HepPoint3D& onWire = l.positionOnWire();
130 unsigned leftRight = WireHitRight;
131 // if (onWire.cross(onTrack).z() < 0) leftRight = WireHitLeft;
132 if((onWire.x()*onTrack.y()-onWire.y()*onTrack.x())<0) leftRight = WireHitLeft;
133
134 //...No correction...
135/* if ((t0Offset == 0.) && (_propagation==0) && (! _tof)) {
136 distance = l.drift(leftRight);
137 err = h.dDrift(leftRight);
138 return;
139 }*/
140
141 //...TOF correction...
142 // momentum ???? or velocity -> assumued light velocity
143 float tof = 0.;
144// if (_tof) {
145 // double length = ((onTrack - t.x0())*t.k())/t.k().mag();
146/* double tl = t.tanl();
147 double length = ((onTrack - t.x0())*t.k())/sqrt(1+tl*tl);
148 static const double Ic = 1/29.9792; //1/[cm/ns]
149 tof = length * Ic;
150*/
151 //cal the time pass through the chamber
152/* const float Rad = 81; // cm
153 float dRho = t.helix().a()[0];
154 float Lproj = sqrt(Rad*Rad - dRho*dRho);
155 float tlmd = t.helix().a()[4];
156 float fct = sqrt(1. + tlmd * tlmd);
157
158 float x[3]={ onWire.x(), onWire.y(), onWire.z()};
159
160 float Rad_wire = sqrt(x[0]*x[0]+x[1]*x[1]); // cm
161 float L_wire = sqrt(Rad_wire*Rad_wire - dRho*dRho);
162 float flyLength = Lproj - L_wire;
163 if (x[1]<0) flyLength = Lproj + L_wire;
164 float Tfly = flyLength/29.98*sqrt(1.0+(0.105/10)*(0.105/10))*fct; //approxiate... cm/ns
165*/
166 double Tfly = _t0_bes/220.*(110.-onWire.y());
167// }
168
169 //...T0 and propagation corrections...
170 int wire = h.wire()->localId();
171 int layerId = h.wire()->layerId();
172// int wire = h.wire()->id();
173 int side = leftRight;
174// if (side==0) side = -1;
175// float p[3] = {-t.sinPhi0(),t.cosPhi0(),t.tanl()};
176// float x[3] = {onWire.x(), onWire.y(), onWire.z()};
177// float time = h.reccdc()->tdc + t0Offset - tof;
178 float time = h.reccdc()->tdc - Tfly;
179 float dist;
180 float edist;
181 IMdcCalibFunSvc* l_mdcCalFunSvc;
182 Gaudi::svcLocator() -> service("MdcCalibFunSvc", l_mdcCalFunSvc);
183 double T0 = l_mdcCalFunSvc->getT0(layerId,wire);
184 double rawadc = 0.;
185 rawadc =h.reccdc()->adc;
186
187 double timeWalk = l_mdcCalFunSvc->getTimeWalk(layerId, rawadc);
188// int prop = _propagation;
189 double drifttime =time -T0-timeWalk;
190// dist = l_mdcCalFunSvc->rawTimeNoTOFToDist(time, layerId, wire, side, 0.0);
191 dist = l_mdcCalFunSvc->driftTimeToDist(drifttime,layerId, wire, side, 0.0);
192 edist = l_mdcCalFunSvc->getSigma(layerId, side, dist, 0.0);
193// if(layerId<20) edist = 999999.;
194 dist = dist/10.0; //mm->cm
195 edist = edist/10.0;
196/*zsl
197 calcdc_driftdist_(& prop,
198 & wire,
199 & side,
200 p,
201 x,
202 & time,
203 & dist,
204 & edist);
205 */
206 distance = (double) dist;
207 err = (double) edist;
208 return;
209}
210
212 return fit(tb,0);
213}
214
215int T3DLineFitter::fit(TTrackBase& tb, float t0Offset) const{
216
217 //std::cout<<"T3DLineFitter::fit start"<<std::endl;
218
219 //...Type check...
220 if(tb.objectType() != Line3D) return TFitUnavailable;
221 T3DLine& t = (T3DLine&) tb;
222
223 //...Already fitted ?...
224 if(t.fitted()) return TFitAlreadyFitted;
225
226 //...Count # of hits...
227 AList<TMLink> cores = t.cores();
228 unsigned nCores = cores.length();
229 unsigned nStereoCores = NStereoHits(cores);
230
231
232 //...Check # of hits...
233 bool flag2D = false;
234 if ((nStereoCores == 0) && (nCores > 3)) flag2D = true;
235 else if ((nStereoCores < 2) || (nCores - nStereoCores < 3))
236 return TFitErrorFewHits;
237
238 //...Move pivot to ORIGIN...
239 const HepPoint3D pivot_bak = t.pivot();
240 t.pivot(ORIGIN);
241
242 //...Setup...
243 Vector a(4),da(4);
244 a = t.a();
245 Vector dxda(4);
246 Vector dyda(4);
247 Vector dzda(4);
248 Vector dDda(4);
249 Vector dchi2da(4);
250 SymMatrix d2chi2d2a(4,0);
251 static const SymMatrix zero4(4,0);
252 double chi2;
253 double chi2Old = DBL_MAX;
254 double factor = 1.0;
255 int err = 0;
256 SymMatrix e(2,0);
257 Vector f(2);
258
259 //...Fitting loop...
260 unsigned nTrial = 0;
261 while(nTrial < 100){
262
263 //...Set up...
264 chi2 = 0;
265 for (unsigned j=0;j<4;j++) dchi2da[j]=0;
266 d2chi2d2a=zero4;
267
268 //...Loop with hits...
269 unsigned i=0;
270 while(TMLink* l = cores[i++]){
271 const TMDCWireHit& h = *l->hit();
272
273 //...Cal. closest points...
274 t.approach(*l,_sag);
275 const HepPoint3D& onTrack=l->positionOnTrack();
276 const HepPoint3D& onWire=l->positionOnWire();
277 unsigned leftRight = WireHitRight;
278 if (onWire.cross(onTrack).z() < 0.) leftRight = WireHitLeft;
279
280 //...Obtain drift distance and its error...
281 double distance;
282 double eDistance;
283 drift(t, * l, t0Offset, distance, eDistance);
284 l->drift(distance,0);
285 l->drift(distance,1);
286 l->dDrift(eDistance,0);
287 l->dDrift(eDistance,1);
288 double eDistance2 = eDistance * eDistance;
289 //cout<<"distance: "<< distance << " eDistance: " << eDistance << endl;
290
291 //...Residual...
292 HepVector3D v = onTrack - onWire;
293 double vmag = v.mag();
294 double dDistance = vmag - distance;
295
296 HepVector3D vw;
297 //...dxda...
298 this->dxda(*l, t, dxda, dyda, dzda, vw);
299
300 //...Chi2 related...
301 dDda = (vmag > 0.)
302 ? ((v.x() * (1. - vw.x() * vw.x()) -
303 v.y() * vw.x() * vw.y() - v.z() * vw.x() * vw.z())
304 * dxda +
305 (v.y() * (1. - vw.y() * vw.y()) -
306 v.z() * vw.y() * vw.z() - v.x() * vw.y() * vw.x())
307 * dyda +
308 (v.z() * (1. - vw.z() * vw.z()) -
309 v.x() * vw.z() * vw.x() - v.y() * vw.z() * vw.y())
310 * dzda) / vmag : Vector(4,0);
311 if (vmag <= 0.0) {
312 std::cout << " in fit " << onTrack << ", " << onWire;
313 h.dump();
314 }
315 dchi2da += (dDistance / eDistance2) * dDda;
316 d2chi2d2a += vT_times_v(dDda) / eDistance2;
317 double pChi2 = dDistance * dDistance / eDistance2;
318 chi2 += pChi2;
319
320 //...Store results...
321 l->update(onTrack, onWire, leftRight, pChi2);
322 }
323
324 //...Check condition...
325 double change = chi2Old - chi2;
326
327 if (fabs(change) < 1.0e-5) break;
328 if (change < 0.) {
329 a += factor * da; //recover
330 factor *= 0.1;
331 }else{
332 chi2Old = chi2;
333 if(flag2D){
334 f = dchi2da.sub(1,2);
335 e = d2chi2d2a.sub(1,2);
336 f = solve(e,f);
337 da[0]=f[0];
338 da[1]=f[1];
339 da[2]= 0;
340 da[3]= 0;
341 }else{
342 //...Cal. helix parameters for next loop...
343 da = solve(d2chi2d2a, dchi2da);
344 }
345 }
346 a -= factor * da;
347 t.a(a);
348 ++nTrial;
349 }
350
351 //...Cal. error matrix...
352 SymMatrix Ea(4,0);
353 unsigned dim;
354 if(flag2D){
355 dim=2;
356 SymMatrix Eb(3,0),Ec(3,0);
357 Eb = d2chi2d2a.sub(1,3);
358 Ec = Eb.inverse(err);
359 Ea[0][0] = Ec[0][0];
360 Ea[0][1] = Ec[0][1];
361 Ea[0][2] = Ec[0][2];
362 Ea[1][1] = Ec[1][1];
363 Ea[1][2] = Ec[1][2];
364 Ea[2][2] = Ec[2][2];
365 }else{
366 dim=4;
367 Ea = d2chi2d2a.inverse(err);
368 }
369
370 //...Store information...
371 if(! err){
372 t.a(a);
373 t.Ea(Ea);
374 t._fitted = true;
375 if (flag2D) err = T3DLine2DFit;
376 }else{
377 err = TFitFailed;
378 }
379
380 t._ndf = nCores - dim;
381 t._chi2 = chi2;
382
383 //...Recover pivot...
384 t.pivot(pivot_bak);
385
386 return err;
387}
388
389int T3DLineFitter::dxda(const TMLink& l,const T3DLine& t,
390 Vector& dxda,Vector& dyda,Vector& dzda,
391 HepVector3D& wireDirection) const{
392 // onTrack = x0 + t * k
393 // onWire = w0 + s * wireDirection
394 //...Setup...
395 const TMDCWire& w = *l.wire();
396 const HepVector3D k = t.k();
397 const double cosPhi0 = t.cosPhi0();
398 const double sinPhi0 = t.sinPhi0();
399 const double dr = t.dr();
400 const HepPoint3D& onWire = l.positionOnWire();
401 const HepPoint3D& onTrack = l.positionOnTrack();
402 // const Vector3 u = onTrack - onWire;
403 const double t_t = (onWire - t.x0()).dot(k)/k.mag2();
404
405 //...Sag correction...
406 HepPoint3D xw = w.xyPosition();
407 HepPoint3D wireBackwardPosition = w.backwardPosition();
408 wireDirection = w.direction();
409 if (_sag) w.wirePosition(onWire.z(),
410 xw,
411 wireBackwardPosition,
412 (HepVector3D&)wireDirection);
413 const HepVector3D& v = wireDirection;
414
415 // onTrack = x0 + t * k
416 // onWire = w0 + s * v
417
418 const double v_k = v.dot(k);
419 const double tvk = v_k*v_k-k.mag2();
420 if(tvk==0) return(-1);
421
422 const HepVector3D& dxdt_a = k;
423
424 const HepVector3D dxda_t[4]
425 ={HepVector3D(cosPhi0,sinPhi0,0),
426 HepVector3D(-dr*sinPhi0-t_t*cosPhi0,dr*cosPhi0-t_t*sinPhi0,0),
427 HepVector3D(0,0,1),
428 HepVector3D(0,0,t_t)};
429
430 const HepVector3D dx0da[4]
431 ={HepVector3D(cosPhi0,sinPhi0,0),
432 HepVector3D(-dr*sinPhi0,dr*cosPhi0,0),
433 HepVector3D(0,0,1),
434 HepVector3D(0,0,0)};
435
436 const HepVector3D dkda[4]
437 ={HepVector3D(0,0,0),
438 HepVector3D(-cosPhi0,-sinPhi0,0),
439 HepVector3D(0,0,0),
440 HepVector3D(0,0,1)};
441
442 const HepVector3D d = t.x0() - wireBackwardPosition;
443 const HepVector3D kvkv = k - v_k*v;
444
445 for(int i=0;i<4;i++){
446 const double v_dkda = v.dot(dkda[i]);
447
448 const double dtda = dx0da[i].dot(kvkv)/tvk
449 + d.dot(dkda[i]-v_dkda*v)/tvk
450 - d.dot(kvkv)*2*(v_k*v_dkda-k.dot(dkda[i]))/(tvk*tvk);
451
452 const HepVector3D dxda3D = dxda_t[i] + dtda*dxdt_a;
453
454 dxda[i] = dxda3D.x();
455 dyda[i] = dxda3D.y();
456 dzda[i] = dxda3D.z();
457 }
458
459 return 0;
460}
Double_t time
#define DBL_MAX
Definition: KalFitAlg.h:13
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
NTuple::Array< double > m_tof
Definition: MdcHistItem.h:105
IMessageSvc * msgSvc()
#define T3DLine2DFit
HepGeom::Vector3D< double > HepVector3D
Definition: T3DLineFitter.h:29
#define Line3D
Definition: T3DLine.h:21
const HepPoint3D ORIGIN
Constants.
Definition: TMDCUtil.cxx:47
#define WireHitLeft
Definition: TMDCWireHit.h:21
#define WireHitRight
Definition: TMDCWireHit.h:22
#define TFitAlreadyFitted
Definition: TMFitter.h:28
#define TFitFailed
Definition: TMFitter.h:30
#define TFitUnavailable
Definition: TMFitter.h:31
#define TFitErrorFewHits
Definition: TMFitter.h:29
virtual double driftTimeToDist(double drifttime, int layid, int cellid, int lr, double entrance=0.0) const =0
virtual double getSigma(int layid, int lr, double dist, double entrance=0.0, double tanlam=0.0, double z=0.0, double Q=1000.0) const =0
virtual double getT0(int layid, int cellid) const =0
virtual double getTimeWalk(int layid, double Q) const =0
T3DLineFitter(const std::string &name)
Constructor.
void propagation(int)
virtual int fit(TTrackBase &) const
void sag(bool)
void tof(bool)
virtual ~T3DLineFitter()
Destructor.
A class to represent a track in tracking.
Definition: T3DLine.h:50
struct MdcRec_wirhit * reccdc(void) const
returns a pointer to RECMDC_WIRHIT.
Definition: TMDCWireHit.h:224
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
Definition: TMDCWireHit.cxx:64
const TMDCWire *const wire(void) const
returns a pointer to a TMDCWire.
Definition: TMDCWireHit.h:218
A class to represent a wire in MDC.
Definition: TMDCWire.h:55
unsigned localId(void) const
returns local id in a wire layer.
Definition: TMDCWire.h:213
unsigned layerId(void) const
returns layer id.
Definition: TMDCWire.h:219
const HepVector3D & direction(void) const
returns direction vector of the wire.
Definition: TMDCWire.h:342
const HepPoint3D & xyPosition(void) const
returns middle position of a wire. z componet is 0.
Definition: TMDCWire.h:327
const HepPoint3D & backwardPosition(void) const
returns position in backward endplate.
Definition: TMDCWire.h:306
void wirePosition(float zPosition, HepPoint3D &xyPosition, HepPoint3D &backwardPosition, HepVector3D &direction) const
calculates position and direction vector with sag correction.
Definition: TMDCWire.cxx:541
A class to fit a TTrackBase object.
Definition: TMFitter.h:34
A virtual class for a track class in tracking.
Definition: TTrackBase.h:46
virtual unsigned objectType(void) const
returns object type.
Definition: TTrackBase.h:268
int t()
Definition: t.c:1