CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFRecoTauDiscriminationByMVAIsolationRun2.cc
Go to the documentation of this file.
1 
11 
17 
19 
27 
31 
32 #include <TMath.h>
33 #include <TFile.h>
34 
35 #include <iostream>
36 
37 using namespace reco;
38 
39 namespace
40 {
41  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName, const std::string& mvaName, std::vector<TFile*>& inputFilesToDelete)
42  {
43  if ( inputFileName.location() == edm::FileInPath::Unknown ) throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
44  << " Failed to find File = " << inputFileName << " !!\n";
45  TFile* inputFile = new TFile(inputFileName.fullPath().data());
46 
47  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
48  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
49  if ( !mva )
50  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
51  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
52 
53  inputFilesToDelete.push_back(inputFile);
54 
55  return mva;
56  }
57 
58  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName)
59  {
61  es.get<GBRWrapperRcd>().get(mvaName, mva);
62  return mva.product();
63  }
64 }
65 
67 {
68  public:
71  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
72  mvaReader_(0),
73  mvaInput_(0),
74  category_output_(0)
75  {
76  mvaName_ = cfg.getParameter<std::string>("mvaName");
77  loadMVAfromDB_ = cfg.exists("loadMVAfromDB") ? cfg.getParameter<bool>("loadMVAfromDB") : false;
78  if ( !loadMVAfromDB_ ) {
79  if(cfg.exists("inputFileName")){
80  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
81  }else throw cms::Exception("MVA input not defined") << "Requested to load tau MVA input from ROOT file but no file provided in cfg file";
82  }
83  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
84  if ( mvaOpt_string == "oldDMwoLT" ) mvaOpt_ = kOldDMwoLT;
85  else if ( mvaOpt_string == "oldDMwLT" ) mvaOpt_ = kOldDMwLT;
86  else if ( mvaOpt_string == "newDMwoLT" ) mvaOpt_ = kNewDMwoLT;
87  else if ( mvaOpt_string == "newDMwLT" ) mvaOpt_ = kNewDMwLT;
88  else if ( mvaOpt_string == "DBoldDMwLT" ) mvaOpt_ = kDBoldDMwLT;
89  else if ( mvaOpt_string == "DBnewDMwLT" ) mvaOpt_ = kDBnewDMwLT;
90  else if ( mvaOpt_string == "PWoldDMwLT" ) mvaOpt_ = kPWoldDMwLT;
91  else if ( mvaOpt_string == "PWnewDMwLT" ) mvaOpt_ = kPWnewDMwLT;
92  else throw cms::Exception("PFRecoTauDiscriminationByMVAIsolationRun2")
93  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
94 
95  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) mvaInput_ = new float[6];
96  else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) mvaInput_ = new float[12];
97  else if ( mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT ||
98  mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT) mvaInput_ = new float[23];
99  else assert(0);
100 
101  TauTransverseImpactParameters_token = consumes<PFTauTIPAssociationByRef>(cfg.getParameter<edm::InputTag>("srcTauTransverseImpactParameters"));
102 
103  ChargedIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcChargedIsoPtSum"));
104  NeutralIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcNeutralIsoPtSum"));
105  PUcorrPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcPUcorrPtSum"));
106  PhotonPtSumOutsideSignalCone_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcPhotonPtSumOutsideSignalCone"));
107  FootprintCorrection_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcFootprintCorrection"));
108 
109  verbosity_ = ( cfg.exists("verbosity") ) ?
110  cfg.getParameter<int>("verbosity") : 0;
111 
112  produces<PFTauDiscriminator>("category");
113  }
114 
115  void beginEvent(const edm::Event&, const edm::EventSetup&);
116 
117  double discriminate(const PFTauRef&) const;
118 
119  void endEvent(edm::Event&);
120 
122  {
123  if(!loadMVAfromDB_) delete mvaReader_;
124  delete[] mvaInput_;
125  for ( std::vector<TFile*>::iterator it = inputFilesToDelete_.begin();
126  it != inputFilesToDelete_.end(); ++it ) {
127  delete (*it);
128  }
129  }
130 
131  private:
132 
134 
139  enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT, kDBoldDMwLT, kDBnewDMwLT, kPWoldDMwLT, kPWnewDMwLT };
140  int mvaOpt_;
141  float* mvaInput_;
142 
146 
157 
159  std::auto_ptr<PFTauDiscriminator> category_output_;
160 
161  std::vector<TFile*> inputFilesToDelete_;
162 
164 };
165 
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(TauTransverseImpactParameters_token, tauLifetimeInfos);
177 
178  evt.getByToken(ChargedIsoPtSum_token, chargedIsoPtSums_);
179  evt.getByToken(NeutralIsoPtSum_token, neutralIsoPtSums_);
180  evt.getByToken(PUcorrPtSum_token, puCorrPtSums_);
181  evt.getByToken(PhotonPtSumOutsideSignalCone_token, photonPtSumOutsideSignalCone_);
182  evt.getByToken(FootprintCorrection_token, footprintCorrection_);
183 
184  evt.getByToken(Tau_token, taus_);
185  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
186 }
187 
189 {
190  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
191  double category = 0.;
192  category_output_->setValue(tauIndex_, category);
193 
194  // CV: computation of MVA value requires presence of leading charged hadron
195  if ( tau->leadPFChargedHadrCand().isNull() ) return 0.;
196 
197  int tauDecayMode = tau->decayMode();
198 
199  if ( ((mvaOpt_ == kOldDMwoLT || mvaOpt_ == kOldDMwLT || mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kPWoldDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 10)) ||
200  ((mvaOpt_ == kNewDMwoLT || mvaOpt_ == kNewDMwLT || mvaOpt_ == kDBnewDMwLT || mvaOpt_ == kPWnewDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 5 || tauDecayMode == 6 || tauDecayMode == 10)) ) {
201 
202  double chargedIsoPtSum = (*chargedIsoPtSums_)[tau];
203  double neutralIsoPtSum = (*neutralIsoPtSums_)[tau];
204  double puCorrPtSum = (*puCorrPtSums_)[tau];
205  double photonPtSumOutsideSignalCone = (*photonPtSumOutsideSignalCone_)[tau];
206  double footprintCorrection = (*footprintCorrection_)[tau];
207 
208  const reco::PFTauTransverseImpactParameter& tauLifetimeInfo = *(*tauLifetimeInfos)[tau];
209 
210  double decayDistX = tauLifetimeInfo.flightLength().x();
211  double decayDistY = tauLifetimeInfo.flightLength().y();
212  double decayDistZ = tauLifetimeInfo.flightLength().z();
213  double decayDistMag = std::sqrt(decayDistX*decayDistX + decayDistY*decayDistY + decayDistZ*decayDistZ);
214 
215  double nPhoton = double(tau_n_photons_total(*tau));
216  double ptWeightedDetaStrip = tau_pt_weighted_deta_strip(*tau, tauDecayMode);
217  double ptWeightedDphiStrip = tau_pt_weighted_dphi_strip(*tau, tauDecayMode);
218  double ptWeightedDrSignal = tau_pt_weighted_dr_signal(*tau, tauDecayMode);
219  double ptWeightedDrIsolation = tau_pt_weighted_dr_iso(*tau, tauDecayMode);
220  double leadingTrackChi2 = tau_leadTrackChi2(*tau);
221  double eRatio = tau_Eratio(*tau);
222 
223  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) {
224  mvaInput_[0] = std::log(std::max(1., Double_t(tau->pt())));
225  mvaInput_[1] = std::fabs(tau->eta());
226  mvaInput_[2] = std::log(std::max(1.e-2, chargedIsoPtSum));
227  mvaInput_[3] = std::log(std::max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
228  mvaInput_[4] = std::log(std::max(1.e-2, puCorrPtSum));
229  mvaInput_[5] = tauDecayMode;
230  } else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) {
231  mvaInput_[0] = std::log(std::max(1., Double_t(tau->pt())));
232  mvaInput_[1] = std::fabs(tau->eta());
233  mvaInput_[2] = std::log(std::max(1.e-2, chargedIsoPtSum));
234  mvaInput_[3] = std::log(std::max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
235  mvaInput_[4] = std::log(std::max(1.e-2, puCorrPtSum));
236  mvaInput_[5] = tauDecayMode;
237  mvaInput_[6] = TMath::Sign(+1., tauLifetimeInfo.dxy());
238  mvaInput_[7] = std::sqrt(std::fabs(std::min(1., tauLifetimeInfo.dxy())));
239  mvaInput_[8] = std::min(10., std::fabs(tauLifetimeInfo.dxy_Sig()));
240  mvaInput_[9] = ( tauLifetimeInfo.hasSecondaryVertex() ) ? 1. : 0.;
241  mvaInput_[10] = std::sqrt(decayDistMag);
242  mvaInput_[11] = std::min(10., tauLifetimeInfo.flightLengthSig());
243  } else if ( mvaOpt_ == kDBoldDMwLT || mvaOpt_ == kDBnewDMwLT ) {
244  mvaInput_[0] = std::log(std::max(1., Double_t(tau->pt())));
245  mvaInput_[1] = std::fabs(tau->eta());
246  mvaInput_[2] = std::log(std::max(1.e-2, chargedIsoPtSum));
247  mvaInput_[3] = std::log(std::max(1.e-2, neutralIsoPtSum));
248  mvaInput_[4] = std::log(std::max(1.e-2, puCorrPtSum));
249  mvaInput_[5] = std::log(std::max(1.e-2, photonPtSumOutsideSignalCone));
250  mvaInput_[6] = tauDecayMode;
251  mvaInput_[7] = std::min(30., nPhoton);
252  mvaInput_[8] = std::min(0.5, ptWeightedDetaStrip);
253  mvaInput_[9] = std::min(0.5, ptWeightedDphiStrip);
254  mvaInput_[10] = std::min(0.5, ptWeightedDrSignal);
255  mvaInput_[11] = std::min(0.5, ptWeightedDrIsolation);
256  mvaInput_[12] = std::min(100., leadingTrackChi2);
257  mvaInput_[13] = std::min(1., eRatio);
258  mvaInput_[14] = TMath::Sign(+1., tauLifetimeInfo.dxy());
259  mvaInput_[15] = std::sqrt(std::fabs(std::min(1., std::fabs(tauLifetimeInfo.dxy()))));
260  mvaInput_[16] = std::min(10., std::fabs(tauLifetimeInfo.dxy_Sig()));
261  mvaInput_[17] = TMath::Sign(+1., tauLifetimeInfo.ip3d());
262  mvaInput_[18] = std::sqrt(std::fabs(std::min(1., std::fabs(tauLifetimeInfo.ip3d()))));
263  mvaInput_[19] = std::min(10., std::fabs(tauLifetimeInfo.ip3d_Sig()));
264  mvaInput_[20] = ( tauLifetimeInfo.hasSecondaryVertex() ) ? 1. : 0.;
265  mvaInput_[21] = std::sqrt(decayDistMag);
266  mvaInput_[22] = std::min(10., tauLifetimeInfo.flightLengthSig());
267  } else if ( mvaOpt_ == kPWoldDMwLT || mvaOpt_ == kPWnewDMwLT ) {
268  mvaInput_[0] = std::log(std::max(1., Double_t(tau->pt())));
269  mvaInput_[1] = std::fabs(tau->eta());
270  mvaInput_[2] = std::log(std::max(1.e-2, chargedIsoPtSum));
271  mvaInput_[3] = std::log(std::max(1.e-2, neutralIsoPtSum));
272  mvaInput_[4] = std::log(std::max(1.e-2, footprintCorrection));
273  mvaInput_[5] = std::log(std::max(1.e-2, photonPtSumOutsideSignalCone));
274  mvaInput_[6] = tauDecayMode;
275  mvaInput_[7] = std::min(30., nPhoton);
276  mvaInput_[8] = std::min(0.5, ptWeightedDetaStrip);
277  mvaInput_[9] = std::min(0.5, ptWeightedDphiStrip);
278  mvaInput_[10] = std::min(0.5, ptWeightedDrSignal);
279  mvaInput_[11] = std::min(0.5, ptWeightedDrIsolation);
280  mvaInput_[12] = std::min(100., leadingTrackChi2);
281  mvaInput_[13] = std::min(1., eRatio);
282  mvaInput_[14] = TMath::Sign(+1., tauLifetimeInfo.dxy());
283  mvaInput_[15] = std::sqrt(std::fabs(std::min(1., std::fabs(tauLifetimeInfo.dxy()))));
284  mvaInput_[16] = std::min(10., std::fabs(tauLifetimeInfo.dxy_Sig()));
285  mvaInput_[17] = TMath::Sign(+1., tauLifetimeInfo.ip3d());
286  mvaInput_[18] = std::sqrt(std::fabs(std::min(1., std::fabs(tauLifetimeInfo.ip3d()))));
287  mvaInput_[19] = std::min(10., std::fabs(tauLifetimeInfo.ip3d_Sig()));
288  mvaInput_[20] = ( tauLifetimeInfo.hasSecondaryVertex() ) ? 1. : 0.;
289  mvaInput_[21] = std::sqrt(decayDistMag);
290  mvaInput_[22] = std::min(10., tauLifetimeInfo.flightLengthSig());
291  }
292 
293  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
294  if ( verbosity_ ) {
295  edm::LogPrint("PFTauDiscByMVAIsol2") << "<PFRecoTauDiscriminationByMVAIsolationRun2::discriminate>:";
296  edm::LogPrint("PFTauDiscByMVAIsol2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
297  edm::LogPrint("PFTauDiscByMVAIsol2") << " isolation: charged = " << chargedIsoPtSum << ", neutral = " << neutralIsoPtSum << ", PUcorr = " << puCorrPtSum;
298  edm::LogPrint("PFTauDiscByMVAIsol2") << " decay mode = " << tauDecayMode;
299  edm::LogPrint("PFTauDiscByMVAIsol2") << " impact parameter: distance = " << tauLifetimeInfo.dxy() << ", significance = " << tauLifetimeInfo.dxy_Sig();
300  edm::LogPrint("PFTauDiscByMVAIsol2") << " has decay vertex = " << tauLifetimeInfo.hasSecondaryVertex() << ":"
301  << " distance = " << decayDistMag << ", significance = " << tauLifetimeInfo.flightLengthSig();
302  edm::LogPrint("PFTauDiscByMVAIsol2") << "--> mvaValue = " << mvaValue;
303  }
304  return mvaValue;
305  } else {
306  return -1.;
307  }
308 }
309 
311 {
312  // add all category indices to event
313  evt.put(category_output_, "category");
314 }
315 
T getParameter(std::string const &) const
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:42
tuple cfg
Definition: looper.py:293
T Sign(T A, T B)
Definition: MathUtil.h:54
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
edm::AssociationVector< reco::PFTauRefProd, std::vector< reco::PFTauTransverseImpactParameterRef > > PFTauTIPAssociationByRef
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< reco::PFTauDiscriminator > NeutralIsoPtSum_token
assert(m_qm.get())
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< reco::PFTauDiscriminator > PhotonPtSumOutsideSignalCone_token
edm::EDGetTokenT< reco::PFTauDiscriminator > FootprintCorrection_token
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
edm::EDGetTokenT< PFTauTIPAssociationByRef > TauTransverseImpactParameters_token
T sqrt(T t)
Definition: SSEVec.h:18
T min(T a, T b)
Definition: MathUtil.h:58
bool isNull() const
Checks for null.
Definition: Ref.h:249
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:178
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< reco::PFTauDiscriminator > ChargedIsoPtSum_token
std::string fullPath() const
Definition: FileInPath.cc:184
moduleLabel_(iConfig.getParameter< string >("@module_label"))
edm::Handle< reco::PFTauDiscriminator > photonPtSumOutsideSignalCone_
void beginEvent(const edm::Event &, const edm::EventSetup &)
tuple loadMVAfromDB
Definition: mvaPFMET_cff.py:84
edm::EDGetTokenT< reco::PFTauDiscriminator > PUcorrPtSum_token