ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVStudy.cc
Revision: 1.3
Committed: Tue Sep 8 07:15:38 2009 UTC (15 years, 8 months ago) by jengbou
Content type: text/plain
Branch: MAIN
CVS Tags: CMSSW_3_1_X_Sep08
Changes since 1.2: +140 -157 lines
Log Message:
update

File Contents

# User Rev Content
1 jengbou 1.1 // -*- C++ -*-
2     //
3     // Package: PVStudy
4     // Class: PVStudy
5     //
6     /**\class PVStudy PVStudy.cc UserCode/PVStudy/plugins/PVStudy.cc
7    
8     Description: <one line class summary>
9    
10     Implementation:
11     <Notes on implementation>
12     */
13     //
14     // Original Author: "Geng-yuan Jeng/UC Riverside"
15     // Created: Thu Aug 20 11:55:40 CDT 2009
16 jengbou 1.3 // $Id: PVStudy.cc,v 1.2 2009/09/03 19:29:38 jengbou Exp $
17 jengbou 1.1 //
18     //
19    
20    
21     // system include files
22     #include <memory>
23     #include <string>
24     #include <vector>
25     #include <iostream>
26     #include <sstream>
27    
28     // user include files
29     #include "FWCore/Framework/interface/Frameworkfwd.h"
30     #include "FWCore/Framework/interface/EDAnalyzer.h"
31    
32     #include "FWCore/Framework/interface/Event.h"
33     #include "FWCore/Framework/interface/MakerMacros.h"
34    
35     #include "FWCore/ParameterSet/interface/ParameterSet.h"
36     #include "FWCore/Utilities/interface/InputTag.h"
37     #include "DataFormats/TrackReco/interface/Track.h"
38     #include "DataFormats/TrackReco/interface/TrackFwd.h"
39     #include "FWCore/ServiceRegistry/interface/Service.h"
40     #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
41     #include "UserCode/PVStudy/interface/PVStudy.h"
42     //
43     #include "DataFormats/VertexReco/interface/Vertex.h"
44     #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
45    
46     // simulated vertices,..., add <use name=SimDataFormats/Vertex> and <../Track>
47     #include <SimDataFormats/Vertex/interface/SimVertex.h>
48     #include <SimDataFormats/Vertex/interface/SimVertexContainer.h>
49     #include <SimDataFormats/Track/interface/SimTrack.h>
50     #include <SimDataFormats/Track/interface/SimTrackContainer.h>
51    
52     //root
53     #include "TROOT.h"
54     #include "TF1.h"
55     #include "TString.h"
56     #include "TStyle.h"
57    
58    
59     using namespace std;
60    
61     PVStudy::PVStudy(const edm::ParameterSet& iConfig):
62 jengbou 1.3 nTrkMin_(10),nTrkMax_(100)
63 jengbou 1.1 {
64     simG4_ = iConfig.getParameter<edm::InputTag>( "simG4" );
65     tracksLabel_ = iConfig.getUntrackedParameter<edm::InputTag>("tracks");
66     vtxSample_ = iConfig.getUntrackedParameter<edm::InputTag>("vtxSample");
67     verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
68     realData_ = iConfig.getUntrackedParameter<bool>("realData",false);
69 jengbou 1.2 analyze_ = iConfig.getUntrackedParameter<bool>("analyzeOnTheFly",false);
70 jengbou 1.3 outputfilename_ = iConfig.getUntrackedParameter<string>("OutputFileName");
71 jengbou 1.1
72     //now do what ever initialization is needed
73 jengbou 1.3 pvinfo.clear();
74 jengbou 1.1
75 jengbou 1.3 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 jengbou 1.1 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);
90    
91     if (!realData_) {
92     h_deltax = fs->make<TH1D>("deltax","x-distance (Rec - Sim)",800,-0.04,0.04);
93     h_deltay = fs->make<TH1D>("deltay","y-distance (Rec - Sim)",800,-0.04,0.04);
94     h_deltaz = fs->make<TH1D>("deltaz","z-distance (Rec - Sim)",800,-0.04,0.04);
95     h_pullx = fs->make<TH1D>("pullx","x-pull (Rec - Sim)",1000,-25.,25.);
96     h_pully = fs->make<TH1D>("pully","y-pull (Rec - Sim)",1000,-25.,25.);
97     h_pullz = fs->make<TH1D>("pullz","z-pull (Rec - Sim)",1000,-25.,25.);
98 jengbou 1.2
99 jengbou 1.1 // Summary of analysis:
100 jengbou 1.2 if (analyze_) {
101     h_resx_nTrk = fs->make<TH2D>("resx_ntrk","x-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
102     h_resx_nTrk->SetMarkerStyle(21);
103 jengbou 1.3 h_resx_nTrk->SetMarkerColor(4);
104     h_resx_nTrk->GetXaxis()->SetTitle("Num of tracks");
105 jengbou 1.2 h_resx_nTrk->GetYaxis()->SetTitle("#mum");
106     h_resy_nTrk = fs->make<TH2D>("resy_ntrk","y-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
107     h_resy_nTrk->SetMarkerStyle(21);
108 jengbou 1.3 h_resy_nTrk->SetMarkerColor(4);
109     h_resy_nTrk->GetXaxis()->SetTitle("Num of tracks");
110 jengbou 1.2 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 jengbou 1.3 h_resz_nTrk->SetMarkerColor(4);
114     h_resz_nTrk->GetXaxis()->SetTitle("Num of tracks");
115 jengbou 1.2 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 jengbou 1.3 h_pullx_nTrk->SetMarkerColor(4);
119 jengbou 1.2 h_pullx_nTrk->SetBit(TH1::kCanRebin);
120 jengbou 1.3 h_pullx_nTrk->GetXaxis()->SetTitle("Num of tracks");
121 jengbou 1.2 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 jengbou 1.3 h_pully_nTrk->SetMarkerColor(4);
124 jengbou 1.2 h_pully_nTrk->SetBit(TH1::kCanRebin);
125 jengbou 1.3 h_pully_nTrk->GetXaxis()->SetTitle("Num of tracks");
126 jengbou 1.2 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 jengbou 1.3 h_pullz_nTrk->SetMarkerColor(4);
129 jengbou 1.2 h_pullz_nTrk->SetBit(TH1::kCanRebin);
130 jengbou 1.3 h_pullz_nTrk->GetXaxis()->SetTitle("Num of tracks");
131 jengbou 1.2 }
132 jengbou 1.1 for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
133     stringstream ss;
134     ss << ntrk;
135     string suffix = ss.str();
136 jengbou 1.3 h["resx_"+suffix] = fs->make<TH1D> (TString("deltax_"+suffix),"x-distance (Rec - Sim)",100,-0.02,0.02);
137 jengbou 1.1 h["resx_"+suffix]->GetXaxis()->SetTitle("cm");
138 jengbou 1.3 h["resy_"+suffix] = fs->make<TH1D> (TString("deltay_"+suffix),"y-distance (Rec - Sim)",100,-0.02,0.02);
139 jengbou 1.1 h["resy_"+suffix]->GetXaxis()->SetTitle("cm");
140 jengbou 1.3 h["resz_"+suffix] = fs->make<TH1D> (TString("deltaz_"+suffix),"z-distance (Rec - Sim)",100,-0.02,0.02);
141 jengbou 1.1 h["resz_"+suffix]->GetXaxis()->SetTitle("cm");
142 jengbou 1.3 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 jengbou 1.1 suffix.clear();
146     }
147     }
148     }
149    
150    
151     PVStudy::~PVStudy()
152     {
153    
154     // do anything here that needs to be done at desctruction time
155     // (e.g. close files, deallocate resources etc.)
156    
157     }
158    
159    
160     //
161     // member functions
162     //
163     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC, std::string suffix="")
164     {
165     std::vector<PVStudy::simPrimaryVertex> simpv;
166     const HepMC::GenEvent* evt=evtMC->GetEvent();
167     if (evt) {
168     if(verbose_) {
169     cout << "process id " << evt->signal_process_id()<<endl;
170     cout << "signal process vertex " << ( evt->signal_process_vertex() ?
171     evt->signal_process_vertex()->barcode() : 0 ) <<endl;
172     cout << "number of vertices " << evt->vertices_size() << endl;
173     }
174    
175     int idx=0;
176     for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
177     vitr != evt->vertices_end(); ++vitr )
178     { // loop for vertex ...
179     HepMC::FourVector pos = (*vitr)->position();
180     //HepLorentzVector pos = (*vitr)->position();
181     if (pos.t()>0) { continue;} // ?
182    
183     bool hasMotherVertex=false;
184     // if(verbose_) cout << "mothers" << endl;
185     for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
186     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
187     HepMC::GenVertex * mv=(*mother)->production_vertex();
188     if (mv) {hasMotherVertex=true;}
189     // if(verbose_) {
190     // 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;
206     }
207     }
208    
209     if(!vp){
210     // this is a new vertex
211     if(verbose_) cout << "this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
212     simpv.push_back(sv);
213     vp=&simpv.back();
214     }else{
215     if(verbose_) cout << "this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
216     }
217     vp->genVertex.push_back((*vitr)->barcode());
218     // collect final state descendants
219     for ( HepMC::GenVertex::particle_iterator
220     daughter = (*vitr)->particles_begin(HepMC::descendants);
221     daughter != (*vitr)->particles_end(HepMC::descendants);
222     ++daughter ) {
223     if (isFinalstateParticle(*daughter)){
224     if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
225     == vp->finalstateParticles.end()){
226     vp->finalstateParticles.push_back((*daughter)->barcode());
227     HepMC::FourVector m=(*daughter)->momentum();
228     // the next four lines used to be "vp->ptot+=m;" in the days of CLHEP::HepLorentzVector
229     // but adding FourVectors seems not to be foreseen
230     vp->ptot.setPx(vp->ptot.px()+m.px());
231     vp->ptot.setPy(vp->ptot.py()+m.py());
232     vp->ptot.setPz(vp->ptot.pz()+m.pz());
233     vp->ptot.setE(vp->ptot.e()+m.e());
234     vp->ptsq+=(m.perp())*(m.perp());
235     if ( (m.perp()>0.8) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
236     vp->nGenTrk++;
237     }
238     }
239     }
240     }
241     idx++;
242     }
243     }
244     return simpv;
245     }
246    
247     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC,
248     const Handle<SimVertexContainer> simVtxs,
249     const Handle<SimTrackContainer> simTrks)
250     {
251     // simvertices don't have enough information to decide,
252     // genVertices don't have the simulated coordinates ( with VtxSmeared they might)
253     // go through simtracks to get the link between simulated and generated vertices
254     std::vector<PVStudy::simPrimaryVertex> simpv;
255     int idx=0;
256     for(SimTrackContainer::const_iterator t=simTrks->begin();
257     t!=simTrks->end(); ++t){
258     if ( !(t->noVertex()) && !(t->type()==-99) ){
259     double ptsq=0;
260     bool primary=false; // something coming directly from the primary vertex
261     bool resonance=false; // resonance
262     bool track=false; // undecayed, charged particle
263     HepMC::GenParticle* gp=evtMC->GetEvent()->barcode_to_particle( (*t).genpartIndex() );
264     if (gp) {
265     HepMC::GenVertex * gv=gp->production_vertex();
266     if (gv) {
267     for ( HepMC::GenVertex::particle_iterator
268     daughter = gv->particles_begin(HepMC::descendants);
269     daughter != gv->particles_end(HepMC::descendants);
270     ++daughter ) {
271     if (isFinalstateParticle(*daughter)){
272     ptsq+=(*daughter)->momentum().perp()*(*daughter)->momentum().perp();
273     }
274     }
275     primary = ( gv->position().t()==0);
276     //resonance= ( gp->mother() && isResonance(gp->mother())); // in CLHEP/HepMC days
277     // no more mother pointer in the improved HepMC GenParticle
278     resonance= ( isResonance(*(gp->production_vertex()->particles_in_const_begin())));
279     if (gp->status()==1){
280     //track=((pdt->particle(gp->pdg_id()))->charge() != 0);
281     track=not isCharged(gp);
282     }
283     }
284     }
285    
286     const HepMC::FourVector & v=(*simVtxs)[t->vertIndex()].position();
287     //const HepLorentzVector & v=(*simVtxs)[t->vertIndex()].position();
288     if(primary or resonance){
289     {
290     // check all primaries found so far to avoid multiple entries
291     bool newVertex=true;
292     for(std::vector<PVStudy::simPrimaryVertex>::iterator v0=simpv.begin();
293     v0!=simpv.end(); v0++){
294     if( (fabs(v0->x-v.x())<0.001) && (fabs(v0->y-v.y())<0.001) && (fabs(v0->z-v.z())<0.001) ){
295     if (track) {
296     v0->simTrackIndex.push_back(idx);
297     if (ptsq>(*v0).ptsq){(*v0).ptsq=ptsq;}
298     }
299     newVertex=false;
300     }
301     }
302     if(newVertex && !resonance){
303     PVStudy::simPrimaryVertex anotherVertex(v.x(),v.y(),v.z());
304     if (track) anotherVertex.simTrackIndex.push_back(idx);
305     anotherVertex.ptsq=ptsq;
306     simpv.push_back(anotherVertex);
307     }
308     }//
309     }
310    
311     }// simtrack has vertex and valid type
312     idx++;
313     }//simTrack loop
314     return simpv;
315     }
316    
317     // ------------ method called to for each event ------------
318     void
319     PVStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
320     {
321     using namespace edm;
322 jengbou 1.3 using reco::TrackCollection;
323    
324     ftree_->SetBranchAddress("residual",&fres);
325     ftree_->SetBranchAddress("error",&ferror);
326     ftree_->SetBranchAddress("nTrk",&fntrk);
327 jengbou 1.1
328     Handle<TrackCollection> tracks;
329     iEvent.getByLabel(tracksLabel_,tracks);
330    
331     Handle<reco::VertexCollection> recVtxs;
332     iEvent.getByLabel(vtxSample_, recVtxs);
333    
334     if (!realData_) {
335     Handle<SimVertexContainer> simVtxs;
336     iEvent.getByLabel( simG4_, simVtxs);
337    
338     Handle<SimTrackContainer> simTrks;
339     iEvent.getByLabel( simG4_, simTrks);
340     }
341    
342     if(verbose_) printRecVtxs(recVtxs);
343    
344     try{
345     iSetup.getData(pdt);
346     }catch(const Exception&){
347     cout << "Some problem occurred with the particle data table. This may not work !." <<endl;
348     }
349    
350     if (!realData_) {
351     bool MC=false;
352     Handle<HepMCProduct> evtMC;
353     iEvent.getByLabel("generator",evtMC);
354     if (!evtMC.isValid()) {
355     MC=false;
356     if(verbose_){
357     cout << "no HepMCProduct found"<< endl;
358     }
359     } else {
360     if(verbose_){
361     cout << "generator HepMCProduct found"<< endl;
362     }
363     MC=true;
364     }
365    
366     if(MC){
367     // make a list of primary vertices:
368     std::vector<simPrimaryVertex> simpv;
369     simpv=getSimPVs(evtMC,"");
370     // simpv=getSimPVs(evtMC, simVtxs, simTrks);
371    
372     int nsimtrk=0;
373     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 jengbou 1.3 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 jengbou 1.1 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 jengbou 1.3 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 jengbou 1.1 }
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     }
434     }
435     }
436     }
437     }// With MC
438    
439     // Loop RecVtxs and find matched SimVtxs
440     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
441     v!=recVtxs->end(); ++v){
442     try {
443     for(reco::Vertex::trackRef_iterator t = v->tracks_begin();
444     t!=v->tracks_end(); t++) {
445     // illegal charge
446     if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
447     // h_trkPtPV->Fill(0.);
448     }
449     else {
450     h_trkPtPV->Fill((**t).pt());
451     }
452     }
453     }
454     catch (...) {
455     // exception thrown when trying to use linked track
456     // h_trkPtPV->Fill(0.);
457     }
458    
459     h_nTrkPV->Fill(v->tracksSize());
460    
461     }
462    
463     h_nTrk->Fill(tracks->size());
464    
465     for(TrackCollection::const_iterator itTrack = tracks->begin();
466     itTrack != tracks->end();
467     ++itTrack) {
468     int charge = 0;
469     charge = itTrack->charge();
470     h_trkPt->Fill(itTrack->pt());
471     }
472    
473     }
474    
475    
476     // ------------ method called once each job just before starting event loop ------------
477     void
478     PVStudy::beginJob()
479     {
480     }
481    
482     // ------------ method called once each job just after ending the event loop ------------
483     void
484     PVStudy::endJob() {
485     double sqt2 = sqrt(2.);
486     if (!realData_) {
487     if (verbose_) cout << "Analyzing PV info" << endl;
488     for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
489     if (verbose_) cout << "Fill point of ntrk = " << ntrk << endl;
490     PVStudy::PVAnaInfo ipv = GetPVAnaInfo(ntrk);
491 jengbou 1.2 if (analyze_) {
492 jengbou 1.3 // if ( ipv.res_.x() > 0 ) h_resx_nTrk->Fill(ntrk,ipv.res_.x()/sqt2, 1.);
493     // if ( ipv.res_.y() > 0 ) h_resy_nTrk->Fill(ntrk,ipv.res_.y()/sqt2, 1.);
494     // if ( ipv.res_.z() > 0 ) h_resz_nTrk->Fill(ntrk,ipv.res_.z()/sqt2, 1.);
495     if ( ipv.res_.x() > 0 ) h_resx_nTrk->Fill(ntrk,ipv.res_.x(), 1.);
496     if ( ipv.res_.y() > 0 ) h_resy_nTrk->Fill(ntrk,ipv.res_.y(), 1.);
497     if ( ipv.res_.z() > 0 ) h_resz_nTrk->Fill(ntrk,ipv.res_.z(), 1.);
498     if ( ipv.pull_.x() > 0 ) h_pullx_nTrk->Fill(ntrk,ipv.pull_.x(), 1.);
499     if ( ipv.pull_.y() > 0 ) h_pully_nTrk->Fill(ntrk,ipv.pull_.y(), 1.);
500     if ( ipv.pull_.z() > 0 ) h_pullz_nTrk->Fill(ntrk,ipv.pull_.z(), 1.);
501 jengbou 1.2 }
502 jengbou 1.1 }
503     }
504 jengbou 1.3 file_->cd();
505     ftree_->Write();
506     file_->Close();
507 jengbou 1.1 }
508    
509     bool PVStudy::matchVertex(const PVStudy::simPrimaryVertex & vsim,
510     const reco::Vertex & vrec)
511     {
512     return (fabs(vsim.z-vrec.z())<0.0500); // =500um
513     }
514    
515     bool PVStudy::isResonance(const HepMC::GenParticle * p){
516     double ctau=(pdt->particle( abs(p->pdg_id()) ))->lifetime();
517     if(verbose_) cout << "isResonance " << p->pdg_id() << " " << ctau << endl;
518     return ctau >0 && ctau <1e-6;
519     }
520    
521     bool PVStudy::isFinalstateParticle(const HepMC::GenParticle * p){
522     return ( !p->end_vertex() && p->status()==1 );
523     }
524    
525     bool PVStudy::isCharged(const HepMC::GenParticle * p){
526     const ParticleData * part = pdt->particle( p->pdg_id() );
527     if (part){
528     return part->charge()!=0;
529     }else{
530     // the new/improved particle table doesn't know anti-particles
531     return pdt->particle( -p->pdg_id() )!=0;
532     }
533     }
534    
535     void PVStudy::printSimVtxs(const Handle<SimVertexContainer> simVtxs){
536     int i=0;
537     for(SimVertexContainer::const_iterator vsim=simVtxs->begin();
538     vsim!=simVtxs->end(); ++vsim){
539     cout << i++ << ")" << scientific
540     << " evtid=" << vsim->eventId().event()
541     << " sim x=" << vsim->position().x()
542     << " sim y=" << vsim->position().y()
543     << " sim z=" << vsim->position().z()
544     << " sim t=" << vsim->position().t()
545     << " parent=" << vsim->parentIndex()
546     << endl;
547     }
548     }
549    
550     void PVStudy::printRecVtxs(const Handle<reco::VertexCollection> recVtxs){
551     int ivtx=0;
552     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
553     v!=recVtxs->end(); ++v){
554     cout << "Recvtx "<< std::setw(3) << std::setfill(' ')<<ivtx++
555     << "#trk " << std::setw(3) << v->tracksSize()
556     << " chi2 " << std::setw(4) << v->chi2()
557     << " ndof " << std::setw(3) << v->ndof() << endl
558     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v->x()
559     << " dx " << std::setw(8) << v->xError()<< endl
560     << " y " << std::setw(8) << v->y()
561     << " dy " << std::setw(8) << v->yError()<< endl
562     << " z " << std::setw(8) << v->z()
563     << " dz " << std::setw(8) << v->zError()
564     << endl;
565     }
566     }
567    
568     void PVStudy::printSimTrks(const Handle<SimTrackContainer> simTrks){
569     cout << " simTrks type, (momentum), vertIndex, genpartIndex" << endl;
570     int i=1;
571     for(SimTrackContainer::const_iterator t=simTrks->begin();
572     t!=simTrks->end(); ++t){
573     //HepMC::GenParticle* gp=evtMC->GetEvent()->particle( (*t).genpartIndex() );
574     cout << i++ << ")"
575     << (*t)
576     << " index="
577     << (*t).genpartIndex();
578     //if (gp) {
579     // HepMC::GenVertex *gv=gp->production_vertex();
580     // cout << " genvertex =" << (*gv);
581     //}
582     cout << endl;
583     }
584     }
585    
586 jengbou 1.3 void PVStudy::fillHisto(res r, pull p, int ntk){
587     stringstream ss;
588     ss << ntk;
589     string suffix = ss.str();
590     if (ntk < nTrkMin_ || ntk > nTrkMax_ ) return;
591     // Fill histograms of res and pull of ntk
592     h["resx_"+suffix]->Fill(r.x());
593     h["resy_"+suffix]->Fill(r.y());
594     h["resz_"+suffix]->Fill(r.z());
595     h["pullx_"+suffix]->Fill(p.x());
596     h["pully_"+suffix]->Fill(p.y());
597     h["pullz_"+suffix]->Fill(p.z());
598     }
599    
600 jengbou 1.1 PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo(int ntk) {
601     map<int,double> data;
602 jengbou 1.2 data.clear();
603 jengbou 1.1 double fpar[2];
604     double cm2um = 10000;
605     stringstream ss;
606     ss << ntk;
607     string suffix = ss.str();
608    
609 jengbou 1.3 if(analyze_) {
610     for ( int i = 0; i < 6; ++i) {
611     cout << "Here" << endl;
612     switch (i) {
613     case 0:
614     fit(h["resx_"+suffix], fpar);
615     data[i] = fpar[0]*cm2um;
616     break;
617     case 1:
618     fit(h["resy_"+suffix], fpar);
619     data[i] = fpar[0]*cm2um;
620     break;
621     case 2:
622     fit(h["resz_"+suffix], fpar);
623     data[i] = fpar[0]*cm2um;
624     break;
625     case 3:
626     fit(h["pullx_"+suffix], fpar);
627     data[i] = fpar[0];
628     break;
629     case 4:
630     fit(h["pully_"+suffix], fpar);
631     data[i] = fpar[0];
632     break;
633     case 5:
634     fit(h["pullz_"+suffix], fpar);
635     data[i] = fpar[0];
636     break;
637 jengbou 1.1 }
638 jengbou 1.3 if (verbose_ && data[i] > 0) cout << "data[" << i << "] = " << data[i] << (i<3?" micro m":" ") << endl;
639 jengbou 1.1 }
640 jengbou 1.3 }
641     else
642     for ( int i = 0; i < 6; ++i) {
643     data[i] = -999;
644 jengbou 1.1 }
645    
646 jengbou 1.3 return PVStudy::PVAnaInfo(res(data[0], data[1], data[2]),
647     error(0.,0.,0.),
648     pull(data[3], data[4], data[5]),
649     error(0.,0.,0.),
650 jengbou 1.1 ntk);
651     }
652    
653 jengbou 1.3 void PVStudy::fit(TH1 *&hdist, double data[]){
654     TAxis *axis0 = hdist->GetXaxis();
655     int nbin = axis0->GetLast();
656     double nOF = hdist->GetBinContent(nbin+1);
657     double nUF = hdist->GetBinContent(0);
658     if ( verbose_ ){
659     cout << "Last bin = " << nbin;
660     cout << "; hdist: Overflow Entries = " << nOF;
661     cout << "; Underflow Entries = " << nUF;
662     cout << "; hdist->GetEntries() = " << hdist->GetEntries() << endl;
663     }
664     if (hdist->GetEntries() - nOF - nUF >= 5) { // FIXME: for reasonable Gaussian fit
665     hdist->Fit("gaus","Q");
666     TF1 *fgaus = hdist->GetFunction("gaus");
667     if (verbose_) cout << "got function" << endl;
668     data[0] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
669     }
670     }
671    
672 jengbou 1.1 //define this as a plug-in
673     DEFINE_FWK_MODULE(PVStudy);