CMS 3D CMS Logo

MCVerticesAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MCVerticesAnalyzer
4 // Class: MCVerticesAnalyzer
5 //
13 //
14 // Original Author: Andrea Venturi
15 // Created: Thu Dec 16 16:32:56 CEST 2010
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <numeric>
22 
23 #include <vector>
24 
25 // user include files
28 
32 
34 
36 
39 
41 
45 
46 #include "TH1F.h"
47 #include "TH2F.h"
48 #include "TProfile.h"
49 
50 //
51 // class decleration
52 //
53 
54 class MCVerticesAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
55 public:
56  explicit MCVerticesAnalyzer(const edm::ParameterSet&);
57  ~MCVerticesAnalyzer() override;
58 
59 private:
60  void analyze(const edm::Event&, const edm::EventSetup&) override;
61 
62  // ----------member data ---------------------------
63 
64  const bool m_useweight;
65 
69 
70  TH1F* m_hnvtx;
71  TH2F* m_hnvtxvsbx;
72  TH1F* m_hlumi;
75  TProfile* m_hnvtxweightprof;
76  TH1F* m_hmainvtxx;
77  TH1F* m_hmainvtxy;
78  TH1F* m_hmainvtxz;
80 };
81 
82 //
83 // constants, enums and typedefs
84 //
85 
86 //
87 // static data member definitions
88 //
89 
90 //
91 // constructors and destructor
92 //
94  : m_useweight(iConfig.getParameter<bool>("useWeight")),
95  m_doubleToken(consumes<double>(iConfig.getParameter<edm::InputTag>("weightProduct"))),
96  m_vecPileupSummaryInfoToken(
97  consumes<std::vector<PileupSummaryInfo> >(iConfig.getParameter<edm::InputTag>("pileupSummaryCollection"))),
98  m_hepMCProductToken(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("mcTruthCollection"))) {
99  //now do what ever initialization is needed
100 
101  usesResource("TFileService");
103 
104  m_hnvtx = tfserv->make<TH1F>("nvtx", "Number of pileup vertices", 400, -0.5, 399.5);
105  m_hnvtx->GetXaxis()->SetTitle("Number of Interactions");
106 
107  m_hnvtxvsbx = tfserv->make<TH2F>("nvtxvsbx", "Number of pileup vertices vs BX", 9, -4.5, 4.5, 400, -0.5, 399.5);
108  m_hnvtxvsbx->GetXaxis()->SetTitle("BX number");
109  m_hnvtxvsbx->GetYaxis()->SetTitle("Number of Interactions");
110 
111  m_hlumi = tfserv->make<TH1F>("lumi", "BX luminosity*xsect", 200, 0., 50.);
112  m_hlumi->GetXaxis()->SetTitle("Average Number of Interactions");
113 
114  m_hnvtxvslumi = tfserv->make<TH2F>("nvtxvslumi", "Npileup vs BX luminosity*xsect", 200, 0., 50., 400, -0.5, 399.5);
115  m_hnvtxvslumi->GetXaxis()->SetTitle("Average Number of Interactions");
116  m_hnvtxvslumi->GetYaxis()->SetTitle("Number of Interactions");
117 
118  if (m_useweight) {
119  m_hnvtxweight = tfserv->make<TH1F>("nvtxweight", "Number of pileup vertices (1-w)", 400, -0.5, 399.5);
120  m_hnvtxweight->GetXaxis()->SetTitle("Number of Interactions");
122  tfserv->make<TProfile>("nvtxweightprof", "Mean (1-w) vs Number of pileup interactions", 400, -0.5, 399.5);
123  m_hnvtxweightprof->GetXaxis()->SetTitle("Number of Interactions");
124  }
125 
126  m_hmainvtxx = tfserv->make<TH1F>("mainvtxx", "Main vertex x position", 200, -.5, .5);
127  m_hmainvtxx->GetXaxis()->SetTitle("X (cm)");
128  m_hmainvtxy = tfserv->make<TH1F>("mainvtxy", "Main vertex y position", 200, -.5, .5);
129  m_hmainvtxy->GetXaxis()->SetTitle("Y (cm)");
130  m_hmainvtxz = tfserv->make<TH1F>("mainvtxz", "Main vertex z position", 600, -30., 30.);
131  m_hmainvtxz->GetXaxis()->SetTitle("Z (cm)");
132  m_hpileupvtxz = tfserv->make<TH1F>("pileupvtxz", "PileUp vertices z position", 600, -30., 30.);
133  m_hpileupvtxz->GetXaxis()->SetTitle("Z (cm)");
134 }
135 
137  // do anything here that needs to be done at desctruction time
138  // (e.g. close files, deallocate resources etc.)
139 }
140 
141 //
142 // member functions
143 //
144 
145 // ------------ method called to for each event ------------
147  double weight = 1.;
148 
149  if (m_useweight) {
150  edm::Handle<double> weightprod;
151  iEvent.getByToken(m_doubleToken, weightprod);
152 
153  weight = *weightprod;
154  }
155 
157  iEvent.getByToken(m_vecPileupSummaryInfoToken, pileupinfos);
158 
159  //
160 
161  if (pileupinfos.isValid()) {
162  // look for the intime PileupSummaryInfo
163 
164  std::vector<PileupSummaryInfo>::const_iterator pileupinfo;
165 
166  for (pileupinfo = pileupinfos->begin(); pileupinfo != pileupinfos->end(); ++pileupinfo) {
167  m_hnvtxvsbx->Fill(pileupinfo->getBunchCrossing(), pileupinfo->getPU_NumInteractions(), weight);
168  }
169 
170  for (pileupinfo = pileupinfos->begin(); pileupinfo != pileupinfos->end(); ++pileupinfo) {
171  if (pileupinfo->getBunchCrossing() == 0)
172  break;
173  }
174 
175  //
176 
177  if (pileupinfo->getBunchCrossing() != 0) {
178  edm::LogError("NoInTimePileUpInfo") << "Cannot find the in-time pileup info " << pileupinfo->getBunchCrossing();
179  } else {
180  m_hlumi->Fill(pileupinfo->getTrueNumInteractions(), weight);
181  m_hnvtx->Fill(pileupinfo->getPU_NumInteractions(), weight);
182  m_hnvtxvslumi->Fill(pileupinfo->getTrueNumInteractions(), pileupinfo->getPU_NumInteractions(), weight);
183 
184  if (m_useweight) {
185  m_hnvtxweight->Fill(pileupinfo->getPU_NumInteractions(), 1. - weight);
186  m_hnvtxweightprof->Fill(pileupinfo->getPU_NumInteractions(), 1. - weight);
187  }
188 
189  const std::vector<float>& zpositions = pileupinfo->getPU_zpositions();
190 
191  for (std::vector<float>::const_iterator zpos = zpositions.begin(); zpos != zpositions.end(); ++zpos) {
192  m_hpileupvtxz->Fill(*zpos, weight);
193  }
194  }
195  }
196  // main interaction part
197 
199  iEvent.getByToken(m_hepMCProductToken, EvtHandle);
200 
201  if (EvtHandle.isValid()) {
202  const HepMC::GenEvent* Evt = EvtHandle->GetEvent();
203 
204  // get the first vertex
205 
206  if (Evt->vertices_begin() != Evt->vertices_end()) {
207  m_hmainvtxx->Fill((*Evt->vertices_begin())->point3d().x() / 10., weight);
208  m_hmainvtxy->Fill((*Evt->vertices_begin())->point3d().y() / 10., weight);
209  m_hmainvtxz->Fill((*Evt->vertices_begin())->point3d().z() / 10., weight);
210  }
211  }
212 }
213 
214 //define this as a plug-in
Definition: weight.py:1
Log< level::Error, false > LogError
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< double > m_doubleToken
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > m_vecPileupSummaryInfoToken
edm::EDGetTokenT< edm::HepMCProduct > m_hepMCProductToken
MCVerticesAnalyzer(const edm::ParameterSet &)
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
void analyze(const edm::Event &, const edm::EventSetup &) override