CMS 3D CMS Logo

TrackFullCloneSelectorBase.h
Go to the documentation of this file.
1 #ifndef RecoAlgos_TrackFullCloneSelectorBase_h
2 #define RecoAlgos_TrackFullCloneSelectorBase_h
3 
15 #include <utility>
16 #include <vector>
17 #include <memory>
18 #include <algorithm>
19 #include <map>
25 
31 
32 
33 namespace reco { namespace modules {
34 
35 template<typename Selector>
37 public:
40  hSrcTrackToken_( consumes<reco::TrackCollection>( cfg.template getParameter<edm::InputTag>( "src" ) ) ),
41  hTrajToken_( mayConsume< std::vector<Trajectory> >( cfg.template getParameter<edm::InputTag>( "src" ) ) ),
42  hTTAssToken_( mayConsume< TrajTrackAssociationCollection >( cfg.template getParameter<edm::InputTag>( "src" ) ) ),
43  copyExtras_(cfg.template getUntrackedParameter<bool>("copyExtras", false)),
44  copyTrajectories_(cfg.template getUntrackedParameter<bool>("copyTrajectories", false)),
45  selector_( cfg, consumesCollector() ) {
46  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
47  produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks" );
48  if (copyExtras_) {
49  produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras" );
50  produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits" );
51  if (copyTrajectories_) {
52  produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories" );
53  produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations" );
54  }
55  }
56  }
59 
60 private:
62  void produce( edm::Event& evt, const edm::EventSetup& es) override {
64  evt.getByToken( hSrcTrackToken_, hSrcTrack );
65 
66  selTracks_ = std::unique_ptr<reco::TrackCollection>(new reco::TrackCollection());
67  if (copyExtras_) {
68  selTrackExtras_ = std::unique_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
69  selHits_ = std::unique_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
70  }
71 
72  TrackRefProd rTracks = evt.template getRefBeforePut<TrackCollection>();
73 
75  TrackExtraRefProd rTrackExtras;
76  if (copyExtras_) {
77  rHits = evt.template getRefBeforePut<TrackingRecHitCollection>();
78  rTrackExtras = evt.template getRefBeforePut<TrackExtraCollection>();
79  }
80 
81  typedef reco::TrackRef::key_type TrackRefKey;
82  std::map<TrackRefKey, reco::TrackRef > goodTracks;
83  TrackRefKey current = 0;
84 
85  selector_.init(evt,es);
86  auto tkBegin = hSrcTrack->begin();
87  for (reco::TrackCollection::const_iterator it = tkBegin, ed = hSrcTrack->end(); it != ed; ++it, ++current) {
88  const reco::Track & trk = * it;
89  const reco::TrackRef tkref(hSrcTrack,std::distance(tkBegin,it));
90  if (!selector_(tkref)) continue;
91 
92  selTracks_->push_back( Track( trk ) ); // clone and store
93  if (!copyExtras_) continue;
94 
95  // TrackExtras
96  selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
97  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
98  trk.outerStateCovariance(), trk.outerDetId(),
99  trk.innerStateCovariance(), trk.innerDetId(),
100  trk.seedDirection() ) );
101  selTracks_->back().setExtra( TrackExtraRef( rTrackExtras, selTrackExtras_->size() - 1) );
102  TrackExtra & tx = selTrackExtras_->back();
103  // TrackingRecHits
104  auto const firstHitIndex = selHits_->size();
105  for(auto const& hit : trk.recHits()) selHits_->push_back( hit->clone() );
106  tx.setHits( rHits, firstHitIndex, selHits_->size() - firstHitIndex );
107  tx.setTrajParams(trk.extra()->trajParams(),trk.extra()->chi2sX5());
108  assert(tx.trajParams().size()==tx.recHitsSize());
109  if (copyTrajectories_) {
110  goodTracks[current] = reco::TrackRef(rTracks, selTracks_->size() - 1);
111  }
112  }
113  if ( copyTrajectories_ ) {
116  evt.getByToken(hTTAssToken_, hTTAss);
117  evt.getByToken(hTrajToken_, hTraj);
118  edm::RefProd< std::vector<Trajectory> > TrajRefProd = evt.template getRefBeforePut< std::vector<Trajectory> >();
119  selTrajs_ = std::unique_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
120  selTTAss_ = std::unique_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
121  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
122  edm::Ref< std::vector<Trajectory> > trajRef(hTraj, i);
124  if (match != hTTAss->end()) {
125  const edm::Ref<reco::TrackCollection> &trkRef = match->val;
126  TrackRefKey oldKey = trkRef.key();
127  std::map<TrackRefKey, reco::TrackRef>::iterator getref = goodTracks.find(oldKey);
128  if (getref != goodTracks.end()) {
129  // do the clone
130  selTrajs_->push_back( Trajectory(*trajRef) );
131  selTTAss_->insert ( edm::Ref< std::vector<Trajectory> >(TrajRefProd, selTrajs_->size() - 1),
132  getref->second );
133  }
134  }
135  }
136  }
137 
138  evt.put(std::move(selTracks_));
139  if (copyExtras_) {
141  evt.put(std::move(selHits_));
142  if ( copyTrajectories_ ) {
143  evt.put(std::move(selTrajs_));
144  evt.put(std::move(selTTAss_));
145  }
146  }
147  }
158  // some space
159  std::unique_ptr<reco::TrackCollection> selTracks_;
160  std::unique_ptr<reco::TrackExtraCollection> selTrackExtras_;
161  std::unique_ptr<TrackingRecHitCollection> selHits_;
162  std::unique_ptr< std::vector<Trajectory> > selTrajs_;
163  std::unique_ptr< TrajTrackAssociationCollection > selTTAss_;
164 };
165 
166 } }
167 #endif
T getParameter(std::string const &) const
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:167
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:194
edm::EDGetTokenT< std::vector< Trajectory > > hTrajToken_
const_iterator end() const
last iterator over the map (read only)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void produce(edm::Event &evt, const edm::EventSetup &es) override
process one event
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
const_iterator find(const key_type &k) const
find element with specified reference key
TrackFullCloneSelectorBase(const edm::ParameterSet &cfg)
constructor
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:52
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
key_type key() const
Accessor for product key.
Definition: Ref.h:263
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:67
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:57
unsigned int recHitsSize() const
number of RecHits
TrajParams const & trajParams() const
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:77
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:96
auto recHits() const
Access to reconstructed hits on the track.
Definition: Track.h:106
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
std::unique_ptr< TrajTrackAssociationCollection > selTTAss_
std::unique_ptr< std::vector< Trajectory > > selTrajs_
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:72
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:47
const PropagationDirection & seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:209
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:21
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:82
edm::EDGetTokenT< reco::TrackCollection > hSrcTrackToken_
source collection label
std::unique_ptr< reco::TrackCollection > selTracks_
fixed size matrix
bool copyTrajectories_
copy also trajectories and trajectory->track associations
HLT enums.
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:62
edm::EDGetTokenT< TrajTrackAssociationCollection > hTTAssToken_
std::unique_ptr< TrackingRecHitCollection > selHits_
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
std::unique_ptr< reco::TrackExtraCollection > selTrackExtras_
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:101
def move(src, dest)
Definition: eostools.py:511
void setTrajParams(TrajParams tmps, Chi2sFive chi2s)