CMS 3D CMS Logo

PFRecoTauDiscriminationByMVAIsolation2.cc
Go to the documentation of this file.
1 
11 
17 
19 
22 
29 
32 
33 #include <TMath.h>
34 #include <TFile.h>
35 
36 #include <iostream>
37 
38 using namespace reco;
39 
40 namespace {
41  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName,
42  const std::string& mvaName,
43  std::vector<TFile*>& inputFilesToDelete) {
44  if (inputFileName.location() == edm::FileInPath::Unknown)
45  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
46  << " Failed to find File = " << inputFileName << " !!\n";
47  TFile* inputFile = new TFile(inputFileName.fullPath().data());
48 
49  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
50  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
51  if (!mva)
52  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
53  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data()
54  << " !!\n";
55 
56  inputFilesToDelete.push_back(inputFile);
57 
58  return mva;
59  }
60 } // namespace
61 
63 public:
66  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
67  mvaReader_(nullptr),
68  mvaInput_(nullptr) {
69  mvaName_ = cfg.getParameter<std::string>("mvaName");
70  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
71  if (!loadMVAfromDB_) {
72  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
73  } else {
74  mvaToken_ = esConsumes(edm::ESInputTag{"", mvaName_});
75  }
76  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
77  if (mvaOpt_string == "oldDMwoLT")
78  mvaOpt_ = kOldDMwoLT;
79  else if (mvaOpt_string == "oldDMwLT")
80  mvaOpt_ = kOldDMwLT;
81  else if (mvaOpt_string == "newDMwoLT")
82  mvaOpt_ = kNewDMwoLT;
83  else if (mvaOpt_string == "newDMwLT")
84  mvaOpt_ = kNewDMwLT;
85  else
86  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2")
87  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
88 
89  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
90  mvaInput_ = new float[6];
91  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
92  mvaInput_ = new float[12];
93  else
94  assert(0);
95 
96  tauTransverseImpactParameters_token_ =
97  consumes<PFTauTIPAssociationByRef>(cfg.getParameter<edm::InputTag>("srcTauTransverseImpactParameters"));
98 
99  basicTauDiscriminators_token_ =
100  consumes<reco::TauDiscriminatorContainer>(cfg.getParameter<edm::InputTag>("srcBasicTauDiscriminators"));
101  chargedIsoPtSum_index_ = cfg.getParameter<int>("srcChargedIsoPtSumIndex");
102  neutralIsoPtSum_index_ = cfg.getParameter<int>("srcNeutralIsoPtSumIndex");
103  pucorrPtSum_index_ = cfg.getParameter<int>("srcPUcorrPtSumIndex");
104 
105  verbosity_ = cfg.getParameter<int>("verbosity");
106  }
107 
108  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
109 
110  reco::SingleTauDiscriminatorContainer discriminate(const PFTauRef&) const override;
111 
113  if (!loadMVAfromDB_)
114  delete mvaReader_;
115  delete[] mvaInput_;
116  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
117  delete (*it);
118  }
119  }
120 
121  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
122 
123 private:
125 
132  int mvaOpt_;
133  float* mvaInput_;
134 
139 
145 
147 
148  std::vector<TFile*> inputFilesToDelete_;
149 
151 };
152 
154  if (!mvaReader_) {
155  if (loadMVAfromDB_) {
156  mvaReader_ = &es.getData(mvaToken_);
157  } else {
158  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
159  }
160  }
161 
162  evt.getByToken(tauTransverseImpactParameters_token_, tauLifetimeInfos_);
163 
164  evt.getByToken(basicTauDiscriminators_token_, basicTauDiscriminators_);
165 
166  evt.getByToken(Tau_token, taus_);
167 }
168 
171  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to apply WP cuts
172  result.rawValues = {-1., 0.};
173 
174  // CV: computation of MVA value requires presence of leading charged hadron
175  if (tau->leadChargedHadrCand().isNull())
176  return 0.;
177 
178  int tauDecayMode = tau->decayMode();
179 
180  if (((mvaOpt_ == kOldDMwoLT || mvaOpt_ == kOldDMwLT) &&
181  (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 10)) ||
182  ((mvaOpt_ == kNewDMwoLT || mvaOpt_ == kNewDMwLT) &&
183  (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 5 || tauDecayMode == 6 ||
184  tauDecayMode == 10))) {
185  double chargedIsoPtSum = (*basicTauDiscriminators_)[tau].rawValues.at(chargedIsoPtSum_index_);
186  double neutralIsoPtSum = (*basicTauDiscriminators_)[tau].rawValues.at(neutralIsoPtSum_index_);
187  double puCorrPtSum = (*basicTauDiscriminators_)[tau].rawValues.at(pucorrPtSum_index_);
188 
189  const reco::PFTauTransverseImpactParameter& tauLifetimeInfo = *(*tauLifetimeInfos_)[tau];
190 
191  double decayDistX = tauLifetimeInfo.flightLength().x();
192  double decayDistY = tauLifetimeInfo.flightLength().y();
193  double decayDistZ = tauLifetimeInfo.flightLength().z();
194  double decayDistMag = TMath::Sqrt(decayDistX * decayDistX + decayDistY * decayDistY + decayDistZ * decayDistZ);
195 
196  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT) {
197  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
198  mvaInput_[1] = TMath::Abs(tau->eta());
199  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
200  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125 * puCorrPtSum));
201  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
202  mvaInput_[5] = tauDecayMode;
203  } else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT) {
204  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
205  mvaInput_[1] = TMath::Abs(tau->eta());
206  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
207  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125 * puCorrPtSum));
208  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
209  mvaInput_[5] = tauDecayMode;
210  mvaInput_[6] = TMath::Sign(+1., tauLifetimeInfo.dxy());
211  mvaInput_[7] = TMath::Sqrt(TMath::Abs(TMath::Min(1., tauLifetimeInfo.dxy())));
212  mvaInput_[8] = TMath::Min(10., TMath::Abs(tauLifetimeInfo.dxy_Sig()));
213  mvaInput_[9] = (tauLifetimeInfo.hasSecondaryVertex()) ? 1. : 0.;
214  mvaInput_[10] = TMath::Sqrt(decayDistMag);
215  mvaInput_[11] = TMath::Min(10., tauLifetimeInfo.flightLengthSig());
216  }
217 
218  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
219  if (verbosity_) {
220  edm::LogPrint("PFTauDiscByMVAIsol2") << "<PFRecoTauDiscriminationByIsolationMVA2::discriminate>:";
221  edm::LogPrint("PFTauDiscByMVAIsol2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
222  edm::LogPrint("PFTauDiscByMVAIsol2") << " isolation: charged = " << chargedIsoPtSum
223  << ", neutral = " << neutralIsoPtSum << ", PUcorr = " << puCorrPtSum;
224  edm::LogPrint("PFTauDiscByMVAIsol2") << " decay mode = " << tauDecayMode;
225  edm::LogPrint("PFTauDiscByMVAIsol2") << " impact parameter: distance = " << tauLifetimeInfo.dxy()
226  << ", significance = " << tauLifetimeInfo.dxy_Sig();
227  edm::LogPrint("PFTauDiscByMVAIsol2")
228  << " has decay vertex = " << tauLifetimeInfo.hasSecondaryVertex() << ":"
229  << " distance = " << decayDistMag << ", significance = " << tauLifetimeInfo.flightLengthSig();
230  edm::LogPrint("PFTauDiscByMVAIsol2") << "--> mvaValue = " << mvaValue;
231  }
232  result.rawValues.at(0) = mvaValue;
233  }
234  return result;
235 }
236 
238  // pfRecoTauDiscriminationByIsolationMVA2
240 
241  desc.add<std::string>("mvaName");
242  desc.add<bool>("loadMVAfromDB");
243  desc.addOptional<edm::FileInPath>("inputFileName");
244  desc.add<std::string>("mvaOpt");
245 
246  desc.add<edm::InputTag>("srcTauTransverseImpactParameters");
247  desc.add<edm::InputTag>("srcBasicTauDiscriminators");
248  desc.add<int>("srcChargedIsoPtSumIndex");
249  desc.add<int>("srcNeutralIsoPtSumIndex");
250  desc.add<int>("srcPUcorrPtSumIndex");
251  desc.add<int>("verbosity", 0);
252 
253  fillProducerDescriptions(desc); // inherited from the base
254 
255  descriptions.add("pfRecoTauDiscriminationByIsolationMVA2", desc);
256 }
257 
ConfigurationDescriptions.h
PFRecoTauDiscriminationByIsolationMVA2::tauLifetimeInfos_
edm::Handle< PFTauTIPAssociationByRef > tauLifetimeInfos_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:138
edm::ESInputTag
Definition: ESInputTag.h:87
PFTauFwd.h
PFRecoTauDiscriminationByIsolationMVA2::neutralIsoPtSum_index_
int neutralIsoPtSum_index_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:143
PFRecoTauDiscriminationByIsolationMVA2::~PFRecoTauDiscriminationByIsolationMVA2
~PFRecoTauDiscriminationByIsolationMVA2() override
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:112
PFRecoTauDiscriminationByIsolationMVA2::PFRecoTauDiscriminationByIsolationMVA2
PFRecoTauDiscriminationByIsolationMVA2(const edm::ParameterSet &cfg)
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:64
TauDiscriminationProducerBase.h
PFTauDiscriminator.h
metsig::tau
Definition: SignAlgoResolutions.h:49
PFRecoTauDiscriminationByIsolationMVA2::basicTauDiscriminators_
edm::Handle< reco::TauDiscriminatorContainer > basicTauDiscriminators_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:141
PFRecoTauDiscriminationByIsolationMVA2::tauTransverseImpactParameters_token_
edm::EDGetTokenT< PFTauTIPAssociationByRef > tauTransverseImpactParameters_token_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:137
edm::EDGetTokenT
Definition: EDGetToken.h:33
PFRecoTauDiscriminationByIsolationMVA2::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:237
PFTauTransverseImpactParameterAssociation.h
GBRWrapperRcd.h
GBRForest
Definition: GBRForest.h:24
PFRecoTauDiscriminationByIsolationMVA2
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:62
edm::LogPrint
Log< level::Warning, true > LogPrint
Definition: MessageLogger.h:130
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
Sign
T Sign(T A, T B)
Definition: MathUtil.h:54
cms::cuda::assert
assert(be >=bs)
reco::tau::kOldDMwoLT
Definition: PFRecoTauClusterVariables.h:59
PFRecoTauDiscriminationByIsolationMVA2::basicTauDiscriminators_token_
edm::EDGetTokenT< reco::TauDiscriminatorContainer > basicTauDiscriminators_token_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:140
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
PFRecoTauDiscriminationByIsolationMVA2::chargedIsoPtSum_index_
int chargedIsoPtSum_index_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:142
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:127
GBRForest.h
edm::Handle
Definition: AssociativeIterator.h:50
PFRecoTauDiscriminationByIsolationMVA2::kOldDMwoLT
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:131
PFRecoTauDiscriminationByIsolationMVA2::mvaInput_
float * mvaInput_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:133
edm::Ref< PFTauCollection >
reco::PFTauTransverseImpactParameter::dxy
double dxy() const
Definition: PFTauTransverseImpactParameter.h:51
edm::FileInPath
Definition: FileInPath.h:61
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
Abs
T Abs(T a)
Definition: MathUtil.h:49
InefficientDoubleROC.inputFileName
inputFileName
Definition: InefficientDoubleROC.py:437
reco::tau::kOldDMwLT
Definition: PFRecoTauClusterVariables.h:60
reco::PFTauTransverseImpactParameter::flightLengthSig
double flightLengthSig() const
Definition: PFTauTransverseImpactParameter.cc:71
reco::tau::kNewDMwLT
Definition: PFRecoTauClusterVariables.h:62
PFRecoTauDiscriminationByIsolationMVA2::beginEvent
void beginEvent(const edm::Event &, const edm::EventSetup &) override
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:153
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
PFRecoTauDiscriminationByIsolationMVA2::inputFilesToDelete_
std::vector< TFile * > inputFilesToDelete_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:148
ParameterSetDescription.h
reco::PFTauTransverseImpactParameter
Definition: PFTauTransverseImpactParameter.h:24
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
FileInPath.h
reco::PFTauTransverseImpactParameter::hasSecondaryVertex
bool hasSecondaryVertex() const
Definition: PFTauTransverseImpactParameter.h:61
edm::AssociationVector
Definition: AssociationVector.h:67
PFRecoTauDiscriminationByIsolationMVA2::verbosity_
int verbosity_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:150
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
PFRecoTauDiscriminationByIsolationMVA2::pucorrPtSum_index_
int pucorrPtSum_index_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:144
PFRecoTauDiscriminationByIsolationMVA2::taus_
edm::Handle< TauCollection > taus_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:146
PFRecoTauDiscriminationByIsolationMVA2::mvaOpt_
int mvaOpt_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:132
PFRecoTauDiscriminationByIsolationMVA2::loadMVAfromDB_
bool loadMVAfromDB_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:128
PFRecoTauDiscriminationByIsolationMVA2::mvaReader_
const GBRForest * mvaReader_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:130
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
Max
T Max(T a, T b)
Definition: MathUtil.h:44
edm::EventSetup
Definition: EventSetup.h:58
PFRecoTauDiscriminationByIsolationMVA2::moduleLabel_
std::string moduleLabel_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:124
reco::PFTauTransverseImpactParameter::dxy_Sig
double dxy_Sig() const
Definition: PFTauTransverseImpactParameter.h:53
PFRecoTauDiscriminationByIsolationMVA2::inputFileName_
edm::FileInPath inputFileName_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:129
PFRecoTauDiscriminationByIsolationMVA2::discriminate
reco::SingleTauDiscriminatorContainer discriminate(const PFTauRef &) const override
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:169
edm::ESGetToken< GBRForest, GBRWrapperRcd >
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
InputTag.h
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
looper.cfg
cfg
Definition: looper.py:296
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
edm::FileInPath::Unknown
Definition: FileInPath.h:63
PFRecoTauDiscriminationByIsolationMVA2::mvaToken_
edm::ESGetToken< GBRForest, GBRWrapperRcd > mvaToken_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:127
PFRecoTauDiscriminationByIsolationMVA2::PFTauTIPAssociationByRef
edm::AssociationVector< reco::PFTauRefProd, std::vector< reco::PFTauTransverseImpactParameterRef > > PFTauTIPAssociationByRef
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:136
Exception
Definition: hltDiff.cc:245
PFTau.h
EventSetup.h
PFRecoTauDiscriminationByIsolationMVA2::mvaName_
std::string mvaName_
Definition: PFRecoTauDiscriminationByMVAIsolation2.cc:126
Exception.h
mps_fire.result
result
Definition: mps_fire.py:311
cms::Exception
Definition: Exception.h:70
Candidate.h
ParameterSet.h
Min
T Min(T a, T b)
Definition: MathUtil.h:39
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
edm::InputTag
Definition: InputTag.h:15
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
reco::PFTauTransverseImpactParameter::flightLength
const Vector & flightLength() const
Definition: PFTauTransverseImpactParameter.cc:67
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9