TrainingSA.C
Go to the documentation of this file.
1 /**********************************************************************************
2  * Project : TMVA - a ROOT-integrated toolkit for multivariate data analysis *
3  * Package : TMVA *
4  * Root Macro: Modified from TMVAClassification *
5  * *
6  * This macro provides examples for the training and testing of the *
7  * TMVA classifiers to use CAF files *
8  * *
9  * The methods to be used can be switched on and off by means of booleans, or *
10  * via the prompt command, for example: *
11  * *
12  * root -l ./TMVAClassification.C\(\"BDT\"\) *
13  * cafe TMVAClassification.C\(\"BDT\"\) *
14  * (note that the backslashes are mandatory) *
15  * If no method given, a default set of classifiers is used. *
16  * *
17  * The output file "CafeMVA.root" can be analysed with the use of dedicated *
18  * macros (simply say: root -l <macro.C>), which can be conveniently *
19  * invoked through a GUI that will appear at the end of the run of this macro. *
20  * Launch the GUI via the command: *
21  * *
22  * root -l ./TMVAGui.C *
23  * *
24  **********************************************************************************/
25 
26 #include <cstdlib>
27 #include <iostream>
28 #include <map>
29 #include <string>
30 #include "TChain.h"
31 #include "TFile.h"
32 #include "TTree.h"
33 #include "TString.h"
34 #include "TObjString.h"
35 #include "TSystem.h"
36 #include "TROOT.h"
37 #include "TMVAGui.C"
38 #if not defined(__CINT__) || defined(__MAKECINT__)
39 // needs to be included when makecint runs (ACLIC)
40 #include "TMVA/Factory.h"
41 #include "TMVA/Tools.h"
42 #endif
43 
44 //using namespace ana;
45 void TrainingSA( TString myMethodList = "" )
46 {
47  // This loads the library
48  TMVA::Tools::Instance();
49 
50  // Default MVA methods to be trained + tested
51  std::map<std::string,int> Use;
52  // --- Boosted Decision Trees
53  Use["BDTA"] = 1; // uses Adaptive Boost
54  //---------------------------------------------------------------------------------------------------------------------------------------//
56  std::cout << "=============================================> Start Classification <=========================================" << std::endl;
57 
58  // Select methods (don't look at this code - not of interest)
59  if (myMethodList != "") {
60  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
61 
62  std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
63  for (UInt_t i=0; i<mlist.size(); i++) {
64  std::string regMethod(mlist[i]);
65 
66  if (Use.find(regMethod) == Use.end()) {
67  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
68  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
69  std::cout << std::endl;
70  return;
71  }
72  Use[regMethod] = 1;
73  }
74  }
75 
76  // ------------------------------------------------------------------------------------------------------------------------------------- //
77  // --- Here the preparation phase begins
78  // Create a ROOT output file where TMVA will store ntuples, histograms, etc.
79  TString outfileName( "BDTA.root" );
80  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
81 
82  /* Create the factory object. Later we can choose the methods whose performance you'd like to investigate. *
83  * The factory is the only TMVA object we have to interact with. *
84  * The first argument is the base of the name of all the weightfiles in the directory weight *
85  * The second argument is the output file for the training results *
86  * All TMVA output can be suppressed by removing the "!" (not) in front of the "Silent" argument in the option string
87  * If you wish to modify default settings (please check "src/Config.h" to see all available global options) */
88  TMVA::Factory *factory = new TMVA::Factory( "SA", outputFile,"V");
89  //"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
90 
91 
92  // -------------------------------------------------------------------------------------------------------------------------------------- //
93 
94  /* Define the input variables that shall be used for the MVA training
95  * Note that we may also use variable expressions, such as: "3*var1/var2*abs(var3)"
96  * [all types of expressions that can also be parsed by TTree::Draw( "expression" )*/
97 
98  /* We can add so-called "Spectator variables", which are not used in the MVA training, *
99  * but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the *
100  * input variables, the response values of all trained MVAs, and the spectator variables */
101 
102  // --------------------------------------------------------------------------------------------------------------------------------------- //
103 
104  /* Read training and test data *
105  * (it is also possible to use ASCII format as input -> On the agenda ) */
106 
107  // Getting the input file and grabing the TTree
108  // TString fInput = "Track_TRA.root";
109  TString fInput = "TrainingSA.root";
110  TFile *input = TFile::Open(fInput);
111  //Get the signal and background trees from TFile source(s)
112  TTree *sigTree = (TTree*)input->Get("ncTree");
113  TTree *bkgTree = (TTree*)input->Get("csTree");
114  //Set the event weights per tree
115  Double_t sigWeight = 1.0;
116  Double_t bkgWeight = 1.0;
117  //Register the trees
118  factory->AddSignalTree (sigTree, sigWeight);
119  factory->AddBackgroundTree (bkgTree, bkgWeight);
120  ///Adding Variables into factory
121  factory->AddVariable( "cosmicid", "CVN Cosmic ID", 'F' ); // CVN
122  factory->AddVariable( "partptp", "PartPtP", 'F' ); // NueCosRe
123  factory->AddVariable( "shwnhit", "Leading Prong Number of Hits", 'F' ); // Shower Branch
124  factory->AddVariable( "shwxminusy", "X View - Y View", 'F' ); // Slice Branch
125  factory->AddVariable( "shwxplusy", "X View + Y View", 'F' ); // Slice Branch
126  factory->AddVariable( "shwxovery", "X-Y/X+Y", 'F' ); // Shower Branch
127  factory->AddVariable( "shwcalE", "Leading Prong CalE", 'F' ); // Shower Branch
128  factory->AddVariable( "shwdirY", "Leading Shower Y Direction", 'F' ); // Shower Branch
129  factory->AddVariable( "shwlen", "Leading Shower Length", 'F' ); // Shower Branch
130  factory->AddVariable( "shwwwidth", "Leading Shower Width", 'F' ); // Shower Branch
131  factory->AddVariable( "shwGap", "Leading Shower Gap", 'F' ); // Shower Branch
132  factory->AddVariable( "nshwlid", "Number of Shower/Prong", 'F' ); // Shower Branch
133  factory->AddVariable( "nmiphit", "Number of MIP Hits in the slice", 'F' ); // Slice Branch
134 
135  TCut preselectionCut="";
136  //factory->PrepareTrainingAndTestTree( preselectionCut,"nTrain_Signal=10000:nTrain_Background=10000:SplitMode=Random:NormMode=NumEvents:!V" );
137 
138  factory->PrepareTrainingAndTestTree( "", "SplitMode=random" );
139 
140  ////////////////////---- Book MVA methods ----/////////////////////
141  // Boosted Decision Trees
142  if (Use["BDTA"]) //Real Adaptive Boost used by Kirk------------------------------
143  factory->BookMethod( TMVA::Types::kBDT, "BDTA",
144  "H:V:NTrees=500:InverseBoostNegWeights::BoostType=RealAdaBoost:Shrinkage=0.1:nCuts=40:MaxDepth=5:MinNodeSize=2%:NodePurityLimit=0.9");
145 
146  // For an example of the category classifier usage, see: TMVAClassificationCategory
147 
148  // --------------------------------------------------------------------------------------------------
149 
150  // ---- Now you can optimize the setting (configuration) of the MVAs using the set of training events
151 
152  // factory->OptimizeAllMethods("SigEffAt001","Scan");
153  // factory->OptimizeAllMethods("ROCIntegral","GA");
154 
155  // --------------------------------------------------------------------------------------------------
156 
157  // ---- Now you can tell the factory to train, test, and evaluate the MVAs
158 
159  // Train MVAs using the set of training events
160  factory->TrainAllMethods();
161 
162  // ---- Evaluate all MVAs using the set of test events
163  factory->TestAllMethods();
164 
165  // ----- Evaluate and compare performance of all configured MVAs
166  factory->EvaluateAllMethods();
167 
168  // --------------------------------------------------------------
169 
170  // Save the output
171  outputFile->Close();
172 
173  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
174  std::cout << "==> TMVAClassification is done!" << std::endl;
175 
176  delete factory;
177 
178  // Launch the GUI for the root macros
179  if (!gROOT->IsBatch()) TMVAGui( outfileName );
180 }
set< int >::iterator it
void TrainingSA(TString myMethodList="")
Definition: TrainingSA.C:45
OStream cout
Definition: OStream.cxx:6
enum BeamMode string