ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Ranking.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate Data analysis *
6  * Package: TMVA *
7  * Class : Ranking *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * *
18  * Copyright (c) 2005: *
19  * CERN, Switzerland *
20  * MPI-K Heidelberg, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 //_______________________________________________________________________
28 //
29 // Ranking for variables in method (implementation)
30 //_______________________________________________________________________
31 
32 #include "TMVA/Ranking.h"
33 
34 #include "TMVA/MsgLogger.h"
35 #include "TMVA/Types.h"
36 
37 #include "TString.h"
38 
39 #include <iomanip>
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// default constructor
45 
46 TMVA::Ranking::Ranking()
47  : fRanking(),
48  fContext(""),
49  fRankingDiscriminatorName( "" ),
50  fLogger( new MsgLogger("", kINFO) )
51 {
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// constructor
56 
57 TMVA::Ranking::Ranking( const TString& context, const TString& rankingDiscriminatorName )
58  : fRanking(),
59  fContext( context ),
60  fRankingDiscriminatorName( rankingDiscriminatorName ),
61  fLogger( new MsgLogger(fContext.Data(), kINFO) )
62 {
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// destructor
67 
69 {
70  fRanking.clear();
71  delete fLogger;
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 
76 void TMVA::Ranking::SetContext( const TString& context)
77 {
78  fContext = context;
79  fLogger->SetSource( fContext.Data() );
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// Add a new rank
84 /// take ownership of it
85 
86 void TMVA::Ranking::AddRank( const Rank& rank )
87 {
88  // sort according to rank value (descending)
89  // Who the hell knows why this does not compile on windos.. write the sorting
90  // reversing myself... (means sorting in "descending" order)
91  // --> std::sort ( fRanking.begin(), fRanking.end() );
92  // --> std::reverse( fRanking.begin(), fRanking.end() );
93  fRanking.push_back( rank );
94 
95  UInt_t sizeofarray=fRanking.size();
96  Rank temp(fRanking[0]);
97  for (UInt_t i=0; i<sizeofarray; i++) {
98  for (UInt_t j=sizeofarray-1; j>i; j--) {
99  if (fRanking[j-1] < fRanking[j]) {
100  temp = fRanking[j-1];fRanking[j-1] = fRanking[j]; fRanking[j] = temp;
101  }
102  }
103  }
104 
105  for (UInt_t i=0; i<fRanking.size(); i++) fRanking[i].SetRank( i+1 );
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// get maximum length of variable names
110 
112 {
113  Int_t maxL = 0;
114  for (std::vector<Rank>::const_iterator ir = fRanking.begin(); ir != fRanking.end(); ir++ )
115  if ((*ir).GetVariable().Length() > maxL) maxL = (*ir).GetVariable().Length();
116 
117  TString hline = "";
118  for (Int_t i=0; i<maxL+15+fRankingDiscriminatorName.Length(); i++) hline += "-";
119  Log() << kINFO << "Ranking result (top variable is best ranked)" << Endl;
120  Log() << kINFO << hline << Endl;
121  Log() << kINFO << std::setiosflags(std::ios::left)
122  << std::setw(5) << "Rank : "
123  << std::setw(maxL+0) << "Variable "
124  << std::resetiosflags(std::ios::right)
125  << " : " << fRankingDiscriminatorName << Endl;
126  Log() << kINFO << hline << Endl;
127  for (std::vector<Rank>::const_iterator ir = fRanking.begin(); ir != fRanking.end(); ir++ ) {
128  Log() << kINFO
129  << Form( "%4i : ",(*ir).GetRank() )
130  << std::setw(TMath::Max(maxL+0,9)) << (*ir).GetVariable().Data()
131  << Form( " : %3.3e", (*ir).GetRankValue() ) << Endl;
132  }
133  Log() << kINFO << hline << Endl;
134 }
135 
136 // ===============================================================================================
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// constructor
140 
141 TMVA::Rank::Rank( const TString& variable, Double_t rankValue )
142  : fVariable( variable ),
143  fRankValue( rankValue ),
144  fRank( -1 )
145 {
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// destructor
150 
152 {
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// comparison operator <
157 
158 Bool_t TMVA::Rank::operator< ( const Rank& other ) const
159 {
160  if (fRankValue < other.fRankValue) return true;
161  else return false;
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// comparison operator >
166 
167 Bool_t TMVA::Rank::operator> ( const Rank& other ) const
168 {
169  if (fRankValue > other.fRankValue) return true;
170  else return false;
171 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual ~Rank()
destructor
Definition: Ranking.cxx:151
void SetContext(const TString &context)
Definition: Ranking.cxx:76
std::vector< std::vector< double > > Data
Bool_t operator>(const Rank &other) const
comparison operator >
Definition: Ranking.cxx:167
ClassImp(TMVA::Ranking) TMVA
default constructor
Definition: Ranking.cxx:41
Rank(const TString &variable, Double_t rankValue)
constructor
Definition: Ranking.cxx:141
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual ~Ranking()
destructor
Definition: Ranking.cxx:68
double Double_t
Definition: RtypesCore.h:55
Double_t fRankValue
Definition: Ranking.h:97
virtual void AddRank(const Rank &rank)
Add a new rank take ownership of it.
Definition: Ranking.cxx:86
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
virtual void Print() const
get maximum length of variable names
Definition: Ranking.cxx:111
Bool_t operator<(const Rank &other) const
comparison operator <
Definition: Ranking.cxx:158
Definition: math.cpp:60