CMS 3D CMS Logo

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
107  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
134  UNLIKELY(std::abs(theTSOS.magneticField()->nominalValue()) < DBL_MIN)++ 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
193  UNLIKELY(!stateForProjectionToBeamLineOnSurface.isValid()) {
194  edm::LogError("CannotPropagateToBeamLine") << "the state on the closest measurement isnot valid. skipping track.";
195  delete theTraj;
196  return false;
197  }
198  const FreeTrajectoryState& stateForProjectionToBeamLine = *stateForProjectionToBeamLineOnSurface.freeState();
199 
200  LogDebug("TrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
201 
202  // TSCBLBuilderNoMaterial tscblBuilder;
203  // TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
204 
206  if (usePropagatorForPCA_) {
207  //std::cout << "PROPAGATOR FOR PCA" << std::endl;
208  TSCBLBuilderWithPropagator tscblBuilder(*thePropagator);
209  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
210  } else {
211  TSCBLBuilderNoMaterial tscblBuilder;
212  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
213  }
214 
215  if
216  UNLIKELY(!tscbl.isValid()) {
217  delete theTraj;
218  return false;
219  }
220 
222  math::XYZPoint pos(v.x(), v.y(), v.z());
224  math::XYZVector mom(p.x(), p.y(), p.z());
225 
226  LogDebug("TrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
227 
228  auto theTrack = new reco::Track(theTraj->chiSquared(),
229  int(ndof), //FIXME fix weight() in TrackingRecHit
230  pos,
231  mom,
232  tscbl.trackStateAtPCA().charge(),
234  algo_);
235 
236  if (originalAlgo_ != reco::TrackBase::undefAlgorithm)
237  theTrack->setOriginalAlgorithm(originalAlgo_);
238  if (algoMask_.any())
239  theTrack->setAlgoMask(algoMask_);
240  theTrack->setQualityMask(qualityMask);
241  theTrack->setNLoops(nLoops);
242  theTrack->setStopReason(stopReason_);
243 
244  LogDebug("TrackProducer") << "theTrack->pt()=" << theTrack->pt();
245 
246  LogDebug("TrackProducer") << "track done\n";
247 
248  AlgoProduct aProduct{theTraj, theTrack, seedDir, 0};
249  algoResults.push_back(aProduct);
250 
251  statCount.track(nLoops);
252 
253  return true;
254 }
255 
256 template <>
258  const Propagator* thePropagator,
259  AlgoProductCollection& algoResults,
261  TrajectoryStateOnSurface& theTSOS,
262  const TrajectorySeed& seed,
263  float ndof,
264  const reco::BeamSpot& bs,
265  SeedRef seedRef,
266  int qualityMask,
267  signed char nLoops) {
268  PropagationDirection seedDir = seed.direction();
269 
270  Trajectory&& trajTmp =
271  theFitter->fitOne(seed, hits, theTSOS, (nLoops > 0) ? TrajectoryFitter::looper : TrajectoryFitter::standard);
272  if
273  UNLIKELY(!trajTmp.isValid()) return false;
274 
275  auto theTraj = new Trajectory(std::move(trajTmp));
276  theTraj->setSeedRef(seedRef);
277 
278 #ifdef EDM_ML_DEBUG
279  TrajectoryStateOnSurface innertsos;
280  TrajectoryStateOnSurface outertsos;
281 
282  if (theTraj->direction() == alongMomentum) {
283  innertsos = theTraj->firstMeasurement().updatedState();
284  outertsos = theTraj->lastMeasurement().updatedState();
285  } else {
286  innertsos = theTraj->lastMeasurement().updatedState();
287  outertsos = theTraj->firstMeasurement().updatedState();
288  }
289  std::ostringstream ss;
290  auto dc = [&](TrajectoryStateOnSurface const& tsos) {
291  std::vector<TrajectoryStateOnSurface> const& components = tsos.components();
292  auto sinTheta = std::sin(tsos.globalMomentum().theta());
293  for (auto const& ic : components)
294  ss << ic.weight() << "/";
295  ss << "\n";
296  for (auto const& ic : components)
297  ss << ic.localParameters().vector()[0] / sinTheta << "/";
298  ss << "\n";
299  for (auto const& ic : components)
300  ss << std::sqrt(ic.localError().matrix()(0, 0)) / sinTheta << "/";
301  };
302  ss << "\ninner comps\n";
303  dc(innertsos);
304  ss << "\nouter comps\n";
305  dc(outertsos);
306  LogDebug("TrackProducer") << "Nr. of first / last states = " << innertsos.components().size() << " "
307  << outertsos.components().size() << ss.str();
308 #endif
309 
310  ndof = 0;
311  for (auto const& tm : theTraj->measurements()) {
312  auto const& h = tm.recHitR();
313  if (h.isValid())
314  ndof = ndof + h.dimension() * h.weight();
315  }
316 
317  ndof = ndof - 5;
318  if
319  UNLIKELY(std::abs(theTSOS.magneticField()->nominalValue()) < DBL_MIN)++ ndof; // same as -4
320 
321  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
322  //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
323  //the two shouuld give the same result for collision tracks that are NOT loopers
324  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
325  if (geometricInnerState_) {
326  stateForProjectionToBeamLineOnSurface =
327  theTraj->closestMeasurement(GlobalPoint(bs.x0(), bs.y0(), bs.z0())).updatedState();
328  } else {
329  if (theTraj->direction() == alongMomentum) {
330  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
331  } else {
332  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
333  }
334  }
335 
336  if
337  UNLIKELY(!stateForProjectionToBeamLineOnSurface.isValid()) {
338  edm::LogError("CannotPropagateToBeamLine") << "the state on the closest measurement isnot valid. skipping track.";
339  delete theTraj;
340  return false;
341  }
342 
343  const FreeTrajectoryState& stateForProjectionToBeamLine = *stateForProjectionToBeamLineOnSurface.freeState();
344 
345  LogDebug("GsfTrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
346 
347  // TSCBLBuilderNoMaterial tscblBuilder;
348  // TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
349 
351  if (usePropagatorForPCA_) {
352  TSCBLBuilderWithPropagator tscblBuilder(*thePropagator);
353  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
354  } else {
355  TSCBLBuilderNoMaterial tscblBuilder;
356  tscbl = tscblBuilder(stateForProjectionToBeamLine, bs);
357  }
358 
359  if
360  UNLIKELY(tscbl.isValid() == false) {
361  delete theTraj;
362  return false;
363  }
364 
366  math::XYZPoint pos(v.x(), v.y(), v.z());
368  math::XYZVector mom(p.x(), p.y(), p.z());
369 
370  LogDebug("GsfTrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
371 
372  auto theTrack =
373  new reco::GsfTrack(theTraj->chiSquared(),
374  int(ndof), //FIXME fix weight() in TrackingRecHit
375  // theTraj->foundHits(),//FIXME to be fixed in Trajectory.h
376  // 0, //FIXME no corresponding method in trajectory.h
377  // theTraj->lostHits(),//FIXME to be fixed in Trajectory.h
378  pos,
379  mom,
380  tscbl.trackStateAtPCA().charge(),
382  theTrack->setAlgorithm(algo_);
383  if (originalAlgo_ != reco::TrackBase::undefAlgorithm)
384  theTrack->setOriginalAlgorithm(originalAlgo_);
385  if (algoMask_.any())
386  theTrack->setAlgoMask(algoMask_);
387 
388  theTrack->setStopReason(stopReason_);
389 
390  LogDebug("GsfTrackProducer") << "track done\n";
391 
392  AlgoProduct aProduct{theTraj, theTrack, seedDir, 0};
393 
394  LogDebug("GsfTrackProducer") << "track done1\n";
395  algoResults.push_back(aProduct);
396  LogDebug("GsfTrackProducer") << "track done2\n";
397 
398  statCount.gsf();
399  return true;
400 }
#define LogDebug(id)
PropagationDirection direction() const
double z0() const
z coordinate
Definition: BeamSpot.h:65
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
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
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
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
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.
virtual Trajectory fitOne(const Trajectory &traj, fitType type=standard) const =0
void setOriginalAlgorithm(const TrackAlgorithm a)
Definition: TrackBase.h:768
T sqrt(T t)
Definition: SSEVec.h:19
FreeTrajectoryState const * freeState(bool withErrors=true) const
T z() const
Definition: PV3DBase.h:61
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
#define CMS_THREAD_SAFE
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
#define DPRINT(x)
void setAlgorithm(const TrackAlgorithm a)
Track algorithm.
Definition: TrackBase.h:762
double a
Definition: hdecay.h:119
double y0() const
y coordinate
Definition: BeamSpot.h:63
bool isUndef(TrackingRecHit const &hit)
#define UNLIKELY(x)
Definition: Likely.h:21
Components const & components() const
T x() const
Definition: PV3DBase.h:59
def move(src, dest)
Definition: eostools.py:511
double x0() const
x coordinate
Definition: BeamSpot.h:61