CMS 3D CMS Logo

PATTauDiscriminantCutMultiplexer.cc
Go to the documentation of this file.
1 /*
2  * \class PATTauDiscriminantCutMultiplexer
3  *
4  * Takes two PFTauDiscriminators.
5  *
6  * The "key" discriminantor is rounded to the nearest integer.
7  *
8  * A set of cuts for different keys on the "toMultiplex" discriminantor is
9  * provided in the config file.
10  *
11  * Both the key and toMultiplex discriminators should map to the same PFTau
12  * collection.
13  *
14  * Adopted from RecoTauTag/RecoTau/plugins/RecoTauDiscriminantCutMultiplexer.cc
15  *
16  * \author Alexander Nehrkorn, RWTH Aachen
17  */
18 
19 #include <boost/foreach.hpp>
25 
28 
33 
34 #include "TMath.h"
35 #include "TGraph.h"
36 #include "TFormula.h"
37 #include "TFile.h"
38 
40 {
41  public:
43 
45  double discriminate(const pat::TauRef&) const override;
46  void beginEvent(const edm::Event& event, const edm::EventSetup& eventSetup) override;
47 
48  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
49  private:
51 
54 
56  {
58  : cutVariable_(),
59  cutFunction_(),
61  {}
63  {
64  }
65  double cutValue_;
67  std::unique_ptr<StringObjectFunction<pat::Tau>> cutVariable_;
68  std::unique_ptr<const TGraph> cutFunction_;
70  int mode_;
71  };
72  typedef std::map<int, std::unique_ptr<DiscriminantCutEntry>> DiscriminantCutMap;
73  DiscriminantCutMap cuts_;
74 
76  std::unique_ptr<const TFormula> mvaOutput_normalization_;
77 
79 
86 
88 };
89 
90 namespace
91 {
92  std::unique_ptr<TFile> openInputFile(const edm::FileInPath& inputFileName) {
93  if ( inputFileName.location() == edm::FileInPath::Unknown){ throw cms::Exception("PATTauDiscriminantCutMultiplexer::loadObjectFromFile")
94  << " Failed to find File = " << inputFileName << " !!\n";
95  }
96  return std::unique_ptr<TFile>{ new TFile(inputFileName.fullPath().data()) };
97  }
98 
99  template <typename T>
100  std::unique_ptr<const T> loadObjectFromFile(TFile& inputFile, const std::string& objectName)
101  {
102  const T* object = dynamic_cast<T*>(inputFile.Get(objectName.data()));
103  if ( !object )
104  throw cms::Exception("PATTauDiscriminantCutMultiplexer::loadObjectFromFile")
105  << " Failed to load Object = " << objectName.data() << " from file = " << inputFile.GetName() << " !!\n";
106  //Need to use TObject::Clone since the type T might be a base class
107  return std::unique_ptr<const T>{ static_cast<T*>(object->Clone()) };
108  }
109 
110  std::unique_ptr<const TGraph> loadTGraphFromDB(const edm::EventSetup& es, const std::string& graphName, const int& verbosity_ = 0)
111  {
112  if(verbosity_){
113  std::cout << "<loadTGraphFromDB>:" << std::endl;
114  std::cout << " graphName = " << graphName << std::endl;
115  }
117  es.get<PhysicsTGraphPayloadRcd>().get(graphName, graphPayload);
118  return std::unique_ptr<const TGraph>{ new TGraph(*graphPayload.product()) };
119  }
120 
121  std::unique_ptr<TFormula> loadTFormulaFromDB(const edm::EventSetup& es, const std::string& formulaName, const TString& newName, const int& verbosity_ = 0)
122  {
123  if(verbosity_){
124  std::cout << "<loadTFormulaFromDB>:" << std::endl;
125  std::cout << " formulaName = " << formulaName << std::endl;
126  }
128  es.get<PhysicsTFormulaPayloadRcd>().get(formulaName, formulaPayload);
129 
130  if ( formulaPayload->formulas().size() == 1 && formulaPayload->limits().size() == 1 ) {
131  return std::unique_ptr<TFormula> {new TFormula(newName, formulaPayload->formulas().at(0).data()) };
132  } else {
133  throw cms::Exception("PATTauDiscriminantCutMultiplexer::loadTFormulaFromDB")
134  << "Failed to load TFormula = " << formulaName << " from Database !!\n";
135  }
136  return std::unique_ptr<TFormula>{};
137  }
138 }
139 
142  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
145 {
146 
147  toMultiplex_ = cfg.getParameter<edm::InputTag>("toMultiplex");
148  toMultiplex_token = consumes<pat::PATTauDiscriminator>(toMultiplex_);
149  key_ = cfg.getParameter<edm::InputTag>("key");
150  key_token = consumes<pat::PATTauDiscriminator>(key_);
151 
152  verbosity_ = cfg.getParameter<int>("verbosity");
153  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
154  if ( !loadMVAfromDB_ ) {
155  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
156  }
157  if(verbosity_) std::cout << moduleLabel_ << " loadMVA = " << loadMVAfromDB_ << std::endl;
158  mvaOutputNormalizationName_ = cfg.getParameter<std::string>("mvaOutput_normalization"); // default value is "" which should just overwrite existing value with same empty string
159 
160  // Setup our cut map
161  typedef std::vector<edm::ParameterSet> VPSet;
162  VPSet mapping = cfg.getParameter<VPSet>("mapping");
163  for ( VPSet::const_iterator mappingEntry = mapping.begin();
164  mappingEntry != mapping.end(); ++mappingEntry ) {
165  unsigned category = mappingEntry->getParameter<uint32_t>("category");
166  std::unique_ptr<DiscriminantCutEntry> cut{new DiscriminantCutEntry()};
167  if ( mappingEntry->existsAs<double>("cut") ) {
168  cut->cutValue_ = mappingEntry->getParameter<double>("cut");
170  } else if ( mappingEntry->existsAs<std::string>("cut") ) {
171  cut->cutName_ = mappingEntry->getParameter<std::string>("cut");
172  std::string cutVariable_string = mappingEntry->getParameter<std::string>("variable");
173  cut->cutVariable_.reset( new StringObjectFunction<pat::Tau>(cutVariable_string) );
175  } else {
176  throw cms::Exception("PATTauDiscriminantCutMultiplexer")
177  << " Undefined Configuration Parameter 'cut' !!\n";
178  }
180  }
181  verbosity_ = cfg.getParameter<int>("verbosity");
182  if(verbosity_) std::cout << "constructed " << moduleLabel_ << std::endl;
183 }
184 
186 {
187 }
188 
190 {
191  if(verbosity_) std::cout << " begin! " << moduleLabel_ << " " << isInitialized_ << std::endl;
192  if ( !isInitialized_ ) {
193  //Only open the file once and we can close it when this routine is done
194  // since all objects gotten from the file will have been copied
195  std::unique_ptr<TFile> inputFile;
196  if ( !mvaOutputNormalizationName_.empty() ) {
197  if ( !loadMVAfromDB_ ) {
198  inputFile = openInputFile(inputFileName_);
199  mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*inputFile, mvaOutputNormalizationName_);
200  } else {
201  auto temp = loadTFormulaFromDB(es, mvaOutputNormalizationName_, Form("%s_mvaOutput_normalization", moduleLabel_.data()), verbosity_);
203  }
204  }
205  for ( DiscriminantCutMap::iterator cut = cuts_.begin();
206  cut != cuts_.end(); ++cut ) {
207  if ( cut->second->mode_ == DiscriminantCutEntry::kVariableCut ) {
208  if ( !loadMVAfromDB_ ) {
209  if(not inputFile) {
210  inputFile = openInputFile(inputFileName_);
211  }
212  if(verbosity_) std::cout << "Loading from file" << inputFileName_ << std::endl;
213  cut->second->cutFunction_ = loadObjectFromFile<TGraph>(*inputFile, cut->second->cutName_);
214  } else {
215  if(verbosity_) std::cout << "Loading from DB" << std::endl;
216  cut->second->cutFunction_ = loadTGraphFromDB(es, cut->second->cutName_, verbosity_);
217  }
218  }
219  }
220  isInitialized_ = true;
221  }
222 
225 }
226 
227 double
229 {
230  if ( verbosity_ ) {
231  std::cout << "<PATTauDiscriminantCutMultiplexer::discriminate>:" << std::endl;
232  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
233  }
234 
235  double disc_result = (*toMultiplexHandle_)[tau];
236  if ( verbosity_ ) {
237  std::cout << "disc_result = " << disc_result << std::endl;
238  }
239  if ( mvaOutput_normalization_ ) {
240  disc_result = mvaOutput_normalization_->Eval(disc_result);
241  //if ( disc_result > 1. ) disc_result = 1.;
242  //if ( disc_result < 0. ) disc_result = 0.;
243  if ( verbosity_ ) {
244  std::cout << "disc_result (normalized) = " << disc_result << std::endl;
245  }
246  }
247  double key_result = (*keyHandle_)[tau];
248  DiscriminantCutMap::const_iterator cutIter = cuts_.find(TMath::Nint(key_result));
249 
250 
251  // Return null if it doesn't exist
252  if ( cutIter == cuts_.end() ) {
254  }
255  // See if the discriminator passes our cuts
256  bool passesCuts = false;
257  if ( cutIter->second->mode_ == DiscriminantCutEntry::kFixedCut ) {
258  passesCuts = (disc_result > cutIter->second->cutValue_);
259  if ( verbosity_ ) {
260  std::cout << "cutValue (fixed) = " << cutIter->second->cutValue_ << " --> passesCuts = " << passesCuts << std::endl;
261  }
262  } else if ( cutIter->second->mode_ == DiscriminantCutEntry::kVariableCut ) {
263  double cutVariable = (*cutIter->second->cutVariable_)(*tau);
264  double xMin, xMax, dummy;
265  cutIter->second->cutFunction_->GetPoint(0, xMin, dummy);
266  cutIter->second->cutFunction_->GetPoint(cutIter->second->cutFunction_->GetN() - 1, xMax, dummy);
267  const double epsilon = 1.e-3;
268  if ( cutVariable < (xMin + epsilon) ) cutVariable = xMin + epsilon;
269  else if ( cutVariable > (xMax - epsilon) ) cutVariable = xMax - epsilon;
270  double cutValue = cutIter->second->cutFunction_->Eval(cutVariable);
271  passesCuts = (disc_result > cutValue);
272  if ( verbosity_ ) {
273  std::cout << "cutValue (@" << cutVariable << ") = " << cutValue << " --> passesCuts = " << passesCuts << std::endl;
274  }
275  } else assert(0);
276 
277  return passesCuts;
278 }
279 
280 void
282  // patTauDiscriminantCutMultiplexer
284  desc.add<edm::InputTag>("toMultiplex", edm::InputTag("fixme"));
285  desc.add<int>("verbosity", 0);
286  {
287  edm::ParameterSetDescription desc_mapping;
288  desc_mapping.add<unsigned int>("category");
289  desc_mapping.setAllowAnything(); //option cut can be double or (string plus additional option variable)
290 
291  std::vector<edm::ParameterSet> vpsd_mapping;
292  edm::ParameterSet psd1;
293  psd1.addParameter<unsigned int>("category", 0);
294  psd1.addParameter<double>("cut", 0.5);
295  vpsd_mapping.push_back(psd1);
296  edm::ParameterSet psd2;
297  psd2.addParameter<unsigned int>("category", 1);
298  psd2.addParameter<double>("cut", 0.2);
299  vpsd_mapping.push_back(psd2);
300  desc.addVPSet("mapping", desc_mapping, vpsd_mapping);
301  }
302  desc.add<edm::FileInPath>("inputFileName", edm::FileInPath("RecoTauTag/RecoTau/data/emptyMVAinputFile"));
303  desc.add<bool>("loadMVAfromDB", true);
304  fillProducerDescriptions(desc); // inherited from the base
305  desc.add<std::string>("mvaOutput_normalization", "");
306  desc.add<edm::InputTag>("key", edm::InputTag("fixme"));
307  descriptions.add("patTauDiscriminantCutMultiplexer", desc);
308 }
309 
T getParameter(std::string const &) const
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
std::unique_ptr< const TFormula > mvaOutput_normalization_
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void setAllowAnything()
allow any parameter label/value pairs
PATTauDiscriminantCutMultiplexer(const edm::ParameterSet &pset)
std::map< int, std::unique_ptr< DiscriminantCutEntry > > DiscriminantCutMap
std::unique_ptr< StringObjectFunction< pat::Tau > > cutVariable_
edm::EDGetTokenT< pat::PATTauDiscriminator > key_token
string newName
Definition: mps_merge.py:86
const std::vector< std::string > & formulas() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillProducerDescriptions(edm::ParameterSetDescription &desc)
edm::EDGetTokenT< pat::PATTauDiscriminator > toMultiplex_token
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:125
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:161
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::Handle< pat::PATTauDiscriminator > toMultiplexHandle_
edm::Handle< pat::PATTauDiscriminator > keyHandle_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double discriminate(const pat::TauRef &) const override
T get() const
Definition: EventSetup.h:71
std::string fullPath() const
Definition: FileInPath.cc:163
const std::vector< std::pair< float, float > > & limits() const
long double T
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1