ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVStudy.cc
Revision: 1.2
Committed: Thu Sep 3 19:29:38 2009 UTC (15 years, 8 months ago) by jengbou
Content type: text/plain
Branch: MAIN
CVS Tags: CMSSW_3_1_X
Changes since 1.1: +94 -84 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.2 // $Id: PVStudy.cc,v 1.1 2009/09/03 17:31:08 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     nTrkMin_(10),nTrkMax_(150)
63     {
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.1
71     //now do what ever initialization is needed
72     pvanainfo.clear();
73    
74     edm::Service<TFileService> fs;
75     // TFileDirectory subDir = fs->mkdir( "Summary" );
76     // TFileDirectory subSubDir = subDir.mkdir( "mySubSubDirectory" );
77    
78     h_nTrk = fs->make<TH1F>("nTrk","Num of total tracks",300,0,300);
79     h_nTrkPV = fs->make<TH1F>("nTrkPV","Num of Tracks from PV",300,0,300);
80     h_trkPt = fs->make<TH1D>("trkPt","Pt of all tracks",100,0,100);
81     h_trkPtPV = fs->make<TH1D>("trkPtPV","Pt dist of tracks from PV",100,0,100);
82    
83     if (!realData_) {
84     h_deltax = fs->make<TH1D>("deltax","x-distance (Rec - Sim)",800,-0.04,0.04);
85     h_deltay = fs->make<TH1D>("deltay","y-distance (Rec - Sim)",800,-0.04,0.04);
86     h_deltaz = fs->make<TH1D>("deltaz","z-distance (Rec - Sim)",800,-0.04,0.04);
87     h_pullx = fs->make<TH1D>("pullx","x-pull (Rec - Sim)",1000,-25.,25.);
88     h_pully = fs->make<TH1D>("pully","y-pull (Rec - Sim)",1000,-25.,25.);
89     h_pullz = fs->make<TH1D>("pullz","z-pull (Rec - Sim)",1000,-25.,25.);
90 jengbou 1.2
91 jengbou 1.1 // Summary of analysis:
92 jengbou 1.2 if (analyze_) {
93     h_resx_nTrk = fs->make<TH2D>("resx_ntrk","x-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
94     h_resx_nTrk->SetMarkerStyle(21);
95     h_resx_nTrk->GetYaxis()->SetTitle("#mum");
96     h_resy_nTrk = fs->make<TH2D>("resy_ntrk","y-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
97     h_resy_nTrk->SetMarkerStyle(21);
98     h_resy_nTrk->GetYaxis()->SetTitle("#mum");
99     h_resz_nTrk = fs->make<TH2D>("resz_ntrk","z-resolution (Rec - Sim) vs number of tracks",150,0,150,400,0.,200);
100     h_resz_nTrk->SetMarkerStyle(21);
101     h_resz_nTrk->GetYaxis()->SetTitle("#mum");
102     h_pullx_nTrk = fs->make<TH2D>("pullx_ntrk","x-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
103     h_pullx_nTrk->SetMarkerStyle(21);
104     h_pullx_nTrk->SetBit(TH1::kCanRebin);
105     h_pullx_nTrk->GetYaxis()->SetTitle("cm");
106     h_pully_nTrk = fs->make<TH2D>("pully_ntrk","y-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
107     h_pully_nTrk->SetMarkerStyle(21);
108     h_pully_nTrk->SetBit(TH1::kCanRebin);
109     h_pully_nTrk->GetYaxis()->SetTitle("cm");
110     h_pullz_nTrk = fs->make<TH2D>("pullz_ntrk","z-pull (Rec - Sim) vs number of tracks",150,0,150,100,0.,2.);
111     h_pullz_nTrk->SetMarkerStyle(21);
112     h_pullz_nTrk->SetBit(TH1::kCanRebin);
113     h_pullz_nTrk->GetYaxis()->SetTitle("cm");
114     }
115 jengbou 1.1 for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
116     stringstream ss;
117     ss << ntrk;
118     string suffix = ss.str();
119     h["resx_"+suffix] = fs->make<TH1D> (TString("deltax_"+suffix),"x-distance (Rec - Sim)",400,-0.02,0.02);
120     h["resx_"+suffix]->GetXaxis()->SetTitle("cm");
121     h["resy_"+suffix] = fs->make<TH1D> (TString("deltay_"+suffix),"y-distance (Rec - Sim)",400,-0.02,0.02);
122     h["resy_"+suffix]->GetXaxis()->SetTitle("cm");
123     h["resz_"+suffix] = fs->make<TH1D> (TString("deltaz_"+suffix),"z-distance (Rec - Sim)",400,-0.02,0.02);
124     h["resz_"+suffix]->GetXaxis()->SetTitle("cm");
125     h["pullx_"+suffix] = fs->make<TH1D> (TString("pullx_"+suffix),"x-pull (Rec - Sim)",200,-10.,10.);
126     h["pullx_"+suffix]->GetXaxis()->SetTitle("cm");
127     h["pully_"+suffix] = fs->make<TH1D> (TString("pully_"+suffix),"y-pull (Rec - Sim)",200,-10.,10.);
128     h["pully_"+suffix]->GetXaxis()->SetTitle("cm");
129     h["pullz_"+suffix] = fs->make<TH1D> (TString("pullz_"+suffix),"z-pull (Rec - Sim)",200,-10.,10.);
130     h["pullz_"+suffix]->GetXaxis()->SetTitle("cm");
131     suffix.clear();
132     }
133     }
134     }
135    
136    
137     PVStudy::~PVStudy()
138     {
139    
140     // do anything here that needs to be done at desctruction time
141     // (e.g. close files, deallocate resources etc.)
142    
143     }
144    
145    
146     //
147     // member functions
148     //
149     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC, std::string suffix="")
150     {
151     std::vector<PVStudy::simPrimaryVertex> simpv;
152     const HepMC::GenEvent* evt=evtMC->GetEvent();
153     if (evt) {
154     if(verbose_) {
155     cout << "process id " << evt->signal_process_id()<<endl;
156     cout << "signal process vertex " << ( evt->signal_process_vertex() ?
157     evt->signal_process_vertex()->barcode() : 0 ) <<endl;
158     cout << "number of vertices " << evt->vertices_size() << endl;
159     }
160    
161     int idx=0;
162     for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
163     vitr != evt->vertices_end(); ++vitr )
164     { // loop for vertex ...
165     HepMC::FourVector pos = (*vitr)->position();
166     //HepLorentzVector pos = (*vitr)->position();
167     if (pos.t()>0) { continue;} // ?
168    
169     bool hasMotherVertex=false;
170     // if(verbose_) cout << "mothers" << endl;
171     for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
172     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
173     HepMC::GenVertex * mv=(*mother)->production_vertex();
174     if (mv) {hasMotherVertex=true;}
175     // if(verbose_) {
176     // cout << "\t";
177     // (*mother)->print();
178     // }
179     }
180    
181     if(hasMotherVertex) {continue;}
182    
183     // could be a new vertex, check all primaries found so far to avoid multiple entries
184     const double mm2cm=0.1;
185     simPrimaryVertex sv(pos.x()*mm2cm,pos.y()*mm2cm,pos.z()*mm2cm); // sim unit mm, rec unit cm
186     simPrimaryVertex *vp=NULL; // will become non-NULL if a vertex is found and then point to it
187     for(vector<simPrimaryVertex>::iterator v0=simpv.begin();
188     v0!=simpv.end(); v0++){
189     if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
190     vp=&(*v0);
191     break;
192     }
193     }
194    
195     if(!vp){
196     // this is a new vertex
197     if(verbose_) cout << "this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
198     simpv.push_back(sv);
199     vp=&simpv.back();
200     }else{
201     if(verbose_) cout << "this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
202     }
203     vp->genVertex.push_back((*vitr)->barcode());
204     // collect final state descendants
205     for ( HepMC::GenVertex::particle_iterator
206     daughter = (*vitr)->particles_begin(HepMC::descendants);
207     daughter != (*vitr)->particles_end(HepMC::descendants);
208     ++daughter ) {
209     if (isFinalstateParticle(*daughter)){
210     if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
211     == vp->finalstateParticles.end()){
212     vp->finalstateParticles.push_back((*daughter)->barcode());
213     HepMC::FourVector m=(*daughter)->momentum();
214     // the next four lines used to be "vp->ptot+=m;" in the days of CLHEP::HepLorentzVector
215     // but adding FourVectors seems not to be foreseen
216     vp->ptot.setPx(vp->ptot.px()+m.px());
217     vp->ptot.setPy(vp->ptot.py()+m.py());
218     vp->ptot.setPz(vp->ptot.pz()+m.pz());
219     vp->ptot.setE(vp->ptot.e()+m.e());
220     vp->ptsq+=(m.perp())*(m.perp());
221     if ( (m.perp()>0.8) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
222     vp->nGenTrk++;
223     }
224     }
225     }
226     }
227     idx++;
228     }
229     }
230     return simpv;
231     }
232    
233     std::vector<PVStudy::simPrimaryVertex> PVStudy::getSimPVs(const Handle<HepMCProduct> evtMC,
234     const Handle<SimVertexContainer> simVtxs,
235     const Handle<SimTrackContainer> simTrks)
236     {
237     // simvertices don't have enough information to decide,
238     // genVertices don't have the simulated coordinates ( with VtxSmeared they might)
239     // go through simtracks to get the link between simulated and generated vertices
240     std::vector<PVStudy::simPrimaryVertex> simpv;
241     int idx=0;
242     for(SimTrackContainer::const_iterator t=simTrks->begin();
243     t!=simTrks->end(); ++t){
244     if ( !(t->noVertex()) && !(t->type()==-99) ){
245     double ptsq=0;
246     bool primary=false; // something coming directly from the primary vertex
247     bool resonance=false; // resonance
248     bool track=false; // undecayed, charged particle
249     HepMC::GenParticle* gp=evtMC->GetEvent()->barcode_to_particle( (*t).genpartIndex() );
250     if (gp) {
251     HepMC::GenVertex * gv=gp->production_vertex();
252     if (gv) {
253     for ( HepMC::GenVertex::particle_iterator
254     daughter = gv->particles_begin(HepMC::descendants);
255     daughter != gv->particles_end(HepMC::descendants);
256     ++daughter ) {
257     if (isFinalstateParticle(*daughter)){
258     ptsq+=(*daughter)->momentum().perp()*(*daughter)->momentum().perp();
259     }
260     }
261     primary = ( gv->position().t()==0);
262     //resonance= ( gp->mother() && isResonance(gp->mother())); // in CLHEP/HepMC days
263     // no more mother pointer in the improved HepMC GenParticle
264     resonance= ( isResonance(*(gp->production_vertex()->particles_in_const_begin())));
265     if (gp->status()==1){
266     //track=((pdt->particle(gp->pdg_id()))->charge() != 0);
267     track=not isCharged(gp);
268     }
269     }
270     }
271    
272     const HepMC::FourVector & v=(*simVtxs)[t->vertIndex()].position();
273     //const HepLorentzVector & v=(*simVtxs)[t->vertIndex()].position();
274     if(primary or resonance){
275     {
276     // check all primaries found so far to avoid multiple entries
277     bool newVertex=true;
278     for(std::vector<PVStudy::simPrimaryVertex>::iterator v0=simpv.begin();
279     v0!=simpv.end(); v0++){
280     if( (fabs(v0->x-v.x())<0.001) && (fabs(v0->y-v.y())<0.001) && (fabs(v0->z-v.z())<0.001) ){
281     if (track) {
282     v0->simTrackIndex.push_back(idx);
283     if (ptsq>(*v0).ptsq){(*v0).ptsq=ptsq;}
284     }
285     newVertex=false;
286     }
287     }
288     if(newVertex && !resonance){
289     PVStudy::simPrimaryVertex anotherVertex(v.x(),v.y(),v.z());
290     if (track) anotherVertex.simTrackIndex.push_back(idx);
291     anotherVertex.ptsq=ptsq;
292     simpv.push_back(anotherVertex);
293     }
294     }//
295     }
296    
297     }// simtrack has vertex and valid type
298     idx++;
299     }//simTrack loop
300     return simpv;
301     }
302    
303     // ------------ method called to for each event ------------
304     void
305     PVStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
306     {
307     using namespace edm;
308    
309     using reco::TrackCollection;
310    
311     Handle<TrackCollection> tracks;
312     iEvent.getByLabel(tracksLabel_,tracks);
313    
314     Handle<reco::VertexCollection> recVtxs;
315     iEvent.getByLabel(vtxSample_, recVtxs);
316    
317     if (!realData_) {
318     Handle<SimVertexContainer> simVtxs;
319     iEvent.getByLabel( simG4_, simVtxs);
320    
321     Handle<SimTrackContainer> simTrks;
322     iEvent.getByLabel( simG4_, simTrks);
323     }
324    
325     if(verbose_) printRecVtxs(recVtxs);
326    
327     try{
328     iSetup.getData(pdt);
329     }catch(const Exception&){
330     cout << "Some problem occurred with the particle data table. This may not work !." <<endl;
331     }
332    
333     if (!realData_) {
334     bool MC=false;
335     Handle<HepMCProduct> evtMC;
336     iEvent.getByLabel("generator",evtMC);
337     if (!evtMC.isValid()) {
338     MC=false;
339     if(verbose_){
340     cout << "no HepMCProduct found"<< endl;
341     }
342     } else {
343     if(verbose_){
344     cout << "generator HepMCProduct found"<< endl;
345     }
346     MC=true;
347     }
348    
349     if(MC){
350     // make a list of primary vertices:
351     std::vector<simPrimaryVertex> simpv;
352     simpv=getSimPVs(evtMC,"");
353     // simpv=getSimPVs(evtMC, simVtxs, simTrks);
354    
355     int nsimtrk=0;
356     int nrectrk=0;
357     for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
358     vsim!=simpv.end(); vsim++){
359     nsimtrk+=vsim->nGenTrk;
360     // look for a matching reconstructed vertex
361     vsim->recVtx=NULL;
362     for(reco::VertexCollection::const_iterator vrec=recVtxs->begin();
363     vrec!=recVtxs->end(); ++vrec){
364     nrectrk=vrec->tracksSize();
365     if(verbose_){
366     cout << "sim primary vertex x = " << vsim->x << "; y = " << vsim->y << "; z = " << vsim->z << endl;
367     cout << "Is matched? " << (matchVertex(*vsim,*vrec)?"Yes":"No") << endl;
368     }
369     if ( matchVertex(*vsim,*vrec) ){
370     // if the matching critera are fulfilled, accept the rec-vertex that is closest in z
371     if( ((vsim->recVtx) && (fabs(vsim->recVtx->position().z()-vsim->z)>fabs(vrec->z()-vsim->z)))
372     || (!vsim->recVtx) )
373     {
374     vsim->recVtx=&(*vrec);
375     }
376     }
377     }
378     if (vsim->recVtx){
379     if(verbose_) {
380     cout <<"primary matched " << vsim->x << " " << vsim->y << " " << vsim->z << endl;
381     }
382     h_deltax->Fill( vsim->recVtx->x()-vsim->x );
383     h_deltay->Fill( vsim->recVtx->y()-vsim->y );
384     h_deltaz->Fill( vsim->recVtx->z()-vsim->z );
385     h_pullx->Fill( (vsim->recVtx->x()-vsim->x )/vsim->recVtx->xError() );
386     h_pully->Fill( (vsim->recVtx->y()-vsim->y )/vsim->recVtx->yError() );
387     h_pullz->Fill( (vsim->recVtx->z()-vsim->z )/vsim->recVtx->zError() );
388     pvanainfo.push_back(PVStudy::PVAnaInfo( vsim->recVtx->x()-vsim->x,
389     vsim->recVtx->y()-vsim->y,
390     vsim->recVtx->z()-vsim->z,
391     (vsim->recVtx->x()-vsim->x )/vsim->recVtx->xError(),
392     (vsim->recVtx->y()-vsim->y )/vsim->recVtx->yError(),
393     (vsim->recVtx->z()-vsim->z )/vsim->recVtx->zError(),
394     nrectrk )
395     );
396     }
397     else{ // no rec vertex found for this simvertex
398     if(verbose_) {
399     cout <<"primary not found " << vsim->x << " " << vsim->y << " " << vsim->z << " nGenTrk=" << vsim->nGenTrk << endl;
400     }
401     }
402     }
403     }
404     }// With MC
405    
406     // Loop RecVtxs and find matched SimVtxs
407     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
408     v!=recVtxs->end(); ++v){
409     try {
410     for(reco::Vertex::trackRef_iterator t = v->tracks_begin();
411     t!=v->tracks_end(); t++) {
412     // illegal charge
413     if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
414     // h_trkPtPV->Fill(0.);
415     }
416     else {
417     h_trkPtPV->Fill((**t).pt());
418     }
419     }
420     }
421     catch (...) {
422     // exception thrown when trying to use linked track
423     // h_trkPtPV->Fill(0.);
424     }
425    
426     h_nTrkPV->Fill(v->tracksSize());
427    
428     }
429    
430     h_nTrk->Fill(tracks->size());
431    
432     for(TrackCollection::const_iterator itTrack = tracks->begin();
433     itTrack != tracks->end();
434     ++itTrack) {
435     int charge = 0;
436     charge = itTrack->charge();
437     h_trkPt->Fill(itTrack->pt());
438     }
439    
440     }
441    
442    
443     // ------------ method called once each job just before starting event loop ------------
444     void
445     PVStudy::beginJob()
446     {
447     }
448    
449     // ------------ method called once each job just after ending the event loop ------------
450     void
451     PVStudy::endJob() {
452     double sqt2 = sqrt(2.);
453     if (!realData_) {
454     if (verbose_) cout << "Analyzing PV info" << endl;
455     for (int ntrk=nTrkMin_;ntrk<=nTrkMax_;++ntrk) {
456     if (verbose_) cout << "Fill point of ntrk = " << ntrk << endl;
457     PVStudy::PVAnaInfo ipv = GetPVAnaInfo(ntrk);
458 jengbou 1.2 if (analyze_) {
459     if ( ipv.resx > 0 ) h_resx_nTrk->Fill(ntrk,ipv.resx/sqt2, 1.);
460     if ( ipv.resy > 0 ) h_resy_nTrk->Fill(ntrk,ipv.resy/sqt2, 1.);
461     if ( ipv.resz > 0 ) h_resz_nTrk->Fill(ntrk,ipv.resz/sqt2, 1.);
462     if ( ipv.pullx > 0 ) h_pullx_nTrk->Fill(ntrk,ipv.pullx, 1.);
463     if ( ipv.pully > 0 ) h_pully_nTrk->Fill(ntrk,ipv.pully, 1.);
464     if ( ipv.pullz > 0 ) h_pullz_nTrk->Fill(ntrk,ipv.pullz, 1.);
465     }
466 jengbou 1.1 }
467     }
468     }
469    
470     bool PVStudy::matchVertex(const PVStudy::simPrimaryVertex & vsim,
471     const reco::Vertex & vrec)
472     {
473     return (fabs(vsim.z-vrec.z())<0.0500); // =500um
474     }
475    
476     bool PVStudy::isResonance(const HepMC::GenParticle * p){
477     double ctau=(pdt->particle( abs(p->pdg_id()) ))->lifetime();
478     if(verbose_) cout << "isResonance " << p->pdg_id() << " " << ctau << endl;
479     return ctau >0 && ctau <1e-6;
480     }
481    
482     bool PVStudy::isFinalstateParticle(const HepMC::GenParticle * p){
483     return ( !p->end_vertex() && p->status()==1 );
484     }
485    
486     bool PVStudy::isCharged(const HepMC::GenParticle * p){
487     const ParticleData * part = pdt->particle( p->pdg_id() );
488     if (part){
489     return part->charge()!=0;
490     }else{
491     // the new/improved particle table doesn't know anti-particles
492     return pdt->particle( -p->pdg_id() )!=0;
493     }
494     }
495    
496     void PVStudy::printSimVtxs(const Handle<SimVertexContainer> simVtxs){
497     int i=0;
498     for(SimVertexContainer::const_iterator vsim=simVtxs->begin();
499     vsim!=simVtxs->end(); ++vsim){
500     cout << i++ << ")" << scientific
501     << " evtid=" << vsim->eventId().event()
502     << " sim x=" << vsim->position().x()
503     << " sim y=" << vsim->position().y()
504     << " sim z=" << vsim->position().z()
505     << " sim t=" << vsim->position().t()
506     << " parent=" << vsim->parentIndex()
507     << endl;
508     }
509     }
510    
511     void PVStudy::printRecVtxs(const Handle<reco::VertexCollection> recVtxs){
512     int ivtx=0;
513     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
514     v!=recVtxs->end(); ++v){
515     cout << "Recvtx "<< std::setw(3) << std::setfill(' ')<<ivtx++
516     << "#trk " << std::setw(3) << v->tracksSize()
517     << " chi2 " << std::setw(4) << v->chi2()
518     << " ndof " << std::setw(3) << v->ndof() << endl
519     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v->x()
520     << " dx " << std::setw(8) << v->xError()<< endl
521     << " y " << std::setw(8) << v->y()
522     << " dy " << std::setw(8) << v->yError()<< endl
523     << " z " << std::setw(8) << v->z()
524     << " dz " << std::setw(8) << v->zError()
525     << endl;
526     }
527     }
528    
529     void PVStudy::printSimTrks(const Handle<SimTrackContainer> simTrks){
530     cout << " simTrks type, (momentum), vertIndex, genpartIndex" << endl;
531     int i=1;
532     for(SimTrackContainer::const_iterator t=simTrks->begin();
533     t!=simTrks->end(); ++t){
534     //HepMC::GenParticle* gp=evtMC->GetEvent()->particle( (*t).genpartIndex() );
535     cout << i++ << ")"
536     << (*t)
537     << " index="
538     << (*t).genpartIndex();
539     //if (gp) {
540     // HepMC::GenVertex *gv=gp->production_vertex();
541     // cout << " genvertex =" << (*gv);
542     //}
543     cout << endl;
544     }
545     }
546    
547     PVStudy::PVAnaInfo PVStudy::GetPVAnaInfo(int ntk) {
548     map<int,double> data;
549 jengbou 1.2 data.clear();
550 jengbou 1.1 double xmin=0.,xmax=0.;
551     double fpar[2];
552     double cm2um = 10000;
553     stringstream ss;
554     ss << ntk;
555     string suffix = ss.str();
556    
557     for ( int i = 0; i < 6; ++i) {
558     switch(i){
559     case 0:
560     case 1:
561     case 2:
562     if (verbose_) cout << "Analyzing Resolution:" << endl;
563     xmin = -0.01; xmax = 0.01;
564     break;
565     default:
566     if (verbose_) cout << "Analyzing Pull:" << endl;
567     xmin = -10.; xmax = 10;
568     break;
569     }
570     TH1D *hdist = new TH1D("hdist","distribution to be fitted", 1000,xmin,xmax);
571    
572     for (std::vector<PVStudy::PVAnaInfo>::const_iterator ipvinfo = pvanainfo.begin();
573     ipvinfo != pvanainfo.end(); ++ipvinfo){
574     if ( verbose_ && i==0 ) {
575     cout << "Num of tracks of the PV = " << ipvinfo->nTrk;
576     cout << "; delta x = " << ipvinfo->resx << endl;
577     }
578     if (ipvinfo->nTrk == ntk) {
579     switch(i) {
580     case 0: if (verbose_) cout << "Matched resx" << endl;
581     hdist->Fill(ipvinfo->resx);
582     h["resx_"+suffix]->Fill(ipvinfo->resx);
583     break;
584     case 1: if (verbose_) cout << "Matched resy" << endl;
585     hdist->Fill(ipvinfo->resy);
586     h["resy_"+suffix]->Fill(ipvinfo->resy);
587     break;
588     case 2: if (verbose_) cout << "Matched resz" << endl;
589     hdist->Fill(ipvinfo->resz);
590     h["resz_"+suffix]->Fill(ipvinfo->resz);
591     break;
592     case 3: if (verbose_) cout << "Matched pullx" << endl;
593     hdist->Fill(ipvinfo->pullx);
594     h["pullx_"+suffix]->Fill(ipvinfo->pullx);
595     break;
596     case 4: if (verbose_) cout << "Matched pully" << endl;
597     hdist->Fill(ipvinfo->pully);
598     h["pully_"+suffix]->Fill(ipvinfo->pully);
599     break;
600     case 5: if (verbose_) cout << "Matched pullz" << endl;
601     hdist->Fill(ipvinfo->pullz);
602     h["pullz_"+suffix]->Fill(ipvinfo->pullz);
603     break;
604     }
605     }
606     else {
607     // switch(i) {
608     // case 0: if (verbose_) cout << "bad resx" << endl; break;
609     // case 1: if (verbose_) cout << "bad resy" << endl; break;
610     // case 2: if (verbose_) cout << "bad resz" << endl; break;
611     // case 3: if (verbose_) cout << "bad pullx" << endl; break;
612     // case 4: if (verbose_) cout << "bad pully" << endl; break;
613     // case 5: if (verbose_) cout << "bad pullz" << endl; break;
614     // }
615     }
616     }
617 jengbou 1.2 if(analyze_) {
618     TAxis *axis0 = hdist->GetXaxis();
619     int nbin = axis0->GetLast();
620     double nOF = hdist->GetBinContent(nbin+1);
621     double nUF = hdist->GetBinContent(0);
622     if ( verbose_ && i==0 ){
623     cout << "Last bin = " << nbin;
624     cout << "; hdist: Overflow Entries = " << nOF;
625     cout << "; Underflow Entries = " << nUF;
626     cout << "; hdist->GetEntries() = " << hdist->GetEntries() << endl;
627     }
628     if (hdist->GetEntries() - nOF - nUF >= 5) { // FIXME: for reasonable Gaussian fit
629     if(i<3){
630     hdist->Fit("gaus","Q0");
631     TF1 *fgaus = hdist->GetFunction("gaus");
632     if (verbose_) cout << "got function" << endl;
633     fpar[0] = ((fgaus->GetParameter(1))?fgaus->GetParameter(1):-999);
634     fpar[1] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
635     delete fgaus;
636     }
637     else {
638     hdist->Fit("gausn","Q0");
639     TF1 *fgaus = hdist->GetFunction("gausn");
640     if (verbose_) cout << "got function" << endl;
641     fpar[0] = ((fgaus->GetParameter(1))?fgaus->GetParameter(1):-999);
642     fpar[1] = ((fgaus->GetParameter(2))?fgaus->GetParameter(2):-999);
643     delete fgaus;
644     }
645     if (verbose_) cout << "fpar[2] = (" << fpar[0] << "," << fpar[1] << ")" << endl;
646     switch (i) {
647     case 0:
648     h["resx_"+suffix]->Fit("gaus","Q");
649     data[i] = fpar[1]*cm2um;
650     break;
651     case 1:
652     h["resy_"+suffix]->Fit("gaus","Q");
653     data[i] = fpar[1]*cm2um;
654     break;
655     case 2:
656     h["resz_"+suffix]->Fit("gaus","Q");
657     data[i] = fpar[1]*cm2um;
658     break;
659     case 3:
660     h["pullx_"+suffix]->Fit("gausn","Q");
661     data[i] = fpar[1];
662     break;
663     case 4:
664     h["pully_"+suffix]->Fit("gausn","Q");
665     data[i] = fpar[1];
666     break;
667     case 5:
668     h["pullz_"+suffix]->Fit("gausn","Q");
669     data[i] = fpar[1];
670     break;
671     }
672     if (verbose_ && data[i] > 0) cout << "data[" << i << "] = " << data[i] << (i<3?" micro m":" cm") << endl;
673 jengbou 1.1 }
674     else {
675 jengbou 1.2 if ( verbose_) cout << "Empty Histo! No Fit!!!" << endl;
676     data[i] = -999;
677 jengbou 1.1 }
678     }
679 jengbou 1.2 else
680 jengbou 1.1 data[i] = -999;
681     delete hdist;
682     }
683    
684     return PVStudy::PVAnaInfo(data[0], data[1], data[2],
685     data[3], data[4], data[5],
686     ntk);
687     }
688    
689     //define this as a plug-in
690     DEFINE_FWK_MODULE(PVStudy);