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  {
61  //produces<TrackCandidateCollection>();
62  // old configuration totally descoped.
63  // if (!conf.exists("src"))
64  // theSeedLabel = InputTag(conf_.getParameter<std::string>("SeedProducer"),conf_.getParameter<std::string>("SeedLabel"));
65  // else
67  }
68 
69 
70  // Virtual destructor needed.
72  delete theInitialState;
73  if (theSeedCleaner) delete theSeedCleaner;
74  }
75 
77  {
78  std::string cleaner = conf_.getParameter<std::string>("RedundantSeedCleaner");
79  if (cleaner == "SeedCleanerByHitPosition") {
81  } else if (cleaner == "SeedCleanerBySharedInput") {
83  } else if (cleaner == "CachingSeedCleanerByHitPosition") {
85  } else if (cleaner == "CachingSeedCleanerBySharedInput") {
87  } else if (cleaner == "none") {
88  theSeedCleaner = 0;
89  } else {
90  throw cms::Exception("RedundantSeedCleaner not found", cleaner);
91  }
92  }
93 
95 
96  //services
99 
100  if (!theInitialState){
101  // constructor uses the EventSetup, it must be in the setEventSetup were it has a proper value.
102  // get nested parameter set for the TransientInitialStateEstimator
103  ParameterSet tise_params = conf_.getParameter<ParameterSet>("TransientInitialStateEstimatorParameters") ;
104  theInitialState = new TransientInitialStateEstimator( es,tise_params);
105  }
106 
108 
109  edm::ESHandle<TrajectoryCleaner> trajectoryCleanerH;
110  es.get<TrajectoryCleaner::Record>().get(theTrajectoryCleanerName, trajectoryCleanerH);
111  theTrajectoryCleaner= trajectoryCleanerH.product();
112 
113  edm::ESHandle<NavigationSchool> navigationSchoolH;
114  es.get<NavigationSchoolRecord>().get(theNavigationSchoolName, navigationSchoolH);
115  theNavigationSchool = navigationSchoolH.product();
116 
117  // set the TrajectoryBuilder
118  edm::ESHandle<TrajectoryBuilder> theTrajectoryBuilderHandle;
119  es.get<CkfComponentsRecord>().get(theTrajectoryBuilderName,theTrajectoryBuilderHandle);
120  theTrajectoryBuilder = theTrajectoryBuilderHandle.product();
121 
122  }
123 
124  // Functions that gets called by framework every event
126  {
127  // getting objects from the EventSetup
128  setEventSetup( es );
129 
130  // set the correct navigation
132 
133  // propagator
134  edm::ESHandle<Propagator> thePropagator;
135  es.get<TrackingComponentsRecord>().get("AnyDirectionAnalyticalPropagator",
136  thePropagator);
137 
138  // method for Debugging
140 
141  // Step A: set Event for the TrajectoryBuilder
143 
144  // Step B: Retrieve seeds
145 
147  e.getByLabel(theSeedLabel, collseed);
148 
149  // Step C: Create empty output collection
150  std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
151  std::auto_ptr<std::vector<Trajectory> > outputT (new std::vector<Trajectory>());
152 
153  if ( (*collseed).size()>theMaxNSeeds ) {
154  LogError("TooManySeeds")<<"Exceeded maximum numeber of seeds! theMaxNSeeds="<<theMaxNSeeds<<" nSeed="<<(*collseed).size();
155  if (theTrackCandidateOutput){ e.put(output);}
156  if (theTrajectoryOutput){e.put(outputT);}
157  return;
158  }
159 
160  // Step D: Invoke the building algorithm
161  if ((*collseed).size()>0){
162 
163  vector<Trajectory> rawResult;
164  rawResult.reserve(collseed->size() * 4);
165 
166  if (theSeedCleaner) theSeedCleaner->init( &rawResult );
167 
168  // method for debugging
170 
171  vector<Trajectory> theTmpTrajectories;
172 
173  // Loop over seeds
174  size_t collseed_size = collseed->size();
175  for (size_t j = 0; j < collseed_size; j++){
176 
177  // Check if seed hits already used by another track
178  if (theSeedCleaner && !theSeedCleaner->good( &((*collseed)[j])) ) {
179  LogDebug("CkfTrackCandidateMakerBase")<<" Seed cleaning kills seed "<<j;
180  continue;
181  }
182 
183  // Build trajectory from seed outwards
184  theTmpTrajectories.clear();
185  theTrajectoryBuilder->trajectories( (*collseed)[j], theTmpTrajectories );
186 
187 
188  LogDebug("CkfPattern") << "======== In-out trajectory building found " << theTmpTrajectories.size()
189  << " trajectories from seed " << j << " ========"<<endl
190  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
191 
193 
194  // Select the best trajectory from this seed (declare others invalid)
195  theTrajectoryCleaner->clean(theTmpTrajectories);
196 
197  LogDebug("CkfPattern") << "======== In-out trajectory cleaning gave the following valid trajectories from seed "
198  << j << " ========"<<endl
199  << PrintoutHelper::dumpCandidates(theTmpTrajectories);
200  }
201 
202  // Optionally continue building trajectory back through
203  // seed and if possible further inwards.
205  theTrajectoryBuilder->rebuildSeedingRegion((*collseed)[j],theTmpTrajectories);
206 
207  LogDebug("CkfPattern") << "======== Out-in trajectory building found " << theTmpTrajectories.size()
208  << " valid/invalid trajectories from seed " << j << " ========"<<endl
209  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
210  }
211 
212  // Select the best trajectory from this seed (after seed region rebuilding, can be more than one)
213  theTrajectoryCleaner->clean(theTmpTrajectories);
214 
215  LogDebug("CkfPattern") << "======== Trajectory cleaning gave the following valid trajectories from seed "
216  << j << " ========"<<endl
217  <<PrintoutHelper::dumpCandidates(theTmpTrajectories);
218 
219  for(vector<Trajectory>::iterator it=theTmpTrajectories.begin();
220  it!=theTmpTrajectories.end(); it++){
221  if( it->isValid() ) {
222  it->setSeedRef(collseed->refAt(j));
223  // Store trajectory
224  rawResult.push_back(*it);
225  // Tell seed cleaner which hits this trajectory used.
226  //TO BE FIXED: this cut should be configurable via cfi file
227  if (theSeedCleaner && it->foundHits()>3) theSeedCleaner->add( & (*it) );
228  //if (theSeedCleaner ) theSeedCleaner->add( & (*it) );
229  }
230  }
231 
232  theTmpTrajectories.clear();
233 
234  LogDebug("CkfPattern") << "rawResult trajectories found so far = " << rawResult.size();
235  }
236  // end of loop over seeds
237 
239 
240  // Step E: Clean the results to avoid duplicate tracks
241  // Rejected ones just flagged as invalid.
242  theTrajectoryCleaner->clean(rawResult);
243 
244  LogDebug("CkfPattern") << "======== Final cleaning of entire event found " << rawResult.size()
245  << " valid/invalid trajectories ======="<<endl
246  <<PrintoutHelper::dumpCandidates(rawResult);
247 
248  LogDebug("CkfPattern") << "removing invalid trajectories.";
249 
250  vector<Trajectory> & unsmoothedResult(rawResult);
251  unsmoothedResult.erase(std::remove_if(unsmoothedResult.begin(),unsmoothedResult.end(),
252  std::not1(std::mem_fun_ref(&Trajectory::isValid))),
253  unsmoothedResult.end());
254 
255 
256  // for (vector<Trajectory>::const_iterator itraw = rawResult.begin();
257  // itraw != rawResult.end(); itraw++) {
258  //if((*itraw).isValid()) unsmoothedResult.push_back( *itraw);
259  //}
260 
261  //analyseCleanedTrajectories(unsmoothedResult);
262 
264  // Step F: Convert to TrackCandidates
265  output->reserve(unsmoothedResult.size());
266  for (vector<Trajectory>::const_iterator it = unsmoothedResult.begin();
267  it != unsmoothedResult.end(); it++) {
268 
270  //it->recHitsV(thits);
271  LogDebug("CkfPattern") << "retrieving "<<(useSplitting?"splitted":"un-splitted")<<" hits from trajectory";
272  it->recHitsV(thits,useSplitting);
274  recHits.reserve(thits.size());
275  LogDebug("CkfPattern") << "cloning hits into new collection.";
276  for (Trajectory::RecHitContainer::const_iterator hitIt = thits.begin();
277  hitIt != thits.end(); hitIt++) {
278  recHits.push_back( (**hitIt).hit()->clone());
279  }
280 
281  LogDebug("CkfPattern") << "getting initial state.";
282  const bool doBackFit = !doSeedingRegionRebuilding;
283  std::pair<TrajectoryStateOnSurface, const GeomDet*> initState =
284  theInitialState->innerState( *it , doBackFit);
285 
286  // temporary protection againt invalid initial states
287  if (! initState.first.isValid() || initState.second == 0) {
288  //cout << "invalid innerState, will not make TrackCandidate" << endl;
289  continue;
290  }
291 
293  if(useSplitting && (initState.second != thits.front()->det()) && thits.front()->det() ){
294  LogDebug("CkfPattern") << "propagating to hit front in case of splitting.";
295  TrajectoryStateOnSurface propagated = thePropagator->propagate(initState.first,thits.front()->det()->surface());
296  if (!propagated.isValid()) continue;
297  state = TrajectoryStateTransform().persistentState(propagated,
298  thits.front()->det()->geographicalId().rawId());
299  }
300 
301  if(!state) state = TrajectoryStateTransform().persistentState( initState.first,
302  initState.second->geographicalId().rawId());
303 
304  LogDebug("CkfPattern") << "pushing a TrackCandidate.";
305  output->push_back(TrackCandidate(recHits,it->seed(),*state,it->seedRef() ) );
306 
307  delete state;
308  }
309  }//output trackcandidates
310 
312  es.get<TrackerDigiGeometryRecord>().get(tracker);
313  LogTrace("CkfPattern|TrackingRegressionTest") << "========== CkfTrackCandidateMaker Info =========="
314  << "number of Seed: " << collseed->size()<<endl
315  <<PrintoutHelper::regressionTest(*tracker,unsmoothedResult);
316 
317 
318 
319  if (theTrajectoryOutput){ outputT->swap(unsmoothedResult);}
320 
321  }// end of ((*collseed).size()>0)
322 
323  // method for debugging
325 
326  // Step G: write output to file
327  if (theTrackCandidateOutput){ e.put(output);}
328  if (theTrajectoryOutput){e.put(outputT);}
329  }
330 }
331 
#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
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:288
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
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
#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:282
Definition: Run.h:31
edm::ESHandle< MagneticField > theMagField