CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CkfTrackCandidateMakerBase.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 
8 
12 
14 
19 
24 
25 
30 
33 
34 #include<algorithm>
35 #include<functional>
36 
38 
39 using namespace edm;
40 using namespace std;
41 
42 namespace cms{
43  CkfTrackCandidateMakerBase::CkfTrackCandidateMakerBase(edm::ParameterSet const& conf) :
44 
45  conf_(conf),
46  theTrackCandidateOutput(true),
47  theTrajectoryOutput(false),
48  useSplitting(conf.getParameter<bool>("useHitsSplitting")),
49  doSeedingRegionRebuilding(conf.getParameter<bool>("doSeedingRegionRebuilding")),
50  cleanTrajectoryAfterInOut(conf.getParameter<bool>("cleanTrajectoryAfterInOut")),
51  theMaxNSeeds(conf.getParameter<unsigned int>("maxNSeeds")),
52  theTrajectoryBuilderName(conf.getParameter<std::string>("TrajectoryBuilder")),
53  theTrajectoryBuilder(0),
54  theTrajectoryCleanerName(conf.getParameter<std::string>("TrajectoryCleaner")),
55  theTrajectoryCleaner(0),
56  theInitialState(0),
57  theNavigationSchoolName(conf.getParameter<std::string>("NavigationSchool")),
58  theNavigationSchool(0),
59  theSeedCleaner(0),
60  maxSeedsBeforeCleaning_(0)
61  {
62  //produces<TrackCandidateCollection>();
63  // old configuration totally descoped.
64  // if (!conf.exists("src"))
65  // theSeedLabel = InputTag(conf_.getParameter<std::string>("SeedProducer"),conf_.getParameter<std::string>("SeedLabel"));
66  // else
68  if ( conf.exists("maxSeedsBeforeCleaning") )
69  maxSeedsBeforeCleaning_=conf.getParameter<unsigned int>("maxSeedsBeforeCleaning");
70  }
71 
72 
73  // Virtual destructor needed.
75  delete theInitialState;
76  if (theSeedCleaner) delete theSeedCleaner;
77  }
78 
80  {
81  std::string cleaner = conf_.getParameter<std::string>("RedundantSeedCleaner");
82  if (cleaner == "SeedCleanerByHitPosition") {
84  } else if (cleaner == "SeedCleanerBySharedInput") {
86  } else if (cleaner == "CachingSeedCleanerByHitPosition") {
88  } else if (cleaner == "CachingSeedCleanerBySharedInput") {
90  } else if (cleaner == "none") {
91  theSeedCleaner = 0;
92  } else {
93  throw cms::Exception("RedundantSeedCleaner not found", cleaner);
94  }
95  }
96 
98 
99  //services
102 
103  if (!theInitialState){
104  // constructor uses the EventSetup, it must be in the setEventSetup were it has a proper value.
105  // get nested parameter set for the TransientInitialStateEstimator
106  ParameterSet tise_params = conf_.getParameter<ParameterSet>("TransientInitialStateEstimatorParameters") ;
107  theInitialState = new TransientInitialStateEstimator( es,tise_params);
108  }
109 
111 
112  edm::ESHandle<TrajectoryCleaner> trajectoryCleanerH;
113  es.get<TrajectoryCleaner::Record>().get(theTrajectoryCleanerName, trajectoryCleanerH);
114  theTrajectoryCleaner= trajectoryCleanerH.product();
115 
116  edm::ESHandle<NavigationSchool> navigationSchoolH;
117  es.get<NavigationSchoolRecord>().get(theNavigationSchoolName, navigationSchoolH);
118  theNavigationSchool = navigationSchoolH.product();
119 
120  // set the TrajectoryBuilder
121  edm::ESHandle<TrajectoryBuilder> theTrajectoryBuilderHandle;
122  es.get<CkfComponentsRecord>().get(theTrajectoryBuilderName,theTrajectoryBuilderHandle);
123  theTrajectoryBuilder = theTrajectoryBuilderHandle.product();
124 
125  }
126 
127  // Functions that gets called by framework every event
129  {
130  // getting objects from the EventSetup
131  setEventSetup( es );
132 
133  // set the correct navigation
135 
136  // propagator
137  edm::ESHandle<Propagator> thePropagator;
138  es.get<TrackingComponentsRecord>().get("AnyDirectionAnalyticalPropagator",
139  thePropagator);
140 
141  // method for Debugging
143 
144  // Step A: set Event for the TrajectoryBuilder
146 
147  // Step B: Retrieve seeds
148 
150  e.getByLabel(theSeedLabel, collseed);
151 
152  // Step C: Create empty output collection
153  std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
154  std::auto_ptr<std::vector<Trajectory> > outputT (new std::vector<Trajectory>());
155 
156  if ( (*collseed).size()>theMaxNSeeds ) {
157  LogError("TooManySeeds")<<"Exceeded maximum numeber of seeds! theMaxNSeeds="<<theMaxNSeeds<<" nSeed="<<(*collseed).size();
158  if (theTrackCandidateOutput){ e.put(output);}
159  if (theTrajectoryOutput){e.put(outputT);}
161  return;
162  }
163 
164  // Step D: Invoke the building algorithm
165  if ((*collseed).size()>0){
166 
167  unsigned int lastCleanResult=0;
168  vector<Trajectory> rawResult;
169  rawResult.reserve(collseed->size() * 4);
170 
171  if (theSeedCleaner) theSeedCleaner->init( &rawResult );
172 
173  // method for debugging
175 
176  vector<Trajectory> theTmpTrajectories;
177 
178  // Loop over seeds
179  size_t collseed_size = collseed->size();
180  for (size_t j = 0; j < collseed_size; j++){
181 
182  // Check if seed hits already used by another track
183  if (theSeedCleaner && !theSeedCleaner->good( &((*collseed)[j])) ) {
184  LogDebug("CkfTrackCandidateMakerBase")<<" Seed cleaning kills seed "<<j;
185  continue;
186  }
187 
188  // Build trajectory from seed outwards
189  theTmpTrajectories.clear();
190  theTrajectoryBuilder->trajectories( (*collseed)[j], theTmpTrajectories );
191 
192 
193  LogDebug("CkfPattern") << "======== In-out trajectory building found " << theTmpTrajectories.size()
194  << " trajectories from seed " << j << " ========"<<endl
195  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
196 
198 
199  // Select the best trajectory from this seed (declare others invalid)
200  theTrajectoryCleaner->clean(theTmpTrajectories);
201 
202  LogDebug("CkfPattern") << "======== In-out trajectory cleaning gave the following valid trajectories from seed "
203  << j << " ========"<<endl
204  << PrintoutHelper::dumpCandidates(theTmpTrajectories);
205  }
206 
207  // Optionally continue building trajectory back through
208  // seed and if possible further inwards.
210  theTrajectoryBuilder->rebuildSeedingRegion((*collseed)[j],theTmpTrajectories);
211 
212  LogDebug("CkfPattern") << "======== Out-in trajectory building found " << theTmpTrajectories.size()
213  << " valid/invalid trajectories from seed " << j << " ========"<<endl
214  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
215  }
216 
217  // Select the best trajectory from this seed (after seed region rebuilding, can be more than one)
218  theTrajectoryCleaner->clean(theTmpTrajectories);
219 
220  LogDebug("CkfPattern") << "======== Trajectory cleaning gave the following valid trajectories from seed "
221  << j << " ========"<<endl
222  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
223 
224  for(vector<Trajectory>::iterator it=theTmpTrajectories.begin();
225  it!=theTmpTrajectories.end(); it++){
226  if( it->isValid() ) {
227  it->setSeedRef(collseed->refAt(j));
228  // Store trajectory
229  rawResult.push_back(*it);
230  // Tell seed cleaner which hits this trajectory used.
231  //TO BE FIXED: this cut should be configurable via cfi file
232  if (theSeedCleaner && it->foundHits()>3) theSeedCleaner->add( & (*it) );
233  //if (theSeedCleaner ) theSeedCleaner->add( & (*it) );
234  }
235  }
236 
237  theTmpTrajectories.clear();
238 
239  LogDebug("CkfPattern") << "rawResult trajectories found so far = " << rawResult.size();
240 
241  if ( maxSeedsBeforeCleaning_ >0 && rawResult.size() > maxSeedsBeforeCleaning_+lastCleanResult) {
242  theTrajectoryCleaner->clean(rawResult);
243  rawResult.erase(std::remove_if(rawResult.begin(),rawResult.end(),
244  std::not1(std::mem_fun_ref(&Trajectory::isValid))),
245  rawResult.end());
246  lastCleanResult=rawResult.size();
247  }
248 
249  }
250  // end of loop over seeds
251 
253 
254  // Step E: Clean the results to avoid duplicate tracks
255  // Rejected ones just flagged as invalid.
256  theTrajectoryCleaner->clean(rawResult);
257 
258  LogDebug("CkfPattern") << "======== Final cleaning of entire event found " << rawResult.size()
259  << " valid/invalid trajectories ======="<<endl
260  <<PrintoutHelper::dumpCandidates(rawResult);
261 
262  LogDebug("CkfPattern") << "removing invalid trajectories.";
263 
264  vector<Trajectory> & unsmoothedResult(rawResult);
265  unsmoothedResult.erase(std::remove_if(unsmoothedResult.begin(),unsmoothedResult.end(),
266  std::not1(std::mem_fun_ref(&Trajectory::isValid))),
267  unsmoothedResult.end());
268 
269 
270  // for (vector<Trajectory>::const_iterator itraw = rawResult.begin();
271  // itraw != rawResult.end(); itraw++) {
272  //if((*itraw).isValid()) unsmoothedResult.push_back( *itraw);
273  //}
274 
275  //analyseCleanedTrajectories(unsmoothedResult);
276 
278  // Step F: Convert to TrackCandidates
279  output->reserve(unsmoothedResult.size());
280  for (vector<Trajectory>::const_iterator it = unsmoothedResult.begin();
281  it != unsmoothedResult.end(); it++) {
282 
284  //it->recHitsV(thits);
285  LogDebug("CkfPattern") << "retrieving "<<(useSplitting?"splitted":"un-splitted")<<" hits from trajectory";
286  it->recHitsV(thits,useSplitting);
288  recHits.reserve(thits.size());
289  LogDebug("CkfPattern") << "cloning hits into new collection.";
290  for (Trajectory::RecHitContainer::const_iterator hitIt = thits.begin();
291  hitIt != thits.end(); hitIt++) {
292  recHits.push_back( (**hitIt).hit()->clone());
293  }
294 
295  LogDebug("CkfPattern") << "getting initial state.";
296  const bool doBackFit = !doSeedingRegionRebuilding;
297  std::pair<TrajectoryStateOnSurface, const GeomDet*> initState =
298  theInitialState->innerState( *it , doBackFit);
299 
300  // temporary protection againt invalid initial states
301  if (! initState.first.isValid() || initState.second == 0) {
302  //cout << "invalid innerState, will not make TrackCandidate" << endl;
303  continue;
304  }
305 
307  if(useSplitting && (initState.second != thits.front()->det()) && thits.front()->det() ){
308  LogDebug("CkfPattern") << "propagating to hit front in case of splitting.";
309  TrajectoryStateOnSurface propagated = thePropagator->propagate(initState.first,thits.front()->det()->surface());
310  if (!propagated.isValid()) continue;
311  state = TrajectoryStateTransform().persistentState(propagated,
312  thits.front()->det()->geographicalId().rawId());
313  }
314 
315  if(!state) state = TrajectoryStateTransform().persistentState( initState.first,
316  initState.second->geographicalId().rawId());
317 
318  LogDebug("CkfPattern") << "pushing a TrackCandidate.";
319  output->push_back(TrackCandidate(recHits,it->seed(),*state,it->seedRef() ) );
320 
321  delete state;
322  }
323  }//output trackcandidates
324 
326  es.get<TrackerDigiGeometryRecord>().get(tracker);
327  LogTrace("CkfPattern|TrackingRegressionTest") << "========== CkfTrackCandidateMaker Info =========="
328  << "number of Seed: " << collseed->size()<<endl
329  <<PrintoutHelper::regressionTest(*tracker,unsmoothedResult);
330 
331 
332 
333  if (theTrajectoryOutput){ outputT->swap(unsmoothedResult);}
334 
335  }// end of ((*collseed).size()>0)
336 
337  // method for debugging
339 
340  // Step G: write output to file
341  if (theTrackCandidateOutput){ e.put(output);}
342  if (theTrajectoryOutput){e.put(outputT);}
343 
344  //reset the MT.
346  }
347 
348 }
349 
#define LogDebug(id)
T getParameter(std::string const &) const
static std::string dumpCandidates(collection &candidates)
void setEventSetup(const edm::EventSetup &es)
Call this at each event until this object will come from the EventSetup as it should.
virtual void rebuildSeedingRegion(const TrajectorySeed &, TrajectoryContainer &result) const
const TrajectoryBuilder * theTrajectoryBuilder
std::vector< TrackCandidate > TrackCandidateCollection
bool exists(std::string const &parameterName) const
checks if a parameter exists
virtual void printHitsDebugger(edm::Event &e)
edm::ESHandle< GeometricSearchTracker > theGeomSearchTracker
virtual void done()=0
Tells the cleaner that the seeds are finished, and so it can clear any cache it has.
virtual void add(const Trajectory *traj)=0
Informs the cleaner that a new trajectory has been made, in case the cleaner keeps a local collection...
virtual bool good(const TrajectorySeed *seed)=0
Returns true if the seed is not overlapping with another trajectory.
static std::string regressionTest(const TrackerGeometry &tracker, std::vector< Trajectory > &unsmoothedResult)
void push_back(D *&d)
Definition: OwnVector.h:290
const TrajectoryCleaner * theTrajectoryCleaner
const NavigationSchool * theNavigationSchool
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
virtual void produceBase(edm::Event &e, const edm::EventSetup &es)
virtual void clean(TrajectoryContainer &) const
int j
Definition: DBlmapReader.cc:9
PTrajectoryStateOnDet * persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid) const
virtual void unset() const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
#define LogTrace(id)
tuple conf
Definition: dbtoconf.py:185
virtual TrajectoryContainer trajectories(const TrajectorySeed &) const =0
virtual void beginRunBase(edm::Run &, edm::EventSetup const &es)
bool isValid() const
Definition: Trajectory.h:225
ConstRecHitContainer RecHitContainer
Definition: Trajectory.h:44
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
TransientInitialStateEstimator * theInitialState
char state
Definition: procUtils.cc:75
void setEventSetup(const edm::EventSetup &es)
Initialize EventSetup objects at each event.
virtual void setEvent(const edm::Event &event) const =0
std::pair< TrajectoryStateOnSurface, const GeomDet * > innerState(const Trajectory &traj, bool doBackFit=true) const
virtual void init(const std::vector< Trajectory > *vect)=0
Provides the cleaner a pointer to the vector where trajectories are stored, in case it does not want ...
void reserve(size_t)
Definition: OwnVector.h:284
Definition: Run.h:32
edm::ESHandle< MagneticField > theMagField