CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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) {
64  es.get<GBRWrapperRcd>().get(mvaName, mva);
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  category_output_() {
77  mvaName_ = cfg.getParameter<std::string>("mvaName");
78  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
79  if (!loadMVAfromDB_) {
80  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
81  }
82  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
83  if (mvaOpt_string == "oldDMwoLT")
84  mvaOpt_ = kOldDMwoLT;
85  else if (mvaOpt_string == "oldDMwLT")
86  mvaOpt_ = kOldDMwLT;
87  else if (mvaOpt_string == "newDMwoLT")
88  mvaOpt_ = kNewDMwoLT;
89  else if (mvaOpt_string == "newDMwLT")
90  mvaOpt_ = kNewDMwLT;
91  else
92  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2")
93  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
94 
95  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
96  mvaInput_ = new float[6];
97  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
98  mvaInput_ = new float[12];
99  else
100  assert(0);
101 
102  TauTransverseImpactParameters_token =
103  consumes<PFTauTIPAssociationByRef>(cfg.getParameter<edm::InputTag>("srcTauTransverseImpactParameters"));
104 
105  ChargedIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcChargedIsoPtSum"));
106  NeutralIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcNeutralIsoPtSum"));
107  PUcorrPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcPUcorrPtSum"));
108 
109  verbosity_ = cfg.getParameter<int>("verbosity");
110 
111  produces<PFTauDiscriminator>("category");
112  }
113 
114  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
115 
116  double discriminate(const PFTauRef&) const override;
117 
118  void endEvent(edm::Event&) override;
119 
121  if (!loadMVAfromDB_)
122  delete mvaReader_;
123  delete[] mvaInput_;
124  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
125  delete (*it);
126  }
127  }
128 
129  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
130 
131 private:
133 
139  int mvaOpt_;
140  float* mvaInput_;
141 
146 
153 
155  std::unique_ptr<PFTauDiscriminator> category_output_;
156 
157  std::vector<TFile*> inputFilesToDelete_;
158 
160 };
161 
163  if (!mvaReader_) {
164  if (loadMVAfromDB_) {
165  mvaReader_ = loadMVAfromDB(es, mvaName_);
166  } else {
167  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
168  }
169  }
170 
171  evt.getByToken(TauTransverseImpactParameters_token, tauLifetimeInfos);
172 
173  evt.getByToken(ChargedIsoPtSum_token, chargedIsoPtSums_);
174  evt.getByToken(NeutralIsoPtSum_token, neutralIsoPtSums_);
175  evt.getByToken(PUcorrPtSum_token, puCorrPtSums_);
176 
177  evt.getByToken(Tau_token, taus_);
178  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
179 }
180 
182  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
183  double category = 0.;
184  category_output_->setValue(tauIndex_, category);
185 
186  // CV: computation of MVA value requires presence of leading charged hadron
187  if (tau->leadChargedHadrCand().isNull())
188  return 0.;
189 
190  int tauDecayMode = tau->decayMode();
191 
192  if (((mvaOpt_ == kOldDMwoLT || mvaOpt_ == kOldDMwLT) &&
193  (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 10)) ||
194  ((mvaOpt_ == kNewDMwoLT || mvaOpt_ == kNewDMwLT) &&
195  (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 5 || tauDecayMode == 6 ||
196  tauDecayMode == 10))) {
197  double chargedIsoPtSum = (*chargedIsoPtSums_)[tau];
198  double neutralIsoPtSum = (*neutralIsoPtSums_)[tau];
199  double puCorrPtSum = (*puCorrPtSums_)[tau];
200 
201  const reco::PFTauTransverseImpactParameter& tauLifetimeInfo = *(*tauLifetimeInfos)[tau];
202 
203  double decayDistX = tauLifetimeInfo.flightLength().x();
204  double decayDistY = tauLifetimeInfo.flightLength().y();
205  double decayDistZ = tauLifetimeInfo.flightLength().z();
206  double decayDistMag = TMath::Sqrt(decayDistX * decayDistX + decayDistY * decayDistY + decayDistZ * decayDistZ);
207 
208  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT) {
209  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
210  mvaInput_[1] = TMath::Abs(tau->eta());
211  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
212  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125 * puCorrPtSum));
213  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
214  mvaInput_[5] = tauDecayMode;
215  } else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT) {
216  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
217  mvaInput_[1] = TMath::Abs(tau->eta());
218  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
219  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125 * puCorrPtSum));
220  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
221  mvaInput_[5] = tauDecayMode;
222  mvaInput_[6] = TMath::Sign(+1., tauLifetimeInfo.dxy());
223  mvaInput_[7] = TMath::Sqrt(TMath::Abs(TMath::Min(1., tauLifetimeInfo.dxy())));
224  mvaInput_[8] = TMath::Min(10., TMath::Abs(tauLifetimeInfo.dxy_Sig()));
225  mvaInput_[9] = (tauLifetimeInfo.hasSecondaryVertex()) ? 1. : 0.;
226  mvaInput_[10] = TMath::Sqrt(decayDistMag);
227  mvaInput_[11] = TMath::Min(10., tauLifetimeInfo.flightLengthSig());
228  }
229 
230  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
231  if (verbosity_) {
232  edm::LogPrint("PFTauDiscByMVAIsol2") << "<PFRecoTauDiscriminationByIsolationMVA2::discriminate>:";
233  edm::LogPrint("PFTauDiscByMVAIsol2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
234  edm::LogPrint("PFTauDiscByMVAIsol2") << " isolation: charged = " << chargedIsoPtSum
235  << ", neutral = " << neutralIsoPtSum << ", PUcorr = " << puCorrPtSum;
236  edm::LogPrint("PFTauDiscByMVAIsol2") << " decay mode = " << tauDecayMode;
237  edm::LogPrint("PFTauDiscByMVAIsol2") << " impact parameter: distance = " << tauLifetimeInfo.dxy()
238  << ", significance = " << tauLifetimeInfo.dxy_Sig();
239  edm::LogPrint("PFTauDiscByMVAIsol2")
240  << " has decay vertex = " << tauLifetimeInfo.hasSecondaryVertex() << ":"
241  << " distance = " << decayDistMag << ", significance = " << tauLifetimeInfo.flightLengthSig();
242  edm::LogPrint("PFTauDiscByMVAIsol2") << "--> mvaValue = " << mvaValue;
243  }
244  return mvaValue;
245  } else {
246  return -1.;
247  }
248 }
249 
251  // add all category indices to event
252  evt.put(std::move(category_output_), "category");
253 }
254 
256  // pfRecoTauDiscriminationByIsolationMVA2
258 
259  desc.add<std::string>("mvaName");
260  desc.add<bool>("loadMVAfromDB");
261  desc.addOptional<edm::FileInPath>("inputFileName");
262  desc.add<std::string>("mvaOpt");
263 
264  desc.add<edm::InputTag>("srcTauTransverseImpactParameters");
265  desc.add<edm::InputTag>("srcChargedIsoPtSum");
266  desc.add<edm::InputTag>("srcNeutralIsoPtSum");
267  desc.add<edm::InputTag>("srcPUcorrPtSum");
268  desc.add<int>("verbosity", 0);
269 
270  fillProducerDescriptions(desc); // inherited from the base
271 
272  descriptions.add("pfRecoTauDiscriminationByIsolationMVA2", desc);
273 }
274 
T getParameter(std::string const &) const
void beginEvent(const edm::Event &, const edm::EventSetup &) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:38
T Sign(T A, T B)
Definition: MathUtil.h:54
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
edm::AssociationVector< reco::PFTauRefProd, std::vector< reco::PFTauTransverseImpactParameterRef > > PFTauTIPAssociationByRef
edm::Handle< reco::PFTauDiscriminator > chargedIsoPtSums_
#define nullptr
edm::Handle< PFTauTIPAssociationByRef > tauLifetimeInfos
T Min(T a, T b)
Definition: MathUtil.h:39
edm::EDGetTokenT< reco::PFTauDiscriminator > ChargedIsoPtSum_token
edm::EDGetTokenT< reco::PFTauDiscriminator > NeutralIsoPtSum_token
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T Abs(T a)
Definition: MathUtil.h:49
edm::EDGetTokenT< PFTauTIPAssociationByRef > TauTransverseImpactParameters_token
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:161
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isNull() const
Checks for null.
Definition: Ref.h:235
T Max(T a, T b)
Definition: MathUtil.h:44
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::Handle< reco::PFTauDiscriminator > neutralIsoPtSums_
fixed size matrix
T get() const
Definition: EventSetup.h:73
std::string fullPath() const
Definition: FileInPath.cc:163
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< reco::PFTauDiscriminator > PUcorrPtSum_token
def move(src, dest)
Definition: eostools.py:511