FairRoot/PandaRoot
Functions
TMVATraining.C File Reference
#include "TMVA/Factory.h"
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TRegexp.h"
#include "TEventList.h"
#include "TLeaf.h"
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>

Go to the source code of this file.

Functions

int gettype (TTree *t, TString varname)
 
int SplitString (TString s, TString delim, TString *toks, int maxtoks)
 
TString getFromCut (TString vars)
 
int TMVATraining (TString fname, TString vars, TString precut="", TString treename="")
 

Function Documentation

TString getFromCut ( TString  vars)

Definition at line 47 of file TMVATraining.C.

References i, n, res, SplitString(), TString, and v.

48 {
49  TString toks[50];
50  int n=SplitString(vars, "&&", toks, 50);
51  TRegexp rvar("[_a-zA-Z][_a-zA-Z0-9]*");
52 
53  TString res=" ";
54 
55  for (int i=0;i<n;++i)
56  {
57  TString v = toks[i](rvar);
58  if (v!="")
59  {
60  if (v=="tag" || res.Contains(" "+v+" ")) continue;
61  res+=v+" ";
62  }
63  }
64 
65  res = res.Strip(TString::kBoth);
66 
67  return res;
68 }
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
int n
__m128 v
Definition: P4_F32vec4.h:4
TString vars[MAX]
Definition: autocutx.C:34
int SplitString(TString s, TString delim, StrVec &toks)
Definition: invexp.C:43
int gettype ( TTree *  t,
TString  varname 
)

Definition at line 16 of file TMVATraining.C.

References TString.

17 {
18  if (t->GetBranch(varname)==0) return -1;
19 
20  TString leaftype = t->GetLeaf(varname)->GetTypeName();
21 
22  if (leaftype=="Float_t") return 0;
23  else if (leaftype=="Int_t") return 1;
24  else if (leaftype=="Bool_t") return 2;
25 
26  return -1;
27 }
TTree * t
Definition: bump_analys.C:13
int SplitString ( TString  s,
TString  delim,
TString toks,
int  maxtoks 
)

Definition at line 31 of file TMVATraining.C.

References i.

32 {
33  TObjArray *tok = s.Tokenize(delim);
34  int N = tok->GetEntries();
35  for (int i=0;i<N;++i)
36  if (i<maxtoks)
37  {
38  toks[i] = ((TObjString*)tok->At(i))->String();
39  toks[i].ReplaceAll("\t","");
40  toks[i] = toks[i].Strip(TString::kBoth);
41  }
42  return N;
43 }
Int_t i
Definition: run_full.C:25
TLorentzVector s
Definition: Pnd2DStar.C:50
int TMVATraining ( TString  fname,
TString  vars,
TString  precut = "",
TString  treename = "" 
)

Definition at line 72 of file TMVATraining.C.

References f, getFromCut(), gettype(), i, nsig, SplitString(), t, and TString.

73 {
74  if (vars.Contains("&&")) vars = getFromCut(vars);
75  cout <<"Vars : "<<vars<<endl;
76 
77  TString sigcut = "tag&&mode%1000!=900";
78  TString bkgcut = "tag&&mode%1000==900";
79 
80  if (precut!="")
81  {
82  cout <<"Precut : "<<precut<<endl;
83  sigcut += "&&" + precut;
84  bkgcut += "&&" + precut;
85  }
86 
87  TRegexp rntp("n[0-9][0-9][0-9]");
88  TRegexp rmod("M[0-9][0-9][0-9]");
89  if (treename=="") treename = fname(rntp);
90  TString smode = fname(rmod);
91 
92  TString code = smode+treename(1,3);
93 
94  TFile *f = TFile::Open(fname);
95  TTree *t =(TTree*) f->Get(treename);
96 
97  TFile* outputFile = TFile::Open( "TMVA_"+code+".root", "RECREATE" );
98 
99  TMVA::Factory *factory = new TMVA::Factory( code, outputFile, "Silent:!V:Transformations=I;N;D");
100 
101  TString toks[30];
102  int N = SplitString(vars," ",toks,30);
103 
104  for (int i=0;i<N;++i)
105  {
106  int btype = gettype(t, toks[i]);
107  if (btype==0 || btype==1)
108  factory->AddVariable(toks[i], btype==0?'F':'I');
109  }
110 
111  factory->AddTree( t, "Signal", 1.0, sigcut.Data());
112  factory->AddTree( t, "Background", 1.0, bkgcut.Data());
113 
114  int nsig = t->GetEntries(sigcut);
115  int nbkg = t->GetEntries(bkgcut);
116 
117  factory->PrepareTrainingAndTestTree( "", int(nsig*0.8), int(nbkg*0.8), int(nsig*0.19), int(nbkg*0.19));
118 
119 // factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood","!V:NAvEvtPerBin=50" );
120  //factory->BookMethod( TMVA::Types::kMLP, "MLP", "!V:NCycles=50:HiddenLayers=10,10:TestRate=5" );
121 
122  // new verion 4.2.0
123 // factory->BookMethod( TMVA::Types::kBDT, "BDT", "!V:nTrees=400:BoostType=AdaBoost:nCuts=10:MaxDepth=3:UseFisherCuts:DoPreselection" );
124  // old verion 4.1.3
125  factory->BookMethod( TMVA::Types::kBDT, "BDT", "!V:nTrees=400:BoostType=AdaBoost:nCuts=10:NNodesMax=10" );
126 
127  factory->TrainAllMethods();
128  //factory->TestAllMethods();
129  //factory->EvaluateAllMethods();
130 
131  outputFile->Close();
132  delete factory;
133  return 0;
134 }
Int_t i
Definition: run_full.C:25
TString getFromCut(TString vars)
Definition: TMVAApply.C:116
int gettype(TTree *t, TString varname)
Definition: autocutx.C:51
TString vars[MAX]
Definition: autocutx.C:34
TFile * f
Definition: bump_analys.C:12
int SplitString(TString s, TString delim, StrVec &toks)
Definition: invexp.C:43
int nsig
Definition: toy_core.C:46
TTree * t
Definition: bump_analys.C:13