CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
67  if (copyExtras_) {
68  selTrackExtras_ = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
69  selHits_ = std::auto_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  for (reco::TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
87  const reco::Track & trk = * it;
88  if (!selector_(trk)) continue;
89 
90  selTracks_->push_back( Track( trk ) ); // clone and store
91  if (!copyExtras_) continue;
92 
93  // TrackExtras
94  selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
95  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
96  trk.outerStateCovariance(), trk.outerDetId(),
97  trk.innerStateCovariance(), trk.innerDetId(),
98  trk.seedDirection() ) );
99  selTracks_->back().setExtra( TrackExtraRef( rTrackExtras, selTrackExtras_->size() - 1) );
100  TrackExtra & tx = selTrackExtras_->back();
101  // TrackingRecHits
102  auto const firstHitIndex = selHits_->size();
103  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
104  selHits_->push_back( (*hit)->clone() );
105  }
106  tx.setHits( rHits, firstHitIndex, selHits_->size() - firstHitIndex );
107 
108  if (copyTrajectories_) {
109  goodTracks[current] = reco::TrackRef(rTracks, selTracks_->size() - 1);
110  }
111  }
112  if ( copyTrajectories_ ) {
115  evt.getByToken(hTTAssToken_, hTTAss);
116  evt.getByToken(hTrajToken_, hTraj);
117  edm::RefProd< std::vector<Trajectory> > TrajRefProd = evt.template getRefBeforePut< std::vector<Trajectory> >();
118  selTrajs_ = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
119  selTTAss_ = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
120  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
121  edm::Ref< std::vector<Trajectory> > trajRef(hTraj, i);
123  if (match != hTTAss->end()) {
124  const edm::Ref<reco::TrackCollection> &trkRef = match->val;
125  TrackRefKey oldKey = trkRef.key();
126  std::map<TrackRefKey, reco::TrackRef>::iterator getref = goodTracks.find(oldKey);
127  if (getref != goodTracks.end()) {
128  // do the clone
129  selTrajs_->push_back( Trajectory(*trajRef) );
130  selTTAss_->insert ( edm::Ref< std::vector<Trajectory> >(TrajRefProd, selTrajs_->size() - 1),
131  getref->second );
132  }
133  }
134  }
135  }
136 
137  evt.put(selTracks_);
138  if (copyExtras_) {
139  evt.put(selTrackExtras_);
140  evt.put(selHits_);
141  if ( copyTrajectories_ ) {
142  evt.put(selTrajs_);
143  evt.put(selTTAss_);
144  }
145  }
146  }
157  // some space
158  std::auto_ptr<reco::TrackCollection> selTracks_;
159  std::auto_ptr<reco::TrackExtraCollection> selTrackExtras_;
160  std::auto_ptr<TrackingRecHitCollection> selHits_;
161  std::auto_ptr< std::vector<Trajectory> > selTrajs_;
162  std::auto_ptr< TrajTrackAssociationCollection > selTTAss_;
163 };
164 
165 } }
166 #endif
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
tuple cfg
Definition: looper.py:259
edm::EDGetTokenT< std::vector< Trajectory > > hTrajToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
void produce(edm::Event &evt, const edm::EventSetup &es) override
process one event
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
TrackFullCloneSelectorBase(const edm::ParameterSet &cfg)
constructor
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:13
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:50
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
key_type key() const
Accessor for product key.
Definition: Ref.h:264
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
std::auto_ptr< reco::TrackExtraCollection > selTrackExtras_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:75
std::auto_ptr< TrajTrackAssociationCollection > selTTAss_
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
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:70
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:45
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:19
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:80
edm::EDGetTokenT< reco::TrackCollection > hSrcTrackToken_
source collection label
std::auto_ptr< std::vector< Trajectory > > selTrajs_
bool copyTrajectories_
copy also trajectories and trajectory-&gt;track associations
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
std::auto_ptr< reco::TrackCollection > selTracks_
edm::EDGetTokenT< TrajTrackAssociationCollection > hTTAssToken_
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
std::auto_ptr< TrackingRecHitCollection > selHits_
volatile std::atomic< bool > shutdown_flag false
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
def template
Definition: svgfig.py:520
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109