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 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 }
Vector3DBase
Definition: Vector3DBase.h:8
TSCBLBuilderNoMaterial.h
FreeTrajectoryState::momentum
GlobalVector momentum() const
Definition: FreeTrajectoryState.h:68
TrajectoryStateClosestToBeamLine
Definition: TrajectoryStateClosestToBeamLine.h:15
TrajectoryStateOnSurface.h
HLT_FULL_cff.track
track
Definition: HLT_FULL_cff.py:11713
MessageLogger.h
TrajectoryFitter::looper
Definition: TrajectoryFitter.h:21
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
TrackBase.h
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pos
Definition: PixelAliasList.h:18
FreeTrajectoryState::charge
TrackCharge charge() const
Definition: FreeTrajectoryState.h:69
TrackingGeometry.h
trackerHitRTTI::isUndef
bool isUndef(TrackingRecHit const &hit)
Definition: trackerHitRTTI.h:24
TrackingRecHitFwd.h
FreeTrajectoryState::position
GlobalPoint position() const
Definition: FreeTrajectoryState.h:67
TransientTrack.h
findQualityFiles.v
v
Definition: findQualityFiles.py:179
TrackProducerAlgorithm::algo_
reco::TrackBase::TrackAlgorithm algo_
Definition: TrackProducerAlgorithm.h:137
TRecHit2DPosConstraint.h
TkTransientTrackingRecHitBuilder.h
funct::sin
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
ndof
Definition: HIMultiTrackSelector.h:49
fileCollector.seed
seed
Definition: fileCollector.py:127
reco::TrackBase::setOriginalAlgorithm
void setOriginalAlgorithm(const TrackAlgorithm a)
Definition: TrackBase.h:838
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
Propagator
Definition: Propagator.h:44
cmsdt::algo
algo
Definition: constants.h:165
UNLIKELY
#define UNLIKELY(x)
Definition: Likely.h:21
reco::GsfTrack
Definition: GsfTrack.h:12
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
cms::cuda::bs
bs
Definition: HistoContainer.h:127
TrajectoryStateOnSurface::freeState
FreeTrajectoryState const * freeState(bool withErrors=true) const
Definition: TrajectoryStateOnSurface.h:58
TrackingRecHit::RecHitContainer
std::vector< ConstRecHitPointer > RecHitContainer
Definition: TrackingRecHit.h:31
TrackProducerAlgorithm::usePropagatorForPCA_
bool usePropagatorForPCA_
Definition: TrackProducerAlgorithm.h:144
FreeTrajectoryState::curvilinearError
const CurvilinearTrajectoryError & curvilinearError() const
Definition: FreeTrajectoryState.h:89
OrphanHandle.h
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
reco::BeamSpot
Definition: BeamSpot.h:21
TrackingRecHitLessFromGlobalPosition.h
reco::Track
Definition: Track.h:27
MagneticField::nominalValue
int nominalValue() const
The nominal field value for this map in kGauss.
Definition: MagneticField.h:49
TrajectoryFitter.h
GetRecoTauVFromDQM_MC_cff.kk
kk
Definition: GetRecoTauVFromDQM_MC_cff.py:84
h
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
Point3DBase< float, GlobalTag >
TrajectorySeed.h
TrackProducerAlgorithm::geometricInnerState_
bool geometricInnerState_
Definition: TrackProducerAlgorithm.h:143
TrajectoryStateOnSurface::components
Components const & components() const
Definition: TrajectoryStateOnSurface.h:85
TrajectoryFitter::standard
Definition: TrajectoryFitter.h:21
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
math::XYZPoint
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
a
double a
Definition: hdecay.h:119
TSCBLBuilderWithPropagator.h
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
thread_safety_macros.h
math::XYZVector
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
reco::TrackBase::undefAlgorithm
Definition: TrackBase.h:90
createfilelist.int
int
Definition: createfilelist.py:10
TrackProducerAlgorithm::algoMask_
reco::TrackBase::AlgoMask algoMask_
Definition: TrackProducerAlgorithm.h:139
trackerHitRTTI::isFromDet
bool isFromDet(TrackingRecHit const &hit)
Definition: trackerHitRTTI.h:36
ntupleEnum.gsf
gsf
Definition: ntupleEnum.py:49
MagneticField.h
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
TrackProducerAlgorithm::originalAlgo_
reco::TrackBase::TrackAlgorithm originalAlgo_
Definition: TrackProducerAlgorithm.h:138
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
reco::TrackBase::setAlgorithm
void setAlgorithm(const TrackAlgorithm a)
Track algorithm.
Definition: TrackBase.h:832
TSCBLBuilderWithPropagator
Definition: TSCBLBuilderWithPropagator.h:16
TrajectoryFitter
Definition: TrajectoryFitter.h:19
TrackProducerAlgorithm.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
ErrorFrameTransformer.h
FreeTrajectoryState
Definition: FreeTrajectoryState.h:27
Trajectory
Definition: Trajectory.h:38
TSCBLBuilderNoMaterial
Definition: TSCBLBuilderNoMaterial.h:13
makeMuonMisalignmentScenario.components
string components
Definition: makeMuonMisalignmentScenario.py:58
PropagationDirection
PropagationDirection
Definition: PropagationDirection.h:4
TransverseImpactPointExtrapolator.h
TrajectorySeed
Definition: TrajectorySeed.h:18
TrackProducerAlgorithm::stopReason_
uint8_t stopReason_
Definition: TrackProducerAlgorithm.h:140
TrajectoryStateTransform.h
Exception.h
TransientTrackingRecHitBuilder.h
TRecHit1DMomConstraint.h
TrackProducerAlgorithm::buildTrack
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.
TrajectoryStateClosestToBeamLine::isValid
bool isValid() const
Definition: TrajectoryStateClosestToBeamLine.h:50
TrackCandidate.h
TrajectoryStateClosestToBeamLine::trackStateAtPCA
FTS const & trackStateAtPCA() const
Definition: TrajectoryStateClosestToBeamLine.h:32
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
DPRINT
#define DPRINT(x)
Definition: TrackProducerAlgorithm.cc:35
TrajectoryFitter::fitOne
virtual Trajectory fitOne(const Trajectory &traj, fitType type=standard) const =0
RecHitSorter.h
TrajectoryStateOnSurface::magneticField
const MagneticField * magneticField() const
Definition: TrajectoryStateOnSurface.h:62
alongMomentum
Definition: PropagationDirection.h:4
TrajectoryStateOnSurface::isValid
bool isValid() const
Definition: TrajectoryStateOnSurface.h:54
Trajectory::isValid
bool isValid() const
Definition: Trajectory.h:257
hit
Definition: SiStripHitEffFromCalibTree.cc:88