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