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 if (mvaOpt_string == "DBnewDMwLTwGJPhase2")
113  mvaOpt_ = kDBnewDMwLTwGJPhase2;
114  else
115  throw cms::Exception("PATTauDiscriminationByMVAIsolationRun2")
116  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
117 
118  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
119  mvaInput_ = new float[6];
120  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
121  mvaInput_ = new float[12];
122  else if (mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT || mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ||
123  mvaOpt_ == kDBoldDMwLTwGJ || mvaOpt_ == kDBnewDMwLTwGJ)
124  mvaInput_ = new float[23];
125  else if (mvaOpt_ == kDBnewDMwLTwGJPhase2)
126  mvaInput_ = new float[30];
127  else
128  assert(0);
129 
130  chargedIsoPtSums_ = cfg.getParameter<std::string>("srcChargedIsoPtSum");
131  neutralIsoPtSums_ = cfg.getParameter<std::string>("srcNeutralIsoPtSum");
132  puCorrPtSums_ = cfg.getParameter<std::string>("srcPUcorrPtSum");
133  photonPtSumOutsideSignalCone_ = cfg.getParameter<std::string>("srcPhotonPtSumOutsideSignalCone");
134  footprintCorrection_ = cfg.getParameter<std::string>("srcFootprintCorrection");
135 
136  verbosity_ = cfg.getParameter<int>("verbosity");
137  }
138 
139  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
140 
141  reco::SingleTauDiscriminatorContainer discriminate(const TauRef&) const override;
142 
144  if (!loadMVAfromDB_)
145  delete mvaReader_;
146  delete[] mvaInput_;
147  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
148  delete (*it);
149  }
150  }
151 
152  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
153 
154  private:
156 
161  int mvaOpt_;
162  float* mvaInput_;
163 
169 
171  std::vector<TFile*> inputFilesToDelete_;
172 
174  };
175 
176  void PATTauDiscriminationByMVAIsolationRun2::beginEvent(const edm::Event& evt, const edm::EventSetup& es) {
177  if (!mvaReader_) {
178  if (loadMVAfromDB_) {
179  mvaReader_ = loadMVAfromDB(es, mvaName_);
180  } else {
181  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
182  }
183  }
184 
185  evt.getByToken(Tau_token, taus_);
186  }
187 
188  reco::SingleTauDiscriminatorContainer PATTauDiscriminationByMVAIsolationRun2::discriminate(const TauRef& tau) const {
189  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
191  result.rawValues = {-1.};
192 
193  // CV: computation of MVA value requires presence of leading charged hadron
194  if (tau->leadChargedHadrCand().isNull()) {
195  result.rawValues.at(0) = 0.;
196  return result;
197  }
198 
199  if (reco::tau::fillIsoMVARun2Inputs(mvaInput_,
200  *tau,
201  mvaOpt_,
202  chargedIsoPtSums_,
203  neutralIsoPtSums_,
204  puCorrPtSums_,
205  photonPtSumOutsideSignalCone_,
206  footprintCorrection_)) {
207  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
208  if (verbosity_) {
209  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "<PATTauDiscriminationByMVAIsolationRun2::discriminate>:";
210  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
211  edm::LogPrint("PATTauDiscByMVAIsolRun2")
212  << " isolation: charged = " << tau->tauID(chargedIsoPtSums_)
213  << ", neutral = " << tau->tauID(neutralIsoPtSums_) << ", PUcorr = " << tau->tauID(puCorrPtSums_);
214  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " decay mode = " << tau->decayMode();
215  edm::LogPrint("PATTauDiscByMVAIsolRun2")
216  << " impact parameter: distance = " << tau->dxy() << ", significance = " << tau->dxy_Sig();
217  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " has decay vertex = " << tau->hasSecondaryVertex() << ":"
218  << ", significance = " << tau->flightLengthSig();
219  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "--> mvaValue = " << mvaValue;
220  }
221  result.rawValues.at(0) = mvaValue;
222  }
223  return result;
224  }
225 
227  // patTauDiscriminationByMVAIsolationRun2
229 
230  desc.add<std::string>("mvaName");
231  desc.add<bool>("loadMVAfromDB");
232  desc.addOptional<edm::FileInPath>("inputFileName");
233  desc.add<std::string>("mvaOpt");
234 
235  desc.add<std::string>("srcChargedIsoPtSum");
236  desc.add<std::string>("srcNeutralIsoPtSum");
237  desc.add<std::string>("srcPUcorrPtSum");
238  desc.add<std::string>("srcPhotonPtSumOutsideSignalCone");
239  desc.add<std::string>("srcFootprintCorrection");
240  desc.add<int>("verbosity", 0);
241 
242  fillProducerDescriptions(desc); // inherited from the base
243 
244  descriptions.add("patTauDiscriminationByMVAIsolationRun2", desc);
245  }
246 
248 
249  } // namespace tau
250 } // namespace reco
ConfigurationDescriptions.h
taus_updatedMVAIds_cff.loadMVAfromDB
loadMVAfromDB
Definition: taus_updatedMVAIds_cff.py:17
reco::tau::PATTauDiscriminationByMVAIsolationRun2::footprintCorrection_
std::string footprintCorrection_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:168
PATTauDiscriminator.h
reco::tau::kOldDMwoLT
Definition: PFRecoTauClusterVariables.h:59
TauDiscriminationProducerBase.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaOpt_
int mvaOpt_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:161
reco::tau::PATTauDiscriminationByMVAIsolationRun2::loadMVAfromDB_
bool loadMVAfromDB_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:158
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:157
GBRWrapperRcd.h
GBRForest
Definition: GBRForest.h:25
reco::tau::kDBoldDMwLT
Definition: PFRecoTauClusterVariables.h:63
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:170
cms::cuda::assert
assert(be >=bs)
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaReader_
const GBRForest * mvaReader_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:160
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:122
GBRForest.h
edm::Handle< TauCollection >
Tau.h
edm::Ref
Definition: AssociativeIterator.h:58
FileInPath.h
reco::tau::PATTauDiscriminationByMVAIsolationRun2::photonPtSumOutsideSignalCone_
std::string photonPtSumOutsideSignalCone_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:167
PFRecoTauClusterVariables.h
edm::FileInPath
Definition: FileInPath.h:64
reco::tau::PATTauDiscriminationByMVAIsolationRun2::chargedIsoPtSums_
std::string chargedIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:164
reco::tau::kNewDMwLT
Definition: PFRecoTauClusterVariables.h:62
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
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::kDBnewDMwLTwGJPhase2
Definition: PFRecoTauClusterVariables.h:69
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::ESHandle< GBRForest >
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:531
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:143
reco::tau::kPWoldDMwLT
Definition: PFRecoTauClusterVariables.h:65
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
reco::tau::kDBnewDMwLT
Definition: PFRecoTauClusterVariables.h:64
reco::tau::PATTauDiscriminationByMVAIsolationRun2::moduleLabel_
std::string moduleLabel_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:155
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
reco::tau::kDBoldDMwLTwGJ
Definition: PFRecoTauClusterVariables.h:67
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:173
reco::tau::PATTauDiscriminationByMVAIsolationRun2::neutralIsoPtSums_
std::string neutralIsoPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:165
InputTag.h
looper.cfg
cfg
Definition: looper.py:297
reco::tau::PATTauDiscriminationByMVAIsolationRun2::inputFilesToDelete_
std::vector< TFile * > inputFilesToDelete_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:171
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
edm::FileInPath::Unknown
Definition: FileInPath.h:66
reco::tau::PATTauDiscriminationByMVAIsolationRun2::puCorrPtSums_
std::string puCorrPtSums_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:166
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:311
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:159
edm::Event
Definition: Event.h:73
reco::tau::PATTauDiscriminationByMVAIsolationRun2::mvaInput_
float * mvaInput_
Definition: PATTauDiscriminationByMVAIsolationRun2.cc:162
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9