CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/PhysicsTools/PatExamples/plugins/PatZToMuMuAnalyzer.cc

Go to the documentation of this file.
00001 #include <map>
00002 #include <string>
00003 
00004 #include "TH1D.h"
00005 #include "TH2D.h"
00006 
00007 #include "FWCore/Framework/interface/Event.h"
00008 #include "FWCore/Utilities/interface/InputTag.h"
00009 #include "FWCore/Framework/interface/EDAnalyzer.h"
00010 #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00012 #include "DataFormats/PatCandidates/interface/Muon.h"
00013 
00033 class PatZToMuMuAnalyzer : public edm::EDAnalyzer {
00034   
00035  public:
00037   typedef math::XYZVector Vector;
00038   typedef math::XYZTLorentzVector LorentzVector;
00039 
00041   explicit PatZToMuMuAnalyzer(const edm::ParameterSet& cfg);
00043   ~PatZToMuMuAnalyzer(){};
00044   
00045  private:
00047   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
00048 
00050   double mass(const math::XYZVector& t1, const math::XYZVector& t2) const;
00052   bool booked(const std::string histName) const { return hists_.find(histName.c_str())!=hists_.end(); };
00054   void fill(const std::string histName, double value) const { if(booked(histName.c_str())) hists_.find(histName.c_str())->second->Fill(value); };
00056   void fill(std::string hists, const reco::TrackRef& t1, const reco::TrackRef& t2) const;
00057 
00059   edm::InputTag muons_;
00062   double shift_;
00064   std::map< std::string, TH1D* > hists_;
00065 };
00066 
00067 inline double 
00068 PatZToMuMuAnalyzer::mass(const Vector& t1,  const Vector& t2) const
00069 {
00070   return (LorentzVector(shift_*t1.x(), shift_*t1.y(), t1.z(), sqrt((0.1057*0.1057)+t1.mag2())) + LorentzVector(shift_*t2.x(), shift_*t2.y(), t2.z(), sqrt((0.1057*0.1057)+t2.mag2()))).mass();
00071 }
00072 
00073 #include "FWCore/ServiceRegistry/interface/Service.h"
00074 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00075 
00076 PatZToMuMuAnalyzer::PatZToMuMuAnalyzer(const edm::ParameterSet& cfg):
00077   muons_(cfg.getParameter< edm::InputTag >("muons")),
00078   shift_(cfg.getParameter< double >("shift"))
00079 {
00080   edm::Service< TFileService > fileService;
00081   
00082   // mass plot around Z peak from global tracks
00083   hists_[ "globalMass"] = fileService->make< TH1D >( "globalMass" , "Mass_{Z} (global) (GeV)",   90,    30.,   120.);
00084   // eta from global tracks
00085   hists_[ "globalEta" ] = fileService->make< TH1D >( "globalEta"  , "#eta (global)"          ,   48,   -2.4,    2.4);
00086   // pt from global tracks
00087   hists_[ "globalPt"  ] = fileService->make< TH1D >( "globalPt"   , "p_{T} (global) (GeV)"   ,  100,     0.,   100.);
00088   // mass plot around Z peak from inner tracks
00089   hists_[ "innerMass" ] = fileService->make< TH1D >( "innerMass"  , "Mass_{Z} (inner) (GeV)" ,   90,    30.,   120.);
00090   // eta from inner tracks
00091   hists_[ "innerEta"  ] = fileService->make< TH1D >( "innerEta"   , "#eta (inner)"           ,   48,   -2.4,    2.4);
00092   // pt from inner tracks
00093   hists_[ "innerPt"   ] = fileService->make< TH1D >( "innerPt"    , "p_{T} (inner) (GeV)"    ,  100,     0.,   100.);
00094   // mass plot around Z peak from outer tracks
00095   hists_[ "outerMass" ] = fileService->make< TH1D >( "outerMass"  , "Mass_{Z} (outer) (GeV)" ,   90,    30.,   120.);
00096   // eta from outer tracks
00097   hists_[ "outerEta"  ] = fileService->make< TH1D >( "outerEta"   , "#eta (outer)"           ,   48,   -2.4,    2.4);
00098   // pt from outer tracks
00099   hists_[ "outerPt"   ] = fileService->make< TH1D >( "outerPt"    , "p_{T} (outer) (GeV)"    ,  100,     0.,   100.);
00100   // delta pt between global and outer track
00101   hists_[ "deltaPt"   ] = fileService->make< TH1D >( "deltaPt"    , "#Delta p_{T} (GeV)"     ,  100,   -20.,    20.);
00102   // delta eta between global and outer track
00103   hists_[ "deltaEta"  ] = fileService->make< TH1D >( "deltaEta"   , "#Delta #eta"            ,  100,   -0.2,    0.2);
00104   // delta phi between global and outer track
00105   hists_[ "deltaPhi"  ] = fileService->make< TH1D >( "deltaPhi"   , "#Delta #phi"            ,  100,   -0.2,    0.2);
00106 }
00107 
00108 void PatZToMuMuAnalyzer::fill(std::string hists, const reco::TrackRef& t1, const reco::TrackRef& t2) const 
00109 {
00110   if( t1.isAvailable() ){
00111     // fill pt from global track for first muon
00112     fill( std::string(hists).append("Pt") , t1->pt() );
00113     // fill pt from global track for second muon
00114     fill( std::string(hists).append("Eta"), t1->eta() );
00115   }
00116   if( t2.isAvailable() ){
00117     // fill eta from global track for first muon
00118     fill( std::string(hists).append("Pt") , t2->pt() );
00119     // fill eta from global track for second muon
00120     fill( std::string(hists).append("Eta"), t2->eta() );
00121   }
00122   if( t1.isAvailable() && t2.isAvailable() ){
00123     // fill invariant mass of the Z boson candidate
00124     fill( std::string(hists).append("Mass"), mass(t1->momentum(), t2->momentum()));
00125   }
00126 }
00127 
00128 void PatZToMuMuAnalyzer::analyze(const edm::Event& event, const edm::EventSetup& setup)
00129 {
00130   // pat candidate collection
00131   edm::Handle< edm::View<pat::Muon> > muons;
00132   event.getByLabel(muons_, muons);
00133 
00134   // Fill some basic muon quantities as 
00135   // reconstructed from inner and outer 
00136   // tack 
00137   for(edm::View<pat::Muon>::const_iterator mu1=muons->begin(); mu1!=muons->end(); ++mu1){
00138     for(edm::View<pat::Muon>::const_iterator mu2=muons->begin(); mu2!=muons->end(); ++mu2){
00139       if(mu2>mu1){ // prevent double conting
00140         if( mu1->charge()*mu2->charge()<0 ){ // check only muon pairs of unequal charge 
00141           fill(std::string("inner" ), mu1->innerTrack (), mu2->innerTrack ());
00142           fill(std::string("outer" ), mu1->outerTrack (), mu2->outerTrack ());
00143           fill(std::string("global"), mu1->globalTrack(), mu2->globalTrack());
00144           
00145           if(mu1->isGlobalMuon()){
00146             fill("deltaPt" , mu1->outerTrack()->pt ()-mu1->globalTrack()->pt ());
00147             fill("deltaEta", mu1->outerTrack()->eta()-mu1->globalTrack()->eta());
00148             fill("deltaPhi", mu1->outerTrack()->phi()-mu1->globalTrack()->phi());
00149           }
00150           if(mu2->isGlobalMuon()){
00151             fill("deltaPt" , mu2->outerTrack()->pt ()-mu2->globalTrack()->pt ());
00152             fill("deltaEta", mu2->outerTrack()->eta()-mu2->globalTrack()->eta());
00153             fill("deltaPhi", mu2->outerTrack()->phi()-mu2->globalTrack()->phi());
00154           }
00155         }
00156       }
00157     }
00158   }
00159 }
00160 
00161 #include "FWCore/Framework/interface/MakerMacros.h"
00162 DEFINE_FWK_MODULE( PatZToMuMuAnalyzer );