CMS 3D CMS Logo

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