Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
#include "FastSimulation/Muons/plugins/FastTSGFromPropagation.h"

#include <memory>

/** \class FastTSGFromPropagation
 *
 *  Emulate TSGFromPropagation in RecoMuon
 *
 *  \author Hwidong Yoo - Purdue University 
 */

#include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
#include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
#include "TrackingTools/PatternTools/interface/Trajectory.h"
#include "TrackingTools/MeasurementDet/interface/LayerMeasurements.h"
#include "TrackingTools/MeasurementDet/interface/MeasurementDet.h"
#include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
#include "TrackingTools/GeomPropagators/interface/Propagator.h"
#include "TrackingTools/GeomPropagators/interface/StateOnTrackerBound.h"

#include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
#include "RecoTracker/Record/interface/CkfComponentsRecord.h"
// #include "RecoTracker/MeasurementDet/interface/TkStripMeasurementDet.h"
#include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
#include "RecoTracker/MeasurementDet/interface/MeasurementTrackerEvent.h"
#include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"

#include "RecoMuon/GlobalTrackingTools/interface/DirectTrackerNavigation.h"
#include "TrackingTools/KalmanUpdators/interface/KFUpdator.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"

#include "SimDataFormats/Track/interface/SimTrackContainer.h"
#include "FastSimulation/Tracking/interface/TrajectorySeedHitCandidate.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
#include "Geometry/CommonDetUnit/interface/GeomDet.h"
#include "TrackingTools/TransientTrackingRecHit/interface/GenericTransientTrackingRecHit.h"
#include "TrackingTools/Records/interface/TransientRecHitRecord.h"
#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"

#include "FastSimulation/Tracking/interface/FastTrackingUtilities.h"

using namespace std;

FastTSGFromPropagation::FastTSGFromPropagation(const edm::ParameterSet& iConfig, edm::ConsumesCollector& iC)
    : FastTSGFromPropagation(iConfig, nullptr, iC) {}

FastTSGFromPropagation::FastTSGFromPropagation(const edm::ParameterSet& iConfig,
                                               const MuonServiceProxy* service,
                                               edm::ConsumesCollector& iC)
    : theCategory("FastSimulation|Muons|FastTSGFromPropagation"),
      theNavigation(),
      theService(service),
      theUpdator(),
      theEstimator(),
      theSigmaZ(0.0),
      theConfig(iConfig),
      theSimTrackCollectionToken_(
          iC.consumes<edm::SimTrackContainer>(theConfig.getParameter<edm::InputTag>("SimTrackCollectionLabel"))),
      recHitCombinationsToken_(
          iC.consumes<FastTrackerRecHitCombinationCollection>(theConfig.getParameter<edm::InputTag>("HitProducer"))),
      beamSpot_(iC.consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
      theMeasurementTrackerEventToken_(
          iC.consumes<MeasurementTrackerEvent>(iConfig.getParameter<edm::InputTag>("MeasurementTrackerEvent"))),
      theGeometryToken(iC.esConsumes<edm::Transition::BeginRun>()),
      theTTRHBuilderToken(iC.esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "WithTrackAngle"))),
      theTrackerToken(iC.esConsumes()) {}

FastTSGFromPropagation::~FastTSGFromPropagation() { LogTrace(theCategory) << " FastTSGFromPropagation dtor called "; }

void FastTSGFromPropagation::trackerSeeds(const TrackCand& staMuon,
                                          const TrackingRegion& region,
                                          const TrackerTopology* tTopo,
                                          std::vector<TrajectorySeed>& result) {
  if (theResetMethod == "discrete")
    getRescalingFactor(staMuon);

  TrajectoryStateOnSurface staState = outerTkState(staMuon);

  if (!staState.isValid()) {
    LogTrace(theCategory) << "Error: initial state from L2 muon is invalid.";
    return;
  }

  LogTrace(theCategory) << "begin of trackerSeed:\n staState pos: " << staState.globalPosition()
                        << " mom: " << staState.globalMomentum() << "pos eta: " << staState.globalPosition().eta()
                        << "mom eta: " << staState.globalMomentum().eta();

  std::vector<const DetLayer*> nls = theNavigation->compatibleLayers(*(staState.freeState()), oppositeToMomentum);

  LogTrace(theCategory) << " compatible layers: " << nls.size();

  if (nls.empty())
    return;

  int ndesLayer = 0;

  bool usePredictedState = false;

  if (theUpdateStateFlag) {  //use updated states
    std::vector<TrajectoryMeasurement> alltm;

    for (std::vector<const DetLayer*>::const_iterator inl = nls.begin(); inl != nls.end(); inl++, ndesLayer++) {
      if ((*inl == nullptr))
        break;
      //         if ( (inl != nls.end()-1 ) && ( (*inl)->subDetector() == GeomDetEnumerators::TEC ) && ( (*(inl+1))->subDetector() == GeomDetEnumerators::TOB ) ) continue;
      alltm = findMeasurements(*inl, staState);
      if ((!alltm.empty())) {
        LogTrace(theCategory) << "final compatible layer: " << ndesLayer;
        break;
      }
    }

    if (alltm.empty()) {
      LogTrace(theCategory) << " NO Measurements Found: eta: " << staState.globalPosition().eta() << "pt "
                            << staState.globalMomentum().perp();
      usePredictedState = true;
    } else {
      LogTrace(theCategory) << " Measurements for seeds: " << alltm.size();
      std::stable_sort(alltm.begin(), alltm.end(), increasingEstimate());
      if (alltm.size() > 5)
        alltm.erase(alltm.begin() + 5, alltm.end());

      const edm::SimTrackContainer* simTracks = &(*theSimTracks);
      TrajectorySeedHitCandidate theSeedHits;
      std::vector<TrajectorySeedHitCandidate> outerHits;

      //std::vector<TrajectorySeed>  tmpTS;
      bool isMatch = false;
      for (std::vector<TrajectoryMeasurement>::const_iterator itm = alltm.begin(); itm != alltm.end(); itm++) {
        const TrajectoryStateOnSurface seedState = itm->predictedState();
        double preY = seedState.globalPosition().y();

        // Check SimTrack
        FreeTrajectoryState simtrack_trackerstate;
        for (unsigned icomb = 0; icomb < recHitCombinations->size(); ++icomb) {
          const auto& recHitCombination = (*recHitCombinations)[icomb];
          if (recHitCombination.empty())
            continue;
          int32_t simTrackId = recHitCombination.back()->simTrackId(0);
          const SimTrack& simtrack = (*simTracks)[simTrackId];

          GlobalPoint position(simtrack.trackerSurfacePosition().x(),
                               simtrack.trackerSurfacePosition().y(),
                               simtrack.trackerSurfacePosition().z());
          GlobalVector momentum(simtrack.trackerSurfaceMomentum().x(),
                                simtrack.trackerSurfaceMomentum().y(),
                                simtrack.trackerSurfaceMomentum().z());
          int charge = (int)simtrack.charge();
          GlobalTrajectoryParameters glb_parameters(
              position, momentum, charge, &*theService->magneticField().product());
          simtrack_trackerstate = FreeTrajectoryState(glb_parameters);

          unsigned int outerId = 0;
          for (const auto& recHitRef : recHitCombination) {
            theSeedHits = TrajectorySeedHitCandidate(recHitRef.get(), tTopo);
            unsigned int id = theSeedHits.hit()->geographicalId().rawId();
            if (preY < 0) {
              if (id > outerId)
                outerId = id;
            } else {
              if (id > outerId)
                outerId = id;
            }
          }
          for (const auto& recHitRef : recHitCombination) {
            theSeedHits = TrajectorySeedHitCandidate(recHitRef.get(), tTopo);
            if (itm->recHit()->hit()->geographicalId().rawId() == theSeedHits.hit()->geographicalId().rawId()) {
              auto aTrackingRecHit = std::unique_ptr<TrackingRecHit>(theSeedHits.hit()->clone());
              TransientTrackingRecHit::ConstRecHitPointer recHit = theTTRHBuilder->build(aTrackingRecHit.get());
              if (!recHit)
                continue;
              TrajectoryStateOnSurface updatedTSOS = updator()->update(seedState, *(recHit));
              if (updatedTSOS.isValid() && passSelection(updatedTSOS)) {
                edm::OwnVector<TrackingRecHit> container;
                container.push_back(recHit->hit()->clone());
                fastTrackingUtilities::setRecHitCombinationIndex(container, icomb);
                TrajectorySeed ts = createSeed(updatedTSOS, container, recHit->geographicalId());
                // check direction
                const TrajectorySeed* aSeed = &ts;
                PTrajectoryStateOnDet PTSOD = aSeed->startingState();

                const GeomDet* g = theGeometry->idToDet(PTSOD.detId());
                TrajectoryStateOnSurface tsos = trajectoryStateTransform::transientState(
                    PTSOD, &(g->surface()), &*theService->magneticField().product());
                if (tsos.globalMomentum().basicVector() * seedState.globalMomentum().basicVector() < 0.)
                  continue;
                result.push_back(ts);
                isMatch = true;
              }
            }
          }
        }
      }
      if (!isMatch) {
        // if there is no hits w.r.t. TM, find outermost hit
        for (std::vector<TrajectoryMeasurement>::const_iterator itm = alltm.begin(); itm != alltm.end(); itm++) {
          const TrajectoryStateOnSurface seedState = itm->predictedState();
          double preY = seedState.globalPosition().y();

          // Check SimTrack
          TrackingRecHit* aTrackingRecHit;
          FreeTrajectoryState simtrack_trackerstate;

          for (unsigned icomb = 0; icomb < recHitCombinations->size(); ++icomb) {
            const auto& recHitCombination = (*recHitCombinations)[icomb];
            if (recHitCombination.empty())
              continue;
            int32_t simTrackId = recHitCombination.back()->simTrackId(0);
            const SimTrack& simtrack = (*simTracks)[simTrackId];

            GlobalPoint position(simtrack.trackerSurfacePosition().x(),
                                 simtrack.trackerSurfacePosition().y(),
                                 simtrack.trackerSurfacePosition().z());
            GlobalVector momentum(simtrack.trackerSurfaceMomentum().x(),
                                  simtrack.trackerSurfaceMomentum().y(),
                                  simtrack.trackerSurfaceMomentum().z());
            int charge = (int)simtrack.charge();
            GlobalTrajectoryParameters glb_parameters(
                position, momentum, charge, &*theService->magneticField().product());
            simtrack_trackerstate = FreeTrajectoryState(glb_parameters);

            unsigned int outerId = 0;
            for (const auto& recHitRef : recHitCombination) {
              theSeedHits = TrajectorySeedHitCandidate(recHitRef.get(), tTopo);
              unsigned int id = theSeedHits.hit()->geographicalId().rawId();
              if (preY < 0) {
                if (id > outerId)
                  outerId = id;
              } else {
                if (id > outerId)
                  outerId = id;
              }
            }
            for (const auto& recHitRef : recHitCombination) {
              theSeedHits = TrajectorySeedHitCandidate(recHitRef.get(), tTopo);
              if (outerId == theSeedHits.hit()->geographicalId().rawId()) {
                aTrackingRecHit = theSeedHits.hit()->clone();
                TransientTrackingRecHit::ConstRecHitPointer recHit = theTTRHBuilder->build(aTrackingRecHit);
                if (!recHit)
                  continue;
                TrajectoryStateOnSurface updatedTSOS = updator()->update(seedState, *(recHit));
                if (updatedTSOS.isValid() && passSelection(updatedTSOS)) {
                  edm::OwnVector<TrackingRecHit> container;
                  container.push_back(recHit->hit()->clone());
                  fastTrackingUtilities::setRecHitCombinationIndex(container, icomb);
                  TrajectorySeed ts = createSeed(updatedTSOS, container, recHit->geographicalId());
                  // check direction
                  const TrajectorySeed* aSeed = &ts;
                  PTrajectoryStateOnDet PTSOD = aSeed->startingState();

                  const GeomDet* g = theGeometry->idToDet(PTSOD.detId());
                  TrajectoryStateOnSurface tsos = trajectoryStateTransform::transientState(
                      PTSOD, &(g->surface()), &*theService->magneticField().product());
                  if (tsos.globalMomentum().basicVector() * seedState.globalMomentum().basicVector() < 0.)
                    continue;
                  result.push_back(ts);
                }
              }
            }
          }
        }
      }

      /*
       for( unsigned ir = 0; ir < tmpTS.size(); ir++ ) {
	 const BasicTrajectorySeed* aSeed = &((tmpTS)[ir]);
	 PTrajectoryStateOnDet PTSOD = aSeed->startingState();
	 
	 DetId seedDetId(PTSOD.detId());
	 const GeomDet * g = theGeometry->idToDet(seedDetId);
	 TrajectoryStateOnSurface tsos = trajectoryStateTransform::transientState(PTSOD, &(g->surface()),  &*theService->magneticField().product());
		 cout << "tsos3 = " << tsos.globalMomentum() << endl;
	 if( _index == ir ) {
		 cout << "tsos4 = " << tsos.globalMomentum() << endl;
	     result.push_back(tmpTS[ir]);
	 }
       }
       */
      LogTrace(theCategory) << "result: " << result.size();
      return;
    }
  }

  if (!theUpdateStateFlag || usePredictedState) {  //use predicted states
    LogTrace(theCategory) << "use predicted state: ";
    for (std::vector<const DetLayer*>::const_iterator inl = nls.begin(); inl != nls.end(); inl++) {
      if (!result.empty() || *inl == nullptr) {
        break;
      }
      std::vector<DetLayer::DetWithState> compatDets = (*inl)->compatibleDets(staState, *propagator(), *estimator());
      LogTrace(theCategory) << " compatDets " << compatDets.size();
      if (compatDets.empty())
        continue;
      TrajectorySeed ts = createSeed(compatDets.front().second, compatDets.front().first->geographicalId());
      result.push_back(ts);
    }
    LogTrace(theCategory) << "result: " << result.size();
    return;
  }
  return;
}

void FastTSGFromPropagation::init(const MuonServiceProxy* service) {
  theMaxChi2 = theConfig.getParameter<double>("MaxChi2");

  theFixedErrorRescaling = theConfig.getParameter<double>("ErrorRescaling");

  theFlexErrorRescaling = 1.0;

  theResetMethod = theConfig.getParameter<std::string>("ResetMethod");

  if (theResetMethod != "discrete" && theResetMethod != "fixed" && theResetMethod != "matrix") {
    edm::LogError("FastTSGFromPropagation") << "Wrong error rescaling method: " << theResetMethod << "\n"
                                            << "Possible choices are: discrete, fixed, matrix.\n"
                                            << "Use discrete method" << std::endl;
    theResetMethod = "discrete";
  }

  theEstimator = std::make_unique<Chi2MeasurementEstimator>(theMaxChi2);

  theCacheId_TG = 0;

  thePropagatorName = theConfig.getParameter<std::string>("Propagator");

  theService = service;

  theUseVertexStateFlag = theConfig.getParameter<bool>("UseVertexState");

  theUpdateStateFlag = theConfig.getParameter<bool>("UpdateState");

  theSelectStateFlag = theConfig.getParameter<bool>("SelectState");

  theUpdator = std::make_unique<KFUpdator>();

  theSigmaZ = theConfig.getParameter<double>("SigmaZ");

  edm::ParameterSet errorMatrixPset = theConfig.getParameter<edm::ParameterSet>("errorMatrixPset");
  if (theResetMethod == "matrix" && !errorMatrixPset.empty()) {
    theAdjustAtIp = errorMatrixPset.getParameter<bool>("atIP");
    theErrorMatrixAdjuster = std::make_unique<MuonErrorMatrix>(errorMatrixPset);
  } else {
    theAdjustAtIp = false;
    theErrorMatrixAdjuster.reset();
  }

  theGeometry = &theService->eventSetup().getData(theGeometryToken);

  theTTRHBuilder = theService->eventSetup().getHandle(theTTRHBuilderToken);
}

void FastTSGFromPropagation::setEvent(const edm::Event& iEvent) {
  iEvent.getByToken(beamSpot_, theBeamSpot);

  // retrieve the MC truth (SimTracks)
  iEvent.getByToken(theSimTrackCollectionToken_, theSimTracks);
  iEvent.getByToken(recHitCombinationsToken_, recHitCombinations);

  if (theUpdateStateFlag) {
    iEvent.getByToken(theMeasurementTrackerEventToken_, theMeasTrackerEvent);
  }

  unsigned long long newCacheId_TG = theService->eventSetup().get<TrackerRecoGeometryRecord>().cacheIdentifier();

  if (newCacheId_TG != theCacheId_TG) {
    LogTrace(theCategory) << "Tracker Reco Geometry changed!";
    theCacheId_TG = newCacheId_TG;
    auto theTracker = theService->eventSetup().getHandle(theTrackerToken);
    theNavigation = std::make_unique<DirectTrackerNavigation>(theTracker);
  }
}

TrajectoryStateOnSurface FastTSGFromPropagation::innerState(const TrackCand& staMuon) const {
  TrajectoryStateOnSurface innerTS;

  if (staMuon.first && staMuon.first->isValid()) {
    if (staMuon.first->direction() == alongMomentum) {
      innerTS = staMuon.first->firstMeasurement().updatedState();
    } else if (staMuon.first->direction() == oppositeToMomentum) {
      innerTS = staMuon.first->lastMeasurement().updatedState();
    }
  } else {
    innerTS = trajectoryStateTransform::innerStateOnSurface(
        *(staMuon.second), *theService->trackingGeometry(), &*theService->magneticField());
  }
  //rescale the error
  adjust(innerTS);

  return innerTS;
}

TrajectoryStateOnSurface FastTSGFromPropagation::outerTkState(const TrackCand& staMuon) const {
  TrajectoryStateOnSurface result;

  if (theUseVertexStateFlag && staMuon.second->pt() > 1.0) {
    FreeTrajectoryState iniState =
        trajectoryStateTransform::initialFreeState(*(staMuon.second), &*theService->magneticField());
    //rescale the error at IP
    adjust(iniState);

    StateOnTrackerBound fromInside(&*(theService->propagator("PropagatorWithMaterial")));
    result = fromInside(iniState);
  } else {
    StateOnTrackerBound fromOutside(&*propagator());
    result = fromOutside(innerState(staMuon));
  }
  return result;
}

TrajectorySeed FastTSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const DetId& id) const {
  edm::OwnVector<TrackingRecHit> container;
  return createSeed(tsos, container, id);
}

TrajectorySeed FastTSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos,
                                                  const edm::OwnVector<TrackingRecHit>& container,
                                                  const DetId& id) const {
  PTrajectoryStateOnDet seedTSOS = trajectoryStateTransform::persistentState(tsos, id.rawId());
  return TrajectorySeed(seedTSOS, container, oppositeToMomentum);
}

void FastTSGFromPropagation::validMeasurements(std::vector<TrajectoryMeasurement>& tms) const {
  std::vector<TrajectoryMeasurement>::iterator tmsend = std::remove_if(tms.begin(), tms.end(), isInvalid());
  tms.erase(tmsend, tms.end());
  return;
}

std::vector<TrajectoryMeasurement> FastTSGFromPropagation::findMeasurements(
    const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
  std::vector<TrajectoryMeasurement> result;

  std::vector<DetLayer::DetWithState> compatDets = nl->compatibleDets(staState, *propagator(), *estimator());
  if (compatDets.empty())
    return result;

  for (std::vector<DetLayer::DetWithState>::const_iterator idws = compatDets.begin(); idws != compatDets.end();
       ++idws) {
    if (idws->second.isValid() && (idws->first)) {
      std::vector<TrajectoryMeasurement> tmptm =
          theMeasTrackerEvent->idToDet(idws->first->geographicalId())
              .fastMeasurements(idws->second, idws->second, *propagator(), *estimator());
      //validMeasurements(tmptm);
      //         if ( tmptm.size() > 2 ) {
      //            std::stable_sort(tmptm.begin(),tmptm.end(),increasingEstimate());
      //            result.insert(result.end(),tmptm.begin(), tmptm.begin()+2);
      //         } else {
      result.insert(result.end(), tmptm.begin(), tmptm.end());
      //         }
    }
  }

  return result;
}

bool FastTSGFromPropagation::passSelection(const TrajectoryStateOnSurface& tsos) const {
  if (!theSelectStateFlag)
    return true;
  else {
    if (theBeamSpot.isValid()) {
      return ((fabs(zDis(tsos) - theBeamSpot->z0()) < theSigmaZ));

    } else {
      return ((fabs(zDis(tsos)) < theSigmaZ));
      //      double theDxyCut = 100;
      //      return ( (zDis(tsos) < theSigmaZ) && (dxyDis(tsos) < theDxyCut) );
    }
  }
}

double FastTSGFromPropagation::dxyDis(const TrajectoryStateOnSurface& tsos) const {
  return fabs(
      (-tsos.globalPosition().x() * tsos.globalMomentum().y() + tsos.globalPosition().y() * tsos.globalMomentum().x()) /
      tsos.globalMomentum().perp());
}

double FastTSGFromPropagation::zDis(const TrajectoryStateOnSurface& tsos) const {
  return tsos.globalPosition().z() -
         tsos.globalPosition().perp() * tsos.globalMomentum().z() / tsos.globalMomentum().perp();
}

void FastTSGFromPropagation::getRescalingFactor(const TrackCand& staMuon) {
  float pt = (staMuon.second)->pt();
  if (pt < 13.0)
    theFlexErrorRescaling = 3;
  else if (pt < 30.0)
    theFlexErrorRescaling = 5;
  else
    theFlexErrorRescaling = 10;
  return;
}

void FastTSGFromPropagation::adjust(FreeTrajectoryState& state) const {
  //rescale the error
  if (theResetMethod == "discreate") {
    state.rescaleError(theFlexErrorRescaling);
    return;
  }

  //rescale the error
  if (theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
    state.rescaleError(theFixedErrorRescaling);
    return;
  }

  CurvilinearTrajectoryError oMat = state.curvilinearError();
  CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.momentum());  //FIXME with position
  MuonErrorMatrix::multiply(oMat, sfMat);

  state = FreeTrajectoryState(state.parameters(), oMat);
}

void FastTSGFromPropagation::adjust(TrajectoryStateOnSurface& state) const {
  //rescale the error
  if (theResetMethod == "discreate") {
    state.rescaleError(theFlexErrorRescaling);
    return;
  }

  if (theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
    state.rescaleError(theFixedErrorRescaling);
    return;
  }

  CurvilinearTrajectoryError oMat = state.curvilinearError();
  CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.globalMomentum());  //FIXME with position
  MuonErrorMatrix::multiply(oMat, sfMat);

  state =
      TrajectoryStateOnSurface(state.weight(), state.globalParameters(), oMat, state.surface(), state.surfaceSide());
}

void FastTSGFromPropagation::stateOnDet(const TrajectoryStateOnSurface& ts,
                                        unsigned int detid,
                                        PTrajectoryStateOnDet& pts) const {
  const AlgebraicSymMatrix55& m = ts.localError().matrix();
  int dim = 5;  /// should check if corresponds to m
  float localErrors[15];
  int k = 0;
  for (int i = 0; i < dim; ++i) {
    for (int j = 0; j <= i; ++j) {
      localErrors[k++] = m(i, j);
    }
  }
  int surfaceSide = static_cast<int>(ts.surfaceSide());
  pts = PTrajectoryStateOnDet(ts.localParameters(), ts.globalMomentum().perp(), localErrors, detid, surfaceSide);
}