make_tmva_NCPi0s_2selection.C
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <map>
4 #include <string>
5 
6 #include "TChain.h"
7 #include "TFile.h"
8 #include "TTree.h"
9 #include "TString.h"
10 #include "TObjString.h"
11 #include "TSystem.h"
12 #include "TROOT.h"
13 
14 //#include "TMVARegGui.C"
15 
16 #if not defined(__CINT__) || defined(__MAKECINT__)
17 #include "TMVA/Tools.h"
18 #include "TMVA/Factory.h"
19 #include "TMVA/TMVAGui.h"
20 #include "TMVA/Reader.h"
21 #endif
22 
23 using namespace TMVA;
24 
26 {
28  std::cout << "==> Start TMVA NCPi0s selection... " << std::endl;
29  //---------------------
30  // Basic I/O
31 
32 
33  // Adding variables to Factory2 and also some spectator variables
34 
35  //input filename
36  TString fileName="BdtTree9VarsOptimizedVolumes.root"; // tree is made of 50% of dataset
37  TFile *inputFile = new TFile(fileName);
38  if(!(inputFile)){
39  std::cout<<" --- "<< fileName <<" file doesn't exist, aborting..."<<std::endl;
40  return -1;
41  }
42  // std::cout << "--- TMVA NCPi0 Selection: Using input file: " << inputFile->GetName() << std::endl;
43 
44  //float pi0mass1=0.135;
45  //float prongenergy= "prong1Energy+prong2Energy";
46  //float recoenergy= prongenergy-pi0mass1;
47 
48  //TCut pinumu = "(IsNumuNC==1) && (IsPi0==1)";
49  TCut pinc = "(IsNC==1) && (IsPi0all>=1)";
50  TCut remcut = "remid<0.375";
51  TCut recocut= "((prong1Energy)-0.135)>0.0";
52 
53 
54  // std::cout << "Reco Energy:" << recocut<< std::endl;
55 
56  //TCut signalCut = pinumu && remcut && recocut;
57  //TCut bkgCut = !pinumu && remcut && recocut;
58 
59  TCut signalCut = pinc && remcut && recocut;
60  TCut bkgCut = !pinc && remcut && recocut;
61 
62 // Defining the output file
63  TFile* outputFile = TFile::Open( "tmva_trained9vars.root", "RECREATE" );
64 
65  //---------------------
66  // Declaring the factory and variables
67  // TMVA::Factory *factory = new TMVA::Factory( "MVAnalysis", outputFile,"V:Color:DrawProgressBar:AnalysisType=Classification");
68  TMVA::Factory *factory2 = new TMVA::Factory( "MVAnalysis", outputFile,"V:Color:DrawProgressBar:AnalysisType=Classification");
69 
70 
71  //factory2->AddVariable("cvnnumu","CVN_numu","", 'F');
72  factory2->AddVariable("cvnphoton","CVN_photon","", 'F');
73  factory2->AddVariable("prong1epi0LLL","Prong epi0LLL","", 'F');
74  factory2->AddVariable("prong1epiLLL","Prong epiLLL","", 'F');
75  factory2->AddVariable("prong1ContPl","Prong Cont planes","", 'F');
76  factory2->AddVariable("prong1epLLT","Prong epLLT","", 'F');
77  //factory2->AddVariable("prong1epiLLT","Prong epiLLT","",'F');
78  factory2->AddVariable("prong1Width","Prong Width","cm", 'F');
79  //factory2->AddVariable("cvnncid","CVN_nc","", 'F');
80  factory2->AddVariable("prong1dedx","Prong dedx","GeV/cm", 'F');
81  factory2->AddVariable("prong1epLLL","Prong epLLL","", 'F');
82  //factory2->AddVariable("prong1epi0LLT","Prong epi0LLT","", 'F');
83  //factory2->AddVariable("nhit","Number of Hits in Slice","", 'I');
84  //factory2->AddVariable("prong1Length","Prong Length","cm", 'F');
85  //factory2->AddVariable("prong1Energy","Prong Energy","GeV",'F');
86  factory2->AddVariable("prong1MissingPl","Missing Planes","",'F');
87  //factory2->AddVariable("png1dirx","Prong X Direction","",'F');
88  //factory2->AddVariable("png1diry","Prong Y Direction","",'F');
89  //factory2->AddVariable("png1dirx","Prong Z Direction","",'F');
90  //factory2->AddVariable("prong1VtxGev","Energy Around Vtx","GeV",'F');
91  //factory2->AddVariable("cvnnue","Cvn_nue","",'F');
92  //factory2->AddVariable("cvnnutau","Cvn_nutau","",'F');
93  //factory2->AddVariable("slcCalE","Slice Energy","GeV",'F');
94 
95 
96 
97 
99  std::cout<<" ++++ Variables and spectators added!!"<<std::endl;
101 
102 // Signal and Bkg events located in the same tree
103 
104  // Getting the input file and grabing the TTree
105  // TString fileName = "preselVariables.root";
106 
107 
108 
109 // --- Register the training and test trees
110  TTree *inputTree = (TTree*)inputFile->Get("fPreSelectionTree");
111 
112 
113  // factory->SetInputTrees( inputTree, signalCut, bkgCut );
114  factory2->SetInputTrees( inputTree, signalCut, bkgCut );
115 
117  std::cout<<" ++++ Input tree set!!"<<std::endl;
119  //---------------------
120  // Preparing the training and testing data
121 
122  // If no numbers of events are given, half of the events in the tree are used
123  // for training, and the other half for testing:
124  //factory->PrepareTrainingAndTestTree( "", "SplitMode=random" );
125  factory2->PrepareTrainingAndTestTree( "", "SplitMode=random" );
126 
128  std::cout<<" ++++ Training and Test Tree prepared!!"<<std::endl;
130 
131  //---------------------
132  // Booking TMVA methods
133 
134 
135 
136  // Boosted Decision Trees (Grad Boost)
137 
138 
139  factory2->BookMethod(TMVA::Types::kBDT, "BDTG","H:V:NTrees=500:BoostType=Grad:IgnoreNegWeightsInTraining:Shrinkage=0.5:nCuts=40:MaxDepth=2:MinNodeSize=2%");
140 
141 
142 
144  std::cout<<" ++++ All Methods booked!"<<std::endl;
146  //---------------------
147  // Train MVAs using the set of training events
148  //factory->TrainAllMethods();
149  factory2->TrainAllMethods();
150 
152  std::cout<<" ++++ Training All Methods!!"<<std::endl;
154  // ---- Evaluate all MVAs using the set of test events
155  //factory->TestAllMethods();
156 factory2->TestAllMethods();
157 
159  std::cout<<" ++++ Testing All Methods!!"<<std::endl;
161  // ----- Evaluate and compare performance of all configured MVAs
162  //factory->EvaluateAllMethods();
163  factory2->EvaluateAllMethods();
164 
166  std::cout<<" ++++ Evaluating All Methods!!"<<std::endl;
168  //---------------------
169  // Save the output
170 
171  outputFile->Close();
172 
173  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
174  std::cout << "==> TMVA NCPi0s selection is done!" << std::endl;
175 
176  //delete factory;
177  delete factory2;
178 
179  // Launch the GUI for the root macros
180  //if (!gROOT->IsBatch()) TMVA::TMVAGui( outputFile );
181 
182 }
TFile * inputFile
Definition: PlotXSec.C:134
fileName
Definition: plotROC.py:78
OStream cout
Definition: OStream.cxx:6
Definition: tmvaglob.h:28
void make_tmva_NCPi0s_2selection()