CMS 3D CMS Logo

CkfTrajectoryBuilder.cc
Go to the documentation of this file.
2 
4 
6 
10 
17 
19 
21 
26 
27 using namespace std;
28 
30  : CkfTrajectoryBuilder(conf,
31  BaseCkfTrajectoryBuilder::createTrajectoryFilter(
32  conf.getParameter<edm::ParameterSet>("trajectoryFilter"), iC)) {}
33 
34 CkfTrajectoryBuilder::CkfTrajectoryBuilder(const edm::ParameterSet& conf, std::unique_ptr<TrajectoryFilter> filter)
36  theMaxCand = conf.getParameter<int>("maxCand");
37  theLostHitPenalty = conf.getParameter<double>("lostHitPenalty");
38  theIntermediateCleaning = conf.getParameter<bool>("intermediateCleaning");
39  theAlwaysUseInvalidHits = conf.getParameter<bool>("alwaysUseInvalidHits");
40  /*
41  theSharedSeedCheck = conf.getParameter<bool>("SharedSeedCheck");
42  std::stringstream ss;
43  ss<<"CkfTrajectoryBuilder_"<<conf.getParameter<std::string>("ComponentName")<<"_"<<this;
44  theUniqueName = ss.str();
45  LogDebug("CkfPattern")<<"my unique name is: "<<theUniqueName;
46  */
47 }
48 
49 /*
50  void CkfTrajectoryBuilder::setEvent(const edm::Event& event) const
51  {
52  theMeasurementTracker->update(event);
53  }
54 */
55 
57 
60  result.reserve(5);
62  return result;
63 }
64 
65 /*
66  void CkfTrajectoryBuilder::rememberSeedAndTrajectories(const TrajectorySeed& seed,
67  CkfTrajectoryBuilder::TrajectoryContainer &result) const
68  {
69 
70  //result ----> theCachedTrajectories
71  //every first iteration on event. forget about everything that happened before
72  if (edm::Service<UpdaterService>()->checkOnce(theUniqueName))
73  theCachedTrajectories.clear();
74 
75  if (seed.nHits()==0) return;
76 
77  //then remember those trajectories
78  for (TrajectoryContainer::iterator traj=result.begin();
79  traj!=result.end(); ++traj) {
80  theCachedTrajectories.insert(std::make_pair(seed.recHits().first->geographicalId(),*traj));
81  }
82  }
83 
84  bool CkfTrajectoryBuilder::sharedSeed(const TrajectorySeed& s1,const TrajectorySeed& s2) const{
85  //quit right away on nH=0
86  if (s1.nHits()==0 || s2.nHits()==0) return false;
87  //quit right away if not the same number of hits
88  if (s1.nHits()!=s2.nHits()) return false;
89  TrajectorySeed::range r1=s1.recHits();
90  TrajectorySeed::range r2=s2.recHits();
91  TrajectorySeed::const_iterator i1,i2;
92  TrajectorySeed::const_iterator & i1_e=r1.second,&i2_e=r2.second;
93  TrajectorySeed::const_iterator & i1_b=r1.first,&i2_b=r2.first;
94  //quit right away if first detId does not match. front exist because of ==0 ->quit test
95  if(i1_b->geographicalId() != i2_b->geographicalId()) return false;
96  //then check hit by hit if they are the same
97  for (i1=i1_b,i2=i2_b;i1!=i1_e,i2!=i2_e;++i1,++i2){
98  if (!i1->sharesInput(&(*i2),TrackingRecHit::all)) return false;
99  }
100  return true;
101  }
102  bool CkfTrajectoryBuilder::seedAlreadyUsed(const TrajectorySeed& seed,
103  CkfTrajectoryBuilder::TempTrajectoryContainer &candidates) const
104  {
105  //theCachedTrajectories ---> candidates
106  if (seed.nHits()==0) return false;
107  bool answer=false;
108  pair<SharedTrajectory::const_iterator, SharedTrajectory::const_iterator> range =
109  theCachedTrajectories.equal_range(seed.recHits().first->geographicalId());
110  SharedTrajectory::const_iterator trajP;
111  for (trajP = range.first; trajP!=range.second;++trajP){
112  //check whether seeds are identical
113  if (sharedSeed(trajP->second.seed(),seed)){
114  candidates.push_back(trajP->second);
115  answer=true;
116  }//already existing trajectory shares the seed.
117  }//loop already made trajectories
118 
119  return answer;
120  }
121 */
122 
125  // analyseSeed( seed);
126  /*
127  if (theSharedSeedCheck){
128  TempTrajectoryContainer candidates;
129  if (seedAlreadyUsed(seed,candidates))
130  {
131  //start with those candidates already made before
132  limitedCandidates(candidates,result);
133  //and quit
134  return;
135  }
136  }
137  */
138 
139  unsigned int tmp;
140  buildTrajectories(seed, result, tmp, nullptr);
141 }
142 
145  unsigned int& nCandPerSeed,
146  const TrajectoryFilter*) const {
147  if (theMeasurementTracker == nullptr) {
148  throw cms::Exception("LogicError")
149  << "Asking to create trajectories to an un-initialized CkfTrajectoryBuilder.\nYou have to call clone(const "
150  "MeasurementTrackerEvent *data) and then call trajectories on it instead.\n";
151  }
152 
154 
157  nCandPerSeed = limitedCandidates(seed, startingTraj, result);
158 
159  return startingTraj;
160 
161  /*
162  //and remember what you just did
163  if (theSharedSeedCheck) rememberSeedAndTrajectories(seed,result);
164  */
165 
166  // analyseResult(result);
167 }
168 
170  TempTrajectory& startingTraj,
171  TrajectoryContainer& result) const {
173  candidates.push_back(startingTraj);
174  std::shared_ptr<const TrajectorySeed> sharedSeed(new TrajectorySeed(seed));
175  return limitedCandidates(sharedSeed, candidates, result);
176 }
177 
178 unsigned int CkfTrajectoryBuilder::limitedCandidates(const std::shared_ptr<const TrajectorySeed>& sharedSeed,
180  TrajectoryContainer& result) const {
181  unsigned int nIter = 1;
182  unsigned int nCands = 0; // ignore startingTraj
183  unsigned int prevNewCandSize = 0;
184  TempTrajectoryContainer newCand; // = TrajectoryContainer();
185  newCand.reserve(2 * theMaxCand);
186 
187  auto trajCandLess = [&](TempTrajectory const& a, TempTrajectory const& b) {
188  return (a.chiSquared() + a.lostHits() * theLostHitPenalty) < (b.chiSquared() + b.lostHits() * theLostHitPenalty);
189  };
190 
191  while (!candidates.empty()) {
192  newCand.clear();
193  for (auto traj = candidates.begin(); traj != candidates.end(); traj++) {
194  std::vector<TM> meas;
195  findCompatibleMeasurements(*sharedSeed, *traj, meas);
196 
197  // --- method for debugging
200  return nCands;
201  // ---
202 
203  if (meas.empty()) {
204  addToResult(sharedSeed, *traj, result);
205  } else {
206  std::vector<TM>::const_iterator last;
208  last = meas.end();
209  else {
210  if (meas.front().recHit()->isValid()) {
211  last = find_if(meas.begin(), meas.end(), [](auto const& meas) { return !meas.recHit()->isValid(); });
212  } else
213  last = meas.end();
214  }
215 
216  for (auto itm = meas.begin(); itm != last; itm++) {
217  TempTrajectory newTraj = *traj;
218  updateTrajectory(newTraj, std::move(*itm));
219 
220  if (toBeContinued(newTraj)) {
221  newCand.push_back(std::move(newTraj));
222  std::push_heap(newCand.begin(), newCand.end(), trajCandLess);
223  } else {
224  addToResult(sharedSeed, newTraj, result);
226  }
227  }
228  }
229 
230  // account only new candidates, i.e.
231  // - 1 candidate -> 1 candidate, don't increase count
232  // - 1 candidate -> 2 candidates, increase count by 1
233  nCands += newCand.size() - prevNewCandSize;
234  prevNewCandSize = newCand.size();
235 
236  /*
237  auto trajVal = [&](TempTrajectory const & a) {
238  return a.chiSquared() + a.lostHits()*theLostHitPenalty;
239  };
240 
241  // safe (stable?) logig: always sort, kill exceeding only if worse than last to keep
242  // if ((int)newCand.size() > theMaxCand) std::cout << "TrajVal " << theMaxCand << ' ' << newCand.size() << ' ' << trajVal(newCand.front());
243  int toCut = int(newCand.size()) - int(theMaxCand);
244  if (toCut>0) {
245  // move largest "toCut" to the end
246  for (int i=0; i<toCut; ++i)
247  std::pop_heap(newCand.begin(),newCand.end()-i,trajCandLess);
248  auto fval = trajVal(newCand.front());
249  // remove till equal to highest to keep
250  for (int i=0; i<toCut; ++i) {
251  if (fval==trajVal(newCand.back())) break;
252  newCand.pop_back();
253  }
254  //assert((int)newCand.size() >= theMaxCand);
255  //std::cout << "; " << newCand.size() << ' ' << trajVal(newCand.front()) << " " << trajVal(newCand.back());
256 
257  // std::make_heap(newCand.begin(),newCand.end(),trajCandLess);
258  // push_heap again the one left
259  for (auto iter = newCand.begin()+theMaxCand+1; iter<=newCand.end(); ++iter )
260  std::push_heap(newCand.begin(),iter,trajCandLess);
261 
262  // std::cout << "; " << newCand.size() << ' ' << trajVal(newCand.front()) << " " << trajVal(newCand.back()) << std::endl;
263  }
264 
265  */
266 
267  // intermedeate login: always sort, kill all exceeding
268  while ((int)newCand.size() > theMaxCand) {
269  std::pop_heap(newCand.begin(), newCand.end(), trajCandLess);
270  // if ((int)newCand.size() == theMaxCand+1) std::cout << " " << trajVal(newCand.front()) << " " << trajVal(newCand.back()) << std::endl;
271  newCand.pop_back();
272  }
273 
274  /*
275  // original logic: sort only if > theMaxCand, kill all exceeding
276  if ((int)newCand.size() > theMaxCand) {
277  std::sort( newCand.begin(), newCand.end(), TrajCandLess<TempTrajectory>(theLostHitPenalty));
278  // std::partial_sort( newCand.begin(), newCand.begin()+theMaxCand, newCand.end(), TrajCandLess<TempTrajectory>(theLostHitPenalty));
279  std::cout << "TrajVal " << theMaxCand << ' ' << newCand.size() << ' '
280  << trajVal(newCand.back()) << ' ' << trajVal(newCand[theMaxCand-1]) << ' ' << trajVal(newCand[theMaxCand]) << std::endl;
281  newCand.resize(theMaxCand);
282  }
283  */
284 
285  } // end loop on candidates
286 
287  std::sort_heap(newCand.begin(), newCand.end(), trajCandLess);
290 
291  candidates.swap(newCand);
292 
293  LogDebug("CkfPattern") << result.size() << " candidates after " << nIter++ << " CKF iteration: \n"
294  << PrintoutHelper::dumpCandidates(result) << "\n " << candidates.size()
295  << " running candidates are: \n"
297  }
298  return nCands;
299 }
300 
302  auto&& predictedState = tm.predictedState();
303  auto&& hit = tm.recHit();
304  if (hit->isValid()) {
305  auto&& upState = theUpdator->update(predictedState, *hit);
306  traj.emplace(std::move(predictedState), std::move(upState), std::move(hit), tm.estimate(), tm.layer());
307  } else {
308  traj.emplace(std::move(predictedState), std::move(hit), 0, tm.layer());
309  }
310 }
311 
313  const TempTrajectory& traj,
314  std::vector<TrajectoryMeasurement>& result) const {
315  int invalidHits = 0;
316  //Use findStateAndLayers which handles the hitless seed use case
317  std::pair<TSOS, std::vector<const DetLayer*> >&& stateAndLayers = findStateAndLayers(seed, traj);
318  if (stateAndLayers.second.empty())
319  return;
320 
321  auto layerBegin = stateAndLayers.second.begin();
322  auto layerEnd = stateAndLayers.second.end();
323  LogDebug("CkfPattern") << "looping on " << stateAndLayers.second.size() << " layers.";
324  const Propagator* fwdPropagator = forwardPropagator(seed);
325  for (auto il = layerBegin; il != layerEnd; il++) {
326  LogDebug("CkfPattern") << "looping on a layer in findCompatibleMeasurements.\n last layer: " << traj.lastLayer()
327  << " current layer: " << (*il);
328 
329  TSOS stateToUse = stateAndLayers.first;
330  //Added protection before asking for the lastLayer on the trajectory
331  if
332  UNLIKELY(!traj.empty() && (*il) == traj.lastLayer()) {
333  LogDebug("CkfPattern") << " self propagating in findCompatibleMeasurements.\n from: \n" << stateToUse;
334  //self navigation case
335  // go to a middle point first
337  GlobalPoint center(0, 0, 0);
338  stateToUse = middle.extrapolate(stateToUse, center, *fwdPropagator);
339 
340  if (!stateToUse.isValid())
341  continue;
342  LogDebug("CkfPattern") << "to: " << stateToUse;
343  }
344 
345  LayerMeasurements layerMeasurements(theMeasurementTracker->measurementTracker(), *theMeasurementTracker);
346  std::vector<TrajectoryMeasurement>&& tmp =
347  layerMeasurements.measurements((**il), stateToUse, *fwdPropagator, *theEstimator);
348 
349  if (!tmp.empty()) {
350  if (result.empty())
351  result.swap(tmp);
352  else {
353  // keep one dummy TM at the end, skip the others
354  result.insert(
355  result.end() - invalidHits, std::make_move_iterator(tmp.begin()), std::make_move_iterator(tmp.end()));
356  }
357  invalidHits++;
358  }
359  }
360 
361  // sort the final result, keep dummy measurements at the end
362  if (result.size() > 1) {
363  std::sort(result.begin(), result.end() - invalidHits, TrajMeasLessEstim());
364  }
365 
366  LogDebug("CkfPattern") << "starting from:\n"
367  << "x: " << stateAndLayers.first.globalPosition() << "\n"
368  << "p: " << stateAndLayers.first.globalMomentum() << "\n"
370 
371 #ifdef DEBUG_INVALID
372  bool afterInvalid = false;
373  for (vector<TM>::const_iterator i = result.begin(); i != result.end(); i++) {
374  if (!i->recHit().isValid())
375  afterInvalid = true;
376  if (afterInvalid && i->recHit().isValid()) {
377  edm::LogError("CkfPattern") << "CkfTrajectoryBuilder error: valid hit after invalid!";
378  }
379  }
380 #endif
381 
382  //analyseMeasurements( result, traj);
383 }
BaseCkfTrajectoryBuilder::addToResult
void addToResult(std::shared_ptr< const TrajectorySeed > const &seed, TempTrajectory &traj, TrajectoryContainer &result, bool inOut=false) const
Definition: BaseCkfTrajectoryBuilder.cc:146
Chi2MeasurementEstimatorBase.h
Propagator.h
TrajMeasLessEstim.h
TrajectoryFilter
Definition: TrajectoryFilter.h:28
BaseCkfTrajectoryBuilder
Definition: BaseCkfTrajectoryBuilder.h:53
mps_fire.i
i
Definition: mps_fire.py:355
MeasurementTrackerEvent.h
MessageLogger.h
PrintoutHelper::dumpCandidates
static std::string dumpCandidates(collection &candidates)
Definition: PrintoutHelper.h:69
CkfTrajectoryBuilder.h
TrajectoryFilter.h
edm
HLT enums.
Definition: AlignableModifier.h:19
TempTrajectory
Definition: TempTrajectory.h:40
BaseCkfTrajectoryBuilder::TrajectoryContainer
std::vector< Trajectory > TrajectoryContainer
Definition: BaseCkfTrajectoryBuilder.h:62
CkfTrajectoryBuilder::buildTrajectories
TempTrajectory buildTrajectories(const TrajectorySeed &, TrajectoryContainer &ret, unsigned int &nCandPerSeed, const TrajectoryFilter *) const override
Definition: CkfTrajectoryBuilder.cc:143
BaseCkfTrajectoryBuilder::theEstimator
const Chi2MeasurementEstimatorBase * theEstimator
Definition: BaseCkfTrajectoryBuilder.h:173
BaseCkfTrajectoryBuilder::findStateAndLayers
StateAndLayers findStateAndLayers(const TrajectorySeed &seed, const TempTrajectory &traj) const
Definition: BaseCkfTrajectoryBuilder.cc:195
TrajMeasLessEstim
Definition: TrajMeasLessEstim.h:10
TransverseImpactPointExtrapolator
Definition: TransverseImpactPointExtrapolator.h:26
CkfTrajectoryBuilder::theLostHitPenalty
float theLostHitPenalty
Definition: CkfTrajectoryBuilder.h:65
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
BaseCkfTrajectoryBuilder::theTTRHBuilder
const TransientTrackingRecHitBuilder * theTTRHBuilder
Definition: BaseCkfTrajectoryBuilder.h:174
TrajectoryStateUpdator.h
TrajCandLess.h
CkfTrajectoryBuilder::theIntermediateCleaning
bool theIntermediateCleaning
Definition: CkfTrajectoryBuilder.h:66
IntermediateTrajectoryCleaner.h
CkfTrajectoryBuilder::TrajectoryContainer
std::vector< Trajectory > TrajectoryContainer
Definition: CkfTrajectoryBuilder.h:36
Propagator
Definition: Propagator.h:44
GeometricSearchTracker.h
UNLIKELY
#define UNLIKELY(x)
Definition: Likely.h:21
CkfTrajectoryBuilder::theAlwaysUseInvalidHits
bool theAlwaysUseInvalidHits
Definition: CkfTrajectoryBuilder.h:68
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
dqmdumpme.last
last
Definition: dqmdumpme.py:56
BaseCkfTrajectoryBuilder::toBeContinued
bool toBeContinued(TempTrajectory &traj, bool inOut=false) const
Definition: BaseCkfTrajectoryBuilder.cc:114
LayerMeasurements
Definition: LayerMeasurements.h:18
BaseCkfTrajectoryBuilder::theUpdator
const TrajectoryStateUpdator * theUpdator
Definition: BaseCkfTrajectoryBuilder.h:170
PrintoutHelper::dumpMeasurements
static std::string dumpMeasurements(const std::vector< TrajectoryMeasurement > &v)
Definition: PrintoutHelper.cc:21
LayerMeasurements.h
IdealMagneticFieldRecord.h
BaseCkfTrajectoryBuilder::theMeasurementTracker
const MeasurementTrackerEvent * theMeasurementTracker
Definition: BaseCkfTrajectoryBuilder.h:175
CkfTrajectoryBuilder::CkfTrajectoryBuilder
CkfTrajectoryBuilder(const edm::ParameterSet &conf, edm::ConsumesCollector &iC)
Definition: CkfTrajectoryBuilder.cc:29
Point3DBase< float, GlobalTag >
b
double b
Definition: hdecay.h:118
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
BaseCkfTrajectoryBuilder::TempTrajectoryContainer
std::vector< TempTrajectory > TempTrajectoryContainer
Definition: BaseCkfTrajectoryBuilder.h:63
TrajectoryStateUpdator::update
virtual TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TrackingRecHit &) const =0
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
edm::ParameterSet
Definition: ParameterSet.h:36
edm::LogError
Definition: MessageLogger.h:183
a
double a
Definition: hdecay.h:119
ParameterSet
Definition: Functions.h:16
CkfTrajectoryBuilder
Definition: CkfTrajectoryBuilder.h:34
BaseCkfTrajectoryBuilder::forwardPropagator
const Propagator * forwardPropagator(const TrajectorySeed &seed) const
Definition: BaseCkfTrajectoryBuilder.h:160
edm::EventSetup
Definition: EventSetup.h:57
BasicSingleTrajectoryState.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
CkfTrajectoryBuilder::updateTrajectory
void updateTrajectory(TempTrajectory &traj, TM &&tm) const
Definition: CkfTrajectoryBuilder.cc:301
CkfTrajectoryBuilder::setEvent_
void setEvent_(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: CkfTrajectoryBuilder.cc:56
Trajectory.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
CkfTrajectoryBuilder::limitedCandidates
unsigned int limitedCandidates(const TrajectorySeed &seed, TempTrajectory &startingTraj, TrajectoryContainer &result) const
Definition: CkfTrajectoryBuilder.cc:169
BaseCkfTrajectoryBuilder::analyzeMeasurementsDebugger
virtual bool analyzeMeasurementsDebugger(Trajectory &traj, const std::vector< TrajectoryMeasurement > &meas, const MeasurementTrackerEvent *theMeasurementTracker, const Propagator *theForwardPropagator, const Chi2MeasurementEstimatorBase *theEstimator, const TransientTrackingRecHitBuilder *theTTRHBuilder) const
Definition: BaseCkfTrajectoryBuilder.h:113
HLT_2018_cff.candidates
candidates
Definition: HLT_2018_cff.py:53513
Exception
Definition: hltDiff.cc:246
TransverseImpactPointExtrapolator.h
TrajectorySeed
Definition: TrajectorySeed.h:17
TransverseImpactPointExtrapolator::extrapolate
TrajectoryStateOnSurface extrapolate(const FreeTrajectoryState &fts, const GlobalPoint &vtx) const
extrapolation with default (=geometrical) propagator
Definition: TransverseImpactPointExtrapolator.cc:23
TempTrajectory::lastLayer
const DetLayer * lastLayer() const
Redundant method, returns the layer of lastMeasurement() .
Definition: TempTrajectory.h:237
CkfTrajectoryBuilder::findCompatibleMeasurements
virtual void findCompatibleMeasurements(const TrajectorySeed &seed, const TempTrajectory &traj, std::vector< TrajectoryMeasurement > &result) const
Definition: CkfTrajectoryBuilder.cc:312
TrajectoryStateTransform.h
CkfTrajectoryBuilder::theMaxCand
int theMaxCand
set Event for the internal MeasurementTracker data member
Definition: CkfTrajectoryBuilder.h:63
mps_fire.result
result
Definition: mps_fire.py:303
TempTrajectory::emplace
void emplace(Args &&... args)
Definition: TempTrajectory.h:113
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
TrajectoryMeasurement
Definition: TrajectoryMeasurement.h:25
CkfTrajectoryBuilder::trajectories
TrajectoryContainer trajectories(const TrajectorySeed &seed) const override
trajectories building starting from a seed
Definition: CkfTrajectoryBuilder.cc:58
edm::ConsumesCollector
Definition: ConsumesCollector.h:39
TrajectoryStateOnSurface::isValid
bool isValid() const
Definition: TrajectoryStateOnSurface.h:54
BaseCkfTrajectoryBuilder::createStartingTrajectory
TempTrajectory createStartingTrajectory(const TrajectorySeed &seed) const
Definition: BaseCkfTrajectoryBuilder.cc:105
SurveyInfoScenario_cff.seed
seed
Definition: SurveyInfoScenario_cff.py:295
hit
Definition: SiStripHitEffFromCalibTree.cc:88
MeasurementTracker.h
IntermediateTrajectoryCleaner::clean
static void clean(TempTrajectoryContainer &tracks)
Definition: IntermediateTrajectoryCleaner.cc:11
TempTrajectory::empty
bool empty() const
True if trajectory has no measurements.
Definition: TempTrajectory.h:210