ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/PVStudy/plugins/PVEffAnalyzer.cc
Revision: 1.2
Committed: Tue Apr 20 09:51:02 2010 UTC (15 years ago) by yygao
Content type: text/plain
Branch: MAIN
Changes since 1.1: +40 -31 lines
Log Message:
add the pre-requirement of cluster

File Contents

# User Rev Content
1 yygao 1.2
2 yygao 1.1 // -*- C++ -*-
3     //
4     // Package: PVEffAnalyzer
5     // Class: PVEffAnalyzer
6     //
7     /**\class PVEffAnalyzer PVEffAnalyzer.cc UserCode/PVEffAnalyzer/plugins/PVEffAnalyzer.cc
8    
9     Description: <one line class summary>
10    
11     Implementation:
12     <Notes on implementation>
13     */
14     //
15     // Original Author: "Geng-yuan Jeng/UC Riverside"
16     // "Yanyan Gao/Fermilab ygao@fnal.gov"
17     // Created: Thu Aug 20 11:55:40 CDT 2009
18 yygao 1.2 // $Id: PVEffAnalyzer.cc,v 1.1 2010/04/19 16:07:17 yygao Exp $
19 yygao 1.1 //
20     //
21    
22    
23     // system include files
24     #include <memory>
25     #include <string>
26     #include <vector>
27     #include <iostream>
28     #include <sstream>
29    
30     // user include files
31     #include "FWCore/Framework/interface/Frameworkfwd.h"
32     #include "FWCore/Framework/interface/EDAnalyzer.h"
33    
34     #include "FWCore/Framework/interface/Event.h"
35     #include "FWCore/Framework/interface/MakerMacros.h"
36    
37     #include "FWCore/ParameterSet/interface/ParameterSet.h"
38     #include "FWCore/Utilities/interface/InputTag.h"
39     #include "DataFormats/TrackReco/interface/Track.h"
40     #include "DataFormats/TrackReco/interface/TrackFwd.h"
41     #include "FWCore/ServiceRegistry/interface/Service.h"
42     #include "CommonTools/UtilAlgos/interface/TFileService.h"
43     #include "UserCode/PVStudy/interface/PVEffAnalyzer.h"
44     //
45     #include "DataFormats/VertexReco/interface/Vertex.h"
46     #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
47    
48     // simulated vertices,..., add <use name=SimDataFormats/Vertex> and <../Track>
49     #include <SimDataFormats/Vertex/interface/SimVertex.h>
50     #include <SimDataFormats/Vertex/interface/SimVertexContainer.h>
51     #include <SimDataFormats/Track/interface/SimTrack.h>
52     #include <SimDataFormats/Track/interface/SimTrackContainer.h>
53 yygao 1.2
54 yygao 1.1 // BeamSpot
55     #include "DataFormats/BeamSpot/interface/BeamSpot.h"
56 yygao 1.2 // For the clusters
57     #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
58     #include "TrackingTools/Records/interface/TransientTrackRecord.h"
59     #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
60     #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
61 yygao 1.1
62     //root
63     #include <TROOT.h>
64     #include <TF1.h>
65     #include <TString.h>
66     #include <TStyle.h>
67     #include <TPaveStats.h>
68     #include <TPad.h>
69    
70     using namespace std;
71 yygao 1.2 using namespace edm;
72     using namespace reco;
73    
74 yygao 1.1 typedef math::XYZTLorentzVectorF LorentzVector;
75     typedef math::XYZPoint Point;
76    
77     PVEffAnalyzer::PVEffAnalyzer(const edm::ParameterSet& iConfig)
78 yygao 1.2 :theTrackClusterizer(iConfig.getParameter<edm::ParameterSet>("TkClusParameters"))
79 yygao 1.1 {
80     //=======================================================================
81     // Get configuration for TrackTupleMaker
82     //=======================================================================
83     simG4_ = iConfig.getParameter<edm::InputTag>( "simG4" );
84     trackCollectionTag_ = iConfig.getUntrackedParameter<edm::InputTag>("trackCollection");
85     splitTrackCollection1Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection1");
86     splitTrackCollection2Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitTrackCollection2");
87     vertexCollectionTag_ = iConfig.getUntrackedParameter<edm::InputTag>("vertexCollection");
88     splitVertexCollection1Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection1");
89     splitVertexCollection2Tag_ = iConfig.getUntrackedParameter<edm::InputTag>("splitVertexCollection2");
90     verbose_ = iConfig.getUntrackedParameter<bool>("verbose",false);
91     realData_ = iConfig.getUntrackedParameter<bool>("realData",false);
92     histoFileName_ = iConfig.getUntrackedParameter<std::string> ("histoFileName");
93     nTrkMin_ = iConfig.getUntrackedParameter<int>("nTrkMin");
94     nTrkMax_ = iConfig.getUntrackedParameter<int>("nTrkMax");
95     zsigncut_ = iConfig.getUntrackedParameter<double>("zsigncut");
96 yygao 1.2 analyze_ = iConfig.getUntrackedParameter<bool>("analyze",false);
97 yygao 1.1 bsSrc = iConfig.getParameter< edm::InputTag >("beamSpot");
98 yygao 1.2 reqCluster_ = iConfig.getUntrackedParameter<bool>("reqCluster",false);
99    
100 yygao 1.1 // Specify the data mode vector
101     if(realData_) datamode.push_back(0);
102     else {
103     datamode.push_back(0);
104     datamode.push_back(1);
105     }
106    
107     theFile = new TFile(histoFileName_.c_str(), "RECREATE");
108     theFile->mkdir("Summary");
109     theFile->cd();
110    
111     // Book MC only plots
112     if (!realData_) {
113     h_gen = new PVEffHistograms();
114     h_gen->Init("generator");
115     }
116    
117     h_summary = new PVEffHistograms();
118     //Book histograms sensitive to data/mc
119     for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {
120     string suffix;
121     edm::LogInfo("Debug")<<"datamode = "<< *it<<endl;
122     switch(*it) {
123     case 0: suffix = "";
124     break;
125     case 1: suffix = "_mct";
126     break;
127     }
128     h_summary->Init("summary",suffix, nTrkMin_, nTrkMax_);
129     }
130    
131     }
132    
133     PVEffAnalyzer::~PVEffAnalyzer()
134     {
135     // do anything here that needs to be done at desctruction time
136     // (e.g. close files, deallocate resources etc.)
137     theFile->cd();
138     theFile->cd("Summary");
139     h_summary->Save();
140     if (!realData_)
141     h_gen->Save();
142     theFile->Close();
143     }
144    
145     //
146     // member functions
147     //
148     std::vector<PVEffAnalyzer::simPrimaryVertex> PVEffAnalyzer::getSimPVs(const Handle<HepMCProduct> evtMC, std::string suffix="")
149     {
150     std::vector<PVEffAnalyzer::simPrimaryVertex> simpv;
151     const HepMC::GenEvent* evt=evtMC->GetEvent();
152     if (evt) {
153     edm::LogInfo("SimPVs") << "[getSimPVs] process id " << evt->signal_process_id()<<endl;
154     edm::LogInfo("SimPVs") << "[getSimPVs] signal process vertex " << ( evt->signal_process_vertex() ?
155     evt->signal_process_vertex()->barcode() : 0 ) <<endl;
156     edm::LogInfo("SimPVs") << "[getSimPVs] number of vertices " << evt->vertices_size() << endl;
157    
158     int idx=0; int npv=0;
159     for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
160     vitr != evt->vertices_end(); ++vitr ) { // loop for vertex ...
161     HepMC::FourVector pos = (*vitr)->position();
162     //HepLorentzVector pos = (*vitr)->position();
163    
164     // t component of PV:
165     for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
166     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
167     // edm::LogInfo("SimPVs") << "Status = " << (*mother)->status() << endl;
168     HepMC::GenVertex * mv=(*mother)->production_vertex();
169     if( ((*mother)->status() == 3) && (!mv)) {
170     // edm::LogInfo("SimPVs") << "npv= " << npv << endl;
171     if (npv == 0) {
172     h_gen->Fill1d("genPart_cT", pos.t()); // mm
173     h_gen->Fill1d("genPart_T", pos.t()/299.792458); // ns
174     }
175     npv++;
176     }
177     }
178     // if (pos.t()>0) { continue;} // for 22X when t of PV was not smeared
179    
180     bool hasMotherVertex=false;
181     if (verbose_) cout << "[getSimPVs] mothers of vertex[" << ++idx << "]: " << endl;
182     for ( HepMC::GenVertex::particle_iterator mother = (*vitr)->particles_begin(HepMC::parents);
183     mother != (*vitr)->particles_end(HepMC::parents); ++mother ) {
184     HepMC::GenVertex * mv=(*mother)->production_vertex();
185     // if (verbose_) cout << "Status = " << (*mother)->status() << endl;
186     if (mv) {
187     hasMotherVertex=true;
188     if(!verbose_) break; //if verbose_, print all particles of gen vertices
189     }
190     if(verbose_) {
191     cout << "\t";
192     (*mother)->print();
193     }
194     }
195    
196     if(hasMotherVertex) continue;
197    
198     // could be a new vertex, check all primaries found so far to avoid multiple entries
199     const double mm2cm=0.1;
200     simPrimaryVertex sv(pos.x()*mm2cm,pos.y()*mm2cm,pos.z()*mm2cm); // sim unit mm, rec unit cm
201     simPrimaryVertex *vp=NULL; // will become non-NULL if a vertex is found and then point to it
202     for(vector<simPrimaryVertex>::iterator v0=simpv.begin();
203     v0!=simpv.end(); v0++){
204     if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
205     vp=&(*v0);
206     break;
207     }
208     }
209    
210     if(!vp){
211     // this is a new vertex
212     edm::LogInfo("SimPVs") << "[getSimPVs] this is a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
213     simpv.push_back(sv);
214     vp=&simpv.back();
215     }else{
216     edm::LogInfo("SimPVs") << "[getSimPVs] this is not a new vertex " << sv.x << " " << sv.y << " " << sv.z << endl;
217     }
218     vp->genVertex.push_back((*vitr)->barcode());
219     // collect final state descendants
220     for ( HepMC::GenVertex::particle_iterator 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     }//loop MC vertices daughters
241     }//loop MC vertices
242     }
243     return simpv;
244     }
245    
246     std::vector<PVEffAnalyzer::simPrimaryVertex> PVEffAnalyzer::getSimPVs(const Handle<HepMCProduct> evtMC,
247     const Handle<SimVertexContainer> simVtxs,
248     const Handle<SimTrackContainer> simTrks)
249     {
250     // simvertices don't have enough information to decide,
251     // genVertices don't have the simulated coordinates ( with VtxSmeared they might)
252     // go through simtracks to get the link between simulated and generated vertices
253     std::vector<PVEffAnalyzer::simPrimaryVertex> simpv;
254     int idx=0;
255     for(SimTrackContainer::const_iterator t=simTrks->begin();
256     t!=simTrks->end(); ++t){
257     if ( !(t->noVertex()) && !(t->type()==-99) ){
258     double ptsq=0;
259     bool primary=false; // something coming directly from the primary vertex
260     bool resonance=false; // resonance
261     bool track=false; // undecayed, charged particle
262     HepMC::GenParticle* gp=evtMC->GetEvent()->barcode_to_particle( (*t).genpartIndex() );
263     if (gp) {
264     HepMC::GenVertex * gv=gp->production_vertex();
265     if (gv) {
266     for ( HepMC::GenVertex::particle_iterator
267     daughter = gv->particles_begin(HepMC::descendants);
268     daughter != gv->particles_end(HepMC::descendants);
269     ++daughter ) {
270     if (isFinalstateParticle(*daughter)){
271     ptsq+=(*daughter)->momentum().perp()*(*daughter)->momentum().perp();
272     }
273     }
274     //primary = ( gv->position().t()==0 );
275     primary = true;
276     h_gen->Fill1d("genPart_cT", gv->position().t()); // mm
277     h_gen->Fill1d("genPart_T", gv->position().t()/299.792458); // ns
278    
279     //resonance= ( gp->mother() && isResonance(gp->mother())); // in CLHEP/HepMC days
280     // no more mother pointer in the improved HepMC GenParticle
281     resonance= ( isResonance(*(gp->production_vertex()->particles_in_const_begin())));
282     if (gp->status()==1){
283     //track=((pdt->particle(gp->pdg_id()))->charge() != 0);
284     track=not isCharged(gp);
285     }
286     }
287     }
288    
289     const HepMC::FourVector & v=(*simVtxs)[t->vertIndex()].position();
290     //const HepLorentzVector & v=(*simVtxs)[t->vertIndex()].position();
291     if(primary or resonance){
292     {
293     // check all primaries found so far to avoid multiple entries
294     bool newVertex=true;
295     for(std::vector<PVEffAnalyzer::simPrimaryVertex>::iterator v0=simpv.begin();
296     v0!=simpv.end(); v0++){
297     if( (fabs(v0->x-v.x())<0.001) && (fabs(v0->y-v.y())<0.001) && (fabs(v0->z-v.z())<0.001) ){
298     if (track) {
299     v0->simTrackIndex.push_back(idx);
300     if (ptsq>(*v0).ptsq){(*v0).ptsq=ptsq;}
301     }
302     newVertex=false;
303     }
304     }
305     if(newVertex && !resonance){
306     PVEffAnalyzer::simPrimaryVertex anotherVertex(v.x(),v.y(),v.z());
307     if (track) anotherVertex.simTrackIndex.push_back(idx);
308     anotherVertex.ptsq=ptsq;
309     simpv.push_back(anotherVertex);
310     }
311     }//
312     }
313    
314     }// simtrack has vertex and valid type
315     idx++;
316     }//simTrack loop
317     return simpv;
318     }
319    
320     // ------------ method called to for each event ------------
321     void
322     PVEffAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
323     {
324     using namespace edm;
325     using namespace reco;
326    
327     //========================================================================
328     // Step 0: Prepare root variables and get information from the Event
329     //========================================================================
330    
331     edm::LogInfo("Debug")<<"[PVEffAnalyzer]"<<endl;
332    
333     // ====== TrackCollection
334     static const reco::TrackCollection s_empty_trackColl;
335     const reco::TrackCollection *trackColl = &s_empty_trackColl;
336     edm::Handle<reco::TrackCollection> trackCollectionHandle;
337     iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle);
338     if( iEvent.getByLabel(trackCollectionTag_, trackCollectionHandle)) {
339     trackColl = trackCollectionHandle.product();
340     } else {
341     edm::LogInfo("Debug") << "[PVEffAnalyzer] trackCollection cannot be found -> using empty collection of same type." <<endl;
342     }
343     // ====== splitTrackCollection1
344     static const reco::TrackCollection s_empty_splitTrackColl1;
345     const reco::TrackCollection *splitTrackColl1 = &s_empty_splitTrackColl1;
346     edm::Handle<reco::TrackCollection> splitTrackCollection1Handle;
347     iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle);
348     if( iEvent.getByLabel(splitTrackCollection1Tag_, splitTrackCollection1Handle)) {
349     splitTrackColl1 = splitTrackCollection1Handle.product();
350     } else {
351     edm::LogInfo("Debug") << "[PVEffAnalyzer] splitTrackCollection1 cannot be found -> using empty collection of same type." <<endl;
352     }
353     // ====== splitTrackCollection2
354     static const reco::TrackCollection s_empty_splitTrackColl2;
355     const reco::TrackCollection *splitTrackColl2 = &s_empty_splitTrackColl2;
356     edm::Handle<reco::TrackCollection> splitTrackCollection2Handle;
357     iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle);
358     if( iEvent.getByLabel(splitTrackCollection2Tag_, splitTrackCollection2Handle)) {
359     splitTrackColl2 = splitTrackCollection2Handle.product();
360     } else {
361     edm::LogInfo("Debug") << "[PVEffAnalyzer] splitTrackCollection2 cannot be found -> using empty collection of same type." <<endl;
362     }
363    
364     // ======= PrimaryVertexCollection
365     static const reco::VertexCollection s_empty_vertexColl;
366     const reco::VertexCollection *vertexColl = &s_empty_vertexColl;
367     edm::Handle<reco::VertexCollection> vertexCollectionHandle;
368     iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle);
369     if( iEvent.getByLabel(vertexCollectionTag_, vertexCollectionHandle)) {
370     vertexColl = vertexCollectionHandle.product();
371     } else {
372     edm::LogInfo("Debug") << "[PVEffAnalyzer] vertexCollection cannot be found -> using empty collection of same type." <<endl;
373     }
374     // ====== splitVertexCollection1
375     static const reco::VertexCollection s_empty_splitVertexColl1;
376     const reco::VertexCollection *splitVertexColl1 = &s_empty_splitVertexColl1;
377     edm::Handle<reco::VertexCollection> splitVertexCollection1Handle;
378     iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle);
379     if( iEvent.getByLabel(splitVertexCollection1Tag_, splitVertexCollection1Handle)) {
380     splitVertexColl1 = splitVertexCollection1Handle.product();
381     } else {
382     edm::LogInfo("Debug") << "[PVEffAnalyzer] splitVertexCollection1 cannot be found -> using empty collection of same type." <<endl;
383     }
384     // ====== splitVertexCollection2
385     static const reco::VertexCollection s_empty_splitVertexColl2;
386     const reco::VertexCollection *splitVertexColl2 = &s_empty_splitVertexColl2;
387     edm::Handle<reco::VertexCollection> splitVertexCollection2Handle;
388     iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle);
389     if( iEvent.getByLabel(splitVertexCollection2Tag_, splitVertexCollection2Handle)) {
390     splitVertexColl2 = splitVertexCollection2Handle.product();
391     } else {
392     edm::LogInfo("Debug") << "[PVEffAnalyzer] splitVertexCollection2 cannot be found -> using empty collection of same type." <<endl;
393     }
394    
395    
396     // ======== BeamSpot accessors
397     edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
398     iEvent.getByLabel(bsSrc,recoBeamSpotHandle);
399     reco::BeamSpot bs = *recoBeamSpotHandle;
400     const Point beamSpot = recoBeamSpotHandle.isValid() ? Point(recoBeamSpotHandle->x0(), recoBeamSpotHandle->y0(), recoBeamSpotHandle->z0()) : Point(0, 0, 0);
401    
402     edm::LogInfo("Debug")<<"[PVEffAnalyzer] End accessing the track, beamSpot, primary vertex collections"<<endl;
403    
404     // ========== MC simvtx accessor
405     if (!realData_) {
406     edm::Handle<SimVertexContainer> simVtxs;
407     iEvent.getByLabel( simG4_, simVtxs);
408    
409     edm::Handle<SimTrackContainer> simTrks;
410     iEvent.getByLabel( simG4_, simTrks);
411     }
412    
413     // ========== GET PDT
414     try{
415     iSetup.getData(pdt);
416     }catch(const Exception&){
417     edm::LogInfo("Debug") << "[PVEffAnalyzer] Some problem occurred with the particle data table. This may not work !." <<endl;
418     }
419    
420     //setUpVectors(RealData, nTrkMin_, nTrkMax_ ) ;
421    
422     // ======= Analyze MC efficiency
423     if (!realData_) {
424     bool MC=false;
425     Handle<HepMCProduct> evtMC;
426     iEvent.getByLabel("generator",evtMC);
427     if (!evtMC.isValid()) {
428     MC=false;
429     edm::LogInfo("Debug") << "[PVEffAnalyzer] no HepMCProduct found"<< endl;
430     } else {
431     edm::LogInfo("Debug") << "[PVEffAnalyzer] generator HepMCProduct found"<< endl;
432     MC=true;
433     }
434     if(MC){
435     TString suffix = "_mct";
436     // make a list of primary vertices:
437     std::vector<simPrimaryVertex> simpv;
438     simpv=getSimPVs(evtMC,"");
439     // simpv=getSimPVs(evtMC, simVtxs, simTrks);
440     h_gen->Fill1d("nsimPV", simpv.size());
441    
442     int isimpv = 0;
443    
444     for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
445     vsim!=simpv.end(); vsim++, isimpv++){
446     //nsimTrkPV_[isimpv] =vsim->nGenTrk;
447     //simx_[isimpv] = vsim->x;
448     //simy_[isimpv] = vsim->y;
449     //simz_[isimpv] = vsim->y;
450     //simptsq_[isimpv] = vsim->ptsq;
451     if(verbose_ && simpv.size() > 1 )
452     std::cout<<"Simulated Vertex # " << isimpv << ": ptsq = " << vsim->ptsq<<std::endl;
453     }
454    
455     // Just analyze the first vertex in the collection
456     simPrimaryVertex vsim = *simpv.begin();
457     h_summary->Fill1d(TString("denom_ntrack"+suffix), int(vsim.nGenTrk));
458     if ( isAssoVertex(vsim, *vertexColl->begin(), zsigncut_) )
459     h_summary->Fill1d(TString("numer_ntrack"+suffix), int(vsim.nGenTrk));
460     }
461     } // End of Analyzing MC Efficiency
462    
463 yygao 1.2 // Get the Builder for TrackClusters
464     edm::ESHandle<TransientTrackBuilder> theB;
465     iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theB);
466    
467     std::vector< std::vector<reco::TransientTrack> > clusters1 = theTrackClusterizer.clusterize((*theB).build(splitTrackCollection1Handle));
468     std::vector< std::vector<reco::TransientTrack> > clusters2 = theTrackClusterizer.clusterize((*theB).build(splitTrackCollection2Handle));
469    
470     bool withValidCluster_tag = false;
471     bool withValidCluster_probe = false;
472    
473     for (vector< vector<reco::TransientTrack> >::const_iterator iclus = clusters1.begin(); iclus != clusters1.end(); iclus++) {
474     if((*iclus).size()>1) withValidCluster_probe = true;
475     }
476     for (vector< vector<reco::TransientTrack> >::const_iterator iclus = clusters2.begin(); iclus != clusters2.end(); iclus++) {
477     if((*iclus).size()>1) withValidCluster_tag = true;
478     }
479    
480 yygao 1.1 // ======= Analyze efficiency with split method
481 yygao 1.2 if( reqCluster_ ) {
482     if ( !isGoodSplitEvent( *vertexColl->begin()) || !withValidCluster_tag || !withValidCluster_probe ) return;
483     }
484     else {
485     if ( !isGoodSplitEvent( *vertexColl->begin())) return;
486     }
487 yygao 1.1 if ( isGoodTagVertex( *splitVertexColl2->begin(), *vertexColl->begin(), zsigncut_) ) {
488     if(verbose_) {
489     std::cout<<"splitTrackColl1->size() = " << int(splitTrackColl1->size()) << std::endl;
490     std::cout<<"splitTrackColl2->size() = " << int(splitTrackColl2->size()) << std::endl;
491     }
492     h_summary->Fill1d("denom_ntrack", int(splitTrackColl1->size()));
493     if ( isGoodProbeVertex( *splitVertexColl1->begin(), *vertexColl->begin(), zsigncut_) )
494     h_summary->Fill1d("numer_ntrack", int(splitTrackColl1->size()) );
495     }
496     }
497    
498    
499     // ------------ method called once each job just before starting event loop ------------
500     void
501     PVEffAnalyzer::beginJob()
502     {
503     }
504    
505     // ------------ method called once each job just after ending the event loop ------------
506     void
507     PVEffAnalyzer::endJob() {
508     edm::LogInfo("Analysis") << "[endJob] Analyzing PV info" << endl;
509    
510     for (vector<int>::const_iterator it= datamode.begin(); it != datamode.end() ; ++it) {
511     string suffix;
512     edm::LogInfo("Debug")<<"datamode = "<< *it<<endl;
513     switch(*it) {
514     case 0: suffix = "";
515     break;
516     case 1: suffix = "_mct";
517     break;
518     }
519 yygao 1.2 if (analyze_)
520     MakeEff(h_summary->ReadHisto1D(TString("numer_ntrack"+suffix)), h_summary->ReadHisto1D(TString("denom_ntrack"+suffix)), h_summary->ReadHisto1D(TString("eff_ntrack"+suffix)), false, 1);
521 yygao 1.1 }
522     }
523    
524    
525    
526     bool PVEffAnalyzer::isResonance(const HepMC::GenParticle * p){
527     double ctau=(pdt->particle( abs(p->pdg_id()) ))->lifetime();
528     edm::LogInfo("Debug") << "[isResonance] isResonance " << p->pdg_id() << " " << ctau << endl;
529     return ctau >0 && ctau <1e-6;
530     }
531    
532     bool PVEffAnalyzer::isFinalstateParticle(const HepMC::GenParticle * p){
533     return ( !p->end_vertex() && p->status()==1 );
534     }
535    
536     bool PVEffAnalyzer::isCharged(const HepMC::GenParticle * p){
537     const ParticleData * part = pdt->particle( p->pdg_id() );
538     if (part){
539     return part->charge()!=0;
540     }else{
541     // the new/improved particle table doesn't know anti-particles
542     return pdt->particle( -p->pdg_id() )!=0;
543     }
544     }
545    
546     void PVEffAnalyzer::printSimVtxs(const Handle<SimVertexContainer> simVtxs){
547     int i=0;
548     for(SimVertexContainer::const_iterator vsim=simVtxs->begin();
549     vsim!=simVtxs->end(); ++vsim){
550     cout << i++ << ")" << scientific
551     << " evtid=" << vsim->eventId().event()
552     << " sim x=" << vsim->position().x()
553     << " sim y=" << vsim->position().y()
554     << " sim z=" << vsim->position().z()
555     << " sim t=" << vsim->position().t()
556     << " parent=" << vsim->parentIndex()
557     << endl;
558     }
559     }
560    
561     void PVEffAnalyzer::printRecVtxs(const Handle<reco::VertexCollection> recVtxs){
562     int ivtx=0;
563     for(reco::VertexCollection::const_iterator v=recVtxs->begin();
564     v!=recVtxs->end(); ++v){
565     cout << "Recvtx "<< std::setw(3) << std::setfill(' ')<<ivtx++
566     << "#trk " << std::setw(3) << v->tracksSize()
567     << " chi2 " << std::setw(4) << v->chi2()
568     << " ndof " << std::setw(3) << v->ndof() << endl
569     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v->x()
570     << " dx " << std::setw(8) << v->xError()<< endl
571     << " y " << std::setw(8) << v->y()
572     << " dy " << std::setw(8) << v->yError()<< endl
573     << " z " << std::setw(8) << v->z()
574     << " dz " << std::setw(8) << v->zError()
575     << endl;
576     }
577     }
578    
579     void PVEffAnalyzer::printRecVtx(const reco::Vertex & v){
580    
581     cout << "#trk " << std::setw(3) << v.tracksSize()
582     << " chi2 " << std::setw(4) << v.chi2()
583     << " ndof " << std::setw(3) << v.ndof() << endl
584     << " x " << std::setw(8) <<std::fixed << std::setprecision(4) << v.x()
585     << " dx " << std::setw(8) << v.xError()<< endl
586     << " y " << std::setw(8) << v.y()
587     << " dy " << std::setw(8) << v.yError()<< endl
588     << " z " << std::setw(8) << v.z()
589     << " dz " << std::setw(8) << v.zError()
590     << endl;
591     }
592    
593     void PVEffAnalyzer::printSimTrks(const Handle<SimTrackContainer> simTrks){
594     cout << " simTrks type, (momentum), vertIndex, genpartIndex" << endl;
595     int i=1;
596     for(SimTrackContainer::const_iterator t=simTrks->begin();
597     t!=simTrks->end(); ++t){
598     //HepMC::GenParticle* gp=evtMC->GetEvent()->particle( (*t).genpartIndex() );
599     cout << i++ << ")"
600     << (*t)
601     << " index="
602     << (*t).genpartIndex();
603     //if (gp) {
604     // HepMC::GenVertex *gv=gp->production_vertex();
605     // cout << " genvertex =" << (*gv);
606     //}
607     cout << endl;
608     }
609     }
610    
611     // Select based on the highest SumPtSq Vertex
612     // Require at least 6 tracks with track weight > ~ 0.8
613     // vtx.ndof = 2*Sum(trackWeight) - 3
614    
615     bool PVEffAnalyzer::isGoodSplitEvent( const reco::Vertex & org_vtx)
616     //, const reco::Vertex & tag_vtx, const reco::Vertex & probe_vtx)
617     {
618     return ( org_vtx.tracksSize() >= 6 && org_vtx.ndof() >= 6 );
619     // tag_vtx.tracksSize() > = 2 && probe_vtx.tracksSize() > = 2 ) ;
620     }
621    
622     // Comparing the tag vertex with the unsplit vertex position in z
623     bool PVEffAnalyzer::isGoodTagVertex( const reco::Vertex & tag_vtx, const reco::Vertex & org_vtx, double & zsign_cut)
624     {
625     if(tag_vtx.isValid () && !tag_vtx.isFake()) {
626     float zsign = TMath::Abs(tag_vtx.z() - org_vtx.z() ) / TMath::Max(tag_vtx.zError(), org_vtx.zError() );
627     return (zsign < zsign_cut) ;
628     }
629     else
630     return false;
631     }
632    
633     // Comparing the probe vertex with the unsplit vertex position in z
634     bool PVEffAnalyzer::isGoodProbeVertex( const reco::Vertex & probe_vtx, const reco::Vertex & org_vtx, double & zsign_cut)
635     {
636     if(probe_vtx.isValid () && !probe_vtx.isFake()) {
637     float zsign = TMath::Abs(probe_vtx.z() - org_vtx.z() ) / TMath::Max(probe_vtx.zError(), org_vtx.zError() );
638     return (zsign < zsign_cut) ;
639     }
640     else
641     return false;
642     }
643    
644    
645     bool PVEffAnalyzer::isAssoVertex(const PVEffAnalyzer::simPrimaryVertex & vsim,
646     const reco::Vertex & vrec, double & zsign_cut)
647     {
648     if(vrec.isValid() && !vrec.isFake() )
649     return (TMath::Abs(vsim.z-vrec.z()) < zsign_cut * vrec.zError() );
650     else
651     return false;
652     }
653    
654    
655     void PVEffAnalyzer::MakeEff(TH1D* numer, TH1D* denom, TH1D* eff, //TGraphAsymmErrors* & gr_eff,
656     const bool rebin, const Float_t n ) {
657     eff->Divide( numer, denom, 1,1,"B" );
658    
659     if( rebin ) {
660     std::vector<Double_t> binEdges;
661     Int_t bin = denom->GetNbinsX() + 1;
662     binEdges.push_back(denom->GetBinLowEdge(bin));
663     Float_t nEntries = 0;
664     while (bin > 1) {
665     bin --;
666     nEntries = denom->GetBinContent(bin);
667     while (nEntries < n && bin > 1) {
668     bin --;
669     nEntries += denom->GetBinContent(bin);
670     }
671     binEdges.push_back(denom->GetBinLowEdge(bin));
672     }
673    
674     Double_t *array = new Double_t[binEdges.size()];
675    
676     Int_t j = 0;
677     for (Int_t i = binEdges.size(); i > 0; --i) {
678     array[j] = binEdges[i - 1];
679     ++j;
680     }
681     }
682    
683     for (int i = 1; i<eff->GetNbinsX()+1; i++) {
684     if(numer->GetBinContent(i) == 0 || denom->GetBinContent(i) == 0 ) continue;
685     float error = eff->GetBinContent(i)*sqrt(pow(numer->GetBinError(i)/numer->GetBinContent(i),2)
686     +pow(denom->GetBinError(i)/denom->GetBinContent(i),2));
687     eff->SetBinError(i, error);
688     }
689     }