ApplicationSA.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: TMVAMulticlassApplication *
5  * *
6  * This macro provides a simple example on how to use the trained multiclass *
7  * classifiers within an analysis module *
8  **********************************************************************************/
9 
10 #include <cstdlib>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TString.h"
19 #include "TSystem.h"
20 #include "TROOT.h"
21 #include "TStopwatch.h"
22 #include "TH1F.h"
23 
24 #if not defined(__CINT__) || defined(__MAKECINT__)
25 #include "TMVA/Tools.h"
26 #include "TMVA/Reader.h"
27 #endif
28 
29 using namespace TMVA;
30 
31 void ApplicationSA( TString myMethodList = "" )
32 {
33 #ifdef __CINT__
34  gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
35 #endif
36 
37  TMVA::Tools::Instance();
38  //---------------------------------------------------------------
39  // default MVA methods to be trained + tested
40  std::map<std::string,int> Use;
41  Use["BDTA"] = 1;
42  //---------------------------------------------------------------
43 
45  std::cout << "==> Start CLAApplication" << std::endl;
46  if (myMethodList != "") {
47  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
48 
49  std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
50  for (UInt_t i=0; i<mlist.size(); i++) {
51  std::string regMethod(mlist[i]);
52  if (Use.find(regMethod) == Use.end()) {
53  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
54  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " " << std::endl;
55  std::cout << std::endl;
56  return;
57  }
58  Use[regMethod] = 1;
59  }
60  }
61 
62  // create the Reader object
63  //TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
64  TMVA::Reader* reader = new TMVA::Reader( "" );
65 
66  // create a set of variables and declare them to the reader
67  // the variable names must corresponds in name and type to
68  // those given in the weight file(s) that you use
69  //Float_t anglekal, dirFY, boxmaxFY, kalnhit, kallen, kalfwdcell, kalbakcell, scatt, nhit, energy, boxminFY, nkal, ncid, nueid, numuid, nutauid;
71  reader->AddVariable( "cosmicid", &cosmicid );
72  reader->AddVariable( "partptp", &partptp );
73  reader->AddVariable( "shwnhit", &shwnhit );
74  reader->AddVariable( "shwxminusy", &shwxminusy );
75  reader->AddVariable( "shwxplusy", &shwxplusy );
76  reader->AddVariable( "shwxovery", &shwxovery );
77  reader->AddVariable( "shwcalE", &shwcalE );
78  reader->AddVariable( "shwdirY", &shwdirY );
79  reader->AddVariable( "shwlen", &shwlen );
80  reader->AddVariable( "shwwwidth", &shwwwidth );
81  reader->AddVariable( "shwGap", &shwGap );
82  reader->AddVariable( "nshwlid", &nshwlid );
83  reader->AddVariable( "nmiphit", &nmiphit );
84 
85  // book the MVA methods
86  TString dir = "weights/";
87  TString prefix = "TrainingSA";
88 
89  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
90  if (it->second) {
91  TString methodName = TString(it->first) + TString(" method");
92  TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
93  reader->BookMVA( methodName, weightfile );
94  }
95  }
96 
97  // book output histograms
98  UInt_t nbin = 100;
99  TH1F *BDTA_NC;
100  TH1F *BDTA_CS;
101  //TH1F *histMLP_signal(0), *histBDTG_signal(0), *histFDAGA_signal(0), *histPDEFoam_signal(0);
102  if (Use["BDTA"])
103  //histBDTA_NC = new TH1F( "MVA_BDTA_NC", "MVA_BDT_NC", nbin, 0., 1.1 );
104  BDTA_NC = new TH1F( "MVA_BDTA_NC", "MVA_BDT_NC", nbin, 0.0, 1.1 );
105  BDTA_CS = new TH1F( "MVA_BDTA_CS", "MVA_BDT_CS", nbin, 0.0, 1.1 );
106 
107  TFile *input(0);
108  TString fname = "TrainingSA.root";
109 
110  if (!gSystem->AccessPathName( fname )) {
111  input = TFile::Open( fname ); // check if file in local directory exists
112  }
113  if (!input) {
114  std::cout << "ERROR: could not open data file, please generate example data first!" << std::endl;
115  exit(1);
116  }
117  std::cout << "--- TMVAMulticlassApp : Using input file: " << input->GetName() << std::endl;
118 
119  // prepare the tree
120  // - here the variable names have to corresponds to your tree
121  // - you can use the same variables as above which is slightly faster,
122  // but of course you can use different ones and copy the values inside the event loop
123 
124  //TTree* theTree = (TTree*)input->Get("ncTree");
125  TTree* theTree = (TTree*)input->Get("csTree");
126  std::cout << "--- Select signal sample" << std::endl;
127  theTree->SetBranchAddress( "ncid", &ncid);
128  theTree->SetBranchAddress( "partptp", &partptp);
129  theTree->SetBranchAddress( "shwnhit", &shwnhit);
130  theTree->SetBranchAddress( "shwxminusy", &shwxminusy);
131  theTree->SetBranchAddress( "shwxplusy", &shwxplusy);
132  theTree->SetBranchAddress( "shwxovery", &shwxovery);
133  theTree->SetBranchAddress( "shwcalE", &shwcalE);
134  theTree->SetBranchAddress( "shwdirY", &shwdirY);
135  theTree->SetBranchAddress( "shwlen", &shwlen);
136  theTree->SetBranchAddress( "shwwwidth", &shwwwidth);
137  theTree->SetBranchAddress( "shwGap", &shwGap);
138  theTree->SetBranchAddress( "nshwlid", &nshwlid);
139  theTree->SetBranchAddress( "nmiphit", &nmiphit);
140 
141  std::cout << "--- Processing: " << theTree->GetEntries() << "events" << std::endl;
142  TStopwatch sw;
143  sw.Start();
144 
145  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
146  if (ievt%1000 == 0){
147  std::cout << "--- ... Processing event: " << ievt << std::endl;
148  }
149  //if (ievt == 3600) break;
150  //if (ievt == 133400) break;
151  theTree->GetEntry(ievt);
152 
153  if (Use["BDTA"])
154  //histBDTA_NC->Fill((reader->EvaluateMulticlass( "BDTA method" ))[0]);
155  //BDTA_NC->Fill((reader->EvaluateMVA( "BDTA method" ))[0]);
156  BDTA_CS->Fill((reader->EvaluateMVA( "BDTA method" ))[0]);
157  //histBDTA_NC-Fill(reader->EvaluateMVA("BDTA"));
158  }
159 
160  // get elapsed time
161  sw.Stop();
162  std::cout << "--- End of event loop: "; sw.Print();
163 
164  //TFile *target = new TFile( "RealBDTNC.root","RECREATE" );
165  TFile *target = new TFile( "RealBDTCS.root","RECREATE" );
166  if (Use["BDTA"])
167  //BDTA_NC->Write();
168  BDTA_CS->Write();
169  target->Close();
170  std::cout << "--- Created root file: \"RealBDTNC.root\" containing the MVA output histograms" << std::endl;
171  delete reader;
172  std::cout << "==> TMVAApplication is done!" << std::endl << std::endl;
173 }
float nshwlid
Definition: FillPIDs.h:18
const XML_Char * target
Definition: expat.h:268
set< int >::iterator it
float shwGap
float shwxplusy
void ApplicationSA(TString myMethodList="")
Definition: ApplicationSA.C:31
float shwcalE
float cosmicid
float shwlen
float nmiphit
float partptp
const XML_Char * prefix
Definition: expat.h:380
float shwdirY
OStream cout
Definition: OStream.cxx:6
Float_t sw
Definition: plot.C:20
float shwxovery
TDirectory * dir
Definition: macro.C:5
float shwxminusy
exit(0)
Definition: tmvaglob.h:28
float shwnhit
enum BeamMode string