CMS 3D CMS Logo

TrackProducerAlgorithm.cc
Go to the documentation of this file.
1 #include <sstream>
2 
29 
30 // #define VI_DEBUG
31 // #define STAT_TSB
32 
33 #ifdef VI_DEBUG
34 #define DPRINT(x) std::cout << x << ": "
35 #else
36 #define DPRINT(x) LogTrace(x)
37 #endif
38 
39 namespace {
40 #ifdef STAT_TSB
41  struct StatCount {
42  long long totTrack = 0;
43  long long totLoop = 0;
44  long long totGsfTrack = 0;
45  long long totFound = 0;
46  long long totLost = 0;
47  long long totAlgo[15];
48  void track(int l) {
49  if (l > 0)
50  ++totLoop;
51  else
52  ++totTrack;
53  }
54  void hits(int f, int l) {
55  totFound += f;
56  totLost += l;
57  }
58  void gsf() { ++totGsfTrack; }
59  void algo(int a) {
60  if (a >= 0 && a < 15)
61  ++totAlgo[a];
62  }
63 
64  void print() const {
65  std::cout << "TrackProducer stat\nTrack/Loop/Gsf/FoundHits/LostHits//algos " << totTrack << '/' << totLoop << '/'
66  << totGsfTrack << '/' << totFound << '/' << totLost << '/';
67  for (auto a : totAlgo)
68  std::cout << '/' << a;
69  std::cout << std::endl;
70  }
71  StatCount() {}
72  ~StatCount() { print(); }
73  };
74  StatCount statCount;
75 
76 #else
77  struct StatCount {
78  void track(int) {}
79  void hits(int, int) {}
80  void gsf() {}
81  void algo(int) {}
82  };
83  CMS_THREAD_SAFE StatCount statCount;
84 #endif
85 
86 } // namespace
87 
88 template <>
90  const Propagator* thePropagator,
91  AlgoProductCollection& algoResults,
93  TrajectoryStateOnSurface& theTSOS,
94  const TrajectorySeed& seed,
95  float ndof,
96  const reco::BeamSpot& bs,
97  SeedRef seedRef,
98  int qualityMask,
99  signed char nLoops) {
100  //variable declarations
101 
102  PropagationDirection seedDir = seed.direction();
103 
104  //perform the fit: the result's size is 1 if it succeded, 0 if fails
105  Trajectory&& trajTmp =
106  theFitter->fitOne(seed, hits, theTSOS, (nLoops > 0) ? TrajectoryFitter::looper : TrajectoryFitter::standard);
107  if UNLIKELY (!trajTmp.isValid()) {
108  DPRINT("TrackFitters") << "fit failed " << algo_ << ": " << hits.size() << '|' << int(nLoops) << ' ' << std::endl;
109  return false;
110  }
111 
112  auto theTraj = new Trajectory(std::move(trajTmp));
113  theTraj->setSeedRef(seedRef);
114 
115  statCount.hits(theTraj->foundHits(), theTraj->lostHits());
116  statCount.algo(int(algo_));
117 
118  // TrajectoryStateOnSurface innertsos;
119  // if (theTraj->direction() == alongMomentum) {
120  // innertsos = theTraj->firstMeasurement().updatedState();
121  // } else {
122  // innertsos = theTraj->lastMeasurement().updatedState();
123  // }
124 
125  ndof = 0;
126  for (auto const& tm : theTraj->measurements()) {
127  auto const& h = tm.recHitR();
128  if (h.isValid())
129  ndof = ndof + float(h.dimension()) * h.weight(); // two virtual calls!
130  }
131 
132  ndof -= 5.f;
133  if UNLIKELY (theTSOS.magneticField()->nominalValue() == 0)
134  ++ndof; // same as -4
135 
136 #if defined(VI_DEBUG) || defined(EDM_ML_DEBUG)
137  int chit[7] = {};
138  int kk = 0;
139  for (auto const& tm : theTraj->measurements()) {
140  ++kk;
141  auto const& hit = tm.recHitR();
142  if (!hit.isValid())
143  ++chit[0];
144  if (hit.det() == nullptr)
145  ++chit[1];
147  continue;
148  if (0)
149  std::cout << "h " << kk << ": " << hit.localPosition() << ' ' << hit.localPositionError() << ' ' << tm.estimate()
150  << std::endl;
151  if (hit.dimension() != 2) {
152  ++chit[2];
153  } else if (trackerHitRTTI::isFromDet(hit)) {
154  auto const& thit = static_cast<BaseTrackerRecHit const&>(hit);
155  auto const& clus = thit.firstClusterRef();
156  if (clus.isPixel())
157  ++chit[3];
158  else if (thit.isMatched()) {
159  ++chit[4];
160  } else if (thit.isProjected()) {
161  ++chit[5];
162  } else {
163  ++chit[6];
164  }
165  }
166  }
167 
168  std::ostringstream ss;
169  ss << algo_ << ": " << hits.size() << '|' << theTraj->measurements().size() << '|' << int(nLoops) << ' ';
170  for (auto c : chit)
171  ss << c << '/';
172  ss << std::endl;
173  DPRINT("TrackProducer") << ss.str();
174 
175 #endif
176 
177  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
178  //if geometricInnerState_ is true the state for projection to beam line is the one from the (geometrically) closest measurement to the beam line: to be sued for non-collision tracks
179  //the two shouuld give the same result for collision tracks that are NOT loopers
180  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
181  if (geometricInnerState_) {
182  stateForProjectionToBeamLineOnSurface =
183  theTraj->closestMeasurement(GlobalPoint(bs.x0(), bs.y0(), bs.z0())).updatedState();
184  } else {
185  if (theTraj->direction() == alongMomentum) {
186  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
187  } else {
188  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
189  }
190  }
191 
192  if UNLIKELY (!stateForProjectionToBeamLineOnSurface.isValid()) {
193  edm::LogError("CannotPropagateToBeamLine") << "the state on the closest measurement isnot valid. skipping track.";
194  delete theTraj;
195  return false;
196  }
197  const FreeTrajectoryState& stateForProjectionToBeamLine = *stateForProjectionToBeamLineOnSurface.freeState();
198 
199  LogDebug("TrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
200 
201  // TSCBLBuilderNoMaterial tscblBuilder;
202  // TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
203 
205  if (usePropagatorForPCA_) {
206  //std::cout << "PROPAGATOR FOR PCA" << std::endl;
207  TSCBLBuilderWithPropagator tscblBuilder(*thePropagator);
208  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
209  } else {
210  TSCBLBuilderNoMaterial tscblBuilder;
211  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
212  }
213 
214  if UNLIKELY (!tscbl.isValid()) {
215  delete theTraj;
216  return false;
217  }
218 
220  math::XYZPoint pos(v.x(), v.y(), v.z());
222  math::XYZVector mom(p.x(), p.y(), p.z());
223 
224  LogDebug("TrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
225 
226  auto theTrack = new reco::Track(theTraj->chiSquared(),
227  int(ndof), //FIXME fix weight() in TrackingRecHit
228  pos,
229  mom,
230  tscbl.trackStateAtPCA().charge(),
232  algo_);
233 
236  if (algoMask_.any())
237  theTrack->setAlgoMask(algoMask_);
238  theTrack->setQualityMask(qualityMask);
239  theTrack->setNLoops(nLoops);
240  theTrack->setStopReason(stopReason_);
241 
242  LogDebug("TrackProducer") << "theTrack->pt()=" << theTrack->pt();
243 
244  LogDebug("TrackProducer") << "track done\n";
245 
246  AlgoProduct aProduct{theTraj, theTrack, seedDir, 0};
247  algoResults.push_back(aProduct);
248 
249  statCount.track(nLoops);
250 
251  return true;
252 }
253 
254 template <>
256  const Propagator* thePropagator,
257  AlgoProductCollection& algoResults,
259  TrajectoryStateOnSurface& theTSOS,
260  const TrajectorySeed& seed,
261  float ndof,
262  const reco::BeamSpot& bs,
263  SeedRef seedRef,
264  int qualityMask,
265  signed char nLoops) {
266  PropagationDirection seedDir = seed.direction();
267 
268  Trajectory&& trajTmp =
269  theFitter->fitOne(seed, hits, theTSOS, (nLoops > 0) ? TrajectoryFitter::looper : TrajectoryFitter::standard);
270  if UNLIKELY (!trajTmp.isValid())
271  return false;
272 
273  auto theTraj = new Trajectory(std::move(trajTmp));
274  theTraj->setSeedRef(seedRef);
275 
276 #ifdef EDM_ML_DEBUG
277  TrajectoryStateOnSurface innertsos;
278  TrajectoryStateOnSurface outertsos;
279 
280  if (theTraj->direction() == alongMomentum) {
281  innertsos = theTraj->firstMeasurement().updatedState();
282  outertsos = theTraj->lastMeasurement().updatedState();
283  } else {
284  innertsos = theTraj->lastMeasurement().updatedState();
285  outertsos = theTraj->firstMeasurement().updatedState();
286  }
287  std::ostringstream ss;
288  auto dc = [&](TrajectoryStateOnSurface const& tsos) {
289  GetComponents comps(tsos);
290  auto const& components = comps();
291  auto sinTheta = std::sin(tsos.globalMomentum().theta());
292  for (auto const& ic : components)
293  ss << ic.weight() << "/";
294  ss << "\n";
295  for (auto const& ic : components)
296  ss << ic.localParameters().vector()[0] / sinTheta << "/";
297  ss << "\n";
298  for (auto const& ic : components)
299  ss << std::sqrt(ic.localError().matrix()(0, 0)) / sinTheta << "/";
300  };
301  ss << "\ninner comps\n";
302  dc(innertsos);
303  GetComponents icomps(innertsos);
304  auto const& tsosComponentsInner = icomps();
305 
306  ss << "\nouter comps\n";
307  dc(outertsos);
308  GetComponents ocomps(outertsos);
309  auto const& tsosComponentsOuter = ocomps();
310 
311  LogDebug("TrackProducer") << "Nr. of first / last states = " << tsosComponentsInner.size() << " "
312  << tsosComponentsOuter.size() << ss.str();
313 #endif
314 
315  ndof = 0;
316  for (auto const& tm : theTraj->measurements()) {
317  auto const& h = tm.recHitR();
318  if (h.isValid())
319  ndof = ndof + h.dimension() * h.weight();
320  }
321 
322  ndof = ndof - 5;
323  if UNLIKELY (theTSOS.magneticField()->nominalValue() == 0)
324  ++ndof; // same as -4
325 
326  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
327  //if geometricInnerState_ is true the state for projection to beam line is the one from the (geometrically) closest measurement to the beam line: to be sued for non-collision tracks
328  //the two shouuld give the same result for collision tracks that are NOT loopers
329  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
330  if (geometricInnerState_) {
331  stateForProjectionToBeamLineOnSurface =
332  theTraj->closestMeasurement(GlobalPoint(bs.x0(), bs.y0(), bs.z0())).updatedState();
333  } else {
334  if (theTraj->direction() == alongMomentum) {
335  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
336  } else {
337  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
338  }
339  }
340 
341  if UNLIKELY (!stateForProjectionToBeamLineOnSurface.isValid()) {
342  edm::LogError("CannotPropagateToBeamLine") << "the state on the closest measurement isnot valid. skipping track.";
343  delete theTraj;
344  return false;
345  }
346 
347  const FreeTrajectoryState& stateForProjectionToBeamLine = *stateForProjectionToBeamLineOnSurface.freeState();
348 
349  LogDebug("GsfTrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
350 
351  // TSCBLBuilderNoMaterial tscblBuilder;
352  // TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
353 
355  if (usePropagatorForPCA_) {
356  TSCBLBuilderWithPropagator tscblBuilder(*thePropagator);
357  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
358  } else {
359  TSCBLBuilderNoMaterial tscblBuilder;
360  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
361  }
362 
363  if UNLIKELY (tscbl.isValid() == false) {
364  delete theTraj;
365  return false;
366  }
367 
369  math::XYZPoint pos(v.x(), v.y(), v.z());
371  math::XYZVector mom(p.x(), p.y(), p.z());
372 
373  LogDebug("GsfTrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
374 
375  auto theTrack =
376  new reco::GsfTrack(theTraj->chiSquared(),
377  int(ndof), //FIXME fix weight() in TrackingRecHit
378  // theTraj->foundHits(),//FIXME to be fixed in Trajectory.h
379  // 0, //FIXME no corresponding method in trajectory.h
380  // theTraj->lostHits(),//FIXME to be fixed in Trajectory.h
381  pos,
382  mom,
383  tscbl.trackStateAtPCA().charge(),
385  theTrack->setAlgorithm(algo_);
387  theTrack->setOriginalAlgorithm(originalAlgo_);
388  if (algoMask_.any())
389  theTrack->setAlgoMask(algoMask_);
390 
391  theTrack->setStopReason(stopReason_);
392 
393  LogDebug("GsfTrackProducer") << "track done\n";
394 
395  AlgoProduct aProduct{theTraj, theTrack, seedDir, 0};
396 
397  LogDebug("GsfTrackProducer") << "track done1\n";
398  algoResults.push_back(aProduct);
399  LogDebug("GsfTrackProducer") << "track done2\n";
400 
401  statCount.gsf();
402  return true;
403 }
bool isValid() const
Definition: Trajectory.h:257
reco::TrackBase::AlgoMask algoMask_
const CurvilinearTrajectoryError & curvilinearError() const
bool isFromDet(TrackingRecHit const &hit)
reco::TrackBase::TrackAlgorithm originalAlgo_
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< ConstRecHitPointer > RecHitContainer
PropagationDirection
Log< level::Error, false > LogError
bool buildTrack(const TrajectoryFitter *, const Propagator *, AlgoProductCollection &, TransientTrackingRecHit::RecHitContainer &, TrajectoryStateOnSurface &, const TrajectorySeed &, float, const reco::BeamSpot &, SeedRef seedRef=SeedRef(), int qualityMask=0, signed char nLoops=0)
Construct Tracks to be put in the event.
GlobalPoint position() const
void setOriginalAlgorithm(const TrackAlgorithm a)
Definition: TrackBase.h:838
T sqrt(T t)
Definition: SSEVec.h:23
TrackCharge charge() const
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
GlobalVector momentum() const
double f[11][100]
#define CMS_THREAD_SAFE
virtual Trajectory fitOne(const Trajectory &traj, fitType type=standard) const =0
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
reco::TrackBase::TrackAlgorithm algo_
#define DPRINT(x)
void setAlgorithm(const TrackAlgorithm a)
Track algorithm.
Definition: TrackBase.h:832
double a
Definition: hdecay.h:121
FreeTrajectoryState const * freeState(bool withErrors=true) const
bool isUndef(TrackingRecHit const &hit)
int nominalValue() const
The nominal field value for this map in kGauss.
Definition: MagneticField.h:49
const MagneticField * magneticField() const
#define UNLIKELY(x)
Definition: Likely.h:21
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)