CMS 3D CMS Logo

PatVertexAnalyzer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cmath>
3 #include <vector>
4 #include <string>
5 
6 #include <TH1.h>
7 #include <TProfile.h>
8 
16 
18 
25 
26 class PatVertexAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
27 public:
30  ~PatVertexAnalyzer() override;
31 
32  // virtual methods called from base class EDAnalyzer
33  void beginJob() override;
34  void analyze(const edm::Event &event, const edm::EventSetup &es) override;
35 
36 private:
37  // configuration parameters
40 
42  TH1 *x_, *y_, *z_;
43  TH1 *xErr_, *yErr_, *zErr_;
45  TH1 *xPull_, *yPull_, *zPull_;
46 };
47 
49  : srcToken_(consumes<reco::VertexCollection>(params.getParameter<edm::InputTag>("src"))),
50  genParticlesToken_(consumes<reco::GenParticleCollection>(params.getParameter<edm::InputTag>("mc"))) {
51  usesResource(TFileService::kSharedResource);
52 }
53 
55 
57  // retrieve handle to auxiliary service
58  // used for storing histograms into ROOT file
60 
61  nVertices_ = fs->make<TH1F>("nVertices", "number of reconstructed primary vertices", 50, 0, 50);
62  nTracks_ = fs->make<TH1F>("nTracks", "number of tracks at primary vertex", 100, 0, 300);
63  x_ = fs->make<TH1F>("pvX", "primary vertex x", 100, -0.1, 0.1);
64  y_ = fs->make<TH1F>("pvY", "primary vertex y", 100, -0.1, 0.1);
65  z_ = fs->make<TH1F>("pvZ", "primary vertex z", 100, -30, 30);
66  xErr_ = fs->make<TH1F>("pvErrorX", "primary vertex x error", 100, 0, 0.005);
67  yErr_ = fs->make<TH1F>("pvErrorY", "primary vertex y error", 100, 0, 0.005);
68  zErr_ = fs->make<TH1F>("pvErrorZ", "primary vertex z error", 100, 0, 0.01);
69  xDelta_ = fs->make<TH1F>("pvDeltaX", "x shift wrt simulated vertex", 100, -0.01, 0.01);
70  yDelta_ = fs->make<TH1F>("pvDeltaY", "y shift wrt simulated vertex", 100, -0.01, 0.01);
71  zDelta_ = fs->make<TH1F>("pvDeltaZ", "z shift wrt simulated vertex", 100, -0.02, 0.02);
72  xPull_ = fs->make<TH1F>("pvPullX", "primary vertex x pull", 100, -5, 5);
73  yPull_ = fs->make<TH1F>("pvPullY", "primary vertex y pull", 100, -5, 5);
74  zPull_ = fs->make<TH1F>("pvPullZ", "primary vertex z pull", 100, -5, 5);
75 }
76 
78  // handle to the primary vertex collection
80  event.getByToken(srcToken_, pvHandle);
81 
82  // handle to the generator particles (i.e. the MC truth)
84  event.getByToken(genParticlesToken_, genParticlesHandle);
85 
86  // extract the position of the simulated vertex
87  math::XYZPoint simPV = (*genParticlesHandle)[2].vertex();
88 
89  // the number of reconstructed primary vertices
90  nVertices_->Fill(pvHandle->size());
91 
92  // if we have at least one, use the first (highest pt^2 sum)
93  if (!pvHandle->empty()) {
94  const reco::Vertex &pv = (*pvHandle)[0];
95 
96  nTracks_->Fill(pv.tracksSize());
97 
98  x_->Fill(pv.x());
99  y_->Fill(pv.y());
100  z_->Fill(pv.z());
101 
102  xErr_->Fill(pv.xError());
103  yErr_->Fill(pv.yError());
104  zErr_->Fill(pv.zError());
105 
106  xDelta_->Fill(pv.x() - simPV.X());
107  yDelta_->Fill(pv.y() - simPV.Y());
108  zDelta_->Fill(pv.z() - simPV.Z());
109 
110  xPull_->Fill((pv.x() - simPV.X()) / pv.xError());
111  yPull_->Fill((pv.y() - simPV.Y()) / pv.yError());
112  zPull_->Fill((pv.z() - simPV.Z()) / pv.zError());
113 
114  // we could access the tracks using the
115  // pv.tracks_begin() ... pv.tracks_end() iterators
116  }
117 }
118 
120 
static const std::string kSharedResource
Definition: TFileService.h:76
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
edm::EDGetTokenT< reco::GenParticleCollection > genParticlesToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< reco::VertexCollection > srcToken_
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
void beginJob() override
void analyze(const edm::Event &event, const edm::EventSetup &es) override
~PatVertexAnalyzer() override
def pv(vc)
Definition: MetAnalyzer.py:7
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
fixed size matrix
HLT enums.
PatVertexAnalyzer(const edm::ParameterSet &params)
constructor and destructor
Definition: event.py:1