98 {
99
100#ifdef TRKRECO_DEBUG_DETAIL
101 std::cout << " TCosmicFitter::fit ..." << std::endl;
102#endif
103
104 IMdcCalibFunSvc* l_mdcCalFunSvc;
105 Gaudi::svcLocator()->service( "MdcCalibFunSvc", l_mdcCalFunSvc );
106
107
109 unsigned nValid = 0;
110 for (
unsigned i = 0; i < b.
links().length(); i++ )
111 {
112 unsigned state = b.
links()[i]->hit()->state();
115 }
117
118
119
121 TTrack&
t = (TTrack&)b;
122
123
124 double _t0_bes = -1.;
126 Gaudi::svcLocator()->service(
"MessageSvc",
msgSvc );
127 MsgStream log(
msgSvc,
"TCosmicFitter" );
128
129 IDataProviderSvc* eventSvc = NULL;
130 Gaudi::svcLocator()->service( "EventDataSvc", eventSvc );
131
132 SmartDataPtr<RecEsTimeCol> aevtimeCol( eventSvc, "/Event/Recon/RecEsTimeCol" );
133 if ( aevtimeCol )
134 {
135 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
136 _t0_bes = ( *iter_evt )->getTest();
137
138 }
139 else { log << MSG::WARNING << "Could not find RecEsTimeCol" << endmsg; }
140
141
142
143
152 double chi2;
153
156 bool allAxial = true;
159 int err = 0;
160 double factor = 1.0;
161
163 for ( unsigned i = 0; i < 5; i++ ) maxDouble[i] = ( FLT_MAX );
164
165
166 unsigned nTrial = 0;
168 {
169
170
171 chi2 = 0.;
172 for ( unsigned j = 0; j < 5; j++ ) dchi2da[j] = 0.;
174
175#if CAL_TOF_HELIX
176
177 const float Rad = 81;
178 float dRho =
t.helix().a()[0];
179 float Lproj = sqrt( Rad * Rad - dRho * dRho );
180 float tlmd =
t.helix().a()[4];
181 float fct = sqrt( 1. + tlmd * tlmd );
182#endif
183
184
185 unsigned i = 0;
186 while ( TMLink* l =
t.links()[i++] )
187 {
188 const TMDCWireHit& h = *l->hit();
189
190
193
194
195 if ( !nTrial )
197
198
199 int doSagCorrection = 0;
200
201 t.approach( *l, doSagCorrection );
202 double dPhi = l->dPhi();
203 const HepPoint3D& onTrack = l->positionOnTrack();
204 const HepPoint3D& onWire = l->positionOnWire();
205
206#ifdef TRKRECO_DEBUG_DETAIL
207
208
209#endif
210
211
213 if ( onWire.cross( onTrack ).z() < 0. ) leftRight =
WireHitLeft;
214 double distance = h.
drift( leftRight );
215 double eDistance = h.
dDrift( leftRight );
216
217 if ( nTrial && !allAxial )
218 {
219 int side = leftRight;
220
221 double Tfly = _t0_bes / 220. * ( 110. - onWire.y() );
222#if CAL_TOF_HELIX
223 float Rad_wire = sqrt(
x[0] *
x[0] +
x[1] *
x[1] );
224 float L_wire = sqrt( Rad_wire * Rad_wire - dRho * dRho );
225 float flyLength = Lproj - L_wire;
226 if (
x[1] < 0 ) flyLength = Lproj + L_wire;
227 Tfly = flyLength / 29.98 * sqrt( 1.0 + ( 0.105 / 0.5 ) * ( 0.105 / 0.5 ) ) *
228 fct;
229#endif
230 float time = l->hit()->reccdc()->tdc - Tfly;
231
234 float dist = distance;
235 float edist = eDistance;
236 double T0 = l_mdcCalFunSvc->
getT0( layerId, wire );
237 double rawadc = 0.;
238 rawadc = l->hit()->reccdc()->adc;
239
240 double timeWalk = l_mdcCalFunSvc->
getTimeWalk( layerId, rawadc );
241 double drifttime =
time - T0 - timeWalk;
242 dist = l_mdcCalFunSvc->
driftTimeToDist( drifttime, layerId, wire, side, 0.0 );
243 edist = l_mdcCalFunSvc->
getSigma( layerId, side, dist, 0.0 );
244 dist = dist / 10.0;
245 edist = edist / 10.0;
246
247
248
249
250 distance = (double)dist;
251 eDistance = (double)edist;
252
253 l->drift( distance, 0 );
254 l->drift( distance, 1 );
255 l->dDrift( eDistance, 0 );
256 l->dDrift( eDistance, 1 );
257 l->tof( Tfly );
258 }
259 double eDistance2 = eDistance * eDistance;
260
261
263 double vmag =
v.mag();
264 double dDistance = vmag - distance;
265
266
267 this->dxda( *l,
t.helix(), dPhi, dxda, dyda, dzda, doSagCorrection );
268
269
270
272 dDda = ( vmag > 0. ) ? ( (
v.x() * ( 1. - vw.x() * vw.x() ) -
v.y() * vw.x() * vw.y() -
273 v.z() * vw.x() * vw.z() ) *
274 dxda +
275 (
v.y() * ( 1. - vw.y() * vw.y() ) -
v.z() * vw.y() * vw.z() -
276 v.x() * vw.y() * vw.x() ) *
277 dyda +
278 (
v.z() * ( 1. - vw.z() * vw.z() ) -
v.x() * vw.z() * vw.x() -
279 v.y() * vw.z() * vw.y() ) *
280 dzda ) /
281 vmag
283 if ( vmag <= 0.0 )
284 {
285 std::cout << " in fit " << onTrack << ", " << onWire;
287 }
288 dchi2da += ( dDistance / eDistance2 ) * dDda;
289 d2chi2d2a += vT_times_v( dDda ) / eDistance2;
290 double pChi2 = dDistance * dDistance / eDistance2;
291 chi2 += pChi2;
292
293
294 l->update( onTrack, onWire, leftRight, pChi2 );
295 }
296
297
298 double change = chi2Old - chi2;
299 if ( fabs( change ) < convergence ) break;
300
301
302 if ( change < 0. )
303 {
304#ifdef TRKRECO_DEBUG_DETAIL
305 std::cout << "chi2Old, chi2=" << chi2Old << " " << chi2 << std::endl;
306#endif
307
308 a += factor * da;
309
311
312 chi2 = 0.;
313 for ( unsigned j = 0; j < 5; j++ ) dchi2da[j] = 0.;
315
316
317 unsigned i = 0;
318 while ( TMLink* l =
t.links()[i++] )
319 {
320 const TMDCWireHit& h = *l->hit();
321
322
325
326
327 if ( !nTrial )
329
330
331 int doSagCorrection = 0;
332
333 t.approach( *l, doSagCorrection );
334 double dPhi = l->dPhi();
335 const HepPoint3D& onTrack = l->positionOnTrack();
336 const HepPoint3D& onWire = l->positionOnWire();
337
338#ifdef TRKRECO_DEBUG_DETAIL
339
340
341#endif
342
343
345 if ( onWire.cross( onTrack ).z() < 0. ) leftRight =
WireHitLeft;
346 double distance = h.
drift( leftRight );
347 double eDistance = h.
dDrift( leftRight );
348 if ( nTrial && !allAxial )
349 {
350 int side = leftRight;
351
352 double Tfly = _t0_bes / 220. * ( 110. - onWire.y() );
353#if CAL_TOF_HELIX
354 float Rad_wire = sqrt(
x[0] *
x[0] +
x[1] *
x[1] );
355 float L_wire = sqrt( Rad_wire * Rad_wire - dRho * dRho );
356 float flyLength = Lproj - L_wire;
357 if (
x[1] < 0 ) flyLength = Lproj + L_wire;
358 Tfly = flyLength / 29.98 * sqrt( 1.0 + ( 0.105 / 10 ) * ( 0.105 / 10 ) ) *
359 fct;
360#endif
361
362 float time = l->hit()->reccdc()->tdc - Tfly;
363
366 float dist = distance;
367 float edist = eDistance;
368
369
370 double T0 = l_mdcCalFunSvc->
getT0( layerId, wire );
371 double rawadc = 0.;
372 rawadc = l->hit()->reccdc()->adc;
373 double timeWalk = l_mdcCalFunSvc->
getTimeWalk( layerId, rawadc );
374 double drifttime =
time - T0 - timeWalk;
375 dist = l_mdcCalFunSvc->
driftTimeToDist( drifttime, layerId, wire, side, 0.0 );
376 edist = l_mdcCalFunSvc->
getSigma( layerId, side, dist, 0.0 );
377 dist = dist / 10.0;
378 edist = edist / 10.0;
379
380
381
382
383 distance = (double)dist;
384 eDistance = (double)edist;
385
386 l->drift( distance, 0 );
387 l->drift( distance, 1 );
388 l->dDrift( eDistance, 0 );
389 l->dDrift( eDistance, 1 );
390 l->tof( Tfly );
391 }
392 double eDistance2 = eDistance * eDistance;
393
394
396 double vmag =
v.mag();
397 double dDistance = vmag - distance;
398
399
400 this->dxda( *l,
t.helix(), dPhi, dxda, dyda, dzda, doSagCorrection );
401
402
403
405 dDda = ( vmag > 0. ) ? ( (
v.x() * ( 1. - vw.x() * vw.x() ) -
v.y() * vw.x() * vw.y() -
406 v.z() * vw.x() * vw.z() ) *
407 dxda +
408 (
v.y() * ( 1. - vw.y() * vw.y() ) -
v.z() * vw.y() * vw.z() -
409 v.x() * vw.y() * vw.x() ) *
410 dyda +
411 (
v.z() * ( 1. - vw.z() * vw.z() ) -
v.x() * vw.z() * vw.x() -
412 v.y() * vw.z() * vw.y() ) *
413 dzda ) /
414 vmag
416 if ( vmag <= 0.0 )
417 {
418 std::cout << " in fit " << onTrack << ", " << onWire;
420 }
421 dchi2da += ( dDistance / eDistance2 ) * dDda;
422 d2chi2d2a += vT_times_v( dDda ) / eDistance2;
423 double pChi2 = dDistance * dDistance / eDistance2;
424 chi2 += pChi2;
425
426
427 l->update( onTrack, onWire, leftRight, pChi2 );
428 }
429
430 factor *= 0.75;
431#ifdef TRKRECO_DEBUG_DETAIL
432 std::cout << "factor = " << factor << std::endl;
433 std::cout << "chi2 = " << chi2 << std::endl;
434#endif
435 if ( factor < 0.01 ) break;
436 }
437
438 chi2Old = chi2;
439
440
441 if ( allAxial )
442 {
443 f = dchi2da.sub( 1, 3 );
444 e = d2chi2d2a.sub( 1, 3 );
449 da[3] = 0.;
450 da[4] = 0.;
451 }
452 else { da = solve( d2chi2d2a, dchi2da ); }
453
454#ifdef TRKRECO_DEBUG_DETAIL
455
456
457
458#endif
459
460 a -= factor * da;
461
463 ++nTrial;
464 }
465
466
468 unsigned dim;
469 if ( allAxial )
470 {
471 dim = 3;
473 Eb = d2chi2d2a.sub( 1, 3 );
474 Ec = Eb.inverse( err );
475 Ea[0][0] = Ec[0][0];
476 Ea[0][1] = Ec[0][1];
477 Ea[0][2] = Ec[0][2];
478 Ea[1][1] = Ec[1][1];
479 Ea[1][2] = Ec[1][2];
480 Ea[2][2] = Ec[2][2];
481 }
482 else
483 {
484 dim = 5;
485 Ea = d2chi2d2a.inverse( err );
486 }
487
488
489 if ( !err )
490 {
493 }
495
496 t._ndf = nValid - dim;
498
499
500 return err;
501}
HepGeom::Vector3D< double > HepVector3D
HepGeom::Point3D< double > HepPoint3D
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
CLHEP::HepSymMatrix SymMatrix
#define WireHitFittingValid
#define WireHitInvalidForFit
**********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
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
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
unsigned state(void) const
returns state.
float dDrift(unsigned) const
returns drift distance error.
float drift(unsigned) const
returns drift distance.
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 HepVector3D & direction(void) const
returns direction vector of the wire.
bool stereo(void) const
returns true if this wire is in a stereo layer.
const AList< TMLink > & links(unsigned mask=0) const
virtual unsigned objectType(void) const
returns object type.