Logo ROOT   6.12/07
Reference Guide
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 /*! \class TMVA::Ranking
28 \ingroup TMVA
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 
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;
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 windows.. 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() << kHEADER << "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 /// constructor
138 
139 TMVA::Rank::Rank( const TString& variable, Double_t rankValue )
140  : fVariable( variable ),
141  fRankValue( rankValue ),
142  fRank( -1 )
143 {
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// destructor
148 
150 {
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// comparison operator <
155 
156 Bool_t TMVA::Rank::operator< ( const Rank& other ) const
157 {
158  if (fRankValue < other.fRankValue) return true;
159  else return false;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// comparison operator >
164 
165 Bool_t TMVA::Rank::operator> ( const Rank& other ) const
166 {
167  if (fRankValue > other.fRankValue) return true;
168  else return false;
169 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
MsgLogger * fLogger
Definition: Ranking.h:68
Bool_t operator<(const Rank &other) const
comparison operator <
Definition: Ranking.cxx:156
Basic string class.
Definition: TString.h:125
Ranking for variables in method (implementation)
Definition: Ranking.h:48
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual ~Rank()
destructor
Definition: Ranking.cxx:149
void SetContext(const TString &context)
Definition: Ranking.cxx:76
std::vector< TMVA::Rank > fRanking
Definition: Ranking.h:64
Rank(const TString &variable, Double_t rankValue)
constructor
Definition: Ranking.cxx:139
TString fContext
Definition: Ranking.h:65
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:386
virtual void Print() const
get maximum length of variable names
Definition: Ranking.cxx:111
virtual ~Ranking()
destructor
Definition: Ranking.cxx:68
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
Double_t fRankValue
Definition: Ranking.h:95
Bool_t operator>(const Rank &other) const
comparison operator >
Definition: Ranking.cxx:165
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
void SetSource(const std::string &source)
Definition: MsgLogger.h:70
Ranking()
default constructor
Definition: Ranking.cxx:46
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:200
MsgLogger & Log() const
message logger
Definition: Ranking.h:69
const char * Data() const
Definition: TString.h:345
TString fRankingDiscriminatorName
Definition: Ranking.h:66