10 #include "TObjString.h"
14 #include "TMVA/Factory.h"
15 #include "TMVA/DataLoader.h"
16 #include "TMVA/Tools.h"
17 #include "TMVA/TMVAGui.h"
21 TMVA::Tools::Instance();
24 std::map<std::string,int>
Use;
34 Use[
"Likelihood"] = 0;
35 Use[
"LikelihoodD"] = 0;
36 Use[
"LikelihoodPCA"] = 0;
37 Use[
"LikelihoodKDE"] = 0;
38 Use[
"LikelihoodMIX"] = 0;
45 Use[
"PDEFoamBoost"] = 0;
52 Use[
"BoostedFisher"] = 0;
95 std::cout << std::endl;
96 std::cout <<
"==> Start TMVAClassification" << std::endl;
99 if (myMethodList !=
"") {
100 for (std::map<std::string,int>::iterator
it = Use.begin();
it != Use.end();
it++)
it->second = 0;
102 std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList,
',' );
103 for (UInt_t
i=0;
i<mlist.size();
i++) {
106 if (Use.find(regMethod) == Use.end()) {
107 std::cout <<
"Method \"" << regMethod <<
"\" not known in TMVA under this name. Choose among the following:" << std::endl;
108 for (std::map<std::string,int>::iterator
it = Use.begin();
it != Use.end();
it++) std::cout <<
it->first <<
" ";
109 std::cout << std::endl;
119 TString
fname =
"/sphenix/user/rosstom/analysis/HF-Particle/KFParticle_sPHENIX/Run40Acceptance082922/Run40_D0_Separated_091922.root";
120 if (!gSystem->AccessPathName( fname )) {
121 input = TFile::Open( fname );
124 TFile::SetCacheFileDir(
".");
125 input = TFile::Open(
"http://root.cern.ch/files/tmva_class_example.root",
"CACHEREAD");
128 std::cout <<
"ERROR: could not open data file" << std::endl;
131 std::cout <<
"--- TMVAClassification : Using input file: " << input->GetName() << std::endl;
135 TTree *D0_Tree = (TTree*)input->Get(
"D0_tree");
136 TTree *D0bar_Tree = (TTree*)input->Get(
"D0bar_tree");
137 TTree *Background_Tree = (TTree*)input->Get(
"Background_tree");
140 TString outfileName(
"TMVA_D0Sep_092122.root" );
142 TFile* outputFile = TFile::Open( outfileName,
"RECREATE" );
154 TMVA::Factory *factory =
new TMVA::Factory(
"TMVAClassification", outputFile,
155 "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
157 TMVA::DataLoader *dataloader=
new TMVA::DataLoader(
"dataset");
167 dataloader->AddVariable(
"outKFP_positive_p",
"P_p",
"GeV/c",
'F' );
168 dataloader->AddVariable(
"outKFP_negative_p",
"N_p",
"GeV/c",
'F' );
169 dataloader->AddVariable(
"outKFP_KpPm_invm",
"KpPm_invm",
"GeV/c^{2}",
'F' );
170 dataloader->AddVariable(
"outKFP_KmPp_invm",
"KmPp_invm",
"GeV/c^{2}",
'F' );
183 Double_t signalWeight = 50.0;
184 Double_t backgroundWeight = 1.0;
187 dataloader->AddSignalTree ( D0_Tree, signalWeight );
188 dataloader->AddBackgroundTree( D0bar_Tree, signalWeight );
189 dataloader->AddBackgroundTree( Background_Tree, backgroundWeight );
251 dataloader->PrepareTrainingAndTestTree( mycuts, mycutb,
252 "nTrain_Signal=1500:nTrain_Background=75000:SplitMode=Random:NormMode=NumEvents:!V" );
263 factory->BookMethod( dataloader, TMVA::Types::kCuts,
"Cuts",
264 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );
267 factory->BookMethod( dataloader, TMVA::Types::kCuts,
"CutsD",
268 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );
271 factory->BookMethod( dataloader, TMVA::Types::kCuts,
"CutsPCA",
272 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=PCA" );
275 factory->BookMethod( dataloader, TMVA::Types::kCuts,
"CutsGA",
276 "H:!V:FitMethod=GA:CutRangeMin[0]=-10:CutRangeMax[0]=10:VarProp[1]=FMax:EffSel:Steps=30:Cycles=3:PopSize=400:SC_steps=10:SC_rate=5:SC_factor=0.95" );
279 factory->BookMethod( dataloader, TMVA::Types::kCuts,
"CutsSA",
280 "!H:!V:FitMethod=SA:EffSel:MaxCalls=150000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );
283 if (Use[
"Likelihood"])
284 factory->BookMethod( dataloader, TMVA::Types::kLikelihood,
"Likelihood",
285 "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
288 if (Use[
"LikelihoodD"])
289 factory->BookMethod( dataloader, TMVA::Types::kLikelihood,
"LikelihoodD",
290 "!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" );
293 if (Use[
"LikelihoodPCA"])
294 factory->BookMethod( dataloader, TMVA::Types::kLikelihood,
"LikelihoodPCA",
295 "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" );
298 if (Use[
"LikelihoodKDE"])
299 factory->BookMethod( dataloader, TMVA::Types::kLikelihood,
"LikelihoodKDE",
300 "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" );
303 if (Use[
"LikelihoodMIX"])
304 factory->BookMethod( dataloader, TMVA::Types::kLikelihood,
"LikelihoodMIX",
305 "!H:!V:!TransformOutput:PDFInterpolSig[0]=KDE:PDFInterpolBkg[0]=KDE:PDFInterpolSig[1]=KDE:PDFInterpolBkg[1]=KDE:PDFInterpolSig[2]=Spline2:PDFInterpolBkg[2]=Spline2:PDFInterpolSig[3]=Spline2:PDFInterpolBkg[3]=Spline2:KDEtype=Gauss:KDEiter=Nonadaptive:KDEborder=None:NAvEvtPerBin=50" );
313 factory->BookMethod( dataloader, TMVA::Types::kPDERS,
"PDERS",
314 "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" );
317 factory->BookMethod( dataloader, TMVA::Types::kPDERS,
"PDERSD",
318 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=Decorrelate" );
321 factory->BookMethod( dataloader, TMVA::Types::kPDERS,
"PDERSPCA",
322 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=PCA" );
326 factory->BookMethod( dataloader, TMVA::Types::kPDEFoam,
"PDEFoam",
327 "!H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );
329 if (Use[
"PDEFoamBoost"])
330 factory->BookMethod( dataloader, TMVA::Types::kPDEFoam,
"PDEFoamBoost",
331 "!H:!V:Boost_Num=30:Boost_Transform=linear:SigBgSeparate=F:MaxDepth=4:UseYesNoCell=T:DTLogic=MisClassificationError:FillFoamWithOrigWeights=F:TailCut=0:nActiveCells=500:nBin=20:Nmin=400:Kernel=None:Compress=T" );
335 factory->BookMethod( dataloader, TMVA::Types::kKNN,
"KNN",
336 "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" );
340 factory->BookMethod( dataloader, TMVA::Types::kHMatrix,
"HMatrix",
"!H:!V:VarTransform=None" );
344 factory->BookMethod( dataloader, TMVA::Types::kLD,
"LD",
"H:!V:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
348 factory->BookMethod( dataloader, TMVA::Types::kFisher,
"Fisher",
"H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
352 factory->BookMethod( dataloader, TMVA::Types::kFisher,
"FisherG",
"H:!V:VarTransform=Gauss" );
355 if (Use[
"BoostedFisher"])
356 factory->BookMethod( dataloader, TMVA::Types::kFisher,
"BoostedFisher",
357 "H:!V:Boost_Num=20:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2:!Boost_DetailedMonitoring" );
361 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_MC",
362 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:SampleSize=100000:Sigma=0.1" );
365 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_GA",
366 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=100:Cycles=2:Steps=5:Trim=True:SaveBestGen=1" );
369 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_SA",
370 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=SA:MaxCalls=15000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );
373 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_MT",
374 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" );
377 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_GAMT",
378 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:Cycles=1:PopSize=5:Steps=5:Trim" );
381 factory->BookMethod( dataloader, TMVA::Types::kFDA,
"FDA_MCMT",
382 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:SampleSize=20" );
386 factory->BookMethod( dataloader, TMVA::Types::kMLP,
"MLP",
"H:!V:EstimatorType=MSE:NeuronType=sigmoid:VarTransform=N:NCycles=21:HiddenLayers=N-1:TestRate=1:UseRegulator" );
389 factory->BookMethod( dataloader, TMVA::Types::kMLP,
"MLPBFGS",
"H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" );
392 factory->BookMethod( dataloader, TMVA::Types::kMLP,
"MLPBNN",
"H:!V:NeuronType=tanh:VarTransform=N:NCycles=60:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" );
396 if (Use[
"DNN_CPU"] or Use[
"DNN_GPU"]) {
398 TString layoutString (
"Layout=TANH|128,TANH|128,TANH|128,LINEAR");
402 TString trainingStrategyString = (
"TrainingStrategy=LearningRate=1e-2,Momentum=0.9,"
403 "ConvergenceSteps=20,BatchSize=100,TestRepetitions=1,"
404 "WeightDecay=1e-4,Regularization=None,"
405 "DropConfig=0.0+0.5+0.5+0.5");
408 TString dnnOptions (
"!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=N:"
409 "WeightInitialization=XAVIERUNIFORM");
410 dnnOptions.Append (
":"); dnnOptions.Append (layoutString);
411 dnnOptions.Append (
":"); dnnOptions.Append (trainingStrategyString);
414 if (Use[
"DNN_GPU"]) {
415 TString gpuOptions = dnnOptions +
":Architecture=GPU";
416 factory->BookMethod(dataloader, TMVA::Types::kDL,
"DNN_GPU", gpuOptions);
419 if (Use[
"DNN_CPU"]) {
420 TString cpuOptions = dnnOptions +
":Architecture=CPU";
421 factory->BookMethod(dataloader, TMVA::Types::kDL,
"DNN_CPU", cpuOptions);
427 factory->BookMethod( dataloader, TMVA::Types::kCFMlpANN,
"CFMlpANN",
"!H:!V:NCycles=200:HiddenLayers=N+1,N" );
431 factory->BookMethod( dataloader, TMVA::Types::kTMlpANN,
"TMlpANN",
"!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3" );
435 factory->BookMethod( dataloader, TMVA::Types::kSVM,
"SVM",
"Gamma=0.25:Tol=0.001:VarTransform=Norm" );
439 factory->BookMethod( dataloader, TMVA::Types::kBDT,
"BDTG",
440 "!H:!V:NTrees=1000:MinNodeSize=2.5%:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=20:MaxDepth=2" );
443 factory->BookMethod( dataloader, TMVA::Types::kBDT,
"BDT",
444 "!H:!V:NTrees=80:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20:DoBoostMonitor" );
447 factory->BookMethod( dataloader, TMVA::Types::kBDT,
"BDTB",
448 "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20" );
451 factory->BookMethod( dataloader, TMVA::Types::kBDT,
"BDTD",
452 "!H:!V:NTrees=400:MinNodeSize=5%:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:VarTransform=Decorrelate" );
455 factory->BookMethod( dataloader, TMVA::Types::kBDT,
"BDTF",
456 "!H:!V:NTrees=50:MinNodeSize=2.5%:UseFisherCuts:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20" );
460 factory->BookMethod( dataloader, TMVA::Types::kRuleFit,
"RuleFit",
461 "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.0:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" );
477 factory->TrainAllMethods();
480 factory->TestAllMethods();
483 factory->EvaluateAllMethods();
490 std::cout <<
"==> Wrote root file: " << outputFile->GetName() << std::endl;
491 std::cout <<
"==> TMVAClassification is done!" << std::endl;
496 if (!gROOT->IsBatch()) TMVA::TMVAGui( outfileName );
501 int main(
int argc,
char** argv )
505 for (
int i=1;
i<argc;
i++) {
506 TString regMethod(argv[
i]);
507 if(regMethod==
"-b" || regMethod==
"--batch")
continue;
508 if (!methodList.IsNull()) methodList += TString(
",");
509 methodList += regMethod;