CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackMultiSelector.cc
Go to the documentation of this file.
1 
11 #include <utility>
12 #include <vector>
13 #include <memory>
14 #include <algorithm>
15 #include <map>
20 
28 
30 
31 
33  private:
34  struct Block {
35  std::pair<double,double> pt;
36  std::pair<uint32_t,uint32_t> vlayers, lhits;
37  std::pair<double,double> chi2n;
38  double d0, dz,d0Rel,dzRel;
39 
40  explicit Block(const edm::ParameterSet & cfg) ;
41  private:
42  template<typename T> std::pair<T,T> p2p(const edm::ParameterSet & cfg, const std::string name);
43  };
44  public:
46  explicit TrackMultiSelector( const edm::ParameterSet & cfg ) ;
48  virtual ~TrackMultiSelector() ;
49 
50  private:
53  void produce( edm::Event& evt, const edm::EventSetup& es ) override;
55  short select ( const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector<Point> &points);
56  void selectVertices ( const reco::VertexCollection &vtxs, std::vector<Point> &points);
57  inline bool testVtx ( const reco::Track &tk, const reco::BeamSpot &beamSpot,
58  const std::vector<Point> &points, const Block &cut);
66 
67  double beamspotDZsigmas_, beamspotD0_;
75  std::vector<Block> blocks_;
77  int32_t vtxNumber_;
78  size_t vtxTracks_;
79  double vtxChi2Prob_;
81  std::vector<std::string> labels_;
83  std::auto_ptr<reco::TrackCollection> *selTracks_;
84  std::auto_ptr<reco::TrackExtraCollection> *selTrackExtras_;
85  std::auto_ptr< TrackingRecHitCollection> *selHits_;
86  std::auto_ptr< std::vector<Trajectory> > *selTrajs_;
87  std::auto_ptr< TrajTrackAssociationCollection > *selTTAss_;
88  std::vector<reco::TrackRefProd> rTracks_;
89  std::vector<reco::TrackExtraRefProd> rTrackExtras_;
90  std::vector<TrackingRecHitRefProd> rHits_;
91  std::vector< edm::RefProd< std::vector<Trajectory> > > rTrajectories_;
92  std::vector< std::pair<short, reco::TrackRef> > whereItWent_;
93 
94  };
95 
96 
97 
98 // template method to be implemented here?
99 template<typename T> std::pair<T,T> TrackMultiSelector::Block::p2p(const edm::ParameterSet & cfg, const std::string name) {
100  typedef typename std::vector<T> Ts;
101  Ts ret = cfg.getParameter<Ts>(name);
102  if (ret.size() != 2) throw cms::Exception("Invalid configuration") << "Parameter '" << name << "' must be given as {min,max}";
103  return std::pair<T,T>(ret[0],ret[1]);
104 }
105 
107 
108 #include <Math/DistFunc.h>
109 #include "TMath.h"
110 
111 
113  pt(p2p<double>(cfg,"pt")),
114  vlayers(p2p<uint32_t>(cfg,"validLayers")),
115  lhits(p2p<uint32_t>(cfg,"lostHits")),
116  chi2n(p2p<double>(cfg,"chi2n")),
117  d0(cfg.getParameter<double>("d0")),
118  dz(cfg.getParameter<double>("dz")),
119  d0Rel(cfg.getParameter<double>("d0Rel")),
120  dzRel(cfg.getParameter<double>("dzRel"))
121 {
122 }
123 
125  src_( cfg.getParameter<edm::InputTag>( "src" ) ),
126  copyExtras_(cfg.getUntrackedParameter<bool>("copyExtras", false)),
127  copyTrajectories_(cfg.getUntrackedParameter<bool>("copyTrajectories", false)),
128  splitOutputs_( cfg.getUntrackedParameter<bool>("splitOutputs", false) ),
129  vtxNumber_( cfg.getParameter<int32_t>("vtxNumber") ),
130  vtxTracks_( cfg.getParameter<uint32_t>("vtxTracks") ),
131  vtxChi2Prob_( cfg.getParameter<double>("vtxChi2Prob") )
132 {
133  edm::ParameterSet beamSpotPSet = cfg.getParameter<edm::ParameterSet>("beamspot");
134  beamspot_ = consumes<reco::BeamSpot>(beamSpotPSet.getParameter<edm::InputTag>("src"));
135  beamspotDZsigmas_ = beamSpotPSet.getParameter<double>("dzSigmas");
136  beamspotD0_ = beamSpotPSet.getParameter<double>("d0");
137  vertices_= consumes<reco::VertexCollection>( cfg.getParameter<edm::InputTag>( "vertices" ) );
138  tokenTracks= consumes<reco::TrackCollection>(src_);
139  if (copyTrajectories_) {
140  tokenTraj= consumes<std::vector<Trajectory> >(src_);
141  tokenTrajTrack= consumes<TrajTrackAssociationCollection>(src_);
142  }
143 
144  typedef std::vector<edm::ParameterSet> VPSet;
145  VPSet psets = cfg.getParameter<VPSet>("cutSets");
146  blocks_.reserve(psets.size());
147  for (VPSet::const_iterator it = psets.begin(), ed = psets.end(); it != ed; ++it) {
148  blocks_.push_back(TrackMultiSelector::Block(*it));
149  }
150 
151  if (splitOutputs_) {
152  char buff[15];
153  for (size_t i = 0; i < blocks_.size(); ++i) {
154  sprintf(buff,"set%d", static_cast<int>(i+1));
155  labels_.push_back(std::string(buff));
156  }
157  } else {
158  labels_.push_back(std::string(""));
159  }
160 
161  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
162  for (std::vector<std::string>::const_iterator li = labels_.begin(), le = labels_.end(); li != le; ++li) {
163  const char *l= li->c_str();
164  produces<reco::TrackCollection>(l).setBranchAlias( alias + "Tracks" + l);
165  if (copyExtras_) {
166  produces<reco::TrackExtraCollection>(l).setBranchAlias( alias + "TrackExtras" + l);
167  produces<TrackingRecHitCollection>(l).setBranchAlias( alias + "RecHits" + l);
168  if (copyTrajectories_) {
169  produces< std::vector<Trajectory> >(l).setBranchAlias( alias + "Trajectories" + l);
170  produces< TrajTrackAssociationCollection >(l).setBranchAlias( alias + "TrajectoryTrackAssociations" + l);
171  }
172  }
173  }
174 
175  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
176  selTracks_ = new std::auto_ptr<reco::TrackCollection>[nblocks];
177  selTrackExtras_ = new std::auto_ptr<reco::TrackExtraCollection>[nblocks];
178  selHits_ = new std::auto_ptr<TrackingRecHitCollection>[nblocks];
179  selTrajs_ = new std::auto_ptr< std::vector<Trajectory> >[nblocks];
180  selTTAss_ = new std::auto_ptr< TrajTrackAssociationCollection >[nblocks];
181  rTracks_ = std::vector<reco::TrackRefProd>(nblocks);
182  rHits_ = std::vector<TrackingRecHitRefProd>(nblocks);
183  rTrackExtras_ = std::vector<reco::TrackExtraRefProd>(nblocks);
184  rTrajectories_ = std::vector< edm::RefProd< std::vector<Trajectory> > >(nblocks);
185  for (size_t i = 0; i < nblocks; i++) {
186  selTracks_[i] = std::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
187  selTrackExtras_[i] = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
188  selHits_[i] = std::auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
189  selTrajs_[i] = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
190  selTTAss_[i] = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
191  }
192 
193 }
194 
196  delete [] selTracks_;
197  delete [] selTrackExtras_;
198  delete [] selHits_;
199  delete [] selTrajs_;
200  delete [] selTTAss_;
201 }
202 
204 {
205  using namespace std;
206  using namespace edm;
207  using namespace reco;
208 
209  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
210 
211  Handle<TrackCollection> hSrcTrack;
214 
216  evt.getByToken(vertices_, hVtx);
217  std::vector<Point> points;
218  if (vtxNumber_ != 0) selectVertices(*hVtx, points);
219 
221  evt.getByToken(beamspot_, hBsp);
222 
223  evt.getByToken( tokenTracks, hSrcTrack );
224 
225  for (size_t i = 0; i < nblocks; i++) {
226  selTracks_[i] = auto_ptr<TrackCollection>(new TrackCollection());
228  if (copyExtras_) {
229  selTrackExtras_[i] = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
230  selHits_[i] = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
233  }
234  }
235 
236  if (copyTrajectories_) whereItWent_.resize(hSrcTrack->size());
237  size_t current = 0;
238  for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
239  const Track & trk = * it;
240  short where = select(trk, *hBsp, points);
241  if (where == -1) {
242  if (copyTrajectories_) whereItWent_[current] = std::pair<short, reco::TrackRef>(-1, reco::TrackRef());
243  continue;
244  }
245  if (!splitOutputs_) where = 0;
246  selTracks_[where]->push_back( Track( trk ) ); // clone and store
247  if (!copyExtras_) continue;
248 
249  // TrackExtras
250  selTrackExtras_[where]->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
251  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
252  trk.outerStateCovariance(), trk.outerDetId(),
253  trk.innerStateCovariance(), trk.innerDetId(),
254  trk.seedDirection(), trk.seedRef() ) );
255  selTracks_[where]->back().setExtra( TrackExtraRef( rTrackExtras_[where], selTrackExtras_[where]->size() - 1) );
256  TrackExtra & tx = selTrackExtras_[where]->back();
257  tx.setResiduals(trk.residuals());
258  // TrackingRecHits
259  auto& selHitsWhere = selHits_[where];
260  auto const firstHitIndex = selHitsWhere->size();
261  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
262  selHitsWhere->push_back( (*hit)->clone() );
263  }
264  tx.setHits( rHits_[where], firstHitIndex, selHitsWhere->size() - firstHitIndex);
265 
266  if (copyTrajectories_) {
267  whereItWent_[current] = std::pair<short, reco::TrackRef>(where, TrackRef(rTracks_[where], selTracks_[where]->size() - 1));
268  }
269  }
270  if ( copyTrajectories_ ) {
273  evt.getByToken(tokenTrajTrack, hTTAss);
274  evt.getByToken(tokenTraj, hTraj);
275  for (size_t i = 0; i < nblocks; i++) {
276  rTrajectories_[i] = evt.getRefBeforePut< vector<Trajectory> >(labels_[i]);
277  selTrajs_[i] = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
278  selTTAss_[i] = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
279  }
280  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
281  Ref< vector<Trajectory> > trajRef(hTraj, i);
283  if (match != hTTAss->end()) {
284  const Ref<TrackCollection> &trkRef = match->val;
285  short oldKey = static_cast<short>(trkRef.key());
286  if (whereItWent_[oldKey].first != -1) {
287  int where = whereItWent_[oldKey].first;
288  selTrajs_[where]->push_back( Trajectory(*trajRef) );
289  selTTAss_[where]->insert ( Ref< vector<Trajectory> >(rTrajectories_[where], selTrajs_[where]->size() - 1), whereItWent_[oldKey].second );
290  }
291  }
292  }
293  }
294 
295 
296  static const std::string emptyString;
297  for (size_t i = 0; i < nblocks; i++) {
298  const std::string & lbl = ( splitOutputs_ ? labels_[i] : emptyString);
299  evt.put(selTracks_[i], lbl);
300  if (copyExtras_ ) {
301  evt.put(selTrackExtras_[i], lbl);
302  evt.put(selHits_[i], lbl);
303  if ( copyTrajectories_ ) {
304  evt.put(selTrajs_[i], lbl);
305  evt.put(selTTAss_[i], lbl);
306  }
307  }
308  }
309 }
310 
312  const std::vector<Point> &points,
314  using std::abs;
315  double d0Err =abs(tk.d0Error()), dzErr = abs(tk.dzError()); // not fully sure they're > 0!
316  if (points.empty()) {
317  Point spot = beamSpot.position();
318  double dz = abs(tk.dz(spot)), d0 = abs(tk.dxy(spot));
319  return ( dz < beamspotDZsigmas_*beamSpot.sigmaZ() ) && ( d0 < beamspotD0_ );
320  }
321  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
322  double dz = abs(tk.dz(*point)), d0 = abs(tk.dxy(*point));
323  if ((dz < cut.dz) && (d0 < cut.d0)
324  && fabs(dz/std::max(dzErr,1e-9)) < cut.dzRel && (d0/std::max(d0Err,1e-8) < cut.d0Rel )) return true;
325  }
326  return false;
327 }
328 
329 short TrackMultiSelector::select(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector<Point> &points) {
330  uint32_t vlayers = tk.hitPattern().trackerLayersWithMeasurement(), lhits = tk.numberOfLostHits();
331  double pt = tk.pt(), chi2n = tk.normalizedChi2();
332  int which = 0;
333  for (std::vector<TrackMultiSelector::Block>::const_iterator itb = blocks_.begin(), edb = blocks_.end(); itb != edb; ++itb, ++which) {
334  if ( ( itb->vlayers.first <= vlayers ) && ( vlayers <= itb->vlayers.second ) &&
335  ( itb->chi2n.first <= chi2n ) && ( chi2n <= itb->chi2n.second ) &&
336  ( itb->pt.first <= pt ) && ( pt <= itb->pt.second ) &&
337  ( itb->lhits.first <= lhits ) && ( lhits <= itb->lhits.second ) &&
338  testVtx(tk, beamSpot, points, *itb) )
339  {
340  return which;
341  }
342  }
343  return -1;
344 }
345 void TrackMultiSelector::selectVertices(const reco::VertexCollection &vtxs, std::vector<Point> &points) {
346  using namespace reco;
347 
348  int32_t toTake = vtxNumber_;
349  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
350  if ((it->tracksSize() >= vtxTracks_) &&
351  ( (it->chi2() == 0.0) || (TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()) ) >= vtxChi2Prob_) ) ) {
352  points.push_back(it->position());
353  toTake--; if (toTake == 0) break;
354  }
355  }
356 }
357 
358 
361 
363 
std::auto_ptr< reco::TrackCollection > * selTracks_
some storage
T getParameter(std::string const &) const
#define dso_hidden
int i
Definition: DBlmapReader.cc:9
std::pair< double, double > pt
double d0Error() const
error on d0
Definition: TrackBase.h:755
edm::EDGetTokenT< std::vector< Trajectory > > tokenTraj
int32_t vtxNumber_
vertex cuts
tuple cfg
Definition: looper.py:259
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
std::vector< reco::TrackExtraRefProd > rTrackExtras_
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:514
edm::InputTag src_
source collection label
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
bool testVtx(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector< Point > &points, const Block &cut)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:13
std::auto_ptr< TrackingRecHitCollection > * selHits_
virtual ~TrackMultiSelector()
destructor
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:50
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
Definition: TrackBase.h:779
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
std::pair< uint32_t, uint32_t > vlayers
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
short select(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector< Point > &points)
return class, or -1 if rejected
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:477
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
U second(std::pair< T, U > const &p)
std::pair< T, T > p2p(const edm::ParameterSet &cfg, const std::string name)
edm::EDGetTokenT< TrajTrackAssociationCollection > tokenTrajTrack
std::vector< edm::RefProd< std::vector< Trajectory > > > rTrajectories_
std::vector< reco::TrackRefProd > rTracks_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
std::auto_ptr< TrajTrackAssociationCollection > * selTTAss_
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:75
double pt() const
track transverse momentum
Definition: TrackBase.h:574
edm::EDGetTokenT< reco::VertexCollection > vertices_
std::vector< std::pair< short, reco::TrackRef > > whereItWent_
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::pair< double, double > chi2n
bool copyTrajectories_
copy also trajectories and trajectory-&gt;track associations
#define end
Definition: vmac.h:37
Block(const edm::ParameterSet &cfg)
std::vector< std::string > labels_
output labels
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
std::vector< Block > blocks_
filter psets
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
edm::EDGetTokenT< reco::TrackCollection > tokenTracks
RefProd< PROD > getRefBeforePut()
Definition: Event.h:135
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:562
double dzError() const
error on dz
Definition: TrackBase.h:767
std::vector< TrackingRecHitRefProd > rHits_
void selectVertices(const reco::VertexCollection &vtxs, std::vector< Point > &points)
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:213
bool splitOutputs_
split selections in more sets
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
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:411
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:19
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:80
TrackMultiSelector(const edm::ParameterSet &cfg)
constructor
std::auto_ptr< std::vector< Trajectory > > * selTrajs_
static std::string const emptyString("")
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
const TrackResiduals & residuals() const
Definition: Track.h:220
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
const Point & position() const
position
Definition: BeamSpot.h:62
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
volatile std::atomic< bool > shutdown_flag false
std::auto_ptr< reco::TrackExtraCollection > * selTrackExtras_
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:544
void produce(edm::Event &evt, const edm::EventSetup &es) override
process one event
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
tuple size
Write out results.
edm::EDGetTokenT< reco::BeamSpot > beamspot_
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
virtual void produce(Event &, EventSetup const &)=0
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:179