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