CMS 3D CMS Logo

PATTauDiscriminationByMVAIsolationRun2.cc
Go to the documentation of this file.
1 
2 /*
3  * \class PATTauDiscriminationByMVAIsolationRun2
4  *
5  * MVA based discriminator against jet -> tau fakes
6  *
7  * Adopted from RecoTauTag/RecoTau/plugins/PFRecoTauDiscriminationByMVAIsolationRun2.cc
8  * to enable computation of MVA isolation on MiniAOD
9  *
10  * \author Alexander Nehrkorn, RWTH Aachen
11  */
12 
13 // todo 1: remove leadingTrackChi2 as input variable from:
14 // - here
15 // - TauPFEssential
16 // - PFRecoTauDiscriminationByMVAIsolationRun2
17 // - Training of BDT
18 
20 
26 
28 
31 
37 
41 
42 #include <TFile.h>
43 
44 #include <iostream>
45 
46 using namespace pat;
47 
48 namespace {
49  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName,
50  const std::string& mvaName,
51  std::vector<TFile*>& inputFilesToDelete) {
52  if (inputFileName.location() == edm::FileInPath::Unknown)
53  throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
54  << " Failed to find File = " << inputFileName << " !!\n";
55  TFile* inputFile = new TFile(inputFileName.fullPath().data());
56 
57  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
58  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
59  if (!mva)
60  throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
61  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data()
62  << " !!\n";
63 
64  inputFilesToDelete.push_back(inputFile);
65 
66  return mva;
67  }
68 
69  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName) {
72  return mva.product();
73  }
74 } // namespace
75 
76 namespace reco {
77  namespace tau {
78 
80  public:
83  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
84  mvaReader_(nullptr),
85  mvaInput_(nullptr) {
86  mvaName_ = cfg.getParameter<std::string>("mvaName");
87  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
88  if (!loadMVAfromDB_) {
89  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
90  }
91  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
92  if (mvaOpt_string == "oldDMwoLT")
93  mvaOpt_ = kOldDMwoLT;
94  else if (mvaOpt_string == "oldDMwLT")
95  mvaOpt_ = kOldDMwLT;
96  else if (mvaOpt_string == "newDMwoLT")
97  mvaOpt_ = kNewDMwoLT;
98  else if (mvaOpt_string == "newDMwLT")
99  mvaOpt_ = kNewDMwLT;
100  else if (mvaOpt_string == "DBoldDMwLT")
101  mvaOpt_ = kDBoldDMwLT;
102  else if (mvaOpt_string == "DBnewDMwLT")
103  mvaOpt_ = kDBnewDMwLT;
104  else if (mvaOpt_string == "PWoldDMwLT")
105  mvaOpt_ = kPWoldDMwLT;
106  else if (mvaOpt_string == "PWnewDMwLT")
107  mvaOpt_ = kPWnewDMwLT;
108  else if (mvaOpt_string == "DBoldDMwLTwGJ")
109  mvaOpt_ = kDBoldDMwLTwGJ;
110  else if (mvaOpt_string == "DBnewDMwLTwGJ")
111  mvaOpt_ = kDBnewDMwLTwGJ;
112  else
113  throw cms::Exception("PATTauDiscriminationByMVAIsolationRun2")
114  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
115 
116  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
117  mvaInput_ = new float[6];
118  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
119  mvaInput_ = new float[12];
120  else if (mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT || mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ||
121  mvaOpt_ == kDBoldDMwLTwGJ || mvaOpt_ == kDBnewDMwLTwGJ)
122  mvaInput_ = new float[23];
123  else
124  assert(0);
125 
126  chargedIsoPtSums_ = cfg.getParameter<std::string>("srcChargedIsoPtSum");
127  neutralIsoPtSums_ = cfg.getParameter<std::string>("srcNeutralIsoPtSum");
128  puCorrPtSums_ = cfg.getParameter<std::string>("srcPUcorrPtSum");
129  photonPtSumOutsideSignalCone_ = cfg.getParameter<std::string>("srcPhotonPtSumOutsideSignalCone");
130  footprintCorrection_ = cfg.getParameter<std::string>("srcFootprintCorrection");
131 
132  verbosity_ = cfg.getParameter<int>("verbosity");
133  }
134 
135  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
136 
137  reco::SingleTauDiscriminatorContainer discriminate(const TauRef&) const override;
138 
140  if (!loadMVAfromDB_)
141  delete mvaReader_;
142  delete[] mvaInput_;
143  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
144  delete (*it);
145  }
146  }
147 
148  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
149 
150  private:
152 
157  int mvaOpt_;
158  float* mvaInput_;
159 
165 
167  std::vector<TFile*> inputFilesToDelete_;
168 
170  };
171 
172  void PATTauDiscriminationByMVAIsolationRun2::beginEvent(const edm::Event& evt, const edm::EventSetup& es) {
173  if (!mvaReader_) {
174  if (loadMVAfromDB_) {
175  mvaReader_ = loadMVAfromDB(es, mvaName_);
176  } else {
177  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
178  }
179  }
180 
181  evt.getByToken(Tau_token, taus_);
182  }
183 
184  reco::SingleTauDiscriminatorContainer PATTauDiscriminationByMVAIsolationRun2::discriminate(const TauRef& tau) const {
185  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
187  result.rawValues = {-1.};
188 
189  // CV: computation of MVA value requires presence of leading charged hadron
190  if (tau->leadChargedHadrCand().isNull()) {
191  result.rawValues.at(0) = 0.;
192  return result;
193  }
194 
195  if (reco::tau::fillIsoMVARun2Inputs(mvaInput_,
196  *tau,
197  mvaOpt_,
198  chargedIsoPtSums_,
199  neutralIsoPtSums_,
200  puCorrPtSums_,
201  photonPtSumOutsideSignalCone_,
202  footprintCorrection_)) {
203  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
204  if (verbosity_) {
205  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "<PATTauDiscriminationByMVAIsolationRun2::discriminate>:";
206  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
207  edm::LogPrint("PATTauDiscByMVAIsolRun2")
208  << " isolation: charged = " << tau->tauID(chargedIsoPtSums_)
209  << ", neutral = " << tau->tauID(neutralIsoPtSums_) << ", PUcorr = " << tau->tauID(puCorrPtSums_);
210  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " decay mode = " << tau->decayMode();
211  edm::LogPrint("PATTauDiscByMVAIsolRun2")
212  << " impact parameter: distance = " << tau->dxy() << ", significance = " << tau->dxy_Sig();
213  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " has decay vertex = " << tau->hasSecondaryVertex() << ":"
214  << ", significance = " << tau->flightLengthSig();
215  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "--> mvaValue = " << mvaValue;
216  }
217  result.rawValues.at(0) = mvaValue;
218  }
219  return result;
220  }
221 
223  // patTauDiscriminationByMVAIsolationRun2
225 
226  desc.add<std::string>("mvaName");
227  desc.add<bool>("loadMVAfromDB");
228  desc.addOptional<edm::FileInPath>("inputFileName");
229  desc.add<std::string>("mvaOpt");
230 
231  desc.add<std::string>("srcChargedIsoPtSum");
232  desc.add<std::string>("srcNeutralIsoPtSum");
233  desc.add<std::string>("srcPUcorrPtSum");
234  desc.add<std::string>("srcPhotonPtSumOutsideSignalCone");
235  desc.add<std::string>("srcFootprintCorrection");
236  desc.add<int>("verbosity", 0);
237 
238  fillProducerDescriptions(desc); // inherited from the base
239 
240  descriptions.add("patTauDiscriminationByMVAIsolationRun2", desc);
241  }
242 
244 
245  } // namespace tau
246 } // namespace reco
ConfigurationDescriptions.h
taus_updatedMVAIds_cff.loadMVAfromDB
loadMVAfromDB
Definition: taus_updatedMVAIds_cff.py:16
reco::tau::PATTauDiscriminationByMVAIsolationRun2::footprintCorrection_
std::string footprintCorrection_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:164
PATTauDiscriminator.h
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
reco::tau::kOldDMwoLT
Definition: PFRecoTauClusterVariables.h:59
TauDiscriminationProducerBase.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaOpt_
int mvaOpt_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:157
reco::tau::PATTauDiscriminationByMVAIsolationRun2::loadMVAfromDB_
bool loadMVAfromDB_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:154
reco::tau::fillIsoMVARun2Inputs
bool fillIsoMVARun2Inputs(float *mvaInput, const pat::Tau &tau, int mvaOpt, const std::string &nameCharged, const std::string &nameNeutral, const std::string &namePu, const std::string &nameOutside, const std::string &nameFootprint)
Definition: PFRecoTauClusterVariables.cc:182
ESHandle.h
metsig::tau
Definition: SignAlgoResolutions.h:49
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaName_
std::string mvaName_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:153
GBRWrapperRcd.h
GBRForest
Definition: GBRForest.h:25
reco::tau::kDBoldDMwLT
Definition: PFRecoTauClusterVariables.h:63
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
reco::tau::PATTauDiscriminationByMVAIsolationRun2::taus_
edm::Handle< TauCollection > taus_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:166
cms::cuda::assert
assert(be >=bs)
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaReader_
const GBRForest * mvaReader_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:156
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:119
GBRForest.h
edm::Handle< TauCollection >
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
Tau.h
edm::Ref
Definition: AssociativeIterator.h:58
FileInPath.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::photonPtSumOutsideSignalCone_
std::string photonPtSumOutsideSignalCone_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:163
deltaR.h
PFRecoTauClusterVariables.h
edm::FileInPath
Definition: FileInPath.h:64
reco::tau::PATTauDiscriminationByMVAIsolationRun2::chargedIsoPtSums_
std::string chargedIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:160
reco::tau::kNewDMwLT
Definition: PFRecoTauClusterVariables.h:62
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
InefficientDoubleROC.inputFileName
inputFileName
Definition: InefficientDoubleROC.py:437
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::ESHandle< GBRForest >
taus_updatedMVAIds_cff.mvaName
mvaName
Definition: taus_updatedMVAIds_cff.py:17
TauDiscriminationProducerBase
Definition: TauDiscriminationProducerBase.h:55
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
ParameterSetDescription.h
reco::tau::kDBnewDMwLTwGJ
Definition: PFRecoTauClusterVariables.h:68
reco::tau::PATTauDiscriminationByMVAIsolationRun2::PATTauDiscriminationByMVAIsolationRun2
PATTauDiscriminationByMVAIsolationRun2(const edm::ParameterSet &cfg)
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:81
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::tau::PATTauDiscriminationByMVAIsolationRun2::~PATTauDiscriminationByMVAIsolationRun2
~PATTauDiscriminationByMVAIsolationRun2() override
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:139
reco::tau::kPWoldDMwLT
Definition: PFRecoTauClusterVariables.h:65
edm::ParameterSet
Definition: ParameterSet.h:36
Event.h
reco::tau::kDBnewDMwLT
Definition: PFRecoTauClusterVariables.h:64
reco::tau::PATTauDiscriminationByMVAIsolationRun2::moduleLabel_
std::string moduleLabel_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:151
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
reco::tau::kDBoldDMwLTwGJ
Definition: PFRecoTauClusterVariables.h:67
edm::LogPrint
Definition: MessageLogger.h:342
reco::tau::PATTauDiscriminationByMVAIsolationRun2
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:79
edm::EventSetup
Definition: EventSetup.h:57
pat
Definition: HeavyIon.h:7
get
#define get
reco::tau::PATTauDiscriminationByMVAIsolationRun2::verbosity_
int verbosity_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:169
reco::tau::PATTauDiscriminationByMVAIsolationRun2::neutralIsoPtSums_
std::string neutralIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:161
InputTag.h
looper.cfg
cfg
Definition: looper.py:297
reco::tau::PATTauDiscriminationByMVAIsolationRun2::inputFilesToDelete_
std::vector< TFile * > inputFilesToDelete_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:167
std
Definition: JetResolutionObject.h:76
edm::FileInPath::Unknown
Definition: FileInPath.h:66
reco::tau::PATTauDiscriminationByMVAIsolationRun2::puCorrPtSums_
std::string puCorrPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:162
Exception
Definition: hltDiff.cc:246
reco::tau::kNewDMwoLT
Definition: PFRecoTauClusterVariables.h:61
GBRWrapperRcd
Definition: GBRWrapperRcd.h:24
EventSetup.h
Exception.h
mps_fire.result
result
Definition: mps_fire.py:303
cms::Exception
Definition: Exception.h:70
Candidate.h
reco::tau::kOldDMwLT
Definition: PFRecoTauClusterVariables.h:60
ParameterSet.h
reco::tau::kPWnewDMwLT
Definition: PFRecoTauClusterVariables.h:66
reco::tau::PATTauDiscriminationByMVAIsolationRun2::inputFileName_
edm::FileInPath inputFileName_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:155
edm::Event
Definition: Event.h:73
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaInput_
float * mvaInput_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:158
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9