CMS 3D CMS Logo

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_()
74  {
75  mvaName_ = cfg.getParameter<std::string>("mvaName");
76  loadMVAfromDB_ = cfg.exists("loadMVAfromDB") ? cfg.getParameter<bool>("loadMVAfromDB") : false;
77  if ( !loadMVAfromDB_ ) {
78  if(cfg.exists("inputFileName")){
79  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
80  }else throw cms::Exception("MVA input not defined") << "Requested to load tau MVA input from ROOT file but no file provided in cfg file";
81  }
82  std::string mvaOpt_string = cfg.getParameter<std::string>("mvaOpt");
83  if ( mvaOpt_string == "oldDMwoLT" ) mvaOpt_ = kOldDMwoLT;
84  else if ( mvaOpt_string == "oldDMwLT" ) mvaOpt_ = kOldDMwLT;
85  else if ( mvaOpt_string == "newDMwoLT" ) mvaOpt_ = kNewDMwoLT;
86  else if ( mvaOpt_string == "newDMwLT" ) mvaOpt_ = kNewDMwLT;
87  else throw cms::Exception("PFRecoTauDiscriminationByIsolationMVA2")
88  << " Invalid Configuration Parameter 'mvaOpt' = " << mvaOpt_string << " !!\n";
89 
90  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) mvaInput_ = new float[6];
91  else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) mvaInput_ = new float[12];
92  else assert(0);
93 
94  TauTransverseImpactParameters_token = consumes<PFTauTIPAssociationByRef>(cfg.getParameter<edm::InputTag>("srcTauTransverseImpactParameters"));
95 
96  ChargedIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcChargedIsoPtSum"));
97  NeutralIsoPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcNeutralIsoPtSum"));
98  PUcorrPtSum_token = consumes<reco::PFTauDiscriminator>(cfg.getParameter<edm::InputTag>("srcPUcorrPtSum"));
99 
100  verbosity_ = ( cfg.exists("verbosity") ) ?
101  cfg.getParameter<int>("verbosity") : 0;
102 
103  produces<PFTauDiscriminator>("category");
104  }
105 
106  void beginEvent(const edm::Event&, const edm::EventSetup&);
107 
108  double discriminate(const PFTauRef&) const;
109 
110  void endEvent(edm::Event&);
111 
113  {
114  if(!loadMVAfromDB_) delete mvaReader_;
115  delete[] mvaInput_;
116  for ( std::vector<TFile*>::iterator it = inputFilesToDelete_.begin();
117  it != inputFilesToDelete_.end(); ++it ) {
118  delete (*it);
119  }
120  }
121 
122  private:
123 
125 
130  enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT };
131  int mvaOpt_;
132  float* mvaInput_;
133 
137 
144 
146  std::unique_ptr<PFTauDiscriminator> category_output_;
147 
148  std::vector<TFile*> inputFilesToDelete_;
149 
151 };
152 
154 {
155  if ( !mvaReader_ ) {
156  if ( loadMVAfromDB_ ) {
157  mvaReader_ = loadMVAfromDB(es, mvaName_);
158  } else {
159  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
160  }
161  }
162 
163  evt.getByToken(TauTransverseImpactParameters_token, tauLifetimeInfos);
164 
165  evt.getByToken(ChargedIsoPtSum_token, chargedIsoPtSums_);
166  evt.getByToken(NeutralIsoPtSum_token, neutralIsoPtSums_);
167  evt.getByToken(PUcorrPtSum_token, puCorrPtSums_);
168 
169  evt.getByToken(Tau_token, taus_);
170  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
171 }
172 
174 {
175  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
176  double category = 0.;
177  category_output_->setValue(tauIndex_, category);
178 
179  // CV: computation of MVA value requires presence of leading charged hadron
180  if ( tau->leadPFChargedHadrCand().isNull() ) return 0.;
181 
182  int tauDecayMode = tau->decayMode();
183 
184  if ( ((mvaOpt_ == kOldDMwoLT || mvaOpt_ == kOldDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 10)) ||
185  ((mvaOpt_ == kNewDMwoLT || mvaOpt_ == kNewDMwLT) && (tauDecayMode == 0 || tauDecayMode == 1 || tauDecayMode == 2 || tauDecayMode == 5 || tauDecayMode == 6 || tauDecayMode == 10)) ) {
186 
187  double chargedIsoPtSum = (*chargedIsoPtSums_)[tau];
188  double neutralIsoPtSum = (*neutralIsoPtSums_)[tau];
189  double puCorrPtSum = (*puCorrPtSums_)[tau];
190 
191  const reco::PFTauTransverseImpactParameter& tauLifetimeInfo = *(*tauLifetimeInfos)[tau];
192 
193  double decayDistX = tauLifetimeInfo.flightLength().x();
194  double decayDistY = tauLifetimeInfo.flightLength().y();
195  double decayDistZ = tauLifetimeInfo.flightLength().z();
196  double decayDistMag = TMath::Sqrt(decayDistX*decayDistX + decayDistY*decayDistY + decayDistZ*decayDistZ);
197 
198  if ( mvaOpt_ == kOldDMwoLT || mvaOpt_ == kNewDMwoLT ) {
199  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
200  mvaInput_[1] = TMath::Abs(tau->eta());
201  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
202  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
203  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
204  mvaInput_[5] = tauDecayMode;
205  } else if ( mvaOpt_ == kOldDMwLT || mvaOpt_ == kNewDMwLT ) {
206  mvaInput_[0] = TMath::Log(TMath::Max(1., Double_t(tau->pt())));
207  mvaInput_[1] = TMath::Abs(tau->eta());
208  mvaInput_[2] = TMath::Log(TMath::Max(1.e-2, chargedIsoPtSum));
209  mvaInput_[3] = TMath::Log(TMath::Max(1.e-2, neutralIsoPtSum - 0.125*puCorrPtSum));
210  mvaInput_[4] = TMath::Log(TMath::Max(1.e-2, puCorrPtSum));
211  mvaInput_[5] = tauDecayMode;
212  mvaInput_[6] = TMath::Sign(+1., tauLifetimeInfo.dxy());
213  mvaInput_[7] = TMath::Sqrt(TMath::Abs(TMath::Min(1., tauLifetimeInfo.dxy())));
214  mvaInput_[8] = TMath::Min(10., TMath::Abs(tauLifetimeInfo.dxy_Sig()));
215  mvaInput_[9] = ( tauLifetimeInfo.hasSecondaryVertex() ) ? 1. : 0.;
216  mvaInput_[10] = TMath::Sqrt(decayDistMag);
217  mvaInput_[11] = TMath::Min(10., tauLifetimeInfo.flightLengthSig());
218  }
219 
220  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
221  if ( verbosity_ ) {
222  edm::LogPrint("PFTauDiscByMVAIsol2") << "<PFRecoTauDiscriminationByIsolationMVA2::discriminate>:";
223  edm::LogPrint("PFTauDiscByMVAIsol2") << " tau: Pt = " << tau->pt() << ", eta = " << tau->eta();
224  edm::LogPrint("PFTauDiscByMVAIsol2") << " isolation: charged = " << chargedIsoPtSum << ", neutral = " << neutralIsoPtSum << ", PUcorr = " << puCorrPtSum;
225  edm::LogPrint("PFTauDiscByMVAIsol2") << " decay mode = " << tauDecayMode;
226  edm::LogPrint("PFTauDiscByMVAIsol2") << " impact parameter: distance = " << tauLifetimeInfo.dxy() << ", significance = " << tauLifetimeInfo.dxy_Sig();
227  edm::LogPrint("PFTauDiscByMVAIsol2") << " has decay vertex = " << tauLifetimeInfo.hasSecondaryVertex() << ":"
228  << " distance = " << decayDistMag << ", significance = " << tauLifetimeInfo.flightLengthSig();
229  edm::LogPrint("PFTauDiscByMVAIsol2") << "--> mvaValue = " << mvaValue;
230  }
231  return mvaValue;
232  } else {
233  return -1.;
234  }
235 }
236 
238 {
239  // add all category indices to event
240  evt.put(std::move(category_output_), "category");
241 }
242 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:39
T Sign(T A, T B)
Definition: MathUtil.h:54
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#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 &)
T Abs(T a)
Definition: MathUtil.h:49
edm::EDGetTokenT< PFTauTIPAssociationByRef > TauTransverseImpactParameters_token
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:178
bool isNull() const
Checks for null.
Definition: Ref.h:250
T Max(T a, T b)
Definition: MathUtil.h:44
const T & get() const
Definition: EventSetup.h:55
edm::Handle< reco::PFTauDiscriminator > neutralIsoPtSums_
fixed size matrix
std::string fullPath() const
Definition: FileInPath.cc:184
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< reco::PFTauDiscriminator > PUcorrPtSum_token
def move(src, dest)
Definition: eostools.py:510