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 
24 
27 // #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
31 
32 namespace {
33 #ifdef STAT_TSB
34  struct StatCount {
35  long long totTrack=0;
36  long long totLoop=0;
37  long long totGsfTrack=0;
38  long long totFound=0;
39  long long totLost=0;
40  long long totAlgo[12];
41  void track(int l) {
42  if (l>0) ++totLoop; else ++totTrack;
43  }
44  void hits(int f, int l) { totFound+=f; totLost+=l;}
45  void gsf() {++totGsfTrack;}
46  void algo(int a) { if (a>=0 && a<12) ++totAlgo[a];}
47 
48 
49  void print() const {
50  std::cout << "TrackProducer stat\nTrack/Loop/Gsf/FoundHits/LostHits/algos "
51  << totTrack <<'/'<< totLoop <<'/'<< totGsfTrack <<'/'<< totFound <<'/'<< totLost;
52  for (auto a : totAlgo) std::cout << '/'<< a;
53  std::cout << std::endl;
54  }
55  StatCount() {}
56  ~StatCount() { print();}
57  };
58  StatCount statCount;
59 
60 #else
61  struct StatCount {
62  void track(int){}
63  void hits(int, int){}
64  void gsf(){}
65  void algo(int){}
66  };
67  [[cms::thread_safe]] StatCount statCount;
68 #endif
69 
70 
71 }
72 
73 
74 
75 
76 template <> bool
78  const Propagator * thePropagator,
79  AlgoProductCollection& algoResults,
81  TrajectoryStateOnSurface& theTSOS,
82  const TrajectorySeed& seed,
83  float ndof,
84  const reco::BeamSpot& bs,
85  SeedRef seedRef,
86  int qualityMask,signed char nLoops)
87 {
88  //variable declarations
89 
90  PropagationDirection seedDir = seed.direction();
91 
92  //perform the fit: the result's size is 1 if it succeded, 0 if fails
93  Trajectory && trajTmp = theFitter->fitOne(seed, hits, theTSOS,(nLoops>0) ? TrajectoryFitter::looper : TrajectoryFitter::standard);
94  if unlikely(!trajTmp.isValid()) {
95 #ifdef VI_DEBUG
96  std::cout << "fit failed " << algo_ << ": " << hits.size() <<'|' << int(nLoops) << ' ' << std::endl;
97 #endif
98  return false;
99  }
100 
101 
102  auto theTraj = new Trajectory(std::move(trajTmp));
103  theTraj->setSeedRef(seedRef);
104 
105  statCount.hits(theTraj->foundHits(),theTraj->lostHits());
106  statCount.algo(int(algo_));
107 
108  // TrajectoryStateOnSurface innertsos;
109  // if (theTraj->direction() == alongMomentum) {
110  // innertsos = theTraj->firstMeasurement().updatedState();
111  // } else {
112  // innertsos = theTraj->lastMeasurement().updatedState();
113  // }
114 
115  ndof = 0;
116  for (auto const & tm : theTraj->measurements()) {
117  auto const & h = tm.recHitR();
118  if (h.isValid()) ndof = ndof + float(h.dimension())*h.weight(); // two virtual calls!
119  }
120 
121  ndof -= 5.f;
122  if unlikely(std::abs(theTSOS.magneticField()->nominalValue())<DBL_MIN) ++ndof; // same as -4
123 
124 
125 #ifdef VI_DEBUG
126 int chit[7]={};
127 int kk=0;
128 for (auto const & tm : theTraj->measurements()) {
129  ++kk;
130  auto const & hit = tm.recHitR();
131  if (!hit.isValid()) ++chit[0];
132  if (hit.det()==nullptr) ++chit[1];
133  if ( trackerHitRTTI::isUndef(hit) ) continue;
134  if(0) std::cout << "h " << kk << ": "<< hit.localPosition() << ' ' << hit.localPositionError() << ' ' << tm.estimate() << std::endl;
135  if ( hit.dimension()!=2 ) {
136  ++chit[2];
137  } else {
138  auto const & thit = static_cast<BaseTrackerRecHit const&>(hit);
139  auto const & clus = thit.firstClusterRef();
140  if (clus.isPixel()) ++chit[3];
141  else if (thit.isMatched()) {
142  ++chit[4];
143  } else if (thit.isProjected()) {
144  ++chit[5];
145  } else {
146  ++chit[6];
147  }
148  }
149  }
150 
151 std::cout << algo_ << ": " << hits.size() <<'|' <<theTraj->measurements().size()<<'|' << int(nLoops) << ' '; for (auto c:chit) std::cout << c <<'/'; std::cout<< std::endl;
152 
153 #endif
154 
155  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
156  //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
157  //the two shouuld give the same result for collision tracks that are NOT loopers
158  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
159  if (geometricInnerState_) {
160  stateForProjectionToBeamLineOnSurface = theTraj->closestMeasurement(GlobalPoint(bs.x0(),bs.y0(),bs.z0())).updatedState();
161  } else {
162  if (theTraj->direction() == alongMomentum) {
163  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
164  } else {
165  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
166  }
167  }
168 
169  if unlikely(!stateForProjectionToBeamLineOnSurface.isValid()){
170  edm::LogError("CannotPropagateToBeamLine")<<"the state on the closest measurement isnot valid. skipping track.";
171  delete theTraj;
172  return false;
173  }
174  const FreeTrajectoryState & stateForProjectionToBeamLine=*stateForProjectionToBeamLineOnSurface.freeState();
175 
176  LogDebug("TrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
177 
178  TSCBLBuilderNoMaterial tscblBuilder;
179  TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
180 
181  if unlikely(!tscbl.isValid()) {
182  delete theTraj;
183  return false;
184  }
185 
187  math::XYZPoint pos( v.x(), v.y(), v.z() );
189  math::XYZVector mom( p.x(), p.y(), p.z() );
190 
191  LogDebug("TrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
192 
193  auto theTrack = new reco::Track(theTraj->chiSquared(),
194  int(ndof),//FIXME fix weight() in TrackingRecHit
195  pos, mom, tscbl.trackStateAtPCA().charge(),
197  algo_);
198 
199  theTrack->setQualityMask(qualityMask);
200  theTrack->setNLoops(nLoops);
201 
202  LogDebug("TrackProducer") << "theTrack->pt()=" << theTrack->pt();
203 
204  LogDebug("TrackProducer") <<"track done\n";
205 
206  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,seedDir));
207  algoResults.push_back(aProduct);
208 
209  statCount.track(nLoops);
210 
211  return true;
212 }
213 
214 template <> bool
216  const Propagator * thePropagator,
217  AlgoProductCollection& algoResults,
219  TrajectoryStateOnSurface& theTSOS,
220  const TrajectorySeed& seed,
221  float ndof,
222  const reco::BeamSpot& bs,
223  SeedRef seedRef,
224  int qualityMask,signed char nLoops)
225 {
226 
227  PropagationDirection seedDir = seed.direction();
228 
229  Trajectory && trajTmp = theFitter->fitOne(seed, hits, theTSOS,(nLoops>0) ? TrajectoryFitter::looper: TrajectoryFitter::standard);
230  if unlikely(!trajTmp.isValid()) return false;
231 
232 
233  auto theTraj = new Trajectory( std::move(trajTmp) );
234  theTraj->setSeedRef(seedRef);
235 
236  // TrajectoryStateOnSurface innertsos;
237  // TrajectoryStateOnSurface outertsos;
238 
239  // if (theTraj->direction() == alongMomentum) {
240  // innertsos = theTraj->firstMeasurement().updatedState();
241  // outertsos = theTraj->lastMeasurement().updatedState();
242  // } else {
243  // innertsos = theTraj->lastMeasurement().updatedState();
244  // outertsos = theTraj->firstMeasurement().updatedState();
245  // }
246  // std::cout
247  // << "Nr. of first / last states = "
248  // << innertsos.components().size() << " "
249  // << outertsos.components().size() << std::endl;
250  // std::vector<TrajectoryStateOnSurface> components =
251  // innertsos.components();
252  // double sinTheta =
253  // sin(innertsos.globalMomentum().theta());
254  // for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
255  // ic!=components.end(); ic++ ) {
256  // std::cout << " comp " << ic-components.begin() << " "
257  // << (*ic).weight() << " "
258  // << (*ic).localParameters().vector()[0]/sinTheta << " "
259  // << sqrt((*ic).localError().matrix()[0][0])/sinTheta << std::endl;
260  // }
261 
262  ndof = 0;
263  for (auto const & tm : theTraj->measurements()) {
264  auto const & h = tm.recHitR();
265  if (h.isValid()) ndof = ndof + h.dimension()*h.weight();
266  }
267 
268  ndof = ndof - 5;
269  if unlikely(std::abs(theTSOS.magneticField()->nominalValue())<DBL_MIN) ++ndof; // same as -4
270 
271 
272  //if geometricInnerState_ is false the state for projection to beam line is the state attached to the first hit: to be used for loopers
273  //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
274  //the two shouuld give the same result for collision tracks that are NOT loopers
275  TrajectoryStateOnSurface stateForProjectionToBeamLineOnSurface;
276  if (geometricInnerState_) {
277  stateForProjectionToBeamLineOnSurface = theTraj->closestMeasurement(GlobalPoint(bs.x0(),bs.y0(),bs.z0())).updatedState();
278  } else {
279  if (theTraj->direction() == alongMomentum) {
280  stateForProjectionToBeamLineOnSurface = theTraj->firstMeasurement().updatedState();
281  } else {
282  stateForProjectionToBeamLineOnSurface = theTraj->lastMeasurement().updatedState();
283  }
284  }
285 
286  if unlikely(!stateForProjectionToBeamLineOnSurface.isValid()){
287  edm::LogError("CannotPropagateToBeamLine")<<"the state on the closest measurement isnot valid. skipping track.";
288  delete theTraj;
289  return false;
290  }
291 
292  const FreeTrajectoryState & stateForProjectionToBeamLine=*stateForProjectionToBeamLineOnSurface.freeState();
293 
294  LogDebug("GsfTrackProducer") << "stateForProjectionToBeamLine=" << stateForProjectionToBeamLine;
295 
296  TSCBLBuilderNoMaterial tscblBuilder;
297  TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(stateForProjectionToBeamLine,bs);
298 
299  if unlikely(tscbl.isValid()==false) {
300  delete theTraj;
301  return false;
302  }
303 
305  math::XYZPoint pos( v.x(), v.y(), v.z() );
307  math::XYZVector mom( p.x(), p.y(), p.z() );
308 
309  LogDebug("GsfTrackProducer") << "pos=" << v << " mom=" << p << " pt=" << p.perp() << " mag=" << p.mag();
310 
311  auto theTrack = new reco::GsfTrack(theTraj->chiSquared(),
312  int(ndof),//FIXME fix weight() in TrackingRecHit
313  // theTraj->foundHits(),//FIXME to be fixed in Trajectory.h
314  // 0, //FIXME no corresponding method in trajectory.h
315  // theTraj->lostHits(),//FIXME to be fixed in Trajectory.h
316  pos, mom, tscbl.trackStateAtPCA().charge(), tscbl.trackStateAtPCA().curvilinearError());
317  theTrack->setAlgorithm(algo_);
318 
319  LogDebug("GsfTrackProducer") <<"track done\n";
320 
321  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,seedDir));
322  LogDebug("GsfTrackProducer") <<"track done1\n";
323  algoResults.push_back(aProduct);
324  LogDebug("GsfTrackProducer") <<"track done2\n";
325 
326  statCount.gsf();
327  return true;
328 }
#define LogDebug(id)
PropagationDirection direction() const
void setQualityMask(int qualMask)
Definition: TrackBase.h:837
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, bool set=true)
Track algorithm.
Definition: TrackBase.h:823
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