CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackProducerWithSCAssociation.cc
Go to the documentation of this file.
2 // system include files
3 #include <memory>
4 // user include files
19 
21  TrackProducerBase<reco::Track>(iConfig.getParameter<bool>("TrajectoryInEvent")),
22  theAlgo(iConfig)
23 {
24  setConf(iConfig);
25  setSrc( iConfig.getParameter<edm::InputTag>( "src" ),
26  iConfig.getParameter<edm::InputTag>( "beamSpot" ));
27  setAlias( iConfig.getParameter<std::string>( "@module_label" ) );
28 
29  if ( iConfig.exists("clusterRemovalInfo") ) {
30  edm::InputTag tag = iConfig.getParameter<edm::InputTag>("clusterRemovalInfo");
31  if (!(tag == edm::InputTag())) { setClusterRemovalInfo( tag ); }
32  }
33 
34 
35  myname_ = iConfig.getParameter<std::string>("ComponentName");
36  conversionTrackCandidateProducer_ = iConfig.getParameter<std::string>("producer");
37  trackCSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("trackCandidateSCAssociationCollection");
38  trackSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("recoTrackSCAssociationCollection");
39  myTrajectoryInEvent_ = iConfig.getParameter<bool>("TrajectoryInEvent");
40 
41 
42  //register your products
43  produces<reco::TrackCollection>().setBranchAlias( alias_ + "Tracks" );
44  produces<reco::TrackExtraCollection>().setBranchAlias( alias_ + "TrackExtras" );
45  produces<TrackingRecHitCollection>().setBranchAlias( alias_ + "RecHits" );
46  produces<std::vector<Trajectory> >() ;
47  produces<TrajTrackAssociationCollection>();
48  // produces< reco::TrackSuperClusterAssociationCollection > (trackSuperClusterAssociationCollection_ );
49  produces< reco::TrackCaloClusterPtrAssociation > (trackSuperClusterAssociationCollection_ );
50 
51 }
52 
53 
55 {
56  //edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
57 
58  //LogDebug("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
59  // std::cout << " TrackProducerWithSCAssociation Analyzing event number: " << theEvent.id() << "\n";
60 
61 
62  //
63  // create empty output collections
64  //
65  std::auto_ptr<TrackingRecHitCollection> outputRHColl (new TrackingRecHitCollection);
66  std::auto_ptr<reco::TrackCollection> outputTColl(new reco::TrackCollection);
67  std::auto_ptr<reco::TrackExtraCollection> outputTEColl(new reco::TrackExtraCollection);
68  std::auto_ptr<std::vector<Trajectory> > outputTrajectoryColl(new std::vector<Trajectory>);
69  // Reco Track - Super Cluster Association
70  std::auto_ptr<reco::TrackCaloClusterPtrAssociation> scTrkAssoc_p(new reco::TrackCaloClusterPtrAssociation);
71 
72  //
73  //declare and get stuff to be retrieved from ES
74  //
78  edm::ESHandle<Propagator> thePropagator;
81  getFromES(setup,theG,theMF,theFitter,thePropagator,theMeasTk,theBuilder);
82 
83 
84 
85  //
86  //declare and get TrackColection to be retrieved from the event
92  if ( !trkCandidateSCAssocHandle.isValid() ) {
93  // std::cout << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
94  edm::LogError("TrackProducerWithSCAssociation") << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
96  }
97  reco::TrackCandidateCaloClusterPtrAssociation scTrkCandAssCollection = *(trkCandidateSCAssocHandle.product());
98  if ( scTrkCandAssCollection.size() ==0 ) validTrackCandidateSCAssociationInput_=false;
99 
100 
101  std::vector<int> tccLocations;
102  AlgoProductCollection algoResults;
103  reco::BeamSpot bs;
104 
105 
106  getFromEvt(theEvent,theTCCollection,bs);
107 
108  if (theTCCollection.failedToGet()){
109  edm::LogError("TrackProducerWithSCAssociation") <<"TrackProducerWithSCAssociation could not get the TrackCandidateCollection.";}
110  else{
111  //
112  //run the algorithm
113  //
114  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
115  // theAlgo.runWithCandidate(theG.product(), theMF.product(), *theTCCollection,
116  // theFitter.product(), thePropagator.product(), theBuilder.product(), algoResults);
117  // we have to copy this method from the algo in order to get the association track-seed
118  // this is ugly temporary code that should be replaced!!!!!
119  // start of copied code ======================================================
120 
121  // std::cout << "TrackProducerWithSCAssociation Number of TrackCandidates: " << theTCCollection->size() << "\n";
122  try{
123  int cont = 0;
124  int tcc=0;
125 
126  for (TrackCandidateCollection::const_iterator i=theTCCollection->begin(); i!=theTCCollection->end();i++)
127  {
128 
129  const TrackCandidate * theTC = &(*i);
131  const TrackCandidate::range& recHitVec=theTC->recHits();
132  const TrajectorySeed& seed = theTC->seed();
133 
134  //convert PTrajectoryStateOnDet to TrajectoryStateOnSurface
135 
136 
137  DetId detId(state.detId());
139  &(theG.product()->idToDet(detId)->surface()),
140  theMF.product());
141 
142  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Initial TSOS\n" << theTSOS << "\n";
143 
144  //convert the TrackingRecHit vector to a TransientTrackingRecHit vector
145  //meanwhile computes the number of degrees of freedom
147 
148  float ndof=0;
149 
151  i!=recHitVec.second; i++){
152  hits.push_back(theBuilder.product()->build(&(*i) ));
153  }
154 
155 
156  //build Track
157  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation going to buildTrack"<< "\n";
158  bool ok = theAlgo.buildTrack(theFitter.product(),thePropagator.product(),algoResults, hits, theTSOS, seed, ndof, bs, theTC->seedRef());
159  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation buildTrack result: " << ok << "\n";
160  if(ok) {
161  cont++;
162  tccLocations.push_back(tcc);
163  }
164  tcc++;
165  }
166  edm::LogInfo("TrackProducerWithSCAssociation") << "Number of Tracks found: " << cont << "\n";
167  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Number of Tracks found: " << cont << "\n";
168  // end of copied code ======================================================
169 
170  } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
171  //
172  //put everything in the event
173  // we copy putInEvt to get OrphanHandle filled...
174  putInEvt(theEvent,thePropagator.product(),theMeasTk.product(),
175  outputRHColl, outputTColl, outputTEColl, outputTrajectoryColl, algoResults);
176 
177  // now construct associationmap and put it in the event
179  int itrack=0;
180  std::vector<edm::Ptr<reco::CaloCluster> > caloPtrVec;
181  for(AlgoProductCollection::iterator i=algoResults.begin(); i!=algoResults.end();i++){
182  edm::Ref<TrackCandidateCollection> trackCRef(theTCCollection,tccLocations[itrack]);
183  const edm::Ptr<reco::CaloCluster>& aClus = (*trkCandidateSCAssocHandle)[trackCRef];
184  caloPtrVec.push_back( aClus );
185  itrack++;
186  }
187 
188 
189  edm::ValueMap<reco::CaloClusterPtr>::Filler filler(*scTrkAssoc_p);
190  filler.insert(rTracks_, caloPtrVec.begin(), caloPtrVec.end());
191  filler.fill();
192  }
193 
194  theEvent.put(scTrkAssoc_p,trackSuperClusterAssociationCollection_ );
195 
196  }
197 
198 }
199 
200 std::vector<reco::TransientTrack> TrackProducerWithSCAssociation::getTransient(edm::Event& theEvent, const edm::EventSetup& setup)
201 {
202  edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
203  //
204  // create empty output collections
205  //
206  std::vector<reco::TransientTrack> ttks;
207 
208  //
209  //declare and get stuff to be retrieved from ES
210  //
214  edm::ESHandle<Propagator> thePropagator;
217  getFromES(setup,theG,theMF,theFitter,thePropagator,theMeasTk,theBuilder);
218 
219  //
220  //declare and get TrackColection to be retrieved from the event
221  //
222  AlgoProductCollection algoResults;
223  reco::BeamSpot bs;
224 
225  try{
227  getFromEvt(theEvent,theTCCollection,bs);
228 
229  //
230  //run the algorithm
231  //
232  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
233  theAlgo.runWithCandidate(theG.product(), theMF.product(), *theTCCollection,
234  theFitter.product(), thePropagator.product(), theBuilder.product(), bs, algoResults);
235 
236  } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
237 
238 
239  for (AlgoProductCollection::iterator prod=algoResults.begin();prod!=algoResults.end(); prod++){
240  ttks.push_back( reco::TransientTrack(*(((*prod).second).first),thePropagator.product()->magneticField() ));
241  }
242 
243  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation end" << "\n";
244 
245  return ttks;
246 }
247 
248 
249 
250 
252  const Propagator* thePropagator,
253  const MeasurementTracker* theMeasTk,
254  std::auto_ptr<TrackingRecHitCollection>& selHits,
255  std::auto_ptr<reco::TrackCollection>& selTracks,
256  std::auto_ptr<reco::TrackExtraCollection>& selTrackExtras,
257  std::auto_ptr<std::vector<Trajectory> >& selTrajectories,
258  AlgoProductCollection& algoResults)
259 {
260 
263 
267  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
268  std::map<unsigned int, unsigned int> tjTkMap;
269 
270  for(AlgoProductCollection::iterator i=algoResults.begin(); i!=algoResults.end();i++){
271  Trajectory * theTraj = (*i).first;
273  selTrajectories->push_back(*theTraj);
274  iTjRef++;
275  }
276  const TrajectoryFitter::RecHitContainer& transHits = theTraj->recHits();
277 
278  reco::Track * theTrack = (*i).second.first;
279  PropagationDirection seedDir = (*i).second.second;
280 
281  //LogDebug("TrackProducer") << "In KfTrackProducerBase::putInEvt - seedDir=" << seedDir;
282 
283  reco::Track t = * theTrack;
284  selTracks->push_back( t );
285  iTkRef++;
286 
287  // Store indices in local map (starts at 0)
288  if(trajectoryInEvent_) tjTkMap[iTjRef-1] = iTkRef-1;
289 
290  //sets the outermost and innermost TSOSs
291 
292  TrajectoryStateOnSurface outertsos;
293  TrajectoryStateOnSurface innertsos;
294  unsigned int innerId, outerId;
295 
296  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
297  // This is consistent with innermost and outermost labels only for tracks from LHC collision
298  if (theTraj->direction() == alongMomentum) {
299  outertsos = theTraj->lastMeasurement().updatedState();
300  innertsos = theTraj->firstMeasurement().updatedState();
301  outerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
302  innerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
303  } else {
304  outertsos = theTraj->firstMeasurement().updatedState();
305  innertsos = theTraj->lastMeasurement().updatedState();
306  outerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
307  innerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
308  }
309  // ---
310  //build the TrackExtra
311  GlobalPoint v = outertsos.globalParameters().position();
312  GlobalVector p = outertsos.globalParameters().momentum();
313  math::XYZVector outmom( p.x(), p.y(), p.z() );
314  math::XYZPoint outpos( v.x(), v.y(), v.z() );
315  v = innertsos.globalParameters().position();
316  p = innertsos.globalParameters().momentum();
317  math::XYZVector inmom( p.x(), p.y(), p.z() );
318  math::XYZPoint inpos( v.x(), v.y(), v.z() );
319 
320  reco::TrackExtraRef teref= reco::TrackExtraRef ( rTrackExtras, idx ++ );
321  reco::Track & track = selTracks->back();
322  track.setExtra( teref );
323 
324  //======= I want to set the second hitPattern here =============
325  if (theSchool.isValid())
326  {
327  NavigationSetter setter( *theSchool );
328  setSecondHitPattern(theTraj,track,thePropagator,theMeasTk);
329  }
330  //==============================================================
331 
332 
333  selTrackExtras->push_back( reco::TrackExtra (outpos, outmom, true, inpos, inmom, true,
334  outertsos.curvilinearError(), outerId,
335  innertsos.curvilinearError(), innerId,
336  seedDir,theTraj->seedRef()));
337 
338 
339  reco::TrackExtra & tx = selTrackExtras->back();
340  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
341  // This is consistent with innermost and outermost labels only for tracks from LHC collisions
342  size_t k = 0;
343  if (theTraj->direction() == alongMomentum) {
344  for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.begin();
345  j != transHits.end(); j ++ ) {
346  if ((**j).hit()!=0){
347  TrackingRecHit * hit = (**j).hit()->clone();
348  track.setHitPattern( * hit, k++ );
349  selHits->push_back( hit );
350  tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
351  }
352  }
353  }else{
354  for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.end()-1;
355  j != transHits.begin()-1; --j ) {
356  if ((**j).hit()!=0){
357  TrackingRecHit * hit = (**j).hit()->clone();
358  track.setHitPattern( * hit, k++ );
359  selHits->push_back( hit );
360  tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
361  }
362  }
363  }
364  // ----
365 
366  delete theTrack;
367  delete theTraj;
368  }
369 
370  //LogTrace("TrackingRegressionTest") << "========== TrackProducer Info ===================";
371  //LogDebug("TrackProducerWithSCAssociation") << "number of finalTracks: " << selTracks->size() << std::endl;
372  //for (reco::TrackCollection::const_iterator it = selTracks->begin(); it != selTracks->end(); it++) {
373  //LogDebug("TrackProducerWithSCAssociation") << "track's n valid and invalid hit, chi2, pt : "
374  // << it->found() << " , "
375  // << it->lost() <<" , "
376  // << it->normalizedChi2() << " , "
377  // << it->pt() << std::endl;
378  // }
379  //LogTrace("TrackingRegressionTest") << "=================================================";
380 
381 
382  rTracks_ = evt.put( selTracks );
383 
384 
385  evt.put( selTrackExtras );
386  evt.put( selHits );
387 
389  edm::OrphanHandle<std::vector<Trajectory> > rTrajs = evt.put(selTrajectories);
390 
391  // Now Create traj<->tracks association map
392  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
393  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
394  i != tjTkMap.end(); i++ ) {
395  edm::Ref<std::vector<Trajectory> > trajRef( rTrajs, (*i).first );
396  edm::Ref<reco::TrackCollection> tkRef( rTracks_, (*i).second );
397  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
398  edm::Ref<reco::TrackCollection>( rTracks_, (*i).second ) );
399  }
400  evt.put( trajTrackMap );
401  }
402 
403 
404 
405 
406 
407 
408 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
virtual void getFromEvt(edm::Event &, edm::Handle< TrackCandidateCollection > &, reco::BeamSpot &)
Get TrackCandidateCollection from the Event (needed by TrackProducer)
range recHits() const
edm::OrphanHandle< reco::TrackCollection > rTracks_
std::vector< reco::TransientTrack > getTransient(edm::Event &, const edm::EventSetup &)
void setSecondHitPattern(Trajectory *traj, reco::Track &track, const Propagator *prop, const MeasurementTracker *measTk)
const CurvilinearTrajectoryError & curvilinearError() const
TrajectorySeed const & seed() const
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
T y() const
Definition: PV3DBase.h:62
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setClusterRemovalInfo(const edm::InputTag &clusterRemovalInfo)
Sets the information on cluster removal, and turns it on.
ConstRecHitPointer recHit() const
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:13
PropagationDirection
std::pair< const_iterator, const_iterator > range
void setAlias(std::string alias)
set the aliases of produced collections
std::vector< ConstRecHitPointer > RecHitContainer
bool buildTrack(const TrajectoryFitter *, const Propagator *, AlgoProductCollection &, TransientTrackingRecHit::RecHitContainer &, TrajectoryStateOnSurface &, const TrajectorySeed &, float, const reco::BeamSpot &, SeedRef seedRef=SeedRef(), int qualityMask=0, signed char nLoops=0)
Construct Tracks to be put in the event.
ConstRecHitContainer recHits(bool splitting=false) const
Definition: Trajectory.cc:67
void putInEvt(edm::Event &evt, const Propagator *thePropagator, const MeasurementTracker *theMeasTk, std::auto_ptr< TrackingRecHitCollection > &selHits, std::auto_ptr< reco::TrackCollection > &selTracks, std::auto_ptr< reco::TrackExtraCollection > &selTrackExtras, std::auto_ptr< std::vector< Trajectory > > &selTrajectories, AlgoProductCollection &algoResults)
PropagationDirection const & direction() const
Definition: Trajectory.cc:195
PTrajectoryStateOnDet const & trajectoryStateOnDet() const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
T z() const
Definition: PV3DBase.h:63
void setConf(edm::ParameterSet conf)
Set parameter set.
int j
Definition: DBlmapReader.cc:9
unsigned int detId() const
TrajectoryStateOnSurface updatedState() const
edm::RefToBase< TrajectorySeed > seedRef(void) const
Definition: Trajectory.h:296
bool isValid() const
Definition: HandleBase.h:76
std::vector< AlgoProduct > AlgoProductCollection
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
virtual void produce(edm::Event &, const edm::EventSetup &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
void setSrc(edm::InputTag src, edm::InputTag bsSrc)
set label of source collection
RefProd< PROD > getRefBeforePut()
Definition: Event.h:97
int k[5][pyjets_maxn]
TrackProducerAlgorithm< reco::Track > theAlgo
bool failedToGet() const
Definition: HandleBase.h:80
Definition: DetId.h:20
void setHitPattern(const C &c)
set hit patterns from vector of hit references
Definition: TrackBase.h:246
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:9
void setExtra(const TrackExtraRef &ref)
set reference to &quot;extra&quot; object
Definition: Track.h:95
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:194
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
const GlobalTrajectoryParameters & globalParameters() const
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
T const * product() const
Definition: ESHandle.h:62
void add(const TrackingRecHitRef &r)
add a reference to a RecHit
T const * product() const
Definition: Handle.h:74
int cont
char state
Definition: procUtils.cc:75
TrackProducerWithSCAssociation(const edm::ParameterSet &iConfig)
edm::EventID id() const
Definition: EventBase.h:56
size_t size() const
Definition: ValueMap.h:152
edm::ESHandle< NavigationSchool > theSchool
void runWithCandidate(const TrackingGeometry *, const MagneticField *, const TrackCandidateCollection &, const TrajectoryFitter *, const Propagator *, const TransientTrackingRecHitBuilder *, const reco::BeamSpot &, AlgoProductCollection &)
Run the Final Fit taking TrackCandidates as input.
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:170
bool isValid() const
Definition: ESHandle.h:37
T x() const
Definition: PV3DBase.h:61
Trajectory::RecHitContainer RecHitContainer
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
edm::RefToBase< TrajectorySeed > seedRef() const
mathSSE::Vec4< T > v
virtual void getFromES(const edm::EventSetup &, edm::ESHandle< TrackerGeometry > &, edm::ESHandle< MagneticField > &, edm::ESHandle< TrajectoryFitter > &, edm::ESHandle< Propagator > &, edm::ESHandle< MeasurementTracker > &, edm::ESHandle< TransientTrackingRecHitBuilder > &)
Get needed services from the Event Setup.