Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RuleCut.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 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 #ifndef ROOT_TMVA_RuleCut
25 #define ROOT_TMVA_RuleCut
26 
27 #include "TMVA/Event.h"
28 
29 namespace TMVA {
30 
31  class Node;
32  class MsgLogger;
33 
34  class RuleCut {
35 
36  public:
37 
38  // main constructor
39  RuleCut( const std::vector< const TMVA::Node * > & nodes );
40 
41  // copy constructor
42  RuleCut( const RuleCut & other ) : fLogger(0) { Copy( other ); }
43 
44  // empty constructor
45  RuleCut();
46 
47  // destructor
48  virtual ~RuleCut();
49 
50  // evaluate an event
51  inline Bool_t EvalEvent( const Event &eve );
52 
53  // get cut range for a given selector
54  Bool_t GetCutRange(Int_t sel,Double_t &rmin, Double_t &rmax, Bool_t &dormin, Bool_t &dormax) const;
55 
56  // number of cuts
57  UInt_t GetNcuts() const;
58 
59  // set members
60  inline void SetNvars( UInt_t nc );
61  void SetNeve( Double_t n ) { fCutNeve = n; }
62  void SetPurity( Double_t ssb ) { fPurity = ssb; }
63  void SetSelector( Int_t i, UInt_t s ) { fSelector[i] = s; }
64  void SetCutMin( Int_t i, Double_t v ) { fCutMin[i] = v; }
65  void SetCutMax( Int_t i, Double_t v ) { fCutMax[i] = v; }
66  void SetCutDoMin( Int_t i, Bool_t v ) { fCutDoMin[i] = v; }
67  void SetCutDoMax( Int_t i, Bool_t v ) { fCutDoMax[i] = v; }
68 
69  // accessors
70  UInt_t GetNvars() const { return fSelector.size(); }
71  UInt_t GetSelector(Int_t is) const { return fSelector[is]; }
72  Double_t GetCutMin(Int_t is) const { return fCutMin[is]; }
73  Double_t GetCutMax(Int_t is) const { return fCutMax[is]; }
74  Char_t GetCutDoMin(Int_t is) const { return fCutDoMin[is]; }
75  Char_t GetCutDoMax(Int_t is) const { return fCutDoMax[is]; }
76  Double_t GetCutNeve() const { return fCutNeve; }
77  Double_t GetPurity() const { return fPurity; }
78 
79  private:
80  // copy
81  inline void Copy( const RuleCut & other);
82 
83  // make the cuts from the array of nodes
84  void MakeCuts( const std::vector< const TMVA::Node * > & nodes );
85 
86  std::vector<UInt_t> fSelector; // array of selectors (expressions)
87  std::vector<Double_t> fCutMin; // array of lower limits
88  std::vector<Double_t> fCutMax; // array of upper limits
89  std::vector<Char_t> fCutDoMin; // array of usage flags for lower limits <--- stores boolean
90  std::vector<Char_t> fCutDoMax; // array of usage flags for upper limits <--- stores boolean
91  Double_t fCutNeve; // N(events) after cut (possibly weighted)
92  Double_t fPurity; // S/(S+B) on training data
93 
94 
95  mutable MsgLogger* fLogger; // message logger
96  MsgLogger& Log() const { return *fLogger; }
97  };
98 }
99 
100 //_______________________________________________________________________
101 inline void TMVA::RuleCut::Copy( const TMVA::RuleCut & other )
102 {
103  // copy from another
104  if (&other != this) {
105  for (UInt_t ns=0; ns<other.GetNvars(); ns++) {
106  fSelector.push_back( other.GetSelector(ns) );
107  fCutMin.push_back( other.GetCutMin(ns) );
108  fCutMax.push_back( other.GetCutMax(ns) );
109  fCutDoMin.push_back( other.GetCutDoMin(ns) );
110  fCutDoMax.push_back( other.GetCutDoMax(ns) );
111  }
112  fCutNeve = other.GetCutNeve();
113  fPurity = other.GetPurity();
114  }
115 }
116 
117 //_______________________________________________________________________
118 inline Bool_t TMVA::RuleCut::EvalEvent( const Event &eve )
119 {
120  // evaluate event using the cut
121 
122  // Loop over all cuts
123  Int_t sel;
124  Double_t val;
125  Bool_t done=kFALSE;
126  Bool_t minOK, cutOK=kFALSE;
127  UInt_t nc=0;
128  while (!done) {
129  sel = fSelector[nc];
130  val = eve.GetValue(sel);
131  minOK = (fCutDoMin[nc] ? (val>fCutMin[nc]):kTRUE); // min cut ok
132  cutOK = (minOK ? ((fCutDoMax[nc] ? (val<fCutMax[nc]):kTRUE)) : kFALSE); // cut ok
133  nc++;
134  done = ((!cutOK) || (nc==fSelector.size())); // done if
135  }
136  // return ( cutOK ? 1.0: 0.0 );
137  return cutOK;
138 }
139 
140 //_______________________________________________________________________
141 inline void TMVA::RuleCut::SetNvars( UInt_t nc )
142 {
143  // set the number of cuts
144  fSelector.clear();
145  fCutMin.clear();
146  fCutMax.clear();
147  fCutDoMin.clear();
148  fCutDoMax.clear();
149  //
150  fSelector.resize(nc);
151  fCutMin.resize(nc);
152  fCutMax.resize(nc);
153  fCutDoMin.resize(nc);
154  fCutDoMax.resize(nc);
155 }
156 
157 #endif