Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <map>
0002 #include <string>
0003 
0004 #include "TH1.h"
0005 
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0012 #include "Math/VectorUtil.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/Muon.h"
0015 
0016 class PatMCMatching : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0017 public:
0018   /// default constructor
0019   explicit PatMCMatching(const edm::ParameterSet&);
0020   /// default destructor
0021   ~PatMCMatching() override;
0022 
0023 private:
0024   void beginJob() override;
0025   void analyze(const edm::Event&, const edm::EventSetup&) override;
0026   void endJob() override;
0027 
0028   // simple map to contain all histograms;
0029   // histograms are booked in the beginJob()
0030   // method
0031   std::map<std::string, TH1F*> histContainer_;
0032 
0033   // input tags
0034   edm::EDGetTokenT<edm::View<pat::Muon> > muonSrcToken_;
0035 };
0036 
0037 PatMCMatching::PatMCMatching(const edm::ParameterSet& iConfig)
0038     : histContainer_(),
0039       muonSrcToken_(consumes<edm::View<pat::Muon> >(iConfig.getUntrackedParameter<edm::InputTag>("muonSrc"))) {
0040   usesResource(TFileService::kSharedResource);
0041 }
0042 
0043 PatMCMatching::~PatMCMatching() {}
0044 
0045 void PatMCMatching::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0046   // get muon collection
0047   edm::Handle<edm::View<pat::Muon> > muons;
0048   iEvent.getByToken(muonSrcToken_, muons);
0049 
0050   for (edm::View<pat::Muon>::const_iterator muon = muons->begin(); muon != muons->end(); ++muon) {
0051     for (unsigned int i = 0; i < muon->genParticleRefs().size(); ++i) {
0052       switch (muon->genParticle(i)->status()) {
0053         case 1:
0054           histContainer_["DR_status1Match"]->Fill(
0055               ROOT::Math::VectorUtil::DeltaR(muon->p4(), muon->genParticle(i)->p4()));
0056           break;
0057         case 3:
0058           histContainer_["DR_status3Match"]->Fill(
0059               ROOT::Math::VectorUtil::DeltaR(muon->p4(), muon->genParticle(i)->p4()));
0060           break;
0061         default:
0062           histContainer_["DR_defaultMatch"]->Fill(
0063               ROOT::Math::VectorUtil::DeltaR(muon->p4(), muon->genParticle(i)->p4()));
0064           break;
0065       }
0066     }
0067   }
0068 }
0069 
0070 void PatMCMatching::beginJob() {
0071   // register to the TFileService
0072   edm::Service<TFileService> fs;
0073 
0074   // book histograms:
0075   histContainer_["DR_defaultMatch"] = fs->make<TH1F>("DR_defaultMatch", "DR_defaultMatch", 100, 0., 0.02);
0076   histContainer_["DR_status1Match"] = fs->make<TH1F>("DR_status1Match", "DR_status1Match", 100, 0., 0.02);
0077   histContainer_["DR_status3Match"] = fs->make<TH1F>("DR_status3Match", "DR_status3Match", 100, 0., 0.02);
0078 }
0079 
0080 void PatMCMatching::endJob() {}
0081 
0082 #include "FWCore/Framework/interface/MakerMacros.h"
0083 DEFINE_FWK_MODULE(PatMCMatching);