CMS 3D CMS Logo

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