CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/RecoEgamma/EgammaHLTAlgos/src/EgammaHLTPixelMatchElectronAlgo.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    EgammaHLTAlgos
00004 // Class:      EgammaHLTPixelMatchElectronAlgo.
00005 // 
00011 //
00012 // Original Author:  Monica Vazquez Acosta (CERN)
00013 // $Id: EgammaHLTPixelMatchElectronAlgo.cc,v 1.16 2012/01/23 12:52:30 sharper Exp $
00014 //
00015 //
00016 #include "RecoEgamma/EgammaHLTAlgos/interface/EgammaHLTPixelMatchElectronAlgo.h"
00017 
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 
00021 #include "TrackingTools/PatternTools/interface/TSCPBuilderNoMaterial.h"
00022 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00023 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00024 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateClosestToPoint.h"
00025 
00026 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00027 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00028 
00029 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00030 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00031 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
00032 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
00033 
00034 #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
00035 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
00036 
00037 #include "DataFormats/TrackReco/interface/Track.h"
00038 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00039 
00040 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00041 #include "DataFormats/Math/interface/Point3D.h"
00042 
00043 #include "TrackingTools/GsfTools/interface/MultiTrajectoryStateMode.h"
00044 #include "TrackingTools/GsfTools/interface/MultiTrajectoryStateTransform.h"
00045 
00046 using namespace edm;
00047 using namespace std;
00048 using namespace reco;
00049 
00050 EgammaHLTPixelMatchElectronAlgo::EgammaHLTPixelMatchElectronAlgo(const edm::ParameterSet &conf) :
00051   trackProducer_(conf.getParameter<edm::InputTag>("TrackProducer")),
00052   gsfTrackProducer_(conf.getParameter<edm::InputTag>("GsfTrackProducer")),
00053   useGsfTracks_(conf.getParameter<bool>("UseGsfTracks")),
00054   bsProducer_(conf.getParameter<edm::InputTag>("BSProducer")), 
00055   mtsMode_(new MultiTrajectoryStateMode()),
00056   mtsTransform_(0),
00057   cacheIDTDGeom_(0),
00058   cacheIDMagField_(0)
00059 {
00060 
00061 }
00062 
00063   EgammaHLTPixelMatchElectronAlgo::~EgammaHLTPixelMatchElectronAlgo() 
00064 {
00065   delete mtsTransform_;
00066   delete mtsMode_;
00067  }
00068 
00069 void EgammaHLTPixelMatchElectronAlgo::setupES(const edm::EventSetup& es) {
00070   //services
00071   
00072   bool updateField(false);
00073   if (cacheIDMagField_!=es.get<IdealMagneticFieldRecord>().cacheIdentifier()){
00074     updateField = true;
00075     cacheIDMagField_=es.get<IdealMagneticFieldRecord>().cacheIdentifier();
00076     es.get<IdealMagneticFieldRecord>().get(magField_);
00077   }
00078   
00079   if(useGsfTracks_){ //only need the geom and mtsTransform if we are doing gsf tracks
00080     bool updateGeometry(false);
00081     if (cacheIDTDGeom_!=es.get<TrackerDigiGeometryRecord>().cacheIdentifier()){
00082       updateGeometry = true;
00083        cacheIDTDGeom_=es.get<TrackerDigiGeometryRecord>().cacheIdentifier();
00084        es.get<TrackerDigiGeometryRecord>().get(trackerGeom_);
00085     }
00086     if ( updateField || updateGeometry || !mtsTransform_ ) {
00087       delete mtsTransform_;
00088       mtsTransform_ = new MultiTrajectoryStateTransform(trackerGeom_.product(),magField_.product());
00089     }
00090   }
00091   
00092   
00093 }
00094 
00095 void  EgammaHLTPixelMatchElectronAlgo::run(Event& e, ElectronCollection & outEle) {
00096   
00097   // get the input 
00098   edm::Handle<TrackCollection> tracksH;
00099   if (!useGsfTracks_)
00100     e.getByLabel(trackProducer_,tracksH);
00101 
00102   // get the input 
00103   edm::Handle<GsfTrackCollection> gsfTracksH;
00104   if (useGsfTracks_)
00105     e.getByLabel(gsfTrackProducer_, gsfTracksH);
00106 
00107   //Get the Beam Spot position
00108   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00109   e.getByLabel(bsProducer_,recoBeamSpotHandle);
00110 
00111   // gets its position
00112   const BeamSpot::Point& bsPosition = recoBeamSpotHandle->position(); 
00113   Global3DPoint bs(bsPosition.x(),bsPosition.y(),0);
00114   process(tracksH, gsfTracksH, outEle, bs);
00115   
00116   return;
00117 }
00118 
00119 void EgammaHLTPixelMatchElectronAlgo::process(edm::Handle<TrackCollection> tracksH, edm::Handle<GsfTrackCollection> gsfTracksH, ElectronCollection & outEle, Global3DPoint & bs) {
00120 
00121   if (!useGsfTracks_) {
00122 
00123     for (unsigned int i=0; i<tracksH->size(); ++i) {
00124       
00125       const TrackRef trackRef = edm::Ref<TrackCollection>(tracksH, i);
00126       edm::RefToBase<TrajectorySeed> seed = trackRef->extra()->seedRef();
00127       ElectronSeedRef elseed=seed.castTo<ElectronSeedRef>();
00128       
00129       edm::RefToBase<CaloCluster> caloCluster = elseed->caloCluster() ;
00130       SuperClusterRef scRef = caloCluster.castTo<SuperClusterRef>() ;
00131       
00132       // Get the momentum at vertex (not at the innermost layer)
00133       TSCPBuilderNoMaterial tscpBuilder;
00134       
00135       FreeTrajectoryState fts = trajectoryStateTransform::innerFreeState(*trackRef, magField_.product());
00136       TrajectoryStateClosestToPoint tscp = tscpBuilder(fts, bs);
00137       
00138       float scale = scRef->energy()/tscp.momentum().mag();
00139       
00140       const math::XYZTLorentzVector momentum(tscp.momentum().x()*scale,
00141                                              tscp.momentum().y()*scale,
00142                                              tscp.momentum().z()*scale,
00143                                              scRef->energy());
00144 
00145       Electron ele(trackRef->charge(), momentum, trackRef->vertex());
00146       ele.setSuperCluster(scRef);
00147       edm::Ref<TrackCollection> myRef(tracksH, i);
00148       ele.setTrack(myRef);
00149       outEle.push_back(ele);
00150     }  // loop over tracks
00151   } else {
00152 
00153     // clean gsf tracks
00154     std::vector<unsigned int> flag(gsfTracksH->size(), 0);
00155     if (gsfTracksH->size() == 0)
00156       return;
00157 
00158     for (unsigned int i=0; i<gsfTracksH->size()-1; ++i) {
00159       const GsfTrackRef trackRef1 = edm::Ref<GsfTrackCollection>(gsfTracksH, i);
00160       ElectronSeedRef elseed1 = trackRef1->extra()->seedRef().castTo<ElectronSeedRef>();
00161       SuperClusterRef scRef1 = elseed1->caloCluster().castTo<SuperClusterRef>();
00162 
00163       TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef1));
00164       TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
00165       GlobalVector innMom;
00166       float pin1 = trackRef1->pMode();
00167       if (fts.isValid()) {
00168         mtsMode_->momentumFromModeCartesian(fts, innMom);  
00169         pin1 = innMom.mag();
00170       }
00171 
00172       for (unsigned int j=i+1; j<gsfTracksH->size(); ++j) {
00173         const GsfTrackRef trackRef2 = edm::Ref<GsfTrackCollection>(gsfTracksH, j);
00174         ElectronSeedRef elseed2 = trackRef2->extra()->seedRef().castTo<ElectronSeedRef>();
00175         SuperClusterRef scRef2 = elseed2->caloCluster().castTo<SuperClusterRef>();
00176 
00177         TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef2));
00178         TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
00179         GlobalVector innMom;
00180         float pin2 = trackRef2->pMode();
00181         if (fts.isValid()) {
00182           mtsMode_->momentumFromModeCartesian(fts, innMom);  
00183           pin2 = innMom.mag();
00184         }
00185         
00186         if (scRef1 == scRef2) {
00187           bool isSameLayer = false;
00188           bool iGsfInnermostWithLostHits = isInnerMostWithLostHits(trackRef2, trackRef1, isSameLayer);
00189           
00190           if (iGsfInnermostWithLostHits) {
00191             flag[j] = 1;
00192           } else if(isSameLayer) {
00193             if(fabs((scRef1->energy()/pin1)-1) < fabs((scRef2->energy()/pin2)-1))
00194               flag[j] = 1;
00195           } else {
00196             flag[i] = 1;
00197           }
00198         }
00199       }
00200     }
00201 
00202     for (unsigned int i=0; i<gsfTracksH->size(); ++i) {
00203       if (flag[i] == 1)
00204         continue;
00205 
00206       const GsfTrackRef trackRef = edm::Ref<GsfTrackCollection>(gsfTracksH, i);
00207       ElectronSeedRef elseed = trackRef->extra()->seedRef().castTo<ElectronSeedRef>();
00208       SuperClusterRef scRef = elseed->caloCluster().castTo<SuperClusterRef>();
00209       
00210       // Get the momentum at vertex (not at the innermost layer)
00211       TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef));
00212       TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
00213       GlobalVector innMom;
00214       mtsMode_->momentumFromModeCartesian(inTSOS, innMom);
00215       if (fts.isValid()) {
00216         mtsMode_->momentumFromModeCartesian(fts, innMom);  
00217       }
00218       
00219       float scale = scRef->energy()/innMom.mag();
00220       const math::XYZTLorentzVector momentum(innMom.x()*scale,
00221                                              innMom.y()*scale,
00222                                              innMom.z()*scale,
00223                                              scRef->energy());
00224       
00225       Electron ele(trackRef->charge(), momentum, trackRef->vertex());
00226       ele.setSuperCluster(scRef);
00227       edm::Ref<GsfTrackCollection> myRef(gsfTracksH, i);
00228       ele.setGsfTrack(myRef);
00229       outEle.push_back(ele);
00230     }
00231   }
00232 }
00233 
00234 bool EgammaHLTPixelMatchElectronAlgo::isInnerMostWithLostHits(const reco::GsfTrackRef& nGsfTrack, const reco::GsfTrackRef& iGsfTrack, bool& sameLayer) {
00235   
00236   // define closest using the lost hits on the expectedhitsineer
00237   unsigned int nLostHits = nGsfTrack->trackerExpectedHitsInner().numberOfLostHits();
00238   unsigned int iLostHits = iGsfTrack->trackerExpectedHitsInner().numberOfLostHits();
00239   
00240   if (nLostHits!=iLostHits) {
00241     return (nLostHits > iLostHits);
00242   } else {
00243     sameLayer = true;
00244     return  false; 
00245   }
00246 }
00247 
00248