CMS 3D CMS Logo

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