CMS 3D CMS Logo

RecoTauDiscriminantCutMultiplexer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauDiscriminantCutMultiplexer
3  *
4  * Author: Evan K. Friis, UW
5  *
6  * Takes two PFTauDiscriminators.
7  *
8  * The "key" discriminantor is rounded to the nearest integer.
9  *
10  * A set of cuts for different keys on the "toMultiplex" discriminantor is
11  * provided in the config file.
12  *
13  * Both the key and toMultiplex discriminators should map to the same PFTau
14  * collection.
15  *
16  */
17 #include <boost/foreach.hpp>
23 
28 
29 #include "TMath.h"
30 #include "TGraph.h"
31 #include "TFormula.h"
32 #include "TFile.h"
33 
35 {
36  public:
38 
40  double discriminate(const reco::PFTauRef&) const override;
41  void beginEvent(const edm::Event& event, const edm::EventSetup& eventSetup) override;
42 
43  private:
45 
48 
50  {
52  : cutVariable_(),
53  cutFunction_(),
55  {}
57  {
58  }
59  double cutValue_;
61  std::unique_ptr<StringObjectFunction<reco::PFTau>> cutVariable_;
62  std::unique_ptr<const TGraph> cutFunction_;
64  int mode_;
65  };
66  typedef std::map<int, std::unique_ptr<DiscriminantCutEntry>> DiscriminantCutMap;
67  DiscriminantCutMap cuts_;
68 
70  std::unique_ptr<const TFormula> mvaOutput_normalization_;
71 
73 
80 
82 };
83 
84 namespace
85 {
86  std::unique_ptr<TFile> openInputFile(const edm::FileInPath& inputFileName) {
87  if ( inputFileName.location() == edm::FileInPath::Unknown){ throw cms::Exception("RecoTauDiscriminantCutMultiplexer::loadObjectFromFile")
88  << " Failed to find File = " << inputFileName << " !!\n";
89  }
90  return std::unique_ptr<TFile>{ new TFile(inputFileName.fullPath().data()) };
91  }
92 
93  template <typename T>
94  std::unique_ptr<const T> loadObjectFromFile(TFile& inputFile, const std::string& objectName)
95  {
96  const T* object = dynamic_cast<T*>(inputFile.Get(objectName.data()));
97  if ( !object )
98  throw cms::Exception("RecoTauDiscriminantCutMultiplexer::loadObjectFromFile")
99  << " Failed to load Object = " << objectName.data() << " from file = " << inputFile.GetName() << " !!\n";
100  //Need to use TObject::Clone since the type T might be a base class
101  std::unique_ptr<const T> copy{ static_cast<T*>(object->Clone()) };
102 
103  return std::move(copy);
104  }
105 
106  std::unique_ptr<const TGraph> loadTGraphFromDB(const edm::EventSetup& es, const std::string& graphName, const int& verbosity_ = 0)
107  {
108  if(verbosity_){
109  std::cout << "<loadTGraphFromDB>:" << std::endl;
110  std::cout << " graphName = " << graphName << std::endl;
111  }
113  es.get<PhysicsTGraphPayloadRcd>().get(graphName, graphPayload);
114  return std::unique_ptr<const TGraph>{ new TGraph(*graphPayload.product()) };
115  }
116 
117  std::unique_ptr<TFormula> loadTFormulaFromDB(const edm::EventSetup& es, const std::string& formulaName, const TString& newName, const int& verbosity_ = 0)
118  {
119  if(verbosity_){
120  std::cout << "<loadTFormulaFromDB>:" << std::endl;
121  std::cout << " formulaName = " << formulaName << std::endl;
122  }
124  es.get<PhysicsTFormulaPayloadRcd>().get(formulaName, formulaPayload);
125 
126  if ( formulaPayload->formulas().size() == 1 && formulaPayload->limits().size() == 1 ) {
127  return std::unique_ptr<TFormula> {new TFormula(newName, formulaPayload->formulas().at(0).data()) };
128  } else {
129  throw cms::Exception("RecoTauDiscriminantCutMultiplexer::loadTFormulaFromDB")
130  << "Failed to load TFormula = " << formulaName << " from Database !!\n";
131  }
132  return std::unique_ptr<TFormula>{};
133  }
134 }
135 
138  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
141 {
142 
143  toMultiplex_ = cfg.getParameter<edm::InputTag>("toMultiplex");
144  toMultiplex_token = consumes<reco::PFTauDiscriminator>(toMultiplex_);
145  key_ = cfg.getParameter<edm::InputTag>("key");
146  key_token = consumes<reco::PFTauDiscriminator>(key_);
147 
148  verbosity_ = ( cfg.exists("verbosity") ) ?
149  cfg.getParameter<int>("verbosity") : 0;
150 
151  loadMVAfromDB_ = cfg.exists("loadMVAfromDB") ? cfg.getParameter<bool>("loadMVAfromDB") : false;
152  if ( !loadMVAfromDB_ ) {
153  if(cfg.exists("inputFileName")){
154  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
155  }else throw cms::Exception("MVA input not defined") << "Requested to load tau MVA input from ROOT file but no file provided in cfg file";
156  }
157  if(verbosity_) std::cout << moduleLabel_ << " loadMVA = " << loadMVAfromDB_ << std::endl;
158  if ( cfg.exists("mvaOutput_normalization") ) {
159  mvaOutputNormalizationName_ = cfg.getParameter<std::string>("mvaOutput_normalization");
160  }
161 
162  // Setup our cut map
163  typedef std::vector<edm::ParameterSet> VPSet;
164  VPSet mapping = cfg.getParameter<VPSet>("mapping");
165  for ( VPSet::const_iterator mappingEntry = mapping.begin();
166  mappingEntry != mapping.end(); ++mappingEntry ) {
167  unsigned category = mappingEntry->getParameter<uint32_t>("category");
168  std::unique_ptr<DiscriminantCutEntry> cut{new DiscriminantCutEntry()};
169  if ( mappingEntry->existsAs<double>("cut") ) {
170  cut->cutValue_ = mappingEntry->getParameter<double>("cut");
172  } else if ( mappingEntry->existsAs<std::string>("cut") ) {
173  cut->cutName_ = mappingEntry->getParameter<std::string>("cut");
174  std::string cutVariable_string = mappingEntry->getParameter<std::string>("variable");
175  cut->cutVariable_.reset( new StringObjectFunction<reco::PFTau>(cutVariable_string) );
177  } else {
178  throw cms::Exception("RecoTauDiscriminantCutMultiplexer")
179  << " Undefined Configuration Parameter 'cut' !!\n";
180  }
182  }
183 
184  if(verbosity_) std::cout << "constructed " << moduleLabel_ << std::endl;
185 }
186 
188 {
189 }
190 
192 {
193  if(verbosity_) std::cout << " begin! " << moduleLabel_ << " " << isInitialized_ << std::endl;
194  if ( !isInitialized_ ) {
195  //Only open the file once and we can close it when this routine is done
196  // since all objects gotten from the file will have been copied
197  std::unique_ptr<TFile> inputFile;
198  if ( !mvaOutputNormalizationName_.empty() ) {
199  if ( !loadMVAfromDB_ ) {
200  inputFile = openInputFile(inputFileName_);
201  mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*inputFile, mvaOutputNormalizationName_);
202  } else {
203  auto temp = loadTFormulaFromDB(es, mvaOutputNormalizationName_, Form("%s_mvaOutput_normalization", moduleLabel_.data()), verbosity_);
205  }
206  }
207  for ( DiscriminantCutMap::iterator cut = cuts_.begin();
208  cut != cuts_.end(); ++cut ) {
209  if ( cut->second->mode_ == DiscriminantCutEntry::kVariableCut ) {
210  if ( !loadMVAfromDB_ ) {
211  if(not inputFile) {
212  inputFile = openInputFile(inputFileName_);
213  }
214  if(verbosity_) std::cout << "Loading from file" << inputFileName_ << std::endl;
215  cut->second->cutFunction_ = loadObjectFromFile<TGraph>(*inputFile, cut->second->cutName_);
216  } else {
217  if(verbosity_) std::cout << "Loading from DB" << std::endl;
218  cut->second->cutFunction_ = loadTGraphFromDB(es, cut->second->cutName_, verbosity_);
219  }
220  }
221  }
222  isInitialized_ = true;
223  }
224 
227 }
228 
229 double
231 {
232  if ( verbosity_ ) {
233  std::cout << "<RecoTauDiscriminantCutMultiplexer::discriminate>:" << std::endl;
234  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
235  }
236 
237  double disc_result = (*toMultiplexHandle_)[tau];
238  if ( verbosity_ ) {
239  std::cout << "disc_result = " << disc_result << std::endl;
240  }
241  if ( mvaOutput_normalization_ ) {
242  disc_result = mvaOutput_normalization_->Eval(disc_result);
243  //if ( disc_result > 1. ) disc_result = 1.;
244  //if ( disc_result < 0. ) disc_result = 0.;
245  if ( verbosity_ ) {
246  std::cout << "disc_result (normalized) = " << disc_result << std::endl;
247  }
248  }
249  double key_result = (*keyHandle_)[tau];
250  DiscriminantCutMap::const_iterator cutIter = cuts_.find(TMath::Nint(key_result));
251 
252 
253  // Return null if it doesn't exist
254  if ( cutIter == cuts_.end() ) {
256  }
257  // See if the discriminator passes our cuts
258  bool passesCuts = false;
259  if ( cutIter->second->mode_ == DiscriminantCutEntry::kFixedCut ) {
260  passesCuts = (disc_result > cutIter->second->cutValue_);
261  if ( verbosity_ ) {
262  std::cout << "cutValue (fixed) = " << cutIter->second->cutValue_ << " --> passesCuts = " << passesCuts << std::endl;
263  }
264  } else if ( cutIter->second->mode_ == DiscriminantCutEntry::kVariableCut ) {
265  double cutVariable = (*cutIter->second->cutVariable_)(*tau);
266  double xMin, xMax, dummy;
267  cutIter->second->cutFunction_->GetPoint(0, xMin, dummy);
268  cutIter->second->cutFunction_->GetPoint(cutIter->second->cutFunction_->GetN() - 1, xMax, dummy);
269  const double epsilon = 1.e-3;
270  if ( cutVariable < (xMin + epsilon) ) cutVariable = xMin + epsilon;
271  else if ( cutVariable > (xMax - epsilon) ) cutVariable = xMax - epsilon;
272  double cutValue = cutIter->second->cutFunction_->Eval(cutVariable);
273  passesCuts = (disc_result > cutValue);
274  if ( verbosity_ ) {
275  std::cout << "cutValue (@" << cutVariable << ") = " << cutValue << " --> passesCuts = " << passesCuts << std::endl;
276  }
277  } else assert(0);
278 
279  return passesCuts;
280 }
281 
T getParameter(std::string const &) const
std::unique_ptr< const TFormula > mvaOutput_normalization_
def copy(args, dbName)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
edm::Handle< reco::PFTauDiscriminator > keyHandle_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup) override
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::Handle< reco::PFTauDiscriminator > toMultiplexHandle_
string newName
Definition: mps_merge.py:84
std::unique_ptr< StringObjectFunction< reco::PFTau > > cutVariable_
const std::vector< std::string > & formulas() const
edm::EDGetTokenT< reco::PFTauDiscriminator > toMultiplex_token
double discriminate(const reco::PFTauRef &) const override
edm::EDGetTokenT< reco::PFTauDiscriminator > key_token
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:191
T get() const
Definition: EventSetup.h:63
RecoTauDiscriminantCutMultiplexer(const edm::ParameterSet &pset)
std::string fullPath() const
Definition: FileInPath.cc:197
const std::vector< std::pair< float, float > > & limits() const
std::map< int, std::unique_ptr< DiscriminantCutEntry > > DiscriminantCutMap
long double T
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510
Definition: event.py:1