ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/TMVApplication.C
Revision: 1.1
Committed: Thu Nov 20 22:34:49 2008 UTC (16 years, 5 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-00-07, V00-00-06, V00-00-05, V00-00-04, V00-01-03, V00-00-02, V00-00-01
Log Message:
created /macros with TMVA scripts in it

File Contents

# User Rev Content
1 kukartse 1.1 /**********************************************************************************
2     * Project : TMVA - a Root-integrated toolkit for multivariate data analysis *
3     * Package : TMVA *
4     * Exectuable: TMVApplication *
5     * *
6     * This macro provides a simple example on how to use the trained classifiers *
7     * within an analysis module *
8     * *
9     * ------------------------------------------------------------------------------ *
10     * see also the alternative (slightly faster) way to retrieve the MVA values in *
11     * examples/TMVApplicationAlternative.cxx *
12     * ------------------------------------------------------------------------------ *
13     **********************************************************************************/
14    
15     // ---------------------------------------------------------------
16     // choose MVA methods to be applied
17     Bool_t Use_Cuts = 0;
18     Bool_t Use_CutsD = 0;
19     Bool_t Use_CutsGA = 1;
20     Bool_t Use_Likelihood = 1;
21     Bool_t Use_LikelihoodD = 0; // the "D" extension indicates decorrelated input variables (see option strings)
22     Bool_t Use_LikelihoodPCA = 1; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
23     Bool_t Use_PDERS = 1;
24     Bool_t Use_PDERSD = 0;
25     Bool_t Use_PDERSPCA = 0;
26     Bool_t Use_KNN = 1;
27     Bool_t Use_HMatrix = 1;
28     Bool_t Use_Fisher = 1;
29     Bool_t Use_FDA_GA = 0;
30     Bool_t Use_FDA_MT = 1;
31     Bool_t Use_MLP = 1; // this is the recommended ANN
32     Bool_t Use_CFMlpANN = 0;
33     Bool_t Use_TMlpANN = 0;
34     Bool_t Use_SVM_Gauss = 1;
35     Bool_t Use_SVM_Poly = 0;
36     Bool_t Use_SVM_Lin = 0;
37     Bool_t Use_BDT = 1;
38     Bool_t Use_BDTD = 0;
39     Bool_t Use_RuleFit = 1;
40     // ---------------------------------------------------------------
41    
42     void TMVApplication( TString myMethodList = "" )
43     {
44     cout << endl;
45     cout << "==> start TMVApplication" << endl;
46    
47     if (myMethodList != "") {
48     Use_CutsGA = Use_CutsD = Use_Cuts
49     = Use_LikelihoodPCA = Use_LikelihoodD = Use_Likelihood
50     = Use_PDERSPCA = Use_PDERSD = Use_PDERS
51     = Use_KNN
52     = Use_MLP = Use_CFMlpANN = Use_TMlpANN
53     = Use_HMatrix = Use_Fisher = Use_BDTD = Use_BDT = Use_RuleFit
54     = Use_SVM_Gauss = Use_SVM_Poly = Use_SVM_Lin
55     = Use_FDA_GA = Use_FDA_MT
56     = 0;
57    
58     TList* mlist = TMVA::Tools::ParseFormatLine( myMethodList, " :," );
59    
60     if (mlist->FindObject( "Cuts" ) != 0) Use_Cuts = 1;
61     if (mlist->FindObject( "CutsD" ) != 0) Use_CutsD = 1;
62     if (mlist->FindObject( "CutsGA" ) != 0) Use_CutsGA = 1;
63     if (mlist->FindObject( "Likelihood" ) != 0) Use_Likelihood = 1;
64     if (mlist->FindObject( "LikelihoodD" ) != 0) Use_LikelihoodD = 1;
65     if (mlist->FindObject( "LikelihoodPCA" ) != 0) Use_LikelihoodPCA = 1;
66     if (mlist->FindObject( "PDERS" ) != 0) Use_PDERS = 1;
67     if (mlist->FindObject( "PDERSD" ) != 0) Use_PDERSD = 1;
68     if (mlist->FindObject( "PDERSPCA" ) != 0) Use_PDERSPCA = 1;
69     if (mlist->FindObject( "KNN" ) != 0) Use_KNN = 1;
70     if (mlist->FindObject( "HMatrix" ) != 0) Use_HMatrix = 1;
71     if (mlist->FindObject( "Fisher" ) != 0) Use_Fisher = 1;
72     if (mlist->FindObject( "MLP" ) != 0) Use_MLP = 1;
73     if (mlist->FindObject( "CFMlpANN" ) != 0) Use_CFMlpANN = 1;
74     if (mlist->FindObject( "TMlpANN" ) != 0) Use_TMlpANN = 1;
75     if (mlist->FindObject( "BDTD" ) != 0) Use_BDTD = 1;
76     if (mlist->FindObject( "BDT" ) != 0) Use_BDT = 1;
77     if (mlist->FindObject( "RuleFit" ) != 0) Use_RuleFit = 1;
78     if (mlist->FindObject( "SVM_Gauss" ) != 0) Use_SVM_Gauss = 1;
79     if (mlist->FindObject( "SVM_Poly" ) != 0) Use_SVM_Poly = 1;
80     if (mlist->FindObject( "SVM_Lin" ) != 0) Use_SVM_Lin = 1;
81     if (mlist->FindObject( "FDA_MT" ) != 0) Use_FDA_MT = 1;
82     if (mlist->FindObject( "FDA_GA" ) != 0) Use_FDA_GA = 1;
83    
84     delete mlist;
85     }
86    
87     //
88     // create the Reader object
89     //
90     TMVA::Reader *reader = new TMVA::Reader("!Color");
91    
92     // create a set of variables and declare them to the reader
93     // - the variable names must corresponds in name and type to
94     // those given in the weight file(s) that you use
95     Float_t var1, var2;
96     Float_t var3, var4;
97     reader->AddVariable( "var1+var2", &var1 );
98     reader->AddVariable( "var1-var2", &var2 );
99     reader->AddVariable( "var3", &var3 );
100     reader->AddVariable( "var4", &var4 );
101    
102     //
103     // book the MVA methods
104     //
105     string dir = "weights/";
106     string prefix = "TMVAnalysis";
107    
108     if (Use_Cuts) reader->BookMVA( "Cuts method", dir + prefix + "_Cuts.weights.txt" );
109     if (Use_CutsD) reader->BookMVA( "CutsD method", dir + prefix + "_CutsD.weights.txt" );
110     if (Use_CutsGA) reader->BookMVA( "CutsGA method", dir + prefix + "_CutsGA.weights.txt" );
111     if (Use_Likelihood) reader->BookMVA( "Likelihood method", dir + prefix + "_Likelihood.weights.txt" );
112     if (Use_LikelihoodD) reader->BookMVA( "LikelihoodD method", dir + prefix + "_LikelihoodD.weights.txt" );
113     if (Use_LikelihoodPCA) reader->BookMVA( "LikelihoodPCA method", dir + prefix + "_LikelihoodPCA.weights.txt" );
114     if (Use_PDERS) reader->BookMVA( "PDERS method", dir + prefix + "_PDERS.weights.txt" );
115     if (Use_PDERSD) reader->BookMVA( "PDERSD method", dir + prefix + "_PDERSD.weights.txt" );
116     if (Use_PDERSPCA) reader->BookMVA( "PDERSPCA method", dir + prefix + "_PDERSPCA.weights.txt" );
117     if (Use_KNN) reader->BookMVA( "KNN method", dir + prefix + "_KNN.weights.txt" );
118     if (Use_HMatrix) reader->BookMVA( "HMatrix method", dir + prefix + "_HMatrix.weights.txt" );
119     if (Use_Fisher) reader->BookMVA( "Fisher method", dir + prefix + "_Fisher.weights.txt" );
120     if (Use_MLP) reader->BookMVA( "MLP method", dir + prefix + "_MLP.weights.txt" );
121     if (Use_CFMlpANN) reader->BookMVA( "CFMlpANN method", dir + prefix + "_CFMlpANN.weights.txt" );
122     if (Use_TMlpANN) reader->BookMVA( "TMlpANN method", dir + prefix + "_TMlpANN.weights.txt" );
123     if (Use_BDT) reader->BookMVA( "BDT method", dir + prefix + "_BDT.weights.txt" );
124     if (Use_BDTD) reader->BookMVA( "BDTD method", dir + prefix + "_BDTD.weights.txt" );
125     if (Use_RuleFit) reader->BookMVA( "RuleFit method", dir + prefix + "_RuleFitTMVA.weights.txt" );
126     if (Use_SVM_Gauss) reader->BookMVA( "SVM_Gauss method", dir + prefix + "_SVM_Gauss.weights.txt" );
127     if (Use_SVM_Poly) reader->BookMVA( "SVM_Poly method", dir + prefix + "_SVM_Poly.weights.txt" );
128     if (Use_SVM_Lin) reader->BookMVA( "SVM_Lin method", dir + prefix + "_SVM_Lin.weights.txt" );
129     if (Use_FDA_MT) reader->BookMVA( "FDA_MT method", dir + prefix + "_FDA_MT.weights.txt" );
130     if (Use_FDA_GA) reader->BookMVA( "FDA_GA method", dir + prefix + "_FDA_GA.weights.txt" );
131    
132     // book output histograms
133     UInt_t nbin = 100;
134     TH1F *histLk, *histLkD, *histLkPCA, *histPD, *histPDD, *histPDPCA, *histKNN, *histHm, *histFi;
135     TH1F *histNn, *histNnC, *histNnT, *histBdt, *histBdtD, *histRf;
136     TH1F *histSVMG, *histSVMP, *histSVML;
137     TH1F *histFDAMT, *histFDAGA;
138    
139     if (Use_Likelihood) histLk = new TH1F( "MVA_Likelihood", "MVA_Likelihood", nbin, 0, 1 );
140     if (Use_LikelihoodD) histLkD = new TH1F( "MVA_LikelihoodD", "MVA_LikelihoodD", nbin, 0.000001, 0.9999 );
141     if (Use_LikelihoodPCA) histLkPCA = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, 0, 1 );
142     if (Use_PDERS) histPD = new TH1F( "MVA_PDERS", "MVA_PDERS", nbin, 0, 1 );
143     if (Use_PDERSD) histPDD = new TH1F( "MVA_PDERSD", "MVA_PDERSD", nbin, 0, 1 );
144     if (Use_PDERSPCA) histPDPCA = new TH1F( "MVA_PDERSPCA", "MVA_PDERSPCA", nbin, 0, 1 );
145     if (Use_KNN) histKNN = new TH1F( "MVA_KNN", "MVA_KNN", nbin, 0, 1 );
146     if (Use_HMatrix) histHm = new TH1F( "MVA_HMatrix", "MVA_HMatrix", nbin, -0.95, 1.55 );
147     if (Use_Fisher) histFi = new TH1F( "MVA_Fisher", "MVA_Fisher", nbin, -4, 4 );
148     if (Use_MLP) histNn = new TH1F( "MVA_MLP", "MVA_MLP", nbin, -0.25, 1.5 );
149     if (Use_CFMlpANN) histNnC = new TH1F( "MVA_CFMlpANN", "MVA_CFMlpANN", nbin, 0, 1 );
150     if (Use_TMlpANN) histNnT = new TH1F( "MVA_TMlpANN", "MVA_TMlpANN", nbin, -1.3, 1.3 );
151     if (Use_BDT) histBdt = new TH1F( "MVA_BDT", "MVA_BDT", nbin, -0.8, 0.8 );
152     if (Use_BDTD) histBdtD = new TH1F( "MVA_BDTD", "MVA_BDTD", nbin, -0.4, 0.6 );
153     if (Use_RuleFit) histRf = new TH1F( "MVA_RuleFitTMVA", "MVA_RuleFitTMVA", nbin, -2.0, 2.0 );
154     if (Use_SVM_Gauss) histSVMG = new TH1F( "MVA_SVM_Gauss", "MVA_SVM_Gauss", nbin, 0.0, 1.0 );
155     if (Use_SVM_Poly) histSVMP = new TH1F( "MVA_SVM_Poly", "MVA_SVM_Poly", nbin, 0.0, 1.0 );
156     if (Use_SVM_Lin) histSVML = new TH1F( "MVA_SVM_Lin", "MVA_SVM_Lin", nbin, 0.0, 1.0 );
157     if (Use_FDA_MT) histFDAMT = new TH1F( "MVA_FDA_MT", "MVA_FDA_MT", nbin, -2.0, 3.0 );
158     if (Use_FDA_GA) histFDAGA = new TH1F( "MVA_FDA_GA", "MVA_FDA_GA", nbin, -2.0, 3.0 );
159    
160     // book examsple histogram for probability (the other methods are done similarly)
161     TH1F *probHistFi, *rarityHistFi;
162     if (Use_Fisher) {
163     probHistFi = new TH1F( "PROBA_MVA_Fisher", "PROBA_MVA_Fisher", nbin, 0, 1 );
164     rarityHistFi = new TH1F( "RARITY_MVA_Fisher", "RARITY_MVA_Fisher", nbin, 0, 1 );
165     }
166    
167     // Prepare input tree (this must be replaced by your data source)
168     // in this example, there is a toy tree with signal and one with background events
169     // we'll later on use only the "signal" events for the test in this example.
170     //
171     TFile *input(0);
172     TString fname = "./tmva_example.root";
173     if (!gSystem->AccessPathName( fname )) {
174     // first we try to find tmva_example.root in the local directory
175     cout << "--- accessing data file: " << fname << endl;
176     input = TFile::Open( fname );
177     }
178     else {
179     // second we try accessing the file via the web from
180     // http://root.cern.ch/files/tmva_example.root
181     cout << "--- accessing tmva_example.root file from http://root.cern.ch/files" << endl;
182     cout << "--- for faster startup you may consider downloading it into you local directory" << endl;
183     input = TFile::Open("http://root.cern.ch/files/tmva_example.root");
184     }
185    
186     if (!input) {
187     std::cout << "ERROR: could not open data file: " << fname << std::endl;
188     exit(1);
189     }
190    
191     //
192     // prepare the tree
193     // - here the variable names have to corresponds to your tree
194     // - you can use the same variables as above which is slightly faster,
195     // but of course you can use different ones and copy the values inside the event loop
196     //
197     TTree* theTree = (TTree*)input->Get("TreeS");
198     cout << "--- select signal sample" << endl;
199     Float_t userVar1, userVar2;
200     theTree->SetBranchAddress( "var1", &userVar1 );
201     theTree->SetBranchAddress( "var2", &userVar2 );
202     theTree->SetBranchAddress( "var3", &var3 );
203     theTree->SetBranchAddress( "var4", &var4 );
204    
205     // efficiency calculator for cut method
206     Int_t nSelCuts = 0, nSelCutsD = 0, nSelCutsGA = 0;
207     Double_t effS = 0.7;
208    
209     cout << "--- processing: " << theTree->GetEntries() << " events" << endl;
210     TStopwatch sw;
211     sw.Start();
212     for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
213    
214     if (ievt%1000 == 0)
215     cout << "--- ... processing event: " << ievt << endl;
216    
217     theTree->GetEntry(ievt);
218    
219     var1 = userVar1 + userVar2;
220     var2 = userVar1 - userVar2;
221    
222     //
223     // return the MVAs and fill to histograms
224     //
225     if (Use_Cuts) {
226     // Cuts is a special case: give the desired signal efficienciy
227     Bool_t passed = reader->EvaluateMVA( "Cuts method", effS );
228     if (passed) nSelCuts++;
229     }
230     if (Use_CutsD) {
231     // Cuts is a special case: give the desired signal efficienciy
232     Bool_t passed = reader->EvaluateMVA( "CutsD method", effS );
233     if (passed) nSelCutsD++;
234     }
235     if (Use_CutsGA) {
236     // Cuts is a special case: give the desired signal efficienciy
237     Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
238     if (passed) nSelCutsGA++;
239     }
240    
241     if (Use_Likelihood ) histLk ->Fill( reader->EvaluateMVA( "Likelihood method" ) );
242     if (Use_LikelihoodD ) histLkD ->Fill( reader->EvaluateMVA( "LikelihoodD method" ) );
243     if (Use_LikelihoodPCA) histLkPCA ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
244     if (Use_PDERS ) histPD ->Fill( reader->EvaluateMVA( "PDERS method" ) );
245     if (Use_PDERSD ) histPDD ->Fill( reader->EvaluateMVA( "PDERSD method" ) );
246     if (Use_PDERSPCA ) histPDPCA ->Fill( reader->EvaluateMVA( "PDERSPCA method" ) );
247     if (Use_KNN ) histKNN ->Fill( reader->EvaluateMVA( "KNN method" ) );
248     if (Use_HMatrix ) histHm ->Fill( reader->EvaluateMVA( "HMatrix method" ) );
249     if (Use_Fisher ) histFi ->Fill( reader->EvaluateMVA( "Fisher method" ) );
250     if (Use_MLP ) histNn ->Fill( reader->EvaluateMVA( "MLP method" ) );
251     if (Use_CFMlpANN ) histNnC ->Fill( reader->EvaluateMVA( "CFMlpANN method" ) );
252     if (Use_TMlpANN ) histNnT ->Fill( reader->EvaluateMVA( "TMlpANN method" ) );
253     if (Use_BDT ) histBdt ->Fill( reader->EvaluateMVA( "BDT method" ) );
254     if (Use_BDTD ) histBdtD ->Fill( reader->EvaluateMVA( "BDTD method" ) );
255     if (Use_RuleFit ) histRf ->Fill( reader->EvaluateMVA( "RuleFit method" ) );
256     if (Use_SVM_Gauss ) histSVMG ->Fill( reader->EvaluateMVA( "SVM_Gauss method" ) );
257     if (Use_SVM_Poly ) histSVMP ->Fill( reader->EvaluateMVA( "SVM_Poly method" ) );
258     if (Use_SVM_Lin ) histSVML ->Fill( reader->EvaluateMVA( "SVM_Lin method" ) );
259     if (Use_FDA_MT ) histFDAMT ->Fill( reader->EvaluateMVA( "FDA_MT method" ) );
260     if (Use_FDA_GA ) histFDAGA ->Fill( reader->EvaluateMVA( "FDA_GA method" ) );
261    
262     // retrieve probability instead of MVA output
263     if (Use_Fisher ) {
264     probHistFi ->Fill( reader->GetProba ( "Fisher method" ) );
265     rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
266     }
267     }
268     sw.Stop();
269     cout << "--- end of event loop: "; sw.Print();
270     // get elapsed time
271     if (Use_Cuts) cout << "--- efficiency for Cuts method : " << double(nSelCuts)/theTree->GetEntries()
272     << " (for a required signal efficiency of " << effS << ")" << endl;
273     if (Use_CutsD) cout << "--- efficiency for CutsD method : " << double(nSelCutsD)/theTree->GetEntries()
274     << " (for a required signal efficiency of " << effS << ")" << endl;
275     if (Use_CutsGA) cout << "--- efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
276     << " (for a required signal efficiency of " << effS << ")" << endl;
277    
278     //
279     // write histograms
280     //
281     TFile *target = new TFile( "TMVApp.root","RECREATE" );
282     if (Use_Likelihood ) histLk ->Write();
283     if (Use_LikelihoodD ) histLkD ->Write();
284     if (Use_LikelihoodPCA) histLkPCA ->Write();
285     if (Use_PDERS ) histPD ->Write();
286     if (Use_PDERSD ) histPDD ->Write();
287     if (Use_PDERSPCA ) histPDPCA ->Write();
288     if (Use_KNN ) histKNN ->Write();
289     if (Use_HMatrix ) histHm ->Write();
290     if (Use_Fisher ) histFi ->Write();
291     if (Use_MLP ) histNn ->Write();
292     if (Use_CFMlpANN ) histNnC ->Write();
293     if (Use_TMlpANN ) histNnT ->Write();
294     if (Use_BDT ) histBdt ->Write();
295     if (Use_BDTD ) histBdtD ->Write();
296     if (Use_RuleFit ) histRf ->Write();
297     if (Use_SVM_Gauss ) histSVMG ->Write();
298     if (Use_SVM_Poly ) histSVMP ->Write();
299     if (Use_SVM_Lin ) histSVML ->Write();
300     if (Use_FDA_MT ) histFDAMT ->Write();
301     if (Use_FDA_GA ) histFDAGA ->Write();
302    
303     if (Use_Fisher ) { probHistFi->Write(); rarityHistFi->Write(); }
304     target->Close();
305    
306     cout << "--- created root file: \"TMVApp.root\" containing the MVA output histograms" << endl;
307    
308     delete reader;
309    
310     cout << "==> TMVApplication is done!" << endl << endl;
311     }