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;