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 
26 
30 
31 #include <TMath.h>
32 #include <TFile.h>
33 
34 #include <iostream>
35 
36 using namespace reco;
37 
38 namespace
39 {
40  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName, const std::string& mvaName, std::vector<TFile*>& inputFilesToDelete)
41  {
42  if ( inputFileName.location() == edm::FileInPath::Unknown ) throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
43  << " Failed to find File = " << inputFileName << " !!\n";
44  TFile* inputFile = new TFile(inputFileName.fullPath().data());
45 
46  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
47  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
48  if ( !mva )
49  throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2::loadMVA")
50  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
51 
52  inputFilesToDelete.push_back(inputFile);
53 
54  return mva;
55  }
56 
57  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName)
58  {
60  es.get<GBRWrapperRcd>().get(mvaName, mva);
61  return mva.product();
62  }
63 }
64 
66 {
67  public:
70  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
71  mvaReader_(0),
72  mvaInput_(0),
73  category_output_(0)
74  {
75  mvaName_ = cfg.getParameter<std::string>("mvaName");
76  loadMVAfromDB_ = cfg.exists("loadMVAfromDB") ? cfg.getParameter<bool>("loadMVAfromDB") : false;
77  if ( !loadMVAfromDB_ ) {
78  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
79  }
80  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
81  if ( mvaOpt_string == "oldDMwoLT" ) mvaOpt_ = kOldDMwoLT;
82  else if ( mvaOpt_string == "oldDMwLT" ) mvaOpt_ = kOldDMwLT;
83  else if ( mvaOpt_string == "newDMwoLT" ) mvaOpt_ = kNewDMwoLT;
84  else if ( mvaOpt_string == "newDMwLT" ) mvaOpt_ = kNewDMwLT;
85  else throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2")
86  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
87 
88  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) mvaInput_ = new float[6];
89  else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) mvaInput_ = new float[12];
90  else assert(0);
91 
92  TauTransverseImpactParameters_token = consumes<PFTauTIPAssociationByRef>(cfg.getParameter<edm::InputTag>("srcTauTransverseImpactParameters"));
93 
94  ChargedIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcChargedIsoPtSum"));
95  NeutralIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcNeutralIsoPtSum"));
96  PUcorrPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcPUcorrPtSum"));
97 
98  verbosity_ = ( cfg.exists("verbosity") ) ?
99  cfg.getParameter<int>("verbosity") : 0;
100 
101  produces<PFTauDiscriminator>("category");
102  }
103 
104  void beginEvent(const edm::Event&, const edm::EventSetup&);
105 
106  double discriminate(const PFTauRef&) const;
107 
108  void endEvent(edm::Event&);
109 
111  {
112  if(!loadMVAfromDB_) delete mvaReader_;
113  delete[] mvaInput_;
114  for ( std::vector<TFile*>::iterator it = inputFilesToDelete_.begin();
115  it != inputFilesToDelete_.end(); ++it ) {
116  delete (*it);
117  }
118  }
119 
120  private:
121 
123 
128  enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT };
129  int mvaOpt_;
130  float* mvaInput_;
131 
135 
142 
144  std::auto_ptr<PFTauDiscriminator> category_output_;
145 
146  std::vector<TFile*> inputFilesToDelete_;
147 
149 };
150 
152 {
153  if ( !mvaReader_ ) {
154  if ( loadMVAfromDB_ ) {
155  mvaReader_ = loadMVAfromDB(es, mvaName_);
156  } else {
157  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
158  }
159  }
160 
161  evt.getByToken(TauTransverseImpactParameters_token, tauLifetimeInfos);
162 
163  evt.getByToken(ChargedIsoPtSum_token, chargedIsoPtSums_);
164  evt.getByToken(NeutralIsoPtSum_token, neutralIsoPtSums_);
165  evt.getByToken(PUcorrPtSum_token, puCorrPtSums_);
166 
167  evt.getByToken(Tau_token, taus_);
168  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
169 }
170 
172 {
173  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
174  double category = 0.;
175  category_output_->setValue(tauIndex_, category);
176 
177  // CV: computation of MVA value requires presence of leading charged hadron
178  if ( tau->leadPFChargedHadrCand().isNull() ) return 0.;
179 
180  int tauDecayMode = tau->decayMode();
181 
182  if ( ((mvaOpt_ == kOldDMwoLT || mvaOpt_ == kOldDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 10)) ||
183  ((mvaOpt_ == kNewDMwoLT || mvaOpt_ == kNewDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 5 || tauDecayMode == 6 || tauDecayMode == 10)) ) {
184 
185  double chargedIsoPtSum = (*chargedIsoPtSums_)[tau];
186  double neutralIsoPtSum = (*neutralIsoPtSums_)[tau];
187  double puCorrPtSum = (*puCorrPtSums_)[tau];
188 
189  const reco::PFTauTransverseImpactParameter& tauLifetimeInfo = *(*tauLifetimeInfos)[tau];
190 
191  double decayDistX = tauLifetimeInfo.flightLength().x();
192  double decayDistY = tauLifetimeInfo.flightLength().y();
193  double decayDistZ = tauLifetimeInfo.flightLength().z();
194  double decayDistMag = TMath::Sqrt(decayDistX*decayDistX + decayDistY*decayDistY + decayDistZ*decayDistZ);
195 
196  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) {
197  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
198  mvaInput_[1] = TMath::Abs(tau->eta());
199  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
200  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
201  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
202  mvaInput_[5] = tauDecayMode;
203  } else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) {
204  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
205  mvaInput_[1] = TMath::Abs(tau->eta());
206  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
207  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
208  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
209  mvaInput_[5] = tauDecayMode;
210  mvaInput_[6] = TMath::Sign(+1., tauLifetimeInfo.dxy());
211  mvaInput_[7] = TMath::Sqrt(TMath::Abs(TMath::Min(1., tauLifetimeInfo.dxy())));
212  mvaInput_[8] = TMath::Min(10., TMath::Abs(tauLifetimeInfo.dxy_Sig()));
213  mvaInput_[9] = ( tauLifetimeInfo.hasSecondaryVertex() ) ? 1. : 0.;
214  mvaInput_[10] = TMath::Sqrt(decayDistMag);
215  mvaInput_[11] = TMath::Min(10., tauLifetimeInfo.flightLengthSig());
216  }
217 
218  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
219  if ( verbosity_ ) {
220  edm::LogPrint("PFTauDiscByMVAIsol2") << "<PFRecoTauDiscriminationByIsolationMVA2::discriminate>:";
221  edm::LogPrint("PFTauDiscByMVAIsol2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
222  edm::LogPrint("PFTauDiscByMVAIsol2") << " isolation: charged = " << chargedIsoPtSum << ", neutral = " << neutralIsoPtSum << ", PUcorr = " << puCorrPtSum;
223  edm::LogPrint("PFTauDiscByMVAIsol2") << " decay mode = " << tauDecayMode;
224  edm::LogPrint("PFTauDiscByMVAIsol2") << " impact parameter: distance = " << tauLifetimeInfo.dxy() << ", significance = " << tauLifetimeInfo.dxy_Sig();
225  edm::LogPrint("PFTauDiscByMVAIsol2") << " has decay vertex = " << tauLifetimeInfo.hasSecondaryVertex() << ":"
226  << " distance = " << decayDistMag << ", significance = " << tauLifetimeInfo.flightLengthSig();
227  edm::LogPrint("PFTauDiscByMVAIsol2") << "--> mvaValue = " << mvaValue;
228  }
229  return mvaValue;
230  } else {
231  return -1.;
232  }
233 }
234 
236 {
237  // add all category indices to event
238  evt.put(category_output_, "category");
239 }
240 
T getParameter(std::string const &) const
tuple cfg
Definition: looper.py:237
T Sign(T A, T B)
Definition: MathUtil.h:54
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::AssociationVector< reco::PFTauRefProd, std::vector< reco::PFTauTransverseImpactParameterRef > > PFTauTIPAssociationByRef
edm::Handle< reco::PFTauDiscriminator > chargedIsoPtSums_
edm::Handle< PFTauTIPAssociationByRef > tauLifetimeInfos
bool exists(std::string const &parameterName) const
checks if a parameter exists
T Min(T a, T b)
Definition: MathUtil.h:39
edm::EDGetTokenT< reco::PFTauDiscriminator > ChargedIsoPtSum_token
edm::EDGetTokenT< reco::PFTauDiscriminator > NeutralIsoPtSum_token
void beginEvent(const edm::Event &, const edm::EventSetup &)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
T Abs(T a)
Definition: MathUtil.h:49
edm::EDGetTokenT< PFTauTIPAssociationByRef > TauTransverseImpactParameters_token
bool isNull() const
Checks for null.
Definition: Ref.h:247
T Max(T a, T b)
Definition: MathUtil.h:44
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
edm::Handle< reco::PFTauDiscriminator > neutralIsoPtSums_
std::string fullPath() const
Definition: FileInPath.cc:165
moduleLabel_(iConfig.getParameter< string >("@module_label"))
edm::EDGetTokenT< reco::PFTauDiscriminator > PUcorrPtSum_token
tuple loadMVAfromDB
Definition: mvaPFMET_cff.py:41