8 #include "TMVA/Reader.h"
9 #include "TMVA/MethodBDT.h"
12 #include <TXMLEngine.h>
45 std::vector<XmlVariableInfo>
48 std::vector<XmlVariableInfo>
result;
52 for (XMLNodePointer_t info_node =
xml->GetChild(
node); info_node !=
nullptr;
53 info_node =
xml->GetNext(info_node))
55 XMLAttrPointer_t attr =
xml->GetFirstAttr(info_node);
58 while (attr !=
nullptr)
60 TString
name =
xml->GetAttrName(attr);
61 if (
name ==
"Expression")
63 else if (
name ==
"Label")
64 varInfo.
label =
xml->GetAttrValue(attr);
65 else if (
name ==
"Type")
67 else if (
name ==
"Min") varInfo.
min=TString(
xml->GetAttrValue(attr)).Atof();
68 else if (
name ==
"Max") varInfo.
max=TString(
xml->GetAttrValue(attr)).Atof();
70 attr =
xml->GetNextAttr(attr);
83 std::vector<XmlVariableInfo>
86 std::vector<XmlVariableInfo>
result;
89 XMLDocPointer_t xmldoc =
xml.ParseFile(xml_filename);
91 std::cerr<<
" file not found " <<xml_filename.Data() <<
" current directory is: " << gSystem->WorkingDirectory()<<std::endl;
94 XMLNodePointer_t mainnode =
xml.DocGetRootElement(xmldoc);
97 XMLNodePointer_t
node =
xml.GetChild(mainnode);
100 TString nodeName =
xml.GetNodeName(
node);
101 if (nodeName ==
"Variables" || nodeName ==
"Spectators") {
105 else if (nodeName ==
"GeneralInfo" &&
node) {
106 for (XMLNodePointer_t info_node =
xml.GetChild(
node); info_node !=
nullptr;
107 info_node =
xml.GetNext(info_node)){
108 XMLAttrPointer_t attr =
xml.GetFirstAttr(info_node);
110 while (attr !=
nullptr) {
112 TString
value =
xml.GetAttrValue(attr);
113 attr =
xml.GetNextAttr(attr);
114 if (
value ==
"AnalysisType")
119 else if (nodeName ==
"Classes") {
134 TString xmlFileName=
"";
137 int last_slash=xmlFileName.Last(
'/');
138 last_slash = (last_slash<0 ? 0 : last_slash+1);
139 TString
outFileName=xmlFileName(last_slash, xmlFileName.Length()-last_slash+1);
144 std::vector<XmlVariableInfo> variable_infos = parseXml(xmlFileName);
149 vector<float> var_avgerage;
152 cout <<
"Boosted Decision Tree for " <<
AnalysisType << endl;
154 for (
const auto & variable_info : variable_infos){
156 TString infoType = (TString(variable_info.nodeName).Contains(
"Variable") ?
157 "variable" :
"spectator");
158 TString
expression = variable_info.expression;
159 TString
varName = variable_info.label;
160 TString
type = variable_info.varType;
162 TString varDefinition(
varName);
167 float average_value = (variable_info.min+variable_info.max)/2 ;
168 var_avgerage.push_back(average_value);
169 vars.push_back(
new float(average_value));
170 if (infoType ==
"variable"){
172 reader->AddVariable(varDefinition, vars.back());
173 cout <<
"Add variable: " << varDefinition <<
" " <<
type << endl;
175 else if (infoType ==
"spectator"){
176 reader->AddSpectator(varDefinition, vars.back());
177 cout <<
"Add spectator: " << varDefinition <<
" " <<
type << endl;
181 cerr <<
"Unknown type from parser "<< infoType.Data()<<endl;
189 reader->BookMVA(
"BDTG", xmlFileName);
191 TMVA::MethodBDT* method_bdt =
dynamic_cast<TMVA::MethodBDT*
> (
reader->FindMVA(
"BDTG"));
192 bool useYesNoLeaf =
false;
194 if(method_bdt->GetOptions().Contains(
"UseYesNoLeaf=True")) useYesNoLeaf =
true;
195 if(method_bdt->GetOptions().Contains(
"BoostType=Grad")) isGrad =
true;
196 cout <<
"UseYesNoLeaf? " << useYesNoLeaf << endl;
197 cout <<
"Gradient Boost? " << isGrad << endl;
202 cout << endl <<
"Testing MVA produced from TMVA::Reader " << endl;
204 cout <<
"MVAUtils::BDT : "
206 <<
" , TMVA::Reader : "
207 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
210 for(
auto &
var : vars) *
var = 0;
211 cout <<
"MVAUtils::BDT : "
213 <<
" , TMVA::Reader : "
214 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
218 cout <<
"Writing MVAUtils::BDT in " <<
outFileName << endl;
221 TNamed*
n =
new TNamed(
"varList",
varList.Data());
225 cout << endl <<
"Reading BDT from root file and testing " <<
outFileName << endl;
228 TTree* bdt_tree =
dynamic_cast<TTree*
> (
f->Get(
"BDT"));
230 cerr <<
"Could not Retrieve BDT TTree from file , should not happen" <<endl;
234 bdt = std::make_unique<MVAUtils::BDT>(bdt_tree);
237 cout <<
"MVAUtils::BDT : "
239 <<
" , TMVA::Reader : "
240 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
242 for(
uint i = 0;
i != vars.size(); ++
i) *vars[
i] = var_avgerage[
i];
243 cout <<
"MVAUtils::BDT : "
245 <<
" , TMVA::Reader : "
246 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
249 cout <<
"Checking over many random events" << endl;
251 for(
int i = 0;
i != 100; ++
i){
252 for(
uint i = 0;
i != vars.size(); ++
i) *vars[
i] = (1+(
rand.Rndm()-0.5)/5)*var_avgerage[
i];
254 float tmva = (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"));
255 if( (tmva-mva)/mva > 0.00001 ){
256 cout <<
"MVAUtils::BDT : " << mva <<
" , TMVA::Reader : " << tmva << endl;
260 cout <<
"Found " << n_events <<
" events in disagreement " << endl;