CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PrimaryVertexProducer.cc
Go to the documentation of this file.
2 
9 
13 
18 
19 
21  :theConfig(conf)
22 {
23 
24  fVerbose = conf.getUntrackedParameter<bool>("verbose", false);
25  trkToken = consumes<reco::TrackCollection>(conf.getParameter<edm::InputTag>("TrackLabel"));
26  bsToken = consumes<reco::BeamSpot>(conf.getParameter<edm::InputTag>("beamSpotLabel"));
27 
28  // select and configure the track selection
29  std::string trackSelectionAlgorithm=conf.getParameter<edm::ParameterSet>("TkFilterParameters").getParameter<std::string>("algorithm");
30  if(trackSelectionAlgorithm=="filter"){
31  theTrackFilter= new TrackFilterForPVFinding( conf.getParameter<edm::ParameterSet>("TkFilterParameters") );
32  }else if (trackSelectionAlgorithm=="filterWithThreshold"){
34  }else{
35  throw VertexException("PrimaryVertexProducerAlgorithm: unknown track selection algorithm: " + trackSelectionAlgorithm);
36  }
37 
38 
39  // select and configure the track clusterizer
40  std::string clusteringAlgorithm=conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<std::string>("algorithm");
41  if (clusteringAlgorithm=="gap"){
42  theTrackClusterizer = new GapClusterizerInZ(conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<edm::ParameterSet>("TkGapClusParameters"));
43  }else if(clusteringAlgorithm=="DA"){
44  theTrackClusterizer = new DAClusterizerInZ(conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<edm::ParameterSet>("TkDAClusParameters"));
45  }
46  // provide the vectorized version of the clusterizer, if supported by the build
47 #ifdef __GXX_EXPERIMENTAL_CXX0X__
48  else if(clusteringAlgorithm == "DA_vect") {
49  theTrackClusterizer = new DAClusterizerInZ_vect(conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<edm::ParameterSet>("TkDAClusParameters"));
50  }
51 #endif
52 
53 
54  else{
55  throw VertexException("PrimaryVertexProducerAlgorithm: unknown clustering algorithm: " + clusteringAlgorithm);
56  }
57 
58 
59  // select and configure the vertex fitters
60  if (conf.exists("vertexCollections")){
61  std::vector<edm::ParameterSet> vertexCollections =conf.getParameter< std::vector<edm::ParameterSet> >("vertexCollections");
62 
63  for( std::vector< edm::ParameterSet >::const_iterator algoconf = vertexCollections.begin(); algoconf != vertexCollections.end(); algoconf++){
64 
66  std::string fitterAlgorithm = algoconf->getParameter<std::string>("algorithm");
67  if (fitterAlgorithm=="KalmanVertexFitter") {
68  algorithm.fitter= new KalmanVertexFitter();
69  } else if( fitterAlgorithm=="AdaptiveVertexFitter") {
70  algorithm.fitter= new AdaptiveVertexFitter();
71  } else {
72  throw VertexException("PrimaryVertexProducerAlgorithm: unknown algorithm: " + fitterAlgorithm);
73  }
74  algorithm.label = algoconf->getParameter<std::string>("label");
75  algorithm.minNdof = algoconf->getParameter<double>("minNdof");
76  algorithm.useBeamConstraint=algoconf->getParameter<bool>("useBeamConstraint");
77  algorithm.vertexSelector=new VertexCompatibleWithBeam(VertexDistanceXY(), algoconf->getParameter<double>("maxDistanceToBeam"));
78  algorithms.push_back(algorithm);
79 
80  produces<reco::VertexCollection>(algorithm.label);
81  }
82  }else{
83  edm::LogWarning("MisConfiguration")<<"this module's configuration has changed, please update to have a vertexCollections=cms.VPSet parameter.";
84 
86  std::string fitterAlgorithm = conf.getParameter<std::string>("algorithm");
87  if (fitterAlgorithm=="KalmanVertexFitter") {
88  algorithm.fitter= new KalmanVertexFitter();
89  } else if( fitterAlgorithm=="AdaptiveVertexFitter") {
90  algorithm.fitter= new AdaptiveVertexFitter();
91  } else {
92  throw VertexException("PrimaryVertexProducerAlgorithm: unknown algorithm: " + fitterAlgorithm);
93  }
94  algorithm.label = "";
95  algorithm.minNdof = conf.getParameter<double>("minNdof");
96  algorithm.useBeamConstraint=conf.getParameter<bool>("useBeamConstraint");
97 
98  algorithm.vertexSelector=new VertexCompatibleWithBeam(VertexDistanceXY(), conf.getParameter<edm::ParameterSet>("PVSelParameters").getParameter<double>("maxDistanceToBeam"));
99 
100  algorithms.push_back(algorithm);
101  produces<reco::VertexCollection>(algorithm.label);
102  }
103 
104 
105 }
106 
107 
109  if (theTrackFilter) delete theTrackFilter;
111  for( std::vector <algo>::const_iterator algorithm=algorithms.begin(); algorithm!=algorithms.end(); algorithm++){
112  if (algorithm->fitter) delete algorithm->fitter;
113  if (algorithm->vertexSelector) delete algorithm->vertexSelector;
114  }
115 }
116 
117 
118 void
120 {
121 
122  // get the BeamSpot, it will alwys be needed, even when not used as a constraint
124  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
125  iEvent.getByToken(bsToken,recoBeamSpotHandle);
126  if (recoBeamSpotHandle.isValid()){
127  beamSpot = *recoBeamSpotHandle;
128  }else{
129  edm::LogError("UnusableBeamSpot") << "No beam spot available from EventSetup";
130  }
131 
132  bool validBS = true;
133  VertexState beamVertexState(beamSpot);
134  if ( (beamVertexState.error().cxx() <= 0.) ||
135  (beamVertexState.error().cyy() <= 0.) ||
136  (beamVertexState.error().czz() <= 0.) ) {
137  validBS = false;
138  edm::LogError("UnusableBeamSpot") << "Beamspot with invalid errors "<<beamVertexState.error().matrix();
139  }
140 
141 
142  // get RECO tracks from the event
143  // `tks` can be used as a ptr to a reco::TrackCollection
145  iEvent.getByToken(trkToken, tks);
146 
147 
148  // interface RECO tracks to vertex reconstruction
150  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theB);
151  std::vector<reco::TransientTrack> t_tks = (*theB).build(tks, beamSpot);
152  if(fVerbose) {std::cout << "RecoVertex/PrimaryVertexProducer"
153  << "Found: " << t_tks.size() << " reconstructed tracks" << "\n";
154  }
155 
156 
157  // select tracks
158  std::vector<reco::TransientTrack> && seltks = theTrackFilter->select( t_tks );
159 
160 
161  // clusterize tracks in Z
162  std::vector< std::vector<reco::TransientTrack> > && clusters = theTrackClusterizer->clusterize(seltks);
163  if (fVerbose){std::cout << " clustering returned "<< clusters.size() << " clusters from " << seltks.size() << " selected tracks" <<std::endl;}
164 
165 
166  // vertex fits
167  for( std::vector <algo>::const_iterator algorithm=algorithms.begin(); algorithm!=algorithms.end(); algorithm++){
168 
169 
170  std::auto_ptr<reco::VertexCollection> result(new reco::VertexCollection);
171  reco::VertexCollection & vColl = (*result);
172 
173 
174  std::vector<TransientVertex> pvs;
175  for (std::vector< std::vector<reco::TransientTrack> >::const_iterator iclus
176  = clusters.begin(); iclus != clusters.end(); iclus++) {
177 
178 
180  if( algorithm->useBeamConstraint && validBS &&((*iclus).size()>1) ){
181 
182  v = algorithm->fitter->vertex(*iclus, beamSpot);
183 
184  }else if( !(algorithm->useBeamConstraint) && ((*iclus).size()>1) ) {
185 
186  v = algorithm->fitter->vertex(*iclus);
187 
188  }// else: no fit ==> v.isValid()=False
189 
190 
191  if (fVerbose){
192  if (v.isValid()) std::cout << "x,y,z=" << v.position().x() <<" " << v.position().y() << " " << v.position().z() << std::endl;
193  else std::cout <<"Invalid fitted vertex\n";
194  }
195 
196  if (v.isValid()
197  && (v.degreesOfFreedom()>=algorithm->minNdof)
198  && (!validBS || (*(algorithm->vertexSelector))(v,beamVertexState))
199  ) pvs.push_back(v);
200  }// end of cluster loop
201 
202  if(fVerbose){
203  std::cout << "PrimaryVertexProducerAlgorithm::vertices candidates =" << pvs.size() << std::endl;
204  }
205 
206 
207  if (clusters.size()>2 && clusters.size() > 2*pvs.size())
208  edm::LogWarning("PrimaryVertexProducer") << "more than half of candidate vertices lost " << pvs.size() << ' ' << clusters.size();
209 
210  if (pvs.empty() && seltks.size()>5)
211  edm::LogWarning("PrimaryVertexProducer") << "no vertex found with " << seltks.size() << " tracks and " << clusters.size() <<" vertex-candidates";
212 
213  // sort vertices by pt**2 vertex (aka signal vertex tagging)
214  if(pvs.size()>1){
215  sort(pvs.begin(), pvs.end(), VertexHigherPtSquared());
216  }
217 
218 
219 
220  // convert transient vertices returned by the theAlgo to (reco) vertices
221  for (std::vector<TransientVertex>::const_iterator iv = pvs.begin();
222  iv != pvs.end(); iv++) {
223  reco::Vertex v = *iv;
224  vColl.push_back(v);
225  }
226 
227  if (vColl.empty()) {
228  GlobalError bse(beamSpot.rotatedCovariance3D());
229  if ( (bse.cxx() <= 0.) ||
230  (bse.cyy() <= 0.) ||
231  (bse.czz() <= 0.) ) {
233  we(0,0)=10000; we(1,1)=10000; we(2,2)=10000;
234  vColl.push_back(reco::Vertex(beamSpot.position(), we,0.,0.,0));
235  if(fVerbose){
236  std::cout <<"RecoVertex/PrimaryVertexProducer: "
237  << "Beamspot with invalid errors "<<bse.matrix()<<std::endl;
238  std::cout << "Will put Vertex derived from dummy-fake BeamSpot into Event.\n";
239  }
240  } else {
241  vColl.push_back(reco::Vertex(beamSpot.position(),
242  beamSpot.rotatedCovariance3D(),0.,0.,0));
243  if(fVerbose){
244  std::cout <<"RecoVertex/PrimaryVertexProducer: "
245  << " will put Vertex derived from BeamSpot into Event.\n";
246  }
247  }
248  }
249 
250  if(fVerbose){
251  int ivtx=0;
252  for(reco::VertexCollection::const_iterator v=vColl.begin();
253  v!=vColl.end(); ++v){
254  std::cout << "recvtx "<< ivtx++
255  << "#trk " << std::setw(3) << v->tracksSize()
256  << " chi2 " << std::setw(4) << v->chi2()
257  << " ndof " << std::setw(3) << v->ndof()
258  << " x " << std::setw(6) << v->position().x()
259  << " dx " << std::setw(6) << v->xError()
260  << " y " << std::setw(6) << v->position().y()
261  << " dy " << std::setw(6) << v->yError()
262  << " z " << std::setw(6) << v->position().z()
263  << " dz " << std::setw(6) << v->zError()
264  << std::endl;
265  }
266  }
267 
268  iEvent.put(result, algorithm->label);
269  }
270 
271 }
272 
273 
274 //define this as a plug-in
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
edm::EDGetTokenT< reco::BeamSpot > bsToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
Common base class.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< reco::TrackCollection > trkToken
const AlgebraicSymMatrix33 & matrix() const
TrackClusterizerInZ * theTrackClusterizer
T y() const
Definition: PV3DBase.h:63
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
virtual std::vector< std::vector< reco::TransientTrack > > clusterize(const std::vector< reco::TransientTrack > &tracks) const =0
int iEvent
Definition: GenABIO.cc:230
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
float degreesOfFreedom() const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
VertexCompatibleWithBeam * vertexSelector
GlobalPoint position() const
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
TrackFilterForPVFindingBase * theTrackFilter
virtual std::vector< reco::TransientTrack > select(const std::vector< reco::TransientTrack > &tracks) const =0
bool isValid() const
Definition: HandleBase.h:75
tuple conf
Definition: dbtoconf.py:185
virtual void produce(edm::Event &, const edm::EventSetup &)
const T & get() const
Definition: EventSetup.h:55
std::vector< algo > algorithms
PrimaryVertexProducer(const edm::ParameterSet &)
GlobalError error() const
Definition: VertexState.h:55
tuple cout
Definition: gather_cfg.py:121
const Point & position() const
position
Definition: BeamSpot.h:62
Covariance3DMatrix rotatedCovariance3D() const
Definition: BeamSpot.cc:78
T x() const
Definition: PV3DBase.h:62
bool isValid() const