ROOT  6.06/09
Reference Guide
Rule.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Fredrik Tegenfeldt, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Rule *
8  * *
9  * Description: *
10  * A class describung a 'rule' *
11  * Each internal node of a tree defines a rule from all the parental nodes. *
12  * A rule consists of atleast 2 nodes. *
13  * Input: a decision tree (in the constructor) *
14  * its coefficient *
15  * *
16  * *
17  * Authors (alphabetical): *
18  * Fredrik Tegenfeldt <Fredrik.Tegenfeldt@cern.ch> - Iowa State U., USA *
19  * Helge Voss <Helge.Voss@cern.ch> - MPI-KP Heidelberg, Ger. *
20  * *
21  * Copyright (c) 2005: *
22  * CERN, Switzerland *
23  * Iowa State U. *
24  * MPI-K Heidelberg, Germany *
25  * *
26  * Redistribution and use in source and binary forms, with or without *
27  * modification, are permitted according to the terms listed in LICENSE *
28  * (http://tmva.sourceforge.net/LICENSE) *
29  **********************************************************************************/
30 
31 #ifndef ROOT_TMVA_Rule
32 #define ROOT_TMVA_Rule
33 
34 #ifndef ROOT_TMath
35 #include "TMath.h"
36 #endif
37 
38 #ifndef ROOT_TMVA_DecisionTree
39 #include "TMVA/DecisionTree.h"
40 #endif
41 #ifndef ROOT_TMVA_Event
42 #include "TMVA/Event.h"
43 #endif
44 #ifndef ROOT_TMVA_RuleCut
45 #include "TMVA/RuleCut.h"
46 #endif
47 
48 namespace TMVA {
49 
50  class RuleEnsemble;
51  class MsgLogger;
52  class Rule;
53 
54  std::ostream& operator<<( std::ostream& os, const Rule & rule );
55 
56  class Rule {
57 
58  // ouput operator for a Rule
59  friend std::ostream& operator<< ( std::ostream& os, const Rule & rule );
60 
61  public:
62 
63  // main constructor
64  Rule( RuleEnsemble *re, const std::vector< const TMVA::Node * > & nodes );
65 
66  // main constructor
67  Rule( RuleEnsemble *re );
68 
69  // copy constructor
70  Rule( const Rule & other ) { Copy( other ); }
71 
72  // empty constructor
73  Rule();
74 
75  virtual ~Rule();
76 
77  // set message type
78  void SetMsgType( EMsgType t );
79 
80  // set RuleEnsemble ptr
81  void SetRuleEnsemble( const RuleEnsemble *re ) { fRuleEnsemble = re; }
82 
83  // set RuleCut ptr
84  void SetRuleCut( RuleCut *rc ) { fCut = rc; }
85 
86  // set Rule norm
87  void SetNorm(Double_t norm) { fNorm = (norm>0 ? 1.0/norm:1.0); }
88 
89  // set coefficient
91 
92  // set support
93  void SetSupport(Double_t v) { fSupport=v; fSigma = TMath::Sqrt(v*(1.0-v));}
94 
95  // set s/(s+b)
96  void SetSSB(Double_t v) { fSSB=v; }
97 
98  // set N(eve) accepted by rule
100 
101  // set reference importance
102  void SetImportanceRef(Double_t v) { fImportanceRef=(v>0 ? v:1.0); }
103 
104  // calculate importance
106 
107  // get the relative importance
109 
110  // evaluate the Rule for the given Event using the coefficient
111  // inline Double_t EvalEvent( const Event& e, Bool_t norm ) const;
112 
113  // evaluate the Rule for the given Event, not using normalization or the coefficent
114  inline Bool_t EvalEvent( const Event& e ) const;
115 
116  // test if two rules are equal
117  Bool_t Equal( const Rule & other, Bool_t useCutValue, Double_t maxdist ) const;
118 
119  // get distance between two equal (ie apart from the cut values) rules
120  Double_t RuleDist( const Rule & other, Bool_t useCutValue ) const;
121 
122  // returns true if the trained S/(S+B) of the last node is > 0.5
123  Double_t GetSSB() const { return fSSB; }
124  Double_t GetSSBNeve() const { return fSSBNeve; }
125  Bool_t IsSignalRule() const { return (fSSB>0.5); }
126 
127  // copy operator
128  void operator=( const Rule & other ) { Copy( other ); }
129 
130  // identical operator
131  Bool_t operator==( const Rule & other ) const;
132 
133  Bool_t operator<( const Rule & other ) const;
134 
135  // get number of variables used in Rule
136  UInt_t GetNumVarsUsed() const { return fCut->GetNvars(); }
137 
138  // get number of cuts in Rule
139  UInt_t GetNcuts() const { return fCut->GetNcuts(); }
140 
141  // check if variable is used by the rule
142  Bool_t ContainsVariable(UInt_t iv) const;
143 
144  // accessors
145  const RuleCut* GetRuleCut() const { return fCut; }
146  const RuleEnsemble* GetRuleEnsemble() const { return fRuleEnsemble; }
148  Double_t GetSupport() const { return fSupport; }
149  Double_t GetSigma() const { return fSigma; }
150  Double_t GetNorm() const { return fNorm; }
151  Double_t GetImportance() const { return fImportance; }
153 
154  // print the rule using flogger
155  void PrintLogger( const char *title=0 ) const;
156 
157  // print just the raw info, used for weight file generation
158  void PrintRaw ( std::ostream& os ) const; // obsolete
159  void* AddXMLTo ( void* parent ) const;
160 
161  void ReadRaw ( std::istream& os ); // obsolete
162  void ReadFromXML( void* wghtnode );
163 
164  private:
165 
166  // set sigma - don't use this as non private!
168 
169  // print info about the Rule
170  void Print( std::ostream& os ) const;
171 
172  // copy from another rule
173  void Copy( const Rule & other );
174 
175  // get the name of variable with index i
176  const TString & GetVarName( Int_t i) const;
177 
178  RuleCut* fCut; // all cuts associated with the rule
179  Double_t fNorm; // normalization - usually 1.0/t(k)
181  Double_t fSigma; // t(k) = sqrt(s*(1-s))
182  Double_t fCoefficient; // rule coeff. a(k)
183  Double_t fImportance; // importance of rule
184  Double_t fImportanceRef; // importance ref
185  const RuleEnsemble* fRuleEnsemble; // pointer to parent RuleEnsemble
186  Double_t fSSB; // S/(S+B) for rule
187  Double_t fSSBNeve; // N(events) reaching the last node in reevaluation
188 
189  mutable MsgLogger* fLogger; //! message logger
190  MsgLogger& Log() const { return *fLogger; }
191 
192  };
193 
194 } // end of TMVA namespace
195 
196 //_______________________________________________________________________
197 inline Bool_t TMVA::Rule::EvalEvent( const TMVA::Event& e ) const
198 {
199  // Checks if event is accepted by rule.
200  // Return true if yes and false if not.
201  //
202  return fCut->EvalEvent(e);
203 }
204 
205 #endif
Double_t fImportance
Definition: Rule.h:183
Rule()
the simple constructor
Definition: Rule.cxx:104
void SetCoefficient(Double_t v)
Definition: Rule.h:90
void SetSSBNeve(Double_t v)
Definition: Rule.h:99
void SetRuleCut(RuleCut *rc)
Definition: Rule.h:84
Basic string class.
Definition: TString.h:137
Bool_t IsSignalRule() const
Definition: Rule.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RuleCut * fCut
Definition: Rule.h:178
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
Bool_t EvalEvent(const Event &eve)
Definition: RuleCut.h:120
void operator=(const Rule &other)
Definition: Rule.h:128
Double_t RuleDist(const Rule &other, Bool_t useCutValue) const
Returns: -1.0 : rules are NOT equal, i.e, variables and/or cut directions are wrong >=0: rules are eq...
Definition: Rule.cxx:183
Bool_t Equal(const Rule &other, Bool_t useCutValue, Double_t maxdist) const
Compare two rules.
Definition: Rule.cxx:164
UInt_t GetNcuts() const
Definition: Rule.h:139
Double_t GetSSBNeve() const
Definition: Rule.h:124
Double_t GetImportanceRef() const
Definition: Rule.h:152
Double_t fNorm
Definition: Rule.h:179
Double_t fSSBNeve
Definition: Rule.h:187
MsgLogger * fLogger
Definition: Rule.h:189
void SetSigma(Double_t v)
Definition: Rule.h:167
void SetSSB(Double_t v)
Definition: Rule.h:96
Double_t GetSSB() const
Definition: Rule.h:123
Double_t fCoefficient
Definition: Rule.h:182
const RuleCut * GetRuleCut() const
Definition: Rule.h:145
UInt_t GetNvars() const
Definition: RuleCut.h:72
const TString & GetVarName(Int_t i) const
returns the name of a rule
Definition: Rule.cxx:267
Bool_t EvalEvent(const Event &e) const
Definition: Rule.h:197
Double_t fSigma
Definition: Rule.h:181
Bool_t operator==(const Rule &other) const
comparison operator ==
Definition: Rule.cxx:242
Double_t GetNorm() const
Definition: Rule.h:150
Double_t fImportanceRef
Definition: Rule.h:184
Bool_t ContainsVariable(UInt_t iv) const
check if variable in node
Definition: Rule.cxx:131
SVector< double, 2 > v
Definition: Dict.h:5
void ReadFromXML(void *wghtnode)
read rule from XML
Definition: Rule.cxx:419
Double_t GetImportance() const
Definition: Rule.h:151
EMsgType
Definition: Types.h:61
void * AddXMLTo(void *parent) const
Definition: Rule.cxx:389
const RuleEnsemble * GetRuleEnsemble() const
Definition: Rule.h:146
void CalcImportance()
Definition: Rule.h:105
Double_t GetSigma() const
Definition: Rule.h:149
unsigned int UInt_t
Definition: RtypesCore.h:42
UInt_t GetNcuts() const
get number of cuts
Definition: RuleCut.cxx:158
void PrintRaw(std::ostream &os) const
extensive print function used to print info for the weight file
Definition: Rule.cxx:358
void ReadRaw(std::istream &os)
read function (format is the same as written by PrintRaw)
Definition: Rule.cxx:468
void PrintLogger(const char *title=0) const
print function
Definition: Rule.cxx:326
void Copy(const Rule &other)
copy function
Definition: Rule.cxx:275
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:155
void SetImportanceRef(Double_t v)
Definition: Rule.h:102
void SetSupport(Double_t v)
Definition: Rule.h:93
void SetNorm(Double_t norm)
Definition: Rule.h:87
Double_t fSSB
Definition: Rule.h:186
double Double_t
Definition: RtypesCore.h:55
void Print(std::ostream &os) const
print function
Definition: Rule.cxx:294
Double_t GetRelImportance() const
Definition: Rule.h:108
UInt_t GetNumVarsUsed() const
Definition: Rule.h:136
Double_t GetCoefficient() const
Definition: Rule.h:147
Bool_t operator<(const Rule &other) const
comparison operator <
Definition: Rule.cxx:250
Rule(const Rule &other)
Definition: Rule.h:70
Abstract ClassifierFactory template that handles arbitrary types.
void SetMsgType(EMsgType t)
Definition: Rule.cxx:148
Double_t GetSupport() const
Definition: Rule.h:148
friend std::ostream & operator<<(std::ostream &os, const Rule &rule)
MsgLogger & Log() const
message logger
Definition: Rule.h:190
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
Double_t fSupport
Definition: Rule.h:180
virtual ~Rule()
destructor
Definition: Rule.cxx:122
void SetRuleEnsemble(const RuleEnsemble *re)
Definition: Rule.h:81
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
const RuleEnsemble * fRuleEnsemble
Definition: Rule.h:185