Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:34:27

0001 /* This module shows how to access the electron ID results using the   
0002 ** VID Framework  
0003 ** It works for both AOD and miniAOD 
0004 **
0005 ** Author: Sam Harper borrowing liberaly from Ilya Kravchenko and 
0006 **         Lindsey Gray's examples
0007 **
0008 */
0009 
0010 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 
0017 #include "DataFormats/Common/interface/ValueMap.h"
0018 #include "DataFormats/Common/interface/View.h"
0019 #include "DataFormats/PatCandidates/interface/Electron.h"
0020 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0021 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0022 
0023 class VIDUsageExample : public edm::stream::EDAnalyzer<> {
0024 private:
0025   //we want this to work on mini-aod and aod, this means we dont know if we are getting
0026   //GsfElectrons or PAT electrons
0027   //so we try both (this could be a bit more sophosticated but lets keep things simple)
0028   edm::EDGetTokenT<reco::GsfElectronCollection> gsfEleToken_;
0029   edm::EDGetTokenT<edm::View<pat::Electron> > patEleToken_;
0030   edm::EDGetTokenT<edm::ValueMap<bool> > idDecisionMapToken_;  //a bool true=passed ID, false = failed ID
0031   //the number of the first cut failed in the order they are defined in the PSet starting at zero (ie if you et,dEtaIn,dPhiIn,hadem cuts defined and it passed et,dEtaIn but failed dPhiIn, this number would be 2, in the case of no cuts failed it is #cuts
0032   edm::EDGetTokenT<edm::ValueMap<unsigned> > firstIdCutFailedMapToken_;
0033   //the md5sum of the ID you are using (E/gamma might ask you for this to verify you are running the right ID)
0034   edm::EDGetTokenT<std::string> idMD5NameToken_;
0035 
0036   size_t nrPassID_;
0037   size_t nrFailID_;
0038 
0039 public:
0040   explicit VIDUsageExample(const edm::ParameterSet& para);
0041   ~VIDUsageExample() {}
0042 
0043   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0044 
0045   void endStream() override {
0046     std::cout << "nrPass " << nrPassID_ << " nrFail " << nrFailID_
0047               << " (note this is all \"electrons\" so is not the ID efficiency)" << std::endl;
0048   }
0049 };
0050 
0051 VIDUsageExample::VIDUsageExample(const edm::ParameterSet& para)
0052     : gsfEleToken_(consumes<reco::GsfElectronCollection>(para.getParameter<edm::InputTag>("eles"))),
0053       patEleToken_(consumes<edm::View<pat::Electron> >(para.getParameter<edm::InputTag>("eles"))),
0054 
0055       idDecisionMapToken_(consumes<edm::ValueMap<bool> >(para.getParameter<edm::InputTag>("id"))),
0056       firstIdCutFailedMapToken_(consumes<edm::ValueMap<unsigned> >(para.getParameter<edm::InputTag>("id"))),
0057       idMD5NameToken_(consumes<std::string>(para.getParameter<edm::InputTag>("id"))),
0058       nrPassID_(0),
0059       nrFailID_(0)
0060 
0061 {}
0062 
0063 void VIDUsageExample::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0064   edm::Handle<edm::View<pat::Electron> > patEles;
0065   iEvent.getByToken(patEleToken_, patEles);
0066 
0067   edm::Handle<reco::GsfElectronCollection> gsfEles;
0068   iEvent.getByToken(gsfEleToken_, gsfEles);
0069 
0070   edm::Handle<edm::ValueMap<bool> > idDecisionMap;
0071   iEvent.getByToken(idDecisionMapToken_, idDecisionMap);
0072 
0073   edm::Handle<edm::ValueMap<unsigned> > firstIdCutFailedMap;
0074   iEvent.getByToken(firstIdCutFailedMapToken_, firstIdCutFailedMap);
0075 
0076   edm::Handle<std::string> idMD5Name;
0077   iEvent.getByToken(idMD5NameToken_, idMD5Name);
0078 
0079   std::cout << "md5sum of the ID " << *idMD5Name << std::endl;
0080 
0081   //note the copy/paste is only to make things clearer, its not a great way of solving this issue
0082 
0083   if (patEles.isValid()) {  //we have pat electrons availible use them
0084     for (auto ele = patEles->begin(); ele != patEles->end(); ++ele) {
0085       //value map is keyed of edm::Ptrs so we need to make one
0086       const edm::Ptr<pat::Electron> elePtr(patEles, ele - patEles->begin());
0087       bool passID = (*idDecisionMap)[elePtr];  //a bool, true if it passed the ID, false if it didnt
0088       if (passID) {
0089         std::cout << "pat ele passed ID" << std::endl;
0090         nrPassID_++;
0091       } else {
0092         int firstCutFailedNr = (*firstIdCutFailedMap)[elePtr];
0093         std::cout << "pat ele failed ID at cut #" << firstCutFailedNr << std::endl;
0094         nrFailID_++;
0095       }
0096     }
0097   } else if (gsfEles.isValid()) {  //no pat electrons availible, fall back to GsfElectrons
0098     for (auto ele = gsfEles->begin(); ele != gsfEles->end(); ++ele) {
0099       //value map is keyed of edm::Ptrs so we need to make one
0100       const edm::Ptr<reco::GsfElectron> elePtr(gsfEles, ele - gsfEles->begin());
0101       bool passID = (*idDecisionMap)[elePtr];  //a bool, true if it passed the ID, false if it didnt
0102       if (passID) {
0103         std::cout << "gsf ele passed ID" << std::endl;
0104         nrPassID_++;
0105       } else {
0106         int firstCutFailedNr = (*firstIdCutFailedMap)[elePtr];
0107         std::cout << "gsf ele failed ID at cut #" << firstCutFailedNr << std::endl;
0108         nrFailID_++;
0109       }
0110     }
0111   } else {
0112     std::cout << "no Gsf or PAT electrons found" << std::endl;
0113   }
0114 }
0115 
0116 DEFINE_FWK_MODULE(VIDUsageExample);