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.
3 
4 #include <Math/DistFunc.h>
5 #include "TMath.h"
6 
8 //using reco::modules::TrackMultiSelector::Block;
9 
10 TrackMultiSelector::Block::Block(const edm::ParameterSet & cfg) :
11  pt(p2p<double>(cfg,"pt")),
12  vlayers(p2p<uint32_t>(cfg,"validLayers")),
13  lhits(p2p<uint32_t>(cfg,"lostHits")),
14  chi2n(p2p<double>(cfg,"chi2n")),
15  d0(cfg.getParameter<double>("d0")),
16  dz(cfg.getParameter<double>("dz")),
17  d0Rel(cfg.getParameter<double>("d0Rel")),
18  dzRel(cfg.getParameter<double>("dzRel"))
19 {
20 }
21 
23  src_( cfg.getParameter<edm::InputTag>( "src" ) ),
24  vertices_( cfg.getParameter<edm::InputTag>( "vertices" ) ),
25  copyExtras_(cfg.getUntrackedParameter<bool>("copyExtras", false)),
26  copyTrajectories_(cfg.getUntrackedParameter<bool>("copyTrajectories", false)),
27  splitOutputs_( cfg.getUntrackedParameter<bool>("splitOutputs", false) ),
28  vtxNumber_( cfg.getParameter<int32_t>("vtxNumber") ),
29  vtxTracks_( cfg.getParameter<uint32_t>("vtxTracks") ),
30  vtxChi2Prob_( cfg.getParameter<double>("vtxChi2Prob") )
31 {
32  edm::ParameterSet beamSpotPSet = cfg.getParameter<edm::ParameterSet>("beamspot");
33  beamspot_ = beamSpotPSet.getParameter<edm::InputTag>("src");
34  beamspotDZsigmas_ = beamSpotPSet.getParameter<double>("dzSigmas");
35  beamspotD0_ = beamSpotPSet.getParameter<double>("d0");
36 
37  typedef std::vector<edm::ParameterSet> VPSet;
38  VPSet psets = cfg.getParameter<VPSet>("cutSets");
39  blocks_.reserve(psets.size());
40  for (VPSet::const_iterator it = psets.begin(), ed = psets.end(); it != ed; ++it) {
41  blocks_.push_back(TrackMultiSelector::Block(*it));
42  }
43 
44  if (splitOutputs_) {
45  char buff[15];
46  for (size_t i = 0; i < blocks_.size(); ++i) {
47  sprintf(buff,"set%d", static_cast<int>(i+1));
48  labels_.push_back(std::string(buff));
49  }
50  } else {
51  labels_.push_back(std::string(""));
52  }
53 
54  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
55  for (std::vector<std::string>::const_iterator li = labels_.begin(), le = labels_.end(); li != le; ++li) {
56  const char *l= li->c_str();
57  produces<reco::TrackCollection>(l).setBranchAlias( alias + "Tracks" + l);
58  if (copyExtras_) {
59  produces<reco::TrackExtraCollection>(l).setBranchAlias( alias + "TrackExtras" + l);
60  produces<TrackingRecHitCollection>(l).setBranchAlias( alias + "RecHits" + l);
61  if (copyTrajectories_) {
62  produces< std::vector<Trajectory> >(l).setBranchAlias( alias + "Trajectories" + l);
63  produces< TrajTrackAssociationCollection >(l).setBranchAlias( alias + "TrajectoryTrackAssociations" + l);
64  }
65  }
66  }
67 
68  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
69  selTracks_ = new std::auto_ptr<reco::TrackCollection>[nblocks];
70  selTrackExtras_ = new std::auto_ptr<reco::TrackExtraCollection>[nblocks];
71  selHits_ = new std::auto_ptr<TrackingRecHitCollection>[nblocks];
72  selTrajs_ = new std::auto_ptr< std::vector<Trajectory> >[nblocks];
73  selTTAss_ = new std::auto_ptr< TrajTrackAssociationCollection >[nblocks];
74  rTracks_ = std::vector<reco::TrackRefProd>(nblocks);
75  rHits_ = std::vector<TrackingRecHitRefProd>(nblocks);
76  rTrackExtras_ = std::vector<reco::TrackExtraRefProd>(nblocks);
77  rTrajectories_ = std::vector< edm::RefProd< std::vector<Trajectory> > >(nblocks);
78  for (size_t i = 0; i < nblocks; i++) {
79  selTracks_[i] = std::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
80  selTrackExtras_[i] = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
81  selHits_[i] = std::auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
82  selTrajs_[i] = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
83  selTTAss_[i] = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
84  }
85 
86 }
87 
89  delete [] selTracks_;
90  delete [] selTrackExtras_;
91  delete [] selHits_;
92  delete [] selTrajs_;
93  delete [] selTTAss_;
94 }
95 
97 {
98  using namespace std;
99  using namespace edm;
100  using namespace reco;
101 
102  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
103 
104  Handle<TrackCollection> hSrcTrack;
107 
109  evt.getByLabel(vertices_, hVtx);
110  std::vector<Point> points;
111  if (vtxNumber_ != 0) selectVertices(*hVtx, points);
112 
114  evt.getByLabel(beamspot_, hBsp);
115 
116  evt.getByLabel( src_, hSrcTrack );
117 
118  for (size_t i = 0; i < nblocks; i++) {
119  selTracks_[i] = auto_ptr<TrackCollection>(new TrackCollection());
121  if (copyExtras_) {
122  selTrackExtras_[i] = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
123  selHits_[i] = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
126  }
127  }
128 
129  if (copyTrajectories_) whereItWent_.resize(hSrcTrack->size());
130  size_t current = 0;
131  for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
132  const Track & trk = * it;
133  short where = select(trk, *hBsp, points);
134  if (where == -1) {
135  if (copyTrajectories_) whereItWent_[current] = std::pair<short, reco::TrackRef>(-1, reco::TrackRef());
136  continue;
137  }
138  if (!splitOutputs_) where = 0;
139  selTracks_[where]->push_back( Track( trk ) ); // clone and store
140  if (!copyExtras_) continue;
141 
142  // TrackExtras
143  selTrackExtras_[where]->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
144  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
145  trk.outerStateCovariance(), trk.outerDetId(),
146  trk.innerStateCovariance(), trk.innerDetId(),
147  trk.seedDirection(), trk.seedRef() ) );
148  selTracks_[where]->back().setExtra( TrackExtraRef( rTrackExtras_[where], selTrackExtras_[where]->size() - 1) );
149  TrackExtra & tx = selTrackExtras_[where]->back();
150  tx.setResiduals(trk.residuals());
151  // TrackingRecHits
152  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
153  selHits_[where]->push_back( (*hit)->clone() );
154  tx.add( TrackingRecHitRef( rHits_[where], selHits_[where]->size() - 1) );
155  }
156  if (copyTrajectories_) {
157  whereItWent_[current] = std::pair<short, reco::TrackRef>(where, TrackRef(rTracks_[where], selTracks_[where]->size() - 1));
158  }
159  }
160  if ( copyTrajectories_ ) {
163  evt.getByLabel(src_, hTTAss);
164  evt.getByLabel(src_, hTraj);
165  for (size_t i = 0; i < nblocks; i++) {
166  rTrajectories_[i] = evt.getRefBeforePut< vector<Trajectory> >(labels_[i]);
167  selTrajs_[i] = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
168  selTTAss_[i] = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
169  }
170  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
171  Ref< vector<Trajectory> > trajRef(hTraj, i);
173  if (match != hTTAss->end()) {
174  const Ref<TrackCollection> &trkRef = match->val;
175  short oldKey = static_cast<short>(trkRef.key());
176  if (whereItWent_[oldKey].first != -1) {
177  int where = whereItWent_[oldKey].first;
178  selTrajs_[where]->push_back( Trajectory(*trajRef) );
179  selTTAss_[where]->insert ( Ref< vector<Trajectory> >(rTrajectories_[where], selTrajs_[where]->size() - 1), whereItWent_[oldKey].second );
180  }
181  }
182  }
183  }
184 
185 
186  static const std::string emptyString;
187  for (size_t i = 0; i < nblocks; i++) {
188  const std::string & lbl = ( splitOutputs_ ? labels_[i] : emptyString);
189  evt.put(selTracks_[i], lbl);
190  if (copyExtras_ ) {
191  evt.put(selTrackExtras_[i], lbl);
192  evt.put(selHits_[i], lbl);
193  if ( copyTrajectories_ ) {
194  evt.put(selTrajs_[i], lbl);
195  evt.put(selTTAss_[i], lbl);
196  }
197  }
198  }
199 }
200 
202  const std::vector<Point> &points,
204  using std::abs;
205  double d0Err =abs(tk.d0Error()), dzErr = abs(tk.dzError()); // not fully sure they're > 0!
206  if (points.empty()) {
207  Point spot = beamSpot.position();
208  double dz = abs(tk.dz(spot)), d0 = abs(tk.dxy(spot));
209  return ( dz < beamspotDZsigmas_*beamSpot.sigmaZ() ) && ( d0 < beamspotD0_ );
210  }
211  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
212  double dz = abs(tk.dz(*point)), d0 = abs(tk.dxy(*point));
213  if ((dz < cut.dz) && (d0 < cut.d0)
214  && fabs(dz/std::max(dzErr,1e-9)) < cut.dzRel && (d0/std::max(d0Err,1e-8) < cut.d0Rel )) return true;
215  }
216  return false;
217 }
218 
219 short TrackMultiSelector::select(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector<Point> &points) {
220  uint32_t vlayers = tk.hitPattern().trackerLayersWithMeasurement(), lhits = tk.numberOfLostHits();
221  double pt = tk.pt(), chi2n = tk.normalizedChi2();
222  int which = 0;
223  for (std::vector<TrackMultiSelector::Block>::const_iterator itb = blocks_.begin(), edb = blocks_.end(); itb != edb; ++itb, ++which) {
224  if ( ( itb->vlayers.first <= vlayers ) && ( vlayers <= itb->vlayers.second ) &&
225  ( itb->chi2n.first <= chi2n ) && ( chi2n <= itb->chi2n.second ) &&
226  ( itb->pt.first <= pt ) && ( pt <= itb->pt.second ) &&
227  ( itb->lhits.first <= lhits ) && ( lhits <= itb->lhits.second ) &&
228  testVtx(tk, beamSpot, points, *itb) )
229  {
230  return which;
231  }
232  }
233  return -1;
234 }
235 void TrackMultiSelector::selectVertices(const reco::VertexCollection &vtxs, std::vector<Point> &points) {
236  using namespace reco;
237 
238  int32_t toTake = vtxNumber_;
239  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
240  if ((it->tracksSize() >= vtxTracks_) &&
241  ( (it->chi2() == 0.0) || (TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()) ) >= vtxChi2Prob_) ) ) {
242  points.push_back(it->position());
243  toTake--; if (toTake == 0) break;
244  }
245  }
246 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
double d0Error() const
error on d0
Definition: TrackBase.h:211
TrackMultiSelector(const edm::ParameterSet &cfg)
constructor
virtual ~TrackMultiSelector()
destructor
void selectVertices(const reco::VertexCollection &vtxs, std::vector< Point > &points)
std::vector< Block > blocks_
filter psets
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:111
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
#define abs(x)
Definition: mlp_lapack.h:159
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:40
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
Definition: TrackBase.h:234
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< reco::TrackExtraRefProd > rTrackExtras_
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:13
std::vector< reco::TrackRefProd > rTracks_
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:47
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:42
U second(std::pair< T, U > const &p)
std::vector< edm::RefProd< std::vector< Trajectory > > > rTrajectories_
const T & max(const T &a, const T &b)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
std::auto_ptr< TrackingRecHitCollection > * selHits_
int trackerLayersWithMeasurement() const
Definition: HitPattern.h:705
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:51
double pt() const
track transverse momentum
Definition: TrackBase.h:131
void produce(edm::Event &evt, const edm::EventSetup &es)
process one event
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
#define end
Definition: vmac.h:38
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
bool copyTrajectories_
copy also trajectories and trajectory-&gt;track associations
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:223
bool first
Definition: L1TdeRCT.cc:94
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
RefProd< PROD > getRefBeforePut()
Definition: Event.h:97
std::vector< std::string > labels_
output labels
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:127
double dzError() const
error on dz
Definition: TrackBase.h:215
std::auto_ptr< reco::TrackExtraCollection > * selTrackExtras_
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:9
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:112
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:49
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:38
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:14
double sigmaZ() const
sigma z
Definition: BeamSpot.h:81
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:53
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void add(const TrackingRecHitRef &r)
add a reference to a RecHit
static std::string const emptyString("")
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:45
std::vector< TrackingRecHitRefProd > rHits_
const TrackResiduals & residuals() const
Definition: Track.h:117
std::auto_ptr< reco::TrackCollection > * selTracks_
some storage
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:105
bool splitOutputs_
split selections in more sets
std::auto_ptr< TrajTrackAssociationCollection > * selTTAss_
const Point & position() const
position
Definition: BeamSpot.h:63
bool testVtx(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector< Point > &points, const Block &cut)
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
std::auto_ptr< std::vector< Trajectory > > * selTrajs_
std::vector< std::pair< short, reco::TrackRef > > whereItWent_
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:121
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
tuple size
Write out results.
edm::InputTag src_
source collection label
*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
unsigned long long le
Definition: VDTMath.h:202
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:131
short select(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector< Point > &points)
return class, or -1 if rejected