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 
214  theTrack->setQualityMask(qualityMask);
215  theTrack->setNLoops(nLoops);
216 
217  LogDebug("TrackProducer") << "theTrack->pt()=" << theTrack->pt();
218 
219  LogDebug("TrackProducer") <<"track done\n";
220 
221  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,seedDir));
222  algoResults.push_back(aProduct);
223 
224  statCount.track(nLoops);
225 
226  return true;
227 }
228 
229 template <> bool
231  const Propagator * thePropagator,
232  AlgoProductCollection& algoResults,
234  TrajectoryStateOnSurface& theTSOS,
235  const TrajectorySeed& seed,
236  float ndof,
237  const reco::BeamSpot& bs,
238  SeedRef seedRef,
239  int qualityMask,signed char nLoops)
240 {
241 
242  PropagationDirection seedDir = seed.direction();
243 
244  Trajectory && trajTmp = theFitter->fitOne(seed, hits, theTSOS,(nLoops>0) ? TrajectoryFitter::looper: TrajectoryFitter::standard);
245  if unlikely(!trajTmp.isValid()) return false;
246 
247 
248  auto theTraj = new Trajectory( std::move(trajTmp) );
249  theTraj->setSeedRef(seedRef);
250 
251  // TrajectoryStateOnSurface innertsos;
252  // TrajectoryStateOnSurface outertsos;
253 
254  // if (theTraj->direction() == alongMomentum) {
255  // innertsos = theTraj->firstMeasurement().updatedState();
256  // outertsos = theTraj->lastMeasurement().updatedState();
257  // } else {
258  // innertsos = theTraj->lastMeasurement().updatedState();
259  // outertsos = theTraj->firstMeasurement().updatedState();
260  // }
261  // std::cout
262  // << "Nr. of first / last states = "
263  // << innertsos.components().size() << " "
264  // << outertsos.components().size() << std::endl;
265  // std::vector<TrajectoryStateOnSurface> components =
266  // innertsos.components();
267  // double sinTheta =
268  // sin(innertsos.globalMomentum().theta());
269  // for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
270  // ic!=components.end(); ic++ ) {
271  // std::cout << " comp " << ic-components.begin() << " "
272  // << (*ic).weight() << " "
273  // << (*ic).localParameters().vector()[0]/sinTheta << " "
274  // << sqrt((*ic).localError().matrix()[0][0])/sinTheta << std::endl;
275  // }
276 
277  ndof = 0;
278  for (auto const & tm : theTraj->measurements()) {
279  auto const & h = tm.recHitR();
280  if (h.isValid()) ndof = ndof + h.dimension()*h.weight();
281  }
282 
283  ndof = ndof - 5;
284  if unlikely(std::abs(theTSOS.magneticField()->nominalValue())<DBL_MIN) ++ndof; // same as -4
285 
286 
287  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
288  //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
289  //the two shouuld give the same result for collision tracks that are NOT loopers
290  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
291  if (geometricInnerState_) {
292  stateForProjectionToBeamLineOnSurface = theTraj->closestMeasurement(GlobalPoint(bs.x0(),bs.y0(),bs.z0())).updatedState();
293  } else {
294  if (theTraj->direction() == alongMomentum) {
295  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
296  } else {
297  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
298  }
299  }
300 
301  if unlikely(!stateForProjectionToBeamLineOnSurface.isValid()){
302  edm::LogError("CannotPropagateToBeamLine")<<"the state on the closest measurement isnot valid. skipping track.";
303  delete theTraj;
304  return false;
305  }
306 
307  const FreeTrajectoryState & stateForProjectionToBeamLine=*stateForProjectionToBeamLineOnSurface.freeState();
308 
309  LogDebug("GsfTrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
310 
311 // TSCBLBuilderNoMaterial tscblBuilder;
312 // TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
313 
316  TSCBLBuilderWithPropagator tscblBuilder(*thePropagator);
317  tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
318  } else {
319  TSCBLBuilderNoMaterial tscblBuilder;
320  tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
321  }
322 
323 
324  if unlikely(tscbl.isValid()==false) {
325  delete theTraj;
326  return false;
327  }
328 
330  math::XYZPoint pos( v.x(), v.y(), v.z() );
332  math::XYZVector mom( p.x(), p.y(), p.z() );
333 
334  LogDebug("GsfTrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
335 
336  auto theTrack = new reco::GsfTrack(theTraj->chiSquared(),
337  int(ndof),//FIXME fix weight() in TrackingRecHit
338  // theTraj->foundHits(),//FIXME to be fixed in Trajectory.h
339  // 0, //FIXME no corresponding method in trajectory.h
340  // theTraj->lostHits(),//FIXME to be fixed in Trajectory.h
341  pos, mom, tscbl.trackStateAtPCA().charge(), tscbl.trackStateAtPCA().curvilinearError());
342  theTrack->setAlgorithm(algo_);
343 
344  LogDebug("GsfTrackProducer") <<"track done\n";
345 
346  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,seedDir));
347  LogDebug("GsfTrackProducer") <<"track done1\n";
348  algoResults.push_back(aProduct);
349  LogDebug("GsfTrackProducer") <<"track done2\n";
350 
351  statCount.gsf();
352  return true;
353 }
#define LogDebug(id)
PropagationDirection direction() const
void setQualityMask(int qualMask)
Definition: TrackBase.h:820
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
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)
std::pair< Trajectory *, std::pair< reco::Track *, PropagationDirection > > AlgoProduct
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]
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:800
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