CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CommonTools/ParticleFlow/interface/ElectronIDPFCandidateSelectorDefinition.h

Go to the documentation of this file.
00001 #ifndef CommonTools_ParticleFlow_ElectronIDPFCandidateSelectorDefinition
00002 #define CommonTools_ParticleFlow_ElectronIDPFCandidateSelectorDefinition
00003 
00012 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
00013 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00014 #include "DataFormats/Common/interface/ValueMap.h"
00015 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00016 #include "CommonTools/ParticleFlow/interface/PFCandidateSelectorDefinition.h"
00017 #include <algorithm>
00018 
00019 namespace pf2pat {
00020 
00021   struct ElectronIDPFCandidateSelectorDefinition : public PFCandidateSelectorDefinition {
00022     
00023     ElectronIDPFCandidateSelectorDefinition ( const edm::ParameterSet & cfg ) :
00024       electrons_(  cfg.getParameter< edm::InputTag >( "recoGsfElectrons" ) ), 
00025       electronId_( cfg.getParameter< edm::InputTag >( "electronIdMap" ) )
00026     { 
00027         if (cfg.exists("bitsToCheck")) {
00028             isBitMap_ = true; 
00029             mask_ = 0;
00030             if (cfg.existsAs<std::vector<std::string> >("bitsToCheck")) {
00031                 std::vector<std::string> strbits = cfg.getParameter<std::vector<std::string> >("bitsToCheck");
00032                 for (std::vector<std::string>::const_iterator istrbit = strbits.begin(), estrbit = strbits.end(); 
00033                         istrbit != estrbit; ++istrbit) {
00034                     if      (*istrbit == "id" )  { mask_ |= 1; }
00035                     else if (*istrbit == "iso")  { mask_ |= 2; }
00036                     else if (*istrbit == "conv") { mask_ |= 4; }
00037                     else if (*istrbit == "ip")   { mask_ |= 8; }
00038                     else throw cms::Exception("Configuration") << "ElectronIDPFCandidateSelector: " <<
00039                         "bitsToCheck allowed string values are only id(0), iso(1), conv(2), ip(3).\n" << 
00040                             "Otherwise, use uint32_t bitmask).\n";
00041                 }
00042             } else if (cfg.existsAs<uint32_t>("bitsToCheck")) {
00043                 mask_ = cfg.getParameter<uint32_t>("bitsToCheck");
00044             } else {
00045                 throw cms::Exception("Configuration") << "ElectronIDPFCandidateSelector: " <<
00046                         "bitsToCheck must be either a vector of strings, or a uint32 bitmask.\n";   
00047             }
00048         } else {
00049             isBitMap_ = false;
00050             value_ = cfg.getParameter<double>("electronIdCut");
00051         }
00052     }
00053 
00054     void select( const HandleToCollection & hc, 
00055                  const edm::Event & e,
00056                  const edm::EventSetup& s) {
00057       selected_.clear();
00058 
00059       edm::Handle<reco::GsfElectronCollection> electrons;
00060       e.getByLabel(electrons_, electrons);
00061 
00062       edm::Handle<edm::ValueMap<float> > electronId;
00063       e.getByLabel(electronId_, electronId);
00064 
00065       unsigned key=0;
00066       for( collection::const_iterator pfc = hc->begin(); 
00067            pfc != hc->end(); ++pfc, ++key) {
00068 
00069         // Get GsfTrack for matching with reco::GsfElectron objects
00070         reco::GsfTrackRef PfTk = pfc->gsfTrackRef();
00071 
00072         // skip ones without GsfTrack: they won't be matched anyway     
00073         if (PfTk.isNull()) continue;
00074 
00075         int match = -1;
00076         // try first the non-ambiguous tracks
00077         for (reco::GsfElectronCollection::const_iterator it = electrons->begin(), ed = electrons->end(); it != ed; ++it) {
00078             if (it->gsfTrack() == PfTk) { match = it - electrons->begin(); break; }
00079         }
00080         // then the ambiguous ones
00081         if (match == -1) {
00082             for (reco::GsfElectronCollection::const_iterator it = electrons->begin(), ed = electrons->end(); it != ed; ++it) {
00083                 if (std::count(it->ambiguousGsfTracksBegin(), it->ambiguousGsfTracksEnd(), PfTk) > 0) {
00084                     match = it - electrons->begin(); break;
00085                 }
00086             }
00087         }
00088         // if found, make a GsfElectronRef and read electron id
00089         if (match != -1) {
00090             reco::GsfElectronRef ref(electrons,match);
00091             float eleId = (*electronId)[ref];
00092             bool pass = false;
00093             if (isBitMap_) {
00094                 uint32_t thisval = eleId; 
00095                 pass = ((thisval & mask_) == mask_);
00096             } else {
00097                 pass = (eleId > value_);
00098             }
00099             if (pass) {
00100                 selected_.push_back( reco::PFCandidate(*pfc) );
00101                 reco::PFCandidatePtr ptrToMother( hc, key );
00102                 selected_.back().setSourceCandidatePtr( ptrToMother );
00103             }
00104         }
00105       }
00106     }
00107     
00108     private:
00109         edm::InputTag  electrons_;
00110         edm::InputTag  electronId_;
00111         bool isBitMap_;
00112         uint32_t mask_;
00113         double   value_;
00114   };
00115 }
00116 
00117 #endif