ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVStudy.cc
(Generate patch)

Comparing UserCode/Jeng/PVStudy/plugins/PVStudy.cc (file contents):
Revision 1.2 by jengbou, Thu Sep 3 19:29:38 2009 UTC vs.
Revision 1.20 by yygao, Thu Feb 18 10:59:48 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines