test
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_));
47  consumes<MeasurementTrackerEvent>(edm::InputTag("MeasurementTrackerEvent")); //hardcoded because the original was and no time to fix (sigh)
48 
49 
50  //register your products
51  produces<reco::TrackCollection>().setBranchAlias( alias_ + "Tracks" );
52  produces<reco::TrackExtraCollection>().setBranchAlias( alias_ + "TrackExtras" );
53  produces<TrackingRecHitCollection>().setBranchAlias( alias_ + "RecHits" );
54  produces<std::vector<Trajectory> >() ;
55  produces<TrajTrackAssociationCollection>();
56  // produces< reco::TrackSuperClusterAssociationCollection > (trackSuperClusterAssociationCollection_ );
57  produces< reco::TrackCaloClusterPtrAssociation > (trackSuperClusterAssociationCollection_ );
58 
59 }
60 
61 
63 {
64  //edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
65 
66  //LogDebug("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
67  // std::cout << " TrackProducerWithSCAssociation Analyzing event number: " << theEvent.id() << "\n";
68 
69 
70  //
71  // create empty output collections
72  //
73  auto outputRHColl = std::make_unique<TrackingRecHitCollection>();
74  auto outputTColl = std::make_unique<reco::TrackCollection>();
75  auto outputTEColl = std::make_unique<reco::TrackExtraCollection>();
76  auto outputTrajectoryColl = std::make_unique<std::vector<Trajectory>>();
77  // Reco Track - Super Cluster Association
78  auto scTrkAssoc_p = std::make_unique<reco::TrackCaloClusterPtrAssociation>();
79 
80  //
81  //declare and get stuff to be retrieved from ES
82  //
86  edm::ESHandle<Propagator> thePropagator;
89  getFromES(setup,theG,theMF,theFitter,thePropagator,theMeasTk,theBuilder);
90 
92  setup.get<TrackerTopologyRcd>().get(httopo);
93  const TrackerTopology *ttopo = httopo.product();
94 
95 
96  //
97  //declare and get TrackColection to be retrieved from the event
102  theEvent.getByToken(assoc_token, trkCandidateSCAssocHandle);
103  if ( !trkCandidateSCAssocHandle.isValid() ) {
104  // std::cout << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
105  edm::LogError("TrackProducerWithSCAssociation") << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
107  }
108  reco::TrackCandidateCaloClusterPtrAssociation scTrkCandAssCollection = *(trkCandidateSCAssocHandle.product());
109  if ( scTrkCandAssCollection.size() ==0 ) validTrackCandidateSCAssociationInput_=false;
110 
111 
112  std::vector<int> tccLocations;
113  AlgoProductCollection algoResults;
114  reco::BeamSpot bs;
115 
116 
117  getFromEvt(theEvent,theTCCollection,bs);
118 
119  if (theTCCollection.failedToGet()){
120  edm::LogError("TrackProducerWithSCAssociation") <<"TrackProducerWithSCAssociation could not get the TrackCandidateCollection.";}
121  else{
122  //
123  //run the algorithm
124  //
125  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
126  // theAlgo.runWithCandidate(theG.product(), theMF.product(), *theTCCollection,
127  // theFitter.product(), thePropagator.product(), theBuilder.product(), algoResults);
128  // we have to copy this method from the algo in order to get the association track-seed
129  // this is ugly temporary code that should be replaced!!!!!
130  // start of copied code ======================================================
131 
132  // std::cout << "TrackProducerWithSCAssociation Number of TrackCandidates: " << theTCCollection->size() << "\n";
133  try{
134  int cont = 0;
135  int tcc=0;
136 
137  for (TrackCandidateCollection::const_iterator i=theTCCollection->begin(); i!=theTCCollection->end();i++)
138  {
139 
140  const TrackCandidate * theTC = &(*i);
142  const TrackCandidate::range& recHitVec=theTC->recHits();
143  const TrajectorySeed& seed = theTC->seed();
144 
145  //convert PTrajectoryStateOnDet to TrajectoryStateOnSurface
146 
147 
148  DetId detId(state.detId());
150  &(theG.product()->idToDet(detId)->surface()),
151  theMF.product());
152 
153  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Initial TSOS\n" << theTSOS << "\n";
154 
155  //convert the TrackingRecHit vector to a TransientTrackingRecHit vector
156  //meanwhile computes the number of degrees of freedom
158 
159  float ndof=0;
160 
162  i!=recHitVec.second; i++){
163  hits.push_back(theBuilder.product()->build(&(*i) ));
164  }
165 
166 
167  //build Track
168  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation going to buildTrack"<< "\n";
169  FitterCloner fc(theFitter.product(),theBuilder.product());
170  bool ok = theAlgo.buildTrack(fc.fitter.get(),thePropagator.product(),algoResults, hits, theTSOS, seed, ndof, bs, theTC->seedRef());
171  // LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation buildTrack result: " << ok << "\n";
172  if(ok) {
173  cont++;
174  tccLocations.push_back(tcc);
175  }
176  tcc++;
177  }
178  edm::LogInfo("TrackProducerWithSCAssociation") << "Number of Tracks found: " << cont << "\n";
179  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Number of Tracks found: " << cont << "\n";
180  // end of copied code ======================================================
181 
182  } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
183  //
184  //put everything in the event
185  // we copy putInEvt to get OrphanHandle filled...
186  putInEvt(theEvent,thePropagator.product(),theMeasTk.product(),
187  std::move(outputRHColl), std::move(outputTColl), std::move(outputTEColl), std::move(outputTrajectoryColl),
188  algoResults, theBuilder.product(), ttopo);
189 
190  // now construct associationmap and put it in the event
192  int itrack=0;
193  std::vector<edm::Ptr<reco::CaloCluster> > caloPtrVec;
194  for(AlgoProductCollection::iterator i=algoResults.begin(); i!=algoResults.end();i++){
195  edm::Ref<TrackCandidateCollection> trackCRef(theTCCollection,tccLocations[itrack]);
196  const edm::Ptr<reco::CaloCluster>& aClus = (*trkCandidateSCAssocHandle)[trackCRef];
197  caloPtrVec.push_back( aClus );
198  itrack++;
199  }
200 
201 
202  edm::ValueMap<reco::CaloClusterPtr>::Filler filler(*scTrkAssoc_p);
203  filler.insert(rTracks_, caloPtrVec.begin(), caloPtrVec.end());
204  filler.fill();
205  }
206 
207  theEvent.put(std::move(scTrkAssoc_p),trackSuperClusterAssociationCollection_ );
208 
209  }
210 
211 }
212 
213 std::vector<reco::TransientTrack> TrackProducerWithSCAssociation::getTransient(edm::Event& theEvent, const edm::EventSetup& setup)
214 {
215  edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
216  //
217  // create empty output collections
218  //
219  std::vector<reco::TransientTrack> ttks;
220 
221  //
222  //declare and get stuff to be retrieved from ES
223  //
227  edm::ESHandle<Propagator> thePropagator;
230  getFromES(setup,theG,theMF,theFitter,thePropagator,theMeasTk,theBuilder);
231 
232  //
233  //declare and get TrackColection to be retrieved from the event
234  //
235  AlgoProductCollection algoResults;
236  reco::BeamSpot bs;
237 
238  try{
240  getFromEvt(theEvent,theTCCollection,bs);
241 
242  //
243  //run the algorithm
244  //
245  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
246  theAlgo.runWithCandidate(theG.product(), theMF.product(), *theTCCollection,
247  theFitter.product(), thePropagator.product(), theBuilder.product(), bs, algoResults);
248 
249  } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
250 
251 
252  for (auto & prod : algoResults){
253  ttks.emplace_back(*prod.track,thePropagator.product()->magneticField());
254  }
255 
256  //LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation end" << "\n";
257 
258  return ttks;
259 }
260 
261 
263 
264 
266  const Propagator* thePropagator,
267  const MeasurementTracker* theMeasTk,
268  std::unique_ptr<TrackingRecHitCollection> selHits,
269  std::unique_ptr<reco::TrackCollection> selTracks,
270  std::unique_ptr<reco::TrackExtraCollection> selTrackExtras,
271  std::unique_ptr<std::vector<Trajectory>> selTrajectories,
272  AlgoProductCollection& algoResults, TransientTrackingRecHitBuilder const * hitBuilder,
273  const TrackerTopology *ttopo)
274 {
275 
278 
282  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
283  std::map<unsigned int, unsigned int> tjTkMap;
284 
285  for(auto & i : algoResults){
286  Trajectory * theTraj = i.trajectory;
288  selTrajectories->push_back(*theTraj);
289  iTjRef++;
290  }
291 
292  reco::Track * theTrack = i.track;
293  PropagationDirection seedDir = i.pDir;
294 
295  //LogDebug("TrackProducer") << "In KfTrackProducerBase::putInEvt - seedDir=" << seedDir;
296 
297  reco::Track t = * theTrack;
298  selTracks->push_back( t );
299  iTkRef++;
300 
301  // Store indices in local map (starts at 0)
302  if(trajectoryInEvent_) tjTkMap[iTjRef-1] = iTkRef-1;
303 
304  //sets the outermost and innermost TSOSs
305 
306  TrajectoryStateOnSurface outertsos;
307  TrajectoryStateOnSurface innertsos;
308  unsigned int innerId, outerId;
309 
310  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
311  // This is consistent with innermost and outermost labels only for tracks from LHC collision
312  if (theTraj->direction() == alongMomentum) {
313  outertsos = theTraj->lastMeasurement().updatedState();
314  innertsos = theTraj->firstMeasurement().updatedState();
315  outerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
316  innerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
317  } else {
318  outertsos = theTraj->firstMeasurement().updatedState();
319  innertsos = theTraj->lastMeasurement().updatedState();
320  outerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
321  innerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
322  }
323  // ---
324  //build the TrackExtra
325  GlobalPoint v = outertsos.globalParameters().position();
326  GlobalVector p = outertsos.globalParameters().momentum();
327  math::XYZVector outmom( p.x(), p.y(), p.z() );
328  math::XYZPoint outpos( v.x(), v.y(), v.z() );
329  v = innertsos.globalParameters().position();
330  p = innertsos.globalParameters().momentum();
331  math::XYZVector inmom( p.x(), p.y(), p.z() );
332  math::XYZPoint inpos( v.x(), v.y(), v.z() );
333 
334  reco::TrackExtraRef teref= reco::TrackExtraRef ( rTrackExtras, idx ++ );
335  reco::Track & track = selTracks->back();
336  track.setExtra( teref );
337 
338  //======= I want to set the second hitPattern here =============
339  if (theSchool.isValid())
340  {
343  setSecondHitPattern(theTraj,track,thePropagator,&*mte, ttopo);
344  }
345  //==============================================================
346 
347 
348  selTrackExtras->push_back( reco::TrackExtra (outpos, outmom, true, inpos, inmom, true,
349  outertsos.curvilinearError(), outerId,
350  innertsos.curvilinearError(), innerId,
351  seedDir,theTraj->seedRef()));
352 
353 
354  reco::TrackExtra & tx = selTrackExtras->back();
355  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
356  // This is consistent with innermost and outermost labels only for tracks from LHC collisions
357  Traj2TrackHits t2t(hitBuilder,false);
358  auto ih = selHits->size();
359  assert(ih==hidx);
360  t2t(*theTraj,*selHits,false);
361  auto const ie = selHits->size();
362  unsigned int nHitsAdded = 0;
363  for (;ih<ie; ++ih) {
364  auto const & hit = (*selHits)[ih];
365  track.appendHitPattern(hit, *ttopo);
366  ++nHitsAdded;
367  }
368  tx.setHits( rHits, hidx, nHitsAdded);
369  hidx +=nHitsAdded;
370  /*
371  if (theTraj->direction() == alongMomentum) {
372  for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.begin();
373  j != transHits.end(); j ++ ) {
374  if ((**j).hit()!=0){
375  TrackingRecHit * hit = (**j).hit()->clone();
376  track.appendHitPattern(*hit, *ttopo);
377  selHits->push_back( hit );
378  tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
379  }
380  }
381  }else{
382  for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.end()-1;
383  j != transHits.begin()-1; --j ) {
384  if ((**j).hit()!=0){
385  TrackingRecHit * hit = (**j).hit()->clone();
386  track.appendHitPattern(*hit, *ttopo);
387  selHits->push_back( hit );
388  tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
389  }
390  }
391  }
392  */
393 
394  // ----
395 
396  delete theTrack;
397  delete theTraj;
398  }
399 
400  //LogTrace("TrackingRegressionTest") << "========== TrackProducer Info ===================";
401  //LogDebug("TrackProducerWithSCAssociation") << "number of finalTracks: " << selTracks->size() << std::endl;
402  //for (reco::TrackCollection::const_iterator it = selTracks->begin(); it != selTracks->end(); it++) {
403  //LogDebug("TrackProducerWithSCAssociation") << "track's n valid and invalid hit, chi2, pt : "
404  // << it->found() << " , "
405  // << it->lost() <<" , "
406  // << it->normalizedChi2() << " , "
407  // << it->pt() << std::endl;
408  // }
409  //LogTrace("TrackingRegressionTest") << "=================================================";
410 
411 
412  rTracks_ = evt.put(std::move(selTracks));
413 
414 
415  evt.put(std::move(selTrackExtras));
416  evt.put(std::move(selHits));
417 
419  edm::OrphanHandle<std::vector<Trajectory> > rTrajs = evt.put(std::move(selTrajectories));
420 
421  // Now Create traj<->tracks association map
422  auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, rTracks_);
423  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
424  i != tjTkMap.end(); i++ ) {
425  edm::Ref<std::vector<Trajectory> > trajRef( rTrajs, (*i).first );
426  edm::Ref<reco::TrackCollection> tkRef( rTracks_, (*i).second );
427  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
428  edm::Ref<reco::TrackCollection>( rTracks_, (*i).second ) );
429  }
430  evt.put(std::move(trajTrackMap));
431  }
432 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::EDGetTokenT< MeasurementTrackerEvent > measurementTrkToken_
ConstRecHitPointer const & recHit() const
virtual void getFromEvt(edm::Event &, edm::Handle< TrackCandidateCollection > &, reco::BeamSpot &)
Get TrackCandidateCollection from the Event (needed by TrackProducer)
tuple cont
load Luminosity info ##
Definition: generateEDF.py:622
range recHits() const
edm::OrphanHandle< reco::TrackCollection > rTracks_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
std::vector< reco::TransientTrack > getTransient(edm::Event &, const edm::EventSetup &)
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
const CurvilinearTrajectoryError & curvilinearError() const
assert(m_qm.get())
TrajectorySeed const & seed() const
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
T y() const
Definition: PV3DBase.h:63
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setSecondHitPattern(Trajectory *traj, reco::Track &track, const Propagator *prop, const MeasurementTrackerEvent *measTk, const TrackerTopology *ttopo)
void setClusterRemovalInfo(const edm::InputTag &clusterRemovalInfo)
Sets the information on cluster removal, and turns it on.
std::vector< ConstRecHitPointer > RecHitContainer
edm::EDGetTokenT< reco::TrackCandidateCaloClusterPtrAssociation > assoc_token
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
PropagationDirection
std::pair< const_iterator, const_iterator > range
void setAlias(std::string alias)
set the aliases of produced collections
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.
PropagationDirection const & direction() const
Definition: Trajectory.cc:140
PTrajectoryStateOnDet const & trajectoryStateOnDet() const
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:174
T z() const
Definition: PV3DBase.h:64
def move
Definition: eostools.py:510
unsigned int detId() const
edm::RefToBase< TrajectorySeed > seedRef(void) const
Definition: Trajectory.h:321
bool isValid() const
Definition: HandleBase.h:75
RefProd< PROD > getRefBeforePut()
Definition: Event.h:134
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:79
Definition: DetId.h:18
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
void setExtra(const TrackExtraRef &ref)
set reference to &quot;extra&quot; object
Definition: Track.h:184
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:187
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
T const * product() const
Definition: Handle.h:81
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
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
bool appendHitPattern(const TrackingRecHit &hit, const TrackerTopology &ttopo)
append a single hit to the HitPattern
Definition: TrackBase.h:455
TrackProducerWithSCAssociation(const edm::ParameterSet &iConfig)
std::vector< AlgoProduct > AlgoProductCollection
edm::EventID id() const
Definition: EventBase.h:58
size_t size() const
Definition: ValueMap.h:157
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
bool isValid() const
Definition: ESHandle.h:47
T x() const
Definition: PV3DBase.h:62
void putInEvt(edm::Event &evt, const Propagator *thePropagator, const MeasurementTracker *theMeasTk, std::unique_ptr< TrackingRecHitCollection > selHits, std::unique_ptr< reco::TrackCollection > selTracks, std::unique_ptr< reco::TrackExtraCollection > selTrackExtras, std::unique_ptr< std::vector< Trajectory >> selTrajectories, AlgoProductCollection &algoResults, TransientTrackingRecHitBuilder const *hitBuilder, const TrackerTopology *ttopo)
edm::RefToBase< TrajectorySeed > seedRef() const
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
virtual void produce(edm::Event &, const edm::EventSetup &) override
Method where the procduction take place. To be implemented in concrete classes.
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.