test
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  copyExtras_(cfg.getUntrackedParameter<bool>("copyExtras", false)),
25  copyTrajectories_(cfg.getUntrackedParameter<bool>("copyTrajectories", false)),
26  splitOutputs_( cfg.getUntrackedParameter<bool>("splitOutputs", false) ),
27  vtxNumber_( cfg.getParameter<int32_t>("vtxNumber") ),
28  vtxTracks_( cfg.getParameter<uint32_t>("vtxTracks") ),
29  vtxChi2Prob_( cfg.getParameter<double>("vtxChi2Prob") )
30 {
31  edm::ParameterSet beamSpotPSet = cfg.getParameter<edm::ParameterSet>("beamspot");
32  beamspot_ = consumes<reco::BeamSpot>(beamSpotPSet.getParameter<edm::InputTag>("src"));
33  beamspotDZsigmas_ = beamSpotPSet.getParameter<double>("dzSigmas");
34  beamspotD0_ = beamSpotPSet.getParameter<double>("d0");
35  vertices_= consumes<reco::VertexCollection>( cfg.getParameter<edm::InputTag>( "vertices" ) );
36  tokenTracks= consumes<reco::TrackCollection>(src_);
37  if (copyTrajectories_) {
38  tokenTraj= consumes<std::vector<Trajectory> >(src_);
39  tokenTrajTrack= consumes<TrajTrackAssociationCollection>(src_);
40  }
41 
42  typedef std::vector<edm::ParameterSet> VPSet;
43  VPSet psets = cfg.getParameter<VPSet>("cutSets");
44  blocks_.reserve(psets.size());
45  for (VPSet::const_iterator it = psets.begin(), ed = psets.end(); it != ed; ++it) {
46  blocks_.push_back(TrackMultiSelector::Block(*it));
47  }
48 
49  if (splitOutputs_) {
50  char buff[15];
51  for (size_t i = 0; i < blocks_.size(); ++i) {
52  sprintf(buff,"set%d", static_cast<int>(i+1));
53  labels_.push_back(std::string(buff));
54  }
55  } else {
56  labels_.push_back(std::string(""));
57  }
58 
59  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
60  for (std::vector<std::string>::const_iterator li = labels_.begin(), le = labels_.end(); li != le; ++li) {
61  const char *l= li->c_str();
62  produces<reco::TrackCollection>(l).setBranchAlias( alias + "Tracks" + l);
63  if (copyExtras_) {
64  produces<reco::TrackExtraCollection>(l).setBranchAlias( alias + "TrackExtras" + l);
65  produces<TrackingRecHitCollection>(l).setBranchAlias( alias + "RecHits" + l);
66  if (copyTrajectories_) {
67  produces< std::vector<Trajectory> >(l).setBranchAlias( alias + "Trajectories" + l);
68  produces< TrajTrackAssociationCollection >(l).setBranchAlias( alias + "TrajectoryTrackAssociations" + l);
69  }
70  }
71  }
72 
73  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
74  selTracks_ = new std::auto_ptr<reco::TrackCollection>[nblocks];
75  selTrackExtras_ = new std::auto_ptr<reco::TrackExtraCollection>[nblocks];
76  selHits_ = new std::auto_ptr<TrackingRecHitCollection>[nblocks];
77  selTrajs_ = new std::auto_ptr< std::vector<Trajectory> >[nblocks];
78  selTTAss_ = new std::auto_ptr< TrajTrackAssociationCollection >[nblocks];
79  rTracks_ = std::vector<reco::TrackRefProd>(nblocks);
80  rHits_ = std::vector<TrackingRecHitRefProd>(nblocks);
81  rTrackExtras_ = std::vector<reco::TrackExtraRefProd>(nblocks);
82  rTrajectories_ = std::vector< edm::RefProd< std::vector<Trajectory> > >(nblocks);
83  for (size_t i = 0; i < nblocks; i++) {
84  selTracks_[i] = std::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
85  selTrackExtras_[i] = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
86  selHits_[i] = std::auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
87  selTrajs_[i] = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
88  selTTAss_[i] = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
89  }
90 
91 }
92 
94  delete [] selTracks_;
95  delete [] selTrackExtras_;
96  delete [] selHits_;
97  delete [] selTrajs_;
98  delete [] selTTAss_;
99 }
100 
102 {
103  using namespace std;
104  using namespace edm;
105  using namespace reco;
106 
107  size_t nblocks = splitOutputs_ ? blocks_.size() : 1;
108 
109  Handle<TrackCollection> hSrcTrack;
112 
114  evt.getByToken(vertices_, hVtx);
115  std::vector<Point> points;
116  if (vtxNumber_ != 0) selectVertices(*hVtx, points);
117 
119  evt.getByToken(beamspot_, hBsp);
120 
121  evt.getByToken( tokenTracks, hSrcTrack );
122 
123  for (size_t i = 0; i < nblocks; i++) {
124  selTracks_[i] = auto_ptr<TrackCollection>(new TrackCollection());
126  if (copyExtras_) {
127  selTrackExtras_[i] = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
128  selHits_[i] = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
131  }
132  }
133 
134  if (copyTrajectories_) whereItWent_.resize(hSrcTrack->size());
135  size_t current = 0;
136  for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
137  const Track & trk = * it;
138  short where = select(trk, *hBsp, points);
139  if (where == -1) {
140  if (copyTrajectories_) whereItWent_[current] = std::pair<short, reco::TrackRef>(-1, reco::TrackRef());
141  continue;
142  }
143  if (!splitOutputs_) where = 0;
144  selTracks_[where]->push_back( Track( trk ) ); // clone and store
145  if (!copyExtras_) continue;
146 
147  // TrackExtras
148  selTrackExtras_[where]->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
149  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
150  trk.outerStateCovariance(), trk.outerDetId(),
151  trk.innerStateCovariance(), trk.innerDetId(),
152  trk.seedDirection(), trk.seedRef() ) );
153  selTracks_[where]->back().setExtra( TrackExtraRef( rTrackExtras_[where], selTrackExtras_[where]->size() - 1) );
154  TrackExtra & tx = selTrackExtras_[where]->back();
155  tx.setResiduals(trk.residuals());
156  // TrackingRecHits
157  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
158  selHits_[where]->push_back( (*hit)->clone() );
159  tx.add( TrackingRecHitRef( rHits_[where], selHits_[where]->size() - 1) );
160  }
161  if (copyTrajectories_) {
162  whereItWent_[current] = std::pair<short, reco::TrackRef>(where, TrackRef(rTracks_[where], selTracks_[where]->size() - 1));
163  }
164  }
165  if ( copyTrajectories_ ) {
168  evt.getByToken(tokenTrajTrack, hTTAss);
169  evt.getByToken(tokenTraj, hTraj);
170  for (size_t i = 0; i < nblocks; i++) {
171  rTrajectories_[i] = evt.getRefBeforePut< vector<Trajectory> >(labels_[i]);
172  selTrajs_[i] = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
173  selTTAss_[i] = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
174  }
175  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
176  Ref< vector<Trajectory> > trajRef(hTraj, i);
178  if (match != hTTAss->end()) {
179  const Ref<TrackCollection> &trkRef = match->val;
180  short oldKey = static_cast<short>(trkRef.key());
181  if (whereItWent_[oldKey].first != -1) {
182  int where = whereItWent_[oldKey].first;
183  selTrajs_[where]->push_back( Trajectory(*trajRef) );
184  selTTAss_[where]->insert ( Ref< vector<Trajectory> >(rTrajectories_[where], selTrajs_[where]->size() - 1), whereItWent_[oldKey].second );
185  }
186  }
187  }
188  }
189 
190 
191  static const std::string emptyString;
192  for (size_t i = 0; i < nblocks; i++) {
193  const std::string & lbl = ( splitOutputs_ ? labels_[i] : emptyString);
194  evt.put(selTracks_[i], lbl);
195  if (copyExtras_ ) {
196  evt.put(selTrackExtras_[i], lbl);
197  evt.put(selHits_[i], lbl);
198  if ( copyTrajectories_ ) {
199  evt.put(selTrajs_[i], lbl);
200  evt.put(selTTAss_[i], lbl);
201  }
202  }
203  }
204 }
205 
207  const std::vector<Point> &points,
209  using std::abs;
210  double d0Err =abs(tk.d0Error()), dzErr = abs(tk.dzError()); // not fully sure they're > 0!
211  if (points.empty()) {
212  Point spot = beamSpot.position();
213  double dz = abs(tk.dz(spot)), d0 = abs(tk.dxy(spot));
214  return ( dz < beamspotDZsigmas_*beamSpot.sigmaZ() ) && ( d0 < beamspotD0_ );
215  }
216  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
217  double dz = abs(tk.dz(*point)), d0 = abs(tk.dxy(*point));
218  if ((dz < cut.dz) && (d0 < cut.d0)
219  && fabs(dz/std::max(dzErr,1e-9)) < cut.dzRel && (d0/std::max(d0Err,1e-8) < cut.d0Rel )) return true;
220  }
221  return false;
222 }
223 
224 short TrackMultiSelector::select(const reco::Track &tk, const reco::BeamSpot &beamSpot, const std::vector<Point> &points) {
225  uint32_t vlayers = tk.hitPattern().trackerLayersWithMeasurement(), lhits = tk.numberOfLostHits();
226  double pt = tk.pt(), chi2n = tk.normalizedChi2();
227  int which = 0;
228  for (std::vector<TrackMultiSelector::Block>::const_iterator itb = blocks_.begin(), edb = blocks_.end(); itb != edb; ++itb, ++which) {
229  if ( ( itb->vlayers.first <= vlayers ) && ( vlayers <= itb->vlayers.second ) &&
230  ( itb->chi2n.first <= chi2n ) && ( chi2n <= itb->chi2n.second ) &&
231  ( itb->pt.first <= pt ) && ( pt <= itb->pt.second ) &&
232  ( itb->lhits.first <= lhits ) && ( lhits <= itb->lhits.second ) &&
233  testVtx(tk, beamSpot, points, *itb) )
234  {
235  return which;
236  }
237  }
238  return -1;
239 }
240 void TrackMultiSelector::selectVertices(const reco::VertexCollection &vtxs, std::vector<Point> &points) {
241  using namespace reco;
242 
243  int32_t toTake = vtxNumber_;
244  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
245  if ((it->tracksSize() >= vtxTracks_) &&
246  ( (it->chi2() == 0.0) || (TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()) ) >= vtxChi2Prob_) ) ) {
247  points.push_back(it->position());
248  toTake--; if (toTake == 0) break;
249  }
250  }
251 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
double d0Error() const
error on d0
Definition: TrackBase.h:209
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:109
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
string emptyString
Definition: archive.py:29
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
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:232
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
void produce(edm::Event &evt, const edm::EventSetup &es) override
process one event
U second(std::pair< T, U > const &p)
std::vector< edm::RefProd< std::vector< Trajectory > > > rTrajectories_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
std::auto_ptr< TrackingRecHitCollection > * selHits_
int trackerLayersWithMeasurement() const
Definition: HitPattern.h:740
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:129
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define end
Definition: vmac.h:37
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:221
bool first
Definition: L1TdeRCT.cc:75
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
RefProd< PROD > getRefBeforePut()
Definition: Event.h:128
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:125
double dzError() const
error on dz
Definition: TrackBase.h:213
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
edm::EDGetTokenT< std::vector< Trajectory > > tokenTraj
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::EDGetTokenT< reco::TrackCollection > tokenTracks
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:14
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
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
edm::EDGetTokenT< reco::VertexCollection > vertices_
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:45
edm::EDGetTokenT< TrajTrackAssociationCollection > tokenTrajTrack
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:62
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_
volatile std::atomic< bool > shutdown_flag false
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:119
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
edm::EDGetTokenT< reco::BeamSpot > beamspot_
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