CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VertexFromTrackProducer.cc
Go to the documentation of this file.
2 
9 
12 
19 
20 //using namespace reco;
21 
22 //
23 // constants, enums and typedefs
24 //
25 
26 //
27 // static data member definitions
28 //
29 
30 //
31 // constructors and destructor
32 //
34  : theConfig(conf)
35 {
36  edm::LogInfo("PVDebugInfo")
37  << "Initializing VertexFromTrackProducer" << "\n";
38  fVerbose = conf.getUntrackedParameter<bool>("verbose", false);
39  trackLabel = conf.getParameter<edm::InputTag>("trackLabel");
40  fIsRecoCandidate = conf.getParameter<bool>("isRecoCandidate");
41  fUseBeamSpot = conf.getParameter<bool>("useBeamSpot");
42  fUseVertex = conf.getParameter<bool>("useVertex");
43  fUseTriggerFilterElectrons = conf.getParameter<bool>("useTriggerFilterElectrons");
44  fUseTriggerFilterMuons = conf.getParameter<bool>("useTriggerFilterMuons");
45  triggerFilterElectronsSrc = conf.getParameter<edm::InputTag>("triggerFilterElectronsSrc");
46  triggerFilterMuonsSrc = conf.getParameter<edm::InputTag>("triggerFilterMuonsSrc");
47  vertexLabel = conf.getParameter<edm::InputTag>("vertexLabel");
48  beamSpotLabel = conf.getParameter<edm::InputTag>("beamSpotLabel");
49 
50  produces<reco::VertexCollection>();
51 
52 }
53 
54 
56 
57 //
58 // member functions
59 //
60 
61 // ------------ method called to produce the data ------------
62 void
64 {
65  using namespace edm;
66 
67  std::auto_ptr<reco::VertexCollection> result(new reco::VertexCollection);
69 
70  math::XYZPoint vertexPoint;
71  bool vertexAvailable = false;
72 
73  // get the BeamSpot
74  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
75  iEvent.getByLabel(beamSpotLabel,recoBeamSpotHandle);
76  if (recoBeamSpotHandle.isValid()){
77  reco::BeamSpot beamSpot = *recoBeamSpotHandle;
78  vertexPoint = beamSpot.position();
79  }else{
80  edm::LogError("UnusableBeamSpot") << "No beam spot found in Event";
81  }
82 
83  if(fUseVertex)
84  {
85  // get the Vertex
86  edm::Handle<edm::View<reco::Vertex> > recoVertexHandle;
87  iEvent.getByLabel(vertexLabel,recoVertexHandle);
88  if ((recoVertexHandle.isValid()) && (recoVertexHandle->size()>0)){
89  reco::Vertex vertex = recoVertexHandle->at(0);
90  vertexPoint = vertex.position();
91  vertexAvailable = true;
92  }else {
93  edm::LogInfo("UnusableVertex")
94  << "No vertex found in Event, beam spot used instaed" << "\n";
95  }
96  }
97 
98  const reco::Track* track = 0;
100  {
102  iEvent.getByLabel(trackLabel, candidateHandle);
103  if ((candidateHandle.isValid())&&(candidateHandle->size()>0)){
104  double maxpt=0.;
105  unsigned i_maxpt=0;
106  for (unsigned i = 0; i < candidateHandle->size(); ++i) {
107  double pt=candidateHandle->ptrAt(i)->pt();
108  if(pt>maxpt)
109  {
110  i_maxpt=i;
111  maxpt=pt;
112  }
113  }
114  track = dynamic_cast<const reco::Track*>(candidateHandle->ptrAt(i_maxpt)->bestTrack());
115  }
116  }
117  else if(fUseTriggerFilterElectrons) {
119  iEvent.getByLabel(triggerFilterElectronsSrc, triggerfilter);
120  std::vector<reco::ElectronRef> recocandidates;
121  triggerfilter->getObjects(trigger::TriggerElectron,recocandidates);
122  if ((recocandidates.size()>0)){
123  double maxpt=0.;
124  unsigned i_maxpt=0;
125  for (unsigned i = 0; i < recocandidates.size(); ++i) {
126  double pt=recocandidates.at(i)->pt();
127  if(pt>maxpt)
128  {
129  i_maxpt=i;
130  maxpt=pt;
131  }
132  track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
133  }
134  }
135  }
136  else if(fUseTriggerFilterMuons) {
138  iEvent.getByLabel(triggerFilterMuonsSrc, triggerfilter);
139  std::vector<reco::RecoChargedCandidateRef> recocandidates;
140  triggerfilter->getObjects(trigger::TriggerMuon,recocandidates);
141  if ((recocandidates.size()>0)){
142  double maxpt=0.;
143  unsigned i_maxpt=0;
144  for (unsigned i = 0; i < recocandidates.size(); ++i) {
145  double pt=recocandidates.at(i)->pt();
146  if(pt>maxpt)
147  {
148  i_maxpt=i;
149  maxpt=pt;
150  }
151  track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
152  }
153  }
154  }
155  else {
157  iEvent.getByLabel(trackLabel, trackHandle);
158  if ((trackHandle.isValid())&&(trackHandle->size()>0)){
159  double maxpt=0.;
160  unsigned i_maxpt=0;
161  for (unsigned i = 0; i < trackHandle->size(); ++i) {
162  double pt=trackHandle->ptrAt(i)->pt();
163  if(pt>maxpt)
164  {
165  i_maxpt=i;
166  maxpt=pt;
167  }
168  }
169  track = dynamic_cast<const reco::Track*>(&*trackHandle->ptrAt(i_maxpt));
170  }
171  }
172 
173  if(track) {
174  if(fUseBeamSpot || (fUseVertex && vertexAvailable) ) {
175  vertexPoint.SetZ(vertexPoint.z()+track->dz(vertexPoint));
176  }
177  else {
178  vertexPoint.SetZ(track->vz());
179  }
180  }
181  math::Error<3>::type noErrors;
182  reco::Vertex v(vertexPoint, noErrors);
183  vColl.push_back(v);
184 
185  // provide beamspot or primary vertex if no candidate found
186  //if(vColl.size()==0)
187  //{
188  // math::Error<3>::type noErrors;
189  // reco::Vertex v(vertexPoint, noErrors);
190  // vColl.push_back(v);
191  //}
192 
193  if(fVerbose){
194  int ivtx=0;
195  for(reco::VertexCollection::const_iterator v=vColl.begin();
196  v!=vColl.end(); ++v){
197  std::cout << "recvtx "<< ivtx++
198  << " x " << std::setw(6) << v->position().x()
199  << " dx " << std::setw(6) << v->xError()
200  << " y " << std::setw(6) << v->position().y()
201  << " dy " << std::setw(6) << v->yError()
202  << " z " << std::setw(6) << v->position().z()
203  << " dz " << std::setw(6) << v->zError()
204  << std::endl;
205  }
206  }
207 
208 
209  *result = vColl;
210  iEvent.put(result);
211 
212 }
T getParameter(std::string const &) const
virtual void produce(edm::Event &, const edm::EventSetup &)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
double zError() const
error on z
Definition: Vertex.h:105
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
ErrorD< N >::type type
Definition: Error.h:30
const Point & position() const
position
Definition: Vertex.h:93
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
tuple result
Definition: query.py:137
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
tuple conf
Definition: dbtoconf.py:185
double xError() const
error on x
Definition: Vertex.h:101
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
tuple cout
Definition: gather_cfg.py:121
const Point & position() const
position
Definition: BeamSpot.h:63
VertexFromTrackProducer(const edm::ParameterSet &)
mathSSE::Vec4< T > v
double yError() const
error on y
Definition: Vertex.h:103