CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/PhysicsTools/PatExamples/plugins/PatTrackAnalyzer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cmath>
00003 #include <vector>
00004 #include <string>
00005 
00006 #include <TH1.h>
00007 #include <TProfile.h>
00008 
00009 #include "FWCore/Framework/interface/Frameworkfwd.h"
00010 #include "FWCore/Framework/interface/Event.h"
00011 #include "FWCore/Framework/interface/EventSetup.h"
00012 #include "FWCore/Framework/interface/EDAnalyzer.h"
00013 #include "FWCore/Utilities/interface/InputTag.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015 #include "FWCore/ServiceRegistry/interface/Service.h"
00016 
00017 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00018 
00019 #include "DataFormats/Common/interface/View.h"
00020 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00021 #include "DataFormats/TrackReco/interface/Track.h"
00022 #include "DataFormats/TrackReco/interface/HitPattern.h"
00023 #include "DataFormats/PatCandidates/interface/Muon.h"
00024 
00025 class PatTrackAnalyzer : public edm::EDAnalyzer  {
00026     public: 
00028         PatTrackAnalyzer(const edm::ParameterSet &params);
00029         ~PatTrackAnalyzer();
00030 
00031         // virtual methods called from base class EDAnalyzer
00032         virtual void beginJob();
00033         virtual void analyze(const edm::Event &event, const edm::EventSetup &es);
00034 
00035     private:
00036         // configuration parameters
00037         edm::InputTag src_;
00038         edm::InputTag beamSpot_;
00039 
00040         // the list of track quality cuts to demand from the tracking
00041         std::vector<std::string> qualities_;
00042 
00043         // holder for the histograms, one set per quality flag
00044         struct Plots {
00045                 TH1 *eta, *phi;
00046                 TH1 *pt, *ptErr;
00047                 TH1 *invPt, *invPtErr;
00048                 TH1 *d0, *d0Err;
00049                 TH1 *nHits;
00050 
00051                 TProfile *pxbHitsEta, *pxeHitsEta;
00052                 TProfile *tibHitsEta, *tobHitsEta;
00053                 TProfile *tidHitsEta, *tecHitsEta;
00054         };
00055 
00056         std::vector<Plots> plots_;
00057 };
00058 
00059 
00060 PatTrackAnalyzer::PatTrackAnalyzer(const edm::ParameterSet &params) :
00061         src_(params.getParameter<edm::InputTag>("src")),
00062         beamSpot_(params.getParameter<edm::InputTag>("beamSpot")),
00063         qualities_(params.getParameter< std::vector<std::string> >("qualities"))
00064 {
00065 }
00066 
00067 PatTrackAnalyzer::~PatTrackAnalyzer()
00068 {
00069 }
00070 
00071 void PatTrackAnalyzer::beginJob()
00072 {
00073         // retrieve handle to auxiliary service
00074         //  used for storing histograms into ROOT file
00075         edm::Service<TFileService> fs;
00076 
00077         // now book the histograms, for each category
00078         unsigned int nQualities = qualities_.size();
00079 
00080         plots_.resize(nQualities);
00081 
00082         for(unsigned int i = 0; i < nQualities; ++i) {
00083                 // the name of the quality flag
00084                 const char *quality = qualities_[i].c_str();
00085 
00086                 // the set of plots
00087                 Plots &plots = plots_[i];
00088 
00089                 plots.eta = fs->make<TH1F>(Form("eta_%s", quality),
00090                                            Form("track \\eta (%s)", quality),
00091                                            100, -3, 3);
00092                 plots.phi = fs->make<TH1F>(Form("phi_%s", quality),
00093                                            Form("track \\phi (%s)", quality),
00094                                            100, -M_PI, +M_PI);
00095                 plots.pt = fs->make<TH1F>(Form("pt_%s", quality),
00096                                           Form("track p_{T} (%s)", quality),
00097                                           100, 0, 10);
00098                 plots.ptErr = fs->make<TH1F>(Form("ptErr_%s", quality),
00099                                              Form("track p_{T} error (%s)", quality),
00100                                              100, 0, 1);
00101                 plots.invPt = fs->make<TH1F>(Form("invPt_%s", quality),
00102                                              Form("track 1/p_{T} (%s)", quality),
00103                                              100, -5, 5);
00104                 plots.invPtErr = fs->make<TH1F>(Form("invPtErr_%s", quality),
00105                                                 Form("track 1/p_{T} error (%s)", quality),
00106                                                 100, 0, 0.1);
00107                 plots.d0 = fs->make<TH1F>(Form("d0_%s", quality),
00108                                           Form("track d0 (%s)", quality),
00109                                                 100, 0, 0.1);
00110                 plots.d0Err = fs->make<TH1F>(Form("d0Err_%s", quality),
00111                                              Form("track d0 error (%s)", quality),
00112                                                    100, 0, 0.1);
00113                 plots.nHits = fs->make<TH1F>(Form("nHits_%s", quality),
00114                                              Form("track number of total hits (%s)", quality),
00115                                                    60, 0, 60);
00116 
00117                 plots.pxbHitsEta = fs->make<TProfile>(Form("pxbHitsEta_%s", quality),
00118                                                       Form("#hits in Pixel Barrel (%s)", quality),
00119                                                            100, 0, 3);
00120                 plots.pxeHitsEta = fs->make<TProfile>(Form("pxeHitsEta_%s", quality),
00121                                                       Form("#hits in Pixel Endcap (%s)", quality),
00122                                                            100, 0, 3);
00123                 plots.tibHitsEta = fs->make<TProfile>(Form("tibHitsEta_%s", quality),
00124                                                       Form("#hits in Tracker Inner Barrel (%s)", quality),
00125                                                            100, 0, 3);
00126                 plots.tobHitsEta = fs->make<TProfile>(Form("tobHitsEta_%s", quality),
00127                                                       Form("#hits in Tracker Outer Barrel (%s)", quality),
00128                                                            100, 0, 3);
00129                 plots.tidHitsEta = fs->make<TProfile>(Form("tidHitsEta_%s", quality),
00130                                                       Form("#hits in Tracker Inner Disk (%s)", quality),
00131                                                            100, 0, 3);
00132                 plots.tecHitsEta = fs->make<TProfile>(Form("tecHitsEta_%s", quality),
00133                                                       Form("#hits in Tracker Endcap (%s)", quality),
00134                                                            100, 0, 3);
00135         }
00136 }
00137 
00138 void PatTrackAnalyzer::analyze(const edm::Event &event, const edm::EventSetup &es)
00139 {  
00140         // handles to kinds of data we might want to read
00141         edm::Handle<reco::BeamSpot> beamSpot;
00142         edm::Handle< edm::View<reco::Track> > tracksHandle;
00143         edm::Handle< pat::MuonCollection > muonsHandle;
00144 
00145         // read the beam spot
00146         event.getByLabel(beamSpot_, beamSpot);
00147 
00148         // our internal copy of track points
00149         // (we need this in order to able to simultaneously access tracks
00150         //  directly or embedded in PAT objects, like muons, normally you
00151         //  would iterate over the handle directly)
00152         std::vector<const reco::Track*> tracks;
00153 
00154         event.getByLabel(src_, tracksHandle);
00155         if (tracksHandle.isValid()) {
00156                 // framework was able to read the collection as a view of
00157                 // tracks, no copy them to our "tracks" variable
00158                 for(edm::View<reco::Track>::const_iterator iter = tracksHandle->begin();
00159                     iter != tracksHandle->end(); ++iter)
00160                         tracks.push_back(&*iter);
00161         } else {
00162                 // does not exist or is not a track collection
00163                 // let's assume it is a collection of PAT muons
00164                 event.getByLabel(src_, muonsHandle);
00165 
00166                 // and copy them over
00167                 // NOTE: We are using ->globalTrack() here
00168                 //       This means we are using the global fit over both
00169                 //       the inner tracker and the muon stations!
00170                 //       other alternatives are: innerTrack(), outerTrack()
00171                 for(pat::MuonCollection::const_iterator iter = muonsHandle->begin();
00172                     iter != muonsHandle->end(); ++iter) {
00173                         reco::TrackRef track = iter->globalTrack();
00174                         // the muon might not be a "global" muon
00175                         if (track.isNonnull())
00176                                 tracks.push_back(&*track);
00177                 }
00178         }
00179 
00180         // we are done filling the tracks into our "tracks" vector.
00181         // now analyze them, once for each track quality category
00182 
00183         unsigned int nQualities = qualities_.size();
00184         for(unsigned int i = 0; i < nQualities; ++i) {
00185                 // we convert the quality flag from its name as a string
00186                 // to the enumeration value used by the tracking code
00187                 // (which is essentially an integer number)
00188                 reco::Track::TrackQuality quality = reco::Track::qualityByName(qualities_[i]);
00189 
00190                 // our set of plots
00191                 Plots &plots = plots_[i];
00192 
00193                 // now loop over the tracks
00194                 for(std::vector<const reco::Track*>::const_iterator iter = tracks.begin();
00195                     iter != tracks.end(); ++iter) {
00196                         // this is our track
00197                         const reco::Track &track = **iter;
00198 
00199                         // ignore tracks that fail the quality cut
00200                         if (!track.quality(quality))
00201                                 continue;
00202 
00203                         // and fill all the plots
00204                         plots.eta->Fill(track.eta());
00205                         plots.phi->Fill(track.phi());
00206 
00207                         plots.pt->Fill(track.pt());
00208                         plots.ptErr->Fill(track.ptError());
00209 
00210                         plots.invPt->Fill(track.qoverp());
00211                         plots.invPtErr->Fill(track.qoverpError());
00212 
00213                         // the transverse IP is taken with respect to
00214                         // the beam spot instead of (0, 0)
00215                         // because the beam spot in CMS is not at (0, 0)
00216                         plots.d0->Fill(track.dxy(beamSpot->position()));
00217                         plots.d0Err->Fill(track.dxyError());
00218 
00219                         plots.nHits->Fill(track.numberOfValidHits());
00220 
00221                         // the hit pattern contains information about
00222                         // which modules of the detector have been hit
00223                         const reco::HitPattern &hits = track.hitPattern();
00224 
00225                         double absEta = std::abs(track.eta());
00226                         // now fill the number of hits in a layer depending on eta
00227                         plots.pxbHitsEta->Fill(absEta, hits.numberOfValidPixelBarrelHits());
00228                         plots.pxeHitsEta->Fill(absEta, hits.numberOfValidPixelEndcapHits());
00229                         plots.tibHitsEta->Fill(absEta, hits.numberOfValidStripTIBHits());
00230                         plots.tobHitsEta->Fill(absEta, hits.numberOfValidStripTOBHits());
00231                         plots.tidHitsEta->Fill(absEta, hits.numberOfValidStripTIDHits());
00232                         plots.tecHitsEta->Fill(absEta, hits.numberOfValidStripTECHits());
00233                 }
00234         }
00235 }
00236         
00237 #include "FWCore/Framework/interface/MakerMacros.h"
00238 
00239 DEFINE_FWK_MODULE(PatTrackAnalyzer);