CMS 3D CMS Logo

TrackTransformer.cc
Go to the documentation of this file.
2 
7 
10 
14 
21 
25 
27 
28 using namespace std;
29 using namespace edm;
30 
33  : theRPCInTheFit(parameterSet.getParameter<bool>("RefitRPCHits")),
34  theDoPredictionsOnly(parameterSet.getParameter<bool>("DoPredictionsOnly")),
35  theRefitDirection(parameterSet.getParameter<string>("RefitDirection")),
36  theFitterName(parameterSet.getParameter<string>("Fitter")),
37  theSmootherName(parameterSet.getParameter<string>("Smoother")),
38  thePropagatorName(parameterSet.getParameter<string>("Propagator")),
39  theTrackerRecHitBuilderName(parameterSet.getParameter<string>("TrackerRecHitBuilder")),
40  theMuonRecHitBuilderName(parameterSet.getParameter<string>("MuonRecHitBuilder")),
41  theMTDRecHitBuilderName(parameterSet.getParameter<string>("MTDRecHitBuilder")) {
50 }
51 
54 
56  bool doPredictionsOnly,
57  const std::string& fitter,
58  const std::string& smoother,
59  const std::string& propagator,
60  const std::string& refitDirection,
61  bool refitRPCHits,
62  const std::string& trackerRecHitBuilder,
63  const std::string& muonRecHitBuilder,
64  const std::string& mtdRecHitBuilder) {
65  desc.add<bool>("DoPredictionsOnly", doPredictionsOnly);
66  desc.add<std::string>("Fitter", fitter);
67  desc.add<std::string>("Smoother", smoother);
68  desc.add<std::string>("Propagator", propagator);
69  desc.add<std::string>("RefitDirection", refitDirection);
70  desc.add<bool>("RefitRPCHits", refitRPCHits);
71  desc.add<std::string>("TrackerRecHitBuilder", trackerRecHitBuilder);
72  desc.add<std::string>("MuonRecHitBuilder", muonRecHitBuilder);
73  desc.add<std::string>("MTDRecHitBuilder", mtdRecHitBuilder);
74 }
75 
77  const std::string metname = "Reco|TrackingTools|TrackTransformer";
78 
79  theFitter = setup.getData(theFitterToken).clone();
80  theSmoother.reset(setup.getData(theSmootherToken).clone());
81 
83 
84  // Global Tracking Geometry
86 
87  // Magfield Field
88  theMGField = setup.getHandle(theMGFieldToken);
89 
90  // Transient Rechit Builders
91  unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
92  if (newCacheId_TRH != theCacheId_TRH) {
93  theCacheId_TRH = newCacheId_TRH;
94  LogTrace(metname) << "TransientRecHitRecord changed!";
100  }
101  theFitter->setHitCloner(&hitCloner);
102  theSmoother->setHitCloner(&hitCloner);
103 }
104 
105 vector<Trajectory> TrackTransformer::transform(const reco::TrackRef& track) const { return transform(*track); }
106 
108  const reco::TransientTrack& track) const {
110  auto tkbuilder = static_cast<TkTransientTrackingRecHitBuilder const*>(theTrackerRecHitBuilder);
111 
112  for (auto hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit) {
113  if ((*hit)->isValid()) {
114  if ((*hit)->geographicalId().det() == DetId::Tracker) {
115  result.emplace_back((**hit).cloneForFit(*tkbuilder->geometry()->idToDet((**hit).geographicalId())));
116  } else if ((*hit)->geographicalId().det() == DetId::Muon) {
117  if ((*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit) {
118  LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
119  continue;
120  }
121  result.push_back(theMuonRecHitBuilder->build(&**hit));
122  } else if ((*hit)->geographicalId().det() == DetId::Forward && (*hit)->geographicalId().subdetId() == FastTime) {
123  if (theMtdAvailable)
124  result.push_back(theMTDRecHitBuilder->build(&**hit));
125  else
126  throw cms::Exception("TrackTransformer") << "MTD hit encountered but MTD not available!";
127  }
128  }
129  }
130 
131  return result;
132 }
133 
134 // FIXME: check this method!
137  if (!recHits.empty()) {
138  GlobalPoint first = trackingGeometry()->idToDet(recHits.front()->geographicalId())->position();
139  GlobalPoint last = trackingGeometry()->idToDet(recHits.back()->geographicalId())->position();
140 
141  // maybe perp2?
142  auto rFirst = first.mag2();
143  auto rLast = last.mag2();
144  if (rFirst < rLast)
146  if (rFirst > rLast)
148  }
149  LogDebug("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" << endl;
151 }
152 
154 vector<Trajectory> TrackTransformer::transform(const reco::Track& newTrack) const {
155  const std::string metname = "Reco|TrackingTools|TrackTransformer";
156 
158 
159  auto recHitsForReFit = getTransientRecHits(track);
160  return transform(track, recHitsForReFit);
161 }
162 
165  TransientTrackingRecHit::ConstRecHitContainer& recHitsForReFit) const {
166  const std::string metname = "Reco|TrackingTools|TrackTransformer";
167 
168  if (recHitsForReFit.size() < 2)
169  return vector<Trajectory>();
170 
171  // 8 cases are foreseen:
172  // [RH = rec hit order, P = momentum dir, FD = fit direction. IO/OI = inside-out/outside-in, AM/OM = along momentum/opposite to momentum]
173  // (1) RH IO | P IO | FD AM ---> Start from IN
174  // (2) RH IO | P IO | FD OM ---> Reverse RH and start from OUT
175  // (3) RH IO | P OI | FD AM ---> Reverse RH and start from IN
176  // (4) RH IO | P OI | FD OM ---> Start from OUT
177  // (5) RH OI | P IO | FD AM ---> Reverse RH and start from IN
178  // (6) RH OI | P IO | FD OM ---> Start from OUT
179  // (7) RH OI | P OI | FD AM ---> Start from IN
180  // (8) RH OI | P OI | FD OM ---> Reverse RH and start from OUT
181  //
182  // *** Rules: ***
183  // -A- If RH-FD agree (IO-AM,OI-OM) do not reverse the RH
184  // -B- If FD along momentum start from innermost state, otherwise use outermost
185 
186  // Other special cases can be handled:
187  // (1 bis) RH IO | P IO | GFD IO => FD AM ---> Start from IN
188  // (2 bis) RH IO | P IO | GFD OI => FD OM ---> Reverse RH and start from OUT
189  // (3 bis) RH IO | P OI | GFD OI => FD AM ---> Reverse RH and start from OUT
190  // (4 bis) RH IO | P OI | GFD IO => FD OM ---> Start from IN
191  // (5 bis) RH OI | P IO | GFD IO => FD AM ---> Reverse RH and start from IN
192  // (6 bis) RH OI | P IO | GFD OI => FD OM ---> Start from OUT
193  // (7 bis) RH OI | P OI | GFD OI => FD AM ---> Start from OUT
194  // (8 bis) RH OI | P OI | GFD IO => FD OM ---> Reverse RH and start from IN
195  //
196  // *** Additional rule: ***
197  // -A0- If P and GFD agree, then FD is AM otherwise is OM
198  // -A00- rechit must be ordered as GFD in order to handle the case of cosmics
199  // -B0- The starting state is decided by GFD
200 
201  // Determine the RH order
203  checkRecHitsOrdering(recHitsForReFit); // FIXME change nome of the *type* --> RecHit order!
204  LogTrace(metname) << "RH order (0-insideOut, 1-outsideIn): " << recHitsOrder;
205 
207 
208  // Apply rule -A0-
209  if (propagationDirection == anyDirection) {
210  GlobalVector momentum = track.innermostMeasurementState().globalMomentum();
211  GlobalVector position = track.innermostMeasurementState().globalPosition() - GlobalPoint(0, 0, 0);
212  RefitDirection::GeometricalDirection p = (momentum.x() * position.x() > 0 || momentum.y() * position.y() > 0)
215 
217  LogTrace(metname) << "P (0-insideOut, 1-outsideIn): " << p;
218  LogTrace(metname) << "FD (0-OM, 1-AM, 2-ANY): " << propagationDirection;
219  }
220  // -A0-
221 
222  // Apply rule -A-
224  if ((recHitsOrder == RefitDirection::insideOut && propagationDirection == oppositeToMomentum) ||
225  (recHitsOrder == RefitDirection::outsideIn && propagationDirection == alongMomentum))
226  reverse(recHitsForReFit.begin(), recHitsForReFit.end());
227  }
228  // -A-
229  // Apply rule -A00-
230  else {
231  // reorder the rechit as defined in theRefitDirection.geometricalDirection();
232  if (theRefitDirection.geometricalDirection() != recHitsOrder)
233  reverse(recHitsForReFit.begin(), recHitsForReFit.end());
234  }
235  // -A00-
236 
237  // Apply rule -B-
238  TrajectoryStateOnSurface firstTSOS = track.innermostMeasurementState();
239  unsigned int innerId = track.track().innerDetId();
241  if (propagationDirection == oppositeToMomentum) {
242  innerId = track.track().outerDetId();
243  firstTSOS = track.outermostMeasurementState();
244  }
245  } else { // if(theRefitDirection.propagationDirection() == anyDirection)
246  // Apply rule -B0-
248  innerId = track.track().outerDetId();
249  firstTSOS = track.outermostMeasurementState();
250  }
251  // -B0-
252  }
253  // -B-
254 
255  if (!firstTSOS.isValid()) {
256  LogTrace(metname) << "Error wrong initial state!" << endl;
257  return vector<Trajectory>();
258  }
259 
260  TrajectorySeed seed({}, {}, propagationDirection);
261 
262  if (recHitsForReFit.front()->geographicalId() != DetId(innerId)) {
263  LogTrace(metname) << "Propagation occured" << endl;
264  firstTSOS = propagator()->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
265  if (!firstTSOS.isValid()) {
266  LogTrace(metname) << "Propagation error!" << endl;
267  return vector<Trajectory>();
268  }
269  }
270 
271  if (theDoPredictionsOnly) {
272  Trajectory aTraj(seed, propagationDirection);
273  TrajectoryStateOnSurface predTSOS = firstTSOS;
274  for (auto const& hit : recHitsForReFit) {
275  predTSOS = propagator()->propagate(predTSOS, hit->det()->surface());
276  if (predTSOS.isValid())
277  aTraj.push(TrajectoryMeasurement(predTSOS, hit));
278  }
279  return vector<Trajectory>(1, aTraj);
280  }
281 
282  auto const& trajectories = theFitter->fit(seed, recHitsForReFit, firstTSOS);
283 
284  if (trajectories.empty()) {
285  LogTrace(metname) << "No Track refitted!" << endl;
286  return trajectories;
287  }
288 
289  auto const& trajectoryBW = trajectories.front();
290 
291  auto const& trajectoriesSM = theSmoother->trajectories(trajectoryBW);
292 
293  if (trajectoriesSM.empty()) {
294  LogTrace(metname) << "No Track smoothed!" << endl;
295  }
296 
297  return trajectoriesSM;
298 }
TransientTrackingRecHit::ConstRecHitContainer getTransientRecHits(const reco::TransientTrack &track) const
const std::string theTrackerRecHitBuilderName
const TransientTrackingRecHitBuilder * theTrackerRecHitBuilder
void setServices(const edm::EventSetup &) override
set the services needed by the TrackTransformer
RefitDirection::GeometricalDirection checkRecHitsOrdering(TransientTrackingRecHit::ConstRecHitContainer const &) const
const std::string metname
const RefitDirection theRefitDirection
const bool theRPCInTheFit
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
const MagneticField * magneticField() const
the magnetic field
std::unique_ptr< TrajectorySmoother > const & smoother() const
the smoother used to smooth the trajectory which came from the refitting step
PropagationDirection
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > theTrackerRecHitBuilderToken
const SurfaceType & surface() const
ParameterSet const & parameterSet(StableProvenance const &provenance, ProcessHistory const &history)
Definition: Provenance.cc:11
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
const GeomDet * idToDet(DetId) const override
unsigned long long theCacheId_TRH
#define LogTrace(id)
edm::ESHandle< GlobalTrackingGeometry > theTrackingGeometry
edm::ESHandle< TransientTrackingRecHitBuilder > theMTDRecHitBuilder
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
edm::ESHandle< Propagator > thePropagator
edm::ESHandle< TransientTrackingRecHitBuilder > theMuonRecHitBuilder
std::unique_ptr< TrajectoryFitter > theFitter
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
const std::string thePropagatorName
PropagationDirection propagationDirection() const
const std::string theFitterName
edm::ESHandle< MagneticField > theMGField
edm::ESGetToken< TrajectoryFitter, TrajectoryFitterRecord > theFitterToken
const std::string theMuonRecHitBuilderName
std::vector< ConstRecHitPointer > ConstRecHitContainer
bool isValid() const
Definition: ESHandle.h:44
TkClonerImpl hitCloner
Definition: DetId.h:17
edm::ESGetToken< Propagator, TrackingComponentsRecord > thePropagatorToken
edm::ESHandle< GlobalTrackingGeometry > trackingGeometry() const
the tracking geometry
const std::string theSmootherName
TrackTransformer(const edm::ParameterSet &, edm::ConsumesCollector &)
Constructor (for modules migrated to ES-consumes)
edm::ESGetToken< TrajectorySmoother, TrajectoryFitterRecord > theSmootherToken
static void fillPSetDescription(edm::ParameterSetDescription &descriptions, bool doPredictionsOnly=false, const std::string &fitter="KFFitterForRefitInsideOut", const std::string &smoother="KFSmootherForRefitInsideOut", const std::string &propagator="SmartPropagatorAnyRK", const std::string &refitDirection="alongMomentum", bool refitRPCHits=true, const std::string &trackerRecHitBuilder="WithTrackAngle", const std::string &muonRecHitBuilder="MuonRecHitBuilder", const std::string &mtdRecHitBuilder="MTDRecHitBuilder")
fillDescriptions
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > theMTDRecHitBuilderToken
const std::string theMTDRecHitBuilderName
const bool theDoPredictionsOnly
HLT enums.
static int position[264][3]
Definition: ReadPGInfo.cc:289
GeometricalDirection geometricalDirection() const
edm::ESHandle< Propagator > const & propagator() const
~TrackTransformer() override
Destructor.
std::vector< Trajectory > transform(const reco::Track &) const override
Convert a reco::Track into Trajectory.
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > theMGFieldToken
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
std::unique_ptr< TrajectorySmoother > theSmoother
#define LogDebug(id)
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > theMuonRecHitBuilderToken
edm::ESGetToken< GlobalTrackingGeometry, GlobalTrackingGeometryRecord > theTrackingGeometryToken