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