129 {
131
132 TMVA::Reader *
reader =
new TMVA::Reader(
"Silent");
133
134 TString xmlFileName="";
135 if(argc>1) xmlFileName=
argv[1];
136 else return 0;
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);
143
144 std::vector<XmlVariableInfo> variable_infos = parseXml(xmlFileName);
148 vector<float*> vars;
149 vector<float> var_avgerage;
150
151
152 cout <<
"Boosted Decision Tree for " <<
AnalysisType << endl;
153
154 for (const auto & variable_info : variable_infos){
155
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;
161
162 TString varDefinition(varName);
163 if (varName != expression){
165 }
166
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;
174 }
175 else if (infoType == "spectator"){
176 reader->AddSpectator(varDefinition, vars.back());
177 cout <<
"Add spectator: " << varDefinition <<
" " <<
type << endl;
178 }
179 else
180 {
181 cerr <<"Unknown type from parser "<< infoType.Data()<<endl;
182
183
184 vars.pop_back();
185 return 0;
186 }
187 }
188
189 reader->BookMVA(
"BDTG", xmlFileName);
190
191 TMVA::MethodBDT* method_bdt =
dynamic_cast<TMVA::MethodBDT*
> (
reader->FindMVA(
"BDTG"));
192 bool useYesNoLeaf = false;
193 bool isGrad = 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;
199 bdt->SetPointers(vars);
200
201
202 cout << endl << "Testing MVA produced from TMVA::Reader " << endl;
203
204 cout << "MVAUtils::BDT : "
205 << (isRegression ? bdt->GetResponse() : isMulti ? bdt->GetMultiResponse(
NClass)[
NClass-1] : isGrad ? bdt->GetGradBoostMVA(vars) : bdt->GetClassification())
206 << " , TMVA::Reader : "
207 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
208 << endl;
209
210 for(
auto & var : vars) *
var = 0;
211 cout << "MVAUtils::BDT : "
212 << (isRegression ? bdt->GetResponse() : isMulti ? bdt->GetMultiResponse(vars,
NClass)[
NClass-1] : isGrad ? bdt->GetGradBoostMVA(vars) : bdt->GetClassification())
213 << " , TMVA::Reader : "
214 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
215 << endl;
216
217
218 cout <<
"Writing MVAUtils::BDT in " <<
outFileName << endl;
219 TFile*
f = TFile::Open(outFileName,
"RECREATE");
220 bdt->WriteTree("BDT")->Write();
221 TNamed*
n =
new TNamed(
"varList",
varList.Data());
225 cout << endl <<
"Reading BDT from root file and testing " <<
outFileName << endl;
226
227 f = TFile::Open(outFileName,
"READ");
228 TTree* bdt_tree =
dynamic_cast<TTree*
> (
f->Get(
"BDT"));
229 if(!bdt_tree){
230 cerr <<"Could not Retrieve BDT TTree from file , should not happen" <<endl;
231 return 0;
232 }
233
234 bdt = std::make_unique<MVAUtils::BDT>(bdt_tree);
235 bdt->SetPointers(vars);
236 cout << bdt->GetResponse() << endl;
237 cout << "MVAUtils::BDT : "
238 << (isRegression ? bdt->GetResponse() : isMulti ? bdt->GetMultiResponse(
NClass)[
NClass-1] : isGrad ? bdt->GetGradBoostMVA(vars) : bdt->GetClassification())
239 << " , TMVA::Reader : "
240 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
241 << endl;
242 for(
uint i = 0;
i != vars.size(); ++
i) *vars[i] = var_avgerage[i];
243 cout << "MVAUtils::BDT : "
244 << (isRegression && !isGrad ? bdt->GetResponse() : isMulti ? bdt->GetMultiResponse(
NClass)[
NClass-1] : isGrad ? bdt->GetGradBoostMVA(vars) : bdt->GetClassification())
245 << " , TMVA::Reader : "
246 << (isRegression ?
reader->EvaluateRegression(0,
"BDTG") : isMulti ?
reader->EvaluateMulticlass(
"BDTG")[
NClass-1] :
reader->EvaluateMVA(
"BDTG"))
247 << endl;
248
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];
253 float mva = (isRegression ? bdt->GetResponse() : isMulti ? bdt->GetMultiResponse(vars,
NClass)[
NClass-1] : isGrad ? bdt->GetGradBoostMVA(vars) : bdt->GetClassification());
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;
258 }
259 }
260 cout <<
"Found " <<
n_events <<
" events in disagreement " << endl;
261 cout << endl;
262}
str varName
end cluster ToT and charge
std::unique_ptr< MVAUtils::BDT > convert(TMVA::MethodBDT *bdt, bool isRegression=true, bool useYesNoLeaf=false)
reader
read the goodrunslist xml file(s)