CMS 3D CMS Logo

PFTauDiscriminantBase.h

Go to the documentation of this file.
00001 #ifndef RecoTauTag_TauTagTools_PFTauDiscriminantBase
00002 #define RecoTauTag_TauTagTools_PFTauDiscriminantBase
00003 
00004 #include "DataFormats/TauReco/interface/PFTauDecayMode.h"
00005 #include "PhysicsTools/UtilAlgos/interface/DeltaR.h"
00006 #include "PhysicsTools/Utilities/interface/Angle.h"
00007 #include "RecoTauTag/TauTagTools/interface/PFTauDiscriminantManager.h"
00008 #include "PhysicsTools/MVAComputer/interface/AtomicId.h"
00009 #include "PhysicsTools/MVAComputer/interface/Variable.h"
00010 #include "TMath.h"
00011 #include "TTree.h"
00012 #include <string>
00013 #include <algorithm>
00014 #include <vector>
00015 
00016 /*  Class: PFTauDiscriminants::Discriminant
00017  *
00018  *  Author: Evan K. Friis, UC Davis friis@physics.ucdavis.edu
00019  *
00020  *  Description:
00021  *      Abstract base class to hold PFTauDecayMode descriminant functions
00022  *      is called by a PFTauDiscriminantManager.
00023  *      Handles TTree branching of the specific requirements of various variables
00024  *      Can construct CMSSW MVA framework input for a given tau object
00025  *      Variables can be multiple, or optional.  Also supports a "NULL TYPE" for 
00026  *      matching reconstruction failures to reconstruction success.
00027  *
00028  *      Implementing discriminants of type T should inherit from DicriminantBase< TYPE >
00029  *      where TYPE is either a simple data type (e.g. 'F' for float, 'I':int)
00030  *      or any complex type for which ROOT dictionaries exists and have a double() operator.
00031  *      OR any STL collection of said types.
00032  *      See RecoTauTag/TauTagTools/interface/Discriminants.h for examples of implementation.
00033  */
00034 
00035 
00036 namespace PFTauDiscriminants {
00037 using namespace std;
00038 
00039 class PFTauDiscriminantManager;
00040 
00041 class Discriminant {
00042    public:
00043       explicit Discriminant(string name, string rootTypeName, bool branchAsSimpleDataType):
00044          discriminantName_(PhysicsTools::AtomicId(name)),
00045          rootTypeName_(rootTypeName),
00046          branchAsSimpleDataType_(branchAsSimpleDataType)
00047       {};
00048       virtual ~Discriminant(){};
00049       virtual void                      compute(PFTauDiscriminantManager *input)=0;
00050       virtual void                      setNullResult(PFTauDiscriminantManager *input)=0;
00051       string                            name()         const {return discriminantName_;}
00052       PhysicsTools::AtomicId            theAtomicId()  const {return discriminantName_;}
00053       string                            rootTypeName() const {return rootTypeName_;}
00054 
00056       virtual void       branchTree(TTree* theTree) = 0; 
00057       virtual void       fillMVA(std::vector<PhysicsTools::Variable::Value>& mvaHolder) const = 0;
00058 
00059    protected:
00061       bool               branchSimply() const { return branchAsSimpleDataType_; }
00062 
00063    private:
00064       PhysicsTools::AtomicId                      discriminantName_;
00065       string                                      rootTypeName_;
00066       bool                                        branchAsSimpleDataType_;
00067 };
00068 
00069 template<class T>
00070 class DiscriminantBase : public Discriminant {
00071    public:
00072       explicit  DiscriminantBase(string name, string rootTypeName, 
00073                         bool branchAsSimpleDataType, bool isMultiple, T defaultValue):Discriminant(name, rootTypeName, branchAsSimpleDataType),isMultiple_(isMultiple),defaultValue_(defaultValue){
00074          resultPtr_ = &result_;
00075       };
00076 
00077       virtual  ~DiscriminantBase(){};
00078       typedef typename std::vector<T>::const_iterator myVectorIterator;
00079       
00080       virtual void setNullResult(PFTauDiscriminantManager *input) //can be overriden in the derived classes if desired (for example, to use edm::Event info)
00081       {
00082          result_.clear();
00083          singleResult_ = defaultValue_;
00084       }
00085 
00088       void compute(PFTauDiscriminantManager* input)
00089       {
00090          result_.clear();
00091 
00092          if (input)
00093             doComputation(input, result_); 
00094          else
00095             edm::LogError("DiscriminantBase") << "Error in DiscriminantBase - trying to compute discriminants on null PFTauDecayMode pointer!";
00096 
00097          size_t numberOfResultsReturned = result_.size();
00098          if(!numberOfResultsReturned) //if there are no results, ROOT branches of simple variables must be filled w/ the default value
00099          {
00100             singleResult_ = defaultValue_;
00101          } else
00102          {
00103             if(!isMultiple_ && numberOfResultsReturned > 1)
00104             {
00105                edm::LogWarning("PFTauDiscriminants::DiscriminantBase") << "Warning, multiple discriminant values recieved for a non-multiple branch, taking only the first!"; 
00106             }
00107             singleResult_ = result_[0];
00108          }
00109       }
00110 
00111       //adds a branch to the tree corresponding to this variable
00112       void branchTree(TTree* theTree) {
00113          if (!this->branchSimply())
00114          {
00115             edm::LogInfo("PFTauDiscriminantBase") << "Branching TTree: " << theTree->GetName() << " with full class name (bronch)";
00116             theTree->Branch(name().c_str(), rootTypeName().c_str(), &resultPtr_); 
00117          }
00118          else
00119          {
00120             edm::LogInfo("PFTauDiscriminantBase") << "Branching TTree: " << theTree->GetName() << " with struct style branch (leaflist)";
00121             stringstream branchType;
00122             branchType << name() << "/" << rootTypeName(); //eg D, F, I, etc
00123             theTree->Branch(this->name().c_str(), &singleResult_, branchType.str().c_str());
00124          }
00125       }
00126       //fills a vector of values for the MVA framework
00127       void fillMVA(std::vector<PhysicsTools::Variable::Value>& mvaHolder) const {
00128          if (isMultiple_)
00129          {
00130             for(myVectorIterator aResult = result_.begin(); aResult != result_.end(); ++aResult)
00131             {
00132                mvaHolder.push_back(PhysicsTools::Variable::Value(theAtomicId(), static_cast<double>(*aResult)));
00133             }
00134          }
00135          else
00136          {
00137             mvaHolder.push_back(PhysicsTools::Variable::Value(theAtomicId(), static_cast<double>(singleResult_)));
00138          }
00139       }
00140       
00141    protected:
00142       virtual void      doComputation(PFTauDiscriminantManager* input, vector<T>& result)=0;
00143    private:
00144       bool              isMultiple_;
00145       T                 defaultValue_;
00146       T                 singleResult_;
00147       std::vector<T>    result_;
00148       std::vector<T>*   resultPtr_;
00149 };
00150 }
00151 #endif

Generated on Tue Jun 9 17:45:05 2009 for CMSSW by  doxygen 1.5.4