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