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