Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:02

0001 #include <map>
0002 #include <string>
0003 
0004 #include "TH1D.h"
0005 #include "TH2D.h"
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "DataFormats/PatCandidates/interface/Muon.h"
0013 
0014 /**
0015    \class   PatZToMuMuAnalyzer PatZToMuMuAnalyzer.cc "PhysicsTools/PatExamples/plugins/PatZToMuMuAnalyzer.h"
0016 
0017    \brief   Module to analyze the performance of muon reconstruction on the example of Z->mumu events
0018 
0019    Module to analyze the performance of muon reconstruction on the example of Z->mumu events: transverse
0020    momentum and eta of the muon candidates and the mass of the Z boson candidate are plotted from inner,
0021    outer and global tracks. The mass is recalculated by an extra finction. The difference of the outer
0022    track and the global track are plotted for the transverse momentum, eta and phi of the two muon candi-
0023    dates, for global muons as far as available. The only input parameters are:
0024 
0025    _muons_  --> indicating the muon collection of choice.
0026    _shift_  --> indicating the relative shift of the transverse momentum for the estimate of the effect
0027                 on the invariant mass.
0028 
0029    The shift is applied to all mass calculations.
0030 */
0031 
0032 class PatZToMuMuAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0033 public:
0034   /// typedef's to simplify get functions
0035   typedef math::XYZVector Vector;
0036   typedef math::XYZTLorentzVector LorentzVector;
0037 
0038   /// default constructor
0039   explicit PatZToMuMuAnalyzer(const edm::ParameterSet& cfg);
0040   /// default destructor
0041   ~PatZToMuMuAnalyzer() override{};
0042 
0043 private:
0044   /// everything that needs to be done during the event loop
0045   void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0046 
0047   /// calculate the mass of the Z boson from the tracker momenta by hand
0048   double mass(const math::XYZVector& t1, const math::XYZVector& t2) const;
0049   /// check if histogram was booked
0050   bool booked(const std::string histName) const { return hists_.find(histName) != hists_.end(); };
0051   /// fill histogram if it had been booked before
0052   void fill(const std::string histName, double value) const {
0053     if (booked(histName))
0054       hists_.find(histName)->second->Fill(value);
0055   };
0056   /// fill a predefined set of histograms from inner outer or global tracks for first and second mu candidate
0057   void fill(std::string hists, const reco::TrackRef& t1, const reco::TrackRef& t2) const;
0058 
0059   /// input for muons
0060   edm::EDGetTokenT<edm::View<pat::Muon> > muonsToken_;
0061   /// shift in transverse momentum to determine a
0062   /// rough uncertainty on the Z mass estimation
0063   double shift_;
0064   /// management of 1d histograms
0065   std::map<std::string, TH1D*> hists_;
0066 };
0067 
0068 inline double PatZToMuMuAnalyzer::mass(const Vector& t1, const Vector& t2) const {
0069   return (LorentzVector(shift_ * t1.x(), shift_ * t1.y(), t1.z(), sqrt((0.1057 * 0.1057) + t1.mag2())) +
0070           LorentzVector(shift_ * t2.x(), shift_ * t2.y(), t2.z(), sqrt((0.1057 * 0.1057) + t2.mag2())))
0071       .mass();
0072 }
0073 
0074 #include "FWCore/ServiceRegistry/interface/Service.h"
0075 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0076 
0077 PatZToMuMuAnalyzer::PatZToMuMuAnalyzer(const edm::ParameterSet& cfg)
0078     : muonsToken_(consumes<edm::View<pat::Muon> >(cfg.getParameter<edm::InputTag>("muons"))),
0079       shift_(cfg.getParameter<double>("shift")) {
0080   usesResource(TFileService::kSharedResource);
0081 
0082   edm::Service<TFileService> fileService;
0083 
0084   // mass plot around Z peak from global tracks
0085   hists_["globalMass"] = fileService->make<TH1D>("globalMass", "Mass_{Z} (global) (GeV)", 90, 30., 120.);
0086   // eta from global tracks
0087   hists_["globalEta"] = fileService->make<TH1D>("globalEta", "#eta (global)", 48, -2.4, 2.4);
0088   // pt from global tracks
0089   hists_["globalPt"] = fileService->make<TH1D>("globalPt", "p_{T} (global) (GeV)", 100, 0., 100.);
0090   // mass plot around Z peak from inner tracks
0091   hists_["innerMass"] = fileService->make<TH1D>("innerMass", "Mass_{Z} (inner) (GeV)", 90, 30., 120.);
0092   // eta from inner tracks
0093   hists_["innerEta"] = fileService->make<TH1D>("innerEta", "#eta (inner)", 48, -2.4, 2.4);
0094   // pt from inner tracks
0095   hists_["innerPt"] = fileService->make<TH1D>("innerPt", "p_{T} (inner) (GeV)", 100, 0., 100.);
0096   // mass plot around Z peak from outer tracks
0097   hists_["outerMass"] = fileService->make<TH1D>("outerMass", "Mass_{Z} (outer) (GeV)", 90, 30., 120.);
0098   // eta from outer tracks
0099   hists_["outerEta"] = fileService->make<TH1D>("outerEta", "#eta (outer)", 48, -2.4, 2.4);
0100   // pt from outer tracks
0101   hists_["outerPt"] = fileService->make<TH1D>("outerPt", "p_{T} (outer) (GeV)", 100, 0., 100.);
0102   // delta pt between global and outer track
0103   hists_["deltaPt"] = fileService->make<TH1D>("deltaPt", "#Delta p_{T} (GeV)", 100, -20., 20.);
0104   // delta eta between global and outer track
0105   hists_["deltaEta"] = fileService->make<TH1D>("deltaEta", "#Delta #eta", 100, -0.2, 0.2);
0106   // delta phi between global and outer track
0107   hists_["deltaPhi"] = fileService->make<TH1D>("deltaPhi", "#Delta #phi", 100, -0.2, 0.2);
0108 }
0109 
0110 void PatZToMuMuAnalyzer::fill(std::string hists, const reco::TrackRef& t1, const reco::TrackRef& t2) const {
0111   if (t1.isAvailable()) {
0112     // fill pt from global track for first muon
0113     fill(std::string(hists).append("Pt"), t1->pt());
0114     // fill pt from global track for second muon
0115     fill(std::string(hists).append("Eta"), t1->eta());
0116   }
0117   if (t2.isAvailable()) {
0118     // fill eta from global track for first muon
0119     fill(std::string(hists).append("Pt"), t2->pt());
0120     // fill eta from global track for second muon
0121     fill(std::string(hists).append("Eta"), t2->eta());
0122   }
0123   if (t1.isAvailable() && t2.isAvailable()) {
0124     // fill invariant mass of the Z boson candidate
0125     fill(std::string(hists).append("Mass"), mass(t1->momentum(), t2->momentum()));
0126   }
0127 }
0128 
0129 void PatZToMuMuAnalyzer::analyze(const edm::Event& event, const edm::EventSetup& setup) {
0130   // pat candidate collection
0131   edm::Handle<edm::View<pat::Muon> > muons;
0132   event.getByToken(muonsToken_, muons);
0133 
0134   // Fill some basic muon quantities as
0135   // reconstructed from inner and outer
0136   // tack
0137   for (edm::View<pat::Muon>::const_iterator mu1 = muons->begin(); mu1 != muons->end(); ++mu1) {
0138     for (edm::View<pat::Muon>::const_iterator mu2 = muons->begin(); mu2 != muons->end(); ++mu2) {
0139       if (mu2 > mu1) {                            // prevent double conting
0140         if (mu1->charge() * mu2->charge() < 0) {  // check only muon pairs of unequal charge
0141           fill(std::string("inner"), mu1->innerTrack(), mu2->innerTrack());
0142           fill(std::string("outer"), mu1->outerTrack(), mu2->outerTrack());
0143           fill(std::string("global"), mu1->globalTrack(), mu2->globalTrack());
0144 
0145           if (mu1->isGlobalMuon()) {
0146             fill("deltaPt", mu1->outerTrack()->pt() - mu1->globalTrack()->pt());
0147             fill("deltaEta", mu1->outerTrack()->eta() - mu1->globalTrack()->eta());
0148             fill("deltaPhi", mu1->outerTrack()->phi() - mu1->globalTrack()->phi());
0149           }
0150           if (mu2->isGlobalMuon()) {
0151             fill("deltaPt", mu2->outerTrack()->pt() - mu2->globalTrack()->pt());
0152             fill("deltaEta", mu2->outerTrack()->eta() - mu2->globalTrack()->eta());
0153             fill("deltaPhi", mu2->outerTrack()->phi() - mu2->globalTrack()->phi());
0154           }
0155         }
0156       }
0157     }
0158   }
0159 }
0160 
0161 #include "FWCore/Framework/interface/MakerMacros.h"
0162 DEFINE_FWK_MODULE(PatZToMuMuAnalyzer);