ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVStudy.cc
Revision: 1.9
Committed: Thu Oct 29 02:05:54 2009 UTC (15 years, 6 months ago) by yygao
Content type: text/plain
Branch: MAIN
Branch point for: CMSSW_3_1_2_RecoVtxMatch
Changes since 1.8: +548 -319 lines
Log Message:
Add ntuple branches and change the recovertex matching

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