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