CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFRecoTauDiscriminationAgainstMuon2.cc
Go to the documentation of this file.
1 
15 
17 
26 
27 #include <vector>
28 #include <string>
29 #include <iostream>
30 
32 {
34  public:
37  moduleLabel_(cfg.getParameter<std::string>("@module_label"))
38  {
39  std::string discriminatorOption_string = cfg.getParameter<std::string>("discriminatorOption");
40  if ( discriminatorOption_string == "loose" ) discriminatorOption_ = kLoose;
41  else if ( discriminatorOption_string == "medium" ) discriminatorOption_ = kMedium;
42  else if ( discriminatorOption_string == "tight" ) discriminatorOption_ = kTight;
43  else if ( discriminatorOption_string == "custom" ) discriminatorOption_ = kCustom;
45  << " Invalid Configuration parameter 'discriminatorOption' = " << discriminatorOption_string << " !!\n";
46  hop_ = cfg.getParameter<double>("HoPMin");
47  maxNumberOfMatches_ = cfg.exists("maxNumberOfMatches") ? cfg.getParameter<int>("maxNumberOfMatches"): 0;
48  doCaloMuonVeto_ = cfg.exists("doCaloMuonVeto") ? cfg.getParameter<bool>("doCaloMuonVeto"): false;
49  maxNumberOfHitsLast2Stations_ = cfg.exists("maxNumberOfHitsLast2Stations") ? cfg.getParameter<int>("maxNumberOfHitsLast2Stations"): 0;
50  if ( cfg.exists("srcMuons") ) {
51  srcMuons_ = cfg.getParameter<edm::InputTag>("srcMuons");
52  dRmuonMatch_ = cfg.getParameter<double>("dRmuonMatch");
53  }
54  verbosity_ = cfg.exists("verbosity") ? cfg.getParameter<int>("verbosity") : 0;
55  }
57 
58  void beginEvent(const edm::Event&, const edm::EventSetup&);
59 
60  double discriminate(const reco::PFTauRef&);
61 
62  private:
63  std::string moduleLabel_;
65  double hop_;
71  double dRmuonMatch_;
73 };
74 
76 {
77  if ( srcMuons_.label() != "" ) {
79  }
80 }
81 
82 namespace
83 {
84  void countHits(const reco::Muon& muon, std::vector<int>& numHitsDT, std::vector<int>& numHitsCSC, std::vector<int>& numHitsRPC)
85  {
86  if ( muon.outerTrack().isNonnull() ) {
87  const reco::HitPattern& muonHitPattern = muon.outerTrack()->hitPattern();
88  for ( int iHit = 0; iHit < muonHitPattern.numberOfHits(); ++iHit ) {
89  uint32_t hit = muonHitPattern.getHitPattern(iHit);
90  if ( hit == 0 ) break;
91  if ( muonHitPattern.muonHitFilter(hit) && (muonHitPattern.getHitType(hit) == TrackingRecHit::valid || muonHitPattern.getHitType(hit) == TrackingRecHit::bad) ) {
92  int muonStation = muonHitPattern.getMuonStation(hit) - 1; // CV: map into range 0..3
93  if ( muonStation >= 0 && muonStation < 4 ) {
94  if ( muonHitPattern.muonDTHitFilter(hit) ) ++numHitsDT[muonStation];
95  else if ( muonHitPattern.muonCSCHitFilter(hit) ) ++numHitsCSC[muonStation];
96  else if ( muonHitPattern.muonRPCHitFilter(hit) ) ++numHitsRPC[muonStation];
97  }
98  }
99  }
100  }
101  }
102 
103  std::string format_vint(const std::vector<int>& vi)
104  {
105  std::ostringstream os;
106  os << "{ ";
107  unsigned numEntries = vi.size();
108  for ( unsigned iEntry = 0; iEntry < numEntries; ++iEntry ) {
109  os << vi[iEntry];
110  if ( iEntry < (numEntries - 1) ) os << ", ";
111  }
112  os << " }";
113  return os.str();
114  }
115 }
116 
118 {
119  if ( verbosity_ ) {
120  std::cout << "<PFRecoTauDiscriminationAgainstMuon2::discriminate>:" << std::endl;
121  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
122  std::cout << "tau #" << pfTau.key() << ": Pt = " << pfTau->pt() << ", eta = " << pfTau->eta() << ", phi = " << pfTau->phi() << std::endl;
123  }
124 
125  int numMatches = 0;
126 
127  std::vector<int> numHitsDT(4);
128  std::vector<int> numHitsCSC(4);
129  std::vector<int> numHitsRPC(4);
130  for ( int iStation = 0; iStation < 4; ++iStation ) {
131  numHitsDT[iStation] = 0;
132  numHitsCSC[iStation] = 0;
133  numHitsRPC[iStation] = 0;
134  }
135 
136  const reco::PFCandidateRef& pfLeadChargedHadron = pfTau->leadPFChargedHadrCand();
137  if ( pfLeadChargedHadron.isNonnull() ) {
138  reco::MuonRef muonRef = pfLeadChargedHadron->muonRef();
139  if ( muonRef.isNonnull() ) {
140  if ( verbosity_ ) std::cout << " has muonRef." << std::endl;
141  numMatches = muonRef->numberOfMatches(reco::Muon::NoArbitration);
142  countHits(*muonRef, numHitsDT, numHitsCSC, numHitsRPC);
143  }
144  }
145 
146  if ( srcMuons_.label() != "" ) {
147  size_t numMuons = muons_->size();
148  for ( size_t idxMuon = 0; idxMuon < numMuons; ++idxMuon ) {
149  reco::MuonRef muon(muons_, idxMuon);
150  if ( verbosity_ ) std::cout << "muon #" << muon.key() << ": Pt = " << muon->pt() << ", eta = " << muon->eta() << ", phi = " << muon->phi() << std::endl;
151  if ( pfLeadChargedHadron.isNonnull() && pfLeadChargedHadron->muonRef().isNonnull() && muon == pfLeadChargedHadron->muonRef() ) {
152  if ( verbosity_ ) std::cout << " matches muonRef of tau --> skipping it." << std::endl;
153  continue;
154  }
155  double dR = deltaR(muon->p4(), pfTau->p4());
156  if ( dR < dRmuonMatch_ ) {
157  if ( verbosity_ ) std::cout << " overlaps with tau, dR = " << dR << std::endl;
158  numMatches += muon->numberOfMatches(reco::Muon::NoArbitration);
159  countHits(*muon, numHitsDT, numHitsCSC, numHitsRPC);
160  }
161  }
162  }
163 
164  if ( verbosity_ ) {
165  std::cout << "numMatches = " << numMatches << std::endl;
166  std::cout << "numHitsDT = " << format_vint(numHitsDT) << std::endl;
167  std::cout << "numHitsCSC = " << format_vint(numHitsCSC) << std::endl;
168  std::cout << "numHitsRPC = " << format_vint(numHitsRPC) << std::endl;
169  }
170 
171  int numLast2StationsWithHits = 0;
172  for ( int iStation = 2; iStation < 4; ++iStation ) {
173  if ( numHitsDT[iStation] > 0 ) ++numLast2StationsWithHits;
174  if ( numHitsCSC[iStation] > 0 ) ++numLast2StationsWithHits;
175  if ( numHitsRPC[iStation] > 0 ) ++numLast2StationsWithHits;
176  }
177 
178 
179  bool passesCaloMuonVeto = true;
180  if ( pfLeadChargedHadron.isNonnull() ) {
181  double energyECALplusHCAL = pfLeadChargedHadron->ecalEnergy() + pfLeadChargedHadron->hcalEnergy();
182  if ( verbosity_ ) {
183  if ( pfLeadChargedHadron->trackRef().isNonnull() ) std::cout << "decayMode = " << pfTau->decayMode() << ", energy(ECAL+HCAL) = " << energyECALplusHCAL << ", leadPFChargedHadronP = " << pfLeadChargedHadron->trackRef()->p() << std::endl;
184  else if ( pfLeadChargedHadron->gsfTrackRef().isNonnull() )
185 std::cout << "decayMode = " << pfTau->decayMode() << ", energy(ECAL+HCAL) = " << energyECALplusHCAL << ", leadPFChargedHadronP = " << pfLeadChargedHadron->gsfTrackRef()->p() << std::endl;
186 
187  }
188  const reco::Track* leadTrack = 0;
189  if ( pfLeadChargedHadron->trackRef().isNonnull() ) leadTrack = pfLeadChargedHadron->trackRef().get();
190  else if ( pfLeadChargedHadron->gsfTrackRef().isNonnull() ) leadTrack = pfLeadChargedHadron->gsfTrackRef().get();
191  if ( pfTau->decayMode() == 0 && leadTrack && energyECALplusHCAL < (hop_*leadTrack->p()) ) passesCaloMuonVeto = false;
192  }
193 
194  double discriminatorValue = 0.;
195  if ( discriminatorOption_ == kLoose && numMatches <= maxNumberOfMatches_ ) discriminatorValue = 1.;
196  else if ( discriminatorOption_ == kMedium && numMatches <= maxNumberOfMatches_ && numLast2StationsWithHits <= maxNumberOfHitsLast2Stations_ ) discriminatorValue = 1.;
197  else if ( discriminatorOption_ == kTight && numMatches <= maxNumberOfMatches_ && numLast2StationsWithHits <= maxNumberOfHitsLast2Stations_ && passesCaloMuonVeto ) discriminatorValue = 1.;
198  else if ( discriminatorOption_ == kCustom ) {
199  bool pass = true;
200  if ( maxNumberOfMatches_ >= 0 && numMatches > maxNumberOfMatches_ ) pass = false;
201  if ( maxNumberOfHitsLast2Stations_ >= 0 && numLast2StationsWithHits > maxNumberOfHitsLast2Stations_ ) pass = false;
202  if ( doCaloMuonVeto_ && !passesCaloMuonVeto ) pass = false;
203  discriminatorValue = pass ? 1.: 0.;
204  }
205  if ( verbosity_ ) std::cout << "--> returning discriminatorValue = " << discriminatorValue << std::endl;
206 
207  return discriminatorValue;
208 }
209 
double p() const
momentum vector magnitude
Definition: TrackBase.h:129
T getParameter(std::string const &) const
static bool muonDTHitFilter(uint32_t pattern)
Definition: HitPattern.h:444
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
static uint32_t getHitType(uint32_t pattern)
Definition: HitPattern.h:501
static uint32_t getMuonStation(uint32_t pattern)
Muon station (1-4). Only valid for muon patterns, of course.
Definition: HitPattern.h:506
static bool muonHitFilter(uint32_t pattern)
Definition: HitPattern.h:472
tuple numMuons
Definition: patZpeak.py:40
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
Long64_t numEntries(TFile *hdl, std::string const &trname)
Definition: CollUtil.cc:50
int numberOfHits() const
Definition: HitPattern.cc:213
static bool muonRPCHitFilter(uint32_t pattern)
Definition: HitPattern.h:458
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:52
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
key_type key() const
Accessor for product key.
Definition: Ref.h:266
std::string const & label() const
Definition: InputTag.h:25
static bool muonCSCHitFilter(uint32_t pattern)
Definition: HitPattern.h:451
tuple cout
Definition: gather_cfg.py:121
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:242
uint32_t getHitPattern(int position) const
Definition: HitPattern.cc:144
void beginEvent(const edm::Event &, const edm::EventSetup &)
PFRecoTauDiscriminationAgainstMuon2(const edm::ParameterSet &cfg)