CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/RecoTauTag/RecoTau/plugins/TauDiscriminationAgainstCaloMuon.cc

Go to the documentation of this file.
00001 
00002 /* 
00003  * class TauDiscriminationAgainstCaloMuon
00004  * created : Nov 20 2010
00005  * revised : 
00006  * Authors : Christian Veelken (UC Davis)
00007  */
00008 
00009 #include "RecoTauTag/RecoTau/interface/TauDiscriminationProducerBase.h"
00010 
00011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00012 #include "FWCore/Utilities/interface/InputTag.h"
00013 
00014 #include "DataFormats/MuonReco/interface/Muon.h"
00015 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00016 
00017 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
00018 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00019 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00020 #include "DataFormats/HcalRecHit/interface/HBHERecHit.h"
00021 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00022 #include "DataFormats/CaloTowers/interface/CaloTower.h"
00023 #include "DataFormats/CaloTowers/interface/CaloTowerCollection.h"
00024 
00025 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
00026 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00027 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00028 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateClosestToPoint.h"
00029 
00030 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00031 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00032 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00033 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00034 
00035 #include "DataFormats/VertexReco/interface/Vertex.h"
00036 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00037 
00038 #include "DataFormats/Math/interface/deltaR.h"
00039 
00040 #include <TVector3.h>
00041 #include <TMath.h>
00042 
00043 #include <string>
00044 
00045 using namespace reco;
00046 
00047 // define acess to lead. track for CaloTaus
00048 template <typename T>
00049 class TauLeadTrackExtractor
00050 {
00051  public:
00052   reco::TrackRef getLeadTrack(const T& tau) const
00053   {
00054     return tau.leadTrack();
00055   }
00056   double getTrackPtSum(const T& tau) const
00057   {
00058     double trackPtSum = 0.;
00059     for ( TrackRefVector::const_iterator signalTrack = tau.signalTracks().begin();
00060           signalTrack != tau.signalTracks().end(); ++signalTrack ) {
00061       trackPtSum += (*signalTrack)->pt();
00062     }
00063     return trackPtSum;
00064   }
00065 };
00066 
00067 // define acess to lead. track for PFTaus
00068 template <>
00069 class TauLeadTrackExtractor<reco::PFTau>
00070 {
00071  public:
00072   reco::TrackRef getLeadTrack(const reco::PFTau& tau) const
00073   {
00074     return tau.leadPFChargedHadrCand()->trackRef();
00075   }
00076   double getTrackPtSum(const reco::PFTau& tau) const
00077   {
00078     double trackPtSum = 0.;
00079     for ( PFCandidateRefVector::const_iterator signalTrack = tau.signalPFChargedHadrCands().begin();
00080           signalTrack != tau.signalPFChargedHadrCands().end(); ++signalTrack ) {
00081       trackPtSum += (*signalTrack)->pt();
00082     }
00083     return trackPtSum;
00084   }
00085 };
00086 
00087 template<class TauType, class TauDiscriminator>
00088 class TauDiscriminationAgainstCaloMuon : public TauDiscriminationProducerBase<TauType, TauDiscriminator>
00089 {
00090  public:
00091   // setup framework types for this tautype
00092   typedef std::vector<TauType>    TauCollection; 
00093   typedef edm::Ref<TauCollection> TauRef;    
00094 
00095   explicit TauDiscriminationAgainstCaloMuon(const edm::ParameterSet&);
00096   ~TauDiscriminationAgainstCaloMuon() {} 
00097 
00098   // called at the beginning of every event
00099   void beginEvent(const edm::Event&, const edm::EventSetup&);
00100 
00101   double discriminate(const TauRef&);
00102 
00103  private:  
00104   edm::InputTag srcEcalRecHitsBarrel_;
00105   edm::Handle<EcalRecHitCollection> ebRecHits_;
00106   edm::InputTag srcEcalRecHitsEndcap_;
00107   edm::Handle<EcalRecHitCollection> eeRecHits_;
00108   edm::InputTag srcHcalRecHits_;
00109   edm::Handle<HBHERecHitCollection> hbheRecHits_;
00110 
00111   edm::InputTag srcVertex_;
00112   GlobalPoint eventVertexPosition_;
00113 
00114   const TransientTrackBuilder* trackBuilder_;
00115   const CaloGeometry* caloGeometry_;
00116 
00117   TauLeadTrackExtractor<TauType> leadTrackExtractor_;
00118 
00119   double minLeadTrackPt_;
00120   double minLeadTrackPtFraction_;
00121 
00122   double drEcal_;
00123   double drHcal_;
00124 
00125   double maxEnEcal_;
00126   double maxEnHcal_;
00127 
00128   double maxEnToTrackRatio_;
00129 };
00130 
00131 template<class TauType, class TauDiscriminator>
00132 TauDiscriminationAgainstCaloMuon<TauType, TauDiscriminator>::TauDiscriminationAgainstCaloMuon(const edm::ParameterSet& cfg)
00133   : TauDiscriminationProducerBase<TauType, TauDiscriminator>(cfg) 
00134 {
00135   srcEcalRecHitsBarrel_ = cfg.getParameter<edm::InputTag>("srcEcalRecHitsBarrel");
00136   srcEcalRecHitsEndcap_ = cfg.getParameter<edm::InputTag>("srcEcalRecHitsEndcap");
00137   srcHcalRecHits_ = cfg.getParameter<edm::InputTag>("srcHcalRecHits");
00138 
00139   srcVertex_ = cfg.getParameter<edm::InputTag>("srcVertex");
00140 
00141   minLeadTrackPt_ = cfg.getParameter<double>("minLeadTrackPt");
00142   minLeadTrackPtFraction_ = cfg.getParameter<double>("minLeadTrackPtFraction");
00143 
00144   drEcal_ = cfg.getParameter<double>("dRecal");
00145   drHcal_ = cfg.getParameter<double>("dRhcal");
00146 
00147   maxEnEcal_ = cfg.getParameter<double>("maxEnEcal");
00148   maxEnHcal_ = cfg.getParameter<double>("maxEnHcal");
00149 
00150   maxEnToTrackRatio_ = cfg.getParameter<double>("maxEnToTrackRatio");
00151 }
00152 
00153 template<class TauType, class TauDiscriminator>
00154 void TauDiscriminationAgainstCaloMuon<TauType, TauDiscriminator>::beginEvent(const edm::Event& evt, const edm::EventSetup& evtSetup)
00155 {
00156   evt.getByLabel(srcEcalRecHitsBarrel_, ebRecHits_);
00157   evt.getByLabel(srcEcalRecHitsEndcap_, eeRecHits_);
00158   evt.getByLabel(srcHcalRecHits_, hbheRecHits_);
00159   
00160   edm::ESHandle<TransientTrackBuilder> trackBuilderHandle;
00161   evtSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", trackBuilderHandle);
00162   trackBuilder_ = trackBuilderHandle.product();
00163   if ( !trackBuilder_ ) {
00164     edm::LogError ("TauDiscriminationAgainstCaloMuon::discriminate")
00165       << " Failed to access TransientTrackBuilder !!";
00166   }
00167 
00168   edm::ESHandle<CaloGeometry> caloGeometryHandle;
00169   evtSetup.get<CaloGeometryRecord>().get(caloGeometryHandle);
00170   caloGeometry_ = caloGeometryHandle.product();
00171   if ( !caloGeometry_ ) {
00172     edm::LogError ("TauDiscriminationAgainstCaloMuon::discriminate")
00173       << " Failed to access CaloGeometry !!";
00174   }
00175 
00176   edm::Handle<reco::VertexCollection> vertices;
00177   evt.getByLabel(srcVertex_, vertices);
00178   eventVertexPosition_ = GlobalPoint(0., 0., 0.);
00179   if ( vertices->size() >= 1 ) {
00180     const reco::Vertex& thePrimaryEventVertex = (*vertices->begin());
00181     eventVertexPosition_ = GlobalPoint(thePrimaryEventVertex.x(), thePrimaryEventVertex.y(), thePrimaryEventVertex.z());
00182   }
00183 }               
00184 
00185 double compEcalEnergySum(const EcalRecHitCollection& ecalRecHits, 
00186                          const CaloSubdetectorGeometry* detGeometry, 
00187                          const reco::TransientTrack& transientTrack, double dR, 
00188                          const GlobalPoint& eventVertexPosition)
00189 {
00190   double ecalEnergySum = 0.;
00191   for ( EcalRecHitCollection::const_iterator ecalRecHit = ecalRecHits.begin();
00192         ecalRecHit != ecalRecHits.end(); ++ecalRecHit ) {
00193     const CaloCellGeometry* cellGeometry = detGeometry->getGeometry(ecalRecHit->detid());
00194     
00195     if ( !cellGeometry ) {
00196       edm::LogError ("compEcalEnergySum") 
00197         << " Failed to access ECAL geometry for detId = " << ecalRecHit->detid().rawId()
00198         << " --> skipping !!";
00199       continue;
00200     }
00201 
00202     const GlobalPoint& cellPosition = cellGeometry->getPosition();
00203 
00204 //--- CV: speed up computation by requiring eta-phi distance
00205 //        between cell position and track direction to be dR < 0.5
00206     Vector3DBase<float, GlobalTag> cellPositionRelVertex = (cellPosition) - eventVertexPosition;
00207     if ( deltaR(cellPositionRelVertex.eta(), cellPositionRelVertex.phi(), 
00208                 transientTrack.track().eta(), transientTrack.track().phi()) > 0.5 ) continue;
00209 
00210     TrajectoryStateClosestToPoint dcaPosition = transientTrack.trajectoryStateClosestToPoint(cellPosition);
00211     
00212     Vector3DBase<float, GlobalTag> d = (cellPosition - dcaPosition.position());
00213 
00214     TVector3 d3(d.x(), d.y(), d.z());
00215     TVector3 dir(transientTrack.track().px(), transientTrack.track().py(), transientTrack.track().pz());
00216 
00217     double dPerp = d3.Cross(dir.Unit()).Mag();
00218     double dParl = TVector3(cellPosition.x(), cellPosition.y(), cellPosition.z()).Dot(dir.Unit());
00219     
00220     if ( dPerp < dR && dParl > 100. ) {
00221       ecalEnergySum += ecalRecHit->energy();
00222     }
00223   }
00224   
00225   return ecalEnergySum;
00226 }
00227 
00228 double compHcalEnergySum(const HBHERecHitCollection& hcalRecHits, 
00229                          const CaloSubdetectorGeometry* hbGeometry, const CaloSubdetectorGeometry* heGeometry, 
00230                          const reco::TransientTrack& transientTrack, double dR, 
00231                          const GlobalPoint& eventVertexPosition)
00232 {
00233   double hcalEnergySum = 0.;
00234   for ( HBHERecHitCollection::const_iterator hcalRecHit = hcalRecHits.begin();
00235         hcalRecHit != hcalRecHits.end(); ++hcalRecHit ) {
00236     const CaloCellGeometry* hbCellGeometry = hbGeometry->getGeometry(hcalRecHit->detid());
00237     const CaloCellGeometry* heCellGeometry = heGeometry->getGeometry(hcalRecHit->detid());
00238 
00239     const GlobalPoint* cellPosition = 0;
00240     if ( hbCellGeometry ) cellPosition = &(hbCellGeometry->getPosition());
00241     if ( heCellGeometry ) cellPosition = &(heCellGeometry->getPosition());
00242 
00243     if ( !cellPosition ) {
00244       edm::LogError ("compHcalEnergySum") 
00245         << " Failed to access HCAL geometry for detId = " << hcalRecHit->detid().rawId()
00246         << " --> skipping !!";
00247       continue;
00248     }
00249 
00250 //--- CV: speed up computation by requiring eta-phi distance
00251 //        between cell position and track direction to be dR < 0.5
00252     Vector3DBase<float, GlobalTag> cellPositionRelVertex = (*cellPosition) - eventVertexPosition;
00253     if ( deltaR(cellPositionRelVertex.eta(), cellPositionRelVertex.phi(), 
00254                 transientTrack.track().eta(), transientTrack.track().phi()) > 0.5 ) continue;
00255 
00256     TrajectoryStateClosestToPoint dcaPosition = transientTrack.trajectoryStateClosestToPoint(*cellPosition);
00257     
00258     Vector3DBase<float, GlobalTag> d = ((*cellPosition) - dcaPosition.position());
00259 
00260     TVector3 d3(d.x(), d.y(), d.z());
00261     TVector3 dir(transientTrack.track().px(), transientTrack.track().py(), transientTrack.track().pz());
00262 
00263     double dPerp = d3.Cross(dir.Unit()).Mag();
00264     double dParl = TVector3(cellPosition->x(), cellPosition->y(), cellPosition->z()).Dot(dir.Unit());
00265 
00266     if ( dPerp < dR && dParl > 100. ) {
00267       hcalEnergySum += hcalRecHit->energy();
00268     }
00269   }
00270   
00271   return hcalEnergySum;
00272 }
00273 
00274 template<class TauType, class TauDiscriminator>
00275 double TauDiscriminationAgainstCaloMuon<TauType, TauDiscriminator>::discriminate(const TauRef& tau)
00276 {
00277   if ( !(trackBuilder_ && caloGeometry_) ) return 0.;
00278 
00279   const CaloSubdetectorGeometry* ebGeometry = caloGeometry_->getSubdetectorGeometry(DetId::Ecal, EcalBarrel);
00280   const CaloSubdetectorGeometry* eeGeometry = caloGeometry_->getSubdetectorGeometry(DetId::Ecal, EcalEndcap);
00281   const CaloSubdetectorGeometry* hbGeometry = caloGeometry_->getSubdetectorGeometry(DetId::Hcal, HcalBarrel);
00282   const CaloSubdetectorGeometry* heGeometry = caloGeometry_->getSubdetectorGeometry(DetId::Hcal, HcalEndcap);
00283 
00284   TrackRef leadTrackRef = leadTrackExtractor_.getLeadTrack(*tau);
00285 
00286   if ( (leadTrackRef.isAvailable() || leadTrackRef.isTransient()) && leadTrackRef.isNonnull() ) {
00287     double leadTrackPt = leadTrackRef->pt();
00288     double trackPtSum = leadTrackExtractor_.getTrackPtSum(*tau);
00289     
00290     double leadTrackPtFraction = ( trackPtSum > 0. ) ? (leadTrackPt/trackPtSum) : -1.;
00291     
00292     if ( leadTrackPt > minLeadTrackPt_ && leadTrackPtFraction > minLeadTrackPtFraction_ ) {
00293       reco::TransientTrack transientTrack = trackBuilder_->build(leadTrackRef);
00294 
00295       double ebEnergySum = compEcalEnergySum(*ebRecHits_, ebGeometry, transientTrack, drEcal_, eventVertexPosition_);
00296       double eeEnergySum = compEcalEnergySum(*eeRecHits_, eeGeometry, transientTrack, drEcal_, eventVertexPosition_);
00297       double ecalEnergySum = ebEnergySum + eeEnergySum;
00298       
00299       double hbheEnergySum = compHcalEnergySum(*hbheRecHits_, hbGeometry, heGeometry, transientTrack, drHcal_, eventVertexPosition_);
00300       
00301       double caloEnergySum = ecalEnergySum + hbheEnergySum;
00302 
00303       if ( ecalEnergySum <  maxEnEcal_ &&
00304            hbheEnergySum <  maxEnHcal_ &&
00305            caloEnergySum < (maxEnToTrackRatio_*leadTrackPt) ) return 0.;
00306     }
00307   }
00308 
00309   return 1.;
00310 }
00311 
00312 #include "FWCore/Framework/interface/MakerMacros.h"
00313 
00314 typedef TauDiscriminationAgainstCaloMuon<PFTau, PFTauDiscriminator> PFRecoTauDiscriminationAgainstCaloMuon;
00315 typedef TauDiscriminationAgainstCaloMuon<CaloTau, CaloTauDiscriminator> CaloRecoTauDiscriminationAgainstCaloMuon;
00316 
00317 DEFINE_FWK_MODULE(PFRecoTauDiscriminationAgainstCaloMuon);
00318 DEFINE_FWK_MODULE(CaloRecoTauDiscriminationAgainstCaloMuon);