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 <memory>
16 
22 #include <algorithm>
23 #include <map>
24 #include <memory>
25 #include <utility>
26 #include <vector>
27 
33 
34 namespace reco {
35  namespace modules {
36 
37  template <typename Selector>
39  public:
42  : hSrcTrackToken_(consumes<reco::TrackCollection>(cfg.template getParameter<edm::InputTag>("src"))),
43  hTrajToken_(mayConsume<std::vector<Trajectory> >(cfg.template getParameter<edm::InputTag>("src"))),
44  hTTAssToken_(mayConsume<TrajTrackAssociationCollection>(cfg.template getParameter<edm::InputTag>("src"))),
45  copyExtras_(cfg.template getUntrackedParameter<bool>("copyExtras", false)),
46  copyTrajectories_(cfg.template getUntrackedParameter<bool>("copyTrajectories", false)),
47  selector_(cfg, consumesCollector()) {
48  std::string alias(cfg.getParameter<std::string>("@module_label"));
49  produces<reco::TrackCollection>().setBranchAlias(alias + "Tracks");
50  if (copyExtras_) {
51  produces<reco::TrackExtraCollection>().setBranchAlias(alias + "TrackExtras");
52  produces<TrackingRecHitCollection>().setBranchAlias(alias + "RecHits");
53  if (copyTrajectories_) {
54  produces<std::vector<Trajectory> >().setBranchAlias(alias + "Trajectories");
55  produces<TrajTrackAssociationCollection>().setBranchAlias(alias + "TrajectoryTrackAssociations");
56  }
57  }
58  }
61 
62  private:
64  void produce(edm::Event& evt, const edm::EventSetup& es) override {
66  evt.getByToken(hSrcTrackToken_, hSrcTrack);
67 
68  selTracks_ = std::make_unique<reco::TrackCollection>();
69  if (copyExtras_) {
70  selTrackExtras_ = std::make_unique<reco::TrackExtraCollection>();
71  selHits_ = std::make_unique<TrackingRecHitCollection>();
72  }
73 
74  TrackRefProd rTracks = evt.template getRefBeforePut<TrackCollection>();
75 
77  TrackExtraRefProd rTrackExtras;
78  if (copyExtras_) {
79  rHits = evt.template getRefBeforePut<TrackingRecHitCollection>();
80  rTrackExtras = evt.template getRefBeforePut<TrackExtraCollection>();
81  }
82 
83  typedef reco::TrackRef::key_type TrackRefKey;
84  std::map<TrackRefKey, reco::TrackRef> goodTracks;
85  TrackRefKey current = 0;
86 
87  selector_.init(evt, es);
88  auto tkBegin = hSrcTrack->begin();
89  for (reco::TrackCollection::const_iterator it = tkBegin, ed = hSrcTrack->end(); it != ed; ++it, ++current) {
90  const reco::Track& trk = *it;
91  const reco::TrackRef tkref(hSrcTrack, std::distance(tkBegin, it));
92  if (!selector_(tkref))
93  continue;
94 
95  selTracks_->push_back(Track(trk)); // clone and store
96  if (!copyExtras_)
97  continue;
98 
99  // TrackExtras
100  selTrackExtras_->push_back(TrackExtra(trk.outerPosition(),
101  trk.outerMomentum(),
102  trk.outerOk(),
103  trk.innerPosition(),
104  trk.innerMomentum(),
105  trk.innerOk(),
106  trk.outerStateCovariance(),
107  trk.outerDetId(),
108  trk.innerStateCovariance(),
109  trk.innerDetId(),
110  trk.seedDirection()));
111  selTracks_->back().setExtra(TrackExtraRef(rTrackExtras, selTrackExtras_->size() - 1));
112  TrackExtra& tx = selTrackExtras_->back();
113  // TrackingRecHits
114  auto const firstHitIndex = selHits_->size();
115  for (auto const& hit : trk.recHits())
116  selHits_->push_back(hit->clone());
117  tx.setHits(rHits, firstHitIndex, selHits_->size() - firstHitIndex);
118  tx.setTrajParams(trk.extra()->trajParams(), trk.extra()->chi2sX5());
119  assert(tx.trajParams().size() == tx.recHitsSize());
120  if (copyTrajectories_) {
121  goodTracks[current] = reco::TrackRef(rTracks, selTracks_->size() - 1);
122  }
123  }
124  if (copyTrajectories_) {
127  evt.getByToken(hTTAssToken_, hTTAss);
128  evt.getByToken(hTrajToken_, hTraj);
129  edm::RefProd<std::vector<Trajectory> > TrajRefProd = evt.template getRefBeforePut<std::vector<Trajectory> >();
130  selTrajs_ = std::make_unique<std::vector<Trajectory> >();
131  selTTAss_ = std::make_unique<TrajTrackAssociationCollection>();
132  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
133  edm::Ref<std::vector<Trajectory> > trajRef(hTraj, i);
135  if (match != hTTAss->end()) {
136  const edm::Ref<reco::TrackCollection>& trkRef = match->val;
137  TrackRefKey oldKey = trkRef.key();
138  std::map<TrackRefKey, reco::TrackRef>::iterator getref = goodTracks.find(oldKey);
139  if (getref != goodTracks.end()) {
140  // do the clone
141  selTrajs_->push_back(Trajectory(*trajRef));
142  selTTAss_->insert(edm::Ref<std::vector<Trajectory> >(TrajRefProd, selTrajs_->size() - 1),
143  getref->second);
144  }
145  }
146  }
147  }
148 
149  evt.put(std::move(selTracks_));
150  if (copyExtras_) {
152  evt.put(std::move(selHits_));
153  if (copyTrajectories_) {
154  evt.put(std::move(selTrajs_));
155  evt.put(std::move(selTTAss_));
156  }
157  }
158  }
169  // some space
170  std::unique_ptr<reco::TrackCollection> selTracks_;
171  std::unique_ptr<reco::TrackExtraCollection> selTrackExtras_;
172  std::unique_ptr<TrackingRecHitCollection> selHits_;
173  std::unique_ptr<std::vector<Trajectory> > selTrajs_;
174  std::unique_ptr<TrajTrackAssociationCollection> selTTAss_;
175  };
176 
177  } // namespace modules
178 } // namespace reco
179 #endif
edm::EDGetTokenT< TrajTrackAssociationCollection > hTTAssToken_
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:164
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
TrajParams const & trajParams() const
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:68
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:62
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:65
void produce(edm::Event &evt, const edm::EventSetup &es) override
process one event
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
unsigned int recHitsSize() const
number of RecHits
TrackFullCloneSelectorBase(const edm::ParameterSet &cfg)
constructor
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:16
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
assert(be >=bs)
key_type key() const
Accessor for product key.
Definition: Ref.h:250
const_iterator find(const key_type &k) const
find element with specified reference key
std::unique_ptr< std::vector< Trajectory > > selTrajs_
const_iterator end() const
last iterator over the map (read only)
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:71
auto recHits() const
Access to reconstructed hits on the track.
Definition: Track.h:85
std::unique_ptr< TrajTrackAssociationCollection > selTTAss_
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:82
Functor that operates on <T>
Definition: Selector.h:22
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:53
const PropagationDirection & seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:148
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:79
edm::EDGetTokenT< std::vector< Trajectory > > hTrajToken_
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.
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
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:59
std::unique_ptr< reco::TrackExtraCollection > selTrackExtras_
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:56
def move(src, dest)
Definition: eostools.py:511
void setTrajParams(TrajParams tmps, Chi2sFive chi2s)
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:50
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:139