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