CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/RecoEgamma/EgammaHLTProducers/src/EgammaHLTElectronDetaDphiProducer.cc

Go to the documentation of this file.
00001 
00009 #include "RecoEgamma/EgammaHLTProducers/interface/EgammaHLTElectronDetaDphiProducer.h"
00010 
00011 // Framework
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/Framework/interface/EventSetup.h"
00014 #include "DataFormats/Common/interface/Handle.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 #include "FWCore/Utilities/interface/Exception.h"
00018 
00019 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00020 #include "DataFormats/RecoCandidate/interface/RecoEcalCandidate.h"
00021 #include "DataFormats/EgammaCandidates/interface/ElectronIsolationAssociation.h"
00022 #include "DataFormats/RecoCandidate/interface/RecoEcalCandidateIsolation.h"
00023 
00024 #include "DataFormats/Common/interface/RefToBase.h"
00025 
00026 #include "DataFormats/Common/interface/RefProd.h"
00027 
00028 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00029 #include "DataFormats/TrackReco/interface/Track.h"
00030 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00031 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00032 #include "RecoEgamma/EgammaElectronAlgos/interface/ElectronUtilities.h"
00033 
00034 #include "DataFormats/TrackReco/interface/Track.h"
00035 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00036 
00037 //#include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"//needed?
00038 //#include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"//needed?
00039 
00040 
00041 #include "DataFormats/Math/interface/Point3D.h"
00042 
00043 #include "RecoEgamma/EgammaTools/interface/ECALPositionCalculator.h"
00044 
00045 #include "FWCore/Framework/interface/EventSetup.h"
00046 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00047 #include "MagneticField/Engine/interface/MagneticField.h"
00048 
00049 EgammaHLTElectronDetaDphiProducer::EgammaHLTElectronDetaDphiProducer(const edm::ParameterSet& config) 
00050 {
00051 
00052   electronProducer_             = config.getParameter<edm::InputTag>("electronProducer");
00053   bsProducer_                   = config.getParameter<edm::InputTag>("BSProducer");
00054   useTrackProjectionToEcal_     = config.getParameter<bool>("useTrackProjectionToEcal");
00055   variablesAtVtx_               = config.getParameter<bool>("variablesAtVtx");
00056   recoEcalCandidateProducer_ = config.getParameter<edm::InputTag>("recoEcalCandidateProducer"); 
00057   useSCRefs_ = config.getParameter<bool>("useSCRefs");
00058 
00059   //register your products
00060   if(!useSCRefs_){
00061     produces < reco::ElectronIsolationMap >( "Deta" ).setBranchAlias( "deta" );
00062     produces < reco::ElectronIsolationMap >( "Dphi" ).setBranchAlias( "dphi" ); 
00063   }else{
00064     produces < reco::RecoEcalCandidateIsolationMap >( "Deta" ).setBranchAlias( "deta" );
00065     produces < reco::RecoEcalCandidateIsolationMap >( "Dphi" ).setBranchAlias( "dphi" ); 
00066   }
00067 }
00068 
00069 EgammaHLTElectronDetaDphiProducer::~EgammaHLTElectronDetaDphiProducer(){}
00070 
00071 
00072 //
00073 // member functions
00074 //
00075 
00076 // ------------ method called to produce the data  ------------
00077 void EgammaHLTElectronDetaDphiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00078 
00079   // Get the HLT filtered objects
00080   edm::Handle<reco::ElectronCollection> electronHandle;
00081   iEvent.getByLabel(electronProducer_,electronHandle);
00082 
00083   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00084   iEvent.getByLabel(bsProducer_,recoBeamSpotHandle);
00085   // gets its position
00086   const reco::BeamSpot::Point& bsPosition = recoBeamSpotHandle->position(); 
00087 
00088   edm::ESHandle<MagneticField> theMagField;
00089   iSetup.get<IdealMagneticFieldRecord>().get(theMagField);
00090 
00091   reco::ElectronIsolationMap detaMap;
00092   reco::ElectronIsolationMap dphiMap;
00093   reco::RecoEcalCandidateIsolationMap detaCandMap;
00094   reco::RecoEcalCandidateIsolationMap dphiCandMap;
00095   
00096   if(!useSCRefs_){
00097     for(reco::ElectronCollection::const_iterator iElectron = electronHandle->begin(); iElectron != electronHandle->end(); iElectron++){
00098   
00099       reco::ElectronRef eleref(reco::ElectronRef(electronHandle,iElectron - electronHandle->begin()));
00100       std::pair<float,float> dEtaDPhi = calDEtaDPhiSCTrk(eleref,bsPosition,theMagField.product());
00101       
00102       detaMap.insert(eleref, dEtaDPhi.first);
00103       dphiMap.insert(eleref, dEtaDPhi.second);
00104     }
00105   }else { //we loop over reco ecal candidates
00106      edm::Handle<reco::RecoEcalCandidateCollection> recoEcalCandHandle;
00107      iEvent.getByLabel(recoEcalCandidateProducer_,recoEcalCandHandle);
00108      for(reco::RecoEcalCandidateCollection::const_iterator iRecoEcalCand = recoEcalCandHandle->begin(); iRecoEcalCand != recoEcalCandHandle->end(); iRecoEcalCand++){
00109     
00110        reco::RecoEcalCandidateRef recoEcalCandRef(recoEcalCandHandle,iRecoEcalCand-recoEcalCandHandle->begin());
00111     
00112        reco::ElectronRef eleRef = getEleRef(recoEcalCandRef,electronHandle);
00113        std::pair<float,float> dEtaDPhi(999999,999999);
00114        if(eleRef.isNonnull()) dEtaDPhi =  calDEtaDPhiSCTrk(eleRef,bsPosition,theMagField.product());
00115        detaCandMap.insert(recoEcalCandRef, dEtaDPhi.first);
00116        dphiCandMap.insert(recoEcalCandRef, dEtaDPhi.second);
00117      }//end loop over reco ecal candidates
00118   }//end if between electrons or reco ecal candidates
00119 
00120   if(!useSCRefs_){
00121     std::auto_ptr<reco::ElectronIsolationMap> detMap(new reco::ElectronIsolationMap(detaMap));
00122     std::auto_ptr<reco::ElectronIsolationMap> dphMap(new reco::ElectronIsolationMap(dphiMap));
00123     iEvent.put(detMap, "Deta" );
00124     iEvent.put(dphMap, "Dphi" );
00125   }else{
00126     std::auto_ptr<reco::RecoEcalCandidateIsolationMap> detaCandMapForEvent(new reco::RecoEcalCandidateIsolationMap(detaCandMap));
00127     std::auto_ptr<reco::RecoEcalCandidateIsolationMap> dphiCandMapForEvent(new reco::RecoEcalCandidateIsolationMap(dphiCandMap));
00128     iEvent.put(detaCandMapForEvent, "Deta" );
00129     iEvent.put(dphiCandMapForEvent, "Dphi" );
00130   }     
00131 }
00132 
00133 std::pair<float,float> EgammaHLTElectronDetaDphiProducer::calDEtaDPhiSCTrk(reco::ElectronRef& eleref, const reco::BeamSpot::Point& bsPosition,const MagneticField *magField) {
00134 
00135   const reco::SuperClusterRef theClus = eleref->superCluster();
00136   const math::XYZVector trackMom =  eleref->track()->momentum();
00137   
00138   math::XYZPoint SCcorrPosition(theClus->x()-bsPosition.x(), theClus->y()-bsPosition.y() , theClus->z()-eleref->track()->vz() );
00139   float deltaeta = fabs(SCcorrPosition.eta()-eleref->track()->eta());
00140   float deltaphi = 999.;
00141   
00142   bool recoveryForFailingPropagation = false;
00143   if (variablesAtVtx_) {
00144     reco::TrackRef track = eleref->track();
00145     reco::TransientTrack tt(track, magField_);
00146     TrajectoryStateOnSurface sclTSOS = tt.stateOnSurface(GlobalPoint(theClus->x(),theClus->y(),theClus->z()));
00147 
00148     if (sclTSOS.isValid()) {
00149       EleRelPointPair scAtVtx(theClus->position(), sclTSOS.globalPosition(), bsPosition);
00150       deltaeta = fabs(scAtVtx.dEta());
00151       deltaphi = fabs(scAtVtx.dPhi());
00152     } else {
00153       recoveryForFailingPropagation = true;
00154     }
00155   } else if (useTrackProjectionToEcal_ or recoveryForFailingPropagation) { 
00156     ECALPositionCalculator posCalc;
00157     const math::XYZPoint vertex(bsPosition.x(),bsPosition.y(),eleref->track()->vz());
00158     
00159     float phi1= posCalc.ecalPhi(magField,trackMom,vertex,1);
00160     float phi2= posCalc.ecalPhi(magField,trackMom,vertex,-1);
00161     
00162     float deltaphi1=fabs( phi1 - theClus->position().phi() );
00163     if(deltaphi1>6.283185308) deltaphi1 -= 6.283185308;
00164     if(deltaphi1>3.141592654) deltaphi1 = 6.283185308-deltaphi1;
00165 
00166     float deltaphi2=fabs( phi2 - theClus->position().phi() );
00167     if(deltaphi2>6.283185308) deltaphi2 -= 6.283185308;
00168     if(deltaphi2>3.141592654) deltaphi2 = 6.283185308-deltaphi2;
00169     
00170     deltaphi = deltaphi1;
00171     if(deltaphi2<deltaphi1){ deltaphi = deltaphi2;}
00172   } else {
00173     deltaphi=fabs(eleref->track()->outerPosition().phi()-theClus->phi());
00174     if(deltaphi>6.283185308) deltaphi -= 6.283185308;
00175     if(deltaphi>3.141592654) deltaphi = 6.283185308-deltaphi;  
00176   }
00177   
00178   return std::make_pair(deltaeta,deltaphi);  
00179 }
00180 
00181 reco::ElectronRef EgammaHLTElectronDetaDphiProducer::getEleRef(const reco::RecoEcalCandidateRef& recoEcalCandRef,const edm::Handle<reco::ElectronCollection>& electronHandle)
00182 {
00183   reco::ElectronRef eleRef;
00184   for(reco::ElectronCollection::const_iterator eleIt = electronHandle->begin(); eleIt != electronHandle->end(); eleIt++){
00185     if(eleIt->superCluster()==recoEcalCandRef->superCluster()){
00186       eleRef = reco::ElectronRef(electronHandle,eleIt - electronHandle->begin());
00187       break;
00188     }
00189   }
00190   return eleRef;
00191 }
00192   
00193 
00194 void EgammaHLTElectronDetaDphiProducer::beginRun(edm::Run&, edm::EventSetup const& iSetup) {
00195   using namespace edm;
00196 
00197   ESHandle<MagneticField> magneticField;
00198   iSetup.get<IdealMagneticFieldRecord>().get(magneticField);
00199   magField_ = magneticField.product();
00200 
00201 }
00202 
00203 void EgammaHLTElectronDetaDphiProducer::endRun(edm::Run&, edm::EventSetup const&){}
00204 
00205 //define this as a plug-in
00206 //DEFINE_FWK_MODULE(EgammaHLTTrackIsolationProducers);