ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVStudy.cc
Revision: 1.19
Committed: Sat Jan 30 18:27:09 2010 UTC (15 years, 3 months ago) by yygao
Content type: text/plain
Branch: MAIN
Changes since 1.18: +345 -420 lines
Log Message:
Remove pixelVertices plots

File Contents

# User Rev Content
1 jengbou 1.1 // -*- C++ -*-
2     //
3     // Package: PVStudy
4     // Class: PVStudy
5     //
6     /**\class PVStudy PVStudy.cc UserCode/PVStudy/plugins/PVStudy.cc
7    
8     Description: <one line class summary>
9    
10     Implementation:
11     <Notes on implementation>
12     */
13     //
14     // Original Author: "Geng-yuan Jeng/UC Riverside"
15 yygao 1.17 // "Yanyan Gao/Fermilab ygao@fnal.gov"
16 jengbou 1.1 // Created: Thu Aug 20 11:55:40 CDT 2009
17 jengbou 1.18 // $Id: PVStudy.cc,v 1.17 2010/01/26 12:06:49 yygao Exp $
18 jengbou 1.1 //
19     //
20    
21    
22     // system include files
23     #include <memory>
24     #include <string>
25     #include <vector>
26     #include <iostream>
27     #include <sstream>
28    
29     // user include files
30     #include "FWCore/Framework/interface/Frameworkfwd.h"
31     #include "FWCore/Framework/interface/EDAnalyzer.h"
32    
33     #include "FWCore/Framework/interface/Event.h"
34     #include "FWCore/Framework/interface/MakerMacros.h"
35    
36     #include "FWCore/ParameterSet/interface/ParameterSet.h"
37     #include "FWCore/Utilities/interface/InputTag.h"
38     #include "DataFormats/TrackReco/interface/Track.h"
39     #include "DataFormats/TrackReco/interface/TrackFwd.h"
40     #include "FWCore/ServiceRegistry/interface/Service.h"
41     #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
42     #include "UserCode/PVStudy/interface/PVStudy.h"
43     //
44     #include "DataFormats/VertexReco/interface/Vertex.h"
45     #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
46    
47     // simulated vertices,..., add <use name=SimDataFormats/Vertex> and <../Track>
48     #include <SimDataFormats/Vertex/interface/SimVertex.h>
49     #include <SimDataFormats/Vertex/interface/SimVertexContainer.h>
50     #include <SimDataFormats/Track/interface/SimTrack.h>
51     #include <SimDataFormats/Track/interface/SimTrackContainer.h>
52 yygao 1.17 // BeamSpot
53     #include "DataFormats/BeamSpot/interface/BeamSpot.h"
54    
55 jengbou 1.1 //root
56 jengbou 1.4 #include <TROOT.h>
57     #include <TF1.h>
58     #include <TString.h>
59     #include <TStyle.h>
60     #include <TPaveStats.h>
61     #include <TPad.h>
62 jengbou 1.1
63     using namespace std;
64 yygao 1.17 typedef math::XYZTLorentzVectorF LorentzVector;
65     typedef math::XYZPoint Point;
66 jengbou 1.1
67 yygao 1.7 PVStudy::PVStudy(const edm::ParameterSet& iConfig)
68 jengbou 1.1 {
69 yygao 1.7 //=======================================================================
70     // Get configuration for TrackTupleMaker
71     //=======================================================================
72 jengbou 1.13 simG4_ = iConfig.getParameter<edm::InputTag>( "simG4" );
73     trackCollectionTag_ = iConfig.getUntrackedParameter<edm::InputTag>("trackCollection");
74     splitTrackCollection1Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection1");
75     splitTrackCollection2Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection2");
76 yygao 1.7 vertexCollectionTag_ = iConfig.getUntrackedParameter<edm::InputTag>("vertexCollection");
77 jengbou 1.13 splitVertexCollection1Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection1");
78 yygao 1.17 splitVertexCollection2Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection2");
79 jengbou 1.13 verbose_ = iConfig.getUntrackedParameter<bool>("verbose",false);
80     realData_ = iConfig.getUntrackedParameter<bool>("realData",false);
81     analyze_ = iConfig.getUntrackedParameter<bool>("analyzeOnTheFly",false);
82     saventuple_ = iConfig.getUntrackedParameter<bool>("saventuple",false);
83 yygao 1.19 outputfilename_ = iConfig.getUntrackedParameter<string>("OutputFileName");
84     histoFileName_ = iConfig.getUntrackedParameter<std::string> ("histoFileName");
85 jengbou 1.13 ntrkdiffcut_ = iConfig.getUntrackedParameter<double>("ntrkdiffcut");
86     nTrkMin_ = iConfig.getUntrackedParameter<int>("nTrkMin");
87     nTrkMax_ = iConfig.getUntrackedParameter<int>("nTrkMax");
88 yygao 1.17 zsigncut_ = iConfig.getUntrackedParameter<int>("zsigncut");
89     bsSrc = iConfig.getParameter< edm::InputTag >("beamSpot");
90    
91 jengbou 1.1 //now do what ever initialization is needed
92 jengbou 1.3 pvinfo.clear();
93 jengbou 1.1
94 yygao 1.7 // Specify the data mode vector
95     if(realData_) datamode.push_back(0);
96     else {
97     datamode.push_back(0);
98     datamode.push_back(1);
99     datamode.push_back(2);
100     datamode.push_back(3);
101     }
102 yygao 1.17 // Create ntuple files if needed
103 yygao 1.7 if(saventuple_) {
104     file_ = TFile::Open(outputfilename_.c_str(),"RECREATE");
105     ftree_ = new TTree("mytree","mytree");
106     ftree_->AutoSave();
107     ftree_->Branch("residual",&fres_,"fres_[3]/D");
108     ftree_->Branch("error",&ferror_,"ferror_[3]/D");
109     ftree_->Branch("nTrkPV",&fntrk_,"fntrk_/I");
110 yygao 1.9
111     // pvtxtree_ analyzing the pvtxs ootb
112     pvtxtree_ = new TTree("pvtxtree","pvtxtree");
113 yygao 1.19
114     // Event information for the data
115     pvtxtree_->Branch("glob_runno",&glob_runno_,"glob_runno/I");
116     pvtxtree_->Branch("glob_evtno",&glob_evtno_,"glob_evtno/I");
117     pvtxtree_->Branch("glob_ls",&glob_ls_,"glob_ls/I");
118     pvtxtree_->Branch("glob_bx",&glob_bx_,"glob_bx/I");
119    
120    
121 yygao 1.9 pvtxtree_->Branch("nrecPV",&nrecPV_,"nrecPV/I");
122 yygao 1.19 pvtxtree_->Branch("nrecPV1_spl",&nrecPV1_spl_,"nrecPV1_spl/I");
123     pvtxtree_->Branch("nrecPV2_spl",&nrecPV2_spl_,"nrecPV2_spl/I");
124     // Event level information
125 yygao 1.10 pvtxtree_->Branch("min_zsep",&min_zsep_,"min_zsep/D");
126     pvtxtree_->Branch("min_ntrksep",&min_ntrksep_,"min_ntrksep/D");
127     pvtxtree_->Branch("min_sumpt2sep",&min_sumpt2sep_,"min_sumpt2sep/D");
128    
129 yygao 1.19 //Fill the variables in the twovtx pair (recvtx1, recvtx2)
130     // Information related to the analyzing the two-vertex method
131    
132    
133 yygao 1.11
134 yygao 1.9 pvtxtree_->Branch("nrecPV_twovtx",&nrecPV_twovtx_,"nrecPV_twovtx/I");
135     pvtxtree_->Branch("nTrkPV_twovtx",&nTrkPV_twovtx_,"nTrkPV_twovtx[nrecPV_twovtx]/I");
136     pvtxtree_->Branch("deltax_twovtx",&deltax_twovtx_,"deltax_twovtx[nrecPV_twovtx]/D");
137     pvtxtree_->Branch("deltay_twovtx",&deltay_twovtx_,"deltay_twovtx[nrecPV_twovtx]/D");
138     pvtxtree_->Branch("deltaz_twovtx",&deltaz_twovtx_,"deltaz_twovtx[nrecPV_twovtx]/D");
139     pvtxtree_->Branch("errx_twovtx",&errx_twovtx_,"errx_twovtx[nrecPV_twovtx]/D");
140     pvtxtree_->Branch("erry_twovtx",&erry_twovtx_,"erry_twovtx[nrecPV_twovtx]/D");
141     pvtxtree_->Branch("errz_twovtx",&errz_twovtx_,"errz_twovtx[nrecPV_twovtx]/D");
142     pvtxtree_->Branch("pullx_twovtx",&pullx_twovtx_,"pullx_twovtx[nrecPV_twovtx]/D");
143     pvtxtree_->Branch("pully_twovtx",&pully_twovtx_,"pully_twovtx[nrecPV_twovtx]/D");
144     pvtxtree_->Branch("pullz_twovtx",&pullz_twovtx_,"pullz_twovtx[nrecPV_twovtx]/D");
145 yygao 1.19
146     // Information for the splitVertexColl1
147     pvtxtree_->Branch("nTrkPV1_spl_twovtx",&nTrkPV1_spl_twovtx_,"nTrkPV1_spl_twovtx[nrecPV_twovtx]/I");
148     pvtxtree_->Branch("ndofPV1_spl_twovtx",&ndofPV1_spl_twovtx_,"ndofPV1_spl_twovtx[nrecPV_twovtx]/D");
149     pvtxtree_->Branch("normchi2PV1_spl_twovtx",&normchi2PV1_spl_twovtx_,"normchi2PV1_spl_twovtx[nrecPV_twovtx]/D");
150     pvtxtree_->Branch("avgPtPV1_spl_twovtx",&avgPtPV1_spl_twovtx_,"avgPtPV1_spl_twovtx[nrecPV_twovtx]/D");
151     pvtxtree_->Branch("errx1_spl_twovtx",&errx1_spl_twovtx_,"errx1_spl_twovtx[nrecPV_twovtx]/D");
152     pvtxtree_->Branch("erry1_spl_twovtx",&erry1_spl_twovtx_,"erry1_spl_twovtx[nrecPV_twovtx]/D");
153     pvtxtree_->Branch("errz1_spl_twovtx",&errz1_spl_twovtx_,"errz1_spl_twovtx[nrecPV_twovtx]/D");
154    
155    
156     // Information for the splitVertexColl2
157     pvtxtree_->Branch("nTrkPV2_spl_twovtx",&nTrkPV2_spl_twovtx_,"nTrkPV2_spl_twovtx[nrecPV_twovtx]/I");
158     pvtxtree_->Branch("ndofPV2_spl_twovtx",&ndofPV2_spl_twovtx_,"ndofPV2_spl_twovtx[nrecPV_twovtx]/D");
159     pvtxtree_->Branch("normchi2PV2_spl_twovtx",&normchi2PV2_spl_twovtx_,"normchi2PV2_spl_twovtx[nrecPV_twovtx]/D");
160     pvtxtree_->Branch("avgPtPV2_spl_twovtx",&avgPtPV2_spl_twovtx_,"avgPtPV2_spl_twovtx[nrecPV_twovtx]/D");
161     pvtxtree_->Branch("errx2_spl_twovtx",&errx2_spl_twovtx_,"errx2_spl_twovtx[nrecPV_twovtx]/D");
162     pvtxtree_->Branch("erry2_spl_twovtx",&erry2_spl_twovtx_,"erry2_spl_twovtx[nrecPV_twovtx]/D");
163     pvtxtree_->Branch("errz2_spl_twovtx",&errz2_spl_twovtx_,"errz2_spl_twovtx[nrecPV_twovtx]/D");
164    
165 yygao 1.9
166     // MC variables
167     if(!realData_) {
168     pvtxtree_->Branch("nsimPV",&nsimPV_,"nsimPV/I");
169     pvtxtree_->Branch("nsimTrkPV",&nsimTrkPV_,"nsimTrkPV[nsimPV]/I");
170     pvtxtree_->Branch("simx",&simx_,"simx[nsimPV]/D");
171     pvtxtree_->Branch("simy",&simy_,"simy[nsimPV]/D");
172     pvtxtree_->Branch("simz",&simz_,"simz[nsimPV]/D");
173     pvtxtree_->Branch("simptsq",&simptsq_,"simptsq[nsimPV]/D");
174    
175     // For pvtxs with all the tracks
176     pvtxtree_->Branch("nrecPV_mct",&nrecPV_mct_,"nrecPV_mct/I");
177     pvtxtree_->Branch("deltax_mct",&deltax_mct_,"deltax_mct[nrecPV_mct]/D");
178     pvtxtree_->Branch("deltay_mct",&deltay_mct_,"deltay_mct[nrecPV_mct]/D");
179     pvtxtree_->Branch("deltaz_mct",&deltaz_mct_,"deltaz_mct[nrecPV_mct]/D");
180     pvtxtree_->Branch("pullx_mct",&pullx_mct_,"pullx_mct[nrecPV_mct]/D");
181     pvtxtree_->Branch("pully_mct",&pully_mct_,"pully_mct[nrecPV_mct]/D");
182 yygao 1.19 pvtxtree_->Branch("pullz_mct",&pullz_mct_,"pullz_mct[nrecPV_mct]/D");
183     pvtxtree_->Branch("errx_mct",&errx_mct_,"errx_mct[nrecPV_mct]/D");
184     pvtxtree_->Branch("erry_mct",&erry_mct_,"erry_mct[nrecPV_mct]/D");
185     pvtxtree_->Branch("errz_mct",&errz_mct_,"errz_mct[nrecPV_mct]/D");
186     pvtxtree_->Branch("nTrkPV_mct",&nTrkPV_mct_,"nTrkPV_mct[nrecPV_mct]/I");
187     pvtxtree_->Branch("ndofPV_mct",&ndofPV_mct_,"ndofPV_mct[nrecPV_mct]/D");
188     pvtxtree_->Branch("normchi2PV_mct",&normchi2PV_mct_,"normchi2PV_mct[nrecPV_mct]/D");
189     pvtxtree_->Branch("avgPtPV_mct",&avgPtPV_mct_,"avgPtPV_mct[nrecPV_mct]/D");
190    
191 yygao 1.9 // For pvtxs with splittracks1
192     pvtxtree_->Branch("nrecPV_spl1_mct",&nrecPV_spl1_mct_,"nrecPV_spl1_mct/I");
193     pvtxtree_->Branch("deltax_spl1_mct",&deltax_spl1_mct_,"deltax_spl1_mct[nrecPV_spl1_mct]/D");
194     pvtxtree_->Branch("deltay_spl1_mct",&deltay_spl1_mct_,"deltay_spl1_mct[nrecPV_spl1_mct]/D");
195     pvtxtree_->Branch("deltaz_spl1_mct",&deltaz_spl1_mct_,"deltaz_spl1_mct[nrecPV_spl1_mct]/D");
196     pvtxtree_->Branch("pullx_spl1_mct",&pullx_spl1_mct_,"pullx_spl1_mct[nrecPV_spl1_mct]/D");
197     pvtxtree_->Branch("pully_spl1_mct",&pully_spl1_mct_,"pully_spl1_mct[nrecPV_spl1_mct]/D");
198     pvtxtree_->Branch("pullz_spl1_mct",&pullz_spl1_mct_,"pullz_spl1_mct[nrecPV_spl1_mct]/D");
199 yygao 1.19 pvtxtree_->Branch("errx_spl1_mct",&errx_spl1_mct_,"errx_spl1_mct[nrecPV_spl1_mct]/D");
200     pvtxtree_->Branch("erry_spl1_mct",&erry_spl1_mct_,"erry_spl1_mct[nrecPV_spl1_mct]/D");
201     pvtxtree_->Branch("errz_spl1_mct",&errz_spl1_mct_,"errz_spl1_mct[nrecPV_spl1_mct]/D");
202     pvtxtree_->Branch("nTrkPV_spl1_mct",&nTrkPV_spl1_mct_,"nTrkPV_spl1_mct[nrecPV_spl1_mct]/I");
203     pvtxtree_->Branch("ndofPV_spl1_mct",&ndofPV_spl1_mct_,"ndofPV_spl1_mct[nrecPV_spl1_mct]/D");
204     pvtxtree_->Branch("normchi2PV_spl1_mct",&normchi2PV_spl1_mct_,"normchi2PV_spl1_mct[nrecPV_spl1_mct]/D");
205     pvtxtree_->Branch("avgPtPV_spl1_mct",&avgPtPV_spl1_mct_,"avgPtPV_spl1_mct[nrecPV_spl1_mct]/D");
206    
207 yygao 1.9 // For pvtxs with splittracks1
208     pvtxtree_->Branch("nrecPV_spl2_mct",&nrecPV_spl2_mct_,"nrecPV_spl2_mct/I");
209     pvtxtree_->Branch("deltax_spl2_mct",&deltax_spl2_mct_,"deltax_spl2_mct[nrecPV_spl2_mct]/D");
210     pvtxtree_->Branch("deltay_spl2_mct",&deltay_spl2_mct_,"deltay_spl2_mct[nrecPV_spl2_mct]/D");
211     pvtxtree_->Branch("deltaz_spl2_mct",&deltaz_spl2_mct_,"deltaz_spl2_mct[nrecPV_spl2_mct]/D");
212     pvtxtree_->Branch("pullx_spl2_mct",&pullx_spl2_mct_,"pullx_spl2_mct[nrecPV_spl2_mct]/D");
213     pvtxtree_->Branch("pully_spl2_mct",&pully_spl2_mct_,"pully_spl2_mct[nrecPV_spl2_mct]/D");
214 yygao 1.19 pvtxtree_->Branch("pullz_spl2_mct",&pullz_spl2_mct_,"pullz_spl2_mct[nrecPV_spl2_mct]/D");
215     pvtxtree_->Branch("errx_spl2_mct",&errx_spl2_mct_,"errx_spl2_mct[nrecPV_spl2_mct]/D");
216     pvtxtree_->Branch("erry_spl2_mct",&erry_spl2_mct_,"erry_spl2_mct[nrecPV_spl2_mct]/D");
217     pvtxtree_->Branch("errz_spl2_mct",&errz_spl2_mct_,"errz_spl2_mct[nrecPV_spl2_mct]/D");
218     pvtxtree_->Branch("nTrkPV_spl2_mct",&nTrkPV_spl2_mct_,"nTrkPV_spl2_mct[nrecPV_spl2_mct]/I");
219     pvtxtree_->Branch("ndofPV_spl2_mct",&ndofPV_spl2_mct_,"ndofPV_spl2_mct[nrecPV_spl2_mct]/D");
220     pvtxtree_->Branch("normchi2PV_spl2_mct",&normchi2PV_spl2_mct_,"normchi2PV_spl2_mct[nrecPV_spl2_mct]/D");
221     pvtxtree_->Branch("avgPtPV_spl2_mct",&avgPtPV_spl2_mct_,"avgPtPV_spl2_mct[nrecPV_spl2_mct]/D");
222    
223 yygao 1.7 }
224     }
225 yygao 1.17
226 jengbou 1.4 setRootStyle();
227 yygao 1.10
228 yygao 1.17 //========================================
229     // Booking histograms
230     //========================================
231    
232 jengbou 1.13 // Create a root file for histograms
233     theFile = new TFile(histoFileName_.c_str(), "RECREATE");
234     // make diretories
235     theFile->mkdir("Summary");
236     theFile->cd();
237     theFile->mkdir("Others");
238     theFile->cd();
239 jengbou 1.15 if (analyze_) {
240     theFile->mkdir("Results");
241     theFile->cd();
242     }
243 jengbou 1.13
244     // Book Histograms:
245     h_pvtrk = new PVHistograms();
246     h_misc = new PVHistograms();
247     h_summary = new PVHistograms();
248     h_others = new PVHistograms();
249    
250     for(int i=0;i<3;i++) {
251     if(i == 0) h_pvtrk->Init("pvTrk");
252     else {
253     stringstream ss;
254     ss << i;
255 yygao 1.17 h_pvtrk->Init("pvTrk", ss.str(),"spl");
256 yygao 1.7 }
257 jengbou 1.13 }
258     h_misc->Init("misc");
259    
260     // Book MC only plots
261     if (!realData_) {
262     h_gen = new PVHistograms();
263     h_gen->Init("generator");
264     }
265 jengbou 1.14 if (analyze_) h_ana = new PVHistograms();
266 yygao 1.10
267 yygao 1.7 //Book histograms sensitive to data/mc
268     for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {
269     string suffix;
270 jengbou 1.14 edm::LogInfo("Debug")<<"datamode = "<< *it<<endl;
271 yygao 1.7 switch(*it) {
272     case 0: suffix = "";
273     break;
274     case 1: suffix = "_spl1_mct";
275     break;
276     case 2: suffix = "_spl2_mct";
277     break;
278     case 3: suffix = "_mct";
279     break;
280 jengbou 1.2 }
281 jengbou 1.13 h_summary->Init("summary",suffix);
282 yygao 1.7
283     // Book residual, error and pull histograms for each nTrk bin
284 jengbou 1.1 for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
285     stringstream ss;
286     ss << ntrk;
287 jengbou 1.13 h_others->Init("others",suffix,ss.str());
288     }
289    
290 yygao 1.7 // Book residual and pull histograms only when analyzeOntheFly is enabled
291 jengbou 1.14 if(analyze_) h_ana->InitA("analysis",suffix,"nTrk",nTrkMin_,nTrkMax_);
292 yygao 1.7 suffix.clear();
293 yygao 1.10 } // End of Book histograms sensitive to data/mc
294 yygao 1.17
295 yygao 1.11
296 jengbou 1.1 }
297    
298     PVStudy::~PVStudy()
299     {
300 yygao 1.17
301     // do anything here that needs to be done at desctruction time
302     // (e.g. close files, deallocate resources etc.)
303 jengbou 1.13 theFile->cd();
304     theFile->cd("Summary");
305     h_pvtrk->Save();
306     h_misc->Save();
307     h_summary->Save();
308 jengbou 1.15 if (!realData_)
309     h_gen->Save();
310     if (analyze_) {
311     theFile->cd();
312     theFile->cd("Results");
313 jengbou 1.13 h_ana->Save();
314 jengbou 1.15 }
315 jengbou 1.13 theFile->cd();
316     theFile->cd("Others");
317     h_others->Save();
318 jengbou 1.1
319 jengbou 1.13 theFile->Close();
320 jengbou 1.1 }
321    
322 jengbou 1.4 void PVStudy::setRootStyle() {
323     //
324     gROOT->SetStyle("Plain");
325     gStyle->SetFillColor(1);
326     gStyle->SetOptDate();
327     // gStyle->SetOptStat(1111110);
328     // gStyle->SetOptFit(0111);
329     // gStyle->SetPadTickX(1);
330     // gStyle->SetPadTickY(1);
331     gStyle->SetMarkerSize(0.5);
332     gStyle->SetMarkerStyle(8);
333     gStyle->SetGridStyle(3);
334     //gStyle->SetPadGridX(1);
335     //gStyle->SetPadGridY(1);
336     gStyle->SetPaperSize(TStyle::kA4);
337     gStyle->SetStatW(0.25); // width of statistics box; default is 0.19
338     // gStyle->SetStatH(0.10); // height of statistics box; default is 0.1
339     gStyle->SetStatFormat("6.4g"); // leave default format for now
340     gStyle->SetTitleSize(0.055, ""); // size for pad title; default is 0.02
341     gStyle->SetLabelSize(0.03, "XYZ"); // size for axis labels; default is 0.04
342     gStyle->SetStatFontSize(0.08); // size for stat. box
343 yygao 1.19 gStyle->SetTitleFont(42, "XYZ"); // times-bold-italic font (p. 153) for axes
344     gStyle->SetTitleFont(42, ""); // same for pad title
345     gStyle->SetLabelFont(42, "XYZ"); // same for axis labels
346     gStyle->SetStatFont(42); // same for stat. box
347 jengbou 1.4 gStyle->SetLabelOffset(0.006, "Y"); // default is 0.005
348     //
349     return;
350     }
351 jengbou 1.1 //
352     // member functions
353     //
354     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC, std::string suffix="")
355     {
356 yygao 1.17 std::vector<PVStudy::simPrimaryVertex> simpv;
357 jengbou 1.1 const HepMC::GenEvent* evt=evtMC->GetEvent();
358     if (evt) {
359 jengbou 1.14 edm::LogInfo("SimPVs") << "[getSimPVs] process id " << evt->signal_process_id()<<endl;
360     edm::LogInfo("SimPVs") << "[getSimPVs] signal process vertex " << ( evt->signal_process_vertex() ?
361     evt->signal_process_vertex()->barcode() : 0 ) <<endl;
362     edm::LogInfo("SimPVs") << "[getSimPVs] number of vertices " << evt->vertices_size() << endl;
363 jengbou 1.1
364 jengbou 1.6 int idx=0; int npv=0;
365 jengbou 1.1 for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
366 jengbou 1.6 vitr != evt->vertices_end(); ++vitr ) { // loop for vertex ...
367     HepMC::FourVector pos = (*vitr)->position();
368     //HepLorentzVector pos = (*vitr)->position();
369    
370     // t component of PV:
371     for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
372     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
373 jengbou 1.14 // edm::LogInfo("SimPVs") << "Status = " << (*mother)->status() << endl;
374 jengbou 1.6 HepMC::GenVertex * mv=(*mother)->production_vertex();
375     if( ((*mother)->status() == 3) && (!mv)) {
376 jengbou 1.14 // edm::LogInfo("SimPVs") << "npv= " << npv << endl;
377 jengbou 1.6 if (npv == 0) {
378 jengbou 1.13 h_gen->Fill1d("genPart_cT", pos.t()); // mm
379     h_gen->Fill1d("genPart_T", pos.t()/299.792458); // ns
380 jengbou 1.6 }
381     npv++;
382     }
383     }
384     // if (pos.t()>0) { continue;} // for 22X when t of PV was not smeared
385 jengbou 1.1
386 jengbou 1.6 bool hasMotherVertex=false;
387 jengbou 1.14 if (verbose_) cout << "[getSimPVs] mothers of vertex[" << ++idx << "]: " << endl;
388 jengbou 1.6 for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
389     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
390     HepMC::GenVertex * mv=(*mother)->production_vertex();
391 jengbou 1.14 // if (verbose_) cout << "Status = " << (*mother)->status() << endl;
392 jengbou 1.6 if (mv) {
393     hasMotherVertex=true;
394     if(!verbose_) break; //if verbose_, print all particles of gen vertices
395     }
396     if(verbose_) {
397     cout << "\t";
398     (*mother)->print();
399     }
400     }
401    
402     if(hasMotherVertex) continue;
403    
404     // could be a new vertex, check all primaries found so far to avoid multiple entries
405     const double mm2cm=0.1;
406     simPrimaryVertex sv(pos.x()*mm2cm,pos.y()*mm2cm,pos.z()*mm2cm); // sim unit mm, rec unit cm
407     simPrimaryVertex *vp=NULL; // will become non-NULL if a vertex is found and then point to it
408     for(vector<simPrimaryVertex>::iterator v0=simpv.begin();
409     v0!=simpv.end(); v0++){
410     if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
411     vp=&(*v0);
412     break;
413 jengbou 1.1 }
414 jengbou 1.6 }
415 jengbou 1.1
416 jengbou 1.6 if(!vp){
417     // this is a new vertex
418 jengbou 1.14 edm::LogInfo("SimPVs") << "[getSimPVs] this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
419 jengbou 1.6 simpv.push_back(sv);
420     vp=&simpv.back();
421     }else{
422 jengbou 1.14 edm::LogInfo("SimPVs") << "[getSimPVs] this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
423 jengbou 1.6 }
424     vp->genVertex.push_back((*vitr)->barcode());
425     // collect final state descendants
426     for ( HepMC::GenVertex::particle_iterator daughter = (*vitr)->particles_begin(HepMC::descendants);
427     daughter != (*vitr)->particles_end(HepMC::descendants);
428     ++daughter ) {
429     if (isFinalstateParticle(*daughter)){
430     if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
431     == vp->finalstateParticles.end()){
432     vp->finalstateParticles.push_back((*daughter)->barcode());
433     HepMC::FourVector m=(*daughter)->momentum();
434     // the next four lines used to be "vp->ptot+=m;" in the days of CLHEP::HepLorentzVector
435     // but adding FourVectors seems not to be foreseen
436     vp->ptot.setPx(vp->ptot.px()+m.px());
437     vp->ptot.setPy(vp->ptot.py()+m.py());
438     vp->ptot.setPz(vp->ptot.pz()+m.pz());
439     vp->ptot.setE(vp->ptot.e()+m.e());
440     vp->ptsq+=(m.perp())*(m.perp());
441     if ( (m.perp()>0.8) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
442     vp->nGenTrk++;
443 jengbou 1.1 }
444     }
445     }
446 jengbou 1.13 }//loop MC vertices daughters
447     }//loop MC vertices
448 jengbou 1.1 }
449     return simpv;
450     }
451    
452     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC,
453     const Handle<SimVertexContainer> simVtxs,
454     const Handle<SimTrackContainer> simTrks)
455     {
456     // simvertices don't have enough information to decide,
457     // genVertices don't have the simulated coordinates ( with VtxSmeared they might)
458     // go through simtracks to get the link between simulated and generated vertices
459     std::vector<PVStudy::simPrimaryVertex> simpv;
460     int idx=0;
461     for(SimTrackContainer::const_iterator t=simTrks->begin();
462     t!=simTrks->end(); ++t){
463     if ( !(t->noVertex()) && !(t->type()==-99) ){
464     double ptsq=0;
465     bool primary=false; // something coming directly from the primary vertex
466     bool resonance=false; // resonance
467     bool track=false; // undecayed, charged particle
468     HepMC::GenParticle* gp=evtMC->GetEvent()->barcode_to_particle( (*t).genpartIndex() );
469     if (gp) {
470     HepMC::GenVertex * gv=gp->production_vertex();
471     if (gv) {
472     for ( HepMC::GenVertex::particle_iterator
473     daughter = gv->particles_begin(HepMC::descendants);
474     daughter != gv->particles_end(HepMC::descendants);
475     ++daughter ) {
476     if (isFinalstateParticle(*daughter)){
477     ptsq+=(*daughter)->momentum().perp()*(*daughter)->momentum().perp();
478     }
479     }
480 jengbou 1.13 //primary = ( gv->position().t()==0 );
481     primary = true;
482     h_gen->Fill1d("genPart_cT", gv->position().t()); // mm
483     h_gen->Fill1d("genPart_T", gv->position().t()/299.792458); // ns
484    
485 jengbou 1.1 //resonance= ( gp->mother() && isResonance(gp->mother())); // in CLHEP/HepMC days
486     // no more mother pointer in the improved HepMC GenParticle
487     resonance= ( isResonance(*(gp->production_vertex()->particles_in_const_begin())));
488     if (gp->status()==1){
489     //track=((pdt->particle(gp->pdg_id()))->charge() != 0);
490     track=not isCharged(gp);
491     }
492     }
493     }
494    
495     const HepMC::FourVector & v=(*simVtxs)[t->vertIndex()].position();
496     //const HepLorentzVector & v=(*simVtxs)[t->vertIndex()].position();
497     if(primary or resonance){
498     {
499     // check all primaries found so far to avoid multiple entries
500     bool newVertex=true;
501     for(std::vector<PVStudy::simPrimaryVertex>::iterator v0=simpv.begin();
502     v0!=simpv.end(); v0++){
503     if( (fabs(v0->x-v.x())<0.001) && (fabs(v0->y-v.y())<0.001) && (fabs(v0->z-v.z())<0.001) ){
504     if (track) {
505     v0->simTrackIndex.push_back(idx);
506     if (ptsq>(*v0).ptsq){(*v0).ptsq=ptsq;}
507     }
508     newVertex=false;
509     }
510     }
511     if(newVertex && !resonance){
512     PVStudy::simPrimaryVertex anotherVertex(v.x(),v.y(),v.z());
513     if (track) anotherVertex.simTrackIndex.push_back(idx);
514     anotherVertex.ptsq=ptsq;
515     simpv.push_back(anotherVertex);
516     }
517     }//
518     }
519    
520     }// simtrack has vertex and valid type
521     idx++;
522     }//simTrack loop
523     return simpv;
524     }
525    
526     // ------------ method called to for each event ------------
527     void
528     PVStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
529     {
530     using namespace edm;
531 yygao 1.7 using namespace reco;
532    
533 yygao 1.19 //========================================================================
534     // Step 0: Prepare root variables and get information from the Event
535     //========================================================================
536    
537 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy]"<<endl;
538 yygao 1.7 // Initialize Root-tuple variables if needed
539 yygao 1.19 SetVarToZero();
540    
541     // ====== TrackCollection
542 yygao 1.7 static const reco::TrackCollection s_empty_trackColl;
543     const reco::TrackCollection *trackColl = &s_empty_trackColl;
544     edm::Handle<reco::TrackCollection> trackCollectionHandle;
545     iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle);
546     if( iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle)) {
547     trackColl = trackCollectionHandle.product();
548     } else {
549 jengbou 1.14 edm::LogInfo("Debug") << "[PVStudy] trackCollection cannot be found -> using empty collection of same type." <<endl;
550 yygao 1.7 }
551 yygao 1.19 // ====== splitTrackCollection1
552 yygao 1.7 static const reco::TrackCollection s_empty_splitTrackColl1;
553     const reco::TrackCollection *splitTrackColl1 = &s_empty_splitTrackColl1;
554     edm::Handle<reco::TrackCollection> splitTrackCollection1Handle;
555     iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle);
556     if( iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle)) {
557     splitTrackColl1 = splitTrackCollection1Handle.product();
558     } else {
559 jengbou 1.14 edm::LogInfo("Debug") << "[PVStudy] splitTrackCollection1 cannot be found -> using empty collection of same type." <<endl;
560 yygao 1.7 }
561 yygao 1.19 // ====== splitTrackCollection2
562 yygao 1.7 static const reco::TrackCollection s_empty_splitTrackColl2;
563     const reco::TrackCollection *splitTrackColl2 = &s_empty_splitTrackColl2;
564     edm::Handle<reco::TrackCollection> splitTrackCollection2Handle;
565     iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle);
566     if( iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle)) {
567     splitTrackColl2 = splitTrackCollection2Handle.product();
568     } else {
569 yygao 1.17 edm::LogInfo("Debug") << "[PVStudy] splitTrackCollection2 cannot be found -> using empty collection of same type." <<endl;
570 yygao 1.7 }
571 yygao 1.17
572 yygao 1.19 // ======= PrimaryVertexCollection
573 yygao 1.7 static const reco::VertexCollection s_empty_vertexColl;
574     const reco::VertexCollection *vertexColl = &s_empty_vertexColl;
575     edm::Handle<reco::VertexCollection> vertexCollectionHandle;
576     iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle);
577     if( iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle)) {
578     vertexColl = vertexCollectionHandle.product();
579     } else {
580 yygao 1.17 edm::LogInfo("Debug") << "[PVStudy] vertexCollection cannot be found -> using empty collection of same type." <<endl;
581 yygao 1.7 }
582 yygao 1.19 // ====== splitVertexCollection1
583 yygao 1.7 static const reco::VertexCollection s_empty_splitVertexColl1;
584     const reco::VertexCollection *splitVertexColl1 = &s_empty_splitVertexColl1;
585     edm::Handle<reco::VertexCollection> splitVertexCollection1Handle;
586     iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle);
587     if( iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle)) {
588     splitVertexColl1 = splitVertexCollection1Handle.product();
589     } else {
590 jengbou 1.14 edm::LogInfo("Debug") << "[PVStudy] splitVertexCollection1 cannot be found -> using empty collection of same type." <<endl;
591 yygao 1.17 }
592 yygao 1.19 // ====== splitVertexCollection2
593 yygao 1.7 static const reco::VertexCollection s_empty_splitVertexColl2;
594     const reco::VertexCollection *splitVertexColl2 = &s_empty_splitVertexColl2;
595     edm::Handle<reco::VertexCollection> splitVertexCollection2Handle;
596     iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle);
597     if( iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle)) {
598     splitVertexColl2 = splitVertexCollection2Handle.product();
599     } else {
600 jengbou 1.14 edm::LogInfo("Debug") << "[PVStudy] splitVertexCollection2 cannot be found -> using empty collection of same type." <<endl;
601 yygao 1.7 }
602 yygao 1.17
603 yygao 1.10
604 yygao 1.19 // ======== BeamSpot accessors
605 yygao 1.17 edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
606     iEvent.getByLabel(bsSrc,recoBeamSpotHandle);
607     reco::BeamSpot bs = *recoBeamSpotHandle;
608     const Point beamSpot = recoBeamSpotHandle.isValid() ? Point(recoBeamSpotHandle->x0(), recoBeamSpotHandle->y0(), recoBeamSpotHandle->z0()) : Point(0, 0, 0);
609    
610 yygao 1.19 edm::LogInfo("Debug")<<"[PVStudy] End accessing the track, beamSpot, primary vertex collections"<<endl;
611 yygao 1.7
612 yygao 1.19 // ========== MC simvtx accessor
613 jengbou 1.1 if (!realData_) {
614 yygao 1.7 edm::Handle<SimVertexContainer> simVtxs;
615 jengbou 1.1 iEvent.getByLabel( simG4_, simVtxs);
616    
617 yygao 1.7 edm::Handle<SimTrackContainer> simTrks;
618 jengbou 1.1 iEvent.getByLabel( simG4_, simTrks);
619     }
620    
621 yygao 1.19 // ========== GET PDT
622 jengbou 1.1 try{
623     iSetup.getData(pdt);
624     }catch(const Exception&){
625 jengbou 1.14 edm::LogInfo("Debug") << "[PVStudy] Some problem occurred with the particle data table. This may not work !." <<endl;
626 jengbou 1.1 }
627 yygao 1.19
628     // ======= Get MC information if needed
629     bool MC=false;
630     Handle<HepMCProduct> evtMC;
631     if (!realData_) {
632     iEvent.getByLabel("generator",evtMC);
633     if (!evtMC.isValid()) {
634     MC=false;
635     edm::LogInfo("Debug") << "[PVStudy] no HepMCProduct found"<< endl;
636     } else {
637     edm::LogInfo("Debug") << "[PVStudy] generator HepMCProduct found"<< endl;
638     MC=true;
639     }
640     }
641 yygao 1.17
642 yygao 1.19 //========================================================================
643     // Step 1: Apply event cleaning for data and MC
644     // WARNING: event selection cut are hard coded!!
645     //========================================================================
646     // =====Loop over the trackColl to get the fraction of HighPurity tracks
647 jengbou 1.18 int nTracks = 0;
648     int nHighPurityTracks=0;
649     double fHighPurity=0;
650    
651     for (unsigned int i=0; i<trackCollectionHandle->size(); i++, nTracks++) {
652     TrackRef tkref(trackCollectionHandle,i);
653     if(tkref->quality(reco::TrackBase::highPurity)) ++nHighPurityTracks;
654     }
655     if(nTracks>0)
656     fHighPurity = double(nHighPurityTracks)/double(nTracks);
657     if(verbose_)
658     cout<<"fraction of HighPurity track is "<<fHighPurity<<endl;
659 yygao 1.17
660 jengbou 1.18 if( (fHighPurity<0.2 && nTracks>10) || vertexColl->begin()->isFake()) {
661 yygao 1.19 glob_runno_ = iEvent.id().run();
662     glob_evtno_ = iEvent.id().event();
663     glob_ls_ = iEvent.luminosityBlock();
664     glob_bx_ = iEvent.bunchCrossing();
665 jengbou 1.18 return;
666     }
667 yygao 1.17
668 yygao 1.19 //========================================================================
669     // Step 2: Fill histograms for the splitting consistency checks
670     //========================================================================
671    
672     // === Fill trackparameters of the input tracks to pvtx fitter
673 yygao 1.17 edm::LogInfo("Debug")<<"[PVStudy] Start filling track parameters of the input tracks to pvtx fitter."<<endl;
674     //fillTrackHisto(const reco::TrackCollection *trackColl, int datatype, const Point & bs)
675     // datatype: unsplittracks (0); splittracks1 (1); splittracks2 (2);
676     fillTrackHisto(trackColl, 0, beamSpot);
677     fillTrackHisto(splitTrackColl1, 1, beamSpot);
678     fillTrackHisto(splitTrackColl2, 2, beamSpot);
679     edm::LogInfo("Debug")<<"[PVStudy] End filling track parameters of the input tracks to pvtx fitter."<<endl;
680 yygao 1.19
681    
682     // ==== Fill number of reconstructed vertices
683 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] Printing vertexCollection: "<<endl;
684     edm::LogInfo("Debug")<<"[PVStudy] Printing splitVertexCollection1: "<<endl;
685     edm::LogInfo("Debug")<<"[PVStudy] Printing splitVertexCollection2: "<<endl;
686     edm::LogInfo("Debug")<<"[PVStudy] Start filling pvtx parameters."<<endl;
687     if (verbose_) {
688 yygao 1.7 printRecVtxs(vertexCollectionHandle);
689     printRecVtxs(splitVertexCollection1Handle);
690     printRecVtxs(splitVertexCollection2Handle);
691     }
692 yygao 1.19
693 yygao 1.9 nrecPV_ = int (vertexColl->size());
694     nrecPV1_spl_ = int (splitVertexColl1->size());
695     nrecPV2_spl_ = int (splitVertexColl2->size());
696 jengbou 1.8
697 jengbou 1.13 h_pvtrk->Fill1d("nrecPV", nrecPV_);
698     h_pvtrk->Fill1d("nrecPV1_spl", nrecPV1_spl_);
699     h_pvtrk->Fill1d("nrecPV2_spl", nrecPV2_spl_);
700     h_misc->Fill1d("nrecPVDiff", double(nrecPV1_spl_)-double(nrecPV2_spl_));
701 yygao 1.11
702 yygao 1.17
703 yygao 1.19 // ======= Fill track parameter ntuple/hist for tracks used in recoVertices
704     //fillTrackHistoInPV(const reco::VertexCollection *vertexColl, int datatype, const Point & bs) {
705 yygao 1.10 if(vertexColl->size() > 0 && vertexColl->begin()->isValid() && !(vertexColl->begin()->isFake()))
706 yygao 1.19 fillTrackHistoInPV(vertexColl, 0, beamSpot);
707 yygao 1.10
708 yygao 1.19 // ======= Fill secondary/primary min separations for multi-vertices
709 yygao 1.10 if(vertexColl->size()>1 && vertexColl->begin()->isValid() && !(vertexColl->begin()->isFake()) ) {
710     double min_xsep = 9999.0;
711     double min_ysep = 9999.0;
712     double min_zsep = 9999.0;
713     double min_xsep_sign = 9999.0;
714     double min_ysep_sign = 9999.0;
715     double min_zsep_sign = 9999.0;
716     double min_ntrksep = 9999.0;
717     double min_sumpt2sep = 9999999.0;
718 yygao 1.9
719 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] leading pvtx z = "<< vertexColl->begin()->z() <<endl;
720 yygao 1.10
721     // Looping through the secondary vertices to find the mininum separation to the primary
722     for(reco::VertexCollection::const_iterator v=vertexColl->begin() + 1 ;
723     v!=vertexColl->end(); ++v) {
724     if(v->isValid() && ! v->isFake()) {
725     // xsep
726     if(fabs(v->x()- vertexColl->begin()->x())<min_xsep)
727     min_xsep = fabs(v->x()- vertexColl->begin()->x());
728     // ysep
729     if(fabs(v->y()- vertexColl->begin()->y())<min_ysep)
730     min_ysep = fabs(v->y()- vertexColl->begin()->y());
731     // zsep
732     if(fabs(v->z()- vertexColl->begin()->z())<min_zsep)
733     min_zsep = fabs(v->z()- vertexColl->begin()->z());
734     // xsep_sign
735     double xsep_sign = fabs(v->x()- vertexColl->begin()->x())/max(v->xError(), vertexColl->begin()->xError());
736     if(xsep_sign < min_xsep_sign)
737     min_xsep_sign = xsep_sign;
738     // ysep_sign
739     double ysep_sign = fabs(v->y()- vertexColl->begin()->y())/max(v->yError(), vertexColl->begin()->yError());
740     if(ysep_sign < min_ysep_sign)
741     min_ysep_sign = ysep_sign;
742     // zsep_sign
743     double zsep_sign = fabs(v->z()- vertexColl->begin()->z())/max(v->zError(), vertexColl->begin()->zError());
744     if(zsep_sign < min_zsep_sign)
745     min_zsep_sign = zsep_sign;
746     // ntrksep
747     if( (double(vertexColl->begin()->tracksSize()) - double(v->tracksSize())) < min_ntrksep)
748     min_ntrksep = double(vertexColl->begin()->tracksSize()) - double(v->tracksSize());
749     // sumpt2sep
750     if(fabs(sumPtSquared(*v) - sumPtSquared(*(vertexColl->begin()))) < min_sumpt2sep)
751     min_sumpt2sep = fabs(sumPtSquared(*v) - sumPtSquared(*(vertexColl->begin())));
752     }
753     }
754 jengbou 1.13 h_misc->Fill1d("min_xsep", min_xsep);
755     h_misc->Fill1d("min_ysep", min_ysep);
756     h_misc->Fill1d("min_zsep", min_zsep);
757     h_misc->Fill1d("min_xsep_sign", min_xsep_sign);
758     h_misc->Fill1d("min_ysep_sign", min_ysep_sign);
759     h_misc->Fill1d("min_zsep_sign", min_zsep_sign);
760     h_misc->Fill1d("min_ntrksep", min_ntrksep);
761     h_misc->Fill1d("min_sumpt2sep", min_sumpt2sep);
762 yygao 1.10
763     min_zsep_ = min_zsep;
764     min_ntrksep_ = min_ntrksep;
765     min_sumpt2sep_ = min_sumpt2sep;
766     } // End of if(vertexColl->size()>1) {
767    
768 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] End filling pvtx parameters."<<endl;
769 yygao 1.17
770 yygao 1.19 //========================================================================
771     // Step 3: PrimaryVertex Matching
772     // 1. In z |z1-z2|< zsigncut * max(sigmaz1, sigmaz2)
773     // 2. |(nTrkPV1 - nTrkPV2)/(nTrkPV1+nTrkPV2)|<ntrkdiffcut_
774     // 3. The first match is the primary vertex, which has largest sum(pT^2)
775     //========================================================================
776 yygao 1.17
777 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] matching pvtxs "<<endl;
778 yygao 1.9 reco::VertexCollection s_empty_matchedVertexColl1;
779     reco::VertexCollection *matchedVertexColl1 = &s_empty_matchedVertexColl1;
780     matchedVertexColl1->reserve(splitVertexColl1->size());
781     reco::VertexCollection s_empty_matchedVertexColl2;
782     reco::VertexCollection *matchedVertexColl2 = &s_empty_matchedVertexColl2;
783     matchedVertexColl2->reserve(splitVertexColl2->size());
784 yygao 1.7
785 yygao 1.10 bool stopmatching = false;
786     for (size_t ivtx = 0; ivtx<splitVertexCollection1Handle->size() && !stopmatching; ++ivtx) {
787 yygao 1.9 reco::VertexRef recvtx1(splitVertexCollection1Handle, ivtx);
788 yygao 1.10 if( !(recvtx1->isValid()) || recvtx1->isFake()) break;
789     for (size_t jvtx = ivtx; jvtx < splitVertexCollection2Handle->size(); ++jvtx) {
790     //------------------------------------------------------------------------
791     //== If only considering matching the first vertex of the splitVertexColl
792     //------------------------------------------------------------------------
793     if (ivtx!=0 || jvtx!=0) { stopmatching = true; break; }
794 yygao 1.9 reco::VertexRef recvtx2(splitVertexCollection2Handle, jvtx);
795 yygao 1.10 if( !(recvtx2->isValid()) || recvtx2->isFake()) break;
796 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] Matching splitVertexColl1: # "<< ivtx<< " and splitVertexColl1: # "<<jvtx<<endl;
797     edm::LogInfo("Debug")<<"[PVStudy] recvtx1->z() = " << recvtx1->z() << "+/- "<< recvtx1->zError() << "." << endl;
798     edm::LogInfo("Debug")<<"[PVStudy] recvtx2->z() = " << recvtx2->z() << "+/- "<< recvtx2->zError() << "." << endl;
799 yygao 1.17
800     double twovtxsig = (recvtx1->z()-recvtx2->z())/max(recvtx1->zError(), recvtx2->zError());
801     h_misc->Fill1d("twovtxzsign", twovtxsig);
802 yygao 1.9 if(matchVertex(recvtx1, recvtx2, zsigncut_)) {
803 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] The two splitted vertices match in Z. "<<endl;
804    
805 yygao 1.9 int nTrkPV1 = recvtx1->tracksSize();
806     int nTrkPV2 = recvtx2->tracksSize();
807     double ntrkreldiff = double(nTrkPV1-nTrkPV2)/double(nTrkPV1+nTrkPV2);
808 jengbou 1.13 h_misc->Fill1d("nTrkPVDiff", nTrkPV1-nTrkPV2);
809     h_misc->Fill1d("nTrkPVRelDiff", ntrkreldiff);
810 yygao 1.9 if(fabs(ntrkreldiff)<ntrkdiffcut_) {
811 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] (nTrkPV1-nTrkPV2)/(nTrkPV1+nTrkPV2) = " << ntrkreldiff<<endl;
812    
813 yygao 1.9 matchedVertexColl1->push_back(*recvtx1);
814     matchedVertexColl2->push_back(*recvtx2);
815 yygao 1.10 stopmatching = true; // stop the matching when the first match is found!
816     break;
817 yygao 1.7 }
818 yygao 1.9 else
819 jengbou 1.14 edm::LogInfo("Debug")<<"WARNING: (nTrkPV1-nTrkPV2)/(nTrkPV1+nTrkPV2) = " << ntrkreldiff<<", exceeding cut "<<ntrkdiffcut_<<endl;
820 yygao 1.7 }
821 yygao 1.9 else {
822 jengbou 1.14 edm::LogInfo("Debug")<<"WARNING: The two reconstructed vertices do not match in z: "<<endl;
823     edm::LogInfo("Debug")<<"recvtx1->z() = " << recvtx1->z() << "+/- "<< recvtx1->zError() << "." << endl;
824     edm::LogInfo("Debug")<<"recvtx2->z() = " << recvtx2->z() << "+/- "<< recvtx2->zError() << "." << endl;
825 yygao 1.7 }
826     }
827 yygao 1.9 }
828 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] End matching pvtxs"<<endl;
829 yygao 1.19
830     //========================================================================
831     // Step 4: Analyze matchedVertexColls
832     //========================================================================
833     edm::LogInfo("Debug")<<"[PVStudy] Begin analyzing the matchedVertexColl with size = "<< matchedVertexColl1->size()<< endl;
834 yygao 1.9
835 yygao 1.19 // ==== If it is MC, analyze the res/pull of the unsplit vertexColl first
836     if(MC){
837     // make a list of primary vertices:
838     std::vector<simPrimaryVertex> simpv;
839     simpv=getSimPVs(evtMC,"");
840     // simpv=getSimPVs(evtMC, simVtxs, simTrks);
841     h_gen->Fill1d("nsimPV", simpv.size());
842     nsimPV_ = simpv.size();
843     int isimpv = 0;
844     for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
845     vsim!=simpv.end(); vsim++, isimpv++){
846     nsimTrkPV_[isimpv] =vsim->nGenTrk;
847     simx_[isimpv] = vsim->x;
848     simy_[isimpv] = vsim->y;
849     simz_[isimpv] = vsim->y;
850     simptsq_[isimpv] = vsim->ptsq;
851     //fillMCHisto((std::vector<simPrimaryVertex>::iterator vsim, int isimpv, const reco::VertexCollection *vtxColl, int datatype) {
852     fillMCHisto(vsim, isimpv, vertexColl, 3);
853     }
854     }
855 yygao 1.9
856     // Compare the reconstructed vertex position and calculate resolution/pulls
857     if(matchedVertexColl1->size() != 0 && matchedVertexColl2->size() != 0) {
858 yygao 1.19 // ==== Analyze the splitted vtx using MC method
859     if(MC){
860     // make a list of primary vertices:
861     std::vector<simPrimaryVertex> simpv;
862     simpv=getSimPVs(evtMC,"");
863     // simpv=getSimPVs(evtMC, simVtxs, simTrks);
864     int isimpv = 0;
865     for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
866     vsim!=simpv.end(); vsim++, isimpv++){
867     //fillMCHisto((std::vector<simPrimaryVertex>::iterator vsim, int isimpv, const reco::VertexCollection *vtxColl, int datatype) {
868     fillMCHisto(vsim, isimpv, matchedVertexColl1, 1);
869     fillMCHisto(vsim, isimpv, matchedVertexColl2, 2);
870     }
871     }
872    
873     // ==== Analyze res/pull two-vertex method
874     //fillPvtxHisto(const reco::VertexCollection *vertexColl, int datatype)
875     fillTrackHistoInPV(matchedVertexColl1, 1, beamSpot);
876     fillTrackHistoInPV(matchedVertexColl2, 2, beamSpot);
877 yygao 1.9
878     reco::VertexCollection::const_iterator v1;
879     reco::VertexCollection::const_iterator v2;
880 yygao 1.19
881 yygao 1.9 nrecPV_twovtx_ = 0;
882     for(v1 = matchedVertexColl1->begin(), v2 = matchedVertexColl2->begin();
883     v1!=matchedVertexColl1->end(), v2 != matchedVertexColl2->end();
884 yygao 1.17 ++v1, ++v2) {
885     h_misc->Fill1d("ndofPVDiff", v1->ndof() - v2->ndof());
886     h_misc->Fill1d("ndofPVRelDiff", (v1->ndof()-v2->ndof())/(v1->ndof()+v2->ndof()));
887 yygao 1.9 fres_[0] = (v1->x()-v2->x())/sqrt(2.0);
888     fres_[1] = (v1->y()-v2->y())/sqrt(2.0);
889     fres_[2] = (v1->z()-v2->z())/sqrt(2.0);
890     ferror_[0] = sqrt(pow(v1->xError(),2)+pow(v2->xError(),2))/sqrt(2);
891     ferror_[1] = sqrt(pow(v1->yError(),2)+pow(v2->yError(),2))/sqrt(2);
892     ferror_[2] = sqrt(pow(v1->zError(),2)+pow(v2->zError(),2))/sqrt(2);
893     fntrk_ = (v1->tracksSize()+v2->tracksSize())/2;
894    
895 jengbou 1.13 h_summary->Fill1d("deltax", fres_[0] );
896     h_summary->Fill1d("deltay", fres_[1] );
897     h_summary->Fill1d("deltaz", fres_[2] );
898     h_summary->Fill1d("pullx", fres_[0]/ferror_[0]);
899     h_summary->Fill1d("pully", fres_[1]/ferror_[1]);
900     h_summary->Fill1d("pullz", fres_[2]/ferror_[2]);
901     h_summary->Fill1d("errPVx", ferror_[0] );
902     h_summary->Fill1d("errPVy", ferror_[1] );
903     h_summary->Fill1d("errPVz", ferror_[2] );
904 yygao 1.9 pvinfo.push_back(PVStudy::PVInfo(res(fres_[0], fres_[1], fres_[2]),
905     error(ferror_[0], ferror_[1], ferror_[2]),
906     fntrk_));
907     // Fill histo according to its track multiplicity, set datamode = 0 for pvtx using all tracks
908     fillHisto(res(fres_[0], fres_[1], fres_[2]),
909     error(ferror_[0], ferror_[1], ferror_[2]),
910     fntrk_,0);
911 yygao 1.19
912 yygao 1.9 // Fill the ntuple variables
913     nTrkPV_twovtx_[nrecPV_twovtx_] = fntrk_;
914     deltax_twovtx_[nrecPV_twovtx_] = fres_[0];
915     deltay_twovtx_[nrecPV_twovtx_] = fres_[1];
916     deltaz_twovtx_[nrecPV_twovtx_] = fres_[2];
917     errx_twovtx_[nrecPV_twovtx_] = ferror_[0];
918     erry_twovtx_[nrecPV_twovtx_] = ferror_[1];
919     errz_twovtx_[nrecPV_twovtx_] = ferror_[2];
920     pullx_twovtx_[nrecPV_twovtx_] = fres_[0]/ferror_[0];
921     pully_twovtx_[nrecPV_twovtx_] = fres_[1]/ferror_[1];
922     pullz_twovtx_[nrecPV_twovtx_] = fres_[2]/ferror_[2];
923 yygao 1.19
924    
925     //SplittedVertex
926    
927     nTrkPV1_spl_twovtx_[nrecPV_twovtx_] = v1->tracksSize();
928     ndofPV1_spl_twovtx_[nrecPV_twovtx_] = v1->ndof();
929     normchi2PV1_spl_twovtx_[nrecPV_twovtx_] = v1->normalizedChi2();
930     avgPtPV1_spl_twovtx_[nrecPV_twovtx_] = avgPtRecVtx(*v1);
931     errx1_spl_twovtx_[nrecPV_twovtx_] = v1->xError();
932     erry1_spl_twovtx_[nrecPV_twovtx_] = v1->yError();
933     errz1_spl_twovtx_[nrecPV_twovtx_] = v1->zError();
934    
935     nTrkPV2_spl_twovtx_[nrecPV_twovtx_] = v2->tracksSize();
936     ndofPV2_spl_twovtx_[nrecPV_twovtx_] = v2->ndof();
937     normchi2PV2_spl_twovtx_[nrecPV_twovtx_] = v2->normalizedChi2();
938     avgPtPV2_spl_twovtx_[nrecPV_twovtx_] = avgPtRecVtx(*v2);
939     errx2_spl_twovtx_[nrecPV_twovtx_] = v2->xError();
940     erry2_spl_twovtx_[nrecPV_twovtx_] = v2->yError();
941     errz2_spl_twovtx_[nrecPV_twovtx_] = v2->zError();
942    
943 yygao 1.9 nrecPV_twovtx_++;
944 yygao 1.17
945 yygao 1.19
946    
947     // Print some information of the two tracks events
948     if(verbose_ && fntrk_ < 4) {
949     cout<<"Printing matchedVertexColl1 for low ntrack bins"<<endl;
950     printRecVtx(*v1);
951     cout<<"Printing matchedVertexColl1 for low ntrack bins"<<endl;
952     printRecVtx(*v2);
953 yygao 1.17 }
954 yygao 1.19 } // End of analyzing res/pull
955 yygao 1.9 } // End of if(matchedVertexColl1->size() == 1 && matchedVertexColl2->size() == 1 ) {
956     else
957 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] WARNING: Cannot find matching pvtxs"<<endl;
958 yygao 1.9
959 jengbou 1.14 edm::LogInfo("Debug")<<"[PVStudy] End analyzing the matchedVertexColl"<<endl;
960 yygao 1.7
961 yygao 1.17
962 yygao 1.19 //========================================================================
963     // Step 5: Fill ntuple if saventuple_ is on
964     //========================================================================
965 yygao 1.9 if(saventuple_) {
966     ftree_->Fill();
967     pvtxtree_->Fill();
968     }
969 yygao 1.17
970 jengbou 1.1 }
971    
972 yygao 1.17
973    
974 jengbou 1.1 // ------------ method called once each job just before starting event loop ------------
975     void
976     PVStudy::beginJob()
977     {
978     }
979    
980     // ------------ method called once each job just after ending the event loop ------------
981     void
982     PVStudy::endJob() {
983 jengbou 1.14 edm::LogInfo("Analysis") << "[endJob] Analyzing PV info" << endl;
984 yygao 1.7 // Looping through the datamodes available
985     for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {
986     string suffix;
987 jengbou 1.14 edm::LogInfo("Analysis")<<"[endJob] datamode = "<< *it<<endl;
988 yygao 1.7 switch(*it) {
989     case 0: suffix = "";
990     break;
991     case 1: suffix = "_spl1_mct";
992     break;
993     case 2: suffix = "_spl2_mct";
994     break;
995     case 3: suffix = "_mct";
996     break;
997     }
998 jengbou 1.14 suffix += "_nTrk";
999 yygao 1.9 if(analyze_) {
1000 jengbou 1.8 for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
1001 jengbou 1.14 edm::LogInfo("Analysis") << "[endJob] ntrk = " << ntrk << endl;
1002 jengbou 1.8 PVStudy::PVAnaInfo ipv = GetPVAnaInfo(ntrk,*it);
1003 jengbou 1.14 if ( ipv.res_.x() > 0 ) h_ana->Fill2d("resx"+suffix, ntrk,ipv.res_.x());
1004     if ( ipv.res_.y() > 0 ) h_ana->Fill2d("resy"+suffix, ntrk,ipv.res_.y());
1005     if ( ipv.res_.z() > 0 ) h_ana->Fill2d("resz"+suffix, ntrk,ipv.res_.z());
1006     if ( ipv.pull_.x() > 0 ) h_ana->Fill2d("pullx"+suffix, ntrk,ipv.pull_.x());
1007     if ( ipv.pull_.y() > 0 ) h_ana->Fill2d("pully"+suffix, ntrk,ipv.pull_.y());
1008     if ( ipv.pull_.z() > 0 ) h_ana->Fill2d("pullz"+suffix, ntrk,ipv.pull_.z());
1009 jengbou 1.2 }
1010 jengbou 1.14 }
1011 yygao 1.7 suffix.clear();
1012 jengbou 1.1 }
1013 jengbou 1.8 if(saventuple_) {
1014     file_->cd();
1015     ftree_->Write();
1016 yygao 1.9 pvtxtree_->Write();
1017 jengbou 1.8 file_->Close();
1018     }
1019 yygao 1.17
1020    
1021 jengbou 1.1 }
1022    
1023 yygao 1.7 // Match a recovertex with a simvertex
1024     // ? Should the cut be parameter dependent?
1025     // cut can be within n sigma of the vertex.zerror()
1026 jengbou 1.1 bool PVStudy::matchVertex(const PVStudy::simPrimaryVertex & vsim,
1027     const reco::Vertex & vrec)
1028     {
1029 yygao 1.17 if(vrec.isFake() || !vrec.isValid()) return false;
1030     else
1031 yygao 1.19 return true;
1032     //return (fabs(vsim.z-vrec.z())<0.0500); // =500um // remove this hard cut
1033 jengbou 1.1 }
1034    
1035 yygao 1.7 // Match two reconstructed vertices
1036 yygao 1.9 bool PVStudy::matchVertex( const reco::VertexRef & vrec1,
1037     const reco::VertexRef & vrec2,
1038     int zsigncut)
1039 yygao 1.7 {
1040 yygao 1.9 double cut = zsigncut*max(vrec1->zError(), vrec2->zError());
1041 jengbou 1.14 edm::LogInfo("Debug")<<"[matchVertex] The matching criteria in z is "<<cut<<endl;
1042 yygao 1.9 return (fabs(vrec1->z()-vrec2->z())<cut);
1043 yygao 1.7 }
1044    
1045    
1046 jengbou 1.1 bool PVStudy::isResonance(const HepMC::GenParticle * p){
1047     double ctau=(pdt->particle( abs(p->pdg_id()) ))->lifetime();
1048 jengbou 1.14 edm::LogInfo("Debug") << "[isResonance] isResonance " << p->pdg_id() << " " << ctau << endl;
1049 jengbou 1.1 return ctau >0 && ctau <1e-6;
1050     }
1051    
1052     bool PVStudy::isFinalstateParticle(const HepMC::GenParticle * p){
1053     return ( !p->end_vertex() && p->status()==1 );
1054     }
1055    
1056     bool PVStudy::isCharged(const HepMC::GenParticle * p){
1057     const ParticleData * part = pdt->particle( p->pdg_id() );
1058     if (part){
1059     return part->charge()!=0;
1060     }else{
1061     // the new/improved particle table doesn't know anti-particles
1062     return pdt->particle( -p->pdg_id() )!=0;
1063     }
1064     }
1065    
1066     void PVStudy::printSimVtxs(const Handle<SimVertexContainer> simVtxs){
1067     int i=0;
1068     for(SimVertexContainer::const_iterator vsim=simVtxs->begin();
1069     vsim!=simVtxs->end(); ++vsim){
1070     cout << i++ << ")" << scientific
1071     << " evtid=" << vsim->eventId().event()
1072     << " sim x=" << vsim->position().x()
1073     << " sim y=" << vsim->position().y()
1074     << " sim z=" << vsim->position().z()
1075     << " sim t=" << vsim->position().t()
1076     << " parent=" << vsim->parentIndex()
1077     << endl;
1078     }
1079     }
1080    
1081     void PVStudy::printRecVtxs(const Handle<reco::VertexCollection> recVtxs){
1082     int ivtx=0;
1083     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
1084     v!=recVtxs->end(); ++v){
1085     cout << "Recvtx "<< std::setw(3) << std::setfill(' ')<<ivtx++
1086     << "#trk " << std::setw(3) << v->tracksSize()
1087     << " chi2 " << std::setw(4) << v->chi2()
1088     << " ndof " << std::setw(3) << v->ndof() << endl
1089     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v->x()
1090     << " dx " << std::setw(8) << v->xError()<< endl
1091     << " y " << std::setw(8) << v->y()
1092     << " dy " << std::setw(8) << v->yError()<< endl
1093     << " z " << std::setw(8) << v->z()
1094     << " dz " << std::setw(8) << v->zError()
1095     << endl;
1096     }
1097     }
1098    
1099 yygao 1.19 void PVStudy::printRecVtx(const reco::Vertex & v){
1100    
1101     cout << "#trk " << std::setw(3) << v.tracksSize()
1102     << " chi2 " << std::setw(4) << v.chi2()
1103     << " ndof " << std::setw(3) << v.ndof() << endl
1104     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v.x()
1105     << " dx " << std::setw(8) << v.xError()<< endl
1106     << " y " << std::setw(8) << v.y()
1107     << " dy " << std::setw(8) << v.yError()<< endl
1108     << " z " << std::setw(8) << v.z()
1109     << " dz " << std::setw(8) << v.zError()
1110     << endl;
1111     }
1112    
1113 jengbou 1.1 void PVStudy::printSimTrks(const Handle<SimTrackContainer> simTrks){
1114     cout << " simTrks type, (momentum), vertIndex, genpartIndex" << endl;
1115     int i=1;
1116     for(SimTrackContainer::const_iterator t=simTrks->begin();
1117     t!=simTrks->end(); ++t){
1118     //HepMC::GenParticle* gp=evtMC->GetEvent()->particle( (*t).genpartIndex() );
1119     cout << i++ << ")"
1120     << (*t)
1121     << " index="
1122     << (*t).genpartIndex();
1123     //if (gp) {
1124     // HepMC::GenVertex *gv=gp->production_vertex();
1125     // cout << " genvertex =" << (*gv);
1126     //}
1127     cout << endl;
1128     }
1129     }
1130    
1131 yygao 1.7 void PVStudy::fillHisto(res r, error er, int ntk, int datamode){
1132 jengbou 1.3 stringstream ss;
1133     ss << ntk;
1134 jengbou 1.14 string suffix;
1135     if(datamode == 0 ) suffix = "_" + ss.str();
1136     if(datamode == 1 ) suffix = "_spl1_mct_" + ss.str();
1137     if(datamode == 2 ) suffix = "_spl2_mct_" + ss.str();
1138     if(datamode == 3 ) suffix = "_mct_" + ss.str();
1139 yygao 1.7
1140 jengbou 1.3 if (ntk < nTrkMin_ || ntk > nTrkMax_ ) return;
1141     // Fill histograms of res and pull of ntk
1142 jengbou 1.13 h_others->Fill1d("deltax"+suffix, r.x());
1143     h_others->Fill1d("deltay"+suffix, r.y());
1144     h_others->Fill1d("deltaz"+suffix, r.z());
1145     h_others->Fill1d("pullx"+suffix, r.x()/er.x());
1146     h_others->Fill1d("pully"+suffix, r.y()/er.y());
1147     h_others->Fill1d("pullz"+suffix, r.z()/er.z());
1148     h_others->Fill1d("errPVx"+suffix, er.x());
1149     h_others->Fill1d("errPVy"+suffix, er.y());
1150     h_others->Fill1d("errPVz"+suffix, er.z());
1151 jengbou 1.3 }
1152    
1153 yygao 1.17
1154 yygao 1.7 PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo(int ntk, int datamode) {
1155 jengbou 1.1 map<int,double> data;
1156 jengbou 1.2 data.clear();
1157 jengbou 1.4 double fpar[2] = {-999,-999};
1158 jengbou 1.1 double cm2um = 10000;
1159     stringstream ss;
1160     ss << ntk;
1161 yygao 1.7 string suffix = ss.str();
1162     if(datamode == 0 ) suffix = "_" + suffix;
1163     if(datamode == 1 ) suffix = "_spl1_mct_" + suffix;
1164     if(datamode == 2 ) suffix = "_spl2_mct_" + suffix;
1165     if(datamode == 3 ) suffix = "_mct_" + suffix;
1166    
1167 jengbou 1.3 if(analyze_) {
1168     for ( int i = 0; i < 6; ++i) {
1169     switch (i) {
1170     case 0:
1171 jengbou 1.13 if (!h_others->ReadHisto1D("deltax"+suffix)) break;
1172     fit(h_others->ReadHisto1D("deltax"+suffix), fpar);
1173 jengbou 1.3 data[i] = fpar[0]*cm2um;
1174     break;
1175     case 1:
1176 jengbou 1.13 if (!h_others->ReadHisto1D("deltay"+suffix)) break;
1177     fit(h_others->ReadHisto1D("deltay"+suffix), fpar);
1178 jengbou 1.3 data[i] = fpar[0]*cm2um;
1179     break;
1180     case 2:
1181 jengbou 1.13 if (!h_others->ReadHisto1D("deltaz"+suffix)) break;
1182     fit(h_others->ReadHisto1D("deltaz"+suffix), fpar);
1183 jengbou 1.3 data[i] = fpar[0]*cm2um;
1184     break;
1185     case 3:
1186 jengbou 1.13 if (!h_others->ReadHisto1D("pullx"+suffix)) break;
1187     fit(h_others->ReadHisto1D("pullx"+suffix), fpar);
1188 jengbou 1.3 data[i] = fpar[0];
1189     break;
1190     case 4:
1191 jengbou 1.13 if (!h_others->ReadHisto1D("pully"+suffix)) break;
1192     fit(h_others->ReadHisto1D("pully"+suffix), fpar);
1193 jengbou 1.3 data[i] = fpar[0];
1194     break;
1195     case 5:
1196 jengbou 1.13 if (!h_others->ReadHisto1D("pullz"+suffix)) break;
1197     fit(h_others->ReadHisto1D("pullz"+suffix), fpar);
1198 jengbou 1.3 data[i] = fpar[0];
1199     break;
1200 jengbou 1.1 }
1201 jengbou 1.14 if (data[i] > 0) edm::LogInfo("Analysis") << "[Analysis] data[" << i << "] = " << data[i] << (i<3?" micro m":" ") << endl;
1202 jengbou 1.1 }
1203 jengbou 1.3 }
1204     else
1205     for ( int i = 0; i < 6; ++i) {
1206     data[i] = -999;
1207 jengbou 1.1 }
1208    
1209 jengbou 1.3 return PVStudy::PVAnaInfo(res(data[0], data[1], data[2]),
1210     error(0.,0.,0.),
1211     pull(data[3], data[4], data[5]),
1212     error(0.,0.,0.),
1213 jengbou 1.1 ntk);
1214     }
1215    
1216 yygao 1.17
1217 jengbou 1.13 void PVStudy::fit(TH1 *hdist, double fitPars[]){
1218 jengbou 1.3 TAxis *axis0 = hdist->GetXaxis();
1219     int nbin = axis0->GetLast();
1220     double nOF = hdist->GetBinContent(nbin+1);
1221     double nUF = hdist->GetBinContent(0);
1222 jengbou 1.4 // double fitRange = axis0->GetBinUpEdge(nbin);
1223 yygao 1.17 // double fitRange = axis0->GetXmax();
1224     double fitRange = 2*hdist->GetRMS();
1225 jengbou 1.5 double sigMax[2] = {0.,0.};
1226 jengbou 1.4
1227 jengbou 1.14 edm::LogInfo("Analysis") << "[Fit] Last bin = " << nbin
1228     << "; hdist: Overflow Entries = " << nOF
1229     << "; Underflow Entries = " << nUF
1230     << "; hdist->GetEntries() = " << hdist->GetEntries()
1231     << "; fitting range = " << -fitRange << " to " << fitRange << endl;
1232    
1233 jengbou 1.4 if (hdist->GetEntries() - nOF - nUF >= 10) { // FIXME: for reasonable Gaussian fit
1234     for (int bi = 0; bi < nbin; bi++) {
1235     if ( (axis0->GetBinLowEdge(bi) < 0) && (hdist->GetBinContent(bi) > 0) ) {
1236     sigMax[0] = axis0->GetBinLowEdge(bi);
1237 jengbou 1.5 if ( abs(sigMax[0]) > abs(sigMax[1]) ) sigMax[1] = abs(sigMax[0]);
1238 jengbou 1.4 }
1239     if ( (axis0->GetBinLowEdge(bi) >= 0) && (hdist->GetBinContent(bi) > 0) ) {
1240 jengbou 1.5 sigMax[0] = axis0->GetBinUpEdge(bi);
1241     if ( abs(sigMax[0]) > abs(sigMax[1]) ) sigMax[1] = abs(sigMax[0]);
1242 jengbou 1.4 }
1243     }
1244 jengbou 1.14 //edm::LogInfo("Analysis") << "[Fit] Fit sigMax = " << sqrt(2.)*sigMax[1] << endl;
1245 jengbou 1.4
1246     TF1 *fgaus = new TF1("fgaus","gaus",-fitRange, fitRange);
1247     fgaus->SetParameter(1, 0.);
1248 yygao 1.17 fgaus->SetParLimits(1, -fitRange/10., fitRange/10.);
1249 jengbou 1.5 fgaus->SetParLimits(2, 0., sqrt(2.)*sigMax[1]);
1250 jengbou 1.4 fgaus->SetLineColor(4);
1251 jengbou 1.16 hdist->Fit(fgaus,"QLRM");
1252 jengbou 1.4 gPad->Update();
1253     TPaveStats *s = (TPaveStats*) hdist->GetListOfFunctions()->FindObject("stats");
1254     s->SetOptStat(1111111);
1255     s->SetOptFit(0111);
1256     gPad->Update();
1257 jengbou 1.14 //edm::LogInfo("Analysis") << "[Fit] got function" << endl;
1258 jengbou 1.4 fitPars[0] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
1259 jengbou 1.3 }
1260 jengbou 1.4 else
1261     fitPars[0] = -999;
1262 jengbou 1.3 }
1263    
1264 yygao 1.17
1265     void PVStudy::fillTrackHisto(const reco::TrackCollection *trackColl, int datatype, const Point & bs) {
1266 yygao 1.9 string suffix;
1267     suffix = ""; // for unsplit tracks
1268     if(datatype == 1) suffix = "1_spl"; // for splittracks 1
1269     if(datatype == 2) suffix = "2_spl"; // for splittracks 2
1270    
1271 jengbou 1.13 h_pvtrk->Fill1d("nTrk"+suffix, trackColl->size());
1272 yygao 1.9 for(reco::TrackCollection::const_iterator itTrack = trackColl->begin();
1273     itTrack != trackColl->end();
1274     ++itTrack) {
1275 jengbou 1.13 h_pvtrk->Fill1d("trkPt"+suffix, itTrack->pt());
1276     h_pvtrk->Fill1d("trkDxy"+suffix, itTrack->dxy());
1277 yygao 1.17 h_pvtrk->Fill1d("trkDxyCorr"+suffix, itTrack->dxy(bs));
1278 jengbou 1.13 h_pvtrk->Fill1d("trkDz"+suffix, itTrack->dz());
1279     h_pvtrk->Fill1d("trkEta"+suffix, itTrack->eta());
1280     h_pvtrk->Fill1d("trkPhi"+suffix, itTrack->phi());
1281 yygao 1.9 }
1282     }
1283    
1284 yygao 1.19 void PVStudy::fillTrackHistoInPV(const reco::VertexCollection *vertexColl, int datatype, const Point & bs) {
1285 yygao 1.9 string suffix;
1286     suffix = ""; // for unsplit tracks
1287     if(datatype == 1) suffix = "1_spl"; // for splittracks 1
1288     if(datatype == 2) suffix = "2_spl"; // for splittracks 2
1289     int ivtx = 0;
1290     for(reco::VertexCollection::const_iterator v=vertexColl->begin();
1291     v!=vertexColl->end(); ++v, ++ivtx) {
1292 yygao 1.19 if(!v->isFake()) {
1293     h_pvtrk->Fill1d("nTrkPV"+suffix, vertexColl->begin()->tracksSize());
1294     h_pvtrk->Fill1d("ndofPV"+suffix, vertexColl->begin()->ndof());
1295     int nHWTrkPV_ = 0;
1296     try {
1297     for(reco::Vertex::trackRef_iterator t = vertexColl->begin()->tracks_begin();
1298     t!=vertexColl->begin()->tracks_end(); t++) {
1299     // illegal charge
1300     if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
1301     // h_pvtrk->Fill1d("trkPtPV", 0.);
1302     }
1303     else {
1304     h_pvtrk->Fill1d("trkPtPV"+suffix, (**t).pt());
1305     h_pvtrk->Fill1d("trkDxyPV"+suffix, (**t).dxy());
1306     h_pvtrk->Fill1d("trkDxyCorrPV"+suffix, (**t).dxy(bs));
1307     h_pvtrk->Fill1d("trkDzPV"+suffix, (**t).dz());
1308     h_pvtrk->Fill1d("trkEtaPV"+suffix, (**t).eta());
1309     h_pvtrk->Fill1d("trkPhiPV"+suffix, (**t).phi());
1310     if(vertexColl->begin()->trackWeight(*t) > 0.5)
1311     nHWTrkPV_++;
1312     }
1313     }
1314 yygao 1.9 }
1315 yygao 1.19 catch (...) {
1316     // exception thrown when trying to use linked track
1317     // h_pvtrk->Fill1d("trkPtPV", 0.);
1318 yygao 1.11 }
1319 yygao 1.19 h_pvtrk->Fill1d("nHWTrkPV"+suffix, nHWTrkPV_);
1320 yygao 1.9 }
1321 yygao 1.10 }
1322 yygao 1.9 }
1323    
1324     void PVStudy::fillMCHisto(std::vector<simPrimaryVertex>::iterator vsim, int isimpv, const reco::VertexCollection *vtxColl, int datatype)
1325     {
1326     std::string suffix;
1327     // Set the vertexColl and histogram suffix according to datamode
1328     if (datatype == 1) {
1329     suffix = "_spl1_mct";
1330     nrecPV_spl1_mct_ = 0;
1331     }
1332     if (datatype == 2) {
1333     suffix = "_spl2_mct";
1334     nrecPV_spl2_mct_ = 0;
1335     }
1336     if (datatype == 3) {
1337     suffix = "_mct";
1338     nrecPV_mct_ = 0;
1339     }
1340    
1341     //========================================================
1342     // look for a matching reconstructed vertex in vertexColl
1343     //========================================================
1344    
1345     for(reco::VertexCollection::const_iterator vrec=vtxColl->begin();
1346     vrec!=vtxColl->end(); ++vrec){
1347     vsim->recVtx=NULL;
1348 jengbou 1.14 edm::LogInfo("Debug") << "[fillMCHisto] sim primary vertex x = " << vsim->x << "; y = " << vsim->y << "; z = " << vsim->z << endl;
1349     edm::LogInfo("Debug") << "[fillMCHisto] Is matched? " << (matchVertex(*vsim,*vrec)?"Yes":"No") << endl;
1350    
1351 yygao 1.17 if ( matchVertex(*vsim,*vrec) ) {
1352 yygao 1.9 vsim->recVtx=&(*vrec);
1353    
1354     // if the matching critera are fulfilled, accept the rec-vertex that is closest in z
1355     //if( ((vsim->recVtx) && (fabs(vsim->recVtx->position().z()-vsim->z)>fabs(vrec->z()-vsim->z)))
1356     //|| (!vsim->recVtx) )
1357     //vsim->recVtx=&(*vrec);
1358     //}
1359 jengbou 1.14 edm::LogInfo("Debug") <<"[fillMCHisto] primary matched in vertexColl" << vsim->x << " " << vsim->y << " " << vsim->z << endl;
1360 yygao 1.9
1361     double fres_mct[3];
1362     double ferror_mct[3];
1363 yygao 1.19 int nrectrk = int(vsim->recVtx->tracksSize());
1364     double ndofPV = vsim->recVtx->ndof();
1365     double normchi2PV = vsim->recVtx->normalizedChi2();
1366     double avgPtPV = avgPtRecVtx(*(vsim->recVtx));
1367    
1368 yygao 1.9 fres_mct[0] = vsim->recVtx->x()-vsim->x;
1369     fres_mct[1] = vsim->recVtx->y()-vsim->y;
1370     fres_mct[2] = vsim->recVtx->z()-vsim->z;
1371     ferror_mct[0] = vsim->recVtx->xError();
1372     ferror_mct[1] = vsim->recVtx->yError();
1373     ferror_mct[2] = vsim->recVtx->zError();
1374    
1375 jengbou 1.13 h_summary->Fill1d("deltax"+suffix, fres_mct[0] );
1376     h_summary->Fill1d("deltay"+suffix, fres_mct[1] );
1377     h_summary->Fill1d("deltaz"+suffix, fres_mct[2] );
1378     h_summary->Fill1d("pullx"+suffix, fres_mct[0]/ferror_mct[0] );
1379     h_summary->Fill1d("pully"+suffix, fres_mct[1]/ferror_mct[1] );
1380     h_summary->Fill1d("pullz"+suffix, fres_mct[2]/ferror_mct[2] );
1381     h_summary->Fill1d("errPVx"+suffix, ferror_mct[0] );
1382     h_summary->Fill1d("errPVy"+suffix, ferror_mct[1] );
1383     h_summary->Fill1d("errPVz"+suffix, ferror_mct[2] );
1384 yygao 1.9 pvinfo.push_back(PVStudy::PVInfo(res(fres_mct[0], fres_mct[1], fres_mct[2]),
1385     error(ferror_mct[0], ferror_mct[1], ferror_mct[2]),
1386     nrectrk));
1387     // Fill histo according to its track multiplicity
1388     fillHisto(res(fres_mct[0], fres_mct[1], fres_mct[2]),
1389     error(ferror_mct[0], ferror_mct[1], ferror_mct[2]),
1390     nrectrk, datatype);
1391    
1392     if(saventuple_) {
1393 yygao 1.19 //Fill the values for variables in pvtxtree_
1394 yygao 1.9 if(datatype == 1) {
1395     nTrkPV_spl1_mct_[nrecPV_spl1_mct_] = nrectrk;
1396 yygao 1.19 ndofPV_spl1_mct_[nrecPV_spl1_mct_] = ndofPV;
1397     normchi2PV_spl1_mct_[nrecPV_spl1_mct_] = normchi2PV;
1398     avgPtPV_spl1_mct_[nrecPV_spl1_mct_] = avgPtPV;
1399 yygao 1.9 deltax_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[0];
1400 yygao 1.19 deltay_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[1];
1401     deltaz_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[2];
1402 yygao 1.9 pullx_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[0]/ferror_mct[0];
1403     pully_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[1]/ferror_mct[1];
1404     pullz_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[2]/ferror_mct[2];
1405 yygao 1.19 errx_spl1_mct_[nrecPV_spl1_mct_] = ferror_mct[0];
1406     erry_spl1_mct_[nrecPV_spl1_mct_] = ferror_mct[1];
1407     errz_spl1_mct_[nrecPV_spl1_mct_] = ferror_mct[2];
1408 yygao 1.9 nrecPV_spl1_mct_++;
1409     }
1410     if(datatype == 2) {
1411 yygao 1.19 nTrkPV_spl2_mct_[nrecPV_spl2_mct_] = nrectrk;
1412     ndofPV_spl2_mct_[nrecPV_spl2_mct_] = ndofPV;
1413     normchi2PV_spl2_mct_[nrecPV_spl2_mct_] = normchi2PV;
1414     avgPtPV_spl2_mct_[nrecPV_spl2_mct_] = avgPtPV;
1415 yygao 1.9 deltax_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[0];
1416 yygao 1.19 deltay_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[1];
1417     deltaz_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[2];
1418 yygao 1.9 pullx_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[0]/ferror_mct[0];
1419     pully_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[1]/ferror_mct[1];
1420 yygao 1.19 pullz_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[2]/ferror_mct[2];
1421     errx_spl2_mct_[nrecPV_spl2_mct_] = ferror_mct[0];
1422     erry_spl2_mct_[nrecPV_spl2_mct_] = ferror_mct[1];
1423     errz_spl2_mct_[nrecPV_spl2_mct_] = ferror_mct[2];
1424 yygao 1.9 nrecPV_spl2_mct_++;
1425     }
1426     if(datatype == 3) {
1427 yygao 1.19 nTrkPV_mct_[nrecPV_mct_] = nrectrk;
1428     ndofPV_mct_[nrecPV_mct_] = ndofPV;
1429     normchi2PV_mct_[nrecPV_mct_] = normchi2PV;
1430     avgPtPV_mct_[nrecPV_mct_] = avgPtPV;
1431 yygao 1.9 deltax_mct_[nrecPV_mct_] = fres_mct[0];
1432     deltay_mct_[nrecPV_mct_] = fres_mct[0];
1433     deltaz_mct_[nrecPV_mct_] = fres_mct[0];
1434     pullx_mct_[nrecPV_mct_] = fres_mct[0]/ferror_mct[0];
1435     pully_mct_[nrecPV_mct_] = fres_mct[1]/ferror_mct[1];
1436 yygao 1.19 pullz_mct_[nrecPV_mct_] = fres_mct[2]/ferror_mct[2];
1437     errx_mct_[nrecPV_mct_] = ferror_mct[0];
1438     erry_mct_[nrecPV_mct_] = ferror_mct[1];
1439     errz_mct_[nrecPV_mct_] = ferror_mct[2];
1440 yygao 1.9 nrecPV_mct_++;
1441     }
1442     } // End of if(saventuple_) {
1443     } // if ( matchVertex(*vsim,*vrec) ) {
1444     else { // no rec vertex found for this simvertex
1445 jengbou 1.14 edm::LogInfo("Debug") <<"[fillMCHisto] primary not found " << vsim->x << " " << vsim->y << " " << vsim->z << " nGenTrk=" << vsim->nGenTrk << endl;
1446 yygao 1.9 }
1447     }
1448     }
1449    
1450 yygao 1.7 void PVStudy::SetVarToZero() {
1451     fntrk_ = 0;
1452     //pvtx position (x,y,z) residual and error
1453 yygao 1.9 for(int i = 0; i<3;i++) {
1454 jengbou 1.13 fres_[i] = 0;
1455     ferror_[i] = 0;
1456 yygao 1.9 }
1457    
1458 yygao 1.19 // Event level information
1459     glob_runno_ = 0;
1460     glob_evtno_ = 0;
1461     glob_ls_ = 0;
1462     glob_bx_ = 0;
1463 yygao 1.9 nrecPV_ = 0;
1464     nrecPV1_spl_ = 0;
1465     nrecPV2_spl_ = 0;
1466     nrecPV_twovtx_ = 0;
1467     nsimPV_ = 0;
1468 yygao 1.10
1469 yygao 1.9 nrecPV_mct_ = 0;
1470     nrecPV_spl1_mct_ = 0;
1471     nrecPV_spl2_mct_ = 0;
1472    
1473 yygao 1.10 // Mininum separation between the secondary pvtxes and leading pvtx
1474     min_zsep_ = 9999.0;
1475     min_ntrksep_ = 9999.0;
1476     min_sumpt2sep_ = -9999.0;
1477    
1478 yygao 1.19 // Variables filled per Vertex
1479 yygao 1.9 for (int i = 0; i < nMaxPVs_; i++) {
1480 yygao 1.10 // matched two-vertices
1481 yygao 1.9 nTrkPV1_spl_twovtx_[i] = 0;
1482 yygao 1.19 ndofPV1_spl_twovtx_[i] = 0;
1483     normchi2PV1_spl_twovtx_[i] = 0;
1484     avgPtPV1_spl_twovtx_[i] = 0;
1485     errx1_spl_twovtx_[i] = 0;
1486     erry1_spl_twovtx_[i] = 0;
1487     errz1_spl_twovtx_[i] = 0;
1488    
1489 yygao 1.9 nTrkPV2_spl_twovtx_[i] = 0;
1490 yygao 1.19 ndofPV2_spl_twovtx_[i] = 0;
1491     normchi2PV2_spl_twovtx_[i] = 0;
1492     avgPtPV2_spl_twovtx_[i] = 0;
1493     errx2_spl_twovtx_[i] = 0;
1494     erry2_spl_twovtx_[i] = 0;
1495     errz2_spl_twovtx_[i] = 0;
1496    
1497 yygao 1.9 deltax_twovtx_[i] = 0;
1498     deltay_twovtx_[i] = 0;
1499     deltaz_twovtx_[i] = 0;
1500     errx_twovtx_[i] = 0;
1501     erry_twovtx_[i] = 0;
1502     errz_twovtx_[i] = 0;
1503     pullx_twovtx_[i] = 0;
1504     pully_twovtx_[i] = 0;
1505     pullz_twovtx_[i] = 0;
1506    
1507 yygao 1.10 //simpv
1508 yygao 1.9 nsimTrkPV_[i] = 0;
1509     simx_[i] = 0;
1510     simy_[i] = 0;
1511     simz_[i] = 0;
1512     simptsq_[i] = 0;
1513    
1514 yygao 1.10 // residual and pulls with mc truth required
1515 yygao 1.9 deltax_mct_[i] = 0;
1516     deltay_mct_[i] = 0;
1517     deltaz_mct_[i] = 0;
1518     pullx_mct_[i] = 0;
1519     pully_mct_[i] = 0;
1520     pullz_mct_[i] = 0;
1521 yygao 1.19 nTrkPV_mct_[i] = 0;
1522     ndofPV_mct_[i] = 0;
1523     normchi2PV_mct_[i] = 0;
1524     avgPtPV_mct_[i] = 0;
1525     errx_mct_[i] = 0;
1526     erry_mct_[i] = 0;
1527     errz_mct_[i] = 0;
1528    
1529 yygao 1.9 deltax_spl1_mct_[i] = 0;
1530     deltay_spl1_mct_[i] = 0;
1531     deltaz_spl1_mct_[i] = 0;
1532     pullx_spl1_mct_[i] = 0;
1533     pully_spl1_mct_[i] = 0;
1534     pullz_spl1_mct_[i] = 0;
1535 yygao 1.19 nTrkPV_spl1_mct_[i] = 0;
1536     ndofPV_spl1_mct_[i] = 0;
1537     normchi2PV_spl1_mct_[i] = 0;
1538     avgPtPV_spl1_mct_[i] = 0;
1539     errx_spl1_mct_[i] = 0;
1540     erry_spl1_mct_[i] = 0;
1541     errz_spl1_mct_[i] = 0;
1542    
1543 yygao 1.9 deltax_spl2_mct_[i] = 0;
1544     deltay_spl2_mct_[i] = 0;
1545     deltaz_spl2_mct_[i] = 0;
1546     pullx_spl2_mct_[i] = 0;
1547     pully_spl2_mct_[i] = 0;
1548     pullz_spl2_mct_[i] = 0;
1549 yygao 1.19 nTrkPV_spl2_mct_[i] = 0;
1550     ndofPV_spl2_mct_[i] = 0;
1551     normchi2PV_spl2_mct_[i] = 0;
1552     avgPtPV_spl2_mct_[i] = 0;
1553     errx_spl2_mct_[i] = 0;
1554     erry_spl2_mct_[i] = 0;
1555     errz_spl2_mct_[i] = 0;
1556    
1557 yygao 1.9 }
1558 yygao 1.7 }
1559    
1560 yygao 1.9 double PVStudy::sumPtSquared(const reco::Vertex & v) {
1561 yygao 1.19 if(v.isFake() || (!v.isValid()) || v.tracksSize() == 0) return 0.0;
1562     else {
1563     double sum = 0.;
1564     double pT;
1565     for (reco::Vertex::trackRef_iterator it = v.tracks_begin(); it != v.tracks_end(); it++) {
1566     pT = (**it).pt();
1567     sum += pT*pT;
1568     }
1569     return sum;
1570     }
1571     }
1572    
1573    
1574    
1575     double PVStudy::avgPtRecVtx(const reco::Vertex & v) {
1576    
1577     if(v.isFake() || !v.isValid() || v.tracksSize()==0 ) return 0;
1578     else {
1579     double sumpT = 0.;
1580     for (reco::Vertex::trackRef_iterator it = v.tracks_begin(); it != v.tracks_end(); it++) {
1581     sumpT += (**it).pt();
1582     }
1583     return sumpT/double(v.tracksSize());
1584 yygao 1.9 }
1585     }
1586 yygao 1.10
1587 yygao 1.17
1588