ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/DGele/PhysicsTools/PatAlgos/plugins/PATSingleVertexSelector.cc
Revision: 1.1
Committed: Tue Oct 20 17:15:14 2009 UTC (15 years, 6 months ago) by dgele
Content type: text/plain
Branch: MAIN
Branch point for: ANA
Log Message:
Initial revision

File Contents

# User Rev Content
1 dgele 1.1 #include "PhysicsTools/PatAlgos/plugins/PATSingleVertexSelector.h"
2     #include "DataFormats/Common/interface/View.h"
3     #include "DataFormats/Candidate/interface/VertexCompositeCandidate.h"
4     #include <DataFormats/BeamSpot/interface/BeamSpot.h>
5    
6     #include <algorithm>
7    
8     using pat::PATSingleVertexSelector;
9    
10    
11     PATSingleVertexSelector::Mode
12     PATSingleVertexSelector::parseMode(const std::string &mode) {
13     if (mode == "firstVertex") {
14     return First;
15     } else if (mode == "nearestToCandidate") {
16     return NearestToCand;
17     } else if (mode == "fromCandidate") {
18     return FromCand;
19     } else if (mode == "beamSpot") {
20     return FromBeamSpot;
21     } else {
22     throw cms::Exception("Configuration") << "PATSingleVertexSelector: Mode '" << mode << "' not recognized or not supported.\n";
23     }
24     }
25    
26    
27     PATSingleVertexSelector::PATSingleVertexSelector(const edm::ParameterSet & iConfig) {
28     using namespace std;
29    
30     modes_.push_back( parseMode(iConfig.getParameter<std::string>("mode")) );
31     if (iConfig.exists("fallbacks")) {
32     vector<string> modes = iConfig.getParameter<vector<string> >("fallbacks");
33     for (vector<string>::const_iterator it = modes.begin(), ed = modes.end(); it != ed; ++it) {
34     modes_.push_back( parseMode(*it) );
35     }
36     }
37     if (hasMode_(First) || hasMode_(NearestToCand)) {
38     vertices_ = iConfig.getParameter<edm::InputTag>("vertices");
39     if (iConfig.existsAs<string>("vertexPreselection")) {
40     string presel = iConfig.getParameter<string>("vertexPreselection");
41     if (!presel.empty()) vtxPreselection_ = auto_ptr<VtxSel>(new VtxSel(presel));
42     }
43     }
44     if (hasMode_(NearestToCand) || hasMode_(FromCand)) {
45     candidates_ = iConfig.getParameter<vector<edm::InputTag> >("candidates");
46     if (iConfig.existsAs<string>("candidatePreselection")) {
47     string presel = iConfig.getParameter<string>("candidatePreselection");
48     if (!presel.empty()) candPreselection_ = auto_ptr<CandSel>(new CandSel(presel));
49     }
50     }
51     if (hasMode_(FromBeamSpot)) {
52     beamSpot_ = iConfig.getParameter<edm::InputTag>("beamSpot");
53     }
54     produces<vector<reco::Vertex> >();
55     }
56    
57    
58     PATSingleVertexSelector::~PATSingleVertexSelector()
59     {
60     }
61    
62     bool PATSingleVertexSelector::hasMode_(Mode mode) const {
63     return (std::find(modes_.begin(), modes_.end(), mode) != modes_.end());
64     }
65    
66     bool
67     PATSingleVertexSelector::filter(edm::Event & iEvent, const edm::EventSetup & iSetup) {
68     using namespace edm;
69     using namespace std;
70    
71     // Clear
72     selVtxs_.clear(); bestCand_ = 0;
73    
74     // Gather data from the Event
75     // -- vertex data --
76     if (hasMode_(First) || hasMode_(NearestToCand)) {
77     Handle<vector<reco::Vertex> > vertices;
78     iEvent.getByLabel(vertices_, vertices);
79     for (vector<reco::Vertex>::const_iterator itv = vertices->begin(), edv = vertices->end(); itv != edv; ++itv) {
80     if ((vtxPreselection_.get() != 0) && !((*vtxPreselection_)(*itv)) ) continue;
81     selVtxs_.push_back( &*itv );
82     }
83     }
84     // -- candidate data --
85     if (hasMode_(NearestToCand) || hasMode_(FromCand)) {
86     vector<pair<double, const reco::Candidate *> > cands;
87     for (vector<edm::InputTag>::const_iterator itt = candidates_.begin(), edt = candidates_.end(); itt != edt; ++itt) {
88     Handle<View<reco::Candidate> > theseCands;
89     iEvent.getByLabel(*itt, theseCands);
90     for (View<reco::Candidate>::const_iterator itc = theseCands->begin(), edc = theseCands->end(); itc != edc; ++itc) {
91     if ((candPreselection_.get() != 0) && !((*candPreselection_)(*itc))) continue;
92     cands.push_back( pair<double, const reco::Candidate *>(-itc->pt(), &*itc) );
93     }
94     }
95     if (!cands.empty()) bestCand_ = cands.front().second;
96     }
97    
98     // Run main mode + possible fallback modes
99     for (std::vector<Mode>::const_iterator itm = modes_.begin(), endm = modes_.end(); itm != endm; ++itm) {
100     if (filter_(*itm, iEvent, iSetup)) return true;
101     }
102     return false;
103    
104     // Clear
105     selVtxs_.clear(); bestCand_ = 0;
106     }
107    
108     bool
109     PATSingleVertexSelector::filter_(Mode mode, edm::Event & iEvent, const edm::EventSetup & iSetup) {
110     using namespace edm;
111     using namespace std;
112     switch(mode) {
113     case First: {
114     if (selVtxs_.empty()) return false;
115     auto_ptr<vector<reco::Vertex> > result(new vector<reco::Vertex>(1, *selVtxs_.front()));
116     iEvent.put(result);
117     return true;
118     }
119     case FromCand: {
120     if (bestCand_ == 0) return false;
121     reco::Vertex vtx;
122     if (typeid(*bestCand_) == typeid(reco::VertexCompositeCandidate)) {
123     vtx = reco::Vertex(bestCand_->vertex(), bestCand_->vertexCovariance(),
124     bestCand_->vertexChi2(), bestCand_->vertexNdof(), bestCand_->numberOfDaughters() );
125     } else {
126     vtx = reco::Vertex(bestCand_->vertex(), reco::Vertex::Error(), 0, 0, 0);
127     }
128     auto_ptr<vector<reco::Vertex> > result(new vector<reco::Vertex>(1, vtx));
129     iEvent.put(result);
130     return true;
131     }
132     case NearestToCand: {
133     if (selVtxs_.empty() || (bestCand_ == 0)) return false;
134     const reco::Vertex * which = 0;
135     float dzmin = 9999.0;
136     for (vector<const reco::Vertex *>::const_iterator itv = selVtxs_.begin(), edv = selVtxs_.end(); itv != edv; ++itv) {
137     float dz = abs((*itv)->z() - bestCand_->vz());
138     if (dz < dzmin) { dzmin = dz; which = *itv; }
139     }
140     if (which == 0) return false; // actually it should not happen, but better safe than sorry
141     auto_ptr<vector<reco::Vertex> > result(new vector<reco::Vertex>(1, *which));
142     iEvent.put(result);
143     return true;
144     }
145     case FromBeamSpot: {
146     Handle<reco::BeamSpot> beamSpot;
147     iEvent.getByLabel(beamSpot_, beamSpot);
148     reco::Vertex bs(beamSpot->position(), beamSpot->covariance3D(), 0, 0, 0);
149     auto_ptr<vector<reco::Vertex> > result(new vector<reco::Vertex>(1, bs));
150     iEvent.put(result);
151     return true;
152     }
153     default:
154     return false;
155     }
156     }
157    
158     #include "FWCore/Framework/interface/MakerMacros.h"
159     DEFINE_FWK_MODULE(PATSingleVertexSelector);