CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PrimaryVertexProducerAlgorithm.cc
Go to the documentation of this file.
5 
15 #include <algorithm>
16 
17 using namespace reco;
18 
19 //
20 // constructors and destructor
21 //
23  // extract relevant parts of config for components
24  : theConfig(conf),
25  theVertexSelector(VertexDistanceXY(),
26  conf.getParameter<edm::ParameterSet>("PVSelParameters").getParameter<double>("maxDistanceToBeam"))
27 {
28  edm::LogInfo("PVDebugInfo")
29  << "PVSelParameters::maxDistanceToBeam = "
30  << conf.getParameter<edm::ParameterSet>("PVSelParameters").getParameter<double>("maxDistanceToBeam") << "\n";
31 
32 
33  fUseBeamConstraint = conf.getParameter<bool>("useBeamConstraint");
34  fVerbose = conf.getUntrackedParameter<bool>("verbose", false);
35  fMinNdof = conf.getParameter<double>("minNdof");
36  fFailsafe = true; //conf.getUntrackedParameter<bool>("failsafe",true);
37 
38 
39  // select and configure the track selection
40  std::string trackSelectionAlgorithm=conf.getParameter<edm::ParameterSet>("TkFilterParameters").getParameter<std::string>("algorithm");
41  if(trackSelectionAlgorithm=="filter"){
42  theTrackFilter= new TrackFilterForPVFinding( conf.getParameter<edm::ParameterSet>("TkFilterParameters") );
43  }else if (trackSelectionAlgorithm=="filterWithThreshold"){
45  }else{
46  throw VertexException("PrimaryVertexProducerAlgorithm: unknown track selection algorithm: " + trackSelectionAlgorithm);
47  }
48 
49 
50  // select and configure the track clusterizer
51  std::string clusteringAlgorithm=conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<std::string>("algorithm");
52  if (clusteringAlgorithm=="gap"){
53  theTrackClusterizer = new GapClusterizerInZ(conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<edm::ParameterSet>("TkGapClusParameters"));
54  }else if(clusteringAlgorithm=="DA"){
55  theTrackClusterizer = new DAClusterizerInZ(conf.getParameter<edm::ParameterSet>("TkClusParameters").getParameter<edm::ParameterSet>("TkDAClusParameters"));
56  }else{
57  throw VertexException("PrimaryVertexProducerAlgorithm: unknown clustering algorithm: " + clusteringAlgorithm);
58  }
59 
60  // select and configure the vertex fitter
61  std::string algorithm = conf.getParameter<std::string>("algorithm");
62  fapply_finder = false;
63  if (algorithm == "TrimmedKalmanFinder") {
64  fapply_finder = true;
65  theFinder.setParameters(conf.getParameter<edm::ParameterSet>("VtxFinderParameters"));
66  } else if (algorithm=="KalmanVertexFitter") {
68  } else if( algorithm=="AdaptiveVertexFitter") {
70  } else {
71  throw VertexException("PrimaryVertexProducerAlgorithm: unknown algorithm: " + algorithm);
72  }
73 
74  edm::LogInfo("PVDebugInfo")
75  << "Using " << algorithm << "\n";
76  edm::LogInfo("PVDebugInfo")
77  << "beam-constraint " << fUseBeamConstraint << "\n";
78 
79  edm::LogInfo("PVDebugInfo")
80  << "PV producer algorithm initialization: done" << "\n";
81 
82 }
83 
84 
86 {
87  if (theFitter) delete theFitter;
88  if (theTrackFilter) delete theTrackFilter;
90 }
91 
92 
93 //
94 // member functions
95 //
96 
97 // obsolete method, unfortunately required throgh inheritance from VertexReconstructor
98 std::vector<TransientVertex>
99 PrimaryVertexProducerAlgorithm::vertices(const std::vector<reco::TransientTrack> & tracks) const
100 {
101 
102  throw VertexException("PrimaryVertexProducerAlgorithm: cannot make a Primary Vertex without a beam spot constraint " );
103 
104  /* std::cout<< "PrimaryVertexProducer::vertices> Obsolete function, using dummy beamspot " << std::endl;
105  reco::BeamSpot dummyBeamSpot;
106  dummyBeamSpot.dummy();
107  return vertices(tracks,dummyBeamSpot); */
108  return std::vector<TransientVertex>();
109 }
110 
111 
112 std::vector<TransientVertex>
113 PrimaryVertexProducerAlgorithm::vertices(const std::vector<reco::TransientTrack> & tracks,
114  const reco::BeamSpot & beamSpot) const
115 {
116  bool validBS = true;
117  VertexState beamVertexState(beamSpot);
118  if ( (beamVertexState.error().cxx() <= 0.) ||
119  (beamVertexState.error().cyy() <= 0.) ||
120  (beamVertexState.error().czz() <= 0.) ) {
121  validBS = false;
122  edm::LogError("UnusableBeamSpot") << "Beamspot with invalid errors "<<beamVertexState.error().matrix();
123  }
124 
125  if ( fapply_finder) {
126  return theFinder.vertices( tracks );
127  }
128  std::vector<TransientVertex> pvs;
129 
130 
131  // select tracks
132  std::vector<TransientTrack> seltks = theTrackFilter->select( tracks );
133 
134 
135  // clusterize tracks in Z
136  std::vector< std::vector<reco::TransientTrack> > clusters = theTrackClusterizer->clusterize(seltks);
137  if (fVerbose){std::cout << " clustering returned "<< clusters.size() << " clusters from " << seltks.size() << " selected tracks" <<std::endl;}
138 
139 
140  // look for primary vertices in each cluster
141  std::vector<TransientVertex> pvCand;
142  int nclu=0;
143  for (std::vector< std::vector<reco::TransientTrack> >::const_iterator iclus
144  = clusters.begin(); iclus != clusters.end(); iclus++) {
145 
146 
148  if( fUseBeamConstraint && validBS &&((*iclus).size()>1) ){
149  if (fVerbose){std::cout << " constrained fit with "<< (*iclus).size() << " tracks" <<std::endl;}
150  v = theFitter->vertex(*iclus, beamSpot);
151  if (v.isValid() && (v.degreesOfFreedom()>=fMinNdof)) pvCand.push_back(v);
152 
153  if (fVerbose){
154  if (v.isValid()) std::cout << "x,y,z=" << v.position().x() <<" " << v.position().y() << " " << v.position().z() << std::endl;
155  else std::cout <<"Invalid fitted vertex\n";
156  }
157 
158  }else if((*iclus).size()>1){
159  if (fVerbose){std::cout << " unconstrained fit with "<< (*iclus).size() << " tracks" << std::endl;}
160 
161  v = theFitter->vertex(*iclus);
162  if (v.isValid() && (v.degreesOfFreedom()>=fMinNdof)) pvCand.push_back(v);
163 
164  if (fVerbose){
165  if (v.isValid()) std::cout << "x,y,z=" << v.position().x() <<" " << v.position().y() << " " << v.position().z() << std::endl;
166  else std::cout <<"Invalid fitted vertex\n";
167  }
168 
169  }
170 
171  nclu++;
172 
173  }// end of cluster loop
174 
175  if(fVerbose){
176  std::cout << "PrimaryVertexProducerAlgorithm::vertices candidates =" << pvCand.size() << std::endl;
177  }
178 
179 
180 
181  // select vertices compatible with beam
182  int npv=0;
183  for (std::vector<TransientVertex>::const_iterator ipv = pvCand.begin();
184  ipv != pvCand.end(); ipv++) {
185  if(fVerbose){
186  std::cout << "PrimaryVertexProducerAlgorithm::vertices cand " << npv++ << " sel=" <<
187  (validBS && theVertexSelector(*ipv,beamVertexState)) << " z=" << ipv->position().z() << std::endl;
188  }
189  if (!validBS || theVertexSelector(*ipv,beamVertexState)) pvs.push_back(*ipv);
190  }
191 
192 
193  if(pvs.size()>0){
194 
195  // sort vertices by pt**2 vertex (aka signal vertex tagging)
196  // sort(pvs.begin(), pvs.end(), VertexHigherPtSquared());
197 
198  // avoid re-evaluating sumptsquared for each comparison
200  std::vector< std::pair< double , unsigned int > > ptsqpv;
201  for(unsigned int i=0; i<pvs.size(); i++){ ptsqpv.push_back( std::make_pair(V.sumPtSquared(pvs.at(i)), i));}
202  std::stable_sort(ptsqpv.begin(), ptsqpv.end());
203  std::vector<TransientVertex> pvsorted( pvs.size());
204  for(unsigned int i=0; i<pvs.size(); i++){ pvsorted.push_back(pvs.at(ptsqpv[i].second));}
205  return pvsorted;
206 
207  }else{
208 
209  if ( fFailsafe
210  && (seltks.size()>1)
211  && ( (clusters.size()!=1) || ( (clusters.size()==1) && (clusters.begin()->size()<seltks.size())) )
212  )
213  {
214  // if no vertex was found, try fitting all selected tracks, unless this has already been tried
215  // in low/no pile-up situations with low multiplicity vertices, this can recover vertices lost in clustering
216  // with very small zSep. Only makes sense when used with a robust fitter, like the AdaptiveVertexFitter
218  if( fUseBeamConstraint && validBS ){
219  v = theFitter->vertex(seltks, beamSpot);
220  }else{
221  v = theFitter->vertex(seltks);
222  }
223  if (fVerbose){ std::cout << "PrimaryVertexProducerAlgorithm: failsafe vertex "
224  <<" tracks=" << seltks.size()
225  <<" valid()=" << v.isValid() << " ndof()=" << v.degreesOfFreedom()
226  <<" selected="<< theVertexSelector(v,beamVertexState) << std::endl; }
227  if ( v.isValid() && (v.degreesOfFreedom()>=fMinNdof) && (theVertexSelector(v,beamVertexState)) ) pvs.push_back(v);
228  }
229 
230  }
231 
232 
233  return pvs;
234 
235 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
< trclass="colgroup">< tdclass="colgroup"colspan=5 > Ecal cluster collections</td ></tr >< tr >< td >< ahref="classreco_1_1BasicCluster.html"> reco::BasicCluster</a ></td >< td >< ahref="DataFormats_EgammaReco.html"> reco::BasicClusterCollection</a ></td >< td >< ahref="#"> hybridSuperClusters</a ></td >< tdclass="description"> Basic clusters reconstructed with hybrid algorithm(barrel only)</td >< td >S.Rahatlou</td ></tr >< tr >< td >< a href
Common base class.
const AlgebraicSymMatrix33 & matrix() const
virtual std::vector< TransientVertex > vertices(const std::vector< reco::TransientTrack > &tracks) const
T y() const
Definition: PV3DBase.h:62
virtual std::vector< std::vector< reco::TransientTrack > > clusterize(const std::vector< reco::TransientTrack > &tracks) const =0
virtual std::vector< TransientVertex > vertices(const std::vector< reco::TransientTrack > &tracks) const
virtual CachingVertex< N > vertex(const std::vector< reco::TransientTrack > &tracks) const =0
float degreesOfFreedom() const
GlobalPoint position() const
T z() const
Definition: PV3DBase.h:63
virtual std::vector< reco::TransientTrack > select(const std::vector< reco::TransientTrack > &tracks) const =0
PrimaryVertexProducerAlgorithm(const edm::ParameterSet &)
void setParameters(const edm::ParameterSet &)
tuple conf
Definition: dbtoconf.py:185
TrackFilterForPVFindingBase * theTrackFilter
double sumPtSquared(const reco::Vertex &v) const
tuple tracks
Definition: testEve_cfg.py:39
GlobalError error() const
Definition: VertexState.h:34
tuple cout
Definition: gather_cfg.py:121
T x() const
Definition: PV3DBase.h:61
bool isValid() const
mathSSE::Vec4< T > v