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 
40 
41 #include <TFile.h>
42 
43 #include <iostream>
44 
45 using namespace pat;
46 
47 namespace {
48  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName,
49  const std::string& mvaName,
50  std::vector<TFile*>& inputFilesToDelete) {
51  if (inputFileName.location() == edm::FileInPath::Unknown)
52  throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
53  << " Failed to find File = " << inputFileName << " !!\n";
54  TFile* inputFile = new TFile(inputFileName.fullPath().data());
55 
56  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
57  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
58  if (!mva)
59  throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
60  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data()
61  << " !!\n";
62 
63  inputFilesToDelete.push_back(inputFile);
64 
65  return mva;
66  }
67 } // namespace
68 
69 namespace reco {
70  namespace tau {
71 
73  public:
76  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
77  mvaReader_(nullptr),
78  mvaInput_(nullptr) {
79  mvaName_ = cfg.getParameter<std::string>("mvaName");
80  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
81  if (!loadMVAfromDB_) {
82  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
83  } else {
84  mvaToken_ = esConsumes(edm::ESInputTag{"", mvaName_});
85  }
86  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
87  if (mvaOpt_string == "oldDMwoLT")
88  mvaOpt_ = kOldDMwoLT;
89  else if (mvaOpt_string == "oldDMwLT")
90  mvaOpt_ = kOldDMwLT;
91  else if (mvaOpt_string == "newDMwoLT")
92  mvaOpt_ = kNewDMwoLT;
93  else if (mvaOpt_string == "newDMwLT")
94  mvaOpt_ = kNewDMwLT;
95  else if (mvaOpt_string == "DBoldDMwLT")
96  mvaOpt_ = kDBoldDMwLT;
97  else if (mvaOpt_string == "DBnewDMwLT")
98  mvaOpt_ = kDBnewDMwLT;
99  else if (mvaOpt_string == "PWoldDMwLT")
100  mvaOpt_ = kPWoldDMwLT;
101  else if (mvaOpt_string == "PWnewDMwLT")
102  mvaOpt_ = kPWnewDMwLT;
103  else if (mvaOpt_string == "DBoldDMwLTwGJ")
104  mvaOpt_ = kDBoldDMwLTwGJ;
105  else if (mvaOpt_string == "DBnewDMwLTwGJ")
106  mvaOpt_ = kDBnewDMwLTwGJ;
107  else if (mvaOpt_string == "DBnewDMwLTwGJPhase2")
108  mvaOpt_ = kDBnewDMwLTwGJPhase2;
109  else
110  throw cms::Exception("PATTauDiscriminationByMVAIsolationRun2")
111  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
112 
113  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
114  mvaInput_ = new float[6];
115  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
116  mvaInput_ = new float[12];
117  else if (mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT || mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ||
118  mvaOpt_ == kDBoldDMwLTwGJ || mvaOpt_ == kDBnewDMwLTwGJ)
119  mvaInput_ = new float[23];
120  else if (mvaOpt_ == kDBnewDMwLTwGJPhase2)
121  mvaInput_ = new float[30];
122  else
123  assert(0);
124 
125  chargedIsoPtSums_ = cfg.getParameter<std::string>("srcChargedIsoPtSum");
126  neutralIsoPtSums_ = cfg.getParameter<std::string>("srcNeutralIsoPtSum");
127  puCorrPtSums_ = cfg.getParameter<std::string>("srcPUcorrPtSum");
128  photonPtSumOutsideSignalCone_ = cfg.getParameter<std::string>("srcPhotonPtSumOutsideSignalCone");
129  footprintCorrection_ = cfg.getParameter<std::string>("srcFootprintCorrection");
130 
131  verbosity_ = cfg.getParameter<int>("verbosity");
132  }
133 
134  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
135 
136  reco::SingleTauDiscriminatorContainer discriminate(const TauRef&) const override;
137 
139  if (!loadMVAfromDB_)
140  delete mvaReader_;
141  delete[] mvaInput_;
142  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
143  delete (*it);
144  }
145  }
146 
147  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
148 
149  private:
151 
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_ = &es.getData(mvaToken_);
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
reco::tau::PATTauDiscriminationByMVAIsolationRun2::footprintCorrection_
std::string footprintCorrection_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:164
PATTauDiscriminator.h
edm::ESInputTag
Definition: ESInputTag.h:87
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:188
metsig::tau
Definition: SignAlgoResolutions.h:49
reco::tau::kDBnewDMwLT
Definition: PFRecoTauClusterVariables.h:64
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaName_
std::string mvaName_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:152
GBRWrapperRcd.h
GBRForest
Definition: GBRForest.h:24
edm::LogPrint
Log< level::Warning, true > LogPrint
Definition: MessageLogger.h:130
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::kOldDMwoLT
Definition: PFRecoTauClusterVariables.h:59
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaReader_
const GBRForest * mvaReader_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:156
reco::tau::kDBnewDMwLTwGJPhase2
Definition: PFRecoTauClusterVariables.h:69
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:132
GBRForest.h
edm::Handle< TauCollection >
Tau.h
edm::Ref
Definition: AssociativeIterator.h:58
reco::tau::PATTauDiscriminationByMVAIsolationRun2::photonPtSumOutsideSignalCone_
std::string photonPtSumOutsideSignalCone_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:163
PFRecoTauClusterVariables.h
edm::FileInPath
Definition: FileInPath.h:61
reco::tau::PATTauDiscriminationByMVAIsolationRun2::chargedIsoPtSums_
std::string chargedIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:160
reco::tau::kNewDMwoLT
Definition: PFRecoTauClusterVariables.h:61
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
reco::tau::kOldDMwLT
Definition: PFRecoTauClusterVariables.h:60
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaToken_
edm::ESGetToken< GBRForest, GBRWrapperRcd > mvaToken_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:153
reco::tau::kNewDMwLT
Definition: PFRecoTauClusterVariables.h:62
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
taus_updatedMVAIds_cff.mvaName
mvaName
Definition: taus_updatedMVAIds_cff.py:18
TauDiscriminationProducerBase
Definition: TauDiscriminationProducerBase.h:54
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
ParameterSetDescription.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::PATTauDiscriminationByMVAIsolationRun2
PATTauDiscriminationByMVAIsolationRun2(const edm::ParameterSet &cfg)
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:74
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
FileInPath.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::~PATTauDiscriminationByMVAIsolationRun2
~PATTauDiscriminationByMVAIsolationRun2() override
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:138
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
reco::tau::kPWnewDMwLT
Definition: PFRecoTauClusterVariables.h:66
reco::tau::kDBoldDMwLT
Definition: PFRecoTauClusterVariables.h:63
reco::tau::PATTauDiscriminationByMVAIsolationRun2::moduleLabel_
std::string moduleLabel_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:150
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
reco::tau::PATTauDiscriminationByMVAIsolationRun2
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:72
reco::tau::kDBnewDMwLTwGJ
Definition: PFRecoTauClusterVariables.h:68
edm::EventSetup
Definition: EventSetup.h:58
pat
Definition: HeavyIon.h:7
edm::ESGetToken< GBRForest, GBRWrapperRcd >
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::tau::PATTauDiscriminationByMVAIsolationRun2::verbosity_
int verbosity_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:169
reco::tau::PATTauDiscriminationByMVAIsolationRun2::neutralIsoPtSums_
std::string neutralIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:161
InputTag.h
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
looper.cfg
cfg
Definition: looper.py:296
reco::tau::PATTauDiscriminationByMVAIsolationRun2::inputFilesToDelete_
std::vector< TFile * > inputFilesToDelete_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:167
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
reco::tau::kDBoldDMwLTwGJ
Definition: PFRecoTauClusterVariables.h:67
std
Definition: JetResolutionObject.h:76
edm::FileInPath::Unknown
Definition: FileInPath.h:63
reco::tau::PATTauDiscriminationByMVAIsolationRun2::puCorrPtSums_
std::string puCorrPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:162
Exception
Definition: hltDiff.cc:245
EventSetup.h
Exception.h
mps_fire.result
result
Definition: mps_fire.py:311
cms::Exception
Definition: Exception.h:70
Candidate.h
ParameterSet.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::inputFileName_
edm::FileInPath inputFileName_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:155
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaInput_
float * mvaInput_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:158
reco::tau::kPWoldDMwLT
Definition: PFRecoTauClusterVariables.h:65
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9