test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
20 // system include files
21 #include <memory>
22 #include <numeric>
23 
24 #include <vector>
25 
26 // user include files
29 
33 
35 
37 
40 
42 
46 
47 
48 #include "TH1F.h"
49 #include "TH2F.h"
50 #include "TProfile.h"
51 
52 //
53 // class decleration
54 //
55 
56 
57 class MCVerticesAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
58 public:
59  explicit MCVerticesAnalyzer(const edm::ParameterSet&);
61 
62 private:
63  virtual void analyze(const edm::Event&, const edm::EventSetup&);
64 
65  // ----------member data ---------------------------
66 
67 
68 
69  const bool m_useweight;
70 
74 
75  TH1F* m_hnvtx;
76  TH2F* m_hnvtxvsbx;
77  TH1F* m_hlumi;
80  TProfile* m_hnvtxweightprof;
81  TH1F* m_hmainvtxx;
82  TH1F* m_hmainvtxy;
83  TH1F* m_hmainvtxz;
85 
86 };
87 
88 //
89 // constants, enums and typedefs
90 //
91 
92 //
93 // static data member definitions
94 //
95 
96 //
97 // constructors and destructor
98 //
100  : m_useweight( iConfig.getParameter< bool >( "useWeight" ) )
101  , m_doubleToken( consumes< double >( iConfig.getParameter< edm::InputTag >( "weightProduct" ) ) )
102  , m_vecPileupSummaryInfoToken( consumes< std::vector<PileupSummaryInfo> >( iConfig.getParameter< edm::InputTag >( "pileupSummaryCollection" ) ) )
103  , m_hepMCProductToken( consumes< edm::HepMCProduct >( iConfig.getParameter< edm::InputTag >( "mcTruthCollection" ) ) )
104 {
105  //now do what ever initialization is needed
106 
107 
108 
109  usesResource("TFileService");
111 
112  m_hnvtx = tfserv->make<TH1F>("nvtx","Number of pileup vertices",60,-0.5,59.5);
113  m_hnvtx->GetXaxis()->SetTitle("Number of Interactions");
114 
115  m_hnvtxvsbx = tfserv->make<TH2F>("nvtxvsbx","Number of pileup vertices vs BX",9,-4.5,4.5,60,-0.5,59.5);
116  m_hnvtxvsbx->GetXaxis()->SetTitle("BX number");
117  m_hnvtxvsbx->GetYaxis()->SetTitle("Number of Interactions");
118 
119  m_hlumi = tfserv->make<TH1F>("lumi","BX luminosity*xsect",200,0.,50.);
120  m_hlumi->GetXaxis()->SetTitle("Average Number of Interactions");
121 
122  m_hnvtxvslumi = tfserv->make<TH2F>("nvtxvslumi","Npileup vs BX luminosity*xsect",200,0.,50.,60,-0.5,59.5);
123  m_hnvtxvslumi->GetXaxis()->SetTitle("Average Number of Interactions"); m_hnvtxvslumi->GetYaxis()->SetTitle("Number of Interactions");
124 
125  if(m_useweight) {
126  m_hnvtxweight = tfserv->make<TH1F>("nvtxweight","Number of pileup vertices (1-w)",60,-0.5,59.5);
127  m_hnvtxweight->GetXaxis()->SetTitle("Number of Interactions");
128  m_hnvtxweightprof = tfserv->make<TProfile>("nvtxweightprof","Mean (1-w) vs Number of pileup interactions",60,-0.5,59.5);
129  m_hnvtxweightprof->GetXaxis()->SetTitle("Number of Interactions");
130  }
131 
132  m_hmainvtxx = tfserv->make<TH1F>("mainvtxx","Main vertex x position",200,-.5,.5);
133  m_hmainvtxx->GetXaxis()->SetTitle("X (cm)");
134  m_hmainvtxy = tfserv->make<TH1F>("mainvtxy","Main vertex y position",200,-.5,.5);
135  m_hmainvtxy->GetXaxis()->SetTitle("Y (cm)");
136  m_hmainvtxz = tfserv->make<TH1F>("mainvtxz","Main vertex z position",600,-30.,30.);
137  m_hmainvtxz->GetXaxis()->SetTitle("Z (cm)");
138  m_hpileupvtxz = tfserv->make<TH1F>("pileupvtxz","PileUp vertices z position",600,-30.,30.);
139  m_hpileupvtxz->GetXaxis()->SetTitle("Z (cm)");
140 
141 }
142 
143 
145 {
146 
147  // do anything here that needs to be done at desctruction time
148  // (e.g. close files, deallocate resources etc.)
149 
150 }
151 
152 
153 //
154 // member functions
155 //
156 
157 // ------------ method called to for each event ------------
158 void
160 {
161 
162  double weight = 1.;
163 
164  if(m_useweight) {
165  edm::Handle<double> weightprod;
166  iEvent.getByToken( m_doubleToken, weightprod );
167 
168  weight = *weightprod;
169 
170  }
171 
172 
174  iEvent.getByToken( m_vecPileupSummaryInfoToken, pileupinfos );
175 
176  //
177 
178  if(pileupinfos.isValid()) {
179 
180  // look for the intime PileupSummaryInfo
181 
182  std::vector<PileupSummaryInfo>::const_iterator pileupinfo;
183 
184  for(pileupinfo = pileupinfos->begin(); pileupinfo != pileupinfos->end() ; ++pileupinfo) {
185  m_hnvtxvsbx->Fill(pileupinfo->getBunchCrossing(),pileupinfo->getPU_NumInteractions(),weight);
186  }
187 
188 
189  for(pileupinfo = pileupinfos->begin(); pileupinfo != pileupinfos->end() ; ++pileupinfo) {
190  if(pileupinfo->getBunchCrossing()==0) break;
191  }
192 
193  //
194 
195  if(pileupinfo->getBunchCrossing()!=0) {
196  edm::LogError("NoInTimePileUpInfo") << "Cannot find the in-time pileup info " << pileupinfo->getBunchCrossing();
197  }
198  else {
199 
200  m_hlumi->Fill(pileupinfo->getTrueNumInteractions(),weight);
201  m_hnvtx->Fill(pileupinfo->getPU_NumInteractions(),weight);
202  m_hnvtxvslumi->Fill(pileupinfo->getTrueNumInteractions(),pileupinfo->getPU_NumInteractions(),weight);
203 
204  if(m_useweight) {
205  m_hnvtxweight->Fill(pileupinfo->getPU_NumInteractions(),1.-weight);
206  m_hnvtxweightprof->Fill(pileupinfo->getPU_NumInteractions(),1.-weight);
207  }
208 
209  const std::vector<float>& zpositions = pileupinfo->getPU_zpositions();
210 
211  for(std::vector<float>::const_iterator zpos = zpositions.begin() ; zpos != zpositions.end() ; ++zpos) {
212 
213  m_hpileupvtxz->Fill(*zpos,weight);
214 
215  }
216  }
217  }
218  // main interaction part
219 
221  iEvent.getByToken( m_hepMCProductToken, EvtHandle );
222 
223  if(EvtHandle.isValid()) {
224 
225  const HepMC::GenEvent* Evt = EvtHandle->GetEvent();
226 
227  // get the first vertex
228 
229  if(Evt->vertices_begin() != Evt->vertices_end()) {
230 
231  m_hmainvtxx->Fill((*Evt->vertices_begin())->point3d().x()/10.,weight);
232  m_hmainvtxy->Fill((*Evt->vertices_begin())->point3d().y()/10.,weight);
233  m_hmainvtxz->Fill((*Evt->vertices_begin())->point3d().z()/10.,weight);
234 
235  }
236  }
237 }
238 
239 //define this as a plug-in
edm::EDGetTokenT< double > m_doubleToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > m_vecPileupSummaryInfoToken
bool isValid() const
Definition: HandleBase.h:75
MCVerticesAnalyzer(const edm::ParameterSet &)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
int weight
Definition: histoStyle.py:50
edm::EDGetTokenT< edm::HepMCProduct > m_hepMCProductToken