test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
24 #include "TMath.h"
25 #include "TGraph.h"
26 #include "TFormula.h"
27 #include "TFile.h"
28 
30 {
31  public:
33 
35  double discriminate(const reco::PFTauRef&) override;
36  void beginEvent(const edm::Event& event, const edm::EventSetup& eventSetup) override;
37 
38  private:
40 
42  {
44  : cutVariable_(0),
45  cutFunction_(0),
47  {}
49  {
50  delete cutVariable_;
51  delete cutFunction_;
52  }
53  double cutValue_;
55  const TGraph* cutFunction_;
57  int mode_;
58  };
59  typedef std::map<int, DiscriminantCutEntry*> DiscriminantCutMap;
61 
68  const TFormula* mvaOutput_normalization_;
69  std::vector<TFile*> inputFilesToDelete_;
70 
72 };
73 
74 namespace
75 {
76  template <typename T>
77  const T* loadObjectFromFile(const edm::FileInPath& inputFileName, const std::string& objectName, std::vector<TFile*>& inputFilesToDelete)
78  {
79  if ( inputFileName.location() == edm::FileInPath::Unknown) throw cms::Exception("RecoTauDiscriminantCutMultiplexer::loadObjectFromFile")
80  << " Failed to find File = " << inputFileName << " !!\n";
81  TFile* inputFile = new TFile(inputFileName.fullPath().data());
82 
83  const T* object = dynamic_cast<T*>(inputFile->Get(objectName.data()));
84  if ( !object )
85  throw cms::Exception("RecoTauDiscriminantCutMultiplexer::loadObjectFromFile")
86  << " Failed to load Object = " << objectName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
87 
88  inputFilesToDelete.push_back(inputFile);
89 
90  return object;
91  }
92 }
93 
96  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
97  mvaOutput_normalization_(0)
98 {
99  toMultiplex_ = cfg.getParameter<edm::InputTag>("toMultiplex");
100  toMultiplex_token = consumes<reco::PFTauDiscriminator>(toMultiplex_);
101  key_ = cfg.getParameter<edm::InputTag>("key");
102  key_token = consumes<reco::PFTauDiscriminator>(key_);
103 
104  if ( cfg.exists("mvaOutput_normalization" ) ) {
106  std::string mvaOutput_normalization_string = cfg.getParameter<std::string>("mvaOutput_normalization");
107  mvaOutput_normalization_ = loadObjectFromFile<TFormula>(inputFileName, mvaOutput_normalization_string, inputFilesToDelete_);
108  }
109 
110  // Setup our cut map
111  typedef std::vector<edm::ParameterSet> VPSet;
112  VPSet mapping = cfg.getParameter<VPSet>("mapping");
113  for ( VPSet::const_iterator mappingEntry = mapping.begin();
114  mappingEntry != mapping.end(); ++mappingEntry ) {
115  unsigned category = mappingEntry->getParameter<uint32_t>("category");
117  if ( mappingEntry->existsAs<double>("cut") ) {
118  cut->cutValue_ = mappingEntry->getParameter<double>("cut");
119  cut->mode_ = DiscriminantCutEntry::kFixedCut;
120  } else if ( mappingEntry->existsAs<std::string>("cut") ) {
121  std::string cut_string = mappingEntry->getParameter<std::string>("cut");
123  cut->cutFunction_ = loadObjectFromFile<TGraph>(inputFileName, cut_string, inputFilesToDelete_);
124  std::string cutVariable_string = mappingEntry->getParameter<std::string>("variable");
125  cut->cutVariable_ = new StringObjectFunction<reco::PFTau>(cutVariable_string.data());
127  } else {
128  throw cms::Exception("RecoTauDiscriminantCutMultiplexer")
129  << " Undefined Configuration Parameter 'cut' !!\n";
130  }
131  cuts_[category] = cut;
132  }
133 
134  verbosity_ = ( cfg.exists("verbosity") ) ?
135  cfg.getParameter<int>("verbosity") : 0;
136 }
137 
139 {
140  for ( std::map<int, DiscriminantCutEntry*>::iterator it = cuts_.begin();
141  it != cuts_.end(); ++it ) {
142  delete it->second;
143  }
144  // CV: all entries in inputFilesToDelete list actually refer to the same file
145  // --> delete the first entry only
146  if ( inputFilesToDelete_.size() >= 1 ) {
147  delete inputFilesToDelete_.front();
148  }
149 }
150 
152 {
155 }
156 
157 double
159 {
160  if ( verbosity_ ) {
161  std::cout << "<RecoTauDiscriminantCutMultiplexer::discriminate>:" << std::endl;
162  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
163  }
164 
165  double disc_result = (*toMultiplexHandle_)[tau];
166  if ( verbosity_ ) {
167  std::cout << "disc_result = " << disc_result << std::endl;
168  }
169  if ( mvaOutput_normalization_ ) {
170  disc_result = mvaOutput_normalization_->Eval(disc_result);
171  //if ( disc_result > 1. ) disc_result = 1.;
172  //if ( disc_result < 0. ) disc_result = 0.;
173  if ( verbosity_ ) {
174  std::cout << "disc_result (normalized) = " << disc_result << std::endl;
175  }
176  }
177  double key_result = (*keyHandle_)[tau];
178  DiscriminantCutMap::const_iterator cutIter = cuts_.find(TMath::Nint(key_result));
179 
180  // Return null if it doesn't exist
181  if ( cutIter == cuts_.end() ) {
183  }
184 
185  // See if the discriminator passes our cuts
186  bool passesCuts = false;
187  if ( cutIter->second->mode_ == DiscriminantCutEntry::kFixedCut ) {
188  passesCuts = (disc_result > cutIter->second->cutValue_);
189  if ( verbosity_ ) {
190  std::cout << "cutValue (fixed) = " << cutIter->second->cutValue_ << " --> passesCuts = " << passesCuts << std::endl;
191  }
192  } else if ( cutIter->second->mode_ == DiscriminantCutEntry::kVariableCut ) {
193  double cutVariable = (*cutIter->second->cutVariable_)(*tau);
194  double xMin, xMax, dummy;
195  cutIter->second->cutFunction_->GetPoint(0, xMin, dummy);
196  cutIter->second->cutFunction_->GetPoint(cutIter->second->cutFunction_->GetN() - 1, xMax, dummy);
197  const double epsilon = 1.e-3;
198  if ( cutVariable < (xMin + epsilon) ) cutVariable = xMin + epsilon;
199  else if ( cutVariable > (xMax - epsilon) ) cutVariable = xMax - epsilon;
200  double cutValue = cutIter->second->cutFunction_->Eval(cutVariable);
201  passesCuts = (disc_result > cutValue);
202  if ( verbosity_ ) {
203  std::cout << "cutValue (@" << cutVariable << ") = " << cutValue << " --> passesCuts = " << passesCuts << std::endl;
204  }
205  } else assert(0);
206 
207  return passesCuts;
208 }
209 
T getParameter(std::string const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
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_
edm::EDGetTokenT< reco::PFTauDiscriminator > toMultiplex_token
edm::EDGetTokenT< reco::PFTauDiscriminator > key_token
std::map< int, DiscriminantCutEntry * > DiscriminantCutMap
double discriminate(const reco::PFTauRef &) override
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
list object
Definition: dbtoconf.py:77
RecoTauDiscriminantCutMultiplexer(const edm::ParameterSet &pset)
tuple cout
Definition: gather_cfg.py:121
const double epsilon
std::string fullPath() const
Definition: FileInPath.cc:165
moduleLabel_(iConfig.getParameter< string >("@module_label"))
long double T