ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/ClassApplication.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-03-01, ZMorph_BASE_20100408, gak040610_morphing, V00-02-02, gak011410, gak010310, ejterm2010_25nov2009, V00-02-01, V00-02-00, gak112409, CMSSW_22X_branch_base, segala101609, V00-01-15, V00-01-14, V00-01-13, V00-01-12, V00-01-11, V00-01-10, gak031009, gak030509, gak022309, gak021209, gak040209, gak012809, V00-01-09, V00-01-08, V00-01-07, V00-01-06, V00-01-05, V00-01-04, V00-00-07, V00-00-06, V00-00-05, V00-00-04, V00-01-03, V00-00-02, V00-00-01, HEAD
Branch point for: ZMorph-V00-03-01, CMSSW_22X_branch
Error occurred while calculating annotation data.
Log Message:
created /macros with TMVA scripts in it

File Contents

# Content
1 /**********************************************************************************
2 * Project : TMVA - a Root-integrated toolkit for multivariate data analysis *
3 * Package : TMVA *
4 * Exectuable: ClassApplication *
5 * *
6 * Test suit for comparison of Reader and standalone class outputs *
7 **********************************************************************************/
8
9 #include <vector>
10
11 void ClassApplication( TString myMethodList = "" )
12 {
13 cout << endl;
14 cout << "==> start ClassApplication" << endl;
15
16 const int Nmvas = 15;
17 const char* bulkname[Nmvas] = { "Likelihood", "LikelihoodD", "LikelihoodPCA", "LikelihoodMIX",
18 "HMatrix", "Fisher",
19 "MLP",
20 "BDT", "RuleFit",
21 "SVM_Gauss", "SVM_Poly", "SVM_Lin",
22 "FDA_MT", "FDA_MC", "FDA_GA" };
23
24 bool iuse[Nmvas] = { Nmvas*kFALSE };
25
26 // interpret input list
27 if (myMethodList != "") {
28 TList* mlist = TMVA::Tools::ParseFormatLine( myMethodList, " :," );
29 for (int imva=0; imva<Nmvas; imva++) if (mlist->FindObject( bulkname[imva] )) iuse[imva] = kTRUE;
30 delete mlist;
31 }
32
33 // create a set of variables and declare them to the reader
34 // - the variable names must corresponds in name and type to
35 // those given in the weight file(s) that you use
36 std::vector<std::string> inputVars;
37 inputVars.push_back( "var1+var2" );
38 inputVars.push_back( "var1-var2" );
39 inputVars.push_back( "var3" );
40 inputVars.push_back( "var4" );
41
42 // preload standalone class(es)
43 string dir = "weights/";
44 string prefix = "TMVAnalysis";
45
46 for (int imva=0; imva<Nmvas; imva++) {
47 if (iuse[imva]) {
48 TString cfile = dir + prefix + "_" + bulkname[imva] + ".class.C++";
49
50 cout << "=== Macro : Loading class file: " << cfile << endl;
51
52 // load the classifier's standalone class
53 gROOT->LoadMacro( cfile );
54 }
55 }
56 cout << "=== Macro : Classifier class loading successfully terminated" << endl;
57
58 // define classes
59 IClassifierReader* classReader[Nmvas] = { Nmvas*0 };
60
61 // ... and create them (and some histograms for the output)
62 int nbin = 100;
63 TH1* hist[Nmvas];
64
65 for (int imva=0; imva<Nmvas; imva++) {
66 if (iuse[imva]) {
67 cout << "=== Macro : Testing " << bulkname[imva] << endl;
68 if (bulkname[imva] == "Likelihood" ) {
69 classReader[imva] = new ReadLikelihood ( inputVars );
70 hist[imva] = new TH1F( "MVA_Likelihood", "MVA_Likelihood", nbin, 0, 1 );
71 }
72 if (bulkname[imva] == "LikelihoodD" ) {
73 classReader[imva] = new ReadLikelihoodD ( inputVars );
74 hist[imva] = new TH1F( "MVA_LikelihoodD", "MVA_LikelihoodD", nbin, 0, 1 );
75 }
76 if (bulkname[imva] == "LikelihoodPCA") {
77 classReader[imva] = new ReadLikelihoodPCA( inputVars );
78 hist[imva] = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, 0, 1 );
79 }
80 if (bulkname[imva] == "LikelihoodMIX") {
81 classReader[imva] = new ReadLikelihoodMIX( inputVars );
82 hist[imva] = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin, 0, 1 );
83 }
84 if (bulkname[imva] == "HMatrix" ) {
85 classReader[imva] = new ReadHMatrix ( inputVars );
86 hist[imva] = new TH1F( "MVA_HMatrix", "MVA_HMatrix", nbin, -0.95, 1.55 );
87 }
88 if (bulkname[imva] == "Fisher" ) {
89 classReader[imva] = new ReadFisher ( inputVars );
90 hist[imva] = new TH1F( "MVA_Fisher", "MVA_Fisher", nbin, -4, 4 );
91 }
92 if (bulkname[imva] == "MLP" ) {
93 classReader[imva] = new ReadMLP ( inputVars );
94 hist[imva] = new TH1F( "MVA_MLP", "MVA_MLP", nbin, -0.25, 1.5 );
95 }
96 if (bulkname[imva] == "BDT" ) {
97 classReader[imva] = new ReadBDT ( inputVars );
98 hist[imva] = new TH1F( "MVA_BDT", "MVA_BDT", nbin, -0.8, 0.8 );
99 }
100 if (bulkname[imva] == "RuleFit" ) {
101 classReader[imva] = new ReadRuleFit ( inputVars );
102 hist[imva] = new TH1F( "MVA_RuleFitTMVA", "MVA_RuleFitTMVA", nbin, -2.0, 2.0 );
103 }
104 if (bulkname[imva] == "SVM_Gauss" ) {
105 classReader[imva] = new ReadSVM_Gauss ( inputVars );
106 hist[imva] = new TH1F( "MVA_SVM_Gauss", "MVA_SVM_Gauss", nbin, 0.0, 1.0 );
107 }
108 if (bulkname[imva] == "SVM_Lin" ) {
109 classReader[imva] = new ReadSVM_Lin ( inputVars );
110 hist[imva] = new TH1F( "MVA_SVM_Lin", "MVA_SVM_Lin", nbin, 0.0, 1.0 );
111 }
112 if (bulkname[imva] == "SVM_Poly" ) {
113 classReader[imva] = new ReadSVM_Poly ( inputVars );
114 hist[imva] = new TH1F( "MVA_SVM_Poly", "MVA_SVM_Poly", nbin, 0.0, 1.0 );
115 }
116 if (bulkname[imva] == "FDA_MT" ) {
117 classReader[imva] = new ReadFDA_MT ( inputVars );
118 hist[imva] = new TH1F( "MVA_FDA_MT", "MVA_FDA_MT", nbin, -2.0, 3.0 );
119 }
120 if (bulkname[imva] == "FDA_MC" ) {
121 classReader[imva] = new ReadFDA_MC ( inputVars );
122 hist[imva] = new TH1F( "MVA_FDA_MC", "MVA_FDA_MC", nbin, -2.0, 3.0 );
123 }
124 if (bulkname[imva] == "FDA_GA" ) {
125 classReader[imva] = new ReadFDA_GA ( inputVars );
126 hist[imva] = new TH1F( "MVA_FDA_GA", "MVA_FDA_GA", nbin, -2.0, 3.0 );
127 }
128 }
129 }
130 cout << "=== Macro : Class creation was successful" << endl;
131
132 // Prepare input tree (this must be replaced by your data source)
133 // in this example, there is a toy tree with signal and one with background events
134 // we'll later on use only the "signal" events for the test in this example.
135 //
136 TFile *input(0);
137 if (!gSystem->AccessPathName("./tmva_example.root")) {
138 // first we try to find tmva_example.root in the local directory
139 cout << "=== Macro : Accessing ./tmva_example.root" << endl;
140 input = TFile::Open("tmva_example.root");
141 }
142 else {
143 // second we try accessing the file via the web from
144 // http://root.cern.ch/files/tmva_example.root
145 cout << "=== Macro : Accessing tmva_example.root file from http://root.cern.ch/files" << endl;
146 cout << "=== Macro : For faster startup you may consider downloading it into you local directory" << endl;
147 input = TFile::Open("http://root.cern.ch/files/tmva_example.root");
148 }
149
150 if (!input) {
151 cout << "ERROR: could not open data file" << endl;
152 exit(1);
153 }
154
155 //
156 // prepare the tree
157 // - here the variable names have to corresponds to your tree
158 // - you can use the same variables as above which is slightly faster,
159 // but of course you can use different ones and copy the values inside the event loop
160 //
161 TTree* theTree = (TTree*)input->Get("TreeS");
162 cout << "=== Macro : Loop over signal sample" << endl;
163
164 // the references to the variables
165 float var1, var2, var3, var4;
166 float userVar1, userVar2;
167 theTree->SetBranchAddress( "var1", &userVar1 );
168 theTree->SetBranchAddress( "var2", &userVar2 );
169 theTree->SetBranchAddress( "var3", &var3 );
170 theTree->SetBranchAddress( "var4", &var4 );
171
172 cout << "=== Macro : Processing total of " << theTree->GetEntries() << " events ... " << endl;
173
174 std::vector<double>* inputVec = new std::vector<double>( 4 );
175 for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
176
177 if (ievt%1000 == 0) cout << "=== Macro : ... processing event: " << ievt << endl;
178
179 theTree->GetEntry(ievt);
180
181 var1 = userVar1 + userVar2;
182 var2 = userVar1 - userVar2;
183
184 (*inputVec)[0] = var1;
185 (*inputVec)[1] = var2;
186 (*inputVec)[2] = var3;
187 (*inputVec)[3] = var4;
188
189 // loop over all booked classifiers
190 for (int imva=0; imva<Nmvas; imva++) {
191
192 if (iuse[imva]) {
193
194 // retrive the classifier responses
195 double retval = classReader[imva]->GetMvaValue( *inputVec );
196 hist[imva]->Fill( retval, 1.0 );
197 }
198 }
199 }
200
201 cout << "=== Macro : Event loop done! " << endl;
202
203 TFile *target = new TFile( "ClassApp.root","RECREATE" );
204 for (int imva=0; imva<Nmvas; imva++) {
205 if (iuse[imva]) {
206 hist[imva]->Write();
207 }
208 }
209 cout << "=== Macro : Created target file: " << target->GetName() << endl;
210 target->Close();
211
212 delete target;
213 delete inputVec;
214
215 cout << "==> ClassApplication is done!" << endl << endl;
216 }