CMS 3D CMS Logo

SoftElectronProducer.cc

Go to the documentation of this file.
00001 
00002 #include <vector>
00003 #include <iostream>
00004 #include <boost/regex.hpp>
00005 
00006 #include "FWCore/Framework/interface/Frameworkfwd.h"
00007 #include "FWCore/Framework/interface/EDProducer.h"
00008 #include "FWCore/Framework/interface/ESHandle.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Utilities/interface/Exception.h"
00012 
00013 #include "DataFormats/Common/interface/Handle.h"
00014 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00015 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00016 #include "DataFormats/TrackReco/interface/Track.h"
00017 #include "DataFormats/VertexReco/interface/Vertex.h"
00018 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00019 #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
00020 #include "DataFormats/EgammaReco/interface/ClusterShape.h"
00021 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00022 #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
00023 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00024 
00025 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00026 
00027 #include "RecoCaloTools/Selectors/interface/CaloConeSelector.h"
00028 #include "RecoCaloTools/MetaCollections/interface/CaloRecHitMetaCollections.h"
00029 
00030 #include "TrackingTools/TrackAssociator/interface/TrackAssociatorParameters.h"
00031 #include "TrackingTools/TrackAssociator/interface/TrackDetectorAssociator.h"
00032 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
00033 
00034 #include "ElectronIdMLP.h"
00035 #include "SoftElectronProducer.h"
00036 
00037 //------------------------------------------------------------------------------
00038 
00039 using namespace std;
00040 using namespace edm;
00041 
00042 //------------------------------------------------------------------------------
00043 
00044 SoftElectronProducer::SoftElectronProducer(const edm::ParameterSet &iConf) :
00045   theConf(iConf), theTrackAssociator(0), theElecNN(0)
00046 {
00047   theTrackTag             = theConf.getParameter<InputTag>("TrackTag");
00048 
00049   theHBHERecHitTag        = theConf.getParameter<InputTag>("HBHERecHitTag");
00050   theBasicClusterTag      = theConf.getParameter<InputTag>("BasicClusterTag");
00051  // theBasicClusterShapeTag = theConf.getParameter<InputTag>("BasicClusterShapeTag");
00052 
00053   theHOverEConeSize = theConf.getParameter<double>("HOverEConeSize");
00054 
00055   barrelRecHitCollection_ = theConf.getParameter<edm::InputTag>("BarrelRecHitCollection");
00056   endcapRecHitCollection_ = theConf.getParameter<edm::InputTag>("EndcapRecHitCollection");
00057 
00058   // TrackAssociator and its parameters
00059   theTrackAssociator = new TrackDetectorAssociator();
00060   theTrackAssociator->useDefaultPropagator();
00061   edm::ParameterSet parameters = iConf.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
00062   theTrackAssociatorParameters = new TrackAssociatorParameters( parameters );
00063 
00064   theDiscriminatorCut = theConf.getParameter<double>("DiscriminatorCut");
00065 
00066   theElecNN = new ElectronIdMLP;
00067 
00068   // register the product
00069   produces<reco::ElectronCollection>();
00070 }
00071 
00072 //------------------------------------------------------------------------------
00073 
00074 SoftElectronProducer::~SoftElectronProducer()
00075 {
00076   if (theElecNN)                    delete theElecNN;
00077   if (theTrackAssociator)           delete theTrackAssociator;
00078   if (theTrackAssociatorParameters) delete theTrackAssociatorParameters;
00079 }
00080 
00081 //------------------------------------------------------------------------------
00082 
00083 void SoftElectronProducer::beginJob (edm::EventSetup const & theEventSetup)
00084 {
00085 
00086 }
00087 
00088 //------------------------------------------------------------------------------
00089 
00090 void SoftElectronProducer::produce(edm::Event &iEvent,
00091                                       const edm::EventSetup &iSetup)
00092 {
00093 
00094   auto_ptr<reco::ElectronCollection> candidates(new reco::ElectronCollection());
00095 
00096   Handle<reco::TrackCollection> handleTrack;
00097   reco::TrackCollection::const_iterator itTrack;
00098 
00099   Handle<reco::BasicClusterCollection> handleCluster;
00100   reco::BasicClusterCollection::const_iterator itCluster;
00101 
00102   //Handle<reco::ClusterShapeCollection> handleShape;
00103   //reco::ClusterShapeCollection::const_iterator itShape;
00104 
00105   Handle<HBHERecHitCollection> handleRecHit;
00106   CaloRecHitMetaCollectionV::const_iterator itRecHit;
00107 
00108   ESHandle<CaloGeometry> handleCaloGeom;
00109   
00110 
00111   double hcalEnergy, clusEnergy, trkP;
00112   double x[2], y[2], z[2], eta[2], phi[2];
00113   double covEtaEta, covEtaPhi, covPhiPhi, emFraction, deltaE;
00114   double eMax, e2x2, e3x3, e5x5, v1, v2, v3, v4;
00115   double value, dist, distMin;
00116 
00117   const reco::BasicCluster *matchedCluster;
00118   //const reco::ClusterShape *matchedShape;
00119   const reco::Track *track;
00120 
00121   // get basic clusters
00122   iEvent.getByLabel(theBasicClusterTag, handleCluster);
00123 
00124   // get basic cluster shapes
00125   //iEvent.getByLabel(theBasicClusterShapeTag, handleShape);
00126 
00127   // get rec. hits
00128   iEvent.getByLabel(theHBHERecHitTag, handleRecHit);
00129   HBHERecHitMetaCollection metaRecHit(*handleRecHit);
00130 
00131   //only barrel is used, giving twice the same inputag..
00132 
00133 
00134   EcalClusterLazyTools ecalTool(iEvent, iSetup, barrelRecHitCollection_, endcapRecHitCollection_ );
00135 
00136   // get calorimeter geometry
00137   iSetup.get<CaloGeometryRecord>().get(handleCaloGeom);
00138 
00139   CaloConeSelector selectorRecHit(theHOverEConeSize, handleCaloGeom.product(), DetId::Hcal);
00140 
00141   // get tracks
00142   iEvent.getByLabel(theTrackTag, handleTrack);
00143 
00144   FreeTrajectoryState tmpFTS;
00145   TrackDetMatchInfo info;
00146   unsigned int counterTrack;
00147 
00148   // loop over tracks
00149   for(itTrack = handleTrack->begin(), counterTrack = 0;
00150       itTrack != handleTrack->end();
00151       ++itTrack, ++counterTrack)
00152   {
00153     track = &(*itTrack);
00154 
00155     try {
00156       tmpFTS = theTrackAssociator->getFreeTrajectoryState(iSetup, *track);
00157       info = theTrackAssociator->associate(iEvent, iSetup, tmpFTS, *theTrackAssociatorParameters);
00158     } catch (cms::Exception e) {
00159       // extrapolation failed, skip this track
00160       std::cerr << "Caught exception during track extrapolation: " << e.what() << ". Skipping track" << endl;
00161       continue;
00162     }
00163 
00164     x[0] = info.trkGlobPosAtEcal.x();
00165     y[0] = info.trkGlobPosAtEcal.y();
00166     z[0] = info.trkGlobPosAtEcal.z();
00167     eta[0] = info.trkGlobPosAtEcal.eta();
00168     phi[0] = info.trkGlobPosAtEcal.phi();
00169 
00170     // analyse only tracks passing quality cuts
00171     if(track->numberOfValidHits() >= 8 && track->pt() > 2.0 &&
00172        abs(track->eta()) < 1.2 && info.isGoodEcal)
00173     {
00174       distMin = 1.0e6;
00175       matchedCluster = 0;
00176 //      matchedShape = 0;
00177 
00178       // loop over basic clusters
00179       for(itCluster = handleCluster->begin(); itCluster != handleCluster->end();++itCluster)
00180       {
00181         x[1] = itCluster->x();
00182         y[1] = itCluster->y();
00183         z[1] = itCluster->z();
00184 
00185         eta[1] = itCluster->eta();
00186         phi[1] = itCluster->phi();
00187 
00188 
00189         dist = hypot(x[0] - x[1], y[0] - y[1]);
00190         dist = hypot(dist, z[0] - z[1]);
00191 
00192         if(dist < distMin)
00193         {
00194           distMin = dist;
00195           matchedCluster = &(*itCluster);
00196         }
00197       }
00198 
00199       // identify electrons based on cluster properties
00200       if(matchedCluster  && distMin < 5.0)
00201       {
00202 
00203         GlobalPoint position(matchedCluster->x(), matchedCluster->y(), matchedCluster->z());
00204         auto_ptr<CaloRecHitMetaCollectionV> chosen = selectorRecHit.select(position, metaRecHit);
00205         hcalEnergy = 0.0;
00206         for(itRecHit = chosen->begin(); itRecHit != chosen->end(); ++itRecHit)
00207         {
00208           hcalEnergy += itRecHit->energy();
00209         }
00210 
00211         clusEnergy = matchedCluster->energy();
00212         trkP = track->p();
00213 
00214         deltaE = (clusEnergy - trkP)/(clusEnergy + trkP);
00215         emFraction =  clusEnergy/(clusEnergy + hcalEnergy);
00216 
00217         eMax = ecalTool.eMax(*matchedCluster);
00218         e2x2 = ecalTool.e2x2(*matchedCluster);
00219         e3x3 = ecalTool.e3x3(*matchedCluster);
00220         e5x5 = ecalTool.e5x5(*matchedCluster);
00221         v1 = eMax/e3x3;
00222         v2 = eMax/e2x2;
00223         v3 = e2x2/e5x5;
00224         v4 = ((e5x5 - eMax) < 0.001) ? 1.0 : (e3x3 - eMax)/(e5x5 - eMax);
00225         std::vector<float> cov = ecalTool.covariances(*matchedCluster); 
00226         covEtaEta = cov[0];
00227         covEtaPhi = cov[1];
00228         covPhiPhi = cov[2];
00229 
00230         value = theElecNN->value(0, covEtaEta, covEtaPhi, covPhiPhi,
00231                                  v1, v2, v3, v4, emFraction, deltaE);
00232 
00233         if (value > theDiscriminatorCut)
00234         {
00235           const reco::Particle::LorentzVector  p4(0.0, 0.0, 0.0, clusEnergy);
00236           const reco::Particle::Point vtx(0.0, 0.0, 0.0);
00237 
00238           reco::Electron newCandidate(0, p4, vtx);
00239           reco::TrackRef refTrack(handleTrack, counterTrack);
00240   
00241           newCandidate.setTrack(refTrack);
00242 
00243           candidates->push_back(newCandidate);
00244         }
00245       }
00246     }
00247   }
00248   
00249   // put the product in the event
00250   iEvent.put(candidates);
00251 
00252 }
00253 
00254 //------------------------------------------------------------------------------
00255 

Generated on Tue Jun 9 17:43:05 2009 for CMSSW by  doxygen 1.5.4