CMS 3D CMS Logo

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