CLAShower.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 CLAShower( TString myMethodList = "" )
46 {
47  /*
48  * The explicit loading of the shared libTMVA is done in TMVAlogon.C, defined in .rootrc *
49  * if you use your private .rootrc, or run from a different directory, *
50  * please copy the corresponding lines from .rootrc methods to be processed can be given as an argument; use format: *
51  * mylinux~> root -l CLA.C\(\"myMethod1,myMethod2,myMethod3\"\) *
52  * if you like to use a method via the plugin mechanism, it is recommended to use *
53  * mylinux~> root -l CLA.C\(\"P_myMethod\"\) */
54 
55  // This loads the library
56  TMVA::Tools::Instance();
57 
58  // Default MVA methods to be trained + tested
59  std::map<std::string,int> Use;
60 
61  // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
62  //Use["MLP"] = 0; // Recommended ANN
63  //Use["MLPBFGS"] = 0; // Recommended ANN with optional training method
64  //Use["MLPBNN"] = 0; // Recommended ANN with BFGS training method and bayesian regulator
65  //Use["CFMlpANN"] = 0; // Depreciated ANN from ALEPH
66  //Use["TMlpANN"] = 0; // ROOT's own ANN
67  // --- Boosted Decision Trees
68  Use["BDTA"] = 1; // uses Adaptive Boost
69  //Use["BDTG"] = 0; // uses Gradient Boost
70  //Use["BDTB"] = 0; // uses Bagging
71  //Use["BDTD"] = 0; // decorrelation + Adaptive Boost
72  //Use["BDTF"] = 0; // allow usage of fisher discriminant for node splitting
73  //---------------------------------------------------------------------------------------------------------------------------------------//
75  std::cout << "=============================================> Start Classification <=========================================" << std::endl;
76 
77  // Select methods (don't look at this code - not of interest)
78  if (myMethodList != "") {
79  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
80 
81  std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
82  for (UInt_t i=0; i<mlist.size(); i++) {
83  std::string regMethod(mlist[i]);
84 
85  if (Use.find(regMethod) == Use.end()) {
86  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
87  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
88  std::cout << std::endl;
89  return;
90  }
91  Use[regMethod] = 1;
92  }
93  }
94 
95  // ------------------------------------------------------------------------------------------------------------------------------------- //
96 
97  // --- Here the preparation phase begins
98 
99  // Create a ROOT output file where TMVA will store ntuples, histograms, etc.
100  TString outfileName( "CLAShower.root" );
101  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
102 
103  /* Create the factory object. Later we can choose the methods whose performance you'd like to investigate. *
104  * The factory is the only TMVA object we have to interact with. *
105  * The first argument is the base of the name of all the weightfiles in the directory weight *
106  * The second argument is the output file for the training results *
107  * All TMVA output can be suppressed by removing the "!" (not) in front of the "Silent" argument in the option string
108  * If you wish to modify default settings (please check "src/Config.h" to see all available global options) */
109  TMVA::Factory *factory = new TMVA::Factory( "CLAShower", outputFile,"V");
110  //"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
111 
112 
113  // -------------------------------------------------------------------------------------------------------------------------------------- //
114 
115  /* Define the input variables that shall be used for the MVA training
116  * Note that we may also use variable expressions, such as: "3*var1/var2*abs(var3)"
117  * [all types of expressions that can also be parsed by TTree::Draw( "expression" )*/
118 
119  /* We can add so-called "Spectator variables", which are not used in the MVA training, *
120  * but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the *
121  * input variables, the response values of all trained MVAs, and the spectator variables */
122 
123  // --------------------------------------------------------------------------------------------------------------------------------------- //
124 
125  /* Read training and test data *
126  * (it is also possible to use ASCII format as input -> On the agenda ) */
127 
128  // Getting the input file and grabing the TTree
129  TString fInput = "Track_TRA.root";
130  TFile *input = TFile::Open(fInput);
131  //Get the signal and background trees from TFile source(s)
132  //TTree *sigTree = (TTree*)input->Get("muTree");
133  TTree *sigTree = (TTree*)input->Get("ncTree");
134  TTree *bkgTree = (TTree*)input->Get("csTree");
135  //Set the event weights per tree
136  Double_t sigWeight = 1.0;
137  Double_t bkgWeight = 1.0;
138  //Register the trees
139  factory->AddSignalTree (sigTree, sigWeight);
140  factory->AddBackgroundTree (bkgTree, bkgWeight);
141  ///Adding Variables into factory
142  factory->AddVariable( "ncid", "CVN NC ID", 'F' ); // CVN
143  factory->AddVariable( "partptp", "PartPtp", 'F' ); // NueCosRe
144  factory->AddVariable( "shwnhit", "Leading Prong Number of Hits", 'F' ); // Shower Branch
145  //factory->AddVariable( "shwnhitx", "Leading Prong Number of Hits X View", 'F' ); // Shower Branch
146  //factory->AddVariable( "shwnhity", "Leading Prong Number of Hits Y View", 'F' ); // Shower Branch
147  factory->AddVariable( "shwxminusy", "X View - Y View", 'F' ); // Slice Branch
148  factory->AddVariable( "shwxplusy", "X View + Y View", 'F' ); // Slice Branch
149  factory->AddVariable( "shwxovery", "X-Y/X+Y", 'F' ); // Shower Branch
150  factory->AddVariable( "shwcalE", "Leading Prong CalE", 'F' ); // Shower Branch
151  factory->AddVariable( "shwdirY", "Leading Shower Y Direction", 'F' ); // Shower Branch
152  factory->AddVariable( "shwlen", "Leading Shower Length", 'F' ); // Shower Branch
153  factory->AddVariable( "shwwwidth", "Leading Shower Width", 'F' ); // Shower Branch
154  factory->AddVariable( "shwGap", "Leading Shower Gap", 'F' ); // Shower Branch
155  factory->AddVariable( "nshwlid", "Number of Shower/Prong", 'F' ); // Shower Branch
156  factory->AddVariable( "nmiphit", "Number of MIP Hits in the slice", 'F' ); // Slice Branch
157 
158  TCut preselectionCut="";
159  //factory->PrepareTrainingAndTestTree( preselectionCut,"nTrain_Signal=10000:nTrain_Background=10000:SplitMode=Random:NormMode=NumEvents:!V" );
160 
161  factory->PrepareTrainingAndTestTree( "", "SplitMode=random" );
162 
163  /* ---- Book MVA methods *
164 
165  // TMVA ANN: MLP (recommended ANN) -- all ANNs in TMVA are Multilayer Perceptrons
166  /*if (Use["MLP"])
167  factory->BookMethod( TMVA::Types::kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:!UseRegulator" );
168 
169  if (Use["MLPBFGS"])
170  factory->BookMethod( TMVA::Types::kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" );
171 
172  if (Use["MLPBNN"])
173  factory->BookMethod( TMVA::Types::kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" ); // BFGS training with bayesian regulators
174 
175  // CF(Clermont-Ferrand)ANN
176  if (Use["CFMlpANN"])
177  factory->BookMethod( TMVA::Types::kCFMlpANN, "CFMlpANN", "!H:!V:NCycles=2000:HiddenLayers=N+1,N" ); // n_cycles:#nodes:#nodes:...
178 
179  // Tmlp(Root)ANN
180  if (Use["TMlpANN"])
181  factory->BookMethod( TMVA::Types::kTMlpANN, "TMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3" );
182  // n_cycles:#nodes:#nodes:...
183  */
184  // Boosted Decision Trees
185  if (Use["BDTA"]) // Adaptive Boost
186  // factory->BookMethod( TMVA::Types::kBDT, "BDT",
187  // "!H:!V:NTrees=200:nEventsMin=10:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
188  factory->BookMethod( TMVA::Types::kBDT, "BDTA",
189  "H:V:NTrees=500:InverseBoostNegWeights::BoostType=RealAdaBoost:AdaBoostBeta=0.1:nCuts=40:MaxDepth=5:MinNodeSize=2%:NodePurityLimit=0.9");
190  //"!H:!V:NTrees=850:nEventsMin=150:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
191 
192 
193  /*if (Use["BDTG"]) // Gradient Boost
194  factory->BookMethod( TMVA::Types::kBDT, "BDTG",
195  "!H:!V:NTrees=1000:BoostType=Grad:AdaBoostR2Loss=Linear:Shrinkage=0.10:UseBaggedGrad:GradBaggingFraction=0.5:nCuts=20:NNodesMax=5" );
196 
197 
198  if (Use["BDTB"]) // Bagging
199  factory->BookMethod( TMVA::Types::kBDT, "BDTB",
200  "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
201 
202  if (Use["BDTD"]) // Decorrelation + Adaptive Boost
203  factory->BookMethod( TMVA::Types::kBDT, "BDTD",
204  "!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" );
205 
206  if (Use["BDTF"]) // Allow Using Fisher discriminant in node splitting for (strong) linearly correlated variables
207  factory->BookMethod( TMVA::Types::kBDT, "BDTMitFisher",
208  "!H:!V:NTrees=50:nEventsMin=150:UseFisherCuts:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );*/
209 
210  // For an example of the category classifier usage, see: TMVAClassificationCategory
211 
212  // --------------------------------------------------------------------------------------------------
213 
214  // ---- Now you can optimize the setting (configuration) of the MVAs using the set of training events
215 
216  // factory->OptimizeAllMethods("SigEffAt001","Scan");
217  // factory->OptimizeAllMethods("ROCIntegral","GA");
218 
219  // --------------------------------------------------------------------------------------------------
220 
221  // ---- Now you can tell the factory to train, test, and evaluate the MVAs
222 
223  // Train MVAs using the set of training events
224  factory->TrainAllMethods();
225 
226  // ---- Evaluate all MVAs using the set of test events
227  factory->TestAllMethods();
228 
229  // ----- Evaluate and compare performance of all configured MVAs
230  factory->EvaluateAllMethods();
231 
232  // --------------------------------------------------------------
233 
234  // Save the output
235  outputFile->Close();
236 
237  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
238  std::cout << "==> TMVAClassification is done!" << std::endl;
239 
240  delete factory;
241 
242  // Launch the GUI for the root macros
243  if (!gROOT->IsBatch()) TMVAGui( outfileName );
244 }
set< int >::iterator it
void CLAShower(TString myMethodList="")
Definition: CLAShower.C:45
OStream cout
Definition: OStream.cxx:6
enum BeamMode string