CMS 3D CMS Logo

PFRecoTauDiscriminationAgainstMuonMVA.cc
Go to the documentation of this file.
1 
11 
17 
19 
22 
29 
33 
34 #include <TMath.h>
35 #include <TFile.h>
36 
37 #include <iostream>
38 
39 using namespace reco;
40 
41 namespace {
42  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName,
43  const std::string& mvaName,
44  std::vector<TFile*>& inputFilesToDelete) {
45  if (inputFileName.location() == edm::FileInPath::Unknown)
46  throw cms::Exception("PFRecoTauDiscriminationAgainstMuonMVA::loadMVA")
47  << " Failed to find File = " << inputFileName << " !!\n";
48  TFile* inputFile = new TFile(inputFileName.fullPath().data());
49 
50  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
51  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
52  if (!mva)
53  throw cms::Exception("PFRecoTauDiscriminationAgainstMuonMVA::loadMVA")
54  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data()
55  << " !!\n";
56 
57  inputFilesToDelete.push_back(inputFile);
58 
59  return mva;
60  }
61 
62  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName) {
65  return mva.product();
66  }
67 
69  public:
72  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
73  mvaReader_(nullptr),
74  mvaInput_(nullptr) {
75  mvaName_ = cfg.getParameter<std::string>("mvaName");
76  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
77  if (!loadMVAfromDB_) {
78  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
79  }
80  mvaInput_ = new float[11];
81 
82  srcMuons_ = cfg.getParameter<edm::InputTag>("srcMuons");
83  Muons_token = consumes<reco::MuonCollection>(srcMuons_);
84  dRmuonMatch_ = cfg.getParameter<double>("dRmuonMatch");
85 
86  verbosity_ = cfg.getParameter<int>("verbosity");
87  }
88 
89  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
90 
91  reco::SingleTauDiscriminatorContainer discriminate(const PFTauRef&) const override;
92 
94  if (!loadMVAfromDB_)
95  delete mvaReader_;
96  delete[] mvaInput_;
97  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
98  delete (*it);
99  }
100  }
101 
102  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
103 
104  private:
105  std::string moduleLabel_;
106 
107  std::string mvaName_;
108  bool loadMVAfromDB_;
109  edm::FileInPath inputFileName_;
110  const GBRForest* mvaReader_;
111  float* mvaInput_;
112 
113  edm::InputTag srcMuons_;
116  double dRmuonMatch_;
117 
119 
120  std::vector<TFile*> inputFilesToDelete_;
121 
122  int verbosity_;
123  };
124 
126  if (!mvaReader_) {
127  if (loadMVAfromDB_) {
128  mvaReader_ = loadMVAfromDB(es, mvaName_);
129  } else {
130  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
131  }
132  }
133 
134  evt.getByToken(Muons_token, muons_);
135 
136  evt.getByToken(Tau_token, taus_);
137  }
138 
139  namespace {
140  void countHits(const reco::Muon& muon,
141  std::vector<int>& numHitsDT,
142  std::vector<int>& numHitsCSC,
143  std::vector<int>& numHitsRPC) {
144  if (muon.outerTrack().isNonnull()) {
145  const reco::HitPattern& muonHitPattern = muon.outerTrack()->hitPattern();
146  for (int iHit = 0; iHit < muonHitPattern.numberOfAllHits(HitPattern::TRACK_HITS); ++iHit) {
147  uint32_t hit = muonHitPattern.getHitPattern(HitPattern::TRACK_HITS, iHit);
148  if (hit == 0)
149  break;
150  if (muonHitPattern.muonHitFilter(hit) && (muonHitPattern.getHitType(hit) == TrackingRecHit::valid ||
151  muonHitPattern.getHitType(hit) == TrackingRecHit::bad)) {
152  int muonStation = muonHitPattern.getMuonStation(hit) - 1; // CV: map into range 0..3
153  if (muonStation >= 0 && muonStation < 4) {
154  if (muonHitPattern.muonDTHitFilter(hit))
155  ++numHitsDT[muonStation];
156  else if (muonHitPattern.muonCSCHitFilter(hit))
157  ++numHitsCSC[muonStation];
158  else if (muonHitPattern.muonRPCHitFilter(hit))
159  ++numHitsRPC[muonStation];
160  }
161  }
162  }
163  }
164  }
165  } // namespace
166 
168  if (verbosity_) {
169  edm::LogPrint("PFTauAgainstMuonMVA") << "<PFRecoTauDiscriminationAgainstMuonMVA::discriminate>:";
170  edm::LogPrint("PFTauAgainstMuonMVA") << " moduleLabel = " << moduleLabel_;
171  }
172 
174  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to apply WP cuts
175  result.rawValues = {-1., 0.};
176 
177  // CV: computation of anti-muon MVA value requires presence of leading charged hadron
178  if (tau->leadPFChargedHadrCand().isNull())
179  return 0.;
180 
181  mvaInput_[0] = TMath::Abs(tau->eta());
182  double tauCaloEnECAL = 0.;
183  double tauCaloEnHCAL = 0.;
184  const std::vector<reco::PFCandidatePtr>& tauSignalPFCands = tau->signalPFCands();
185  for (std::vector<reco::PFCandidatePtr>::const_iterator tauSignalPFCand = tauSignalPFCands.begin();
186  tauSignalPFCand != tauSignalPFCands.end();
187  ++tauSignalPFCand) {
188  tauCaloEnECAL += (*tauSignalPFCand)->ecalEnergy();
189  tauCaloEnHCAL += (*tauSignalPFCand)->hcalEnergy();
190  }
191  mvaInput_[1] = TMath::Sqrt(TMath::Max(0., tauCaloEnECAL));
192  mvaInput_[2] = TMath::Sqrt(TMath::Max(0., tauCaloEnHCAL));
193  mvaInput_[3] = tau->leadPFChargedHadrCand()->pt() / TMath::Max(1., Double_t(tau->pt()));
194  mvaInput_[4] = TMath::Sqrt(TMath::Max(0., tau->leadPFChargedHadrCand()->ecalEnergy()));
195  mvaInput_[5] = TMath::Sqrt(TMath::Max(0., tau->leadPFChargedHadrCand()->hcalEnergy()));
196  int numMatches = 0;
197  std::vector<int> numHitsDT(4);
198  std::vector<int> numHitsCSC(4);
199  std::vector<int> numHitsRPC(4);
200  for (int iStation = 0; iStation < 4; ++iStation) {
201  numHitsDT[iStation] = 0;
202  numHitsCSC[iStation] = 0;
203  numHitsRPC[iStation] = 0;
204  }
205  if (tau->leadPFChargedHadrCand().isNonnull()) {
206  reco::MuonRef muonRef = tau->leadPFChargedHadrCand()->muonRef();
207  if (muonRef.isNonnull()) {
208  numMatches = muonRef->numberOfMatches(reco::Muon::NoArbitration);
209  countHits(*muonRef, numHitsDT, numHitsCSC, numHitsRPC);
210  }
211  }
212  size_t numMuons = muons_->size();
213  for (size_t idxMuon = 0; idxMuon < numMuons; ++idxMuon) {
214  reco::MuonRef muon(muons_, idxMuon);
215  if (tau->leadPFChargedHadrCand().isNonnull() && tau->leadPFChargedHadrCand()->muonRef().isNonnull() &&
216  muon == tau->leadPFChargedHadrCand()->muonRef()) {
217  continue;
218  }
219  double dR = deltaR(muon->p4(), tau->p4());
220  if (dR < dRmuonMatch_) {
221  numMatches += muon->numberOfMatches(reco::Muon::NoArbitration);
222  countHits(*muon, numHitsDT, numHitsCSC, numHitsRPC);
223  }
224  }
225  mvaInput_[6] = numMatches;
226  mvaInput_[7] = numHitsDT[0] + numHitsCSC[0] + numHitsRPC[0];
227  mvaInput_[8] = numHitsDT[1] + numHitsCSC[1] + numHitsRPC[1];
228  mvaInput_[9] = numHitsDT[2] + numHitsCSC[2] + numHitsRPC[2];
229  mvaInput_[10] = numHitsDT[3] + numHitsCSC[3] + numHitsRPC[3];
230 
231  result.rawValues.at(0) = mvaReader_->GetClassifier(mvaInput_);
232  if (verbosity_) {
233  edm::LogPrint("PFTauAgainstMuonMVA") << "mvaValue = " << result.rawValues.at(0);
234  }
235  return result.rawValues.at(0);
236  }
237 } // namespace
238 
240  // pfRecoTauDiscriminationAgainstMuonMVA
242  desc.add<double>("mvaMin", 0.0);
243  desc.add<std::string>("mvaName", "againstMuonMVA");
244  desc.add<edm::InputTag>("PFTauProducer", edm::InputTag("pfTauProducer"));
245  desc.add<int>("verbosity", 0);
246  desc.add<bool>("returnMVA", true);
247  desc.add<edm::FileInPath>("inputFileName", edm::FileInPath("RecoTauTag/RecoTau/data/emptyMVAinputFile"));
248  desc.add<bool>("loadMVAfromDB", true);
249  {
251  psd0.add<std::string>("BooleanOperator", "and");
252  {
254  psd1.add<double>("cut");
255  psd1.add<edm::InputTag>("Producer");
256  psd0.addOptional<edm::ParameterSetDescription>("leadTrack", psd1);
257  }
258  desc.add<edm::ParameterSetDescription>("Prediscriminants", psd0);
259  }
260  desc.add<double>("dRmuonMatch", 0.3);
261  desc.add<edm::InputTag>("srcMuons", edm::InputTag("muons"));
262  descriptions.add("pfRecoTauDiscriminationAgainstMuonMVA", desc);
263 }
264 
ConfigurationDescriptions.h
taus_updatedMVAIds_cff.loadMVAfromDB
loadMVAfromDB
Definition: taus_updatedMVAIds_cff.py:17
reco::HitPattern::getHitPattern
uint16_t getHitPattern(HitCategory category, int position) const
Definition: HitPattern.h:530
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
PFTauFwd.h
Muon.h
TauDiscriminationProducerBase.h
ESHandle.h
metsig::tau
Definition: SignAlgoResolutions.h:49
muon
Definition: MuonCocktails.h:17
reco::HitPattern::muonRPCHitFilter
static bool muonRPCHitFilter(uint16_t pattern)
Definition: HitPattern.h:640
edm::EDGetTokenT< reco::MuonCollection >
GBRWrapperRcd.h
GBRForest
Definition: GBRForest.h:25
edm::LogPrint
Log< level::Warning, true > LogPrint
Definition: MessageLogger.h:130
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
reco::HitPattern::muonHitFilter
static bool muonHitFilter(uint16_t pattern)
Definition: HitPattern.h:667
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
PFRecoTauDiscriminationAgainstMuonMVA
reco::HitPattern::muonCSCHitFilter
static bool muonCSCHitFilter(uint16_t pattern)
Definition: HitPattern.h:632
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:124
GBRForest.h
edm::Handle< reco::MuonCollection >
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
reco::HitPattern::getHitType
static uint32_t getHitType(uint16_t pattern)
Definition: HitPattern.h:725
reco::Muon
Definition: Muon.h:27
edm::Ref< PFTauCollection >
FileInPath.h
edm::FileInPath
Definition: FileInPath.h:64
reco::HitPattern
Definition: HitPattern.h:147
edm::EventSetup::get
T get() const
Definition: EventSetup.h:87
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Abs
T Abs(T a)
Definition: MathUtil.h:49
InefficientDoubleROC.inputFileName
inputFileName
Definition: InefficientDoubleROC.py:437
HLT_FULL_cff.muon
muon
Definition: HLT_FULL_cff.py:11710
MuonFwd.h
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::ESHandle< GBRForest >
TauDiscriminationProducerBase::discriminate
virtual TauDiscriminatorDataType discriminate(const TauRef &tau) const =0
taus_updatedMVAIds_cff.mvaName
mvaName
Definition: taus_updatedMVAIds_cff.py:18
TauDiscriminationProducerBase
Definition: TauDiscriminationProducerBase.h:55
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:535
ParameterSetDescription.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TrackingRecHit::bad
Definition: TrackingRecHit.h:49
reco::Muon::NoArbitration
Definition: Muon.h:188
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
edm::Ref::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
reco::HitPattern::getMuonStation
static uint16_t getMuonStation(uint16_t pattern)
Muon station (1-4). Only valid for muon patterns, of course. only for patterns from muon,...
Definition: HitPattern.h:732
Max
T Max(T a, T b)
Definition: MathUtil.h:44
edm::EventSetup
Definition: EventSetup.h:58
TrackingRecHit::valid
Definition: TrackingRecHit.h:46
TauDiscriminationProducerBase::beginEvent
virtual void beginEvent(const edm::Event &, const edm::EventSetup &)
Definition: TauDiscriminationProducerBase.h:74
InputTag.h
reco::get
T get(const Candidate &c)
Definition: component.h:60
reco::HitPattern::TRACK_HITS
Definition: HitPattern.h:155
looper.cfg
cfg
Definition: looper.py:297
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
reco::HitPattern::numberOfAllHits
int numberOfAllHits(HitCategory category) const
Definition: HitPattern.h:773
edm::FileInPath::Unknown
Definition: FileInPath.h:66
Exception
Definition: hltDiff.cc:245
PFTau.h
GBRWrapperRcd
Definition: GBRWrapperRcd.h:24
EventSetup.h
reco::tau::countHits
void countHits(const reco::Muon &muon, std::vector< int > &numHitsDT, std::vector< int > &numHitsCSC, std::vector< int > &numHitsRPC)
Definition: RecoTauMuonTools.cc:7
reco::deltaR
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
Exception.h
mps_fire.result
result
Definition: mps_fire.py:311
cms::Exception
Definition: Exception.h:70
Candidate.h
HGC3DClusterGenMatchSelector_cfi.dR
dR
Definition: HGC3DClusterGenMatchSelector_cfi.py:7
ParameterSet.h
edm::Event
Definition: Event.h:73
reco::HitPattern::muonDTHitFilter
static bool muonDTHitFilter(uint16_t pattern)
Definition: HitPattern.h:624
patZpeak.numMuons
numMuons
Definition: patZpeak.py:41
edm::InputTag
Definition: InputTag.h:15
hit
Definition: SiStripHitEffFromCalibTree.cc:88
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9