CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CkfTrajectoryBuilder.cc
Go to the documentation of this file.
2 
4 
6 
10 
17 
18 
21 
23 
27 
28 using namespace std;
29 
30 
33  const TrajectoryStateUpdator* updator,
34  const Propagator* propagatorAlong,
35  const Propagator* propagatorOpposite,
36  const Chi2MeasurementEstimatorBase* estimator,
37  const TransientTrackingRecHitBuilder* recHitBuilder,
38  const TrajectoryFilter* filter):
39 
41  updator, propagatorAlong,propagatorOpposite,
42  estimator, recHitBuilder, filter)
43 {
44  theMaxCand = conf.getParameter<int>("maxCand");
45  theLostHitPenalty = conf.getParameter<double>("lostHitPenalty");
46  theIntermediateCleaning = conf.getParameter<bool>("intermediateCleaning");
47  theAlwaysUseInvalidHits = conf.getParameter<bool>("alwaysUseInvalidHits");
48  /*
49  theSharedSeedCheck = conf.getParameter<bool>("SharedSeedCheck");
50  std::stringstream ss;
51  ss<<"CkfTrajectoryBuilder_"<<conf.getParameter<std::string>("ComponentName")<<"_"<<this;
52  theUniqueName = ss.str();
53  LogDebug("CkfPattern")<<"my unique name is: "<<theUniqueName;
54  */
55 }
56 
57 /*
58  void CkfTrajectoryBuilder::setEvent(const edm::Event& event) const
59  {
60  theMeasurementTracker->update(event);
61  }
62 */
63 
66 {
68  result.reserve(5);
69  trajectories(seed, result);
70  return result;
71 }
72 
73 /*
74  void CkfTrajectoryBuilder::rememberSeedAndTrajectories(const TrajectorySeed& seed,
75  CkfTrajectoryBuilder::TrajectoryContainer &result) const
76  {
77 
78  //result ----> theCachedTrajectories
79  //every first iteration on event. forget about everything that happened before
80  if (edm::Service<UpdaterService>()->checkOnce(theUniqueName))
81  theCachedTrajectories.clear();
82 
83  if (seed.nHits()==0) return;
84 
85  //then remember those trajectories
86  for (TrajectoryContainer::iterator traj=result.begin();
87  traj!=result.end(); ++traj) {
88  theCachedTrajectories.insert(std::make_pair(seed.recHits().first->geographicalId(),*traj));
89  }
90  }
91 
92  bool CkfTrajectoryBuilder::sharedSeed(const TrajectorySeed& s1,const TrajectorySeed& s2) const{
93  //quit right away on nH=0
94  if (s1.nHits()==0 || s2.nHits()==0) return false;
95  //quit right away if not the same number of hits
96  if (s1.nHits()!=s2.nHits()) return false;
97  TrajectorySeed::range r1=s1.recHits();
98  TrajectorySeed::range r2=s2.recHits();
99  TrajectorySeed::const_iterator i1,i2;
100  TrajectorySeed::const_iterator & i1_e=r1.second,&i2_e=r2.second;
101  TrajectorySeed::const_iterator & i1_b=r1.first,&i2_b=r2.first;
102  //quit right away if first detId does not match. front exist because of ==0 ->quit test
103  if(i1_b->geographicalId() != i2_b->geographicalId()) return false;
104  //then check hit by hit if they are the same
105  for (i1=i1_b,i2=i2_b;i1!=i1_e,i2!=i2_e;++i1,++i2){
106  if (!i1->sharesInput(&(*i2),TrackingRecHit::all)) return false;
107  }
108  return true;
109  }
110  bool CkfTrajectoryBuilder::seedAlreadyUsed(const TrajectorySeed& seed,
111  CkfTrajectoryBuilder::TempTrajectoryContainer &candidates) const
112  {
113  //theCachedTrajectories ---> candidates
114  if (seed.nHits()==0) return false;
115  bool answer=false;
116  pair<SharedTrajectory::const_iterator, SharedTrajectory::const_iterator> range =
117  theCachedTrajectories.equal_range(seed.recHits().first->geographicalId());
118  SharedTrajectory::const_iterator trajP;
119  for (trajP = range.first; trajP!=range.second;++trajP){
120  //check whether seeds are identical
121  if (sharedSeed(trajP->second.seed(),seed)){
122  candidates.push_back(trajP->second);
123  answer=true;
124  }//already existing trajectory shares the seed.
125  }//loop already made trajectories
126 
127  return answer;
128  }
129 */
130 
131 void
133 {
134  // analyseSeed( seed);
135  /*
136  if (theSharedSeedCheck){
137  TempTrajectoryContainer candidates;
138  if (seedAlreadyUsed(seed,candidates))
139  {
140  //start with those candidates already made before
141  limitedCandidates(candidates,result);
142  //and quit
143  return;
144  }
145  }
146  */
147 
148  buildTrajectories(seed, result,nullptr);
149 }
150 
153  const TrajectoryFilter*) const {
154  if (theMeasurementTracker == 0) {
155  throw cms::Exception("LogicError") << "Asking to create trajectories to an un-initialized CkfTrajectoryBuilder.\nYou have to call clone(const MeasurementTrackerEvent *data) and then call trajectories on it instead.\n";
156  }
157 
158  TempTrajectory startingTraj = createStartingTrajectory( seed );
159 
162  limitedCandidates(seed, startingTraj, result);
163 
164  return startingTraj;
165 
166  /*
167  //and remember what you just did
168  if (theSharedSeedCheck) rememberSeedAndTrajectories(seed,result);
169  */
170 
171  // analyseResult(result);
172 }
173 
177 {
178  TempTrajectoryContainer candidates;
179  candidates.push_back( startingTraj);
180  boost::shared_ptr<const TrajectorySeed> sharedSeed(new TrajectorySeed(seed));
181  limitedCandidates(sharedSeed, candidates,result);
182 }
183 
185 limitedCandidates(const boost::shared_ptr<const TrajectorySeed> & sharedSeed, TempTrajectoryContainer &candidates,
187 {
188  unsigned int nIter=1;
189  TempTrajectoryContainer newCand; // = TrajectoryContainer();
190 
191 
192  while ( !candidates.empty()) {
193 
194  newCand.clear();
195  for (TempTrajectoryContainer::iterator traj=candidates.begin();
196  traj!=candidates.end(); traj++) {
197  std::vector<TM> meas;
198  findCompatibleMeasurements(*sharedSeed, *traj, meas);
199 
200  // --- method for debugging
201  if(!analyzeMeasurementsDebugger(*traj,meas,
204  theTTRHBuilder)) return;
205  // ---
206 
207  if ( meas.empty()) {
208  if ( qualityFilter( *traj)) addToResult(sharedSeed, *traj, result);
209  }
210  else {
211  std::vector<TM>::const_iterator last;
212  if ( theAlwaysUseInvalidHits) last = meas.end();
213  else {
214  if (meas.front().recHit()->isValid()) {
215  last = find_if( meas.begin(), meas.end(), RecHitIsInvalid());
216  }
217  else last = meas.end();
218  }
219 
220  for( std::vector<TM>::const_iterator itm = meas.begin();
221  itm != last; itm++) {
222  TempTrajectory newTraj = *traj;
223  updateTrajectory( newTraj, *itm);
224 
225  if ( toBeContinued(newTraj)) {
226  newCand.push_back(newTraj);
227  }
228  else {
229  if ( qualityFilter(newTraj)) addToResult(sharedSeed, newTraj, result);
231  }
232  }
233  }
234 
235  if ((int)newCand.size() > theMaxCand) {
236  sort( newCand.begin(), newCand.end(), TrajCandLess<TempTrajectory>(theLostHitPenalty));
237  newCand.erase( newCand.begin()+theMaxCand, newCand.end());
238  }
239  }
240 
242 
243  candidates.swap(newCand);
244 
245  LogDebug("CkfPattern") <<result.size()<<" candidates after "<<nIter++<<" CKF iteration: \n"
247  <<"\n "<<candidates.size()<<" running candidates are: \n"
248  <<PrintoutHelper::dumpCandidates(candidates);
249 
250  }
251 }
252 
253 
254 
256  const TM& tm) const
257 {
258  TSOS predictedState = tm.predictedState();
260 
261  if ( hit->isValid()) {
262  TM tmp = TM( predictedState, theUpdator->update( predictedState, *hit),
263  hit, tm.estimate(), tm.layer());
264  traj.push(tmp );
265  }
266  else {
267  traj.push( TM( predictedState, hit, 0, tm.layer()));
268  }
269 }
270 
271 
272 void
274  const TempTrajectory& traj,
275  std::vector<TrajectoryMeasurement> & result) const
276 {
277  int invalidHits = 0;
278  std::pair<TSOS,std::vector<const DetLayer*> > stateAndLayers = findStateAndLayers(traj);
279  if (stateAndLayers.second.empty()) return;
280 
281  vector<const DetLayer*>::iterator layerBegin = stateAndLayers.second.begin();
282  vector<const DetLayer*>::iterator layerEnd = stateAndLayers.second.end();
283  LogDebug("CkfPattern")<<"looping on "<< stateAndLayers.second.size()<<" layers.";
284  for (vector<const DetLayer*>::iterator il = layerBegin;
285  il != layerEnd; il++) {
286 
287  LogDebug("CkfPattern")<<"looping on a layer in findCompatibleMeasurements.\n last layer: "<<traj.lastLayer()<<" current layer: "<<(*il);
288 
289  TSOS stateToUse = stateAndLayers.first;
290  if ((*il)==traj.lastLayer())
291  {
292  LogDebug("CkfPattern")<<" self propagating in findCompatibleMeasurements.\n from: \n"<<stateToUse;
293  //self navigation case
294  // go to a middle point first
296  GlobalPoint center(0,0,0);
297  stateToUse = middle.extrapolate(stateToUse, center, *theForwardPropagator);
298 
299  if (!stateToUse.isValid()) continue;
300  LogDebug("CkfPattern")<<"to: "<<stateToUse;
301  }
302 
304  vector<TrajectoryMeasurement> tmp = layerMeasurements.measurements((**il),stateToUse, *theForwardPropagator, *theEstimator);
305 
306  if ( !tmp.empty()) {
307  if ( result.empty()) result = tmp;
308  else {
309  // keep one dummy TM at the end, skip the others
310  result.insert( result.end()-invalidHits, tmp.begin(), tmp.end());
311  }
312  invalidHits++;
313  }
314  }
315 
316  // sort the final result, keep dummy measurements at the end
317  if ( result.size() > 1) {
318  sort( result.begin(), result.end()-invalidHits, TrajMeasLessEstim());
319  }
320 
321  LogDebug("CkfPattern")<<"starting from:\n"
322  <<"x: "<<stateAndLayers.first.globalPosition()<<"\n"
323  <<"p: "<<stateAndLayers.first.globalMomentum()<<"\n"
325 
326 #ifdef DEBUG_INVALID
327  bool afterInvalid = false;
328  for (vector<TM>::const_iterator i=result.begin();
329  i!=result.end(); i++) {
330  if ( ! i->recHit().isValid()) afterInvalid = true;
331  if (afterInvalid && i->recHit().isValid()) {
332  edm::LogError("CkfPattern") << "CkfTrajectoryBuilder error: valid hit after invalid!" ;
333  }
334  }
335 #endif
336 
337  //analyseMeasurements( result, traj);
338 
339 }
340 
#define LogDebug(id)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
static std::string dumpCandidates(collection &candidates)
std::vector< Trajectory > TrajectoryContainer
TrajectoryStateOnSurface const & predictedState() const
ConstRecHitPointer const & recHit() const
static void clean(TempTrajectoryContainer &tracks)
virtual TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TransientTrackingRecHit &) const =0
virtual bool analyzeMeasurementsDebugger(Trajectory &traj, const std::vector< TrajectoryMeasurement > &meas, const MeasurementTrackerEvent *theMeasurementTracker, const Propagator *theForwardPropagator, const Chi2MeasurementEstimatorBase *theEstimator, const TransientTrackingRecHitBuilder *theTTRHBuilder) const
const TrajectoryStateUpdator * theUpdator
CkfTrajectoryBuilder(const edm::ParameterSet &conf, const TrajectoryStateUpdator *updator, const Propagator *propagatorAlong, const Propagator *propagatorOpposite, const Chi2MeasurementEstimatorBase *estimator, const TransientTrackingRecHitBuilder *recHitBuilder, const TrajectoryFilter *filter)
TempTrajectory buildTrajectories(const TrajectorySeed &, TrajectoryContainer &ret, const TrajectoryFilter *) const
static std::string dumpMeasurements(const std::vector< TrajectoryMeasurement > &v)
bool qualityFilter(const TempTrajectory &traj, bool inOut=false) const
const TransientTrackingRecHitBuilder * theTTRHBuilder
tuple result
Definition: query.py:137
const DetLayer * layer() const
void addToResult(boost::shared_ptr< const TrajectorySeed > const &seed, TempTrajectory &traj, TrajectoryContainer &result, bool inOut=false) const
const MeasurementTracker & measurementTracker() const
tuple conf
Definition: dbtoconf.py:185
void updateTrajectory(TempTrajectory &traj, const TM &tm) const
StateAndLayers findStateAndLayers(const TrajectorySeed &seed, const TempTrajectory &traj) const
const MeasurementTrackerEvent * theMeasurementTracker
virtual TrajectoryContainer trajectories(const TrajectorySeed &seed) const
trajectories building starting from a seed
std::vector< TempTrajectory > TempTrajectoryContainer
TrajectoryStateOnSurface extrapolate(const FreeTrajectoryState &fts, const GlobalPoint &vtx) const
extrapolation with default (=geometrical) propagator
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
bool toBeContinued(TempTrajectory &traj, bool inOut=false) const
virtual void findCompatibleMeasurements(const TrajectorySeed &seed, const TempTrajectory &traj, std::vector< TrajectoryMeasurement > &result) const
const DetLayer * lastLayer() const
Redundant method, returns the layer of lastMeasurement() .
int theMaxCand
set Event for the internal MeasurementTracker data member
std::vector< Trajectory > TrajectoryContainer
const Chi2MeasurementEstimatorBase * theEstimator
TempTrajectory createStartingTrajectory(const TrajectorySeed &seed) const
void limitedCandidates(const TrajectorySeed &seed, TempTrajectory &startingTraj, TrajectoryContainer &result) const
void push(const TrajectoryMeasurement &tm)
const Propagator * theForwardPropagator