Logo ROOT   6.08/07
Reference Guide
RuleCut.cxx
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 describing a 'rule cut' *
11  * *
12  * *
13  * Authors (alphabetical): *
14  * Fredrik Tegenfeldt <Fredrik.Tegenfeldt@cern.ch> - Iowa State U., USA *
15  * *
16  * Copyright (c) 2005: *
17  * CERN, Switzerland *
18  * Iowa State U. *
19  * *
20  * Redistribution and use in source and binary forms, with or without *
21  * modification, are permitted according to the terms listed in LICENSE *
22  * (http://tmva.sourceforge.net/LICENSE) *
23  **********************************************************************************/
24 #include <algorithm>
25 #include <list>
26 
27 #include "TMVA/RuleCut.h"
28 #include "TMVA/DecisionTree.h"
29 #include "TMVA/MsgLogger.h"
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// main constructor
33 
34 TMVA::RuleCut::RuleCut( const std::vector<const Node*> & nodes )
35  : fCutNeve(0),
36  fPurity(0),
37  fLogger(new MsgLogger("RuleFit"))
38 {
39  MakeCuts( nodes );
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// empty constructor
44 
46  : fCutNeve(0),
47  fPurity(0),
48  fLogger(new MsgLogger("RuleFit"))
49 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// destructor
54 
56  delete fLogger;
57 }
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Construct the cuts from the given array of nodes
62 
63 void TMVA::RuleCut::MakeCuts( const std::vector<const Node*> & nodes )
64 {
65  // Atleast 2 nodes are required
66  UInt_t nnodes = nodes.size();
67  if (nnodes<2) {
68  Log() << kWARNING << "<MakeCuts()> Empty cut created." << Endl;
69  return;
70  }
71 
72  // Set number of events and S/S+B in last node
73  const DecisionTreeNode* dtn = dynamic_cast<const DecisionTreeNode*>(nodes.back());
74  if(!dtn) return;
75  fCutNeve = dtn->GetNEvents();
76  fPurity = dtn->GetPurity();
77 
78  // some local typedefs
79  typedef std::pair<Double_t,Int_t> CutDir_t; // first is cut value, second is direction
80  typedef std::pair<Int_t,CutDir_t> SelCut_t;
81 
82  // Clear vectors
83  fSelector.clear();
84  fCutMin.clear();
85  fCutMax.clear();
86  fCutDoMin.clear();
87  fCutDoMax.clear();
88 
89  // Count the number of variables in cut
90  // Exclude last node since that does not lead to a cut
91  std::list<SelCut_t> allsel;
92  Int_t sel;
93  Double_t val;
94  Int_t dir;
95  const Node *nextNode;
96  for ( UInt_t i=0; i<nnodes-1; i++) {
97  nextNode = nodes[i+1];
98  const DecisionTreeNode* dtn_ = dynamic_cast<const DecisionTreeNode*>(nodes[i]);
99  if(!dtn_) return;
100  sel = dtn_->GetSelector();
101  val = dtn_->GetCutValue();
102  if (nodes[i]->GetRight() == nextNode) { // val>cut
103  dir = 1;
104  }
105  else if (nodes[i]->GetLeft() == nextNode) { // val<cut
106  dir = -1;
107  }
108  else {
109  Log() << kFATAL << "<MakeTheRule> BUG! Should not be here - an end-node before the end!" << Endl;
110  dir = 0;
111  }
112  allsel.push_back(SelCut_t(sel,CutDir_t(val,dir)));
113  }
114  // sort after the selector (first element of CutDir_t)
115  allsel.sort();
116  Int_t prevSel=-1;
117  Int_t nsel=0;
118  Bool_t firstMin=kTRUE;
119  Bool_t firstMax=kTRUE;
120  for ( std::list<SelCut_t>::const_iterator it = allsel.begin(); it!=allsel.end(); it++ ) {
121  sel = (*it).first;
122  val = (*it).second.first;
123  dir = (*it).second.second;
124  if (sel!=prevSel) { // a new selector!
125  firstMin = kTRUE;
126  firstMax = kTRUE;
127  nsel++;
128  fSelector.push_back(sel);
129  fCutMin.resize( fSelector.size(),0);
130  fCutMax.resize( fSelector.size(),0);
131  fCutDoMin.resize( fSelector.size(), kFALSE);
132  fCutDoMax.resize( fSelector.size(), kFALSE);
133  }
134  switch ( dir ) {
135  case 1:
136  if ((val<fCutMin[nsel-1]) || firstMin) {
137  fCutMin[nsel-1] = val;
138  fCutDoMin[nsel-1] = kTRUE;
139  firstMin = kFALSE;
140  }
141  break;
142  case -1:
143  if ((val>fCutMax[nsel-1]) || firstMax) {
144  fCutMax[nsel-1] = val;
145  fCutDoMax[nsel-1] = kTRUE;
146  firstMax = kFALSE;
147  }
148  default:
149  break;
150  }
151  prevSel = sel;
152  }
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// get number of cuts
157 
159 {
160  UInt_t rval=0;
161  for (UInt_t i=0; i<fSelector.size(); i++) {
162  if (fCutDoMin[i]) rval += 1;
163  if (fCutDoMax[i]) rval += 1;
164  }
165  return rval;
166 }
167 ////////////////////////////////////////////////////////////////////////////////
168 /// get cut range for a given selector
169 
170 Bool_t TMVA::RuleCut::GetCutRange(Int_t sel,Double_t &rmin, Double_t &rmax, Bool_t &dormin, Bool_t &dormax) const
171 {
172  dormin=kFALSE;
173  dormax=kFALSE;
174  Bool_t done=kFALSE;
175  Bool_t foundIt=kFALSE;
176  UInt_t ind=0;
177  while (!done) {
178  foundIt = (Int_t(fSelector[ind])==sel);
179  ind++;
180  done = (foundIt || (ind==fSelector.size()));
181  }
182  if (!foundIt) return kFALSE;
183  rmin = fCutMin[ind-1];
184  rmax = fCutMax[ind-1];
185  dormin = fCutDoMin[ind-1];
186  dormax = fCutDoMax[ind-1];
187  return kTRUE;
188 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Float_t GetNEvents(void) const
Double_t fPurity
Definition: RuleCut.h:94
Bool_t GetCutRange(Int_t sel, Double_t &rmin, Double_t &rmax, Bool_t &dormin, Bool_t &dormax) const
get cut range for a given selector
Definition: RuleCut.cxx:170
MsgLogger * fLogger
Definition: RuleCut.h:97
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
std::vector< Double_t > fCutMax
Definition: RuleCut.h:90
std::vector< Char_t > fCutDoMin
Definition: RuleCut.h:91
MsgLogger & Log() const
Definition: RuleCut.h:98
Float_t GetCutValue(void) const
MsgLogger * fLogger
Definition: Rule.h:189
RuleCut()
empty constructor
Definition: RuleCut.cxx:45
std::vector< Char_t > fCutDoMax
Definition: RuleCut.h:92
std::vector< UInt_t > fSelector
Definition: RuleCut.h:88
UInt_t GetNcuts() const
get number of cuts
Definition: RuleCut.cxx:158
unsigned int UInt_t
Definition: RtypesCore.h:42
Float_t GetPurity(void) const
double Double_t
Definition: RtypesCore.h:55
void MakeCuts(const std::vector< const TMVA::Node * > &nodes)
Construct the cuts from the given array of nodes.
Definition: RuleCut.cxx:63
std::vector< Double_t > fCutMin
Definition: RuleCut.h:89
virtual ~RuleCut()
destructor
Definition: RuleCut.cxx:55
Short_t GetSelector() const
const Bool_t kTRUE
Definition: Rtypes.h:91
Double_t fCutNeve
Definition: RuleCut.h:93