4 #include "GenFit/ConstField.h"
5 #include "GenFit/EventDisplay.h"
6 #include "GenFit/Exception.h"
7 #include "GenFit/FieldManager.h"
8 #include "GenFit/KalmanFitStatus.h"
9 #include "GenFit/GblFitter.h"
11 #include "GenFit/KalmanFitter.h"
12 #include "GenFit/KalmanFitterInfo.h"
13 #include "GenFit/KalmanFitterRefTrack.h"
14 #include "GenFit/MaterialEffects.h"
15 #include "GenFit/PlanarMeasurement.h"
16 #include "GenFit/RKTrackRep.h"
17 #include "GenFit/SpacepointMeasurement.h"
18 #include "GenFit/StateOnPlane.h"
19 #include "GenFit/TGeoMaterialInterface.h"
20 #include "GenFit/Track.h"
21 #include "GenFit/TrackPoint.h"
23 #include "Criteria/SimpleCircle.h"
25 #include "TDatabasePDG.h"
26 #include "TGeoManager.h"
35 #include "StFwdTrackMaker/Common.h"
37 #include "StFwdTrackMaker/include/Tracker/FwdHit.h"
38 #include "StFwdTrackMaker/include/Tracker/TrackFitter.h"
39 #include "StFwdTrackMaker/include/Tracker/STARField.h"
40 #include "StFwdTrackMaker/include/Tracker/FwdGeomUtils.h"
42 #include "StarGenerator/UTIL/StarRandom.h"
51 genfit::FitStatus getStatus() {
return mFitStatus; }
52 genfit::AbsTrackRep *getTrackRep() {
return mTrackRep; }
53 genfit::Track *getTrack() {
return mFitTrack; }
54 void setGenerateHistograms(
bool gen) { mGenHistograms = gen;}
69 TGeoManager * gMan =
nullptr;
72 LOG_INFO <<
"StFwdTrackMaker is loading the geometry cache: " << mConfig.
get<
string>(
"Geometry", mGeoCache.Data()).c_str() << endm;
73 TGeoManager::Import(mConfig.
get<
string>(
"Geometry", mGeoCache.Data()).c_str());
76 genfit::MaterialEffects::getInstance()->init(
new genfit::TGeoMaterialInterface());
79 genfit::MaterialEffects::getInstance()->setDebugLvl( mConfig.
get<
int>(
"TrackFitter.MaterialEffects:DebugLvl", 0) );
81 genfit::MaterialEffects::getInstance()->setEnergyLossBetheBloch( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.EnergyLossBetheBloch",
true) );
82 genfit::MaterialEffects::getInstance()->setNoiseBetheBloch( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.NoiseBetheBloch",
true) );
83 genfit::MaterialEffects::getInstance()->setNoiseCoulomb( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.NoiseCoulomb",
true) );
84 genfit::MaterialEffects::getInstance()->setEnergyLossBrems( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.EnergyLossBrems",
true) );
85 genfit::MaterialEffects::getInstance()->setNoiseBrems( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.NoiseBrems",
true) );
86 genfit::MaterialEffects::getInstance()->ignoreBoundariesBetweenEqualMaterials( mConfig.
get<
int>(
"TrackFitter.MaterialEffects.ignoreBoundariesBetweenEqualMaterials",
true) );
89 genfit::MaterialEffects::getInstance()->setNoEffects( !mConfig.
get<
bool>(
"TrackFitter:MaterialEffects",
false));
90 if (!mConfig.
get<
bool>(
"TrackFitter:MaterialEffects",
false)){
91 LOG_INFO <<
"Turning OFF GenFit Material Effects in stepper" << endm;
96 if (mConfig.
get<
bool>(
"TrackFitter:constB",
false)) {
97 mBField = std::unique_ptr<genfit::AbsBField>(
new genfit::ConstField(0., 0., 5.));
98 LOG_INFO <<
"StFwdTrackMaker: Tracking with constant magnetic field" << endl;
99 }
else if (mConfig.
get<
bool>(
"TrackFitter:zeroB",
false)) {
100 mBField = std::unique_ptr<genfit::AbsBField>(
new genfit::ConstField(0., 0., 0.));
101 LOG_INFO <<
"StFwdTrackMaker: Tracking with ZERO magnetic field" << endl;
104 LOG_INFO <<
"StFwdTrackMaker: Tracking with StarFieldAdapter" << endl;
108 genfit::FieldManager::getInstance()->init(mBField.get());
111 mFitter = std::unique_ptr<genfit::AbsKalmanFitter>(
new genfit::KalmanFitterRefTrack());
115 mFitter->setMaxFailedHits(mConfig.
get<
int>(
"TrackFitter.KalmanFitterRefTrack:MaxFailedHits", -1));
116 mFitter->setDebugLvl(mConfig.
get<
int>(
"TrackFitter.KalmanFitterRefTrack:DebugLvl", 0));
117 mFitter->setMaxIterations(mConfig.
get<
int>(
"TrackFitter.KalmanFitterRefTrack:MaxIterations", 4));
118 mFitter->setMinIterations(mConfig.
get<
int>(
"TrackFitter.KalmanFitterRefTrack:MinIterations", 0));
128 mFSTZLocations = fwdGeoUtils.fstZ(
129 mConfig.
getVector<
double>(
"TrackFitter.Geometry:fst",
130 {140.286011, 154.286011, 168.286011 }
135 if ( fwdGeoUtils.fstZ( 0 ) < 1.0 ) {
136 LOG_WARN <<
"Using FST z-locations from config or defautl, may not match hits" << endm;
139 const double dzInnerFst = 1.715 + 0.04;
140 const double dzOuterFst = 0.240 + 0.04;
143 std::stringstream sstr;
144 sstr <<
"Adding FST Planes at: ";
146 for (
auto z : mFSTZLocations) {
147 mFSTPlanes.push_back(
148 genfit::SharedPlanePtr(
150 new genfit::DetPlane(TVector3(0, 0, z), TVector3(1, 0, 0), TVector3(0, 1, 0) )
155 mFSTPlanesInner.push_back(
156 genfit::SharedPlanePtr(
158 new genfit::DetPlane(TVector3(0, 0, z - dzInnerFst), TVector3(1, 0, 0), TVector3(0, 1, 0) )
161 mFSTPlanesInner.push_back(
162 genfit::SharedPlanePtr(
164 new genfit::DetPlane(TVector3(0, 0, z + dzInnerFst), TVector3(1, 0, 0), TVector3(0, 1, 0) )
168 mFSTPlanesOuter.push_back(
169 genfit::SharedPlanePtr(
171 new genfit::DetPlane(TVector3(0, 0, z - dzOuterFst), TVector3(1, 0, 0), TVector3(0, 1, 0) )
174 mFSTPlanesOuter.push_back(
175 genfit::SharedPlanePtr(
177 new genfit::DetPlane(TVector3(0, 0, z + dzOuterFst), TVector3(1, 0, 0), TVector3(0, 1, 0) )
181 sstr << delim << z <<
" (-dzInner=" << z - dzInnerFst <<
", +dzInner=" << z+dzInnerFst <<
", -dzOuter=" << z - dzOuterFst <<
", +dzOuter=" << z + dzOuterFst <<
")";
184 LOG_DEBUG << sstr.str() << endm;
188 mFTTZLocations = fwdGeoUtils.fttZ(
189 mConfig.getVector<
double>(
"TrackFitter.Geometry:ftt", {281.082,304.062,325.058,348.068})
192 if ( fwdGeoUtils.fttZ( 0 ) < 1.0 ) {
193 LOG_WARN <<
"Using FTT z-locations from config or default, may not match hits" << endm;
196 if ( mFTTZLocations.size() != 4 ){
197 LOG_ERROR <<
"Wrong number of FTT layers, got " << mFTTZLocations.size() <<
" but expected 4" << endm;
202 sstr <<
"Adding FTT Planes at: ";
204 for (
auto z : mFTTZLocations) {
205 mFTTPlanes.push_back(
206 genfit::SharedPlanePtr(
208 new genfit::DetPlane(TVector3(0, 0, z), TVector3(1, 0, 0), TVector3(0, 1, 0))
214 LOG_DEBUG << sstr.str() << endm;
217 mVertexSigmaXY = mConfig.get<
double>(
"TrackFitter.Vertex:sigmaXY", 1.0);
218 mVertexSigmaZ = mConfig.get<
double>(
"TrackFitter.Vertex:sigmaZ", 30.0);
219 mVertexPos = mConfig.getVector<
double>(
"TrackFitter.Vertex:pos", {0.0,0.0,0.0});
220 mIncludeVertexInFit = mConfig.get<
bool>(
"TrackFitter.Vertex:includeInFit",
false);
222 if ( mGenHistograms )
226 void makeHistograms() {
228 mHist[
"ECalProjPosXY"] =
new TH2F(
"ECalProjPosXY",
";X;Y", 1000, -500, 500, 1000, -500, 500);
229 mHist[
"ECalProjSigmaXY"] =
new TH2F(
"ECalProjSigmaXY",
";#sigma_{X};#sigma_{Y}", 50, 0, 0.5, 50, 0, 0.5);
230 mHist[
"ECalProjSigmaR"] =
new TH1F(
"ECalProjSigmaR",
";#sigma_{XY} (cm) at ECAL", 50, 0, 0.5);
232 mHist[
"SiProjPosXY"] =
new TH2F(
"SiProjPosXY",
";X;Y", 1000, -500, 500, 1000, -500, 500);
233 mHist[
"SiProjSigmaXY"] =
new TH2F(
"SiProjSigmaXY",
";#sigma_{X};#sigma_{Y}", 150, 0, 15, 150, 0, 15);
235 mHist[
"VertexProjPosXY"] =
new TH2F(
"VertexProjPosXY",
";X;Y", 100, -5, 5, 100, -5, 5);
236 mHist[
"VertexProjSigmaXY"] =
new TH2F(
"VertexProjSigmaXY",
";#sigma_{X};#sigma_{Y}", 150, 0, 20, 150, 0, 20);
238 mHist[
"VertexProjPosZ"] =
new TH1F(
"VertexProjPosZ",
";Z;", 100, -50, 50);
239 mHist[
"VertexProjSigmaZ"] =
new TH1F(
"VertexProjSigmaZ",
";#sigma_{Z};", 100, 0, 10);
241 mHist[
"SiWrongProjPosXY"] =
new TH2F(
"SiWrongProjPosXY",
";X;Y", 1000, -500, 500, 1000, -500, 500);
242 mHist[
"SiWrongProjSigmaXY"] =
new TH2F(
"SiWrongProjSigmaXY",
";#sigma_{X};#sigma_{Y}", 50, 0, 0.5, 50, 0, 0.5);
244 mHist[
"SiDeltaProjPosXY"] =
new TH2F(
"SiDeltaProjPosXY",
";X;Y", 1000, 0, 20, 1000, 0, 20);
246 mHist[
"FstDiffZVsR"] =
new TH2F(
"FstDiffZVsR",
";R;dz", 400, 0, 40, 500, -5, 5 );
247 mHist[
"FstDiffZVsPhiSliceInner"] =
new TH2F(
"FstDiffZVsPhiSliceInner",
";slice;dz", 15, 0, 15, 500, -5, 5 );
248 mHist[
"FstDiffZVsPhiSliceOuter"] =
new TH2F(
"FstDiffZVsPhiSliceOuter",
";slice;dz", 15, 0, 15, 500, -5, 5 );
250 mHist[
"FstDiffZVsPhiOuter"] =
new TH2F(
"FstDiffZVsPhiOuter",
";slice;dz", 628, 0, TMath::Pi()*2, 500, -5, 5 );
252 mHist[
"CorrFstDiffZVsPhiSliceInner"] =
new TH2F(
"CorrFstDiffZVsPhiSliceInner",
";slice;dz", 15, 0, 15, 500, -5, 5 );
253 mHist[
"CorrFstDiffZVsPhiSliceOuter"] =
new TH2F(
"CorrFstDiffZVsPhiSliceOuter",
";slice;dz", 15, 0, 15, 500, -5, 5 );
257 mHist[n] =
new TH1F(n.c_str(),
";curv", 1000, 0, 10000);
259 mHist[n] =
new TH1F(n.c_str(),
";pT (GeV/c)", 500, 0, 10);
261 mHist[n] =
new TH1F(n.c_str(),
";eta", 500, 0, 5);
263 n =
"delta_fit_seed_pT";
264 mHist[n] =
new TH1F(n.c_str(),
";#Delta( fit, seed ) pT (GeV/c)", 500, -5, 5);
265 n =
"delta_fit_seed_eta";
266 mHist[n] =
new TH1F(n.c_str(),
";#Delta( fit, seed ) eta", 500, 0, 5);
267 n =
"delta_fit_seed_phi";
268 mHist[n] =
new TH1F(n.c_str(),
";#Delta( fit, seed ) phi", 500, -5, 5);
271 mHist[n] =
new TH1F(n.c_str(),
";", 5, 0, 5);
272 FwdTrackerUtils::labelAxis(mHist[n]->GetXaxis(), {
"Total",
"Pass",
"Fail",
"GoodCardinal",
"Exception"});
275 mHist[n] =
new TH1F(n.c_str(),
"; Duraton (ms)", 5000, 0, 50000);
277 n =
"FailedFitDuration";
278 mHist[n] =
new TH1F(n.c_str(),
"; Duraton (ms)", 500, 0, 50000);
282 void writeHistograms() {
283 if ( !mGenHistograms )
285 for (
auto nh : mHist) {
286 nh.second->SetDirectory(gDirectory);
294 TMatrixDSym CovMatPlane(KiTrack::IHit *h){
296 cm(0, 0) =
static_cast<FwdHit*
>(h)->_covmat(0, 0);
297 cm(1, 1) =
static_cast<FwdHit*
>(h)->_covmat(1, 1);
298 cm(0, 1) =
static_cast<FwdHit*
>(h)->_covmat(0, 1);
308 float fitSimpleCircle(
Seed_t trackCand,
size_t i0,
size_t i1,
size_t i2) {
312 if (i0 > 12 || i1 > 12 || i2 > 12)
316 KiTrack::SimpleCircle sc(trackCand[i0]->getX(), trackCand[i0]->getY(), trackCand[i1]->getX(), trackCand[i1]->getY(), trackCand[i2]->getX(), trackCand[i2]->getY());
317 curv = sc.getRadius();
318 }
catch (KiTrack::InvalidParameter &e) {
332 float seedState(
Seed_t trackCand, TVector3 &seedPos, TVector3 &seedMom) {
334 if(trackCand.size() < 3){
343 FwdHit *hit_closest_to_IP =
static_cast<FwdHit *
>(trackCand[0]);
346 std::map<size_t, size_t> vol_map;
349 for (
size_t i = 0; i < 13; i++)
352 for (
size_t i = 0; i < trackCand.size(); i++) {
353 auto fwdHit =
static_cast<FwdHit *
>(trackCand[i]);
354 vol_map[abs(fwdHit->_vid)] = i;
356 if (hit_closest_to_IP->getZ() > fwdHit->getZ())
357 hit_closest_to_IP = fwdHit;
367 curvs.push_back(fitSimpleCircle(trackCand, vol_map[12], vol_map[11], vol_map[10]));
368 curvs.push_back(fitSimpleCircle(trackCand, vol_map[12], vol_map[11], vol_map[9]));
369 curvs.push_back(fitSimpleCircle(trackCand, vol_map[12], vol_map[10], vol_map[9]));
370 curvs.push_back(fitSimpleCircle(trackCand, vol_map[11], vol_map[10], vol_map[9]));
376 for (
size_t i = 0; i < curvs.size(); i++) {
378 this->mHist[
"seed_curv"]->Fill(curvs[i]);
386 mcurv = mcurv / nmeas;
393 p0.SetXYZ(trackCand[vol_map[9]]->getX(), trackCand[vol_map[9]]->getY(), trackCand[vol_map[9]]->getZ());
395 if (vol_map[10] < 13)
396 p1.SetXYZ(trackCand[vol_map[10]]->getX(), trackCand[vol_map[10]]->getY(), trackCand[vol_map[10]]->getZ());
398 const double K = 0.00029979;
399 double pt = mcurv * K * 5;
400 double dx = (p1.X() - p0.X());
401 double dy = (p1.Y() - p0.Y());
402 double dz = (p1.Z() - p0.Z());
403 double phi = TMath::ATan2(dy, dx);
404 double Rxy = sqrt(dx * dx + dy * dy);
405 double theta = TMath::ATan2(Rxy, dz);
409 seedMom.SetPtThetaPhi(pt, theta, phi);
410 seedPos.SetXYZ(hit_closest_to_IP->getX(), hit_closest_to_IP->getY(), hit_closest_to_IP->getZ());
412 if (mGenHistograms) {
413 this->mHist[
"seed_pT"]->Fill(seedMom.Pt());
414 this->mHist[
"seed_eta"]->Fill(seedMom.Eta());
421 genfit::MeasuredStateOnPlane projectToFst(
size_t si_plane, genfit::Track *fitTrack) {
423 genfit::MeasuredStateOnPlane nil;
427 auto detSi = mFSTPlanes[si_plane];
428 genfit::MeasuredStateOnPlane tst = fitTrack->getFittedState(1);
429 auto TCM = fitTrack->getCardinalRep()->get6DCov(tst);
432 fitTrack->getCardinalRep()->extrapolateToPlane(tst, detSi);
434 TCM = fitTrack->getCardinalRep()->get6DCov(tst);
437 float x = tst.getPos().X();
438 float y = tst.getPos().Y();
439 float z = tst.getPos().Z();
441 LOG_DEBUG <<
"Track Uncertainty at FST (plane=" << si_plane <<
") @ x= " << x <<
", y= " << y <<
", z= " << z <<
" : " << sqrt(TCM(0, 0)) <<
", " << sqrt(TCM(1, 1)) << endm;
446 genfit::SharedPlanePtr getFstPlane(
FwdHit * h ){
448 size_t planeId = h->getSector();
450 TVector3 hitXYZ( h->getX(), h->getY(), h->getZ() );
452 double phi = hitXYZ.Phi();
453 if ( phi < 0 ) phi = TMath::Pi() * 2 + phi;
454 const double phi_slice = phi / (TMath::Pi() / 6.0);
455 const int phi_index = ((int)phi_slice);
456 const double r =sqrt( pow(hitXYZ.x(), 2) + pow(hitXYZ.y(), 2) );
458 const size_t idx = phi_index % 2;
459 auto planeCorr = mFSTPlanesInner[planeId*2 + idx];
461 planeCorr = mFSTPlanesOuter[planeId*2 + idx];
463 double cdz = (h->getZ() - planeCorr->getO().Z());
466 LOG_WARN <<
"FST Z =" << h->getZ() <<
" vs CORR Plane Z = " << planeCorr->getO().Z() <<
" DIFF: " << cdz <<
" phi_slice = " << phi_slice <<
", phi_index = " << phi_index <<
" R=" << hitXYZ.Pt() <<
" idx=" << idx << endm;
477 TVector3 refitTrackWithSiHits(genfit::Track *originalTrack,
Seed_t si_hits) {
479 TVector3 pOrig = originalTrack->getCardinalRep()->getMom(originalTrack->getFittedState(1, originalTrack->getCardinalRep()));
483 if (originalTrack->getFitStatus(originalTrack->getCardinalRep())->isFitConverged() ==
false) {
490 auto trackRepPos =
new genfit::RKTrackRep(mPdgPositron);
491 auto trackRepNeg =
new genfit::RKTrackRep(mPdgElectron);
494 auto trackPoints = originalTrack->getPointsWithMeasurement();
496 if ((trackPoints.size() < (mFTTZLocations.size() +1) && mIncludeVertexInFit) || trackPoints.size() < mFTTZLocations.size() ) {
501 TVectorD rawCoords = trackPoints[0]->getRawMeasurement()->getRawHitCoords();
502 double z = mFSTZLocations[0];
503 if (mIncludeVertexInFit)
506 TVector3 seedPos(rawCoords(0), rawCoords(1), z);
507 TVector3 seedMom = pOrig;
510 auto mFitTrack =
new genfit::Track(trackRepPos, seedPos, seedMom);
511 mFitTrack->addTrackRep(trackRepNeg);
513 genfit::Track &fitTrack = *mFitTrack;
515 size_t firstFTTIndex = 0;
516 if (mIncludeVertexInFit) {
518 fitTrack.insertPoint(
new genfit::TrackPoint(trackPoints[0]->getRawMeasurement(), &fitTrack));
523 TVectorD hitCoords(2);
531 for (
auto h : si_hits) {
532 if (
nullptr == h )
continue;
534 hitCoords[0] = h->getX();
535 hitCoords[1] = h->getY();
536 genfit::PlanarMeasurement *measurement =
new genfit::PlanarMeasurement(hitCoords, CovMatPlane(h), h->getSector(), ++hitId,
nullptr);
538 planeId = h->getSector();
540 if (mFSTPlanes.size() <= planeId) {
541 LOG_WARN <<
"invalid VolumId -> out of bounds DetPlane, vid = " << planeId << endm;
546 auto plane = getFstPlane( static_cast<FwdHit*>(h) );
548 measurement->setPlane(plane, planeId);
549 fitTrack.insertPoint(
new genfit::TrackPoint(measurement, &fitTrack));
552 TVector3 hitXYZ( h->getX(), h->getY(), h->getZ() );
553 float phi = hitXYZ.Phi();
554 if ( phi < 0 ) phi = TMath::Pi() * 2 + phi;
555 double phi_slice = phi / (TMath::Pi() / 6.0);
556 int phi_index = ((int)phi_slice);
557 double dz = (h->getZ() - plane->getO().Z());
559 double r =sqrt( pow(hitXYZ.x(), 2) + pow(hitXYZ.y(), 2) );
561 size_t idx = phi_index % 2;
562 auto planeCorr = mFSTPlanesInner[planeId + idx];
564 planeCorr = mFSTPlanesOuter[planeId + idx];
566 double cdz = (h->getZ() - planeCorr->getO().Z());
570 ((TH2*)mHist[
"FstDiffZVsR" ])->Fill( r, dz );
573 mHist[
"FstDiffZVsPhiSliceInner"]->Fill( phi_slice, dz );
574 mHist[
"CorrFstDiffZVsPhiSliceInner"]->Fill( phi_slice, cdz );
576 mHist[
"FstDiffZVsPhiSliceOuter"]->Fill( phi_slice, dz );
577 mHist[
"CorrFstDiffZVsPhiSliceOuter"]->Fill( phi_slice, cdz );
578 mHist[
"FstDiffZVsPhiOuter"]->Fill( phi, dz );
585 for (
size_t i = firstFTTIndex; i < trackPoints.size(); i++) {
587 fitTrack.insertPoint(
new genfit::TrackPoint(trackPoints[i]->getRawMeasurement(), &fitTrack));
593 fitTrack.checkConsistency();
596 mFitter->processTrack(&fitTrack);
598 fitTrack.checkConsistency();
601 fitTrack.determineCardinalRep();
603 }
catch (genfit::Exception &e) {
605 LOG_WARN <<
"Track fit exception : " << e.what() << endm;
608 if (fitTrack.getFitStatus(fitTrack.getCardinalRep())->isFitConverged() ==
false) {
615 auto cardinalRep = fitTrack.getCardinalRep();
616 auto cardinalStatus = fitTrack.getFitStatus(cardinalRep);
617 mFitStatus = *cardinalStatus;
618 }
catch (genfit::Exception &e) {
621 TVector3 p = fitTrack.getCardinalRep()->getMom(fitTrack.getFittedState(1, fitTrack.getCardinalRep()));
627 TVector3 refitTrackWithGBL( genfit::Track *originalTrack ) {
629 TVector3 pOrig = originalTrack->getCardinalRep()->getMom(originalTrack->getFittedState(1, originalTrack->getCardinalRep()));
633 if (originalTrack->getFitStatus(originalTrack->getCardinalRep())->isFitConverged() ==
false) {
640 auto trackRepPos =
new genfit::RKTrackRep(mPdgPositron);
641 auto trackRepNeg =
new genfit::RKTrackRep(mPdgElectron);
644 auto trackPoints = originalTrack->getPointsWithMeasurement();
647 TVectorD rawCoords = trackPoints[0]->getRawMeasurement()->getRawHitCoords();
648 TVector3 seedPos(rawCoords(0), rawCoords(1), rawCoords(2));
649 TVector3 seedMom = pOrig;
652 auto pFitTrack =
new genfit::Track(trackRepPos, seedPos, seedMom);
653 pFitTrack->addTrackRep(trackRepNeg);
655 genfit::Track &fitTrack = *pFitTrack;
657 for (
size_t i = 0; i < trackPoints.size(); i++) {
659 fitTrack.insertPoint(
new genfit::TrackPoint(trackPoints[i]->getRawMeasurement(), &fitTrack));
662 auto gblFitter = std::unique_ptr<genfit::GblFitter>(
new genfit::GblFitter());
665 fitTrack.checkConsistency();
668 mFitter->processTrack(&fitTrack);
670 fitTrack.checkConsistency();
673 fitTrack.determineCardinalRep();
675 }
catch (genfit::Exception &e) {
677 LOG_WARN <<
"Track fit exception : " << e.what() << endm;
680 if (fitTrack.getFitStatus(fitTrack.getCardinalRep())->isFitConverged() ==
false) {
681 LOG_WARN <<
"GBL fit did not converge" << endm;
688 auto cardinalRep = fitTrack.getCardinalRep();
689 auto cardinalStatus = fitTrack.getFitStatus(cardinalRep);
690 mFitStatus = *cardinalStatus;
691 }
catch (genfit::Exception &e) {
692 LOG_WARN <<
"Failed to get cardinal status from converged fit" << endm;
694 auto mom = fitTrack.getCardinalRep()->getMom(fitTrack.getFittedState(1, fitTrack.getCardinalRep()));
708 TVector3 fitSpacePoints( vector<genfit::SpacepointMeasurement*> spoints, TVector3 &seedPos, TVector3 &seedMom ){
711 auto trackRepPos =
new genfit::RKTrackRep(mPdgPositron);
712 auto trackRepNeg =
new genfit::RKTrackRep(mPdgElectron);
715 auto mFitTrack =
new genfit::Track(trackRepPos, seedPos, seedMom);
716 mFitTrack->addTrackRep(trackRepNeg);
718 genfit::Track &fitTrack = *mFitTrack;
722 for (
size_t i = 0; i < spoints.size(); i++ ){
723 fitTrack.insertPoint(
new genfit::TrackPoint(spoints[i], &fitTrack));
726 mFitter->processTrackWithRep(&fitTrack, trackRepPos);
727 mFitter->processTrackWithRep(&fitTrack, trackRepNeg);
729 }
catch (genfit::Exception &e) {
730 LOG_ERROR <<
"GenFit failed to fit track with: " << e.what() << endm;
734 fitTrack.checkConsistency();
736 fitTrack.determineCardinalRep();
737 auto cardinalRep = fitTrack.getCardinalRep();
739 TVector3 p = cardinalRep->getMom(fitTrack.getFittedState(1, cardinalRep));
742 }
catch (genfit::Exception &e) {
743 LOG_ERROR <<
"GenFit failed to fit track with: " << e.what() << endm;
745 return TVector3(0, 0, 0);
752 TVector3 fitTrack(
Seed_t trackCand,
double *
Vertex = 0, TVector3 *McSeedMom = 0) {
753 long long itStart = FwdTrackerUtils::nowNanoSecond();
754 if (mGenHistograms) this->mHist[
"FitStatus"]->Fill(
"Total", 1);
761 pv[0] = mVertexPos[0] + rand.
gauss(mVertexSigmaXY);
762 pv[1] = mVertexPos[1] + rand.
gauss(mVertexSigmaXY);
763 pv[2] = mVertexPos[2] + rand.
gauss(mVertexSigmaZ);
771 TVector3 seedMom, seedPos;
773 seedState(trackCand, seedPos, seedMom);
775 if (McSeedMom !=
nullptr) {
776 seedMom = *McSeedMom;
780 if (mIncludeVertexInFit) {
781 LOG_DEBUG <<
"Primary Vertex in fit (seed pos) @ " << TString::Format(
"(%f, %f, %f)", pv[0], pv[1], pv[2] ).Data() << endm;
782 seedPos.SetXYZ(pv[0], pv[1], pv[2]);
790 auto trackRepPos =
new genfit::RKTrackRep(mPdgMuon);
791 auto trackRepNeg =
new genfit::RKTrackRep(mPdgAntiMuon);
794 mFitTrack =
new genfit::Track(trackRepPos, seedPos, seedMom);
795 mFitTrack->addTrackRep(trackRepNeg);
799 <<
"seedPos : (" << seedPos.X() <<
", " << seedPos.Y() <<
", " << seedPos.Z() <<
" )"
800 <<
", seedMom : (" << seedMom.X() <<
", " << seedMom.Y() <<
", " << seedMom.Z() <<
" )"
801 <<
", seedMom : (" << seedMom.Pt() <<
", " << seedMom.Eta() <<
", " << seedMom.Phi() <<
" )"
804 genfit::Track &fitTrack = *mFitTrack;
810 TVectorD hitCoords(2);
817 if (mIncludeVertexInFit) {
819 TMatrixDSym hitCov3(3);
820 hitCov3(0, 0) = mVertexSigmaXY * mVertexSigmaXY;
821 hitCov3(1, 1) = mVertexSigmaXY * mVertexSigmaXY;
822 hitCov3(2, 2) = mVertexSigmaZ * mVertexSigmaZ;
824 genfit::SpacepointMeasurement *measurement =
new genfit::SpacepointMeasurement(pv, hitCov3, 0, ++hitId,
nullptr);
825 fitTrack.insertPoint(
new genfit::TrackPoint(measurement, &fitTrack));
831 for (
auto h : trackCand) {
833 hitCoords[0] = h->getX();
834 hitCoords[1] = h->getY();
836 genfit::PlanarMeasurement *measurement =
new genfit::PlanarMeasurement(hitCoords, CovMatPlane(h), h->getSector(), ++hitId,
nullptr);
838 planeId = h->getSector();
840 if (mFTTPlanes.size() <= planeId) {
841 LOG_WARN <<
"invalid VolumId -> out of bounds DetPlane, vid = " << planeId << endm;
842 return TVector3(0, 0, 0);
845 auto plane = mFTTPlanes[planeId];
846 measurement->setPlane(plane, planeId);
847 fitTrack.insertPoint(
new genfit::TrackPoint(measurement, &fitTrack));
849 if (abs(h->getZ() - plane->getO().Z()) > 0.05) {
850 LOG_WARN <<
"Z Mismatch h->z = " << h->getZ() <<
", plane->z = "<< plane->getO().Z() <<
", diff = " << abs(h->getZ() - plane->getO().Z()) << endm;
860 mFitter->processTrackWithRep(&fitTrack, trackRepPos);
861 mFitter->processTrackWithRep(&fitTrack, trackRepNeg);
863 }
catch (genfit::Exception &e) {
864 if (mGenHistograms) mHist[
"FitStatus"]->Fill(
"Exception", 1);
874 fitTrack.checkConsistency();
877 fitTrack.determineCardinalRep();
878 auto cardinalRep = fitTrack.getCardinalRep();
879 auto cardinalStatus = fitTrack.getFitStatus(cardinalRep);
880 mFitStatus = *cardinalStatus;
887 mTrackRep = cardinalRep->clone();
888 if (fitTrack.getFitStatus(cardinalRep)->isFitConverged() && mGenHistograms ) {
889 this->mHist[
"FitStatus"]->Fill(
"GoodCardinal", 1);
892 if (fitTrack.getFitStatus(trackRepPos)->isFitConverged() ==
false &&
893 fitTrack.getFitStatus(trackRepNeg)->isFitConverged() ==
false) {
895 LOG_WARN <<
"FWD Track GenFit Failed" << endm;
898 long long duration = (FwdTrackerUtils::nowNanoSecond() - itStart) * 1e-6;
899 if (mGenHistograms) {
900 this->mHist[
"FitStatus"]->Fill(
"Fail", 1);
901 this->mHist[
"FailedFitDuration"]->Fill(duration);
906 p = cardinalRep->getMom(fitTrack.getFittedState(1, cardinalRep));
907 mQ = cardinalRep->getCharge(fitTrack.getFittedState(1, cardinalRep));
910 LOG_DEBUG <<
"track fit p = " << TString::Format(
"(%f, %f, %f), q=%f", p.X(), p.Y(), p.Z(), mQ ).Data() << endm;
912 }
catch (genfit::Exception &e) {
913 LOG_WARN <<
"Exception on track fit: " << e.what() << endm;
916 long long duration = (FwdTrackerUtils::nowNanoSecond() - itStart) * 1e-6;
917 if (mGenHistograms) {
918 this->mHist[
"FitStatus"]->Fill(
"Exception", 1);
919 this->mHist[
"FailedFitDuration"]->Fill(duration);
925 long long duration = (FwdTrackerUtils::nowNanoSecond() - itStart) * 1e-6;
926 if (mGenHistograms) {
927 this->mHist[
"FitStatus"]->Fill(
"Pass", 1);
928 this->mHist[
"delta_fit_seed_pT"]->Fill(p.Pt() - seedMom.Pt());
929 this->mHist[
"delta_fit_seed_eta"]->Fill(p.Eta() - seedMom.Eta());
930 this->mHist[
"delta_fit_seed_phi"]->Fill(p.Phi() - seedMom.Phi());
931 this->mHist[
"FitDuration"]->Fill(duration);
943 vector<genfit::SharedPlanePtr> mFTTPlanes;
944 vector<genfit::SharedPlanePtr> mFSTPlanes;
945 vector<genfit::SharedPlanePtr> mFSTPlanesInner;
946 vector<genfit::SharedPlanePtr> mFSTPlanesOuter;
948 void SetIncludeVertex(
bool vert ) { mIncludeVertexInFit = vert; }
951 std::unique_ptr<genfit::AbsBField> mBField;
957 std::map<std::string, TH1 *> mHist;
958 bool mGenHistograms =
false;
961 std::unique_ptr<genfit::AbsKalmanFitter> mFitter =
nullptr;
964 const int mPdgPiPlus = 211;
965 const int mPdgPiMinus = -211;
966 const int mPdgPositron = 11;
967 const int mPdgElectron = -11;
968 const int mPdgMuon = 13;
969 const int mPdgAntiMuon = -13;
973 vector<double> mFSTZLocations, mFTTZLocations;
976 double mVertexSigmaXY = 1;
977 double mVertexSigmaZ = 30;
978 vector<double> mVertexPos;
979 bool mIncludeVertexInFit =
false;
982 genfit::FitStatus mFitStatus;
983 genfit::AbsTrackRep *mTrackRep;
984 genfit::Track *mFitTrack;
Double_t gauss(const Double_t sigma) const
Return a random number distributed according to a gaussian with specified sigma.
static StarRandom & Instance()
Obtain the single instance of the random number generator.
std::vector< T > getVector(std::string path, std::vector< T > dv) const
Get a Vector object from config.
A class for providing random number generation.
T get(std::string path, T dv) const
template function for getting any type that can be converted from string via stringstream ...