CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackTransformer.cc
Go to the documentation of this file.
2 
6 
9 
13 
20 
24 
25 using namespace std;
26 using namespace edm;
27 
30 
31  // Refit direction
32  string refitDirectionName = parameterSet.getParameter<string>("RefitDirection");
33  theRefitDirection = RefitDirection(refitDirectionName);
34 
35  theFitterName = parameterSet.getParameter<string>("Fitter");
36  theSmootherName = parameterSet.getParameter<string>("Smoother");
37  thePropagatorName = parameterSet.getParameter<string>("Propagator");
38 
39  theTrackerRecHitBuilderName = parameterSet.getParameter<string>("TrackerRecHitBuilder");
40  theMuonRecHitBuilderName = parameterSet.getParameter<string>("MuonRecHitBuilder");
41 
42  theRPCInTheFit = parameterSet.getParameter<bool>("RefitRPCHits");
43  theDoPredictionsOnly = parameterSet.getParameter<bool>("DoPredictionsOnly");
44 
45  theCacheId_TC = theCacheId_GTG = theCacheId_MG = theCacheId_TRH = 0;
46 }
47 
50 
51 
53 
54  const std::string metname = "Reco|TrackingTools|TrackTransformer";
55 
58  setup.get<TrajectoryFitter::Record>().get(theFitterName,aFitter);
59  setup.get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
60  theFitter = aFitter->clone();
61  theSmoother.reset(aSmoother->clone());
62 
63  unsigned long long newCacheId_TC = setup.get<TrackingComponentsRecord>().cacheIdentifier();
64 
65  if ( newCacheId_TC != theCacheId_TC ){
66  LogTrace(metname) << "Tracking Component changed!";
67  theCacheId_TC = newCacheId_TC;
68  setup.get<TrackingComponentsRecord>().get(thePropagatorName,thePropagator);
69  }
70 
71  // Global Tracking Geometry
72  unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
73  if ( newCacheId_GTG != theCacheId_GTG ) {
74  LogTrace(metname) << "GlobalTrackingGeometry changed!";
75  theCacheId_GTG = newCacheId_GTG;
76  setup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry);
77  }
78 
79  // Magfield Field
80  unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
81  if ( newCacheId_MG != theCacheId_MG ) {
82  LogTrace(metname) << "Magnetic Field changed!";
83  theCacheId_MG = newCacheId_MG;
84  setup.get<IdealMagneticFieldRecord>().get(theMGField);
85  }
86 
87  // Transient Rechit Builders
88  unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
89  if ( newCacheId_TRH != theCacheId_TRH ) {
90  theCacheId_TRH = newCacheId_TRH;
91  LogTrace(metname) << "TransientRecHitRecord changed!";
92  setup.get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
93  setup.get<TransientRecHitRecord>().get(theMuonRecHitBuilderName,theMuonRecHitBuilder);
94  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
95  }
96  theFitter->setHitCloner(&hitCloner);
97  theSmoother->setHitCloner(&hitCloner);
98 
99 }
100 
101 
102 vector<Trajectory> TrackTransformer::transform(const reco::TrackRef& track) const {
103  return transform(*track);
104 }
105 
106 
109 
111  auto tkbuilder = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product());
112 
113 
114  for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit) {
115  if((*hit)->isValid()) {
116  if ( (*hit)->geographicalId().det() == DetId::Tracker ) {
117  result.emplace_back((**hit).cloneForFit(*tkbuilder->geometry()->idToDet( (**hit).geographicalId() ) ) );
118  } else if ( (*hit)->geographicalId().det() == DetId::Muon ){
119  if( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit){
120  LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
121  continue;
122  }
123  result.push_back(theMuonRecHitBuilder->build(&**hit));
124  }
125  }
126  }
127 
128  return result;
129 }
130 
131 // FIXME: check this method!
134 
135  if (!recHits.empty()){
136  GlobalPoint first = trackingGeometry()->idToDet(recHits.front()->geographicalId())->position();
137  GlobalPoint last = trackingGeometry()->idToDet(recHits.back()->geographicalId())->position();
138 
139  double rFirst = first.mag();
140  double rLast = last.mag();
141  if(rFirst < rLast) return RefitDirection::insideOut;
142  else if(rFirst > rLast) return RefitDirection::outsideIn;
143  else{
144  LogDebug("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
146  }
147  }
148  else{
149  LogDebug("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
151  }
152 }
153 
154 // void reorder(TransientTrackingRecHit::ConstRecHitContainer& recHits, RefitDirection::GeometricalDirection recHitsOrder) const{
155 
156 // if(theRefitDirection.geometricalDirection() != recHitsOrder) reverse(recHits.begin(),recHits.end());
157 
158 // if(theRefitDirection.geometricalDirection() == RefitDirection::insideOut &&recHitsOrder){}
159 // else if(theRefitDirection.geometricalDirection() == RefitDirection::outsideIn){}
160 // else LogWarning("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
161 // }
162 
163 
165 vector<Trajectory> TrackTransformer::transform(const reco::Track& newTrack) const {
166 
167  const std::string metname = "Reco|TrackingTools|TrackTransformer";
168 
169  reco::TransientTrack track(newTrack,magneticField(),trackingGeometry());
170 
171  // Build the transient Rechits
172  TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = getTransientRecHits(track);
173 
174  return transform(track, recHitsForReFit);
175 }
176 
177 
179 vector<Trajectory> TrackTransformer::transform(const reco::TransientTrack& track,
180  const TransientTrackingRecHit::ConstRecHitContainer& _recHitsForReFit) const {
181 
182  TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = _recHitsForReFit;
183  const std::string metname = "Reco|TrackingTools|TrackTransformer";
184 
185  if(recHitsForReFit.size() < 2) return vector<Trajectory>();
186 
187  // 8 cases are foreseen:
188  // [RH = rec hit order, P = momentum dir, FD = fit direction. IO/OI = inside-out/outside-in, AM/OM = along momentum/opposite to momentum]
189  // (1) RH IO | P IO | FD AM ---> Start from IN
190  // (2) RH IO | P IO | FD OM ---> Reverse RH and start from OUT
191  // (3) RH IO | P OI | FD AM ---> Reverse RH and start from IN
192  // (4) RH IO | P OI | FD OM ---> Start from OUT
193  // (5) RH OI | P IO | FD AM ---> Reverse RH and start from IN
194  // (6) RH OI | P IO | FD OM ---> Start from OUT
195  // (7) RH OI | P OI | FD AM ---> Start from IN
196  // (8) RH OI | P OI | FD OM ---> Reverse RH and start from OUT
197  //
198  // *** Rules: ***
199  // -A- If RH-FD agree (IO-AM,OI-OM) do not reverse the RH
200  // -B- If FD along momentum start from innermost state, otherwise use outermost
201 
202  // Other special cases can be handled:
203  // (1 bis) RH IO | P IO | GFD IO => FD AM ---> Start from IN
204  // (2 bis) RH IO | P IO | GFD OI => FD OM ---> Reverse RH and start from OUT
205  // (3 bis) RH IO | P OI | GFD OI => FD AM ---> Reverse RH and start from OUT
206  // (4 bis) RH IO | P OI | GFD IO => FD OM ---> Start from IN
207  // (5 bis) RH OI | P IO | GFD IO => FD AM ---> Reverse RH and start from IN
208  // (6 bis) RH OI | P IO | GFD OI => FD OM ---> Start from OUT
209  // (7 bis) RH OI | P OI | GFD OI => FD AM ---> Start from OUT
210  // (8 bis) RH OI | P OI | GFD IO => FD OM ---> Reverse RH and start from IN
211  //
212  // *** Additional rule: ***
213  // -A0- If P and GFD agree, then FD is AM otherwise is OM
214  // -A00- rechit must be ordered as GFD in order to handle the case of cosmics
215  // -B0- The starting state is decided by GFD
216 
217  // Determine the RH order
218  RefitDirection::GeometricalDirection recHitsOrder = checkRecHitsOrdering(recHitsForReFit); // FIXME change nome of the *type* --> RecHit order!
219  LogTrace(metname) << "RH order (0-insideOut, 1-outsideIn): " << recHitsOrder;
220 
221  PropagationDirection propagationDirection = theRefitDirection.propagationDirection();
222 
223  // Apply rule -A0-
224  if(propagationDirection == anyDirection){
227  RefitDirection::GeometricalDirection p = (momentum.x()*position.x() > 0 || momentum.y()*position.y() > 0) ? RefitDirection::insideOut : RefitDirection::outsideIn;
228 
229  propagationDirection = p == theRefitDirection.geometricalDirection() ? alongMomentum : oppositeToMomentum;
230  LogTrace(metname) << "P (0-insideOut, 1-outsideIn): " << p;
231  LogTrace(metname) << "FD (0-OM, 1-AM, 2-ANY): " << propagationDirection;
232  }
233  // -A0-
234 
235  // Apply rule -A-
236  if(theRefitDirection.propagationDirection() != anyDirection){
237  if((recHitsOrder == RefitDirection::insideOut && propagationDirection == oppositeToMomentum) ||
238  (recHitsOrder == RefitDirection::outsideIn && propagationDirection == alongMomentum) )
239  reverse(recHitsForReFit.begin(),recHitsForReFit.end());}
240  // -A-
241  // Apply rule -A00-
242  else{
243  // reorder the rechit as defined in theRefitDirection.geometricalDirection();
244  if(theRefitDirection.geometricalDirection() != recHitsOrder) reverse(recHitsForReFit.begin(),recHitsForReFit.end());
245  }
246  // -A00-
247 
248  // Apply rule -B-
250  unsigned int innerId = track.track().innerDetId();
251  if(theRefitDirection.propagationDirection() != anyDirection){
252  if(propagationDirection == oppositeToMomentum){
253  innerId = track.track().outerDetId();
254  firstTSOS = track.outermostMeasurementState();
255  }
256  }
257  else { // if(theRefitDirection.propagationDirection() == anyDirection)
258  // Apply rule -B0-
259  if(theRefitDirection.geometricalDirection() == RefitDirection::outsideIn){
260  innerId = track.track().outerDetId();
261  firstTSOS = track.outermostMeasurementState();
262  }
263  // -B0-
264  }
265  // -B-
266 
267  if(!firstTSOS.isValid()){
268  LogTrace(metname)<<"Error wrong initial state!"<<endl;
269  return vector<Trajectory>();
270  }
271 
273 
274  if(recHitsForReFit.front()->geographicalId() != DetId(innerId)){
275  LogTrace(metname)<<"Propagation occured"<<endl;
276  firstTSOS = propagator()->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
277  if(!firstTSOS.isValid()){
278  LogTrace(metname)<<"Propagation error!"<<endl;
279  return vector<Trajectory>();
280  }
281  }
282 
283  if(theDoPredictionsOnly){
284  Trajectory aTraj(seed,propagationDirection);
285  TrajectoryStateOnSurface predTSOS = firstTSOS;
286  for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = recHitsForReFit.begin();
287  ihit != recHitsForReFit.end(); ++ihit ) {
288  predTSOS = propagator()->propagate(predTSOS, (*ihit)->det()->surface());
289  if (predTSOS.isValid()) aTraj.push(TrajectoryMeasurement(predTSOS, *ihit));
290  }
291  return vector<Trajectory>(1, aTraj);
292  }
293 
294 
295  vector<Trajectory> trajectories = theFitter->fit(seed,recHitsForReFit,firstTSOS);
296 
297  if(trajectories.empty()){
298  LogTrace(metname)<<"No Track refitted!"<<endl;
299  return vector<Trajectory>();
300  }
301 
302  Trajectory trajectoryBW = trajectories.front();
303 
304  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(trajectoryBW);
305 
306  if(trajectoriesSM.empty()){
307  LogTrace(metname)<<"No Track smoothed!"<<endl;
308  return vector<Trajectory>();
309  }
310 
311  return trajectoriesSM;
312 
313 }
314 
315 
#define LogDebug(id)
T getParameter(std::string const &) const
RefitDirection::GeometricalDirection checkRecHitsOrdering(TransientTrackingRecHit::ConstRecHitContainer &) const
const std::string metname
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
PropagationDirection
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
TrajectoryStateOnSurface innermostMeasurementState() const
TransientTrackingRecHit::ConstRecHitContainer getTransientRecHits(const reco::TransientTrack &track) const
const SurfaceType & surface() const
T mag() const
Definition: PV3DBase.h:67
tuple result
Definition: query.py:137
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
virtual void setServices(const edm::EventSetup &)
set the services needed by the TrackTransformer
TrajectoryStateOnSurface outermostMeasurementState() const
bool first
Definition: L1TdeRCT.cc:75
#define LogTrace(id)
trackingRecHit_iterator recHitsEnd() const
last iterator to RecHits
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:18
const Track & track() const
const T & get() const
Definition: EventSetup.h:55
GlobalVector globalMomentum() const
virtual std::vector< Trajectory > transform(const reco::Track &) const
Convert a reco::Track into Trajectory.
virtual ~TrackTransformer()
Destructor.
T x() const
Definition: PV3DBase.h:62
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:30
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
trackingRecHit_iterator recHitsBegin() const
first iterator to RecHits
ParameterSet const & parameterSet(Provenance const &provenance)
Definition: Provenance.cc:11
TrackTransformer(const edm::ParameterSet &)
Constructor.