CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFTauDiscriminantBase.h
Go to the documentation of this file.
1 #ifndef RecoTauTag_TauTagTools_PFTauDiscriminantBase
2 #define RecoTauTag_TauTagTools_PFTauDiscriminantBase
3 
10 #include "TMath.h"
11 #include "TTree.h"
12 #include <string>
13 #include <algorithm>
14 #include <vector>
15 
16 /* Class: PFTauDiscriminants::Discriminant
17  *
18  * Author: Evan K. Friis, UC Davis friis@physics.ucdavis.edu
19  *
20  * Description:
21  * Abstract base class to hold PFTauDecayMode descriminant functions
22  * is called by a PFTauDiscriminantManager.
23  * Handles TTree branching of the specific requirements of various variables
24  * Can construct CMSSW MVA framework input for a given tau object
25  * Variables can be multiple, or optional. Also supports a "NULL TYPE" for
26  * matching reconstruction failures to reconstruction success.
27  *
28  * Implementing discriminants of type T should inherit from DicriminantBase< TYPE >
29  * where TYPE is either a simple data type (e.g. 'F' for float, 'I':int)
30  * or any complex type for which ROOT dictionaries exists and have a double() operator.
31  * OR any STL collection of said types.
32  * See RecoTauTag/TauTagTools/interface/Discriminants.h for examples of implementation.
33  */
34 
35 
36 namespace PFTauDiscriminants {
37 
38 class PFTauDiscriminantManager;
39 
40 class Discriminant {
41  public:
42  explicit Discriminant(std::string name, std::string rootTypeName, bool branchAsSimpleDataType):
43  discriminantName_(PhysicsTools::AtomicId(name)),
44  rootTypeName_(rootTypeName),
45  branchAsSimpleDataType_(branchAsSimpleDataType)
46  {};
47  virtual ~Discriminant(){};
48  virtual void compute(PFTauDiscriminantManager *input)=0;
50  std::string name() const {return discriminantName_;}
52  std::string rootTypeName() const {return rootTypeName_;}
53 
55  virtual void branchTree(TTree* theTree) = 0;
56  virtual void fillMVA(std::vector<PhysicsTools::Variable::Value>& mvaHolder) const = 0;
57 
58  protected:
60  bool branchSimply() const { return branchAsSimpleDataType_; }
61 
62  private:
64  std::string rootTypeName_;
66 };
67 
68 template<class T>
70  public:
71  explicit DiscriminantBase(std::string name, std::string rootTypeName,
72  bool branchAsSimpleDataType, bool isMultiple, T defaultValue):Discriminant(name, rootTypeName, branchAsSimpleDataType),isMultiple_(isMultiple),defaultValue_(defaultValue){
74  };
75 
76  virtual ~DiscriminantBase(){};
77  typedef typename std::vector<T>::const_iterator myVectorIterator;
78 
79  virtual void setNullResult(PFTauDiscriminantManager *input) //can be overriden in the derived classes if desired (for example, to use edm::Event info)
80  {
81  result_.clear();
83  }
84 
88  {
89  result_.clear();
90 
91  if (input)
92  doComputation(input, result_);
93  else
94  edm::LogError("DiscriminantBase") << "Error in DiscriminantBase - trying to compute discriminants on null PFTauDecayMode pointer!";
95 
96  size_t numberOfResultsReturned = result_.size();
97  if(!numberOfResultsReturned) //if there are no results, ROOT branches of simple variables must be filled w/ the default value
98  {
100  } else
101  {
102  if(!isMultiple_ && numberOfResultsReturned > 1)
103  {
104  edm::LogWarning("PFTauDiscriminants::DiscriminantBase") << "Warning, multiple discriminant values recieved for a non-multiple branch, taking only the first!";
105  }
106  singleResult_ = result_[0];
107  }
108  }
109 
110  //adds a branch to the tree corresponding to this variable
111  void branchTree(TTree* theTree) {
112  if (!this->branchSimply())
113  {
114  edm::LogInfo("PFTauDiscriminantBase") << "Branching TTree: " << theTree->GetName() << " with full class name (bronch)";
115  theTree->Branch(name().c_str(), rootTypeName().c_str(), &resultPtr_);
116  }
117  else
118  {
119  edm::LogInfo("PFTauDiscriminantBase") << "Branching TTree: " << theTree->GetName() << " with struct style branch (leaflist)";
120  std::stringstream branchType;
121  branchType << name() << "/" << rootTypeName(); //eg D, F, I, etc
122  theTree->Branch(this->name().c_str(), &singleResult_, branchType.str().c_str());
123  }
124  }
125  //fills a vector of values for the MVA framework
126  void fillMVA(std::vector<PhysicsTools::Variable::Value>& mvaHolder) const {
127  if (isMultiple_)
128  {
129  for(myVectorIterator aResult = result_.begin(); aResult != result_.end(); ++aResult)
130  {
131  mvaHolder.push_back(PhysicsTools::Variable::Value(theAtomicId(), static_cast<double>(*aResult)));
132  }
133  }
134  else
135  {
136  mvaHolder.push_back(PhysicsTools::Variable::Value(theAtomicId(), static_cast<double>(singleResult_)));
137  }
138  }
139 
140  protected:
141  virtual void doComputation(PFTauDiscriminantManager* input, std::vector<T>& result)=0;
142  private:
146  std::vector<T> result_;
147  std::vector<T>* resultPtr_;
148 };
149 }
150 #endif
void compute(PFTauDiscriminantManager *input)
DiscriminantBase(std::string name, std::string rootTypeName, bool branchAsSimpleDataType, bool isMultiple, T defaultValue)
PhysicsTools::AtomicId theAtomicId() const
PhysicsTools::AtomicId discriminantName_
void branchTree(TTree *theTree)
add a branch to a ttree corresponding to this variable
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:32
Discriminant(std::string name, std::string rootTypeName, bool branchAsSimpleDataType)
tuple result
Definition: query.py:137
virtual void fillMVA(std::vector< PhysicsTools::Variable::Value > &mvaHolder) const =0
virtual void doComputation(PFTauDiscriminantManager *input, std::vector< T > &result)=0
virtual void compute(PFTauDiscriminantManager *input)=0
virtual void branchTree(TTree *theTree)=0
add a branch to a ttree corresponding to this variable
Helper class that can contain an identifier-value pair.
Definition: Variable.h:52
bool branchSimply() const
determines whether or not to use simple struct like branching or custom class branching (e...
virtual void setNullResult(PFTauDiscriminantManager *input)
std::vector< T >::const_iterator myVectorIterator
void fillMVA(std::vector< PhysicsTools::Variable::Value > &mvaHolder) const
long double T
virtual void setNullResult(PFTauDiscriminantManager *input)=0