CMS 3D CMS Logo

PATElectronProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATElectronProducer.cc,v 1.20.2.8 2009/02/07 00:59:51 pioppi Exp $
00003 //
00004 
00005 #include "PhysicsTools/PatAlgos/plugins/PATElectronProducer.h"
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009 
00010 #include "DataFormats/Common/interface/Association.h"
00011 #include "DataFormats/Common/interface/ValueMap.h"
00012 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00013 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00014 
00015 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
00016 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00017 
00018 #include "PhysicsTools/PatUtils/interface/TrackerIsolationPt.h"
00019 #include "PhysicsTools/PatUtils/interface/CaloIsolationEnergy.h"
00020 
00021 #include <vector>
00022 #include <memory>
00023 
00024 
00025 using namespace pat;
00026 
00027 
00028 PATElectronProducer::PATElectronProducer(const edm::ParameterSet & iConfig) :
00029   isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
00030   userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
00031 {
00032 
00033   // general configurables
00034   electronSrc_      = iConfig.getParameter<edm::InputTag>( "electronSource" );
00035   embedGsfTrack_    = iConfig.getParameter<bool>         ( "embedGsfTrack" );
00036   embedSuperCluster_= iConfig.getParameter<bool>         ( "embedSuperCluster" );
00037   embedTrack_       = iConfig.getParameter<bool>         ( "embedTrack" );
00038 
00039   // pflow specific
00040   pfElecSrc_           = iConfig.getParameter<edm::InputTag>( "pfElectronSource" );
00041   useParticleFlow_        = iConfig.getParameter<bool>( "useParticleFlow" );
00042   embedPFCandidate_   = iConfig.getParameter<bool>( "embedPFCandidate" );
00043 
00044   
00045   // MC matching configurables
00046   addGenMatch_      = iConfig.getParameter<bool>          ( "addGenMatch" );
00047   if (addGenMatch_) {
00048       embedGenMatch_ = iConfig.getParameter<bool>         ( "embedGenMatch" );
00049       if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00050           genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00051       } else {
00052           genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00053       }
00054   }
00055   
00056   // trigger matching configurables
00057   addTrigMatch_     = iConfig.getParameter<bool>         ( "addTrigMatch" );
00058   trigMatchSrc_     = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
00059 
00060   // resolution configurables
00061   addResolutions_   = iConfig.getParameter<bool>         ( "addResolutions" );
00062 
00063   // electron ID configurables
00064   addElecID_        = iConfig.getParameter<bool>         ( "addElectronID" );
00065   if (addElecID_) {
00066       // it might be a single electron ID
00067       if (iConfig.existsAs<edm::InputTag>("electronIDSource")) {
00068           elecIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("electronIDSource")));
00069       }
00070       // or there might be many of them
00071       if (iConfig.existsAs<edm::ParameterSet>("electronIDSources")) {
00072           // please don't configure me twice
00073           if (!elecIDSrcs_.empty()) throw cms::Exception("Configuration") << 
00074                 "PATElectronProducer: you can't specify both 'electronIDSource' and 'electronIDSources'\n";
00075           // read the different electron ID names
00076           edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("electronIDSources");
00077           std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
00078           for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
00079               elecIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
00080           }
00081       }
00082       // but in any case at least once
00083       if (elecIDSrcs_.empty()) throw cms::Exception("Configuration") <<
00084             "PATElectronProducer: id addElectronID is true, you must specify either:\n" <<
00085             "\tInputTag electronIDSource = <someTag>\n" << "or\n" <<
00086             "\tPSet electronIDSources = { \n" <<
00087             "\t\tInputTag <someName> = <someTag>   // as many as you want \n " <<
00088             "\t}\n";
00089   }
00090   
00091   // construct resolution calculator
00092 
00093   // IsoDeposit configurables
00094   if (iConfig.exists("isoDeposits")) {
00095      edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00096      if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
00097      if (depconf.exists("ecal"))    isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
00098      if (depconf.exists("hcal"))    isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
00099      if (depconf.exists("particle"))           isoDepositLabels_.push_back(std::make_pair(ParticleIso, depconf.getParameter<edm::InputTag>("particle")));
00100      if (depconf.exists("chargedparticle"))    isoDepositLabels_.push_back(std::make_pair(ChargedParticleIso, depconf.getParameter<edm::InputTag>("chargedparticle")));
00101      if (depconf.exists("neutralparticle")) isoDepositLabels_.push_back(std::make_pair(NeutralParticleIso,depconf.getParameter<edm::InputTag>("neutralparticle")));
00102      if (depconf.exists("gammaparticle"))    isoDepositLabels_.push_back(std::make_pair(GammaParticleIso, depconf.getParameter<edm::InputTag>("gammaparticle")));
00103 
00104 
00105      if (depconf.exists("user")) {
00106         std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00107         std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00108         int key = UserBaseIso;
00109         for ( ; it != ed; ++it, ++key) {
00110             isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00111         }
00112      }
00113   }
00114 
00115   // Efficiency configurables
00116   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00117   if (addEfficiencies_) {
00118      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00119   }
00120 
00121   // Check to see if the user wants to add user data
00122   useUserData_ = false;
00123   if ( iConfig.exists("userData") ) {
00124     useUserData_ = true;
00125   }
00126 
00127   // electron ID configurables
00128   addElecShapes_        = iConfig.getParameter<bool>("addElectronShapes" );
00129   reducedBarrelRecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedBarrelRecHitCollection") ;
00130   reducedEndcapRecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedEndcapRecHitCollection") ;
00131    
00132   // produces vector of muons
00133   produces<std::vector<Electron> >();
00134 
00135 }
00136 
00137 
00138 PATElectronProducer::~PATElectronProducer() {
00139 }
00140 
00141 
00142 void PATElectronProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00143 
00144   // Get the collection of electrons from the event
00145   edm::Handle<edm::View<ElectronType> > electrons;
00146   iEvent.getByLabel(electronSrc_, electrons);
00147 
00148   if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00149 
00150   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00151 
00152   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00153   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00154     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00155   }
00156 
00157   // prepare the MC matching
00158   GenAssociations  genMatches(genMatchSrc_.size());
00159   if (addGenMatch_) {
00160         for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00161             iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00162         }
00163   }
00164 
00165 
00166   // prepare the trigger matching
00167   TrigAssociations  trigMatches(trigMatchSrc_.size());
00168   if ( addTrigMatch_ ) {
00169     for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
00170       iEvent.getByLabel(trigMatchSrc_[i], trigMatches[i]);
00171     }
00172   }
00173 
00174   // prepare ID extraction 
00175   std::vector<edm::Handle<edm::ValueMap<float> > > idhandles;
00176   std::vector<pat::Electron::IdPair>               ids;
00177   if (addElecID_) {
00178      idhandles.resize(elecIDSrcs_.size());
00179      ids.resize(elecIDSrcs_.size());
00180      for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
00181         iEvent.getByLabel(elecIDSrcs_[i].second, idhandles[i]);
00182         ids[i].first = elecIDSrcs_[i].first;
00183      }
00184   }
00185 
00186 
00187   if (addElecShapes_) {
00188     lazyTools_ .reset(new EcalClusterLazyTools( iEvent , iSetup , reducedBarrelRecHitCollection_ , reducedEndcapRecHitCollection_ ));  
00189   }
00190 
00191   std::vector<Electron> * patElectrons = new std::vector<Electron>();
00192 
00193 
00194   if( useParticleFlow_ ) {
00195     edm::Handle< reco::PFCandidateCollection >  pfElectrons;
00196     iEvent.getByLabel(pfElecSrc_, pfElectrons);
00197     unsigned index=0;
00198   
00199     for( reco::PFCandidateConstIterator i = pfElectrons->begin(); 
00200          i != pfElectrons->end(); ++i, ++index) {
00201 
00202       reco::PFCandidateRef pfRef(pfElectrons,index);
00203       reco::PFCandidatePtr ptrToMother(pfElectrons,index);
00204       reco::CandidateBaseRef pfBaseRef( pfRef ); 
00205       
00206       reco::GsfTrackRef PfTk= i->gsfTrackRef(); 
00207 
00208       bool Matched=false;
00209       for (edm::View<ElectronType>::const_iterator itElectron = electrons->begin(); itElectron != electrons->end(); ++itElectron) {
00210         unsigned int idx = itElectron - electrons->begin();
00211         if (Matched) continue;
00212         reco::GsfTrackRef EgTk= itElectron->gsfTrack();
00213         if (itElectron->gsfTrack()==i->gsfTrackRef()){
00214           const ElectronBaseRef elecsRef = electrons->refAt(idx);
00215           Electron anElectron(elecsRef);
00216           FillElectron(anElectron,elecsRef,pfBaseRef, genMatches, trigMatches);
00217           Matched=true;
00218           anElectron.setPFCandidateRef( pfRef  );
00219           if( embedPFCandidate_ ) anElectron.embedPFCandidate();
00220 
00221           if (isolator_.enabled()){
00222             reco::CandidatePtr mother =  ptrToMother->sourceCandidatePtr(0);
00223             isolator_.fill(mother, isolatorTmpStorage_);
00224             typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00225             for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), 
00226                    ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00227               anElectron.setIsolation(it->first, it->second);
00228             }
00229             for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00230               anElectron.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[mother]);
00231             }
00232           }
00233           //Electron Id
00234           if (addElecID_) {
00235             //STANDARD EL ID 
00236             for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
00237               ids[i].second = (*idhandles[i])[elecsRef];    
00238             }
00239             //SPECIFIC PF ID
00240             ids.push_back(std::make_pair("pf_evspi",pfRef->mva_e_pi()));
00241             ids.push_back(std::make_pair("pf_evsmu",pfRef->mva_e_mu()));
00242             anElectron.setElectronIDs(ids);
00243           }
00244 
00245           patElectrons->push_back(anElectron);
00246 
00247         }
00248 
00249         
00250       }
00251  
00252       
00253     }
00254   }
00255 
00256   else{
00257   for (edm::View<ElectronType>::const_iterator itElectron = electrons->begin(); itElectron != electrons->end(); ++itElectron) {
00258     // construct the Electron from the ref -> save ref to original object
00259     unsigned int idx = itElectron - electrons->begin();
00260 
00261     const ElectronBaseRef elecsRef = electrons->refAt(idx);
00262     reco::CandidateBaseRef elecBaseRef(elecsRef);
00263 
00264     //    const edm::Ptr<ElectronType> electronPtr = electrons->ptrAt(idx);
00265     Electron anElectron(elecsRef);
00266 
00267     FillElectron(anElectron,elecsRef,elecBaseRef, genMatches, trigMatches);
00268     
00269 
00270     // add resolution info
00271     
00272     // Isolation
00273     if (isolator_.enabled()) {
00274         isolator_.fill(*electrons, idx, isolatorTmpStorage_);
00275         typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00276         // better to loop backwards, so the vector is resized less times
00277         for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00278             anElectron.setIsolation(it->first, it->second);
00279         }
00280     }
00281 
00282     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00283         anElectron.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[elecsRef]);
00284     }
00285 
00286     // add electron ID info
00287     if (addElecID_) {
00288         for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
00289             ids[i].second = (*idhandles[i])[elecsRef];    
00290         }
00291         anElectron.setElectronIDs(ids);
00292     }
00293     
00294 
00295     if ( useUserData_ ) {
00296       userDataHelper_.add( anElectron, iEvent, iSetup );
00297     }
00298     
00299     
00300     // add sel to selected
00301     patElectrons->push_back(anElectron);
00302   }
00303   }
00304   
00305   // sort electrons in pt
00306   std::sort(patElectrons->begin(), patElectrons->end(), pTComparator_);
00307 
00308   // add the electrons to the event output
00309   std::auto_ptr<std::vector<Electron> > ptr(patElectrons);
00310   iEvent.put(ptr);
00311 
00312   // clean up
00313   if (isolator_.enabled()) isolator_.endEvent();
00314 
00315 }
00316 
00317 void PATElectronProducer::FillElectron(Electron& anElectron,
00318                                        const ElectronBaseRef& elecsRef,
00319                                        const reco::CandidateBaseRef& baseRef,
00320                                        const GenAssociations& genMatches,
00321                                        const TrigAssociations& trigMatches)const {
00322   if (embedGsfTrack_) anElectron.embedGsfTrack();
00323   if (embedSuperCluster_) anElectron.embedSuperCluster();
00324   if (embedTrack_) anElectron.embedTrack();
00325 
00326   // store the match to the generated final state muons
00327   if (addGenMatch_) {
00328     for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00329       reco::GenParticleRef genElectron = (*genMatches[i])[elecsRef];
00330       anElectron.addGenParticleRef(genElectron);
00331     }
00332     if (embedGenMatch_) anElectron.embedGenParticle();
00333   }
00334     // matches to trigger primitives
00335     if ( addTrigMatch_ ) {
00336       for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
00337         TriggerPrimitiveRef trigPrim = (*trigMatches[i])[elecsRef];
00338         if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
00339           anElectron.addTriggerMatch(*trigPrim);
00340         }
00341       }
00342     }
00343     
00344     if (efficiencyLoader_.enabled()) {
00345       efficiencyLoader_.setEfficiencies( anElectron, elecsRef );
00346     }
00347     //  add electron shapes info
00348     if (addElecShapes_) {
00349         std::vector<float> covariances = lazyTools_->covariances(*(elecsRef->superCluster()->seed())) ;
00350         std::vector<float> localCovariances = lazyTools_->localCovariances(*(elecsRef->superCluster()->seed())) ;
00351         float scSigmaEtaEta = sqrt(covariances[0]) ;
00352         float scSigmaIEtaIEta = sqrt(localCovariances[0]) ;
00353         float scE1x5 = lazyTools_->e1x5(*(elecsRef->superCluster()->seed()))  ;
00354         float scE2x5Max = lazyTools_->e2x5Max(*(elecsRef->superCluster()->seed()))  ;
00355         float scE5x5 = lazyTools_->e5x5(*(elecsRef->superCluster()->seed())) ;
00356         anElectron.setClusterShapes(scSigmaEtaEta,scSigmaIEtaIEta,scE1x5,scE2x5Max,scE5x5) ;
00357     }
00358 }
00359 
00360 
00361 #include "FWCore/Framework/interface/MakerMacros.h"
00362 
00363 DEFINE_FWK_MODULE(PATElectronProducer);

Generated on Tue Jun 9 17:41:38 2009 for CMSSW by  doxygen 1.5.4