ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/benhoob/HWW/ClassApplication.C
Revision: 1.1
Committed: Mon Feb 14 12:39:14 2011 UTC (14 years, 3 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Initial commit

File Contents

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