CMS 3D CMS Logo

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