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 {
50  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName, const std::string& mvaName, std::vector<TFile*>& inputFilesToDelete)
51  {
52  if ( inputFileName.location() == edm::FileInPath::Unknown ) throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
53  << " Failed to find File = " << inputFileName << " !!\n";
54  TFile* inputFile = new TFile(inputFileName.fullPath().data());
55 
56  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
57  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
58  if ( !mva )
59  throw cms::Exception("PATTauDiscriminationByIsolationMVARun2::loadMVA")
60  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
61 
62  inputFilesToDelete.push_back(inputFile);
63 
64  return mva;
65  }
66 
67  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName)
68  {
70  es.get<GBRWrapperRcd>().get(mvaName, mva);
71  return mva.product();
72  }
73 }
74 
75 namespace reco { namespace tau {
76 
78 {
79  public:
82  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
83  mvaReader_(nullptr),
84  mvaInput_(nullptr),
85  category_output_()
86  {
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" ) mvaOpt_ = kOldDMwoLT;
94  else if ( mvaOpt_string == "oldDMwLT" ) mvaOpt_ = kOldDMwLT;
95  else if ( mvaOpt_string == "newDMwoLT" ) mvaOpt_ = kNewDMwoLT;
96  else if ( mvaOpt_string == "newDMwLT" ) mvaOpt_ = kNewDMwLT;
97  else if ( mvaOpt_string == "DBoldDMwLT" ) mvaOpt_ = kDBoldDMwLT;
98  else if ( mvaOpt_string == "DBnewDMwLT" ) mvaOpt_ = kDBnewDMwLT;
99  else if ( mvaOpt_string == "PWoldDMwLT" ) mvaOpt_ = kPWoldDMwLT;
100  else if ( mvaOpt_string == "PWnewDMwLT" ) mvaOpt_ = kPWnewDMwLT;
101  else if ( mvaOpt_string == "DBoldDMwLTwGJ" ) mvaOpt_ = kDBoldDMwLTwGJ;
102  else if ( mvaOpt_string == "DBnewDMwLTwGJ" ) mvaOpt_ = kDBnewDMwLTwGJ;
103  else throw cms::Exception("PATTauDiscriminationByMVAIsolationRun2")
104  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
105 
106  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) mvaInput_ = new float[6];
107  else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) mvaInput_ = new float[12];
108  else if ( mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT ||
109  mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ||
110  mvaOpt_ == kDBoldDMwLTwGJ || mvaOpt_ == kDBnewDMwLTwGJ) mvaInput_ = new float[23];
111  else assert(0);
112 
113  chargedIsoPtSums_ = cfg.getParameter<std::string>("srcChargedIsoPtSum");
114  neutralIsoPtSums_ = cfg.getParameter<std::string>("srcNeutralIsoPtSum");
115  puCorrPtSums_ = cfg.getParameter<std::string>("srcPUcorrPtSum");
116  photonPtSumOutsideSignalCone_ = cfg.getParameter<std::string>("srcPhotonPtSumOutsideSignalCone");
117  footprintCorrection_ = cfg.getParameter<std::string>("srcFootprintCorrection");
118 
119  verbosity_ = cfg.getParameter<int>("verbosity");
120 
121  produces<pat::PATTauDiscriminator>("category");
122  }
123 
124  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
125 
126  double discriminate(const TauRef&) const override;
127 
128  void endEvent(edm::Event&) override;
129 
131  {
132  if(!loadMVAfromDB_) delete mvaReader_;
133  delete[] mvaInput_;
134  for ( std::vector<TFile*>::iterator it = inputFilesToDelete_.begin();
135  it != inputFilesToDelete_.end(); ++it ) {
136  delete (*it);
137  }
138  }
139 
140  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
141 
142  private:
143 
145 
150  int mvaOpt_;
151  float* mvaInput_;
152 
158 
160  std::unique_ptr<pat::PATTauDiscriminator> category_output_;
161  std::vector<TFile*> inputFilesToDelete_;
162 
164 };
165 
166 void PATTauDiscriminationByMVAIsolationRun2::beginEvent(const edm::Event& evt, const edm::EventSetup& es)
167 {
168  if( !mvaReader_ ) {
169  if ( loadMVAfromDB_ ) {
170  mvaReader_ = loadMVAfromDB(es, mvaName_);
171  } else {
172  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
173  }
174  }
175 
176  evt.getByToken(Tau_token, taus_);
177  category_output_.reset(new pat::PATTauDiscriminator(TauRefProd(taus_)));
178 }
179 
180 double PATTauDiscriminationByMVAIsolationRun2::discriminate(const TauRef& tau) const
181 {
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() ) return 0.;
188 
189  if (reco::tau::fillIsoMVARun2Inputs(mvaInput_, *tau, mvaOpt_, chargedIsoPtSums_, neutralIsoPtSums_, puCorrPtSums_, photonPtSumOutsideSignalCone_, footprintCorrection_)) {
190  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
191  if ( verbosity_ ) {
192  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "<PATTauDiscriminationByMVAIsolationRun2::discriminate>:";
193  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
194  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " isolation: charged = " << tau->tauID(chargedIsoPtSums_) << ", neutral = " << tau->tauID(neutralIsoPtSums_) << ", PUcorr = " << tau->tauID(puCorrPtSums_);
195  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " decay mode = " << tau->decayMode();
196  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " impact parameter: distance = " << tau->dxy() << ", significance = " << tau->dxy_Sig();
197  edm::LogPrint("PATTauDiscByMVAIsolRun2") << " has decay vertex = " << tau->hasSecondaryVertex() << ":"
198  << ", significance = " << tau->flightLengthSig();
199  edm::LogPrint("PATTauDiscByMVAIsolRun2") << "--> mvaValue = " << mvaValue;
200  }
201  return mvaValue;
202  }
203  return -1.;
204 }
205 
206 void PATTauDiscriminationByMVAIsolationRun2::endEvent(edm::Event& evt)
207 {
208  // add all category indices to event
209  evt.put(std::move(category_output_), "category");
210 }
211 
212 void
214  // patTauDiscriminationByMVAIsolationRun2
216 
217  desc.add<std::string>("mvaName");
218  desc.add<bool>("loadMVAfromDB");
219  desc.addOptional<edm::FileInPath>("inputFileName");
220  desc.add<std::string>("mvaOpt");
221 
222  desc.add<std::string>("srcChargedIsoPtSum");
223  desc.add<std::string>("srcNeutralIsoPtSum");
224  desc.add<std::string>("srcPUcorrPtSum");
225  desc.add<std::string>("srcPhotonPtSumOutsideSignalCone");
226  desc.add<std::string>("srcFootprintCorrection");
227  desc.add<int>("verbosity", 0);
228 
229  fillProducerDescriptions(desc); // inherited from the base
230 
231  descriptions.add("patTauDiscriminationByMVAIsolationRun2", desc);
232 }
233 
235 
236 }} //namespace
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:40
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
#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:248
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
fixed size matrix
T get() const
Definition: EventSetup.h:71
std::string fullPath() const
Definition: FileInPath.cc:163
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:511