CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFMuonCaloCleaner.cc
Go to the documentation of this file.
2 
4 
12 
15 
17 
18 #include <TMath.h>
19 
20 #include <iostream>
21 #include <iomanip>
22 
24  : srcSelectedMuons_(cfg.getParameter<edm::InputTag>("selectedMuons")),
25  srcPFCandidates_(cfg.getParameter<edm::InputTag>("pfCandidates")),
26  dRmatch_(cfg.getParameter<double>("dRmatch"))
27 {
28  // maps of detId to energy deposit attributed to muon
29  produces<detIdToFloatMap>("energyDepositsMuPlus");
30  produces<detIdToFloatMap>("energyDepositsMuMinus");
31 
32  verbosity_ = ( cfg.exists("verbosity") ) ?
33  cfg.getParameter<int>("verbosity") : 0;
34 }
35 
37 {
38  std::auto_ptr<detIdToFloatMap> energyDepositMuPlus(new detIdToFloatMap());
39  std::auto_ptr<detIdToFloatMap> energyDepositMuMinus(new detIdToFloatMap());
40 
41  std::vector<reco::CandidateBaseRef > selMuons = getSelMuons(evt, srcSelectedMuons_);
42  const reco::CandidateBaseRef muPlus = getTheMuPlus(selMuons);
43  const reco::CandidateBaseRef muMinus = getTheMuMinus(selMuons);
44 
45  typedef edm::View<reco::PFCandidate> PFCandidateView;
47  evt.getByLabel(srcPFCandidates_, pfCandidates);
48 
49  if ( muPlus.isNonnull() ) fillEnergyDepositMap(dynamic_cast<const reco::Muon*>(&*muPlus), *pfCandidates, *energyDepositMuPlus);
50  if ( muMinus.isNonnull() ) fillEnergyDepositMap(dynamic_cast<const reco::Muon*>(&*muMinus), *pfCandidates, *energyDepositMuMinus);
51 
52  evt.put(energyDepositMuPlus, "energyDepositsMuPlus");
53  evt.put(energyDepositMuMinus, "energyDepositsMuMinus");
54 }
55 
56 namespace
57 {
58  std::string getPFCandidateType(reco::PFCandidate::ParticleType pfCandidateType)
59  {
60  if ( pfCandidateType == reco::PFCandidate::X ) return "undefined";
61  else if ( pfCandidateType == reco::PFCandidate::h ) return "PFChargedHadron";
62  else if ( pfCandidateType == reco::PFCandidate::e ) return "PFElectron";
63  else if ( pfCandidateType == reco::PFCandidate::mu ) return "PFMuon";
64  else if ( pfCandidateType == reco::PFCandidate::gamma ) return "PFGamma";
65  else if ( pfCandidateType == reco::PFCandidate::h0 ) return "PFNeutralHadron";
66  else if ( pfCandidateType == reco::PFCandidate::h_HF ) return "HF_had";
67  else if ( pfCandidateType == reco::PFCandidate::egamma_HF ) return "HF_em";
68  else assert(0);
69  }
70 }
71 
73 {
74  if ( !muon )
75  throw cms::Exception("InvalidData")
76  << "Collection 'selectedMuons' = " << srcSelectedMuons_.label() << " is not of valid type !!\n";
77 
78  bool isMatched = false;
79  for ( edm::View<reco::PFCandidate>::const_iterator pfCandidate = pfCandidates.begin();
80  pfCandidate != pfCandidates.end(); ++pfCandidate ) {
81  if ( pfCandidate->particleId() == reco::PFCandidate::mu ) {
82  double dR = deltaR(pfCandidate->p4(), muon->p4());
83  if ( dR < dRmatch_ ) {
84  // NOTE: particleFlow sequence needs to be rerun for Zmumu event
85  // in order to recreate the links from PFCandidates->PFBlocks->PFClusters->PFRecHitFractions
86  // (not stored in AOD/RECO event content)
87  const reco::PFCandidate::ElementsInBlocks& pfBlocks = pfCandidate->elementsInBlocks();
88  for ( reco::PFCandidate::ElementsInBlocks::const_iterator pfBlock = pfBlocks.begin();
89  pfBlock != pfBlocks.end(); ++pfBlock ) {
90  const edm::OwnVector<reco::PFBlockElement>& pfBlockElements = pfBlock->first->elements();
91  for ( edm::OwnVector<reco::PFBlockElement>::const_iterator pfBlockElement = pfBlockElements.begin();
92  pfBlockElement != pfBlockElements.end(); ++pfBlockElement ) {
93  if ( pfBlockElement->clusterRef().isNonnull() ) {
94  reco::PFClusterRef pfCluster = pfBlockElement->clusterRef();
95  const std::vector<reco::PFRecHitFraction>& pfRecHitFractions = pfCluster->recHitFractions();
96  for ( std::vector<reco::PFRecHitFraction>::const_iterator pfRecHitFraction = pfRecHitFractions.begin();
97  pfRecHitFraction != pfRecHitFractions.end(); ++pfRecHitFraction ) {
98  const reco::PFRecHitRef& pfRecHit = pfRecHitFraction->recHitRef();
99  energyDepositMap[pfRecHit->detId()] += pfRecHitFraction->fraction();
100  }
101  }
102  }
103  }
104  isMatched = true;
105  }
106  }
107  }
108  if ( !isMatched ) {
109  edm::LogWarning("PFMuonCaloCleaner")
110  << "Failed to match Muon to PFCandidate: Pt = " << muon->pt() << ", eta = " << muon->eta() << ", phi = " << muon->phi() << " !!" << std::endl;
111  if ( verbosity_ ) {
112  int idx = 0;
113  for ( edm::View<reco::PFCandidate>::const_iterator pfCandidate = pfCandidates.begin();
114  pfCandidate != pfCandidates.end(); ++pfCandidate ) {
115  std::cout << "PFCandidate #" << idx << " (" << getPFCandidateType(pfCandidate->particleId()) << "):"
116  << " Pt = " << pfCandidate->pt() << ", eta = " << pfCandidate->eta() << ", phi = " << pfCandidate->phi() << std::endl;
117  ++idx;
118  }
119  }
120  }
121 }
122 
124 
126 
127 
128 
129 
130 
T getParameter(std::string const &) const
reco::CandidateBaseRef getTheMuMinus(const std::vector< reco::CandidateBaseRef > &)
ParticleType
particle types
Definition: PFCandidate.h:44
tuple cfg
Definition: looper.py:237
virtual float pt() const
transverse momentum
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual float phi() const
momentum azimuthal angle
bool exists(std::string const &parameterName) const
checks if a parameter exists
void fillEnergyDepositMap(const reco::Muon *, const edm::View< reco::PFCandidate > &, detIdToFloatMap &)
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:280
edm::InputTag srcPFCandidates_
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:382
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
iterator begin()
Definition: OwnVector.h:234
const_iterator begin() const
virtual void produce(edm::Event &, const edm::EventSetup &)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
virtual float eta() const
momentum pseudorapidity
bool isMatched(TrackingRecHit const &hit)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
iterator end()
Definition: OwnVector.h:239
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
PFMuonCaloCleaner(const edm::ParameterSet &)
std::map< uint32_t, float > detIdToFloatMap
std::string const & label() const
Definition: InputTag.h:42
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
tuple cout
Definition: gather_cfg.py:121
const_iterator end() const
std::vector< reco::CandidateBaseRef > getSelMuons(const edm::Event &, const edm::InputTag &)
reco::CandidateBaseRef getTheMuPlus(const std::vector< reco::CandidateBaseRef > &)
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
edm::InputTag srcSelectedMuons_