CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFTauMVADiscriminator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PFTauMVADiscriminator
4 // Class: PFTauMVADiscriminator
5 //
13 //
14 // Original Author: Evan K. Friis, UC Davis (friis@physics.ucdavis.edu)
15 // Created: Fri Aug 15 11:22:14 PDT 2008
16 // $Id: PFTauMVADiscriminator.cc,v 1.4 2010/10/19 21:29:13 wmtan Exp $
17 //
18 //
19 
21 
22 // Tau discriminant computation
26 
27 // PFTauDecayMode data formats
30 
31 // DB access
37 
38 using namespace PFTauDiscriminants;
39 using namespace reco;
40 
42  public:
45 
49  double userCut;
50  };
51 
52  typedef std::vector<MVAComputerFromDB> MVAList;
53  typedef std::map<int, MVAList::iterator> DecayModeToMVAMap;
54 
55  void beginEvent(const edm::Event&, const edm::EventSetup&); // called at the beginning of each event
56  double discriminate(const PFTauRef&); // called on every tau in input collection
57 
58  private:
60  bool remapOutput_; // TMVA defaults output to (-1, 1). Option to remap to (0, 1)
61  bool applyCut_; //Specify whether to output the MVA value, or whether to use
62  // the cuts specified in the DecayMode VPSet specified in the cfg file
63  DecayModeToMVAMap computerMap_; //Maps decay mode to MVA implementation
66  DiscriminantList myDiscriminants_; // collection of functions to compute the discriminants
68 
69  edm::Handle<PFTauDecayModeAssociation> pfTauDecayModes; // edm::Handle to PFTauDecayModes for current event
70 
71  std::vector<PhysicsTools::Variable::Value> mvaComputerInput_;
72 };
73 
75 {
76  pfTauDecayModeSrc_ = iConfig.getParameter<edm::InputTag>("pfTauDecayModeSrc");
77  remapOutput_ = iConfig.getParameter<bool>("RemapOutput");
78  applyCut_ = iConfig.getParameter<bool>("MakeBinaryDecision");
79  prediscriminantFailValue_ = iConfig.getParameter<double>("prefailValue"); //defined in base class
80  dbLabel_ = iConfig.getParameter<std::string>("dbLabel");
81 
82  // build the decaymode->computer map
83  std::vector<edm::ParameterSet> decayModeMap = iConfig.getParameter<std::vector<edm::ParameterSet> >("computers");
84  computers_.reserve(decayModeMap.size());
85  for(std::vector<edm::ParameterSet>::const_iterator iComputer = decayModeMap.begin(); iComputer != decayModeMap.end(); ++iComputer)
86  {
87  MVAComputerFromDB toInsert;
88  toInsert.computerName = iComputer->getParameter<std::string>("computerName");
89  toInsert.userCut = iComputer->getParameter<double>("cut");
91  MVAList::iterator computerJustAdded = computers_.insert(computers_.end(), toInsert); //add this computer to the end of the list
92 
93  //populate the map
94  std::vector<int> associatedDecayModes = iComputer->getParameter<std::vector<int> >("decayModeIndices");
95  for(std::vector<int>::const_iterator iDecayMode = associatedDecayModes.begin();
96  iDecayMode != associatedDecayModes.end();
97  ++iDecayMode)
98  {
99  //map this integer specifying the decay mode to the MVA comptuer we just added to the list
100  std::pair<DecayModeToMVAMap::iterator, bool> insertResult = computerMap_.insert(std::make_pair(*iDecayMode, computerJustAdded));
101 
102  //make sure we aren't double mapping a decay mode
103  if(insertResult.second == false) { //indicates that the current key (decaymode) has already been entered!
104  throw cms::Exception("PFTauMVADiscriminator::ctor") << "A tau decay mode: " << *iDecayMode << " has been mapped to two different MVA implementations, "
105  << insertResult.first->second->computerName << " and " << toInsert.computerName
106  << ". Please check the appropriate cfi file." << std::endl;
107  }
108  }
109  }
110 
112  aDiscriminant != myDiscriminants_.end();
113  ++aDiscriminant)
114  {
115  //load the discriminants into the discriminant manager
116  discriminantManager_.addDiscriminant(*aDiscriminant);
117  }
118 
119 }
120 
122 {
123  for(MVAList::iterator iMVAComputer = computers_.begin(); iMVAComputer != computers_.end(); ++iMVAComputer)
124  {
125  delete iMVAComputer->computer;
126  }
127 }
128 
129 // ------------ method called at the beginning of every event by base class
131 {
132  // load the PFTauDecayModes
134 
135  // expose the event to the PFTau discriminant quantity computers
136  discriminantManager_.setEvent(iEvent, 1.0); //event weight = 1
137 
138  // Refresh MVAs
139  //we do this on each event as the crossing an IOV boundary could change the appropriate record
140  for(MVAList::iterator iMVAComputer = computers_.begin();
141  iMVAComputer != computers_.end();
142  ++iMVAComputer)
143  {
144  std::string nameToGet = iMVAComputer->computerName;
145  iMVAComputer->computer->update<TauMVAFrameworkDBRcd>(dbLabel_.c_str(), iSetup, nameToGet.c_str());
146  }
147 }
148 
150 {
152 
153  mvaComputerInput_.clear();
154  const PFTauDecayMode& theTauDecayMode = (*pfTauDecayModes)[pfTau]; // get PFTauDecayMode associated to this PFTau
155 
156 
157  //get appropriate MVA computer
158  int decayMode = theTauDecayMode.getDecayMode();
159  DecayModeToMVAMap::iterator iterToComputer = computerMap_.find(decayMode);
160 
161  if(iterToComputer != computerMap_.end()) //if we don't have a MVA mapped to this decay mode, skip it.
162  {
163  const PhysicsTools::MVAComputerCache* mvaComputer = iterToComputer->second->computer;
164  if ( (*mvaComputer) )
165  {
166  //sets the current tau decay mode as the active object
167  discriminantManager_.setTau(theTauDecayMode);
168  //applies associated discriminants (see ctor) and constructs the appropriate MVA framework input
170  result = (*mvaComputer)->eval(mvaComputerInput_);
171  }
172  else
173  {
174  edm::LogWarning("PFTauMVADiscriminator") << "Warning: got a null pointer to MVA computer in conditions database"
175  << " for decay mode: " << decayMode << ", expected MVA computer name: "
176  << iterToComputer->second->computerName;
177  }
178 
179  if (remapOutput_) // TMVA maps result to [-1, 1]. Remap, if desired, to [0, 1]
180  {
181  if (result > 1) result = 1.;
182  else if (result < -1) result = 0.;
183  else {
184  result += 1.;
185  result /= 2.;
186  }
187  }
188  if (applyCut_)
189  {
190  //If the user desires a yes or no decision,
191  // use the supplied cut to make a decision
192  if (result > iterToComputer->second->userCut)
193  result = 1.0;
194  else
195  result = 0.0;
196  }
197  }
198 
199  return result;
200 }
201 
202 
203 //define this as a plug-in
T getParameter(std::string const &) const
DiscriminantList myDiscriminants_
void addDiscriminant(Discriminant *const aDiscriminant)
add a discriminant
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool setTau(const reco::PFTauDecayMode &theTau, bool prePass=false, bool preFail=false)
set objects for this discriminant
void beginEvent(const edm::Event &, const edm::EventSetup &)
int iEvent
Definition: GenABIO.cc:243
hadronicTauDecayModes getDecayMode() const
void setEvent(const edm::Event &, double eventWeight)
set the current event. Must be called (once per event) before setTau or setNullResult ...
std::vector< PhysicsTools::Variable::Value > mvaComputerInput_
edm::Handle< PFTauDecayModeAssociation > pfTauDecayModes
tuple result
Definition: query.py:137
PFTauMVADiscriminator(const edm::ParameterSet &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
std::vector< MVAComputerFromDB > MVAList
const_iterator begin()
iterators over the list
DecayModeToMVAMap computerMap_
collection::const_iterator const_iterator
PFTauDiscriminantManager discriminantManager_
Creates and and MVAComputer from calibrations via EventSetup.
double discriminate(const PFTauRef &)
std::map< int, MVAList::iterator > DecayModeToMVAMap
PhysicsTools::MVAComputerCache * computer
void buildMVAComputerLink(std::vector< PhysicsTools::Variable::Value > &)
connect to an MVA computer