62int TMVAClassification(
TString myMethodList =
"" )
77 std::map<std::string,int> Use;
87 Use[
"Likelihood"] = 1;
88 Use[
"LikelihoodD"] = 0;
89 Use[
"LikelihoodPCA"] = 1;
90 Use[
"LikelihoodKDE"] = 0;
91 Use[
"LikelihoodMIX"] = 0;
98 Use[
"PDEFoamBoost"] = 0;
105 Use[
"BoostedFisher"] = 0;
148 std::cout << std::endl;
149 std::cout <<
"==> Start TMVAClassification" << std::endl;
152 if (myMethodList !=
"") {
153 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
156 for (
UInt_t i=0; i<mlist.size(); i++) {
157 std::string regMethod(mlist[i]);
159 if (Use.find(regMethod) == Use.end()) {
160 std::cout <<
"Method \"" << regMethod <<
"\" not known in TMVA under this name. Choose among the following:" << std::endl;
161 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first <<
" ";
162 std::cout << std::endl;
179 std::unique_ptr<TFile>
input{
TFile::Open(
"http://root.cern/files/tmva_class_example.root",
"CACHEREAD")};
181 throw std::runtime_error(
"ERROR: could not open data file");
183 std::cout <<
"--- TMVAClassification : Using input file: " <<
input->GetName() << std::endl;
191 TString outfileName(
"TMVAC.root");
192 std::unique_ptr<TFile> outputFile{
TFile::Open(outfileName,
"RECREATE")};
193 if (!outputFile || outputFile->IsZombie()) {
194 throw std::runtime_error(
"ERROR: could not open output file");
207 auto factory = std::make_unique<TMVA::Factory>(
208 "TMVAClassification", outputFile.get(),
209 "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
210 auto dataloader_raii = std::make_unique<TMVA::DataLoader>(
"dataset");
211 auto *dataloader = dataloader_raii.get();
221 dataloader->AddVariable(
"myvar1 := var1+var2",
'F' );
222 dataloader->AddVariable(
"myvar2 := var1-var2",
"Expression 2",
"",
'F' );
223 dataloader->AddVariable(
"var3",
"Variable 3",
"units",
'F' );
224 dataloader->AddVariable(
"var4",
"Variable 4",
"units",
'F' );
230 dataloader->AddSpectator(
"spec1 := var1*2",
"Spectator 1",
"units",
'F' );
231 dataloader->AddSpectator(
"spec2 := var1*3",
"Spectator 2",
"units",
'F' );
239 dataloader->AddSignalTree ( signalTree, signalWeight );
240 dataloader->AddBackgroundTree( background, backgroundWeight );
285 dataloader->SetBackgroundWeightExpression(
"weight" );
302 dataloader->PrepareTrainingAndTestTree( mycuts, mycutb,
303 "nTrain_Signal=1000:nTrain_Background=1000:SplitMode=Random:NormMode=NumEvents:!V" );
315 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );
319 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );
323 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=PCA" );
327 "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" );
331 "!H:!V:FitMethod=SA:EffSel:MaxCalls=150000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );
334 if (Use[
"Likelihood"])
336 "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
339 if (Use[
"LikelihoodD"])
341 "!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" );
344 if (Use[
"LikelihoodPCA"])
346 "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" );
349 if (Use[
"LikelihoodKDE"])
351 "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" );
354 if (Use[
"LikelihoodMIX"])
356 "!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" );
365 "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" );
369 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=Decorrelate" );
373 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=PCA" );
378 "!H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );
380 if (Use[
"PDEFoamBoost"])
382 "!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" );
387 "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" );
395 factory->BookMethod( dataloader,
TMVA::Types::kLD,
"LD",
"H:!V:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
399 factory->BookMethod( dataloader,
TMVA::Types::kFisher,
"Fisher",
"H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
406 if (Use[
"BoostedFisher"])
408 "H:!V:Boost_Num=20:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2:!Boost_DetailedMonitoring" );
413 "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" );
417 "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" );
421 "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" );
425 "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" );
429 "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" );
433 "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" );
437 factory->BookMethod( dataloader,
TMVA::Types::kMLP,
"MLP",
"H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:!UseRegulator" );
440 factory->BookMethod( dataloader,
TMVA::Types::kMLP,
"MLPBFGS",
"H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" );
443 factory->BookMethod( dataloader,
TMVA::Types::kMLP,
"MLPBNN",
"H:!V:NeuronType=tanh:VarTransform=N:NCycles=60:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" );
447 if (Use[
"DNN_CPU"] or Use[
"DNN_GPU"]) {
449 TString layoutString (
"Layout=TANH|128,TANH|128,TANH|128,LINEAR");
453 TString trainingStrategyString = (
"TrainingStrategy=LearningRate=1e-2,Momentum=0.9,"
454 "ConvergenceSteps=20,BatchSize=100,TestRepetitions=1,"
455 "WeightDecay=1e-4,Regularization=None,"
456 "DropConfig=0.0+0.5+0.5+0.5");
459 TString dnnOptions (
"!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=N:"
460 "WeightInitialization=XAVIERUNIFORM");
461 dnnOptions.Append (
":"); dnnOptions.Append (layoutString);
462 dnnOptions.Append (
":"); dnnOptions.Append (trainingStrategyString);
465 if (Use[
"DNN_GPU"]) {
466 TString gpuOptions = dnnOptions +
":Architecture=GPU";
470 if (Use[
"DNN_CPU"]) {
471 TString cpuOptions = dnnOptions +
":Architecture=CPU";
478 factory->BookMethod( dataloader,
TMVA::Types::kCFMlpANN,
"CFMlpANN",
"!H:!V:NCycles=200:HiddenLayers=N+1,N" );
482 factory->BookMethod( dataloader,
TMVA::Types::kTMlpANN,
"TMlpANN",
"!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3" );
486 factory->BookMethod( dataloader,
TMVA::Types::kSVM,
"SVM",
"Gamma=0.25:Tol=0.001:VarTransform=Norm" );
491 "!H:!V:NTrees=1000:MinNodeSize=2.5%:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=20:MaxDepth=2" );
495 "!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20" );
499 "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20" );
503 "!H:!V:NTrees=400:MinNodeSize=5%:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:VarTransform=Decorrelate" );
507 "!H:!V:NTrees=50:MinNodeSize=2.5%:UseFisherCuts:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20" );
512 "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" );
528 factory->TrainAllMethods();
531 factory->TestAllMethods();
534 factory->EvaluateAllMethods();
541 std::cout <<
"==> Wrote root file: " << outputFile->GetName() << std::endl;
542 std::cout <<
"==> TMVAClassification is done!" << std::endl;
550int main(
int argc,
char** argv )
554 for (
int i=1; i<argc; i++) {
556 if(regMethod==
"-b" || regMethod==
"--batch")
continue;
558 methodList += regMethod;
560 return TMVAClassification(methodList);
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
A specialized string object used for TTree selections.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
static Bool_t SetCacheFileDir(std::string_view cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
A TTree represents a columnar dataset.
void TMVAGui(const char *fName="TMVA.root", TString dataset="")