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) {
71  es.get<GBRWrapperRcd>().get(mvaName, mva);
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  category_output_() {
87  mvaName_ = cfg.getParameter<std::string>("mvaName");
88  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
89  if (!loadMVAfromDB_) {
90  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
91  }
92  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
93  if (mvaOpt_string == "oldDMwoLT")
94  mvaOpt_ = kOldDMwoLT;
95  else if (mvaOpt_string == "oldDMwLT")
96  mvaOpt_ = kOldDMwLT;
97  else if (mvaOpt_string == "newDMwoLT")
98  mvaOpt_ = kNewDMwoLT;
99  else if (mvaOpt_string == "newDMwLT")
100  mvaOpt_ = kNewDMwLT;
101  else if (mvaOpt_string == "DBoldDMwLT")
102  mvaOpt_ = kDBoldDMwLT;
103  else if (mvaOpt_string == "DBnewDMwLT")
104  mvaOpt_ = kDBnewDMwLT;
105  else if (mvaOpt_string == "PWoldDMwLT")
106  mvaOpt_ = kPWoldDMwLT;
107  else if (mvaOpt_string == "PWnewDMwLT")
108  mvaOpt_ = kPWnewDMwLT;
109  else if (mvaOpt_string == "DBoldDMwLTwGJ")
110  mvaOpt_ = kDBoldDMwLTwGJ;
111  else if (mvaOpt_string == "DBnewDMwLTwGJ")
112  mvaOpt_ = kDBnewDMwLTwGJ;
113  else
114  throw cms::Exception("PATTauDiscriminationByMVAIsolationRun2")
115  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
116 
117  if (mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT)
118  mvaInput_ = new float[6];
119  else if (mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT)
120  mvaInput_ = new float[12];
121  else if (mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT || mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ||
122  mvaOpt_ == kDBoldDMwLTwGJ || mvaOpt_ == kDBnewDMwLTwGJ)
123  mvaInput_ = new float[23];
124  else
125  assert(0);
126 
127  chargedIsoPtSums_ = cfg.getParameter<std::string>("srcChargedIsoPtSum");
128  neutralIsoPtSums_ = cfg.getParameter<std::string>("srcNeutralIsoPtSum");
129  puCorrPtSums_ = cfg.getParameter<std::string>("srcPUcorrPtSum");
130  photonPtSumOutsideSignalCone_ = cfg.getParameter<std::string>("srcPhotonPtSumOutsideSignalCone");
131  footprintCorrection_ = cfg.getParameter<std::string>("srcFootprintCorrection");
132 
133  verbosity_ = cfg.getParameter<int>("verbosity");
134 
135  produces<pat::PATTauDiscriminator>("category");
136  }
137 
138  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
139 
140  double discriminate(const TauRef&) const override;
141 
142  void endEvent(edm::Event&) override;
143 
145  if (!loadMVAfromDB_)
146  delete mvaReader_;
147  delete[] mvaInput_;
148  for (std::vector<TFile*>::iterator it = inputFilesToDelete_.begin(); it != inputFilesToDelete_.end(); ++it) {
149  delete (*it);
150  }
151  }
152 
153  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
154 
155  private:
157 
162  int mvaOpt_;
163  float* mvaInput_;
164 
170 
172  std::unique_ptr<pat::PATTauDiscriminator> category_output_;
173  std::vector<TFile*> inputFilesToDelete_;
174 
176  };
177 
178  void PATTauDiscriminationByMVAIsolationRun2::beginEvent(const edm::Event& evt, const edm::EventSetup& es) {
179  if (!mvaReader_) {
180  if (loadMVAfromDB_) {
181  mvaReader_ = loadMVAfromDB(es, mvaName_);
182  } else {
183  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
184  }
185  }
186 
187  evt.getByToken(Tau_token, taus_);
188  category_output_.reset(new pat::PATTauDiscriminator(TauRefProd(taus_)));
189  }
190 
191  double PATTauDiscriminationByMVAIsolationRun2::discriminate(const TauRef& tau) const {
192  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
193  double category = 0.;
194  category_output_->setValue(tauIndex_, category);
195 
196  // CV: computation of MVA value requires presence of leading charged hadron
197  if (tau->leadChargedHadrCand().isNull())
198  return 0.;
199 
200  if (reco::tau::fillIsoMVARun2Inputs(mvaInput_,
201  *tau,
202  mvaOpt_,
203  chargedIsoPtSums_,
204  neutralIsoPtSums_,
205  puCorrPtSums_,
206  photonPtSumOutsideSignalCone_,
207  footprintCorrection_)) {
208  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
209  if (verbosity_) {
210  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "<PATTauDiscriminationByMVAIsolationRun2::discriminate>:";
211  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
212  edm::LogPrint("PATTauDiscByMVAIsolRun2")
213  << " isolation: charged = " << tau->tauID(chargedIsoPtSums_)
214  << ", neutral = " << tau->tauID(neutralIsoPtSums_) << ", PUcorr = " << tau->tauID(puCorrPtSums_);
215  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " decay mode = " << tau->decayMode();
216  edm::LogPrint("PATTauDiscByMVAIsolRun2")
217  << " impact parameter: distance = " << tau->dxy() << ", significance = " << tau->dxy_Sig();
218  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " has decay vertex = " << tau->hasSecondaryVertex() << ":"
219  << ", significance = " << tau->flightLengthSig();
220  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "--> mvaValue = " << mvaValue;
221  }
222  return mvaValue;
223  }
224  return -1.;
225  }
226 
227  void PATTauDiscriminationByMVAIsolationRun2::endEvent(edm::Event& evt) {
228  // add all category indices to event
229  evt.put(std::move(category_output_), "category");
230  }
231 
233  // patTauDiscriminationByMVAIsolationRun2
235 
236  desc.add<std::string>("mvaName");
237  desc.add<bool>("loadMVAfromDB");
238  desc.addOptional<edm::FileInPath>("inputFileName");
239  desc.add<std::string>("mvaOpt");
240 
241  desc.add<std::string>("srcChargedIsoPtSum");
242  desc.add<std::string>("srcNeutralIsoPtSum");
243  desc.add<std::string>("srcPUcorrPtSum");
244  desc.add<std::string>("srcPhotonPtSumOutsideSignalCone");
245  desc.add<std::string>("srcFootprintCorrection");
246  desc.add<int>("verbosity", 0);
247 
248  fillProducerDescriptions(desc); // inherited from the base
249 
250  descriptions.add("patTauDiscriminationByMVAIsolationRun2", desc);
251  }
252 
254 
255  } // namespace tau
256 } // namespace reco
T getParameter(std::string const &) const
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
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
#define nullptr
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: HeavyIon.h:7
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
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
def move(src, dest)
Definition: eostools.py:511