test
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 
11 
13 
23 
24 #include <vector>
25 #include <string>
26 #include <iostream>
27 
29 {
31  public:
34  moduleLabel_(cfg.getParameter<std::string>("@module_label"))
35  {
36  std::string discriminatorOption_string = cfg.getParameter<std::string>("discriminatorOption");
37  if ( discriminatorOption_string == "loose" ) discriminatorOption_ = kLoose;
38  else if ( discriminatorOption_string == "medium" ) discriminatorOption_ = kMedium;
39  else if ( discriminatorOption_string == "tight" ) discriminatorOption_ = kTight;
40  else if ( discriminatorOption_string == "custom" ) discriminatorOption_ = kCustom;
42  << " Invalid Configuration parameter 'discriminatorOption' = " << discriminatorOption_string << " !!\n";
43  hop_ = cfg.getParameter<double>("HoPMin");
44  maxNumberOfMatches_ = cfg.exists("maxNumberOfMatches") ? cfg.getParameter<int>("maxNumberOfMatches"): 0;
45  doCaloMuonVeto_ = cfg.exists("doCaloMuonVeto") ? cfg.getParameter<bool>("doCaloMuonVeto"): false;
46  maxNumberOfHitsLast2Stations_ = cfg.exists("maxNumberOfHitsLast2Stations") ? cfg.getParameter<int>("maxNumberOfHitsLast2Stations"): 0;
47  if ( cfg.exists("srcMuons") ) {
48  srcMuons_ = cfg.getParameter<edm::InputTag>("srcMuons");
49  Muons_token = consumes<reco::MuonCollection>(srcMuons_);
50  dRmuonMatch_ = cfg.getParameter<double>("dRmuonMatch");
51  dRmuonMatchLimitedToJetArea_ = cfg.getParameter<bool>("dRmuonMatchLimitedToJetArea");
52  minPtMatchedMuon_ = cfg.getParameter<double>("minPtMatchedMuon");
53  }
54  typedef std::vector<int> vint;
55  maskMatchesDT_ = cfg.getParameter<vint>("maskMatchesDT");
56  maskMatchesCSC_ = cfg.getParameter<vint>("maskMatchesCSC");
57  maskMatchesRPC_ = cfg.getParameter<vint>("maskMatchesRPC");
58  maskHitsDT_ = cfg.getParameter<vint>("maskHitsDT");
59  maskHitsCSC_ = cfg.getParameter<vint>("maskHitsCSC");
60  maskHitsRPC_ = cfg.getParameter<vint>("maskHitsRPC");
61  numWarnings_ = 0;
62  maxWarnings_ = 3;
63  verbosity_ = cfg.exists("verbosity") ? cfg.getParameter<int>("verbosity") : 0;
64  }
66 
67  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
68 
69  double discriminate(const reco::PFTauRef&) override;
70 
71  private:
74  double hop_;
81  double dRmuonMatch_;
84  std::vector<int> maskMatchesDT_;
85  std::vector<int> maskMatchesCSC_;
86  std::vector<int> maskMatchesRPC_;
87  std::vector<int> maskHitsDT_;
88  std::vector<int> maskHitsCSC_;
89  std::vector<int> maskHitsRPC_;
90  mutable int numWarnings_;
93 };
94 
96 {
97  if ( srcMuons_.label() != "" ) {
99  }
100 }
101 
102 namespace
103 {
104  void countHits(const reco::Muon& muon, std::vector<int>& numHitsDT, std::vector<int>& numHitsCSC, std::vector<int>& numHitsRPC)
105  {
106  if ( muon.outerTrack().isNonnull() ) {
107  const reco::HitPattern& muonHitPattern = muon.outerTrack()->hitPattern();
108  for ( int iHit = 0; iHit < muonHitPattern.numberOfHits(); ++iHit ) {
109  uint32_t hit = muonHitPattern.getHitPattern(iHit);
110  if ( hit == 0 ) break;
111  if ( muonHitPattern.muonHitFilter(hit) && (muonHitPattern.getHitType(hit) == TrackingRecHit::valid || muonHitPattern.getHitType(hit) == TrackingRecHit::bad) ) {
112  int muonStation = muonHitPattern.getMuonStation(hit) - 1; // CV: map into range 0..3
113  if ( muonStation >= 0 && muonStation < 4 ) {
114  if ( muonHitPattern.muonDTHitFilter(hit) ) ++numHitsDT[muonStation];
115  else if ( muonHitPattern.muonCSCHitFilter(hit) ) ++numHitsCSC[muonStation];
116  else if ( muonHitPattern.muonRPCHitFilter(hit) ) ++numHitsRPC[muonStation];
117  }
118  }
119  }
120  }
121  }
122 
123  std::string format_vint(const std::vector<int>& vi)
124  {
125  std::ostringstream os;
126  os << "{ ";
127  unsigned numEntries = vi.size();
128  for ( unsigned iEntry = 0; iEntry < numEntries; ++iEntry ) {
129  os << vi[iEntry];
130  if ( iEntry < (numEntries - 1) ) os << ", ";
131  }
132  os << " }";
133  return os.str();
134  }
135 
136  void countMatches(const reco::Muon& muon, std::vector<int>& numMatchesDT, std::vector<int>& numMatchesCSC, std::vector<int>& numMatchesRPC)
137  {
138  const std::vector<reco::MuonChamberMatch>& muonSegments = muon.matches();
139  for ( std::vector<reco::MuonChamberMatch>::const_iterator muonSegment = muonSegments.begin();
140  muonSegment != muonSegments.end(); ++muonSegment ) {
141  if ( muonSegment->segmentMatches.empty() ) continue;
142  int muonDetector = muonSegment->detector();
143  int muonStation = muonSegment->station() - 1;
144  assert(muonStation >= 0 && muonStation <= 3);
145  if ( muonDetector == MuonSubdetId::DT ) ++numMatchesDT[muonStation];
146  else if ( muonDetector == MuonSubdetId::CSC ) ++numMatchesCSC[muonStation];
147  else if ( muonDetector == MuonSubdetId::RPC ) ++numMatchesRPC[muonStation];
148  }
149  }
150 }
151 
153 {
154  if ( verbosity_ ) {
155  edm::LogPrint("PFTauAgainstMuon2") << "<PFRecoTauDiscriminationAgainstMuon2::discriminate>:" ;
156  edm::LogPrint("PFTauAgainstMuon2") << " moduleLabel = " << moduleLabel_ ;
157  edm::LogPrint("PFTauAgainstMuon2") << "tau #" << pfTau.key() << ": Pt = " << pfTau->pt() << ", eta = " << pfTau->eta() << ", phi = " << pfTau->phi() ;
158  }
159 
160  std::vector<int> numMatchesDT(4);
161  std::vector<int> numMatchesCSC(4);
162  std::vector<int> numMatchesRPC(4);
163  std::vector<int> numHitsDT(4);
164  std::vector<int> numHitsCSC(4);
165  std::vector<int> numHitsRPC(4);
166  for ( int iStation = 0; iStation < 4; ++iStation ) {
167  numMatchesDT[iStation] = 0;
168  numMatchesCSC[iStation] = 0;
169  numMatchesRPC[iStation] = 0;
170  numHitsDT[iStation] = 0;
171  numHitsCSC[iStation] = 0;
172  numHitsRPC[iStation] = 0;
173  }
174 
175  const reco::PFCandidatePtr& pfLeadChargedHadron = pfTau->leadPFChargedHadrCand();
176  if ( pfLeadChargedHadron.isNonnull() ) {
177  reco::MuonRef muonRef = pfLeadChargedHadron->muonRef();
178  if ( muonRef.isNonnull() ) {
179  if ( verbosity_ ) edm::LogPrint("PFTauAgainstMuon2") << " has muonRef." ;
180  countMatches(*muonRef, numMatchesDT, numMatchesCSC, numMatchesRPC);
181  countHits(*muonRef, numHitsDT, numHitsCSC, numHitsRPC);
182  }
183  }
184 
185  if ( srcMuons_.label() != "" ) {
186  size_t numMuons = muons_->size();
187  for ( size_t idxMuon = 0; idxMuon < numMuons; ++idxMuon ) {
188  reco::MuonRef muon(muons_, idxMuon);
189  if ( verbosity_ ) edm::LogPrint("PFTauAgainstMuon2") << "muon #" << muon.key() << ": Pt = " << muon->pt() << ", eta = " << muon->eta() << ", phi = " << muon->phi() ;
190  if ( !(muon->pt() > minPtMatchedMuon_) ) {
191  if ( verbosity_ ){ edm::LogPrint("PFTauAgainstMuon2") << " fails Pt cut --> skipping it." ;}
192  continue;
193  }
194  if ( pfLeadChargedHadron.isNonnull() && pfLeadChargedHadron->muonRef().isNonnull() && muon == pfLeadChargedHadron->muonRef() ) {
195  if ( verbosity_ ){ edm::LogPrint("PFTauAgainstMuon2") << " matches muonRef of tau --> skipping it." ;}
196  continue;
197  }
198  double dR = deltaR(muon->p4(), pfTau->p4());
199  double dRmatch = dRmuonMatch_;
201  double jetArea = 0.;
202  if ( pfTau->jetRef().isNonnull() ) jetArea = pfTau->jetRef()->jetArea();
203  if ( jetArea > 0. ) {
204  dRmatch = TMath::Min(dRmatch, TMath::Sqrt(jetArea/TMath::Pi()));
205  } else {
206  if ( numWarnings_ < maxWarnings_ ) {
207  edm::LogWarning("PFRecoTauDiscriminationAgainstMuon2::discriminate")
208  << "Jet associated to Tau: Pt = " << pfTau->pt() << ", eta = " << pfTau->eta() << ", phi = " << pfTau->phi() << " has area = " << jetArea << " !!" ;
209  ++numWarnings_;
210  }
211  dRmatch = 0.1;
212  }
213  }
214  if ( dR < dRmatch ) {
215  if ( verbosity_ ) edm::LogPrint("PFTauAgainstMuon2") << " overlaps with tau, dR = " << dR ;
216  countMatches(*muon, numMatchesDT, numMatchesCSC, numMatchesRPC);
217  countHits(*muon, numHitsDT, numHitsCSC, numHitsRPC);
218  }
219  }
220  }
221 
222  int numStationsWithMatches = 0;
223  for ( int iStation = 0; iStation < 4; ++iStation ) {
224  if ( numMatchesDT[iStation] > 0 && !maskMatchesDT_[iStation] ) ++numStationsWithMatches;
225  if ( numMatchesCSC[iStation] > 0 && !maskMatchesCSC_[iStation] ) ++numStationsWithMatches;
226  if ( numMatchesRPC[iStation] > 0 && !maskMatchesRPC_[iStation] ) ++numStationsWithMatches;
227  }
228 
229  int numLast2StationsWithHits = 0;
230  for ( int iStation = 2; iStation < 4; ++iStation ) {
231  if ( numHitsDT[iStation] > 0 && !maskHitsDT_[iStation] ) ++numLast2StationsWithHits;
232  if ( numHitsCSC[iStation] > 0 && !maskHitsCSC_[iStation] ) ++numLast2StationsWithHits;
233  if ( numHitsRPC[iStation] > 0 && !maskHitsRPC_[iStation] ) ++numLast2StationsWithHits;
234  }
235 
236  if ( verbosity_ ) {
237  edm::LogPrint("PFTauAgainstMuon2") << "numMatchesDT = " << format_vint(numMatchesDT) ;
238  edm::LogPrint("PFTauAgainstMuon2") << "numMatchesCSC = " << format_vint(numMatchesCSC) ;
239  edm::LogPrint("PFTauAgainstMuon2") << "numMatchesRPC = " << format_vint(numMatchesRPC) ;
240  edm::LogPrint("PFTauAgainstMuon2") << " --> numStationsWithMatches = " << numStationsWithMatches ;
241  edm::LogPrint("PFTauAgainstMuon2") << "numHitsDT = " << format_vint(numHitsDT) ;
242  edm::LogPrint("PFTauAgainstMuon2") << "numHitsCSC = " << format_vint(numHitsCSC) ;
243  edm::LogPrint("PFTauAgainstMuon2") << "numHitsRPC = " << format_vint(numHitsRPC) ;
244  edm::LogPrint("PFTauAgainstMuon2") << " --> numLast2StationsWithHits = " << numLast2StationsWithHits ;
245  }
246 
247  bool passesCaloMuonVeto = true;
248  if ( pfLeadChargedHadron.isNonnull() ) {
249  double energyECALplusHCAL = pfLeadChargedHadron->ecalEnergy() + pfLeadChargedHadron->hcalEnergy();
250  if ( verbosity_ ) {
251  if ( pfLeadChargedHadron->trackRef().isNonnull() ) {
252  edm::LogPrint("PFTauAgainstMuon2") << "decayMode = " << pfTau->decayMode() << ", energy(ECAL+HCAL) = " << energyECALplusHCAL << ", leadPFChargedHadronP = " << pfLeadChargedHadron->trackRef()->p() ;
253  } else if ( pfLeadChargedHadron->gsfTrackRef().isNonnull() ) {
254  edm::LogPrint("PFTauAgainstMuon2") << "decayMode = " << pfTau->decayMode() << ", energy(ECAL+HCAL) = " << energyECALplusHCAL << ", leadPFChargedHadronP = " << pfLeadChargedHadron->gsfTrackRef()->p() ;
255  }
256  }
257  const reco::Track* leadTrack = 0;
258  if ( pfLeadChargedHadron->trackRef().isNonnull() ) leadTrack = pfLeadChargedHadron->trackRef().get();
259  else if ( pfLeadChargedHadron->gsfTrackRef().isNonnull() ) leadTrack = pfLeadChargedHadron->gsfTrackRef().get();
260  if ( pfTau->decayMode() == 0 && leadTrack && energyECALplusHCAL < (hop_*leadTrack->p()) ) passesCaloMuonVeto = false;
261  }
262 
263  double discriminatorValue = 0.;
264  if ( discriminatorOption_ == kLoose && numStationsWithMatches <= maxNumberOfMatches_ ) discriminatorValue = 1.;
265  else if ( discriminatorOption_ == kMedium && numStationsWithMatches <= maxNumberOfMatches_ && numLast2StationsWithHits <= maxNumberOfHitsLast2Stations_ ) discriminatorValue = 1.;
266  else if ( discriminatorOption_ == kTight && numStationsWithMatches <= maxNumberOfMatches_ && numLast2StationsWithHits <= maxNumberOfHitsLast2Stations_ && passesCaloMuonVeto ) discriminatorValue = 1.;
267  else if ( discriminatorOption_ == kCustom ) {
268  bool pass = true;
269  if ( maxNumberOfMatches_ >= 0 && numStationsWithMatches > maxNumberOfMatches_ ) pass = false;
270  if ( maxNumberOfHitsLast2Stations_ >= 0 && numLast2StationsWithHits > maxNumberOfHitsLast2Stations_ ) pass = false;
271  if ( doCaloMuonVeto_ && !passesCaloMuonVeto ) pass = false;
272  discriminatorValue = pass ? 1.: 0.;
273  }
274  if ( verbosity_ ) edm::LogPrint("PFTauAgainstMuon2") << "--> returning discriminatorValue = " << discriminatorValue ;
275 
276  return discriminatorValue;
277 }
278 
double p() const
momentum vector magnitude
Definition: TrackBase.h:127
const double Pi
T getParameter(std::string const &) const
double discriminate(const reco::PFTauRef &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
static bool muonDTHitFilter(uint32_t pattern)
Definition: HitPattern.h:479
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void beginEvent(const edm::Event &, const edm::EventSetup &) override
bool exists(std::string const &parameterName) const
checks if a parameter exists
static uint32_t getHitType(uint32_t pattern)
Definition: HitPattern.h:536
static uint32_t getMuonStation(uint32_t pattern)
Muon station (1-4). Only valid for muon patterns, of course.
Definition: HitPattern.h:541
T Min(T a, T b)
Definition: MathUtil.h:39
double vint[400]
static bool muonHitFilter(uint32_t pattern)
Definition: HitPattern.h:507
tuple numMuons
Definition: patZpeak.py:40
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
static const int CSC
Definition: MuonSubdetId.h:13
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
Long64_t numEntries(TFile *hdl, std::string const &trname)
Definition: CollUtil.cc:50
int numberOfHits() const
Definition: HitPattern.cc:211
static bool muonRPCHitFilter(uint32_t pattern)
Definition: HitPattern.h:493
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:51
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::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:140
std::string const & label() const
Definition: InputTag.h:42
static const int RPC
Definition: MuonSubdetId.h:14
static bool muonCSCHitFilter(uint32_t pattern)
Definition: HitPattern.h:486
static const int DT
Definition: MuonSubdetId.h:12
uint32_t getHitPattern(int position) const
Definition: HitPattern.cc:142
PFRecoTauDiscriminationAgainstMuon2(const edm::ParameterSet &cfg)
edm::EDGetTokenT< reco::MuonCollection > Muons_token