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.3 by jengbou, Tue Sep 8 07:15:38 2009 UTC vs.
Revision 1.17 by yygao, Tue Jan 26 12:06:49 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 + // Trigger
55 + #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
56 + #include "CondFormats/DataRecord/interface/L1GtTriggerMenuRcd.h"
57 + #include "CondFormats/L1TObjects/interface/L1GtPrescaleFactors.h"
58 + #include "CondFormats/DataRecord/interface/L1GtPrescaleFactorsAlgoTrigRcd.h"
59 + #include "CondFormats/DataRecord/interface/L1GtPrescaleFactorsTechTrigRcd.h"
60 + #include "CondFormats/L1TObjects/interface/L1GtTriggerMask.h"
61 + #include "CondFormats/DataRecord/interface/L1GtTriggerMaskAlgoTrigRcd.h"
62 + #include "CondFormats/DataRecord/interface/L1GtTriggerMaskTechTrigRcd.h"
63 + #include "CondFormats/DataRecord/interface/L1GtTriggerMaskVetoAlgoTrigRcd.h"
64 + #include "CondFormats/DataRecord/interface/L1GtTriggerMaskVetoTechTrigRcd.h"
65 + #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetupFwd.h"
66 + #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetup.h"
67 + #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
68  
52 //root
53 #include "TROOT.h"
54 #include "TF1.h"
55 #include "TString.h"
56 #include "TStyle.h"
69  
70 + //root
71 + #include <TROOT.h>
72 + #include <TF1.h>
73 + #include <TString.h>
74 + #include <TStyle.h>
75 + #include <TPaveStats.h>
76 + #include <TPad.h>
77  
78   using namespace std;
79 + typedef math::XYZTLorentzVectorF LorentzVector;
80 + typedef math::XYZPoint Point;
81  
82 < PVStudy::PVStudy(const edm::ParameterSet& iConfig):
62 <  nTrkMin_(10),nTrkMax_(100)
82 > PVStudy::PVStudy(const edm::ParameterSet& iConfig)
83   {
84 <  simG4_           = iConfig.getParameter<edm::InputTag>( "simG4" );
85 <  tracksLabel_     = iConfig.getUntrackedParameter<edm::InputTag>("tracks");
86 <  vtxSample_       = iConfig.getUntrackedParameter<edm::InputTag>("vtxSample");
87 <  verbose_         = iConfig.getUntrackedParameter<bool>("verbose", false);
88 <  realData_        = iConfig.getUntrackedParameter<bool>("realData",false);
89 <  analyze_         = iConfig.getUntrackedParameter<bool>("analyzeOnTheFly",false);
90 <  outputfilename_  = iConfig.getUntrackedParameter<string>("OutputFileName");
91 <
84 >  //=======================================================================
85 >  // Get configuration for TrackTupleMaker
86 >  //=======================================================================
87 >  simG4_                     = iConfig.getParameter<edm::InputTag>( "simG4" );
88 >  trackCollectionTag_        = iConfig.getUntrackedParameter<edm::InputTag>("trackCollection");  
89 >  splitTrackCollection1Tag_  = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection1");
90 >  splitTrackCollection2Tag_  = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection2");
91 >  vertexCollectionTag_       = iConfig.getUntrackedParameter<edm::InputTag>("vertexCollection");
92 >  splitVertexCollection1Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection1");
93 >  splitVertexCollection2Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection2");
94 >  verbose_                   = iConfig.getUntrackedParameter<bool>("verbose",false);
95 >  realData_                  = iConfig.getUntrackedParameter<bool>("realData",false);
96 >  analyze_                   = iConfig.getUntrackedParameter<bool>("analyzeOnTheFly",false);
97 >  saventuple_                = iConfig.getUntrackedParameter<bool>("saventuple",false);  
98 >  outputfilename_            = iConfig.getUntrackedParameter<string>("OutputFileName");
99 >  ntrkdiffcut_               = iConfig.getUntrackedParameter<double>("ntrkdiffcut");
100 >  nTrkMin_                   = iConfig.getUntrackedParameter<int>("nTrkMin");
101 >  nTrkMax_                   = iConfig.getUntrackedParameter<int>("nTrkMax");
102 >  zsigncut_                  = iConfig.getUntrackedParameter<int>("zsigncut");
103 >  bsSrc = iConfig.getParameter< edm::InputTag >("beamSpot");
104 >  applyEvtClean_            = iConfig.getUntrackedParameter<bool>("applyEvtClean",false);
105 >  histoFileName_             = iConfig.getUntrackedParameter<std::string> ("histoFileName");
106 >  
107    //now do what ever initialization is needed
108    pvinfo.clear();
109  
75  file_ = TFile::Open(outputfilename_.c_str(),"RECREATE");
76  ftree_ = new TTree("mytree","mytree");
77  ftree_->AutoSave();
78  ftree_->Branch("residual",&fres,"fres[3]/D");
79  ftree_->Branch("error",&ferror,"ferror[3]/D");
80  ftree_->Branch("nTrk",&fntrk,"fntrk/I");
81  
82  edm::Service<TFileService> fs;
83 //   TFileDirectory subDir = fs->mkdir( "Summary" );
84 //   TFileDirectory subSubDir = subDir.mkdir( "mySubSubDirectory" );
85
86  h_nTrk           = fs->make<TH1F>("nTrk","Num of total tracks",300,0,300);
87  h_nTrkPV         = fs->make<TH1F>("nTrkPV","Num of Tracks from PV",300,0,300);
88  h_trkPt          = fs->make<TH1D>("trkPt","Pt of all tracks",100,0,100);
89  h_trkPtPV        = fs->make<TH1D>("trkPtPV","Pt dist of tracks from PV",100,0,100);
110  
111 +  // Specify the data mode vector
112 +  if(realData_) datamode.push_back(0);
113 +  else {
114 +    datamode.push_back(0);
115 +    datamode.push_back(1);
116 +    datamode.push_back(2);
117 +    datamode.push_back(3);
118 +  }
119 + // Create ntuple files if needed
120 +  if(saventuple_) {
121 +    file_ = TFile::Open(outputfilename_.c_str(),"RECREATE");
122 +    ftree_ = new TTree("mytree","mytree");
123 +    ftree_->AutoSave();
124 +    ftree_->Branch("residual",&fres_,"fres_[3]/D");
125 +    ftree_->Branch("error",&ferror_,"ferror_[3]/D");
126 +    ftree_->Branch("nTrkPV",&fntrk_,"fntrk_/I");
127 +
128 +    // pvtxtree_ analyzing the pvtxs ootb
129 +    pvtxtree_ = new TTree("pvtxtree","pvtxtree");
130 +    //pvtx using all tracks
131 +    pvtxtree_->Branch("nrecPV",&nrecPV_,"nrecPV/I");
132 +    pvtxtree_->Branch("nTrkPV",&nTrkPV_,"nTrkPV[nrecPV]/I");
133 +    pvtxtree_->Branch("sumptsq",&sumptsq_,"sumptsq[nrecPV]/D");    
134 +    pvtxtree_->Branch("isValid",&isValid_,"isValid/I");
135 +    pvtxtree_->Branch("isFake",&isFake_,"isFake/I");
136 +    pvtxtree_->Branch("recx",&recx_,"recx[nrecPV]/D");
137 +    pvtxtree_->Branch("recy",&recy_,"recy[nrecPV]/D");
138 +    pvtxtree_->Branch("recz",&recz_,"recz[nrecPV]/D");
139 +    pvtxtree_->Branch("recx_err",&recx_err_,"recx_err[nrecPV]/D");
140 +    pvtxtree_->Branch("recy_err",&recy_err_,"recy_err[nrecPV]/D");
141 +    pvtxtree_->Branch("recz_err",&recz_err_,"recz_err[nrecPV]/D");
142 +
143 +    pvtxtree_->Branch("min_zsep",&min_zsep_,"min_zsep/D");
144 +    pvtxtree_->Branch("min_ntrksep",&min_ntrksep_,"min_ntrksep/D");
145 +    pvtxtree_->Branch("min_sumpt2sep",&min_sumpt2sep_,"min_sumpt2sep/D");
146 +
147 +    //pvtx using splittracks1
148 +    pvtxtree_->Branch("nrecPV1_spl",&nrecPV1_spl_,"nrecPV1_spl/I");
149 +    pvtxtree_->Branch("nTrkPV1_spl",&nTrkPV1_spl_,"nTrkPV1_spl[nrecPV1_spl]/I");  
150 +    pvtxtree_->Branch("sumptsq1_spl",&sumptsq1_spl_,"sumptsq1_spl[nrecPV1_spl]/D");  
151 +    pvtxtree_->Branch("isValid1_spl",&isValid1_spl_,"isValid1_spl/I");
152 +    pvtxtree_->Branch("isFake1_spl",&isFake1_spl_,"isFake1_spl/I");
153 +    pvtxtree_->Branch("recx1_spl",&recx1_spl_,"recx1_spl[nrecPV1_spl]/D");
154 +    pvtxtree_->Branch("recy1_spl",&recy1_spl_,"recy1_spl[nrecPV1_spl]/D");
155 +    pvtxtree_->Branch("recz1_spl",&recz1_spl_,"recz1_spl[nrecPV1_spl]/D");
156 +    pvtxtree_->Branch("recx1_err_spl",&recx1_err_spl_,"recx1_err_spl[nrecPV1_spl]/D");
157 +    pvtxtree_->Branch("recy1_err_spl",&recy1_err_spl_,"recy1_err_spl[nrecPV1_spl]/D");
158 +    pvtxtree_->Branch("recz1_err_spl",&recz1_err_spl_,"recz1_err_spl[nrecPV1_spl]/D");  
159 +  
160 +    //pvtx using splittracks2
161 +    pvtxtree_->Branch("nrecPV2_spl",&nrecPV2_spl_,"nrecPV2_spl/I");
162 +    pvtxtree_->Branch("nTrkPV2_spl",&nTrkPV2_spl_,"nTrkPV2_spl[nrecPV2_spl]/I");    
163 +    pvtxtree_->Branch("sumptsq2_spl",&sumptsq2_spl_,"sumptsq2_spl[nrecPV2_spl]/D");  
164 +    pvtxtree_->Branch("isValid2_spl",&isValid2_spl_,"isValid2_spl/I");
165 +    pvtxtree_->Branch("isFake2_spl",&isFake2_spl_,"isFake2_spl/I");
166 +    pvtxtree_->Branch("recx2_spl",&recx2_spl_,"recx2_spl[nrecPV2_spl]/D");
167 +    pvtxtree_->Branch("recy2_spl",&recy2_spl_,"recy2_spl[nrecPV2_spl]/D");
168 +    pvtxtree_->Branch("recz2_spl",&recz2_spl_,"recz2_spl[nrecPV2_spl]/D");
169 +    pvtxtree_->Branch("recx2_err_spl",&recx2_err_spl_,"recx2_err_spl[nrecPV2_spl]/D");
170 +    pvtxtree_->Branch("recy2_err_spl",&recy2_err_spl_,"recy2_err_spl[nrecPV2_spl]/D");
171 +    pvtxtree_->Branch("recz2_err_spl",&recz2_err_spl_,"recz2_err_spl[nrecPV2_spl]/D");  
172 +    
173 +    //pixeVertices
174 +    pvtxtree_->Branch("nrecPV_pxlpvtx",&nrecPV_pxlpvtx_,"nrecPV_pxlpvtx/I");
175 +    pvtxtree_->Branch("nTrkPV_pxlpvtx",&nTrkPV_pxlpvtx_,"nTrkPV_pxlpvtx[nrecPV_pxlpvtx]/I");    
176 +    pvtxtree_->Branch("sumptsq_pxlpvtx",&sumptsq_pxlpvtx_,"sumptsq_pxlpvtx[nrecPV_pxlpvtx]/D");  
177 +    pvtxtree_->Branch("isValid_pxlpvtx",&isValid_pxlpvtx_,"isValid_pxlpvtx/I");
178 +    pvtxtree_->Branch("isFake_pxlpvtx",&isFake_pxlpvtx_,"isFake_pxlpvtx/I");
179 +    pvtxtree_->Branch("recx_pxlpvtx",&recx_pxlpvtx_,"recx_pxlpvtx[nrecPV_pxlpvtx]/D");
180 +    pvtxtree_->Branch("recy_pxlpvtx",&recy_pxlpvtx_,"recy_pxlpvtx[nrecPV_pxlpvtx]/D");
181 +    pvtxtree_->Branch("recz_pxlpvtx",&recz_pxlpvtx_,"recz_pxlpvtx[nrecPV_pxlpvtx]/D");
182 +    pvtxtree_->Branch("recx_err_pxlpvtx",&recx_err_pxlpvtx_,"recx_err_pxlpvtx[nrecPV_pxlpvtx]/D");
183 +    pvtxtree_->Branch("recy_err_pxlpvtx",&recy_err_pxlpvtx_,"recy_err_pxlpvtx[nrecPV_pxlpvtx]/D");
184 +    pvtxtree_->Branch("recz_err_pxlpvtx",&recz_err_pxlpvtx_,"recz_err_pxlpvtx[nrecPV_pxlpvtx]/D");  
185 +
186 +    //Fill the variables in the twovtx pair (recvtx1, recvtx2)
187 +    pvtxtree_->Branch("nrecPV_twovtx",&nrecPV_twovtx_,"nrecPV_twovtx/I");
188 +    pvtxtree_->Branch("nTrkPV1_spl_twovtx",&nTrkPV1_spl_twovtx_,"nTrkPV1_spl_twovtx[nrecPV_twovtx]/I");
189 +    pvtxtree_->Branch("nTrkPV2_spl_twovtx",&nTrkPV2_spl_twovtx_,"nTrkPV2_spl_twovtx[nrecPV_twovtx]/I");
190 +    pvtxtree_->Branch("sumptsq1_spl_twovtx",&sumptsq1_spl_twovtx_,"sumptsq1_spl_twovtx[nrecPV_twovtx]/D");
191 +    pvtxtree_->Branch("sumptsq2_spl_twovtx",&sumptsq2_spl_twovtx_,"sumptsq2_spl_twovtx[nrecPV_twovtx]/D");
192 +    pvtxtree_->Branch("nTrkPV_twovtx",&nTrkPV_twovtx_,"nTrkPV_twovtx[nrecPV_twovtx]/I");
193 +    pvtxtree_->Branch("deltax_twovtx",&deltax_twovtx_,"deltax_twovtx[nrecPV_twovtx]/D");
194 +    pvtxtree_->Branch("deltay_twovtx",&deltay_twovtx_,"deltay_twovtx[nrecPV_twovtx]/D");
195 +    pvtxtree_->Branch("deltaz_twovtx",&deltaz_twovtx_,"deltaz_twovtx[nrecPV_twovtx]/D");
196 +    pvtxtree_->Branch("errx_twovtx",&errx_twovtx_,"errx_twovtx[nrecPV_twovtx]/D");
197 +    pvtxtree_->Branch("erry_twovtx",&erry_twovtx_,"erry_twovtx[nrecPV_twovtx]/D");
198 +    pvtxtree_->Branch("errz_twovtx",&errz_twovtx_,"errz_twovtx[nrecPV_twovtx]/D");
199 +    pvtxtree_->Branch("pullx_twovtx",&pullx_twovtx_,"pullx_twovtx[nrecPV_twovtx]/D");
200 +    pvtxtree_->Branch("pully_twovtx",&pully_twovtx_,"pully_twovtx[nrecPV_twovtx]/D");
201 +    pvtxtree_->Branch("pullz_twovtx",&pullz_twovtx_,"pullz_twovtx[nrecPV_twovtx]/D");
202 +    
203 +    // MC variables
204 +    if(!realData_) {  
205 +      pvtxtree_->Branch("nsimPV",&nsimPV_,"nsimPV/I");
206 +      pvtxtree_->Branch("nsimTrkPV",&nsimTrkPV_,"nsimTrkPV[nsimPV]/I");
207 +      pvtxtree_->Branch("simx",&simx_,"simx[nsimPV]/D");
208 +      pvtxtree_->Branch("simy",&simy_,"simy[nsimPV]/D");
209 +      pvtxtree_->Branch("simz",&simz_,"simz[nsimPV]/D");
210 +      pvtxtree_->Branch("simptsq",&simptsq_,"simptsq[nsimPV]/D");
211 +
212 +      // For pvtxs with all the tracks
213 +      pvtxtree_->Branch("nrecPV_mct",&nrecPV_mct_,"nrecPV_mct/I");
214 +      pvtxtree_->Branch("deltax_mct",&deltax_mct_,"deltax_mct[nrecPV_mct]/D");
215 +      pvtxtree_->Branch("deltay_mct",&deltay_mct_,"deltay_mct[nrecPV_mct]/D");
216 +      pvtxtree_->Branch("deltaz_mct",&deltaz_mct_,"deltaz_mct[nrecPV_mct]/D");
217 +      pvtxtree_->Branch("pullx_mct",&pullx_mct_,"pullx_mct[nrecPV_mct]/D");
218 +      pvtxtree_->Branch("pully_mct",&pully_mct_,"pully_mct[nrecPV_mct]/D");
219 +      pvtxtree_->Branch("pullz_mct",&pullz_mct_,"pullz_mct[nrecPV_mct]/D");
220 +      // For pvtxs with splittracks1
221 +      pvtxtree_->Branch("nrecPV_spl1_mct",&nrecPV_spl1_mct_,"nrecPV_spl1_mct/I");
222 +      pvtxtree_->Branch("deltax_spl1_mct",&deltax_spl1_mct_,"deltax_spl1_mct[nrecPV_spl1_mct]/D");
223 +      pvtxtree_->Branch("deltay_spl1_mct",&deltay_spl1_mct_,"deltay_spl1_mct[nrecPV_spl1_mct]/D");
224 +      pvtxtree_->Branch("deltaz_spl1_mct",&deltaz_spl1_mct_,"deltaz_spl1_mct[nrecPV_spl1_mct]/D");
225 +      pvtxtree_->Branch("pullx_spl1_mct",&pullx_spl1_mct_,"pullx_spl1_mct[nrecPV_spl1_mct]/D");
226 +      pvtxtree_->Branch("pully_spl1_mct",&pully_spl1_mct_,"pully_spl1_mct[nrecPV_spl1_mct]/D");
227 +      pvtxtree_->Branch("pullz_spl1_mct",&pullz_spl1_mct_,"pullz_spl1_mct[nrecPV_spl1_mct]/D");
228 +      // For pvtxs with splittracks1
229 +      pvtxtree_->Branch("nrecPV_spl2_mct",&nrecPV_spl2_mct_,"nrecPV_spl2_mct/I");
230 +      pvtxtree_->Branch("deltax_spl2_mct",&deltax_spl2_mct_,"deltax_spl2_mct[nrecPV_spl2_mct]/D");
231 +      pvtxtree_->Branch("deltay_spl2_mct",&deltay_spl2_mct_,"deltay_spl2_mct[nrecPV_spl2_mct]/D");
232 +      pvtxtree_->Branch("deltaz_spl2_mct",&deltaz_spl2_mct_,"deltaz_spl2_mct[nrecPV_spl2_mct]/D");
233 +      pvtxtree_->Branch("pullx_spl2_mct",&pullx_spl2_mct_,"pullx_spl2_mct[nrecPV_spl2_mct]/D");
234 +      pvtxtree_->Branch("pully_spl2_mct",&pully_spl2_mct_,"pully_spl2_mct[nrecPV_spl2_mct]/D");
235 +      pvtxtree_->Branch("pullz_spl2_mct",&pullz_spl2_mct_,"pullz_spl2_mct[nrecPV_spl2_mct]/D");
236 +    }
237 +  }
238 +  
239 +  setRootStyle();
240 +
241 +  //========================================
242 +  // Booking histograms
243 +  //========================================
244 +  
245 +  // Create a root file for histograms
246 +  theFile = new TFile(histoFileName_.c_str(), "RECREATE");
247 +  // make diretories
248 +  theFile->mkdir("Summary");
249 +  theFile->cd();
250 +  theFile->mkdir("Others");
251 +  theFile->cd();
252 +  if (analyze_) {
253 +    theFile->mkdir("Results");
254 +    theFile->cd();
255 +  }
256 +
257 +  // Book Histograms:
258 +  h_pvtrk   = new PVHistograms();
259 +  h_pixvtx  = new PVHistograms();
260 +  h_misc    = new PVHistograms();
261 +  h_summary = new PVHistograms();
262 +  h_others  = new PVHistograms();
263 +
264 +  for(int i=0;i<3;i++) {  
265 +    if(i == 0) h_pvtrk->Init("pvTrk");
266 +    else {
267 +      stringstream ss;
268 +      ss << i;
269 +      h_pvtrk->Init("pvTrk", ss.str(),"spl");
270 +    }
271 +  }
272 +  h_pixvtx->Init("pixVtx");
273 +  h_misc->Init("misc");
274 +
275 +  // Book MC only plots
276    if (!realData_) {
277 <    h_deltax       = fs->make<TH1D>("deltax","x-distance (Rec - Sim)",800,-0.04,0.04);
278 <    h_deltay       = fs->make<TH1D>("deltay","y-distance (Rec - Sim)",800,-0.04,0.04);
279 <    h_deltaz       = fs->make<TH1D>("deltaz","z-distance (Rec - Sim)",800,-0.04,0.04);
280 <    h_pullx        = fs->make<TH1D>("pullx","x-pull (Rec - Sim)",1000,-25.,25.);
281 <    h_pully        = fs->make<TH1D>("pully","y-pull (Rec - Sim)",1000,-25.,25.);
282 <    h_pullz        = fs->make<TH1D>("pullz","z-pull (Rec - Sim)",1000,-25.,25.);
283 <
284 <    // Summary of analysis:
285 <    if (analyze_) {
286 <      h_resx_nTrk    = fs->make<TH2D>("resx_ntrk","x-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
287 <      h_resx_nTrk->SetMarkerStyle(21);
288 <      h_resx_nTrk->SetMarkerColor(4);
289 <      h_resx_nTrk->GetXaxis()->SetTitle("Num of tracks");
290 <      h_resx_nTrk->GetYaxis()->SetTitle("#mum");
291 <      h_resy_nTrk    = fs->make<TH2D>("resy_ntrk","y-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
292 <      h_resy_nTrk->SetMarkerStyle(21);
293 <      h_resy_nTrk->SetMarkerColor(4);
294 <      h_resy_nTrk->GetXaxis()->SetTitle("Num of tracks");
110 <      h_resy_nTrk->GetYaxis()->SetTitle("#mum");
111 <      h_resz_nTrk    = fs->make<TH2D>("resz_ntrk","z-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
112 <      h_resz_nTrk->SetMarkerStyle(21);
113 <      h_resz_nTrk->SetMarkerColor(4);
114 <      h_resz_nTrk->GetXaxis()->SetTitle("Num of tracks");
115 <      h_resz_nTrk->GetYaxis()->SetTitle("#mum");
116 <      h_pullx_nTrk   = fs->make<TH2D>("pullx_ntrk","x-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
117 <      h_pullx_nTrk->SetMarkerStyle(21);
118 <      h_pullx_nTrk->SetMarkerColor(4);
119 <      h_pullx_nTrk->SetBit(TH1::kCanRebin);
120 <      h_pullx_nTrk->GetXaxis()->SetTitle("Num of tracks");
121 <      h_pully_nTrk   = fs->make<TH2D>("pully_ntrk","y-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
122 <      h_pully_nTrk->SetMarkerStyle(21);
123 <      h_pully_nTrk->SetMarkerColor(4);
124 <      h_pully_nTrk->SetBit(TH1::kCanRebin);
125 <      h_pully_nTrk->GetXaxis()->SetTitle("Num of tracks");
126 <      h_pullz_nTrk   = fs->make<TH2D>("pullz_ntrk","z-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
127 <      h_pullz_nTrk->SetMarkerStyle(21);
128 <      h_pullz_nTrk->SetMarkerColor(4);
129 <      h_pullz_nTrk->SetBit(TH1::kCanRebin);
130 <      h_pullz_nTrk->GetXaxis()->SetTitle("Num of tracks");
277 >    h_gen = new PVHistograms();
278 >    h_gen->Init("generator");
279 >  }
280 >  if (analyze_) h_ana = new PVHistograms();
281 >
282 >  //Book histograms sensitive to data/mc
283 >  for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {
284 >    string suffix;
285 >    edm::LogInfo("Debug")<<"datamode = "<< *it<<endl;
286 >    switch(*it) {
287 >    case 0: suffix = "";
288 >      break;
289 >    case 1: suffix = "_spl1_mct";
290 >      break;
291 >    case 2: suffix = "_spl2_mct";      
292 >      break;
293 >    case 3: suffix = "_mct";
294 >      break;
295      }
296 +    h_summary->Init("summary",suffix);
297 +    
298 +    // Book residual, error and pull histograms for each nTrk bin
299      for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
300        stringstream ss;
301        ss << ntrk;
302 <      string suffix = ss.str();
136 <      h["resx_"+suffix]  = fs->make<TH1D> (TString("deltax_"+suffix),"x-distance (Rec - Sim)",100,-0.02,0.02);
137 <      h["resx_"+suffix]->GetXaxis()->SetTitle("cm");
138 <      h["resy_"+suffix]  = fs->make<TH1D> (TString("deltay_"+suffix),"y-distance (Rec - Sim)",100,-0.02,0.02);
139 <      h["resy_"+suffix]->GetXaxis()->SetTitle("cm");
140 <      h["resz_"+suffix]  = fs->make<TH1D> (TString("deltaz_"+suffix),"z-distance (Rec - Sim)",100,-0.02,0.02);
141 <      h["resz_"+suffix]->GetXaxis()->SetTitle("cm");
142 <      h["pullx_"+suffix] = fs->make<TH1D> (TString("pullx_"+suffix),"x-pull (Rec - Sim)",100,-10.,10.);
143 <      h["pully_"+suffix] = fs->make<TH1D> (TString("pully_"+suffix),"y-pull (Rec - Sim)",100,-10.,10.);
144 <      h["pullz_"+suffix] = fs->make<TH1D> (TString("pullz_"+suffix),"z-pull (Rec - Sim)",100,-10.,10.);
145 <      suffix.clear();
302 >      h_others->Init("others",suffix,ss.str());
303      }
147  }
148 }
304  
305 +    // Book residual and pull histograms only when analyzeOntheFly is enabled
306 +    if(analyze_) h_ana->InitA("analysis",suffix,"nTrk",nTrkMin_,nTrkMax_);
307 +    suffix.clear();
308 +  } // End of Book histograms sensitive to data/mc    
309 +  
310 +
311 + }
312  
313   PVStudy::~PVStudy()
314   {
315  
316    // do anything here that needs to be done at desctruction time
317    // (e.g. close files, deallocate resources etc.)
318 +  theFile->cd();
319 +  theFile->cd("Summary");
320 +  h_pvtrk->Save();
321 +  h_pixvtx->Save();
322 +  h_misc->Save();
323 +  h_summary->Save();
324 +  if (!realData_)
325 +    h_gen->Save();
326 +  if (analyze_) {
327 +    theFile->cd();
328 +    theFile->cd("Results");
329 +    h_ana->Save();
330 +  }
331 +  theFile->cd();
332 +  theFile->cd("Others");
333 +  h_others->Save();
334  
335 +  theFile->Close();
336   }
337  
338 <
338 > void PVStudy::setRootStyle() {
339 >  //
340 >  gROOT->SetStyle("Plain");
341 >  gStyle->SetFillColor(1);
342 >  gStyle->SetOptDate();
343 >  //   gStyle->SetOptStat(1111110);
344 >  //   gStyle->SetOptFit(0111);
345 >  //   gStyle->SetPadTickX(1);
346 >  //   gStyle->SetPadTickY(1);
347 >  gStyle->SetMarkerSize(0.5);
348 >  gStyle->SetMarkerStyle(8);
349 >  gStyle->SetGridStyle(3);
350 >  //gStyle->SetPadGridX(1);
351 >  //gStyle->SetPadGridY(1);
352 >  gStyle->SetPaperSize(TStyle::kA4);
353 >  gStyle->SetStatW(0.25);             // width of statistics box; default is 0.19
354 >  //   gStyle->SetStatH(0.10);             // height of statistics box; default is 0.1
355 >  gStyle->SetStatFormat("6.4g");      // leave default format for now
356 >  gStyle->SetTitleSize(0.055, "");    // size for pad title; default is 0.02
357 >  gStyle->SetLabelSize(0.03, "XYZ");  // size for axis labels; default is 0.04
358 >  gStyle->SetStatFontSize(0.08);      // size for stat. box
359 >  gStyle->SetTitleFont(32, "XYZ");    // times-bold-italic font (p. 153) for axes
360 >  gStyle->SetTitleFont(32, "");       // same for pad title
361 >  gStyle->SetLabelFont(32, "XYZ");    // same for axis labels
362 >  gStyle->SetStatFont(32);            // same for stat. box
363 >  gStyle->SetLabelOffset(0.006, "Y"); // default is 0.005
364 >  //
365 >  return;
366 > }
367   //
368   // member functions
369   //
370   std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC, std::string suffix="")
371   {
372 <  std::vector<PVStudy::simPrimaryVertex> simpv;
372 > std::vector<PVStudy::simPrimaryVertex> simpv;
373    const HepMC::GenEvent* evt=evtMC->GetEvent();
374    if (evt) {
375 <    if(verbose_) {
376 <      cout << "process id " << evt->signal_process_id()<<endl;
377 <      cout << "signal process vertex " << ( evt->signal_process_vertex() ?
378 <                                            evt->signal_process_vertex()->barcode() : 0 )   <<endl;
172 <      cout << "number of vertices " << evt->vertices_size() << endl;
173 <    }
375 >    edm::LogInfo("SimPVs") << "[getSimPVs] process id " << evt->signal_process_id()<<endl;
376 >    edm::LogInfo("SimPVs") << "[getSimPVs] signal process vertex " << ( evt->signal_process_vertex() ?
377 >                                                                        evt->signal_process_vertex()->barcode() : 0 ) <<endl;
378 >    edm::LogInfo("SimPVs") << "[getSimPVs] number of vertices " << evt->vertices_size() << endl;
379  
380 <    int idx=0;
380 >    int idx=0; int npv=0;
381      for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
382 <        vitr != evt->vertices_end(); ++vitr )
383 <      { // loop for vertex ...
384 <        HepMC::FourVector pos = (*vitr)->position();
385 <        //HepLorentzVector pos = (*vitr)->position();
386 <        if (pos.t()>0) { continue;} // ?
387 <        
388 <        bool hasMotherVertex=false;
389 <        //      if(verbose_) cout << "mothers" << endl;
390 <        for ( HepMC::GenVertex::particle_iterator mother  = (*vitr)->particles_begin(HepMC::parents);
391 <              mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
392 <          HepMC::GenVertex * mv=(*mother)->production_vertex();
393 <          if (mv) {hasMotherVertex=true;}
394 <          //      if(verbose_) {
395 <          //        cout << "\t";
191 <          //        (*mother)->print();
192 <          //      }
193 <        }
194 <
195 <        if(hasMotherVertex) {continue;}
196 <
197 <        // could be a new vertex, check  all primaries found so far to avoid multiple entries
198 <        const double mm2cm=0.1;
199 <        simPrimaryVertex sv(pos.x()*mm2cm,pos.y()*mm2cm,pos.z()*mm2cm); // sim unit mm, rec unit cm
200 <        simPrimaryVertex *vp=NULL;  // will become non-NULL if a vertex is found and then point to it
201 <        for(vector<simPrimaryVertex>::iterator v0=simpv.begin();
202 <            v0!=simpv.end(); v0++){
203 <          if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
204 <            vp=&(*v0);
205 <            break;
382 >        vitr != evt->vertices_end(); ++vitr ) { // loop for vertex ...
383 >      HepMC::FourVector pos = (*vitr)->position();
384 >      //HepLorentzVector pos = (*vitr)->position();
385 >
386 >      // t component of PV:
387 >      for ( HepMC::GenVertex::particle_iterator mother  = (*vitr)->particles_begin(HepMC::parents);
388 >            mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
389 >        //        edm::LogInfo("SimPVs") << "Status = " << (*mother)->status() << endl;
390 >        HepMC::GenVertex * mv=(*mother)->production_vertex();
391 >        if( ((*mother)->status() == 3) && (!mv)) {
392 >          //        edm::LogInfo("SimPVs") << "npv= " << npv << endl;
393 >          if (npv == 0) {
394 >            h_gen->Fill1d("genPart_cT", pos.t()); // mm
395 >            h_gen->Fill1d("genPart_T", pos.t()/299.792458); // ns
396            }
397 +          npv++;
398          }
399 +      }
400 +      //       if (pos.t()>0) { continue;} // for 22X when t of PV was not smeared
401 +        
402 +      bool hasMotherVertex=false;
403 +      if (verbose_) cout << "[getSimPVs] mothers of vertex[" << ++idx << "]: " << endl;
404 +      for ( HepMC::GenVertex::particle_iterator mother  = (*vitr)->particles_begin(HepMC::parents);
405 +            mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
406 +        HepMC::GenVertex * mv=(*mother)->production_vertex();
407 +        // if (verbose_) cout << "Status = " << (*mother)->status() << endl;
408 +        if (mv) {
409 +          hasMotherVertex=true;
410 +          if(!verbose_) break; //if verbose_, print all particles of gen vertices
411 +        }
412 +        if(verbose_) {
413 +          cout << "\t";
414 +          (*mother)->print();
415 +        }
416 +      }
417  
418 <        if(!vp){
419 <          // this is a new vertex
420 <          if(verbose_) cout << "this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
421 <          simpv.push_back(sv);
422 <          vp=&simpv.back();
423 <        }else{
424 <          if(verbose_) cout << "this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
425 <        }
426 <        vp->genVertex.push_back((*vitr)->barcode());
427 <        // collect final state descendants
428 <        for ( HepMC::GenVertex::particle_iterator
429 <                daughter  = (*vitr)->particles_begin(HepMC::descendants);
430 <              daughter != (*vitr)->particles_end(HepMC::descendants);
431 <              ++daughter ) {
432 <          if (isFinalstateParticle(*daughter)){
433 <            if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
434 <                 == vp->finalstateParticles.end()){
435 <              vp->finalstateParticles.push_back((*daughter)->barcode());
436 <              HepMC::FourVector m=(*daughter)->momentum();
437 <              // the next four lines used to be "vp->ptot+=m;" in the days of CLHEP::HepLorentzVector
438 <              // but adding FourVectors seems not to be foreseen
439 <              vp->ptot.setPx(vp->ptot.px()+m.px());
440 <              vp->ptot.setPy(vp->ptot.py()+m.py());
441 <              vp->ptot.setPz(vp->ptot.pz()+m.pz());
442 <              vp->ptot.setE(vp->ptot.e()+m.e());
443 <              vp->ptsq+=(m.perp())*(m.perp());
444 <              if ( (m.perp()>0.8) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
445 <                vp->nGenTrk++;
446 <              }
418 >      if(hasMotherVertex) continue;
419 >
420 >      // could be a new vertex, check  all primaries found so far to avoid multiple entries
421 >      const double mm2cm=0.1;
422 >      simPrimaryVertex sv(pos.x()*mm2cm,pos.y()*mm2cm,pos.z()*mm2cm); // sim unit mm, rec unit cm
423 >      simPrimaryVertex *vp=NULL;  // will become non-NULL if a vertex is found and then point to it
424 >      for(vector<simPrimaryVertex>::iterator v0=simpv.begin();
425 >          v0!=simpv.end(); v0++){
426 >        if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
427 >          vp=&(*v0);
428 >          break;
429 >        }
430 >      }
431 >
432 >      if(!vp){
433 >        // this is a new vertex
434 >        edm::LogInfo("SimPVs") << "[getSimPVs] this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
435 >        simpv.push_back(sv);
436 >        vp=&simpv.back();
437 >      }else{
438 >        edm::LogInfo("SimPVs") << "[getSimPVs] this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
439 >      }
440 >      vp->genVertex.push_back((*vitr)->barcode());
441 >      // collect final state descendants
442 >      for ( HepMC::GenVertex::particle_iterator daughter  = (*vitr)->particles_begin(HepMC::descendants);
443 >            daughter != (*vitr)->particles_end(HepMC::descendants);
444 >            ++daughter ) {
445 >        if (isFinalstateParticle(*daughter)){
446 >          if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
447 >               == vp->finalstateParticles.end()){
448 >            vp->finalstateParticles.push_back((*daughter)->barcode());
449 >            HepMC::FourVector m=(*daughter)->momentum();
450 >            // the next four lines used to be "vp->ptot+=m;" in the days of CLHEP::HepLorentzVector
451 >            // but adding FourVectors seems not to be foreseen
452 >            vp->ptot.setPx(vp->ptot.px()+m.px());
453 >            vp->ptot.setPy(vp->ptot.py()+m.py());
454 >            vp->ptot.setPz(vp->ptot.pz()+m.pz());
455 >            vp->ptot.setE(vp->ptot.e()+m.e());
456 >            vp->ptsq+=(m.perp())*(m.perp());
457 >            if ( (m.perp()>0.8) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
458 >              vp->nGenTrk++;
459              }
460            }
461          }
462 <        idx++;
463 <      }
462 >      }//loop MC vertices daughters
463 >    }//loop MC vertices
464    }
465    return simpv;
466   }
# Line 272 | Line 493 | std::vector<PVStudy::simPrimaryVertex> P
493                ptsq+=(*daughter)->momentum().perp()*(*daughter)->momentum().perp();
494              }
495            }
496 <          primary =  ( gv->position().t()==0);
496 >          //primary =  ( gv->position().t()==0 );
497 >          primary = true;
498 >          h_gen->Fill1d("genPart_cT", gv->position().t()); // mm
499 >          h_gen->Fill1d("genPart_T", gv->position().t()/299.792458); // ns
500 >
501            //resonance= ( gp->mother() && isResonance(gp->mother()));  // in CLHEP/HepMC days
502            // no more mother pointer in the improved HepMC GenParticle
503            resonance= ( isResonance(*(gp->production_vertex()->particles_in_const_begin())));
# Line 319 | Line 544 | void
544   PVStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
545   {
546    using namespace edm;
547 <  using reco::TrackCollection;
548 <
549 <  ftree_->SetBranchAddress("residual",&fres);
550 <  ftree_->SetBranchAddress("error",&ferror);
551 <  ftree_->SetBranchAddress("nTrk",&fntrk);
552 <
553 <  Handle<TrackCollection> tracks;
554 <  iEvent.getByLabel(tracksLabel_,tracks);
547 >  using namespace reco;
548 >  
549 >  edm::LogInfo("Debug")<<"[PVStudy]"<<endl;
550 >  //=======================================================
551 >  // Initialize Root-tuple variables if needed
552 >  //=======================================================
553 >  //if(saventuple_)
554 >    SetVarToZero();
555 >  
556 >  //=======================================================
557 >  // Track accessors
558 >  //=======================================================
559 >  //trackCollection
560 >  static const reco::TrackCollection s_empty_trackColl;
561 >  const reco::TrackCollection *trackColl = &s_empty_trackColl;
562 >  edm::Handle<reco::TrackCollection> trackCollectionHandle;
563 >  iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle);
564 >  if( iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle)) {
565 >    trackColl = trackCollectionHandle.product();
566 >  } else {
567 >    edm::LogInfo("Debug") << "[PVStudy] trackCollection cannot be found -> using empty collection of same type." <<endl;
568 >  }
569 >  //splitTrackCollection1
570 >  static const reco::TrackCollection s_empty_splitTrackColl1;
571 >  const reco::TrackCollection *splitTrackColl1 = &s_empty_splitTrackColl1;
572 >  edm::Handle<reco::TrackCollection> splitTrackCollection1Handle;
573 >  iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle);
574 >  if( iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle)) {
575 >    splitTrackColl1 = splitTrackCollection1Handle.product();
576 >  } else {
577 >    edm::LogInfo("Debug") << "[PVStudy] splitTrackCollection1 cannot be found -> using empty collection of same type." <<endl;
578 >  }
579 >  //splitTrackCollection2
580 >  static const reco::TrackCollection s_empty_splitTrackColl2;
581 >  const reco::TrackCollection *splitTrackColl2 = &s_empty_splitTrackColl2;
582 >  edm::Handle<reco::TrackCollection> splitTrackCollection2Handle;
583 >  iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle);
584 >  if( iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle)) {
585 >    splitTrackColl2 = splitTrackCollection2Handle.product();
586 >  } else {
587 >   edm::LogInfo("Debug") << "[PVStudy] splitTrackCollection2 cannot be found -> using empty collection of same type." <<endl;
588 >  }
589 >  
590 >  //=======================================================
591 >  // PVTX accessors
592 >  //=======================================================
593 >  //vertexCollection
594 >  static const reco::VertexCollection s_empty_vertexColl;
595 >  const reco::VertexCollection *vertexColl = &s_empty_vertexColl;
596 >  edm::Handle<reco::VertexCollection> vertexCollectionHandle;
597 >  iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle);
598 >  if( iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle)) {
599 >    vertexColl = vertexCollectionHandle.product();
600 >  } else {
601 >   edm::LogInfo("Debug") << "[PVStudy] vertexCollection cannot be found -> using empty collection of same type." <<endl;
602 >  }
603 >  //splitVertexCollection1
604 >  static const reco::VertexCollection s_empty_splitVertexColl1;
605 >  const reco::VertexCollection *splitVertexColl1 = &s_empty_splitVertexColl1;
606 >  edm::Handle<reco::VertexCollection> splitVertexCollection1Handle;
607 >  iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle);
608 >  if( iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle)) {
609 >    splitVertexColl1 = splitVertexCollection1Handle.product();
610 >  } else {
611 >    edm::LogInfo("Debug") << "[PVStudy] splitVertexCollection1 cannot be found -> using empty collection of same type." <<endl;
612 >  }
613 >  //splitVertexCollection2
614 >  static const reco::VertexCollection s_empty_splitVertexColl2;
615 >  const reco::VertexCollection *splitVertexColl2 = &s_empty_splitVertexColl2;
616 >  edm::Handle<reco::VertexCollection> splitVertexCollection2Handle;
617 >  iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle);
618 >  if( iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle)) {
619 >    splitVertexColl2 = splitVertexCollection2Handle.product();
620 >  } else {
621 >    edm::LogInfo("Debug") << "[PVStudy] splitVertexCollection2 cannot be found -> using empty collection of same type." <<endl;
622 >  }
623  
624 <  Handle<reco::VertexCollection> recVtxs;
625 <  iEvent.getByLabel(vtxSample_, recVtxs);
624 >  
625 >  //=======================================================
626 >  // GET pixelVertices
627 >  //=======================================================
628 >  static const reco::VertexCollection s_empty_pixelVertexColl;
629 >  const reco::VertexCollection *pixelVertexColl = &s_empty_pixelVertexColl;
630 >  edm::Handle<reco::VertexCollection>  pixelVertexCollectionHandle;
631 >  iEvent.getByLabel(pixelVertexCollectionTag_, pixelVertexCollectionHandle);
632 >  if( iEvent.getByLabel(pixelVertexCollectionTag_, pixelVertexCollectionHandle)) {
633 >    pixelVertexColl = pixelVertexCollectionHandle.product();
634 >  } else {
635 >    edm::LogInfo("Debug") << "[PVStudy] pixelVertexCollection cannot be found. -> using empty collection of same type." <<endl;
636 >  }
637  
638 +  //=======================================================
639 +  // BeamSpot accessors
640 +  //=======================================================
641 +
642 +  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
643 +  iEvent.getByLabel(bsSrc,recoBeamSpotHandle);
644 +  reco::BeamSpot bs = *recoBeamSpotHandle;    
645 +  const Point beamSpot = recoBeamSpotHandle.isValid() ? Point(recoBeamSpotHandle->x0(), recoBeamSpotHandle->y0(), recoBeamSpotHandle->z0()) : Point(0, 0, 0);
646 +  
647 +  edm::LogInfo("Debug")<<"[PVStudy] End accessing the track, beamSpot, primary vertex and pixelvertices collections"<<endl;
648 +  
649 +  //=======================================================
650 +  // MC simvtx accessor
651 +  //=======================================================
652    if (!realData_) {
653 <    Handle<SimVertexContainer> simVtxs;
653 >    edm::Handle<SimVertexContainer> simVtxs;
654      iEvent.getByLabel( simG4_, simVtxs);
655      
656 <    Handle<SimTrackContainer> simTrks;
656 >    edm::Handle<SimTrackContainer> simTrks;
657      iEvent.getByLabel( simG4_, simTrks);
658    }
659  
660 <  if(verbose_) printRecVtxs(recVtxs);
661 <
660 >  //=======================================================
661 >  // GET PDT
662 >  //=======================================================
663    try{
664      iSetup.getData(pdt);
665    }catch(const Exception&){
666 <    cout << "Some problem occurred with the particle data table. This may not work !." <<endl;
666 >    edm::LogInfo("Debug") << "[PVStudy] Some problem occurred with the particle data table. This may not work !." <<endl;
667    }
668  
669 <  if (!realData_) {
670 <    bool MC=false;
671 <    Handle<HepMCProduct> evtMC;
672 <    iEvent.getByLabel("generator",evtMC);
673 <    if (!evtMC.isValid()) {
674 <      MC=false;
675 <      if(verbose_){
676 <        cout << "no HepMCProduct found"<< endl;
677 <      }
678 <    } else {
679 <      if(verbose_){
680 <        cout << "generator HepMCProduct found"<< endl;
681 <      }
682 <      MC=true;
683 <    }
684 <
685 <    if(MC){
686 <      // make a list of primary vertices:
687 <      std::vector<simPrimaryVertex> simpv;
688 <      simpv=getSimPVs(evtMC,"");
689 <      //     simpv=getSimPVs(evtMC, simVtxs, simTrks);
690 <    
691 <      int nsimtrk=0;
692 <      int nrectrk=0;
374 <      for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
375 <          vsim!=simpv.end(); vsim++){
376 <        nsimtrk+=vsim->nGenTrk;
377 <        // look for a matching reconstructed vertex
378 <        vsim->recVtx=NULL;
379 <        for(reco::VertexCollection::const_iterator vrec=recVtxs->begin();
380 <            vrec!=recVtxs->end(); ++vrec){
381 <          nrectrk=vrec->tracksSize();
382 <          if(verbose_){
383 <            cout << "sim primary vertex x = " << vsim->x << "; y = " << vsim->y << "; z = " << vsim->z <<  endl;
384 <            cout << "Is matched? " << (matchVertex(*vsim,*vrec)?"Yes":"No") << endl;
385 <          }
386 <          if ( matchVertex(*vsim,*vrec) ){
387 <            // if the matching critera are fulfilled, accept the rec-vertex that is closest in z
388 <            if(    ((vsim->recVtx) && (fabs(vsim->recVtx->position().z()-vsim->z)>fabs(vrec->z()-vsim->z)))
389 <                   || (!vsim->recVtx) )
390 <              {
391 <                vsim->recVtx=&(*vrec);
392 <              }
393 <          }
394 <        }
395 <        if (vsim->recVtx){
396 <          if(verbose_) {
397 <            cout <<"primary matched " << vsim->x << " " << vsim->y << " " << vsim->z <<  endl;
398 <          }
399 <          fres[0] = vsim->recVtx->x()-vsim->x;
400 <          fres[1] = vsim->recVtx->y()-vsim->y;
401 <          fres[2] = vsim->recVtx->z()-vsim->z;
402 <          ferror[0] = vsim->recVtx->xError();
403 <          ferror[1] = vsim->recVtx->yError();
404 <          ferror[2] = vsim->recVtx->zError();
405 <          fntrk = nrectrk;
406 <          ftree_->Fill();
407 <
408 <          h_deltax->Fill( vsim->recVtx->x()-vsim->x );
409 <          h_deltay->Fill( vsim->recVtx->y()-vsim->y );
410 <          h_deltaz->Fill( vsim->recVtx->z()-vsim->z );
411 <          h_pullx->Fill( (vsim->recVtx->x()-vsim->x )/vsim->recVtx->xError() );
412 <          h_pully->Fill( (vsim->recVtx->y()-vsim->y )/vsim->recVtx->yError() );
413 <          h_pullz->Fill( (vsim->recVtx->z()-vsim->z )/vsim->recVtx->zError() );
414 <          pvinfo.push_back(PVStudy::PVInfo(res(vsim->recVtx->x()-vsim->x,
415 <                                               vsim->recVtx->y()-vsim->y,
416 <                                               vsim->recVtx->z()-vsim->z),
417 <                                           error(vsim->recVtx->xError(),
418 <                                                 vsim->recVtx->yError(),
419 <                                                 vsim->recVtx->zError()),
420 <                                           nrectrk));
421 <          // Fill histo according to its track multiplicity
422 <          fillHisto(res(vsim->recVtx->x()-vsim->x,
423 <                        vsim->recVtx->y()-vsim->y,
424 <                        vsim->recVtx->z()-vsim->z),
425 <                    pull((vsim->recVtx->x()-vsim->x )/vsim->recVtx->xError(),
426 <                         (vsim->recVtx->y()-vsim->y )/vsim->recVtx->yError(),
427 <                         (vsim->recVtx->z()-vsim->z )/vsim->recVtx->zError()),
428 <                    nrectrk);
429 <        }
430 <        else{  // no rec vertex found for this simvertex
431 <          if(verbose_) {
432 <            cout <<"primary not found " << vsim->x << " " << vsim->y << " " << vsim->z << " nGenTrk=" << vsim->nGenTrk << endl;
433 <          }
669 >  //=======================================================
670 >  // Apply event cleaning for firstcoll data and MC
671 >  //=======================================================
672 >  if(applyEvtClean_) {
673 >    // =====Select on the trigger bits
674 >    edm::ESHandle<L1GtTriggerMenu> menuRcd;
675 >    iSetup.get<L1GtTriggerMenuRcd>().get(menuRcd) ;
676 >    const L1GtTriggerMenu* menu = menuRcd.product();
677 >    edm::Handle< L1GlobalTriggerReadoutRecord > gtRecord;
678 >    iEvent.getByLabel( edm::InputTag("gtDigis"), gtRecord);
679 >    
680 >    bool isTechBit0 = false;
681 >    bool isTechBit40 = false;
682 >    bool isBeamHalo = false;
683 >
684 >    TechnicalTriggerWord tw = gtRecord->technicalTriggerWord();
685 >    if ( ! tw.empty() ) {
686 >      // loop over dec. bit to get total rate (no overlap)
687 >      for ( int i = 0; i < 64; ++i ) {
688 >        if ( tw[i] ) {
689 >          //cout<<"technical number "<<i<<"  "<<endl;
690 >          if (i == 0) isTechBit0  = true;
691 >          if (i < 40 && i > 35) isBeamHalo = true;   // The beamHalo bits are 36-39
692 >          if (i == 40 || i == 41) isTechBit40 = true;
693          }
694        }
695 +    } // =====End select on the trigger bits
696 +    // =====Loop over the trackColl to tet the fraction of HighPurity tracks
697 +    int nTracks = 0;
698 +    int nHighPurityTracks=0;
699 +    double fHighPurity=0;
700 +
701 +    for (unsigned int i=0; i<trackCollectionHandle->size(); i++, nTracks++) {
702 +      TrackRef tkref(trackCollectionHandle,i);
703 +      if( (tkref->qualityMask() & 4 ) == 4) ++nHighPurityTracks;
704      }
705 <  }// With MC
706 <  
707 <  // Loop RecVtxs and find matched SimVtxs
708 <  for(reco::VertexCollection::const_iterator v=recVtxs->begin();
709 <      v!=recVtxs->end(); ++v){
710 <    try {
711 <      for(reco::Vertex::trackRef_iterator t = v->tracks_begin();
712 <          t!=v->tracks_end(); t++) {
713 <        // illegal charge
714 <        if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
715 <          //      h_trkPtPV->Fill(0.);
716 <        }
717 <        else {
718 <          h_trkPtPV->Fill((**t).pt());
719 <        }
705 >    if(nTracks>0)
706 >      fHighPurity =  double(nHighPurityTracks)/double(nTracks);
707 >    if(verbose_)
708 >      cout<<"fraction of HighPurity track is "<<fHighPurity<<endl;  
709 >    
710 >    // Apply the event selection for MC events
711 >    if(realData_) {
712 >      if(!isTechBit40 || isBeamHalo || (fHighPurity<0.2&&nTracks>10) || vertexColl->begin()->isFake()) {
713 >        glob_runno_ = iEvent.id().run();
714 >        glob_evtno_ = iEvent.id().event();
715 >        glob_ls_   = iEvent.luminosityBlock();
716 >        glob_bx_  = iEvent.bunchCrossing();  
717 >        return;
718 >      }
719 >      else {
720 >        // TechBit 0, beamHalo are not simulted in MC
721 >        if ( !isTechBit0 || !isTechBit40 || isBeamHalo || (fHighPurity<0.2&&nTracks>10) || vertexColl->begin()->isFake()) return;
722        }
723      }
724 <    catch (...) {
725 <      // exception thrown when trying to use linked track
726 <      //          h_trkPtPV->Fill(0.);
724 >  } // End of Apply event cleaning cuts for firstcoll data
725 >
726 >
727 >
728 >  //=======================================================
729 >  // Fill trackparameters of the input tracks to pvtx fitter
730 >  //=======================================================
731 >  edm::LogInfo("Debug")<<"[PVStudy] Start filling track parameters of the input tracks to pvtx fitter."<<endl;
732 >  //fillTrackHisto(const reco::TrackCollection *trackColl, int datatype, const Point & bs)
733 >  // datatype: unsplittracks (0); splittracks1 (1);  splittracks2 (2);
734 >  fillTrackHisto(trackColl, 0, beamSpot);
735 >  fillTrackHisto(splitTrackColl1, 1, beamSpot);
736 >  fillTrackHisto(splitTrackColl2, 2, beamSpot);
737 >  edm::LogInfo("Debug")<<"[PVStudy] End filling track parameters of the input tracks to pvtx fitter."<<endl;
738 >
739 >
740 >  //=======================================================
741 >  // Fill pixelVertices related histograms
742 >  //=======================================================
743 >  nrecPV_pxlpvtx_ = int (pixelVertexColl->size());
744 >  if(pixelVertexColl->size()>0 && pixelVertexColl->begin()->isValid() && !(pixelVertexColl->begin()->isFake())) {
745 >    //fillTrackHistoInPV(const reco::VertexCollection *vertexColl, int datatype, bool fillHisto, bool fillNtuple, const Point & bs) {
746 >    fillTrackHistoInPV(pixelVertexColl, 4, false, true, beamSpot);
747 >    h_pixvtx->Fill1d("dzErr_pxlpvtx", pixelVertexColl->begin()->zError());    
748 >    // Get the dZ error of the tracks in the leading pixelVertexColl
749 >    for(reco::Vertex::trackRef_iterator t = (pixelVertexColl->begin())->tracks_begin();
750 >        t!= (pixelVertexColl->begin())->tracks_end(); t++) {
751 >      
752 >      if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
753 >        //        h_pvtrk->Fill1d("trkPtPV", 0.);
754 >      }
755 >      else
756 >        h_pixvtx->Fill1d("trkdzErr_pxlpvtx", (**t).dzError());
757      }
758 <    
759 <    h_nTrkPV->Fill(v->tracksSize());
760 <    
758 >    // Fill the track->dz() in the PV difference from first pixelpvtx
759 >    for(reco::Vertex::trackRef_iterator t = (vertexColl->begin())->tracks_begin();
760 >        t!= (vertexColl->begin())->tracks_end(); t++) {
761 >      // illegal charge
762 >      if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
763 >        //        h_pvtrk->Fill1d("trkPtPV", 0.);
764 >      }
765 >      else {
766 >        if(pixelVertexColl->size()>0) {
767 >          h_pixvtx->Fill1d("trkdz_pxlpvtxdz", (**t).dz() -  pixelVertexColl->begin()->z());
768 >          h_pixvtx->Fill1d("trkdz_pxlpvtxdz_pxlpvtxdzerr", fabs((**t).dz() -  pixelVertexColl->begin()->z())/pixelVertexColl->begin()->zError());
769 >          h_pixvtx->Fill1d("trkdz_pxlpvtxdz_trkdzerr", fabs((**t).dz() -  pixelVertexColl->begin()->z())/(**t).dzError());
770 >          h_pixvtx->Fill1d("trkdzErr_pvtx", (**t).dzError());
771 >        }
772 >      }
773 >    }
774 >  }
775 >
776 >  //=======================================================
777 >  // Fill number of reconstructed vertices
778 >  //=======================================================
779 >
780 >  edm::LogInfo("Debug")<<"[PVStudy] Printing vertexCollection: "<<endl;
781 >  edm::LogInfo("Debug")<<"[PVStudy] Printing splitVertexCollection1: "<<endl;
782 >  edm::LogInfo("Debug")<<"[PVStudy] Printing splitVertexCollection2: "<<endl;
783 >  edm::LogInfo("Debug")<<"[PVStudy] Start filling pvtx parameters."<<endl;
784 >  if (verbose_) {
785 >    printRecVtxs(vertexCollectionHandle);
786 >    printRecVtxs(splitVertexCollection1Handle);
787 >    printRecVtxs(splitVertexCollection2Handle);
788    }
789 +  nrecPV_ = int (vertexColl->size());
790 +  nrecPV1_spl_ = int (splitVertexColl1->size());
791 +  nrecPV2_spl_ = int (splitVertexColl2->size());
792 +
793 +  h_pvtrk->Fill1d("nrecPV", nrecPV_);
794 +  h_pvtrk->Fill1d("nrecPV1_spl", nrecPV1_spl_);
795 +  h_pvtrk->Fill1d("nrecPV2_spl", nrecPV2_spl_);
796 +  h_misc->Fill1d("nrecPVDiff", double(nrecPV1_spl_)-double(nrecPV2_spl_));
797    
463  h_nTrk->Fill(tracks->size());
798    
799 <  for(TrackCollection::const_iterator itTrack = tracks->begin();
800 <      itTrack != tracks->end();                      
801 <      ++itTrack) {
802 <    int charge = 0;
803 <    charge = itTrack->charge();
804 <    h_trkPt->Fill(itTrack->pt());
799 > //=================================================================
800 >  // Fill track parameter ntuple/hist for tracks used in recoVertices
801 >  //=================================================================
802 >
803 >  //fillTrackHistoInPV(const reco::VertexCollection *vertexColl, int datatype, bool fillHisto, bool fillNtuple, const Point & bs) {
804 >  if(vertexColl->size() > 0 && vertexColl->begin()->isValid() && !(vertexColl->begin()->isFake()))
805 >    fillTrackHistoInPV(vertexColl, 0, true, true, beamSpot);
806 >  
807 >  if(splitVertexColl1->size() > 0 && splitVertexColl1->begin()->isValid() && !(splitVertexColl1->begin()->isFake()))
808 >    fillTrackHistoInPV(splitVertexColl1, 1, false, true, beamSpot);
809 >
810 >  if(splitVertexColl2->size() > 0 && splitVertexColl2->begin()->isValid() && !(splitVertexColl2->begin()->isFake()))
811 >    fillTrackHistoInPV(splitVertexColl2, 2, false, true, beamSpot);
812 >
813 >  //=======================================================
814 >  // Compare offlinePrimaryVertices with pixelVertices
815 >  //=======================================================
816 >  if( (pixelVertexColl->size()>0 && pixelVertexColl->begin()->isValid() && !(pixelVertexColl->begin()->isFake()))
817 >      && (vertexColl->size()>0 && vertexColl->begin()->isValid() && !(vertexColl->begin()->isFake())) ) {    
818 >    h_pixvtx->Fill1d("nrecPV_minus_nrecPxlPV", double (nrecPV_ - nrecPV_pxlpvtx_));
819 >    // difference in reconstructed position of the leading pvtx
820 >    //edm::LogInfo("Debug")<<"recx_[0] = "<< recx_[0] << "recx_pxlpvtx_[0] = "<< recx_pxlpvtx_[0]<<endl;  
821 >    //edm::LogInfo("Debug")<<"recy_[0] = "<< recy_[0] << "recy_pxlpvtx_[0] = "<< recy_pxlpvtx_[0]<<endl;
822 >    h_pixvtx->Fill1d("recxPV_minus_recxPxlPV", recx_[0] - recx_pxlpvtx_[0]);
823 >    h_pixvtx->Fill1d("recyPV_minus_recyPxlPV", recy_[0] - recy_pxlpvtx_[0]);
824 >    h_pixvtx->Fill1d("reczPV_minus_reczPxlPV", recz_[0] - recz_pxlpvtx_[0]);
825    }
826    
827 +  
828 + //==========================================================
829 +  // Fill secondary/primary min separations for multi-vertices
830 +  //==========================================================
831 +  
832 +  if(vertexColl->size()>1 && vertexColl->begin()->isValid() && !(vertexColl->begin()->isFake()) ) {  
833 +
834 +    double min_xsep = 9999.0;
835 +    double min_ysep = 9999.0;
836 +    double min_zsep = 9999.0;    
837 +    double min_xsep_sign = 9999.0;
838 +    double min_ysep_sign = 9999.0;
839 +    double min_zsep_sign = 9999.0;
840 +    double min_ntrksep = 9999.0;
841 +    double min_sumpt2sep = 9999999.0;
842 +    
843 +    edm::LogInfo("Debug")<<"[PVStudy] leading pvtx z = "<< vertexColl->begin()->z() <<endl;
844 +
845 +    // Looping through the secondary vertices to find the mininum separation to the primary
846 +    for(reco::VertexCollection::const_iterator v=vertexColl->begin() + 1 ;
847 +        v!=vertexColl->end(); ++v) {  
848 +      if(v->isValid() && ! v->isFake()) {
849 +        // xsep
850 +        if(fabs(v->x()- vertexColl->begin()->x())<min_xsep)
851 +          min_xsep = fabs(v->x()- vertexColl->begin()->x());
852 +        // ysep
853 +        if(fabs(v->y()- vertexColl->begin()->y())<min_ysep)
854 +          min_ysep = fabs(v->y()- vertexColl->begin()->y());
855 +        // zsep
856 +        if(fabs(v->z()- vertexColl->begin()->z())<min_zsep)
857 +          min_zsep = fabs(v->z()- vertexColl->begin()->z());
858 +        // xsep_sign
859 +        double xsep_sign = fabs(v->x()- vertexColl->begin()->x())/max(v->xError(), vertexColl->begin()->xError());
860 +        if(xsep_sign < min_xsep_sign)
861 +          min_xsep_sign = xsep_sign;
862 +        // ysep_sign
863 +        double ysep_sign = fabs(v->y()- vertexColl->begin()->y())/max(v->yError(), vertexColl->begin()->yError());
864 +        if(ysep_sign < min_ysep_sign)
865 +          min_ysep_sign = ysep_sign;
866 +        // zsep_sign
867 +        double zsep_sign = fabs(v->z()- vertexColl->begin()->z())/max(v->zError(), vertexColl->begin()->zError());
868 +        if(zsep_sign < min_zsep_sign)
869 +          min_zsep_sign = zsep_sign;
870 +        // ntrksep
871 +        if( (double(vertexColl->begin()->tracksSize()) - double(v->tracksSize())) < min_ntrksep)
872 +          min_ntrksep = double(vertexColl->begin()->tracksSize()) - double(v->tracksSize());
873 +        // sumpt2sep
874 +        if(fabs(sumPtSquared(*v) - sumPtSquared(*(vertexColl->begin()))) < min_sumpt2sep)
875 +          min_sumpt2sep = fabs(sumPtSquared(*v) - sumPtSquared(*(vertexColl->begin())));
876 +      }
877 +    }
878 +    h_misc->Fill1d("min_xsep", min_xsep);
879 +    h_misc->Fill1d("min_ysep", min_ysep);
880 +    h_misc->Fill1d("min_zsep", min_zsep);
881 +    h_misc->Fill1d("min_xsep_sign", min_xsep_sign);
882 +    h_misc->Fill1d("min_ysep_sign", min_ysep_sign);
883 +    h_misc->Fill1d("min_zsep_sign", min_zsep_sign);
884 +    h_misc->Fill1d("min_ntrksep", min_ntrksep);
885 +    h_misc->Fill1d("min_sumpt2sep", min_sumpt2sep);
886 +
887 +    min_zsep_ = min_zsep;
888 +    min_ntrksep_ = min_ntrksep;
889 +    min_sumpt2sep_ = min_sumpt2sep;
890 +
891 +
892 +  } // End of  if(vertexColl->size()>1) {
893 +  
894 +  edm::LogInfo("Debug")<<"[PVStudy] End filling pvtx parameters."<<endl;
895 +
896 +  
897 +    
898 +  //=======================================================
899 +  //           PrimaryVertex Matching
900 +  // 1. In z  |z1-z2|< zsigncut * max(sigmaz1, sigmaz2)
901 +  // 2. |(nTrkPV1 - nTrkPV2)/(nTrkPV1+nTrkPV2)|<ntrkdiffcut_
902 +  // Assume the first match is the primary vertex,
903 +  // since vertexColl are sorted in decreasing order of sum(pT)
904 +  //=======================================================
905 +  
906 +  edm::LogInfo("Debug")<<"[PVStudy] matching pvtxs "<<endl;
907 +  reco::VertexCollection s_empty_matchedVertexColl1;
908 +  reco::VertexCollection *matchedVertexColl1 = &s_empty_matchedVertexColl1;
909 +  matchedVertexColl1->reserve(splitVertexColl1->size());
910 +  reco::VertexCollection s_empty_matchedVertexColl2;
911 +  reco::VertexCollection *matchedVertexColl2 = &s_empty_matchedVertexColl2;
912 +  matchedVertexColl2->reserve(splitVertexColl2->size());
913 +  
914 +  bool stopmatching = false;
915 +  for (size_t ivtx = 0; ivtx<splitVertexCollection1Handle->size() && !stopmatching; ++ivtx) {
916 +    reco::VertexRef recvtx1(splitVertexCollection1Handle, ivtx);
917 +    if( !(recvtx1->isValid()) || recvtx1->isFake()) break;
918 +    for (size_t jvtx = ivtx; jvtx < splitVertexCollection2Handle->size(); ++jvtx) {    
919 +      //------------------------------------------------------------------------
920 +      //== If only considering matching the first vertex of the splitVertexColl
921 +      //------------------------------------------------------------------------
922 +      if (ivtx!=0 || jvtx!=0) { stopmatching = true; break; }
923 +      reco::VertexRef recvtx2(splitVertexCollection2Handle, jvtx);
924 +      if( !(recvtx2->isValid()) || recvtx2->isFake()) break;
925 +      edm::LogInfo("Debug")<<"[PVStudy] Matching splitVertexColl1: #  "<< ivtx<< " and splitVertexColl1: # "<<jvtx<<endl;
926 +      edm::LogInfo("Debug")<<"[PVStudy] recvtx1->z() = " << recvtx1->z() << "+/- "<< recvtx1->zError() << "." << endl;
927 +      edm::LogInfo("Debug")<<"[PVStudy] recvtx2->z() = " << recvtx2->z() << "+/- "<< recvtx2->zError() << "." << endl;
928 +      
929 +      double twovtxsig = (recvtx1->z()-recvtx2->z())/max(recvtx1->zError(), recvtx2->zError());
930 +      h_misc->Fill1d("twovtxzsign", twovtxsig);
931 +      if(matchVertex(recvtx1, recvtx2, zsigncut_)) {
932 +        edm::LogInfo("Debug")<<"[PVStudy] The two splitted vertices match in Z. "<<endl;
933 +
934 +        int nTrkPV1 = recvtx1->tracksSize();
935 +        int nTrkPV2 = recvtx2->tracksSize();    
936 +        double ntrkreldiff = double(nTrkPV1-nTrkPV2)/double(nTrkPV1+nTrkPV2);
937 +        h_misc->Fill1d("nTrkPVDiff", nTrkPV1-nTrkPV2);
938 +        h_misc->Fill1d("nTrkPVRelDiff", ntrkreldiff);
939 +        if(fabs(ntrkreldiff)<ntrkdiffcut_) {    
940 +          edm::LogInfo("Debug")<<"[PVStudy] (nTrkPV1-nTrkPV2)/(nTrkPV1+nTrkPV2) = " << ntrkreldiff<<endl;
941 +
942 +          matchedVertexColl1->push_back(*recvtx1);
943 +          matchedVertexColl2->push_back(*recvtx2);
944 +          stopmatching = true; // stop the matching when the first match is found!
945 +          break;
946 +        }
947 +        else
948 +          edm::LogInfo("Debug")<<"WARNING: (nTrkPV1-nTrkPV2)/(nTrkPV1+nTrkPV2) = " << ntrkreldiff<<", exceeding cut "<<ntrkdiffcut_<<endl;
949 +      }
950 +      else {
951 +        edm::LogInfo("Debug")<<"WARNING: The two reconstructed vertices do not match in z: "<<endl;
952 +        edm::LogInfo("Debug")<<"recvtx1->z() = " << recvtx1->z() << "+/- "<< recvtx1->zError() << "." << endl;
953 +        edm::LogInfo("Debug")<<"recvtx2->z() = " << recvtx2->z() << "+/- "<< recvtx2->zError() << "." << endl;  
954 +      }
955 +    }
956 +  }
957 +  edm::LogInfo("Debug")<<"[PVStudy] End matching pvtxs"<<endl;
958 +  
959 +  //=======================================================
960 +  //  Analyze matchedVertexColls
961 +  //=======================================================
962 +  edm::LogInfo("Debug")<<"[PVStudy] Begin analyzing the matchedVertexColl with size = "<< matchedVertexColl1->size()<< endl;
963 +  
964 +  // Compare the reconstructed vertex position and calculate resolution/pulls
965 +  if(matchedVertexColl1->size() != 0 && matchedVertexColl2->size() != 0) {  
966 +    //fillPvtxHisto(const reco::VertexCollection *vertexColl, int datatype, bool fillHisto, bool fillNtuple)
967 +    fillTrackHistoInPV(matchedVertexColl1, 1, true, false, beamSpot);
968 +    fillTrackHistoInPV(matchedVertexColl2, 2, true, false, beamSpot);
969 +
970 +    reco::VertexCollection::const_iterator v1;
971 +    reco::VertexCollection::const_iterator v2;
972 +    nrecPV_twovtx_ = 0;
973 +    for(v1 = matchedVertexColl1->begin(), v2 = matchedVertexColl2->begin();
974 +        v1!=matchedVertexColl1->end(), v2 != matchedVertexColl2->end();
975 +        ++v1, ++v2) {
976 +      h_misc->Fill1d("ndofPVDiff", v1->ndof() - v2->ndof());
977 +      h_misc->Fill1d("ndofPVRelDiff", (v1->ndof()-v2->ndof())/(v1->ndof()+v2->ndof()));
978 +      fres_[0] = (v1->x()-v2->x())/sqrt(2.0);
979 +      fres_[1] = (v1->y()-v2->y())/sqrt(2.0);
980 +      fres_[2] = (v1->z()-v2->z())/sqrt(2.0);
981 +      ferror_[0] = sqrt(pow(v1->xError(),2)+pow(v2->xError(),2))/sqrt(2);
982 +      ferror_[1] = sqrt(pow(v1->yError(),2)+pow(v2->yError(),2))/sqrt(2);
983 +      ferror_[2] = sqrt(pow(v1->zError(),2)+pow(v2->zError(),2))/sqrt(2);
984 +      fntrk_ =  (v1->tracksSize()+v2->tracksSize())/2;
985 +      
986 +      h_summary->Fill1d("deltax", fres_[0] );
987 +      h_summary->Fill1d("deltay", fres_[1] );
988 +      h_summary->Fill1d("deltaz", fres_[2] );
989 +      h_summary->Fill1d("pullx", fres_[0]/ferror_[0]);
990 +      h_summary->Fill1d("pully", fres_[1]/ferror_[1]);
991 +      h_summary->Fill1d("pullz", fres_[2]/ferror_[2]);
992 +      h_summary->Fill1d("errPVx", ferror_[0] );
993 +      h_summary->Fill1d("errPVy", ferror_[1] );
994 +      h_summary->Fill1d("errPVz", ferror_[2] );
995 +      pvinfo.push_back(PVStudy::PVInfo(res(fres_[0], fres_[1], fres_[2]),
996 +                                       error(ferror_[0], ferror_[1], ferror_[2]),
997 +                                       fntrk_));
998 +      // Fill histo according to its track multiplicity, set datamode = 0 for pvtx using all tracks
999 +      fillHisto(res(fres_[0], fres_[1], fres_[2]),
1000 +                error(ferror_[0], ferror_[1], ferror_[2]),
1001 +                fntrk_,0);  
1002 +      // Fill the ntuple variables
1003 +      nTrkPV1_spl_twovtx_[nrecPV_twovtx_] = v1->tracksSize();
1004 +      nTrkPV2_spl_twovtx_[nrecPV_twovtx_] = v2->tracksSize();
1005 +      sumptsq1_spl_twovtx_[nrecPV_twovtx_] = sumPtSquared(*v1);
1006 +      sumptsq2_spl_twovtx_[nrecPV_twovtx_] = sumPtSquared(*v2);
1007 +      nTrkPV_twovtx_[nrecPV_twovtx_] = fntrk_;
1008 +      deltax_twovtx_[nrecPV_twovtx_] = fres_[0];
1009 +      deltay_twovtx_[nrecPV_twovtx_] = fres_[1];
1010 +      deltaz_twovtx_[nrecPV_twovtx_] = fres_[2];
1011 +      errx_twovtx_[nrecPV_twovtx_] = ferror_[0];
1012 +      erry_twovtx_[nrecPV_twovtx_] = ferror_[1];
1013 +      errz_twovtx_[nrecPV_twovtx_] = ferror_[2];
1014 +      pullx_twovtx_[nrecPV_twovtx_] = fres_[0]/ferror_[0];
1015 +      pully_twovtx_[nrecPV_twovtx_] = fres_[1]/ferror_[1];
1016 +      pullz_twovtx_[nrecPV_twovtx_] = fres_[2]/ferror_[2];    
1017 +      nrecPV_twovtx_++;
1018 +    } // End of analyzing res/pull
1019 +
1020 +    //=======================================================
1021 +    // Fill simulated vertices
1022 +    //=======================================================
1023 +    if (!realData_) {
1024 +      bool MC=false;
1025 +      Handle<HepMCProduct> evtMC;
1026 +      iEvent.getByLabel("generator",evtMC);
1027 +      if (!evtMC.isValid()) {
1028 +        MC=false;
1029 +        edm::LogInfo("Debug") << "[PVStudy] no HepMCProduct found"<< endl;
1030 +      } else {
1031 +        edm::LogInfo("Debug") << "[PVStudy] generator HepMCProduct found"<< endl;
1032 +        MC=true;
1033 +      }
1034 +      
1035 +      if(MC){
1036 +        // make a list of primary vertices:
1037 +        std::vector<simPrimaryVertex> simpv;
1038 +        simpv=getSimPVs(evtMC,"");
1039 +        //     simpv=getSimPVs(evtMC, simVtxs, simTrks);
1040 +        h_gen->Fill1d("nsimPV", simpv.size());
1041 +        nsimPV_ = simpv.size();
1042 +        
1043 +        int isimpv = 0;
1044 +        for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
1045 +            vsim!=simpv.end(); vsim++, isimpv++){
1046 +          nsimTrkPV_[isimpv]  =vsim->nGenTrk;
1047 +          simx_[isimpv] = vsim->x;
1048 +          simy_[isimpv] = vsim->y;
1049 +          simz_[isimpv] = vsim->y;
1050 +          simptsq_[isimpv] = vsim->ptsq;
1051 +          
1052 +          //fillMCHisto((std::vector<simPrimaryVertex>::iterator vsim, int isimpv, const reco::VertexCollection *vtxColl, int datatype) {
1053 +          fillMCHisto(vsim, isimpv, splitVertexColl1, 1);
1054 +          fillMCHisto(vsim, isimpv, splitVertexColl2, 2);
1055 +          fillMCHisto(vsim, isimpv, vertexColl, 3);
1056 +        }
1057 +      } // End of  for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin()
1058 +    } // End of   if (!realData_) {
1059 +
1060 +  } // End of  if(matchedVertexColl1->size() == 1 && matchedVertexColl2->size() == 1 ) {
1061 +  else
1062 +    edm::LogInfo("Debug")<<"[PVStudy] WARNING: Cannot find matching pvtxs"<<endl;
1063 +  
1064 +  edm::LogInfo("Debug")<<"[PVStudy] End analyzing the matchedVertexColl"<<endl;
1065 +
1066 +
1067 +
1068 +  // Finally fill the ftree_
1069 +  if(saventuple_) {
1070 +    ftree_->Fill();
1071 +    pvtxtree_->Fill();
1072 +  }
1073 +  
1074   }
1075  
1076  
1077 +  
1078   // ------------ method called once each job just before starting event loop  ------------
1079   void
1080   PVStudy::beginJob()
# Line 482 | Line 1084 | PVStudy::beginJob()
1084   // ------------ method called once each job just after ending the event loop  ------------
1085   void
1086   PVStudy::endJob() {
1087 <  double sqt2 = sqrt(2.);
1088 <  if (!realData_) {
1089 <    if (verbose_) cout << "Analyzing PV info" << endl;
1090 <    for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
1091 <      if (verbose_) cout << "Fill point of ntrk = " << ntrk << endl;
1092 <      PVStudy::PVAnaInfo ipv = GetPVAnaInfo(ntrk);
1093 <      if (analyze_) {
1094 < //      if ( ipv.res_.x() > 0 ) h_resx_nTrk->Fill(ntrk,ipv.res_.x()/sqt2, 1.);
1095 < //      if ( ipv.res_.y() > 0 ) h_resy_nTrk->Fill(ntrk,ipv.res_.y()/sqt2, 1.);
1096 < //      if ( ipv.res_.z() > 0 ) h_resz_nTrk->Fill(ntrk,ipv.res_.z()/sqt2, 1.);
1097 <        if ( ipv.res_.x() > 0 ) h_resx_nTrk->Fill(ntrk,ipv.res_.x(), 1.);
1098 <        if ( ipv.res_.y() > 0 ) h_resy_nTrk->Fill(ntrk,ipv.res_.y(), 1.);
1099 <        if ( ipv.res_.z() > 0 ) h_resz_nTrk->Fill(ntrk,ipv.res_.z(), 1.);
1100 <        if ( ipv.pull_.x() > 0 ) h_pullx_nTrk->Fill(ntrk,ipv.pull_.x(), 1.);
1101 <        if ( ipv.pull_.y() > 0 ) h_pully_nTrk->Fill(ntrk,ipv.pull_.y(), 1.);
1102 <        if ( ipv.pull_.z() > 0 ) h_pullz_nTrk->Fill(ntrk,ipv.pull_.z(), 1.);
1087 >  edm::LogInfo("Analysis") << "[endJob] Analyzing PV info" << endl;
1088 >  // Looping through the datamodes available
1089 >  for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {  
1090 >    string suffix;
1091 >    edm::LogInfo("Analysis")<<"[endJob] datamode = "<< *it<<endl;
1092 >    switch(*it) {
1093 >    case 0: suffix = "";
1094 >      break;
1095 >    case 1: suffix = "_spl1_mct";
1096 >      break;
1097 >    case 2: suffix = "_spl2_mct";      
1098 >      break;
1099 >    case 3: suffix = "_mct";
1100 >      break;
1101 >    }
1102 >    suffix += "_nTrk";
1103 >    if(analyze_) {
1104 >      for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
1105 >        edm::LogInfo("Analysis") << "[endJob] ntrk = " << ntrk << endl;
1106 >        PVStudy::PVAnaInfo ipv = GetPVAnaInfo(ntrk,*it);
1107 >        if ( ipv.res_.x() > 0 ) h_ana->Fill2d("resx"+suffix, ntrk,ipv.res_.x());
1108 >        if ( ipv.res_.y() > 0 ) h_ana->Fill2d("resy"+suffix, ntrk,ipv.res_.y());
1109 >        if ( ipv.res_.z() > 0 ) h_ana->Fill2d("resz"+suffix, ntrk,ipv.res_.z());
1110 >        if ( ipv.pull_.x() > 0 ) h_ana->Fill2d("pullx"+suffix, ntrk,ipv.pull_.x());
1111 >        if ( ipv.pull_.y() > 0 ) h_ana->Fill2d("pully"+suffix, ntrk,ipv.pull_.y());
1112 >        if ( ipv.pull_.z() > 0 ) h_ana->Fill2d("pullz"+suffix, ntrk,ipv.pull_.z());
1113        }
1114      }
1115 +    suffix.clear();
1116 +  }
1117 +  if(saventuple_) {
1118 +    file_->cd();
1119 +    ftree_->Write();
1120 +    pvtxtree_->Write();
1121 +    file_->Close();
1122    }
1123 <  file_->cd();
1124 <  ftree_->Write();
506 <  file_->Close();
1123 >  
1124 >
1125   }
1126  
1127 + // Match a recovertex with a simvertex
1128 + // ? Should the cut be parameter dependent?
1129 + // cut can be within n sigma of the vertex.zerror()
1130   bool PVStudy::matchVertex(const PVStudy::simPrimaryVertex & vsim,
1131                            const reco::Vertex & vrec)
1132   {
1133 <  return (fabs(vsim.z-vrec.z())<0.0500); // =500um
1133 >  if(vrec.isFake() || !vrec.isValid()) return false;
1134 >  else
1135 >    return (fabs(vsim.z-vrec.z())<0.0500); // =500um
1136   }
1137  
1138 + // Match two reconstructed vertices
1139 + bool PVStudy::matchVertex( const reco::VertexRef & vrec1,
1140 +                           const reco::VertexRef & vrec2,
1141 +                           int zsigncut)
1142 + {
1143 +  double cut = zsigncut*max(vrec1->zError(), vrec2->zError());
1144 +  edm::LogInfo("Debug")<<"[matchVertex] The matching criteria in z is "<<cut<<endl;
1145 +  return (fabs(vrec1->z()-vrec2->z())<cut);
1146 + }
1147 +
1148 +
1149   bool PVStudy::isResonance(const HepMC::GenParticle * p){
1150    double ctau=(pdt->particle( abs(p->pdg_id()) ))->lifetime();
1151 <  if(verbose_) cout << "isResonance   " << p->pdg_id() << " " << ctau << endl;
1151 >  edm::LogInfo("Debug") << "[isResonance] isResonance   " << p->pdg_id() << " " << ctau << endl;
1152    return  ctau >0 && ctau <1e-6;
1153   }
1154  
# Line 583 | Line 1217 | void PVStudy::printSimTrks(const Handle<
1217    }
1218   }
1219  
1220 < void PVStudy::fillHisto(res r, pull p, int ntk){
1220 > void PVStudy::fillHisto(res r, error er, int ntk, int datamode){
1221    stringstream ss;
1222    ss << ntk;
1223 <  string suffix = ss.str();
1223 >  string suffix;
1224 >  if(datamode == 0 )  suffix = "_" + ss.str();
1225 >  if(datamode == 1 )  suffix = "_spl1_mct_" + ss.str();
1226 >  if(datamode == 2 )  suffix = "_spl2_mct_" + ss.str();
1227 >  if(datamode == 3 )  suffix = "_mct_" + ss.str();
1228 >  
1229    if (ntk < nTrkMin_ || ntk > nTrkMax_ ) return;
1230    // Fill histograms of res and pull of ntk
1231 <  h["resx_"+suffix]->Fill(r.x());
1232 <  h["resy_"+suffix]->Fill(r.y());
1233 <  h["resz_"+suffix]->Fill(r.z());
1234 <  h["pullx_"+suffix]->Fill(p.x());
1235 <  h["pully_"+suffix]->Fill(p.y());
1236 <  h["pullz_"+suffix]->Fill(p.z());
1231 >  h_others->Fill1d("deltax"+suffix, r.x());
1232 >  h_others->Fill1d("deltay"+suffix, r.y());
1233 >  h_others->Fill1d("deltaz"+suffix, r.z());
1234 >  h_others->Fill1d("pullx"+suffix, r.x()/er.x());
1235 >  h_others->Fill1d("pully"+suffix, r.y()/er.y());
1236 >  h_others->Fill1d("pullz"+suffix, r.z()/er.z());  
1237 >  h_others->Fill1d("errPVx"+suffix, er.x());
1238 >  h_others->Fill1d("errPVy"+suffix, er.y());
1239 >  h_others->Fill1d("errPVz"+suffix, er.z());
1240   }
1241  
1242 < PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo(int ntk) {
1242 >
1243 > PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo(int ntk, int datamode) {
1244    map<int,double> data;
1245    data.clear();
1246 <  double fpar[2];
1246 >  double fpar[2] = {-999,-999};
1247    double cm2um = 10000;
1248    stringstream ss;
1249    ss << ntk;
1250 <  string suffix = ss.str();
1251 <
1250 >  string suffix = ss.str();
1251 >  if(datamode == 0 )  suffix = "_" + suffix;
1252 >  if(datamode == 1 )  suffix = "_spl1_mct_" + suffix;
1253 >  if(datamode == 2 )  suffix = "_spl2_mct_" + suffix;
1254 >  if(datamode == 3 )  suffix = "_mct_" + suffix;
1255 >  
1256    if(analyze_) {
1257      for ( int i = 0; i < 6; ++i) {
611      cout << "Here" << endl;
1258        switch (i) {
1259        case 0:
1260 <        fit(h["resx_"+suffix], fpar);
1260 >        if (!h_others->ReadHisto1D("deltax"+suffix)) break;
1261 >        fit(h_others->ReadHisto1D("deltax"+suffix), fpar);
1262          data[i] = fpar[0]*cm2um;
1263          break;
1264        case 1:
1265 <        fit(h["resy_"+suffix], fpar);
1265 >        if (!h_others->ReadHisto1D("deltay"+suffix)) break;
1266 >        fit(h_others->ReadHisto1D("deltay"+suffix), fpar);
1267          data[i] = fpar[0]*cm2um;
1268          break;
1269        case 2:
1270 <        fit(h["resz_"+suffix], fpar);
1270 >        if (!h_others->ReadHisto1D("deltaz"+suffix)) break;
1271 >        fit(h_others->ReadHisto1D("deltaz"+suffix), fpar);
1272          data[i] = fpar[0]*cm2um;
1273          break;
1274        case 3:
1275 <        fit(h["pullx_"+suffix], fpar);
1275 >        if (!h_others->ReadHisto1D("pullx"+suffix)) break;
1276 >        fit(h_others->ReadHisto1D("pullx"+suffix), fpar);
1277          data[i] = fpar[0];
1278          break;
1279        case 4:
1280 <        fit(h["pully_"+suffix], fpar);
1280 >        if (!h_others->ReadHisto1D("pully"+suffix)) break;
1281 >        fit(h_others->ReadHisto1D("pully"+suffix), fpar);
1282          data[i] = fpar[0];
1283          break;
1284        case 5:
1285 <        fit(h["pullz_"+suffix], fpar);
1285 >        if (!h_others->ReadHisto1D("pullz"+suffix)) break;
1286 >        fit(h_others->ReadHisto1D("pullz"+suffix), fpar);
1287          data[i] = fpar[0];
1288          break;
1289        }
1290 <      if (verbose_ && data[i] > 0) cout << "data[" << i << "] = " << data[i] << (i<3?" micro m":" ") << endl;
1290 >      if (data[i] > 0) edm::LogInfo("Analysis") << "[Analysis] data[" << i << "] = " << data[i] << (i<3?" micro m":" ") << endl;
1291      }
1292    }
1293    else
# Line 650 | Line 1302 | PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo
1302                              ntk);
1303   }
1304  
1305 < void PVStudy::fit(TH1 *&hdist, double data[]){
1305 >
1306 > void PVStudy::fit(TH1 *hdist, double fitPars[]){
1307    TAxis *axis0 = hdist->GetXaxis();
1308    int nbin = axis0->GetLast();
1309    double nOF = hdist->GetBinContent(nbin+1);
1310    double nUF = hdist->GetBinContent(0);
1311 <  if ( verbose_ ){
1312 <    cout << "Last bin = " << nbin;
1313 <    cout << "; hdist: Overflow Entries = " << nOF;
1314 <    cout << "; Underflow Entries = " << nUF;
1315 <    cout << "; hdist->GetEntries() = " << hdist->GetEntries() << endl;
1316 <  }
1317 <  if (hdist->GetEntries() - nOF - nUF >= 5) { // FIXME: for reasonable Gaussian fit
1318 <    hdist->Fit("gaus","Q");
1319 <    TF1 *fgaus = hdist->GetFunction("gaus");
1320 <    if (verbose_) cout << "got function" << endl;
1321 <    data[0] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
1311 >  //   double fitRange = axis0->GetBinUpEdge(nbin);
1312 >  // double fitRange = axis0->GetXmax();
1313 >  double fitRange = 2*hdist->GetRMS();
1314 >  double sigMax[2] = {0.,0.};
1315 >
1316 >  edm::LogInfo("Analysis") << "[Fit] Last bin = " << nbin
1317 >                           << "; hdist: Overflow Entries = " << nOF
1318 >                           << "; Underflow Entries = " << nUF
1319 >                           << "; hdist->GetEntries() = " << hdist->GetEntries()
1320 >                           << "; fitting range = " << -fitRange << " to " << fitRange << endl;
1321 >
1322 >  if (hdist->GetEntries() - nOF - nUF >= 10) { // FIXME: for reasonable Gaussian fit
1323 >    for (int bi = 0; bi < nbin; bi++) {
1324 >      if ( (axis0->GetBinLowEdge(bi) < 0) && (hdist->GetBinContent(bi) > 0) ) {
1325 >        sigMax[0] = axis0->GetBinLowEdge(bi);
1326 >        if ( abs(sigMax[0]) > abs(sigMax[1]) ) sigMax[1] = abs(sigMax[0]);
1327 >      }
1328 >      if ( (axis0->GetBinLowEdge(bi) >= 0) && (hdist->GetBinContent(bi) > 0) ) {
1329 >        sigMax[0] = axis0->GetBinUpEdge(bi);
1330 >        if ( abs(sigMax[0]) > abs(sigMax[1]) ) sigMax[1] = abs(sigMax[0]);
1331 >      }
1332 >    }
1333 >    //edm::LogInfo("Analysis") << "[Fit] Fit sigMax = " << sqrt(2.)*sigMax[1] << endl;
1334 >
1335 >    TF1 *fgaus = new TF1("fgaus","gaus",-fitRange, fitRange);
1336 >    fgaus->SetParameter(1, 0.);
1337 >    fgaus->SetParLimits(1, -fitRange/10., fitRange/10.);
1338 >    fgaus->SetParLimits(2, 0., sqrt(2.)*sigMax[1]);
1339 >    fgaus->SetLineColor(4);
1340 >    hdist->Fit(fgaus,"QLRM");
1341 >    gPad->Update();
1342 >    TPaveStats *s = (TPaveStats*) hdist->GetListOfFunctions()->FindObject("stats");
1343 >    s->SetOptStat(1111111);
1344 >    s->SetOptFit(0111);
1345 >    gPad->Update();
1346 >    //edm::LogInfo("Analysis") << "[Fit] got function" << endl;
1347 >    fitPars[0] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
1348 >  }
1349 >  else
1350 >    fitPars[0] = -999;
1351 > }
1352 >
1353 >
1354 > void PVStudy::fillTrackHisto(const reco::TrackCollection *trackColl, int datatype, const Point & bs) {
1355 >  string suffix;
1356 >  suffix = ""; // for unsplit tracks
1357 >  if(datatype == 1) suffix = "1_spl"; // for splittracks 1
1358 >  if(datatype == 2) suffix = "2_spl"; // for splittracks 2
1359 >  
1360 >  h_pvtrk->Fill1d("nTrk"+suffix, trackColl->size());
1361 >  for(reco::TrackCollection::const_iterator itTrack = trackColl->begin();
1362 >      itTrack != trackColl->end();                      
1363 >      ++itTrack) {
1364 >    h_pvtrk->Fill1d("trkPt"+suffix, itTrack->pt());
1365 >    h_pvtrk->Fill1d("trkDxy"+suffix, itTrack->dxy());
1366 >    h_pvtrk->Fill1d("trkDxyCorr"+suffix, itTrack->dxy(bs));
1367 >    h_pvtrk->Fill1d("trkDz"+suffix, itTrack->dz());
1368 >    h_pvtrk->Fill1d("trkEta"+suffix, itTrack->eta());
1369 >    h_pvtrk->Fill1d("trkPhi"+suffix, itTrack->phi());
1370 >  }
1371 > }
1372 >
1373 > void PVStudy::fillTrackHistoInPV(const reco::VertexCollection *vertexColl, int datatype, bool fillHisto, bool fillNtuple, const Point & bs) {
1374 >  string suffix;
1375 >  suffix = ""; // for unsplit tracks
1376 >  if(datatype == 1) suffix = "1_spl"; // for splittracks 1
1377 >  if(datatype == 2) suffix = "2_spl"; // for splittracks 2
1378 >  int ivtx = 0;
1379 >  for(reco::VertexCollection::const_iterator v=vertexColl->begin();
1380 >      v!=vertexColl->end(); ++v, ++ivtx) {  
1381 >    if(v->isFake()) continue;
1382 >    if(fillNtuple) {
1383 >      if(datatype == 0) {
1384 >        nTrkPV_[ivtx] = v->tracksSize();        
1385 >        sumptsq_[ivtx] = sumPtSquared(*v);
1386 >        isValid_[ivtx] = v->isValid();
1387 >        isFake_[ivtx] = v->isFake();    
1388 >        recx_[ivtx] = v->x();
1389 >        recy_[ivtx] = v->y();
1390 >        recz_[ivtx] = v->z();
1391 >        recx_err_[ivtx] = v->xError();
1392 >        recy_err_[ivtx] = v->yError();
1393 >        recz_err_[ivtx] = v->zError();
1394 >      }
1395 >      if(datatype == 1) {
1396 >        nTrkPV1_spl_[ivtx] = v->tracksSize();  
1397 >        sumptsq1_spl_[ivtx] = sumPtSquared(*v);        
1398 >        isValid1_spl_[ivtx] = v->isValid();
1399 >        isFake1_spl_[ivtx] = v->isFake();      
1400 >        recx1_spl_[ivtx] = v->x();
1401 >        recy1_spl_[ivtx] = v->y();
1402 >        recz1_spl_[ivtx] = v->z();
1403 >        recx1_err_spl_[ivtx] = v->xError();
1404 >        recy1_err_spl_[ivtx] = v->yError();
1405 >        recz1_err_spl_[ivtx] = v->zError();
1406 >
1407 >      }
1408 >      if(datatype == 2) {
1409 >        nTrkPV2_spl_[ivtx] = v->tracksSize();  
1410 >        sumptsq2_spl_[ivtx] = sumPtSquared(*v);        
1411 >        isValid2_spl_[ivtx] = v->isValid();
1412 >        isFake2_spl_[ivtx] = v->isFake();
1413 >        recx2_spl_[ivtx] = v->x();
1414 >        recy2_spl_[ivtx] = v->y();
1415 >        recz2_spl_[ivtx] = v->z();
1416 >        recx2_err_spl_[ivtx] = v->xError();
1417 >        recy2_err_spl_[ivtx] = v->yError();
1418 >        recz2_err_spl_[ivtx] = v->zError();
1419 >      }
1420 >      if(datatype == 4) { // for pixelVertices
1421 >        nTrkPV_pxlpvtx_[ivtx] = v->tracksSize();        
1422 >        sumptsq_pxlpvtx_[ivtx] = sumPtSquared(*v);      
1423 >        isValid_pxlpvtx_[ivtx] = v->isValid();
1424 >        isFake_pxlpvtx_[ivtx] = v->isFake();
1425 >        recx_pxlpvtx_[ivtx] = v->x();
1426 >        recy_pxlpvtx_[ivtx] = v->y();
1427 >        recz_pxlpvtx_[ivtx] = v->z();
1428 >        recx_err_pxlpvtx_[ivtx] = v->xError();
1429 >        recy_err_pxlpvtx_[ivtx] = v->yError();
1430 >        recz_err_pxlpvtx_[ivtx] = v->zError();
1431 >      }
1432 >    }
1433 >  }
1434 >  // For histogram only fill the leading pvtx parameters
1435 >  if(fillHisto) {
1436 >    h_pvtrk->Fill1d("nTrkPV"+suffix, vertexColl->begin()->tracksSize());
1437 >    h_pvtrk->Fill1d("ndofPV"+suffix, vertexColl->begin()->ndof());
1438 >    int nHWTrkPV_ = 0;
1439 >    
1440 >    try {
1441 >      for(reco::Vertex::trackRef_iterator t = vertexColl->begin()->tracks_begin();
1442 >          t!=vertexColl->begin()->tracks_end(); t++) {
1443 >        // illegal charge
1444 >        if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
1445 >          //      h_pvtrk->Fill1d("trkPtPV", 0.);
1446 >        }
1447 >        else {
1448 >          h_pvtrk->Fill1d("trkPtPV"+suffix, (**t).pt());
1449 >          h_pvtrk->Fill1d("trkDxyPV"+suffix, (**t).dxy());  
1450 >          h_pvtrk->Fill1d("trkDxyCorrPV"+suffix, (**t).dxy(bs));
1451 >          h_pvtrk->Fill1d("trkDzPV"+suffix, (**t).dz());
1452 >          h_pvtrk->Fill1d("trkEtaPV"+suffix, (**t).eta());
1453 >          h_pvtrk->Fill1d("trkPhiPV"+suffix, (**t).phi());
1454 >          
1455 >          if(vertexColl->begin()->trackWeight(*t) > 0.5)
1456 >            nHWTrkPV_++;
1457 >        }
1458 >      }
1459 >    }
1460 >    catch (...) {
1461 >      // exception thrown when trying to use linked track
1462 >      //          h_pvtrk->Fill1d("trkPtPV", 0.);
1463 >    }
1464 >    h_pvtrk->Fill1d("nHWTrkPV"+suffix, nHWTrkPV_);
1465 >  }
1466 >  
1467 >  
1468 > }
1469 >
1470 > void PVStudy::fillMCHisto(std::vector<simPrimaryVertex>::iterator vsim, int isimpv, const reco::VertexCollection *vtxColl, int datatype)
1471 > {
1472 >  std::string suffix;
1473 >  // Set the vertexColl and histogram suffix according to datamode
1474 >  if (datatype == 1) {
1475 >    suffix = "_spl1_mct";
1476 >    nrecPV_spl1_mct_ = 0;  
1477 >  }
1478 >  if (datatype == 2) {
1479 >    suffix = "_spl2_mct";
1480 >    nrecPV_spl2_mct_ = 0;    
1481 >  }
1482 >  if (datatype == 3) {
1483 >    suffix = "_mct";
1484 >    nrecPV_mct_ = 0;  
1485    }
1486 +  
1487 +  //========================================================
1488 +  //  look for a matching reconstructed vertex in vertexColl
1489 +  //========================================================        
1490 +
1491 +  for(reco::VertexCollection::const_iterator vrec=vtxColl->begin();
1492 +      vrec!=vtxColl->end(); ++vrec){
1493 +    int nrectrk = vrec->tracksSize();
1494 +    vsim->recVtx=NULL;  
1495 +  
1496 +    edm::LogInfo("Debug") << "[fillMCHisto] sim primary vertex x = " << vsim->x << "; y = " << vsim->y << "; z = " << vsim->z <<  endl;
1497 +    edm::LogInfo("Debug") << "[fillMCHisto] Is matched? " << (matchVertex(*vsim,*vrec)?"Yes":"No") << endl;
1498 +
1499 +    if ( matchVertex(*vsim,*vrec)  ) {
1500 +      vsim->recVtx=&(*vrec);
1501 +      
1502 +      // if the matching critera are fulfilled, accept the rec-vertex that is closest in z
1503 +      //if(    ((vsim->recVtx) && (fabs(vsim->recVtx->position().z()-vsim->z)>fabs(vrec->z()-vsim->z)))
1504 +      //|| (!vsim->recVtx) )
1505 +      //vsim->recVtx=&(*vrec);
1506 +      //}
1507 +      edm::LogInfo("Debug") <<"[fillMCHisto] primary matched in vertexColl" << vsim->x << " " << vsim->y << " " << vsim->z <<  endl;
1508 +      
1509 +      double fres_mct[3];
1510 +      double ferror_mct[3];
1511 +      
1512 +      fres_mct[0] = vsim->recVtx->x()-vsim->x;
1513 +      fres_mct[1] = vsim->recVtx->y()-vsim->y;
1514 +      fres_mct[2] = vsim->recVtx->z()-vsim->z;
1515 +      ferror_mct[0] = vsim->recVtx->xError();
1516 +      ferror_mct[1] = vsim->recVtx->yError();
1517 +      ferror_mct[2] = vsim->recVtx->zError();
1518 +      
1519 +      h_summary->Fill1d("deltax"+suffix, fres_mct[0] );
1520 +      h_summary->Fill1d("deltay"+suffix, fres_mct[1] );
1521 +      h_summary->Fill1d("deltaz"+suffix, fres_mct[2] );
1522 +      h_summary->Fill1d("pullx"+suffix, fres_mct[0]/ferror_mct[0] );
1523 +      h_summary->Fill1d("pully"+suffix, fres_mct[1]/ferror_mct[1] );
1524 +      h_summary->Fill1d("pullz"+suffix, fres_mct[2]/ferror_mct[2] );
1525 +      h_summary->Fill1d("errPVx"+suffix, ferror_mct[0] );
1526 +      h_summary->Fill1d("errPVy"+suffix, ferror_mct[1] );
1527 +      h_summary->Fill1d("errPVz"+suffix, ferror_mct[2] );
1528 +      pvinfo.push_back(PVStudy::PVInfo(res(fres_mct[0], fres_mct[1], fres_mct[2]),
1529 +                                       error(ferror_mct[0], ferror_mct[1], ferror_mct[2]),
1530 +                                       nrectrk));
1531 +      // Fill histo according to its track multiplicity
1532 +      fillHisto(res(fres_mct[0], fres_mct[1], fres_mct[2]),
1533 +                error(ferror_mct[0], ferror_mct[1], ferror_mct[2]),
1534 +                nrectrk, datatype);
1535 +      
1536 +      if(saventuple_) {
1537 +        //Fill the values for variables in ftree_  
1538 +        if(datatype == 1) {
1539 +          nTrkPV_spl1_mct_[nrecPV_spl1_mct_] =   nrectrk;
1540 +          deltax_spl1_mct_[nrecPV_spl1_mct_] =  fres_mct[0];
1541 +          deltay_spl1_mct_[nrecPV_spl1_mct_] =  fres_mct[0];
1542 +          deltaz_spl1_mct_[nrecPV_spl1_mct_] =  fres_mct[0];
1543 +          pullx_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[0]/ferror_mct[0];  
1544 +          pully_spl1_mct_[nrecPV_spl1_mct_] = fres_mct[1]/ferror_mct[1];
1545 +          pullz_spl1_mct_[nrecPV_spl1_mct_] =  fres_mct[2]/ferror_mct[2];
1546 +          nrecPV_spl1_mct_++;
1547 +        }
1548 +        
1549 +        if(datatype == 2) {
1550 +          nTrkPV_spl2_mct_[nrecPV_spl2_mct_] = nrectrk;
1551 +          deltax_spl2_mct_[nrecPV_spl2_mct_] =  fres_mct[0];
1552 +          deltay_spl2_mct_[nrecPV_spl2_mct_] =  fres_mct[0];
1553 +          deltaz_spl2_mct_[nrecPV_spl2_mct_] =  fres_mct[0];
1554 +          pullx_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[0]/ferror_mct[0];  
1555 +          pully_spl2_mct_[nrecPV_spl2_mct_] = fres_mct[1]/ferror_mct[1];
1556 +          pullz_spl2_mct_[nrecPV_spl2_mct_] =  fres_mct[2]/ferror_mct[2];
1557 +          nrecPV_spl2_mct_++;
1558 +        }
1559 +        if(datatype == 3) {    
1560 +          nTrkPV_mct_[nrecPV_mct_] = nrectrk;
1561 +          deltax_mct_[nrecPV_mct_] =  fres_mct[0];
1562 +          deltay_mct_[nrecPV_mct_] =  fres_mct[0];
1563 +          deltaz_mct_[nrecPV_mct_] =  fres_mct[0];
1564 +          pullx_mct_[nrecPV_mct_] = fres_mct[0]/ferror_mct[0];  
1565 +          pully_mct_[nrecPV_mct_] = fres_mct[1]/ferror_mct[1];
1566 +          pullz_mct_[nrecPV_mct_] =  fres_mct[2]/ferror_mct[2];          
1567 +          nrecPV_mct_++;
1568 +        }
1569 +      } // End of  if(saventuple_) {
1570 +    } //  if ( matchVertex(*vsim,*vrec) ) {
1571 +    else {  // no rec vertex found for this simvertex
1572 +      edm::LogInfo("Debug") <<"[fillMCHisto] primary not found " << vsim->x << " " << vsim->y << " " << vsim->z << " nGenTrk=" << vsim->nGenTrk << endl;
1573 +    }
1574 +  }
1575 + }
1576 +
1577 + void PVStudy::SetVarToZero() {
1578 +  //Greg's variables
1579 +  fntrk_ = 0;
1580 +  //pvtx position (x,y,z) residual and error
1581 +  for(int i = 0; i<3;i++)    {
1582 +    fres_[i] = 0;
1583 +    ferror_[i] = 0;
1584 +  }
1585 +  
1586 +  //Yanyan's variables
1587 +  // Number of reconstructed vertices  
1588 +  nrecPV_ = 0;
1589 +  nrecPV1_spl_ = 0;
1590 +  nrecPV2_spl_ = 0;
1591 +  nrecPV_twovtx_ = 0;
1592 +  nsimPV_ = 0;
1593 +
1594 +  nrecPV_mct_ = 0;
1595 +  nrecPV_spl1_mct_ = 0;
1596 +  nrecPV_spl2_mct_ = 0;
1597 +
1598 +  // Mininum separation between the secondary pvtxes and leading pvtx
1599 +  min_zsep_ = 9999.0;
1600 +  min_ntrksep_ = 9999.0;
1601 +  min_sumpt2sep_ = -9999.0;
1602 +  
1603 +
1604 +  for (int i = 0; i < nMaxPVs_; i++) {
1605 +    // recoVertices with all tracks
1606 +    nTrkPV_[i] = 0; // Number of tracks in the pvtx    
1607 +    sumptsq_[i] = 0;
1608 +    isValid_[i] = -1;
1609 +    isFake_[i] = -1;
1610 +    recx_[i] = 0;
1611 +    recy_[i] = 0;
1612 +    recz_[i] = 0;
1613 +    recx_err_[i] = 0;
1614 +    recy_err_[i] = 0;
1615 +    recz_err_[i] = 0;
1616 +    
1617 +    // recoVertices with splitTrack1
1618 +    nTrkPV1_spl_[i] = 0; // Number of tracks in the pvtx    
1619 +    sumptsq1_spl_[i] = 0;
1620 +    isValid1_spl_[i] = -1;
1621 +    isFake1_spl_[i] = -1;
1622 +    recx1_spl_[i] = 0;
1623 +    recy1_spl_[i] = 0;
1624 +    recz1_spl_[i] = 0;
1625 +    recx1_err_spl_[i] = 0;
1626 +    recy1_err_spl_[i] = 0;
1627 +    recz1_err_spl_[i] = 0;
1628 +  
1629 +    // recoVertices with splitTrack2
1630 +    nTrkPV2_spl_[i] = 0; // Number of tracks in the pvtx  
1631 +    sumptsq2_spl_[i] = 0;
1632 +    isValid2_spl_[i] = -1;
1633 +    isFake2_spl_[i] = -1;
1634 +    recx2_spl_[i] = 0;
1635 +    recy2_spl_[i] = 0;
1636 +    recz2_spl_[i] = 0;
1637 +    recx2_err_spl_[i] = 0;
1638 +    recy2_err_spl_[i] = 0;
1639 +    recz2_err_spl_[i] = 0;
1640 +    
1641 +    //pixelVertices
1642 +    nTrkPV_pxlpvtx_[i] = 0; // Number of tracks in the pvtx  
1643 +    sumptsq_pxlpvtx_[i] = 0;
1644 +    isValid_pxlpvtx_[i] = -1;
1645 +    isFake_pxlpvtx_[i] = -1;
1646 +    recx_pxlpvtx_[i] = 0;
1647 +    recy_pxlpvtx_[i] = 0;
1648 +    recz_pxlpvtx_[i] = 0;
1649 +    recx_err_pxlpvtx_[i] = 0;
1650 +    recy_err_pxlpvtx_[i] = 0;
1651 +    recz_err_pxlpvtx_[i] = 0;
1652 +
1653 +    // matched two-vertices
1654 +    nTrkPV1_spl_twovtx_[i] = 0;
1655 +    nTrkPV2_spl_twovtx_[i] = 0;
1656 +    nTrkPV_twovtx_[i] = 0;  
1657 +    sumptsq1_spl_twovtx_[i] = 0; // Sum of pT squred in the pvtx
1658 +    sumptsq2_spl_twovtx_[i] = 0; // Sum of pT squred in the pvtx
1659 +    deltax_twovtx_[i] = 0;
1660 +    deltay_twovtx_[i] = 0;
1661 +    deltaz_twovtx_[i] = 0;
1662 +    errx_twovtx_[i] = 0;
1663 +    erry_twovtx_[i] = 0;
1664 +    errz_twovtx_[i] = 0;
1665 +    pullx_twovtx_[i] = 0;
1666 +    pully_twovtx_[i] = 0;
1667 +    pullz_twovtx_[i] = 0;
1668 +    
1669 +    //simpv
1670 +    nsimTrkPV_[i] = 0;
1671 +    simx_[i] = 0;
1672 +    simy_[i] = 0;
1673 +    simz_[i] = 0;
1674 +    simptsq_[i] = 0;
1675 +    
1676 +    // residual and pulls with mc truth required
1677 +    deltax_mct_[i] = 0;
1678 +    deltay_mct_[i] = 0;
1679 +    deltaz_mct_[i] = 0;
1680 +    pullx_mct_[i] = 0;
1681 +    pully_mct_[i] = 0;
1682 +    pullz_mct_[i] = 0;
1683 +        
1684 +    deltax_spl1_mct_[i] = 0;
1685 +    deltay_spl1_mct_[i] = 0;
1686 +    deltaz_spl1_mct_[i] = 0;
1687 +    pullx_spl1_mct_[i] = 0;
1688 +    pully_spl1_mct_[i] = 0;
1689 +    pullz_spl1_mct_[i] = 0;
1690 +    
1691 +    deltax_spl2_mct_[i] = 0;
1692 +    deltay_spl2_mct_[i] = 0;
1693 +    deltaz_spl2_mct_[i] = 0;
1694 +    pullx_spl2_mct_[i] = 0;
1695 +    pully_spl2_mct_[i] = 0;
1696 +    pullz_spl2_mct_[i] = 0;
1697 +  }
1698 + }
1699 +
1700 + double PVStudy::sumPtSquared(const reco::Vertex & v)  {
1701 +  double sum = 0.;
1702 +  double pT;
1703 +  for (reco::Vertex::trackRef_iterator it = v.tracks_begin(); it != v.tracks_end(); it++) {
1704 +    pT = (**it).pt();
1705 +    sum += pT*pT;
1706 +  }
1707 +  return sum;
1708   }
1709  
1710 < //define this as a plug-in
1711 < DEFINE_FWK_MODULE(PVStudy);
1710 >
1711 >

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines