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