135 const std::shared_ptr<const Acts::TrackingGeometry> trackingGeo =
m_trackingGeometrySvc->trackingGeometry();
136 const Acts::GeometryContext tgContext =
m_ctxProvider.getGeometryContext(ctx);
137 const Acts::MagneticFieldContext mfContext =
m_ctxProvider.getMagneticFieldContext(ctx);
138 const Acts::CalibrationContext calContext =
m_ctxProvider.getCalibrationContext(ctx);
142 rngWrapper->
setSeed(name(), ctx);
143 CLHEP::HepRandomEngine* randEngine = rngWrapper->
getEngine(ctx);
146 ATH_CHECK(outHandle.
record(std::make_unique<xAOD::MuonSegmentContainer>(),
147 std::make_unique<xAOD::MuonSegmentAuxContainer>()));
150 auto handleCreation =
m_auxMeasProv.makeHandle(ctx, tgContext);
151 if (!handleCreation.ok()){
152 ATH_MSG_ERROR(
"Auxiliary measurement containers cannot be made");
153 return StatusCode::FAILURE;
155 auto& auxMeasHandle{*handleCreation};
165 SeedingAux::Config cfg{};
166 cfg.parsToUse.clear();
169 SeedingAux::Line_t line{};
171 Acts::PropagatorPlainOptions propagationOption{tgContext, mfContext};
174 constexpr bool doScat =
false;
175 constexpr bool doEloss =
false;
177 std::move(propagationOption),
178 nullptr, doScat, doEloss,
185 Acts::ObjVisualization3D visualHelper{};
189 auto saveDisplay = [&](
const std::string& state) {
193 const auto objFile = std::format(
"SegmentReFitTest_{:}_{:}_{:}_{:}.obj",
194 state, ctx.eventID().event_number(), reFitMe->index(),
196 visualHelper.write(objFile);
198 visualHelper = Acts::ObjVisualization3D{};
202 m_detMgr->getSectorEnvelope(reFitMe->chamberIndex(),
204 reFitMe->etaIndex());
214 const Acts::Transform3 invTrf = sectorTrf.inverse();
217 auto& seedPars = dec_seedPars(*reFitMe);
218 seedPars[Acts::toUnderlying(ParamDefs::x0)] = locSeedPos.x();
219 seedPars[Acts::toUnderlying(ParamDefs::y0)] = locSeedPos.y();
220 seedPars[Acts::toUnderlying(ParamDefs::theta)] = locSeedDir.theta();
221 seedPars[Acts::toUnderlying(ParamDefs::phi)] = locSeedDir.phi();
224 const GeoTrf::CoordEulerAngles sectorAngles = GeoTrf::getCoordRotationAngles(sectorTrf);
232 hitsToFit.front() : hitsToFit.at(1);
234 const Acts::Surface* entrancePortal =
portalSurface(entrance,
true);
235 const Acts::Surface* exitPortal =
portalSurface(hitsToFit.back(),
false);
240 using namespace Acts::PlanarHelper;
242 const Acts::Intersection3D isectEntrance = intersectPlane(seedPos, seedDir, planeNormal,
243 entrancePortal->center(tgContext));
244 const Acts::Intersection3D isectFirst = intersectPlane(seedPos, seedDir, planeNormal,
247 if (reFitMe->nPhiLayers() < 1) {
249 const Acts::Intersection3D isectExit = intersectPlane(seedPos, seedDir, planeNormal,
250 exitPortal->center(tgContext));
251 const Acts::Intersection3D isectLast = intersectPlane(seedPos, seedDir, planeNormal,
256 const Acts::Transform3 trfBeneath =
Amg::toIsometry3D(GeoTrf::GeoTransformRT{sectorAngles, 0.5*isectFirst.position() +
257 0.5*isectEntrance.position()});
258 const Acts::Transform3 trfAbove =
Amg::toIsometry3D(GeoTrf::GeoTransformRT{sectorAngles, 0.85*isectExit.position() +
259 0.15*isectLast.position() });
261 auto surfBeneath = Acts::Surface::makeShared<Acts::PlaneSurface>(trfBeneath);
262 auto surfAbove = Acts::Surface::makeShared<Acts::PlaneSurface>(trfAbove);
266 constexpr double covVal = Acts::square(1._cm);
267 hitsToFit.push_back(auxMeasHandle.newMeasurement<1>(surfBeneath, ProjectorType::e1DimNoTime,
AmgSymMatrix(1){covVal}));
271 hitsToFit.push_back(auxMeasHandle.newMeasurement<1>(surfAbove, ProjectorType::e1DimNoTime,
AmgSymMatrix(1){covVal}));
278 Acts::GeometryView3D::drawSurface(visualHelper, *entrancePortal, tgContext,
279 Acts::Transform3::Identity(), Acts::s_viewPortal);
280 Acts::GeometryView3D::drawSurface(visualHelper, *exitPortal, tgContext,
281 Acts::Transform3::Identity(), Acts::s_viewPortal);
285 Acts::ViewConfig{.color = {220, 0, 0}});
289 <<
", path length: "<<isectEntrance.pathLength()<<
", "
290 <<
" - First surface position: "<<
Amg::toString(isectFirst.position())
291 <<
", path length: "<<isectFirst.pathLength());
295 + 0.15 * isectFirst.position();
296 const Acts::Transform3 trf =
Amg::toIsometry3D(GeoTrf::GeoTransformRT{sectorAngles, refPos});
297 if (
msgLvl(MSG::VERBOSE)) {
300 std::stringstream sstr{};
302 <<reFitMe->chiSquared() / reFitMe->numberDoF()<<
", nDoF: "<<reFitMe->numberDoF()
303 <<
", prec: "<<reFitMe->nPrecisionHits()<<
", phi: "<<reFitMe->nPhiLayers()<<std::endl;
305 pullCalculator.updateSpatialResidual(line, *meas);
306 const Acts::Surface* surface = meas->spacePoint() ?
m_surfAccessor.get(meas->spacePoint()->primaryMeasurement()) :
nullptr;
307 const Acts::GeometryIdentifier geoId = surface ? surface->geometryId() : Acts::GeometryIdentifier{};
308 sstr<<
" **** "<<(*meas)<<
", chi2: "<<SeedingAux::chi2Term(locPos, locDir, *meas)
309 <<
", sign: "<<(meas->isStraw() ?
310 (SeedingAux::strawSign(locPos,locDir, *meas) == 1 ?
"R" :
"L") :
"X")
311 <<
", geoId: "<<geoId;
312 if (geoId != Acts::GeometryIdentifier{}){
313 const Amg::Vector3D globPos = meas->spacePoint()->msSector()->localToGlobalTransform(tgContext) *
314 meas->localPosition();
315 const Acts::GeometryIdentifier volId = geoId.withSensitive(0);
317 const Acts::Vector2 lPos = (*surface->globalToLocal(tgContext,globPos, reFitMe->direction()));
318 sstr<<
", inside volume: "<<volume->inside(tgContext, globPos);
319 sstr<<
", inside surface: "<<surface->bounds().inside(lPos);
327 <<
", refPoint: "<<
Amg::toString(trf.inverse()*refPos)<<std::endl;
331 std::shared_ptr<const Acts::Surface> target{};
334 using namespace Acts::detail::LineHelper;
335 const Acts::Intersection3D lineIsect =
336 lineIntersect<3>(target->center(tgContext),
337 Amg::Vector3D::UnitZ(),
341 refPos = lineIsect.position();
344 target = Acts::Surface::makeShared<Acts::PlaneSurface>(trf,
345 std::make_unique<Acts::RectangleBounds>(1._m, 1._m));
348 Acts::GeometryView3D::drawSurface(visualHelper, *target,
349 tgContext, Acts::Transform3::Identity(),
350 Acts::ViewConfig{.color={0,0,220}});
354 Acts::BoundMatrix initialCov{Acts::BoundMatrix::Identity()};
356 auto initialPars = Acts::BoundTrackParameters::create(tgContext, target, fourPos, seedDir, straightQoverP,
357 initialCov, Acts::ParticleHypothesis::muon());
358 if (!initialPars.ok()) {
360 saveDisplay(
"invalidPars");
365 Acts::ViewConfig{.color={0,220,0}});
368 <<
", "<<std::format(
"theta: {:.2f}, ", (*initialPars).theta() / 1._degree)
369 <<std::format(
"phi: {:.2f}", (*initialPars).phi() / 1._degree));
371 std::vector<Acts::SourceLink> sourceLinks{};
372 std::ranges::transform(hitsToFit, std::back_inserter(sourceLinks),
376 fitOptions.referenceSurface = target.get();
378 auto fitTraject =
m_fitter->fit(sourceLinks.begin(), sourceLinks.end(),
379 *initialPars, fitOptions, tracks);
380 if (!fitTraject.ok()) {
381 saveDisplay(
"failed");
386 ATH_MSG_ERROR(
"Segment refit failed. Albeit start parameters are taken from segment");
387 return StatusCode::FAILURE;
391 auto track = *fitTraject;
394 Acts::BoundTrackParameters parameters = track.createParametersAtReference();
397 Acts::ViewConfig{.color={0,220,220}});
400 saveDisplay(
"goodone");
402 std::uint8_t nPrecHits{0}, nTrigEtaHits{0}, nTrigPhiHits{0};
404 std::vector<const xAOD::UncalibratedMeasurement*> goodMeas{};
406 for (
const auto state :track.trackStatesReversed()) {
407 if (!state.hasUncalibratedSourceLink()){
410 goodMeas.insert(goodMeas.begin(),
419 nTrigPhiHits += m->numDimensions() == 2;
425 assert(M !=
nullptr);
427 nTrigEtaHits += (M->numDimensions() == 2 || !M->measuresPhi());
428 nTrigPhiHits += (M->numDimensions() == 2 || M->measuresPhi());
436 const Acts::Transform3 globToLoc{sectorTrf.inverse()};
437 const Amg::Vector3D refitPos = globToLoc * parameters.position(tgContext);
443 xAOD::MuonSegment* newSegment = outHandle->push_back(std::make_unique<xAOD::MuonSegment>());
444 dec_segLink(*newSegment) =
Link_t{*segments, reFitMe->index(), ctx};
446 newSegment->
setDirection(globDir.x(), globDir.y(), globDir.z());
447 newSegment->
setPosition(globPos.x(), globPos.y(), globPos.z());
450 newSegment->
setNHits(nPrecHits, nTrigPhiHits, nTrigEtaHits);
451 newSegment->
setIdentifier(reFitMe->sector(), reFitMe->chamberIndex(),
452 reFitMe->etaIndex(), reFitMe->technology());
454 auto& locFitPars = dec_locPars(*newSegment);
455 locFitPars[Acts::toUnderlying(ParamDefs::x0)] = refitSeg.x();
456 locFitPars[Acts::toUnderlying(ParamDefs::y0)] = refitSeg.y();
457 locFitPars[Acts::toUnderlying(ParamDefs::theta)] = refitDir.theta();
458 locFitPars[Acts::toUnderlying(ParamDefs::phi)] = refitDir.phi();
460 return StatusCode::SUCCESS;