stressTMVA.cxx

Go to the documentation of this file.
00001 // 
00002 // this stress test for TMVA is a shorter version of the extended TMVA testsuite 
00003 // generated by script tmvaValidation/testsuite/RootTest/buildRootTest.sh
00004 //
00005 // Eckhard von Toerne, Dec 2010
00006 //
00007 /* if working, it creates output like this:
00008 
00009 ******************************************************************                                                                            
00010 * TMVA - S T R E S S and U N I T test suite (FAST)                                                                                            
00011 ******************************************************************                                                                            
00012 Event [107/107]..................................................OK     
00013 VariableInfo [31/31].............................................OK 
00014 DataSetInfo [20/20]..............................................OK   
00015 DataSet [15/15]..................................................OK 
00016 Factory [11/11]..................................................OK
00017 Reader [2/2].....................................................OK
00018 CutsGA [3/3].....................................................OK
00019 LikelihoodD [4/4]................................................OK
00020 PDERS [4/4]......................................................OK
00021 PDEFoam [4/4]....................................................OK
00022 KNN [4/4]........................................................OK
00023 Fisher [4/4].....................................................OK
00024 BoostedFisher [4/4]..............................................OK
00025 LD [4/4].........................................................OK
00026 MLP [4/4]........................................................OK
00027 MLPBFGS [4/4]....................................................OK
00028 SVM [4/4]........................................................OK
00029 BDTG [4/4].......................................................OK
00030 BDT [4/4]........................................................OK
00031 Regression_LD [4/4]..............................................OK
00032 Regression_MLPBFGSN [4/4]........................................OK
00033 Regression_BDTG2 [4/4]...........................................OK
00034 Event [107/107]..................................................OK
00035 VariableInfo [31/31].............................................OK
00036 DataSetInfo [20/20]..............................................OK
00037 DataSet [15/15]..................................................OK
00038 Factory [11/11]..................................................OK
00039 Reader [2/2].....................................................OK
00040 CutsGA [3/3].....................................................OK
00041 LikelihoodD [4/4]................................................OK
00042 PDERS [4/4]......................................................OK
00043 PDEFoam [4/4]....................................................OK
00044 KNN [4/4]........................................................OK
00045 Fisher [4/4].....................................................OK
00046 BoostedFisher [4/4]..............................................OK
00047 LD [4/4].........................................................OK
00048 MLP [4/4]........................................................OK
00049 MLPBFGS [4/4]....................................................OK
00050 SVM [4/4]........................................................OK
00051 BDTG [4/4].......................................................OK
00052 BDT [4/4]........................................................OK
00053 Regression_LD [4/4]..............................................OK
00054 Regression_MLPBFGSN [4/4]........................................OK
00055 Regression_BDTG2 [4/4]...........................................OK
00056 ******************************************************************
00057 *  SYS: Linux linux-7p93 2.6.27.7-9-default #1 SMP 2008-12-04 18:10:
00058 *  SYS: "openSUSE 11.1 (x86_64)"
00059 ******************************************************************
00060 *  CPUTIME   =  90.2   *  Root5.27/07   20100929/1318
00061 ******************************************************************
00062 */
00063 // including file tmvaut/UnitTest.h
00064 #ifndef UNITTEST_H
00065 #define UNITTEST_H
00066 
00067 // Author: Christoph Rosemann   Dec. 2009
00068 // TMVA unit tests
00069 
00070 #include <string>
00071 #include <iostream>
00072 #include <cassert>
00073 
00074 // The following have underscores because
00075 // they are macros. For consistency,
00076 // succeed_() also has an underscore.
00077 
00078 #define test_(cond) \
00079   do_test(cond, #cond, __FILE__, __LINE__)
00080 #define fail_(str) \
00081   do_fail(str, __FILE__, __LINE__)
00082 
00083 namespace UnitTesting
00084 {
00085 
00086    class UnitTest
00087    {
00088    public:
00089       UnitTest(const std::string & xname="", const std::string & filename="", std::ostream* osptr = &std::cout);
00090       virtual ~UnitTest(){}
00091       virtual void run() = 0;
00092       long getNumPassed() const;
00093       long getNumFailed() const;
00094       const std::ostream* getStream() const;
00095       void setStream(std::ostream* osptr);
00096       void succeed_();
00097       long report() const;
00098       virtual void reset();
00099       void intro() const;
00100       const std::string & name() const;
00101    protected:
00102       void do_test(bool cond, const std::string& lbl, const char* fname, long lineno);
00103       void do_fail(const std::string& lbl, const char* fname, long lineno);
00104       bool floatCompare(float x1, float x2);
00105    private:
00106       std::ostream* osptr;
00107       long nPass;
00108       long nFail;
00109       mutable std::string fName;
00110       std::string fFileName;
00111       // Disallowed:
00112       UnitTest(const UnitTest&);
00113       UnitTest& operator=(const UnitTest&);
00114    };
00115 
00116    inline UnitTest::UnitTest(const std::string & xname,
00117                              const std::string & filename,
00118                              std::ostream* sptr) :
00119       fName(xname),
00120       fFileName(filename)
00121    {
00122       this->osptr = sptr;
00123       nPass = nFail = 0;
00124    }
00125 
00126    inline long UnitTest::getNumPassed() const
00127    {
00128       return nPass;
00129    }
00130 
00131    inline long UnitTest::getNumFailed() const
00132    {
00133       return nFail;
00134    }
00135 
00136    inline const std::ostream* UnitTest::getStream() const
00137    {
00138       return osptr;
00139    }
00140 
00141    inline void UnitTest::setStream(std::ostream* sptr)
00142    {
00143       this->osptr = sptr;
00144    }
00145 
00146    inline void UnitTest::succeed_()
00147    {
00148       ++nPass;
00149    }
00150 
00151    inline void UnitTest::reset()
00152    {
00153       nPass = nFail = 0;
00154    }
00155 
00156 } // namespace UnitTesting
00157 #endif // UNITTEST_H
00158 // including file tmvaut/UnitTest.cxx
00159 
00160 
00161 #include <iostream>
00162 #include <iomanip>
00163 #include "TString.h"
00164 #include "TMath.h"
00165 #include <typeinfo>
00166 
00167 using namespace std;
00168 using namespace UnitTesting;
00169 
00170 void UnitTest::do_test(bool cond, const std::string& lbl, const char* fname, long lineno)
00171 {
00172    if (!cond)
00173       do_fail(lbl, fname, lineno);
00174    else
00175       succeed_();
00176 }
00177 
00178 void UnitTest::do_fail(const std::string& lbl, const char* /* fname */ , long lineno)
00179 {
00180    ++nFail;
00181    if (osptr)
00182       {
00183          *osptr << "failure: " << setw(76) << std::left << lbl << std::right << " [line " << setw(3) << lineno << "]\n";
00184       }
00185 }
00186 
00187 bool UnitTest::floatCompare(float x1, float x2)
00188 {
00189    bool ret = (TMath::Abs(x1-x2)<1.e-9); // fix me, empirical number
00190    if (!ret) cout << "warning floatCompare: x1="<<x1<<" x2="<<x2<<", diff="<<x1-x2<<endl;
00191    return ret;
00192 }
00193 
00194 const std::string & UnitTest::name() const
00195 {
00196    if(fName=="")
00197       fName=std::string(typeid(*this).name());
00198    return fName;
00199 }
00200 
00201 
00202 long UnitTest::report() const
00203 {
00204    if (osptr)
00205       {
00206          std::string counts(Form(" [%li/%li]", nPass, nPass+nFail));
00207 
00208          *osptr << name() << counts;
00209 
00210          UInt_t ndots = 82-19-name().size() - counts.size();
00211 
00212          for (UInt_t i=0; i<ndots; ++i) *osptr << '.';
00213 
00214          *osptr << (nFail==0?"..OK":"FAIL") << endl;
00215       }
00216    return nFail;
00217 }
00218 
00219 void UnitTest::intro() const
00220 {
00221 #ifdef COUTDEBUG
00222    if (osptr) {
00223       *osptr << "************************************************************************************************" << endl;
00224       *osptr << "* Starting U N I T test : " << name() << " (file " << fFileName << ")" << endl;
00225       *osptr << "************************************************************************************************" << endl;
00226    }
00227 #endif
00228 }
00229 // including file tmvaut/UnitTestSuite.h
00230 #ifndef UNITTESTSUITE_H
00231 #define UNITTESTSUITE_H
00232 
00233 // Author: Christoph Rosemann   Dec. 2009
00234 // TMVA unit tests
00235 
00236 
00237 #include <vector>
00238 #include <stdexcept>
00239 
00240 namespace UnitTesting
00241 {
00242 
00243    class UnitTestSuiteError : public std::logic_error
00244    {
00245    public:
00246       UnitTestSuiteError(const std::string& s = "") : logic_error(s) {}
00247    };
00248 
00249    class UnitTestSuite
00250    {
00251    public:
00252       UnitTestSuite(const std::string& title, std::ostream* sptr = &std::cout);
00253       std::string getName() const;
00254       long getNumPassed() const;
00255       long getNumFailed() const;
00256       const std::ostream* getStream() const;
00257       void setStream(std::ostream* osptr);
00258       void addTest(UnitTest* t) throw (UnitTestSuiteError);
00259       void addSuite(const UnitTestSuite&);
00260       void run();  // Calls Test::run() repeatedly
00261       void intro() const;
00262       long report() const;
00263       void free();  // Deletes tests
00264    private:
00265       std::string name;
00266       std::ostream* osptr;
00267       std::vector<UnitTest*> tests;
00268       void reset();
00269       // Disallowed ops:
00270       UnitTestSuite(const UnitTestSuite&);
00271       UnitTestSuite& operator=(const UnitTestSuite&);
00272    };
00273 
00274    inline UnitTestSuite::UnitTestSuite(const std::string& title, std::ostream* sptr) : name(title)
00275    {
00276       this->osptr = sptr;
00277    }
00278 
00279    inline std::string UnitTestSuite::getName() const
00280    {
00281       return name;
00282    }
00283 
00284    inline const std::ostream* UnitTestSuite::getStream() const
00285    {
00286       return osptr;
00287    }
00288 
00289    inline void UnitTestSuite::setStream(std::ostream* sptr)
00290    {
00291       this->osptr = sptr;
00292    }
00293 
00294 } // namespace UnitTesting
00295 #endif // UNITTESTSUITE_H
00296 // including file tmvaut/UnitTestSuite.cxx
00297 
00298 #include <iostream>
00299 #include <iomanip>
00300 #include <cassert>
00301 using namespace std;
00302 using namespace UnitTesting;
00303 
00304 void UnitTestSuite::addTest(UnitTest* t) throw(UnitTestSuiteError)
00305 {
00306    // Verify test is valid and has a stream:
00307    if (t == 0)
00308       throw UnitTestSuiteError("Null test in UnitTestSuite::addTest");
00309    else if (osptr && !t->getStream())
00310       t->setStream(osptr);
00311    tests.push_back(t);
00312    t->reset();
00313 }
00314 
00315 void UnitTestSuite::addSuite(const UnitTestSuite& s)
00316 {
00317    for (size_t i = 0; i < s.tests.size(); ++i)
00318       {
00319          assert(tests[i]);
00320          addTest(s.tests[i]);
00321       }
00322 }
00323 
00324 void UnitTestSuite::free()
00325 {
00326    for (size_t i = 0; i < tests.size(); ++i)
00327       {
00328          delete tests[i];
00329          tests[i] = 0;
00330       }
00331 }
00332 
00333 void UnitTestSuite::run()
00334 {
00335    reset();
00336    for (size_t i = 0; i < tests.size(); ++i)
00337       {
00338          assert(tests[i]);
00339          tests[i]->intro();
00340          tests[i]->run();
00341 #ifndef FULL
00342          tests[i]->report();
00343 #endif
00344       }
00345 }
00346 
00347 void UnitTestSuite::intro() const
00348 {
00349    if (osptr) {
00350       *osptr << "******************************************************************" << endl;
00351       *osptr << "* TMVA - S T R E S S and U N I T test suite ";
00352 #ifdef FULL
00353       *osptr << "(FULL)"<< endl;
00354 #else
00355       *osptr << "(FAST)"<< endl;
00356 #endif
00357       //*osptr << "                                             *" << endl;
00358       *osptr << "******************************************************************" << endl;
00359 
00360    }
00361 }
00362 
00363 long UnitTestSuite::report() const
00364 {
00365    if (osptr) {
00366       long totFail = 0;
00367 #ifdef FULL
00368       *osptr << "************************************************************************************************" << endl;
00369       *osptr << "* TMVA - U N I T test : Summary                                                                *" << endl;
00370       *osptr << "************************************************************************************************" << endl;
00371 #endif
00372       size_t i;
00373       for (i = 0; i < tests.size(); ++i)
00374          {
00375             assert(tests[i]);
00376 #ifdef FULL
00377             *osptr << "Test " << setw(2) << i << " : ";
00378 #endif
00379             totFail += tests[i]->report();
00380          }
00381       return totFail;
00382    }
00383    else
00384       return getNumFailed();
00385 }
00386 
00387 long UnitTestSuite::getNumPassed() const
00388 {
00389    long totPass = 0;
00390    for (size_t i = 0; i < tests.size(); ++i)
00391       {
00392          assert(tests[i]);
00393          totPass += tests[i]->getNumPassed();
00394       }
00395    return totPass;
00396 }
00397 
00398 long UnitTestSuite::getNumFailed() const
00399 {
00400    long totFail = 0;
00401    for (size_t i = 0; i < tests.size(); ++i)
00402       {
00403          assert(tests[i]);
00404          totFail += tests[i]->getNumFailed();
00405       }
00406    return totFail;
00407 }
00408 
00409 void UnitTestSuite::reset()
00410 {
00411    for (size_t i = 0; i < tests.size(); ++i)
00412       {
00413          assert(tests[i]);
00414          tests[i]->reset();
00415       }
00416 }
00417 // including file tmvaut/utDataSetInfo.h
00418 #ifndef UTDATASETINFO_H
00419 #define UTDATASETINFO_H
00420 
00421 // Author: Christoph Rosemann   Dec. 2009
00422 // TMVA unit tests
00423 
00424 
00425 
00426 #include <vector>
00427 
00428 #include "TString.h"
00429 #include "TCut.h"
00430 #include "TMatrixDfwd.h"
00431 
00432 #include "TMVA/VariableInfo.h"
00433 
00434 namespace TMVA {
00435    class DataSetInfo;
00436    class ClassInfo;
00437    class Event;
00438 }
00439 
00440 class utDataSetInfo : public UnitTesting::UnitTest
00441 {
00442 public:
00443    utDataSetInfo();
00444    void run();
00445 
00446 private:
00447    void testConstructor();
00448    void testMethods();
00449 
00450    TMVA::DataSetInfo* datasetinfo;
00451 
00452    // the members needed for the creation and comparison of instances
00453    TString     name;
00454    TString     expression;
00455    TString     title;
00456    TString     unit;
00457    Double_t    min;
00458    Double_t    max;
00459    Int_t       varcounter;
00460    char        vartype;
00461    Bool_t      normalized;
00462    void*       external;
00463    TString     norm;
00464    TString     classname;
00465    TCut        cut1;
00466    TCut        cut2;
00467    TString     splitoption;
00468    TMatrixD*   matrix;
00469    TString     histname;
00470    TString     histtitle;
00471    TString     weightexpr;
00472 
00473    TMVA::Event*                    event;
00474    TMVA::ClassInfo*                classinfo;
00475    TMVA::VariableInfo              varinfo;
00476    std::vector<TMVA::VariableInfo> vecvarinfo;
00477 };
00478 #endif // UTDATASETINFO_H
00479 // including file tmvaut/utDataSetInfo.cxx
00480 
00481 
00482 
00483 #include "TMatrixD.h"
00484 
00485 #include "TMVA/DataSetInfo.h"
00486 #include "TMVA/ClassInfo.h"
00487 #include "TMVA/Event.h"
00488 
00489 using namespace std;
00490 using namespace UnitTesting;
00491 using namespace TMVA;
00492 
00493 utDataSetInfo::utDataSetInfo() :
00494    UnitTest("DataSetInfo", __FILE__)
00495 {
00496    name = "name";
00497    expression = "expression";
00498    title  = "title";
00499    unit = "unit";
00500    min = 2.781828;
00501    max = 3.1416;
00502    vartype = 'D';
00503    varcounter = 123;
00504    normalized = kFALSE;
00505    external = &max;
00506    norm = "norm";
00507    classname = "classname";
00508    cut1 = "x<1.";
00509    cut2 = "y>2.";
00510    splitoption = "splitoption";
00511    matrix = new TMatrixD();
00512    histname = "histname";
00513    histtitle = "histtitle";
00514    weightexpr = "weightexpr";
00515    event = new Event();
00516 
00517    varinfo = VariableInfo( expression, title,  unit, varcounter, vartype, external, min, max, normalized);
00518 
00519    vecvarinfo.push_back(varinfo);
00520    vecvarinfo.push_back(varinfo);
00521 }
00522 
00523 
00524 
00525 void utDataSetInfo::run()
00526 {
00527    testConstructor();
00528    testMethods();
00529 }
00530 
00531 
00532 void utDataSetInfo::testConstructor()
00533 {
00534    datasetinfo  = new DataSetInfo(name);
00535    datasetinfo->SetMsgType(TMVA::kWARNING);
00536    test_(datasetinfo->GetName() == name);
00537 }
00538 
00539 
00540 void utDataSetInfo::testMethods()
00541 {
00542    // FIXME:this doesn't work straightforward
00543    // test_(datasetinfo->GetDataSet() == 0);
00544 
00545    // note: the "constructor"-like call is different from the /standard/ VariableInfo constructor
00546    datasetinfo->AddVariable(expression, title,  unit, min, max, vartype, normalized, external);
00547    datasetinfo->AddVariable(varinfo);
00548    // note: the method has a different argument list than the other two
00549    datasetinfo->AddTarget(expression, title,  unit, min, max, normalized, external);
00550    datasetinfo->AddTarget(varinfo);
00551    datasetinfo->AddSpectator(expression, title,  unit, min, max, vartype, normalized, external);
00552    datasetinfo->AddSpectator(varinfo);
00553    datasetinfo->AddClass(classname);
00554 
00555    test_((datasetinfo->GetVariableInfos()).size()  == vecvarinfo.size());
00556    test_((datasetinfo->GetTargetInfos()).size()    == vecvarinfo.size());
00557    test_((datasetinfo->GetSpectatorInfos()).size() == vecvarinfo.size());
00558 
00559    test_(datasetinfo->GetNVariables()  == vecvarinfo.size());
00560    test_(datasetinfo->GetNTargets()    == vecvarinfo.size());
00561    test_(datasetinfo->GetNSpectators() == vecvarinfo.size());
00562 
00563    datasetinfo->SetNormalization(norm);
00564    test_(datasetinfo->GetNormalization() == norm);
00565 
00566    // there is no comparison operator for VariableInfo, this will be broken if the implementation changes
00567    /*
00568      for(vector<VariableInfo>::const_iterator varinf = vecvarinfo.begin(); varinf < vecvarinfo.end(); ++varinf)
00569      {
00570      unsigned int index = vecvarinfo.begin() - varinf;
00571      test_(datasetinfo->GetVariableInfo(index) == varinfo); // doesn't work!
00572      }
00573    */
00574 
00575    test_(datasetinfo->GetClassNameMaxLength() == classname.Length() );
00576    test_(datasetinfo->GetNClasses()           == 1);
00577    test_(datasetinfo->IsSignal(event)         == kTRUE);
00578 
00579    /*
00580    // classification information
00581    ClassInfo*         GetClassInfo( Int_t clNum ) const;
00582    ClassInfo*         GetClassInfo( const TString& name ) const;
00583    */
00584 
00585 
00586    test_(datasetinfo->HasCuts() == kFALSE); // why does this fail?
00587 
00588    datasetinfo->SetCut(cut1, classname);
00589    test_(datasetinfo->HasCuts() == kTRUE);
00590 
00591    test_(datasetinfo->GetCut(0) == cut1);
00592    //  test_(datasetinfo->GetCut(1) == cut2); // there is no guard against using the wrong index
00593    test_(datasetinfo->GetCut(classname) == cut1);
00594    datasetinfo->AddCut(cut2, classname);
00595    cut1 += cut2;
00596    test_(TString(datasetinfo->GetCut(classname)) == TString(cut1));
00597 
00598    datasetinfo->SetSplitOptions(splitoption);
00599    test_(datasetinfo->GetSplitOptions() == splitoption);
00600 
00601    test_(datasetinfo->GetWeightExpression(0) == "");
00602    datasetinfo->SetWeightExpression(weightexpr);
00603    test_(datasetinfo->GetWeightExpression(0) == weightexpr);
00604 
00605    datasetinfo->SetCorrelationMatrix(classname,matrix);
00606    //  datasetinfo->PrintCorrelationMatrix(classname);// this is insecure!
00607 
00608    test_(datasetinfo->FindVarIndex(expression) == 0);
00609 
00610    vector<TString> vars = datasetinfo->GetListOfVariables();
00611 
00612 
00613    /*
00614      void               ClearDataSet() const;
00615      Int_t              FindVarIndex( const TString& )      const;
00616      std::vector<TString> GetListOfVariables() const;
00617      const TMatrixD*    CorrelationMatrix     ( const TString& className ) const;
00618      void               SetCorrelationMatrix  ( const TString& className, TMatrixD* matrix );
00619      TH2*               CreateCorrelationMatrixHist( const TMatrixD* m, const TString& hName, const TString& hTitle ) const;
00620    */
00621 
00622 
00623    /*
00624      unclear to me, what to test:
00625      void               SetRootDir(TDirectory* d) { fOwnRootDir = d; }
00626      TDirectory*        GetRootDir() const { return fOwnRootDir; }
00627    */
00628 
00629 }
00630 
00631 // including file tmvaut/utDataSet.h
00632 #ifndef UTDATASET_H
00633 #define UTDATASET_H
00634 
00635 // Author: Christoph Rosemann   Dec. 2009
00636 // TMVA unit tests
00637 
00638 
00639 
00640 #include <vector>
00641 
00642 namespace TMVA {
00643    class DataSet;
00644    class DataSetInfo;
00645    class Event;
00646 }
00647 
00648 class utDataSet : public UnitTesting::UnitTest
00649 {
00650 public:
00651    utDataSet();
00652    void run();
00653 
00654 private:
00655    void testMethods();
00656 
00657    TMVA::DataSet*            dataset;
00658    TMVA::DataSetInfo*        datasetinfo;
00659    TMVA::Event*              event0;
00660    TMVA::Event*              event1;
00661    TMVA::Event*              event2;
00662    TMVA::Event*              event3;
00663    TMVA::Event*              event4;
00664    std::vector<TMVA::Event>* vecevent;
00665    //  TMVA::Results*            result;
00666 };
00667 #endif // UTDATASET_H
00668 // including file tmvaut/utDataSet.cxx
00669 
00670 
00671 
00672 #include "TMVA/VariableInfo.h"
00673 #include "TMVA/Types.h"
00674 #include "TMVA/Event.h"
00675 #include "TMVA/DataSet.h"
00676 #include "TMVA/DataSetInfo.h"
00677 
00678 #include "TString.h"
00679 
00680 using namespace std;
00681 using namespace TMVA;
00682 
00683 utDataSet::utDataSet() :
00684    UnitTest("DataSet", __FILE__)
00685 {
00686    //   event;
00687    //   vecevent;
00688    //  results;
00689 
00690    TString xname       = "name";
00691    TString expression1 = "expression1";
00692    TString expression2 = "expression2";
00693    TString expression3 = "expression3";
00694    TString title      = "title";
00695    TString unit       = "unit";
00696    char    vartype    = 'D';
00697    Float_t min        = 2.781828;
00698    Float_t max        = 3.1416;
00699    Bool_t  normalized = kFALSE;
00700    TString classname  = "classname";
00701    void* external = &max;
00702    UInt_t   _testClassVal    = 2;
00703 
00704    // the test values for initialisation
00705    vector<Float_t> _testValueVec, _testTargetVec, _testSpectatorVec;
00706    _testValueVec.push_back(1.);
00707    _testValueVec.push_back(2.);
00708    _testValueVec.push_back(3.);
00709    _testTargetVec.push_back(11.);
00710    _testTargetVec.push_back(12.);
00711    _testTargetVec.push_back(13.);
00712    _testSpectatorVec.push_back(25.);
00713    Float_t  _testWeight      = 3.1415;
00714    Float_t _testBoostWeight = 0.1234;
00715    event0 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00716    event1 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00717    event2 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00718    event3 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00719    event4 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00720 
00721    UInt_t  varcounter = 0;
00722 
00723    VariableInfo* varinfo1     = new VariableInfo( expression1, title,  unit, varcounter++, vartype, external, min, max, normalized);
00724    VariableInfo* varinfo2     = new VariableInfo( expression2, title,  unit, varcounter++, vartype, external, min, max, normalized);
00725    VariableInfo* varinfo3     = new VariableInfo( expression3, title,  unit, varcounter++, vartype, external, min, max, normalized);
00726    datasetinfo               = new DataSetInfo(xname);
00727    datasetinfo->AddVariable(*varinfo1);
00728    datasetinfo->AddVariable(*varinfo2);
00729    datasetinfo->AddVariable(*varinfo3);
00730    datasetinfo->AddTarget(*varinfo1);
00731    datasetinfo->AddTarget(*varinfo2);
00732    datasetinfo->AddTarget(*varinfo3);
00733    datasetinfo->AddSpectator(*varinfo1);
00734 
00735    dataset = new DataSet(*datasetinfo);
00736 }
00737 
00738 
00739 
00740 void utDataSet::run()
00741 {
00742   testMethods();
00743 }
00744 
00745 
00746 
00747 void utDataSet::testMethods()
00748 {
00749    test_(dataset->GetNEvents() == 0);
00750    dataset->AddEvent(event0, Types::kTraining);
00751    dataset->AddEvent(event1, Types::kTraining);
00752    dataset->AddEvent(event2, Types::kTesting);
00753    dataset->AddEvent(event3, Types::kMaxTreeType);
00754    dataset->AddEvent(event4, Types::kValidation);
00755 
00756    test_(dataset->GetNEvents(Types::kTraining) == 2);
00757    test_(dataset->GetNEvents(Types::kTesting) == 1);
00758    //test_(dataset->GetNEvents(Types::kMaxTreeType) == 1); //ToDo check this isuue
00759    test_(dataset->GetNEvents(Types::kValidation) == 1);
00760    //test_(dataset->GetNEvents(Types::kTrainingOriginal) == 0);//ToDo check this issue
00761 
00762    test_(dataset->GetNTrainingEvents() == 2);
00763    test_(dataset->GetNTestEvents() == 1);
00764 
00765    test_(dataset->GetNVariables()  == 3);
00766    test_(dataset->GetNTargets()    == 3);
00767    test_(dataset->GetNSpectators() == 1);
00768 
00769    // this is only a temporary solution for the testing
00770    // FIXME:: Extend to a real test!
00771 
00772    test_(dataset->GetEvent());
00773    test_(dataset->GetEvent(0));
00774    test_(dataset->GetTrainingEvent(0));
00775    test_(dataset->GetTestEvent(0));
00776    test_(dataset->GetEvent(1, Types::kTraining));
00777 
00778    // calls to these function will cause the program to crash
00779    //  test_(dataset->GetNEvtSigTest() == 1);
00780    //  test_(dataset->GetNEvtBkgdTest() == 1);
00781    //  test_(dataset->GetNEvtSigTrain() == 1);
00782    //   test_(dataset->GetNEvtBkgdTrain() == 1);
00783 
00784    test_(dataset->HasNegativeEventWeights() == kFALSE);
00785 
00786    /* function still to develop tests for:
00787       void      SetCurrentEvent( Long64_t ievt         ) const { fCurrentEventIdx = ievt; }
00788       void      SetCurrentType ( Types::ETreeType type ) const { fCurrentTreeIdx = TreeIndex(type); }
00789 
00790       void                       SetEventCollection( std::vector<Event*>*, Types::ETreeType );
00791       const std::vector<Event*>& GetEventCollection( Types::ETreeType type = Types::kMaxTreeType ) const;
00792       const TTree*               GetEventCollectionAsTree();
00793 
00794       Results*  GetResults   ( const TString &,Types::ETreeType type, Types::EAnalysisType analysistype );
00795       void      DivideTrainingSet( UInt_t blockNum );
00796       void      MoveTrainingBlock( Int_t blockInd,Types::ETreeType dest, Bool_t applyChanges = kTRUE );
00797       void      IncrementNClassEvents( Int_t type, UInt_t classNumber );
00798       Long64_t  GetNClassEvents      ( Int_t type, UInt_t classNumber );
00799       void      ClearNClassEvents    ( Int_t type );
00800       TTree*    GetTree( Types::ETreeType type );
00801       void      InitSampling( Float_t fraction, Float_t weight, UInt_t seed = 0 );
00802       void      EventResult( Bool_t successful, Long64_t evtNumber = -1 );
00803       void      CreateSampling() const;
00804       UInt_t    TreeIndex(Types::ETreeType type) const;
00805    */
00806 }
00807 
00808 
00809 // including file tmvaut/utEvent.h
00810 #ifndef UTEVENT_H
00811 #define UTEVENT_H
00812 
00813 // Author: Christoph Rosemann   Dec. 2009
00814 // TMVA unit tests
00815 
00816 #include <vector>
00817 
00818 #ifndef ROOT_Rtypes
00819 #include "Rtypes.h"
00820 #endif
00821 
00822 
00823 
00824 namespace TMVA {
00825    class Event;
00826 }
00827 
00828 class utEvent : public UnitTesting::UnitTest
00829 {
00830 public:
00831    utEvent();
00832    void run();
00833 
00834 private:
00835    // the test calls in different blocks
00836    // the distinctions are arbitrary:
00837    void _testConstructor1();
00838    void _testConstructor2();
00839    void _testConstructor3();
00840    void _testConstructor4();
00841    void _testConstructor5();
00842    void _testConstructor6();
00843    void _testMutators();
00844 
00845    // there are six different constructors:
00846    TMVA::Event* _eventC1;
00847    TMVA::Event* _eventC2;
00848    TMVA::Event* _eventC3;
00849    TMVA::Event* _eventC4;
00850    TMVA::Event* _eventC5;
00851    TMVA::Event* _eventC6;
00852 
00853    // the values needed to create all "Event" objects in all ways
00854    std::vector<Float_t>  _testValueVec;
00855    std::vector<Float_t*> _testPointerVec;
00856    std::vector<Float_t>  _testTargetVec;
00857    std::vector<Float_t>  _testSpectatorVec;
00858 
00859    std::vector<Float_t>  _compareValueVec;
00860    std::vector<Float_t>  _compareTargetVec;
00861    std::vector<Float_t>  _compareSpectatorVec;
00862 
00863    Float_t               _t, _u, _v;
00864    Float_t               _testScale;
00865    UInt_t                _testClassVal;
00866    Float_t               _testWeight;
00867    Float_t               _testBoostWeight;
00868    UInt_t                _testNVar;
00869 };
00870 #endif // UTEVENT_H
00871 // including file tmvaut/utEvent.cxx
00872 
00873 
00874 
00875 #include "TMath.h"
00876 
00877 #include "TMVA/Event.h"
00878 
00879 using namespace std;
00880 using namespace TMVA;
00881 
00882 utEvent::utEvent() :
00883    UnitTest("Event", __FILE__)
00884 {
00885    // the test values for initialisation
00886    _testValueVec.push_back(1.);
00887    _testValueVec.push_back(2.);
00888    _testValueVec.push_back(3.);
00889    _t = 1.;
00890    _u = 2.;
00891    _v = 3.;
00892    _testPointerVec.push_back(&_t);
00893    _testPointerVec.push_back(&_u);
00894    _testPointerVec.push_back(&_v);
00895    _testTargetVec.push_back(11.);
00896    _testTargetVec.push_back(12.);
00897    _testTargetVec.push_back(13.);
00898    _testSpectatorVec.push_back(25.);
00899    _testScale       = 2.7818;
00900    _testClassVal    = 2;
00901    _testWeight      = 3.1415;
00902    _testBoostWeight = 0.1234;
00903    _testNVar        = 3;
00904 }
00905 
00906 
00907 
00908 void utEvent::run()
00909 {
00910    _testConstructor1();
00911    _testConstructor3();
00912    _testConstructor2(); // constructor 3 must be called before...
00913    _testConstructor4();
00914    _testConstructor5();
00915    _testConstructor6();
00916 }
00917 
00918 
00919 
00920 void utEvent::_testConstructor1()
00921 {
00922    _eventC1 = new Event();
00923 
00924    test_(_eventC1->IsDynamic()         == false);
00925 
00926    test_(floatCompare(_eventC1->GetWeight(), 1.));
00927    test_(floatCompare(_eventC1->GetOriginalWeight(), 1.));
00928    test_(floatCompare(_eventC1->GetBoostWeight(),1.));
00929 
00930    test_(_eventC1->GetWeight()         == 1.);
00931    test_(_eventC1->GetOriginalWeight() == 1.);
00932    test_(_eventC1->GetBoostWeight()    == 1.);
00933    test_(_eventC1->GetClass()          == 0);
00934    test_(_eventC1->GetNVariables()     == 0);
00935    test_(_eventC1->GetNTargets()       == 0);
00936    test_(_eventC1->GetNSpectators()    == 0);
00937 
00938    _testMutators();
00939 }
00940 
00941 
00942 
00943 void utEvent::_testConstructor2()
00944 {
00945    _eventC2 = new Event(*_eventC3);
00946 
00947    test_(_eventC2->IsDynamic()         == false);
00948    
00949    test_(floatCompare(_eventC2->GetWeight(), _testWeight*_testBoostWeight));
00950    test_(floatCompare(_eventC2->GetOriginalWeight(), _testWeight));
00951    test_(floatCompare(_eventC2->GetBoostWeight(), _testBoostWeight));
00952 
00953    test_(_eventC2->GetClass()          == _testClassVal);
00954    test_(_eventC2->GetNVariables()     == (UInt_t)_testValueVec.size());
00955    test_(_eventC2->GetNTargets()       == (UInt_t)_testTargetVec.size());
00956    test_(_eventC2->GetNSpectators()    == (UInt_t)_testSpectatorVec.size());
00957    // ? const std::vector<UInt_t>* GetVariableArrangement() const { return fVariableArrangement; }
00958 
00959    _compareValueVec = _eventC2->GetValues();
00960    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
00961       {
00962          unsigned int index = it - _testValueVec.begin();
00963          test_(_eventC2->GetValue(index)  == *it);
00964          test_(_compareValueVec.at(index) == *it);
00965       }
00966    _compareTargetVec = _eventC2->GetTargets();
00967    for(vector<Float_t>::const_iterator it = _testTargetVec.begin(); it < _testTargetVec.end(); ++it)
00968       {
00969          unsigned int index = it - _testTargetVec.begin();
00970          test_(_eventC2->GetTarget(index)  == *it);
00971          test_(_compareTargetVec.at(index) == *it);
00972       }
00973    _compareSpectatorVec = _eventC2->GetSpectators();
00974    for(vector<Float_t>::const_iterator it = _testSpectatorVec.begin(); it < _testSpectatorVec.end(); ++it)
00975       {
00976          unsigned int index = it - _testSpectatorVec.begin();
00977          test_(_eventC2->GetSpectator(index)  == *it);
00978          test_(_compareSpectatorVec.at(index) == *it);
00979       }
00980 
00981 }
00982 
00983 
00984 
00985 void utEvent::_testConstructor3()
00986 {
00987    _eventC3 = new Event( _testValueVec, _testTargetVec, _testSpectatorVec, _testClassVal, _testWeight, _testBoostWeight);
00988 
00989    test_(_eventC3->IsDynamic()         == false);
00990 
00991    test_(floatCompare(_eventC3->GetWeight(), _testWeight*_testBoostWeight));
00992    test_(floatCompare(_eventC3->GetOriginalWeight(), _testWeight));
00993    test_(floatCompare(_eventC3->GetBoostWeight(), _testBoostWeight));
00994 
00995    test_(_eventC3->GetClass()          == _testClassVal);
00996    test_(_eventC3->GetNVariables()     == (UInt_t)_testValueVec.size());
00997    test_(_eventC3->GetNTargets()       == (UInt_t)_testTargetVec.size());
00998    test_(_eventC3->GetNSpectators()    == (UInt_t)_testSpectatorVec.size());
00999    // ? const std::vector<UInt_t>* GetVariableArrangement() const { return fVariableArrangement; }
01000 
01001    _compareValueVec = _eventC3->GetValues();
01002    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01003       {
01004          unsigned int index = it - _testValueVec.begin();
01005          test_(_eventC3->GetValue(index)  == *it);
01006          test_(_compareValueVec.at(index) == *it);
01007       }
01008    _compareTargetVec = _eventC3->GetTargets();
01009    for(vector<Float_t>::const_iterator it = _testTargetVec.begin(); it < _testTargetVec.end(); ++it)
01010       {
01011          unsigned int index = it - _testTargetVec.begin();
01012          test_(_eventC3->GetTarget(index)  == *it);
01013          test_(_compareTargetVec.at(index) == *it);
01014       }
01015    _compareSpectatorVec = _eventC3->GetSpectators();
01016    for(vector<Float_t>::const_iterator it = _testSpectatorVec.begin(); it < _testSpectatorVec.end(); ++it)
01017       {
01018          unsigned int index = it - _testSpectatorVec.begin();
01019          test_(_eventC3->GetSpectator(index)  == *it);
01020          test_(_compareSpectatorVec.at(index) == *it);
01021       }
01022 }
01023 
01024 
01025 
01026 void utEvent::_testConstructor4()
01027 {
01028    _eventC4 = new Event( _testValueVec, _testTargetVec, _testClassVal, _testWeight, _testBoostWeight);
01029 
01030    test_(_eventC4->IsDynamic()         == false);
01031 
01032    test_(floatCompare(_eventC4->GetWeight(), _testWeight*_testBoostWeight));
01033    test_(floatCompare(_eventC4->GetOriginalWeight(), _testWeight));
01034    test_(floatCompare(_eventC4->GetBoostWeight(), _testBoostWeight));
01035 
01036    test_(_eventC4->GetClass()          == _testClassVal);
01037    test_(_eventC4->GetNVariables()     == (UInt_t)_testValueVec.size());
01038    test_(_eventC4->GetNTargets()       == (UInt_t)_testTargetVec.size());
01039 
01040    _compareValueVec = _eventC4->GetValues();
01041    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01042       {
01043          unsigned int index = it - _testValueVec.begin();
01044          test_(_eventC4->GetValue(index)  == *it);
01045          test_(_compareValueVec.at(index) == *it);
01046       }
01047    _compareTargetVec = _eventC4->GetTargets();
01048    for(vector<Float_t>::const_iterator it = _testTargetVec.begin(); it < _testTargetVec.end(); ++it)
01049       {
01050          unsigned int index = it - _testTargetVec.begin();
01051          test_(_eventC4->GetTarget(index)  == *it);
01052          test_(_compareTargetVec.at(index) == *it);
01053       }
01054 }
01055 
01056 
01057 
01058 void utEvent::_testConstructor5()
01059 {
01060    _eventC5 = new Event( _testValueVec, _testClassVal, _testWeight, _testBoostWeight);
01061 
01062    test_(_eventC5->IsDynamic()         == false);
01063 
01064    test_(floatCompare(_eventC5->GetWeight(), _testWeight*_testBoostWeight));
01065    test_(floatCompare(_eventC5->GetOriginalWeight(), _testWeight));
01066    test_(floatCompare(_eventC5->GetBoostWeight(), _testBoostWeight));
01067 
01068    test_(_eventC5->GetClass()          == _testClassVal);
01069    test_(_eventC5->GetNVariables()     == (UInt_t)_testValueVec.size());
01070 
01071    _compareValueVec = _eventC5->GetValues();
01072    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01073       {
01074          unsigned int index = it - _testValueVec.begin();
01075          test_(_eventC5->GetValue(index)  == *it);
01076          test_(_compareValueVec.at(index) == *it);
01077       }
01078 }
01079 
01080 
01081 
01082 void utEvent::_testConstructor6()
01083 {
01084    const vector<Float_t*>* _constPointerToPointerVec = &_testPointerVec;
01085    _eventC6 = new Event( _constPointerToPointerVec, _testNVar);
01086 
01087    // TODO I don't understand what the constructor is for
01088    // or in what cases it should be used
01089 
01090    test_(_eventC6->IsDynamic()         == true);
01091 
01092    //   test_(_eventC6->GetWeight()         == 1.);
01093    //   test_(_eventC6->GetOriginalWeight() == 1.);
01094    //   test_(_eventC6->GetBoostWeight()    == 1.);
01095    //   test_(_eventC6->GetClass()          == _testClassVal);
01096    //   test_(_eventC6->GetNVariables()     == (UInt_t)_testValueVec.size());
01097 
01098    // ? const std::vector<UInt_t>* GetVariableArrangement() const { return fVariableArrangement; }
01099   
01100    //   _compareValueVec = _eventC6->GetValues();
01101    //   for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01102    //     {
01103    //       unsigned int index = it - _testValueVec.begin();
01104    //       test_(_eventC6->GetValue(index)  == *it);
01105    //       test_(_compareValueVec.at(index) == *it);
01106    //     }
01107 }
01108 
01109 
01110 
01111 void utEvent::_testMutators()
01112 {
01113    // the empty/default constructor is taken for these tests
01114 
01115    _eventC1->SetWeight(_testWeight);
01116    test_(_eventC1->GetWeight() == _testWeight);
01117 
01118    _eventC1->ScaleWeight(_testScale);
01119    test_(floatCompare((float) _eventC1->GetWeight(), _testWeight*_testScale)); 
01120 
01121    _eventC1->SetBoostWeight(_testBoostWeight);
01122    test_(floatCompare( _eventC1->GetBoostWeight() , _testBoostWeight));
01123    _eventC1->ScaleBoostWeight(_testScale);
01124    test_(floatCompare( _eventC1->GetBoostWeight(), _testBoostWeight*_testScale));
01125 
01126 
01127    _eventC1->SetClass(_testClassVal);
01128    test_(_eventC1->GetClass() == (UInt_t)_testClassVal);
01129 
01130    // check variables
01131    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01132       {
01133          unsigned int index = it - _testValueVec.begin();
01134          _eventC1->SetVal(index, *it);
01135       }
01136    _compareValueVec = _eventC1->GetValues();
01137    for(vector<Float_t>::const_iterator it = _testValueVec.begin(); it < _testValueVec.end(); ++it)
01138       {
01139          unsigned int index = it - _testValueVec.begin();
01140          test_(_eventC1->GetValue(index)  == *it);
01141          test_(_compareValueVec.at(index) == *it);
01142       }
01143    // check targets
01144    for(vector<Float_t>::const_iterator it = _testTargetVec.begin(); it < _testTargetVec.end(); ++it)
01145       {
01146          unsigned int index = it - _testTargetVec.begin();
01147          _eventC1->SetTarget(index, *it);
01148       }
01149    _compareTargetVec = _eventC1->GetTargets();
01150    for(vector<Float_t>::const_iterator it = _testTargetVec.begin(); it < _testTargetVec.end(); ++it)
01151       {
01152          unsigned int index = it - _testTargetVec.begin();
01153          test_(_eventC1->GetTarget(index)  == *it);
01154          test_(_compareTargetVec.at(index) == *it);
01155       }
01156    // check spectators
01157    for(vector<Float_t>::const_iterator it = _testSpectatorVec.begin(); it < _testSpectatorVec.end(); ++it)
01158       {
01159          unsigned int index = it - _testSpectatorVec.begin();
01160          _eventC1->SetSpectator(index, *it);
01161       }
01162    _compareSpectatorVec = _eventC1->GetSpectators();
01163    for(vector<Float_t>::const_iterator it = _testSpectatorVec.begin(); it < _testSpectatorVec.end(); ++it)
01164       {
01165          unsigned int index = it - _testSpectatorVec.begin();
01166          test_(_eventC1->GetSpectator(index)  == *it);
01167          test_(_compareSpectatorVec.at(index) == *it);
01168       }
01169   
01170    _eventC1->SetClass(_testClassVal);
01171    test_(_eventC1->GetClass() == (UInt_t)_testClassVal);
01172 
01173    // ??    _eventC1->SetVariableArrangement( std::vector<UInt_t>* const m ) const;
01174 }
01175 
01176 // including file tmvaut/utReader.h
01177 #ifndef UTREADER_H
01178 #define UTREADER_H
01179 
01180 // Author: E. v. Toerne,  Nov 2011, implementing unit tests by C. Rosemann
01181 // TMVA unit tests
01182 //
01183 // this class acts as interface to several reader applications
01184 
01185 #include <string>
01186 #include <iostream>
01187 #include <cassert>
01188 #include <vector>
01189 
01190 #include "TTree.h"
01191 #include "TString.h"
01192 
01193 #include "TMVA/Reader.h"
01194 #include "TMVA/Types.h"
01195 
01196 
01197 
01198 namespace UnitTesting
01199 {
01200   class utReader : public UnitTest
01201   {
01202   public:
01203     utReader(const char* theOption="");
01204     virtual ~utReader();
01205     
01206     virtual void run();
01207     
01208   protected:
01209 
01210   private:
01211      // disallow copy constructor and assignment
01212      utReader(const utReader&);
01213      utReader& operator=(const utReader&);
01214   };
01215 } // namespace UnitTesting
01216 #endif // 
01217 // including file tmvaut/utReader.cxx
01218 
01219 
01220 #include <string>
01221 #include <iostream>
01222 #include <cassert>
01223 #include <vector>
01224 
01225 #include "TTree.h"
01226 #include "TString.h"
01227 
01228 #include "TMVA/Reader.h"
01229 #include "TMVA/Types.h"
01230 
01231 
01232 
01233 using namespace std;
01234 using namespace UnitTesting;
01235 using namespace TMVA;
01236 
01237 utReader::utReader(const char* /*theOption*/) 
01238    : UnitTest(string("Reader"))
01239 { 
01240 
01241 }
01242 utReader::~utReader(){ }
01243 
01244 void utReader::run()
01245 {
01246    float xtest,xtest2;
01247    Reader* reader2 = new Reader();
01248    Reader* reader3 = new Reader();
01249    reader2->AddVariable("test", &xtest);
01250    reader2->AddVariable("test2", &xtest2);
01251    reader3->AddVariable("test", &xtest);
01252    
01253    delete reader2;
01254    delete reader3;
01255    test_(1>0); 
01256    const int nTest=3;
01257    int ievt;
01258    vector<float> testvar(10);
01259    std::vector< TMVA::Reader* > reader(nTest);
01260    for (int iTest=0;iTest<nTest;iTest++){
01261       reader[iTest] = new TMVA::Reader( "!Color:Silent" );
01262       if (iTest==0){
01263          reader[iTest]->AddVariable( "var0" ,&testvar[0]);
01264          reader[iTest]->AddVariable( "var1" ,&testvar[1]);
01265          reader[iTest]->AddSpectator( "ievt" ,&ievt);
01266          reader[iTest]->BookMVA( "LD method", "weights/TMVATest_LD.weights.xml") ;
01267       }
01268       if (iTest==1){
01269          reader[iTest]->AddVariable( "var0" ,&testvar[0]);
01270          reader[iTest]->AddVariable( "var1" ,&testvar[1]);
01271          reader[iTest]->AddVariable( "var2" ,&testvar[2]);
01272          reader[iTest]->AddSpectator( "ievt" ,&ievt);
01273          reader[iTest]->BookMVA( "LD method", "weights/TMVATest3Var_LD.weights.xml") ;
01274       }
01275       if (iTest==2){
01276          reader[iTest]->AddVariable( "var0" ,&testvar[0]);
01277          reader[iTest]->AddVariable( "var1" ,&testvar[1]);
01278          reader[iTest]->AddVariable( "var2" ,&testvar[2]);
01279          reader[iTest]->AddVariable( "ivar0" ,&testvar[3]);
01280          reader[iTest]->AddVariable( "ivar1" ,&testvar[4]);
01281          reader[iTest]->AddSpectator( "ievt" ,&ievt);
01282          reader[iTest]->BookMVA( "LD method", "weights/TMVATest3VarF2VarI_LD.weights.xml") ;
01283       }
01284    }
01285    reader[0]->EvaluateMVA( "LD method");
01286    reader[1]->EvaluateMVA( "LD method");
01287    reader[2]->EvaluateMVA( "LD method");
01288    test_(1>0);
01289 }
01290 // including file tmvaut/utFactory.h
01291 #ifndef UTFACTORY_H
01292 #define UTFACTORY_H
01293 
01294 // Author: E. v. Toerne,  Nov 2011, implementing unit tests by C. Rosemann
01295 // TMVA unit tests
01296 //
01297 // this class acts as interface to several factory applications
01298 
01299 #include <string>
01300 #include <iostream>
01301 #include <cassert>
01302 #include <vector>
01303 
01304 #include "TTree.h"
01305 #include "TString.h"
01306 
01307 #include "TMVA/Factory.h"
01308 #include "TMVA/Types.h"
01309 
01310 
01311 
01312 namespace UnitTesting
01313 {
01314    class utFactory : public UnitTest
01315    {
01316    public:
01317       utFactory(const char* theOption="");
01318       virtual ~utFactory();
01319       virtual void run();
01320       
01321    protected:
01322       virtual TTree* create_Tree(const char* opt="");
01323       virtual bool operateSingleFactory(const char* factoryname, const char* opt="");
01324       virtual bool addEventsToFactoryByHand(const char* factoryname, const char* opt="");
01325 
01326    private:
01327       // disallow copy constructor and assignment
01328       utFactory(const utFactory&);
01329       utFactory& operator=(const utFactory&);
01330    };
01331 } // namespace UnitTesting
01332 #endif // 
01333 // including file tmvaut/utFactory.cxx
01334 
01335 
01336 #include <string>
01337 #include <iostream>
01338 #include <cassert>
01339 #include <vector>
01340 #include <exception>
01341 
01342 #include "TMath.h"
01343 #include "TTree.h"
01344 #include "TFile.h"
01345 #include "TSystem.h"
01346 #include "TString.h"
01347 
01348 #include "TMVA/Factory.h"
01349 #include "TMVA/MethodBase.h"
01350 #include "TMVA/Types.h"
01351 
01352 
01353 
01354 using namespace std;
01355 using namespace UnitTesting;
01356 using namespace TMVA;
01357 
01358 utFactory::utFactory(const char* /*theOption*/)
01359    : UnitTest(string("Factory"))
01360 {
01361 
01362 }
01363 utFactory::~utFactory(){ }
01364 TTree* utFactory::create_Tree(const char* opt)
01365 {
01366    TString option = opt;
01367    const int nmax=100;
01368    const int nvar=3, nfval=2, nivar=2, nclass=3;
01369    float weight=1.;
01370    vector<float> var(nvar), fval(nfval); // fix me hardcoded nvar
01371    vector<int> ivar(nivar), nevt(nclass);
01372    Int_t iclass, ievt=0,i=0;
01373 
01374    TTree* tree = new TTree( "Tree", "Tree");
01375    for (i=0; i<nvar ; i++) tree->Branch(Form( "var%i", i), &var[i],
01376                                         Form( "var%i/F", i ));
01377    for (i=0; i<nfval; i++) tree->Branch(Form( "fval%i", i), &fval[i],
01378                                         Form( "fval%i/F", i ));
01379    for (i=0; i<nivar; i++) tree->Branch(Form( "ivar%i", i), &ivar[i],
01380                                         Form( "ivar%i/I", i ));
01381    tree->Branch("iclass",&iclass,"iclass/I");
01382    tree->Branch("ievt",&ievt,"ievt/I");
01383    tree->Branch("weight",&weight,"weight/F");
01384    TRandom3 R( 99 );
01385 
01386    do {
01387       for (i=0; i<nvar; i++)  var[i]  = 2.*R.Rndm()-1.;
01388       for (i=0; i<nivar; i++) ivar[i] = (int) (20.*(R.Rndm()-0.5));
01389       for (i=0; i<nfval; i++)  fval[i]  = 5.*(R.Rndm()-0.5);
01390       Float_t xout = var[0]+var[1]+var[2]*var[1]-var[0]*var[1]*var[1]+2.*(R.Rndm()-1.);
01391       if (xout < -2.) iclass=2;
01392       else if (xout > 0) iclass=0; // signal
01393       else iclass=1;               // background
01394       nevt[iclass]++;
01395       //std::cout << xout<< " " <<nevt[0]<< " " << nevt[1]<< " " << nevt[2]<< std::endl;
01396       ievt++;
01397       tree->Fill();
01398    } while ( TMath::Min(nevt[0],TMath::Min(nevt[1],nevt[2])) < nmax);
01399    //tree->Print();
01400    return tree;
01401 }
01402 
01403 bool utFactory::addEventsToFactoryByHand(const char* factoryname, const char* opt)
01404 {
01405 #ifdef COUTDEBUG
01406    std::cout <<"addEventsToFactoryByHand option="<<opt<<std::endl;
01407 #endif
01408    TString option=opt;
01409    bool useWeights = option.Contains("useWeights");
01410    bool useNegWeights = option.Contains("useNegWeights");
01411    TString _methodTitle,_methodOption="!H:!V"; // fix me
01412    if (option.Contains("useBDT")) _methodTitle="BDT";
01413    else if (option.Contains("useMLP")) _methodTitle="MLP";
01414    else _methodTitle="LD";
01415 
01416    TString prepareString="";
01417    string factoryOptions( "!V:Silent:Transformations=I:AnalysisType=Classification:!Color:!DrawProgressBar" );
01418    TString outfileName( "weights/ByHand.root" );
01419    TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
01420    Factory* factory = new Factory(factoryname,outputFile,factoryOptions);
01421    factory->AddVariable( "var0",  "Variable 0", 'F' );
01422    factory->AddVariable( "var1",  "Variable 1", 'F' );
01423 
01424    vector <double> vars(2);
01425    TRandom3 r(99);
01426    double weight = 1.;
01427    for (int i=0;i<100;i++){
01428       if (useWeights){
01429          vars[0]= 4. * (r.Rndm()-0.5);
01430          vars[1]= 4. * (r.Rndm()-0.5);
01431          weight = TMath::Gaus(vars[0],1.,1.)*TMath::Gaus(vars[1],0.,1.);
01432          if (useNegWeights && i>90) weight = -weight;
01433       }
01434       else {
01435          vars[0]=r.Gaus(1.,1.);
01436          vars[1]=r.Gaus(0.,1.);
01437       }
01438       factory->AddSignalTrainingEvent( vars, weight );
01439       factory->AddSignalTestEvent( vars, weight );
01440    }
01441    for (int i=0;i<100;i++){
01442       vars[0]= 4. * (r.Rndm()-0.5);
01443       vars[1]= 4. * (r.Rndm()-0.5);
01444       weight = 1.;
01445       factory->AddBackgroundTrainingEvent( vars, weight);
01446       factory->AddBackgroundTestEvent( vars, weight);
01447    }
01448    if (prepareString=="") prepareString = "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" ;
01449    factory->PrepareTrainingAndTestTree( "", "", prepareString);
01450 
01451    factory->BookMethod(_methodTitle,_methodTitle, "!H:!V");
01452    factory->TrainAllMethods();
01453    factory->TestAllMethods();
01454    factory->EvaluateAllMethods();
01455    MethodBase* theMethod = dynamic_cast<TMVA::MethodBase*> (factory->GetMethod(_methodTitle));
01456    double ROCValue = theMethod->GetROCIntegral();
01457    //cout << "ROC="<<ROCValue<<endl;
01458    delete factory;
01459    outputFile->Close();
01460    if (outputFile) delete outputFile;
01461    return (ROCValue>0.6);
01462 }
01463 
01464 bool utFactory::operateSingleFactory(const char* factoryname, const char* opt)
01465 {
01466    if (TString(factoryname)=="") factoryname = "TMVATest";
01467 #ifdef COUTDEBUG
01468    std::cout <<"operateSingleFactory option="<<opt<<std::endl;
01469 #endif
01470    TString option=opt;
01471    TTree* tree(0);
01472    TFile* inFile(0);
01473    if (!(option.Contains("MemoryResidentTree"))){
01474       inFile = TFile::Open( "weights/input.root", "RECREATE" );
01475       tree = create_Tree();
01476       inFile->Write();
01477       inFile->Close();
01478       if (inFile) delete inFile;
01479       inFile = TFile::Open( "weights/input.root");
01480       tree = (TTree*) inFile->Get("Tree");
01481    }
01482    else if (! (option.Contains("LateTreeBooking"))) tree = create_Tree();
01483 
01484    TString _methodTitle="LD",_methodOption="!H:!V"; // fix me
01485    TString prepareString="";
01486    string factoryOptions( "!V:Silent:Transformations=I,D:AnalysisType=Classification:!Color:!DrawProgressBar" );
01487    TString outfileName( "weights/TMVA.root" );
01488    TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
01489    if (option.Contains("LateTreeBooking") && option.Contains("MemoryResidentTree")) tree = create_Tree();
01490 
01491    Factory* factory = new Factory(factoryname,outputFile,factoryOptions);
01492    factory->AddVariable( "var0",  "Variable 0", 'F' );
01493    factory->AddVariable( "var1",  "Variable 1", 'F' );
01494    if (option.Contains("var2"))  factory->AddVariable( "var2",  "Var 2", 'F' );
01495    if (option.Contains("ivar0")) factory->AddVariable( "ivar0",  "Var i0", 'I' );
01496    if (option.Contains("ivar1")) factory->AddVariable( "ivar1",  "Var i1", 'I' );
01497 
01498    factory->AddSpectator( "ievt", 'I' );
01499    factory->AddSignalTree(tree);
01500    factory->AddBackgroundTree(tree);
01501    if (prepareString=="") prepareString = "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" ;
01502    // this crashes "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" ;
01503    factory->PrepareTrainingAndTestTree( "iclass==0", "iclass==1", prepareString);
01504 
01505    if (option.Contains("StringMethodBooking")) factory->BookMethod("LD","LD","!H:!V");
01506    else factory->BookMethod(TMVA::Types::kLD,"LD","!H:!V");
01507 
01508    //factory->BookMethod(_methodType, _methodTitle, _methodOption);
01509 
01510    factory->TrainAllMethods();
01511    factory->TestAllMethods();
01512    factory->EvaluateAllMethods();
01513    MethodBase* theMethod = dynamic_cast<TMVA::MethodBase*> (factory->GetMethod(_methodTitle));
01514    double ROCValue = theMethod->GetROCIntegral();   
01515    delete tree;
01516    delete factory;
01517    outputFile->Close(); 
01518    if (option.Contains("InputFile")){
01519       inFile->Close();
01520    }
01521    if (outputFile) delete outputFile;
01522    if (inFile) delete inFile;
01523 
01524    Bool_t ret = (ROCValue>0.6);
01525    if (!ret) {
01526       std::cout <<"FAILURE with operateSingleFactory option="<<opt<< ",  bad value ROC="<<ROCValue<<std::endl;
01527    }
01528    return ret;
01529 }
01530 
01531 void utFactory::run()
01532 {
01533    // create directory weights if necessary 
01534    FileStat_t stat;   
01535    if(gSystem->GetPathInfo("./weights",stat)) {// FIXME:: give the filename of the sample somewhere else?
01536       gSystem->MakeDirectory("weights"); 
01537 #ifdef COUTDEBUG
01538       std::cout << "creating directory weights"<<std::endl;
01539 #endif
01540    }
01541    // create three factories with two methods each
01542    test_(addEventsToFactoryByHand("ByHand","")); // uses Factory::AddSignalTrainingEvent
01543    test_(addEventsToFactoryByHand("ByHand2","useWeights")); 
01544 
01545    test_(addEventsToFactoryByHand("ByHand","useMLP")); // uses Factory::AddSignalTrainingEvent
01546    test_(addEventsToFactoryByHand("ByHand2","useWeights:useMLP")); 
01547 
01548 
01549    test_(addEventsToFactoryByHand("ByHand","useBDT")); // uses Factory::AddSignalTrainingEvent
01550    test_(addEventsToFactoryByHand("ByHand2","useWeights:useNegWeights:useBDT")); 
01551    test_(addEventsToFactoryByHand("ByHand2","useWeights:useBDT")); 
01552 
01553 
01554 
01555    test_(operateSingleFactory("TMVATest","StringMethodBooking"));
01556    test_(operateSingleFactory("TMVATest",""));
01557    test_(operateSingleFactory("TMVATest3Var","var2"));
01558    test_(operateSingleFactory("TMVATest3VarF2VarI","var2:ivar0:ivar1"));
01559 
01560 
01561 // uses Factory::AddSignalTrainingEvent
01562 
01563    //creates crash test_(operateSingleFactory("TMVATest","MemoryResidentTree:StringMethodBooking"));
01564    //creates crash test_(operateSingleFactory("TMVATest","MemoryResidentTree"));
01565    //creates crash test_(operateSingleFactory("TMVATest","MemoryResidentTree:LateTreeBooking"));
01566    //creates crash test_(operateSingleFactory("TMVATest","MemoryResidentTree:LateTreeBooking:StringMethodBooking"));
01567 
01568 
01569 
01570 }
01571 
01572 // including file tmvaut/utVariableInfo.h
01573 #ifndef UTVARIABLEINFO_H
01574 #define UTVARIABLEINFO_H
01575 
01576 // Author: Christoph Rosemann   Dec. 2009
01577 // TMVA unit tests
01578 
01579 #include <vector>
01580 
01581 #include "TString.h"
01582 
01583 
01584 
01585 namespace TMVA {
01586    class VariableInfo;
01587 }
01588 
01589 
01590 class utVariableInfo : public UnitTesting::UnitTest
01591 {
01592  public:
01593   utVariableInfo();
01594   void run();
01595 
01596  private:
01597   // the test calls in different blocks
01598   // the distinctions are arbitrary:
01599   void _testConstructor1();
01600   void _testConstructor2();
01601   void _testConstructor3();
01602 
01603   void _testMethods();
01604   
01605   // there are six different constructors:
01606   TMVA::VariableInfo* _varinfoC1; 
01607   TMVA::VariableInfo* _varinfoC2; 
01608   TMVA::VariableInfo* _varinfoC3; 
01609 
01610   // the values needed to create all "VariableInfo" objects in all ways
01611   TString  expression;
01612   TString  title;
01613   TString  unit;
01614   Int_t    varCounter;
01615   char     varType;
01616   Double_t min;
01617   Double_t max;
01618   Bool_t   normalized;
01619   void*    external;
01620   Float_t  mean;
01621   Float_t  rms;
01622 };
01623 #endif // UTVARIABLEINFO_H
01624 // including file tmvaut/utVariableInfo.cxx
01625 
01626 #include "TMath.h"
01627 
01628 #include "TMVA/VariableInfo.h"
01629 
01630 using namespace std;
01631 using namespace UnitTesting;
01632 using namespace TMVA;
01633 
01634 utVariableInfo::utVariableInfo() :
01635    UnitTest("VariableInfo", __FILE__)
01636 {
01637    expression = "expression";
01638    title      = "title";
01639    unit       = "unit";
01640 
01641    varCounter = 123;
01642    varType    = 'D';
01643    min        = 2.781828;
01644    max        = 3.1416;
01645    normalized = kTRUE;
01646    external   = &max;
01647 
01648    mean       = 42.;
01649    rms        = 47.11;
01650 }
01651 
01652 
01653 
01654 void utVariableInfo::run()
01655 {
01656    _testConstructor1();
01657    _testConstructor2();
01658    _testConstructor3();
01659    _testMethods();
01660 }
01661 
01662 
01663 
01664 void utVariableInfo::_testConstructor1()
01665 {
01666    _varinfoC1 = new VariableInfo( expression, title,  unit, varCounter, varType, external, min, max, normalized);
01667 
01668    test_(_varinfoC1->GetExpression()  == expression);
01669    //  test_(_varinfoC1->GetInternalName() == );
01670    //  test_(_varinfoC1->GetLabel()
01671    test_(_varinfoC1->GetTitle()   == title);
01672    test_(_varinfoC1->GetUnit()    == unit);
01673    test_(_varinfoC1->GetVarType() == varType);
01674 
01675    test_(_varinfoC1->GetMin()  == min);
01676    test_(_varinfoC1->GetMax()  == max);
01677    //   test_(_varinfoC1->GetMean() == 0.);
01678    //   test_(_varinfoC1->GetRMS()  == 0.);
01679    test_(_varinfoC1->GetExternalLink() == external);
01680 
01681 }
01682 
01683 
01684 
01685 void utVariableInfo::_testConstructor2()
01686 {
01687    _varinfoC2 = new VariableInfo();
01688    test_(_varinfoC2->GetExpression()  == "");
01689    //  test_(_varinfoC2->GetInternalName() == );
01690    //  test_(_varinfoC2->GetLabel()
01691    test_(_varinfoC2->GetTitle()   == "");
01692    test_(_varinfoC2->GetUnit()    == "");
01693    test_(_varinfoC2->GetVarType() == '\0');
01694 
01695    test_(_varinfoC2->GetMin()  == 1e30);
01696    test_(_varinfoC2->GetMax()  == -1e30);
01697    //   test_(_varinfoC2->GetMean() == 0.);
01698    //   test_(_varinfoC2->GetRMS()  == 0.);
01699    //  test_(_varinfoC2->GetExternalLink() == external);
01700 }
01701 
01702 
01703 
01704 void utVariableInfo::_testConstructor3()
01705 {
01706    _varinfoC3 = new VariableInfo(*_varinfoC1);
01707 
01708    test_(_varinfoC3->GetExpression()  == expression);
01709    //  test_(_varinfoC3->GetInternalName() == );
01710    //  test_(_varinfoC3->GetLabel()
01711    test_(_varinfoC3->GetTitle()   == title);
01712    test_(_varinfoC3->GetUnit()    == unit);
01713    test_(_varinfoC3->GetVarType() == varType);
01714 
01715    test_(_varinfoC3->GetMin()  == min);
01716    test_(_varinfoC3->GetMax()  == max);
01717    //   test_(_varinfoC3->GetMean() == 0.);
01718    //   test_(_varinfoC3->GetRMS()  == 0.);
01719    test_(_varinfoC3->GetExternalLink() == external);
01720 }
01721 
01722 
01723 
01724 void utVariableInfo::_testMethods()
01725 {
01726    _varinfoC2->SetMin(min);
01727    _varinfoC2->SetMax(max);
01728    _varinfoC2->SetMean(mean);
01729    _varinfoC2->SetRMS(rms);
01730    _varinfoC2->SetExternalLink(external);
01731 
01732    test_(_varinfoC2->GetMin()  == min);
01733    test_(_varinfoC2->GetMax()  == max);
01734    test_(_varinfoC2->GetMean() == mean);
01735    test_(_varinfoC2->GetRMS()  == rms);
01736 
01737    _varinfoC2->ResetMinMax();
01738    test_(_varinfoC2->GetMin()  == 1e30);
01739    test_(_varinfoC2->GetMax()  == -1e30);
01740 
01741    // test assignment
01742    *_varinfoC2 = *_varinfoC1;
01743 
01744    test_(_varinfoC2->GetExpression()  == expression);
01745    //  test_(_varinfoC2->GetTitle()   == title); // title is not copied
01746    //  test_(_varinfoC2->GetUnit()    == unit);  // unit is not copied
01747    test_(_varinfoC2->GetVarType() == varType);
01748 
01749    test_(_varinfoC2->GetMin()  == min);
01750    test_(_varinfoC2->GetMax()  == max);
01751    test_(_varinfoC2->GetExternalLink() == external);
01752 }
01753 
01754 
01755 
01756 
01757 // including file tmvaut/MethodUnitTestWithROCLimits.h
01758 #ifndef METHODUNITTESTWITHROCLIMITS_H
01759 #define METHODUNITTESTWITHROCLIMITS_H
01760 
01761 // Author: Christoph Rosemann  Jul 2010
01762 // TMVA unit tests
01763 //
01764 // this class acts as interface to create, train and evaluate the method 
01765 // specified in the constructor
01766 // as additional argument the limits of the ROC integral can be given 
01767 // to determine the performance
01768 
01769 #include <string>
01770 #include <iostream>
01771 #include <cassert>
01772 #include <vector>
01773 
01774 #include "TTree.h"
01775 #include "TString.h"
01776 
01777 #include "TMVA/Factory.h"
01778 #include "TMVA/MethodBase.h"
01779 #include "TMVA/Types.h"
01780 
01781 
01782 
01783 namespace UnitTesting
01784 {
01785   class MethodUnitTestWithROCLimits : public UnitTest
01786   {
01787   public:
01788     MethodUnitTestWithROCLimits(const TMVA::Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
01789                                                                 double lowLimit = 0., double upLimit = 1.,
01790                                                                 const std::string & name="", const std::string & filename="", std::ostream* osptr = &std::cout);
01791     virtual ~MethodUnitTestWithROCLimits();
01792     
01793     virtual void run();
01794     
01795   protected:
01796     TTree*          theTree;
01797 
01798   private:
01799      TMVA::Factory* _factory; 
01800      TMVA::MethodBase* _theMethod;
01801      TMVA::Types::EMVA _methodType;
01802      TString _methodTitle;
01803      TString _methodOption;
01804         
01805      double _upROCLimit;
01806      double _lowROCLimit;
01807      double _ROCValue;
01808 
01809      //TString _OutputROOTFile;
01810      std::vector<TString>* _VariableNames;
01811      std::vector<TString>* _TreeVariableNames;
01812      bool ROCIntegralWithinInterval();
01813 
01814      // disallow copy constructor and assignment
01815      MethodUnitTestWithROCLimits(const MethodUnitTestWithROCLimits&);
01816      MethodUnitTestWithROCLimits& operator=(const MethodUnitTestWithROCLimits&);
01817   };
01818 } // namespace UnitTesting
01819 #endif // METHODUNITTESTWITHROCLIMITS_H
01820 // including file tmvaut/MethodUnitTestWithROCLimits.cxx
01821 
01822 #include "TFile.h"
01823 #include "TROOT.h"
01824 #include "TSystem.h"
01825 #include "TMath.h"
01826 #include "TMVA/MethodBase.h"
01827 #include "TMVA/Reader.h"
01828 #include <iostream>
01829 #include <fstream>
01830 #include <sstream>
01831 
01832 using namespace std;
01833 using namespace UnitTesting;
01834 using namespace TMVA;
01835 
01836 MethodUnitTestWithROCLimits::MethodUnitTestWithROCLimits(const Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
01837                                                                                                                 double lowLimit, double upLimit,
01838                                                          const std::string & /* xname */ ,const std::string & /* filename */ , std::ostream* /* sptr */ ) :
01839    UnitTest((string)methodTitle, __FILE__), _methodType(theMethod) , _methodTitle(methodTitle), _methodOption(theOption), _upROCLimit(upLimit), _lowROCLimit(lowLimit), _VariableNames(0), _TreeVariableNames(0)
01840 {
01841    _VariableNames  = new std::vector<TString>(0);
01842    _TreeVariableNames = new std::vector<TString>(0);
01843    _VariableNames->push_back("var1+var2");
01844    _VariableNames->push_back("var1-var2");
01845    _VariableNames->push_back("var3");
01846    _VariableNames->push_back("var4");
01847    _TreeVariableNames->push_back("myvar1");
01848    _TreeVariableNames->push_back("myvar2");
01849    _TreeVariableNames->push_back("var3");
01850    _TreeVariableNames->push_back("var4");
01851 }
01852 
01853 
01854 MethodUnitTestWithROCLimits::~MethodUnitTestWithROCLimits()
01855 {
01856 }
01857 
01858 bool MethodUnitTestWithROCLimits::ROCIntegralWithinInterval()
01859 {
01860    return (_ROCValue <= _upROCLimit) && (_ROCValue >= _lowROCLimit);
01861 }
01862 
01863 void MethodUnitTestWithROCLimits::run()
01864 {
01865    TString outfileName( "weights/TMVA.root" );                                                                                                                       
01866    TFile* outputFile = TFile::Open( outfileName, "RECREATE" );         
01867 
01868 // FIXME:: if file can't be created do something more?
01869   if(!outputFile)
01870     return;    
01871 
01872 // FIXME:: make the factory option mutable?
01873 // absolute silence options:
01874   string factoryOptions( "!V:Silent:AnalysisType=Classification:!Color:!DrawProgressBar" );
01875 
01876   if (_methodOption.Contains("VarTransform")) factoryOptions+=":Transformations=I;D;P;G";
01877   Factory* factory = new Factory( "TMVAUnitTesting", outputFile, factoryOptions );
01878   // factory->AddVariable( "myvar1 := var1+var2", 'F' );
01879   // factory->AddVariable( "myvar2 := var1-var2", "Expression 2", "", 'F' );
01880   factory->AddVariable( Form("%s  := %s",_TreeVariableNames->at(0).Data(), _VariableNames->at(0).Data()), 'F' );
01881   factory->AddVariable( Form("%s  := %s",_TreeVariableNames->at(1).Data(), _VariableNames->at(1).Data()), "Expression 2", "",'F' );
01882   factory->AddVariable( _VariableNames->at(2),                "Variable 3", "units", 'F' );
01883   factory->AddVariable( _VariableNames->at(3),                "Variable 4", "units", 'F' );
01884   
01885   TFile* input(0);
01886 
01887   FileStat_t stat;
01888   
01889   TString fname = "../tmva/test/data/toy_sigbkg.root"; //tmva_example.root"; 
01890   if(!gSystem->GetPathInfo(fname,stat)) {
01891      input = TFile::Open( fname );
01892   } else if(!gSystem->GetPathInfo("../"+fname,stat)) {
01893      input = TFile::Open( "../"+fname );
01894   } else {
01895      input = TFile::Open( "http://root.cern.ch/files/tmva_example.root" );
01896   }
01897   if (input == NULL) {
01898      cerr << "broken/inaccessible input file" << endl;
01899   }
01900   
01901   TTree *signal     = (TTree*)input->Get("TreeS");
01902   TTree *background = (TTree*)input->Get("TreeB");
01903   
01904   factory->AddSignalTree(signal);  
01905   factory->AddBackgroundTree(background);
01906   
01907   factory->SetBackgroundWeightExpression("weight");
01908   
01909   TCut mycuts = "";
01910   TCut mycutb = "";
01911   
01912   // FIXME:: make options string mutable?
01913   factory->PrepareTrainingAndTestTree( mycuts, mycutb,
01914                                        "nTrain_Signal=1000:nTrain_Background=1000:nTest_Signal=5000:nTest_Background=5000:SplitMode=Random:NormMode=NumEvents:!V" );
01915  
01916   factory->BookMethod(_methodType, _methodTitle, _methodOption);
01917 
01918   factory->TrainAllMethods();
01919 
01920   factory->TestAllMethods();
01921 
01922   factory->EvaluateAllMethods();
01923 
01924   _theMethod = dynamic_cast<TMVA::MethodBase*> (factory->GetMethod(_methodTitle));
01925 
01926   if (_methodType == TMVA::Types::kCuts) {
01927      // ToDo make class variable _theEffi
01928      Double_t err=0.;
01929      Double_t effi = _theMethod->GetEfficiency("Efficiency:0.1", Types::kTesting,err);
01930 #ifdef COUTDEBUG
01931      std::cout << "Cuts Signal effi at for Background effi of 0.1 = " << effi<<" low limit="<<_lowROCLimit<<" high limit="<<_upROCLimit<<std::endl;
01932 #endif
01933      test_(effi <= _upROCLimit && effi>=_lowROCLimit);
01934   }
01935   else {
01936      _ROCValue = _theMethod->GetROCIntegral();
01937 #ifdef COUTDEBUG
01938      std::cout << "ROC integral = "<<_ROCValue <<" low limit="<<_lowROCLimit<<" high limit="<<_upROCLimit<<std::endl;
01939 #endif     
01940      if (!ROCIntegralWithinInterval()){
01941         std::cout << "failure in " << _methodTitle<<", ROC integral = "<<_ROCValue <<" low limit="<<_lowROCLimit<<" high limit="<<_upROCLimit<<std::endl;
01942      }     
01943      test_(ROCIntegralWithinInterval());
01944   }
01945   outputFile->Close();
01946   delete factory;
01947 
01948   if (outputFile) delete outputFile;
01949   
01950   // Reader tests
01951   const int nTest=6; // 3 reader usages + 3 tests with additional readers
01952   float testTreeVal,readerVal=0.;
01953   vector<float>  testvar(_VariableNames->size());
01954   vector<float>  dummy(_VariableNames->size());
01955   vector<float>  dummy2(_VariableNames->size());
01956   vector<float>  testvarFloat(_VariableNames->size());
01957   vector<double> testvarDouble(_VariableNames->size());
01958 
01959   // setup test tree access
01960   TFile* testFile = new TFile("weights/TMVA.root");
01961   TTree* testTree = (TTree*)(testFile->Get("TestTree"));
01962   for (UInt_t i=0;i<_VariableNames->size();i++)
01963      testTree->SetBranchAddress(_TreeVariableNames->at(i),&testvar[i]);
01964   testTree->SetBranchAddress(_methodTitle.Data(),&testTreeVal);
01965 
01966   std::vector<TString> variableNames2;
01967   variableNames2.push_back("var0");
01968   variableNames2.push_back("var1");
01969   TFile* testFile2 = new TFile("weights/ByHand.root");
01970   TTree* testTree2 = (TTree*)(testFile2->Get("TestTree"));
01971   testTree2->SetBranchAddress("var0",&dummy[0]);
01972   testTree2->SetBranchAddress("var1",&dummy[1]);
01973 
01974   TString readerName = _methodTitle + TString(" method");
01975   TString readerOption="!Color:Silent";
01976   TString dir    = "weights/TMVAUnitTesting_";
01977   TString weightfile=dir+_methodTitle+".weights.xml";
01978   TString weightfile2="weights/ByHand_BDT.weights.xml"; //TMVATest3VarF2VarI_BDT.weights.xml
01979   TString readerName2 = "BDT method";
01980   double diff, maxdiff = 0., sumdiff=0., previousVal=0.;
01981   int stuckCount=0, nevt= TMath::Min((int) testTree->GetEntries(),100);
01982   const float effS=0.301;
01983 
01984   TMVA::Reader* reader2=0;
01985   std::vector< TMVA::Reader* > reader(nTest);
01986   for (int iTest=0;iTest<nTest;iTest++){
01987      //std::cout << "iTest="<<iTest<<std::endl;
01988      if (iTest==0){
01989         reader[iTest] = new TMVA::Reader( readerOption );
01990         for (UInt_t i=0;i<_VariableNames->size();i++)
01991            reader[iTest]->AddVariable( _VariableNames->at(i),&testvar[i]);
01992         reader[iTest] ->BookMVA( readerName, weightfile) ;
01993      }
01994      else if (iTest==1 || iTest ==2) {
01995         reader[iTest] = new TMVA::Reader( *_VariableNames, readerOption );
01996         reader[iTest] ->BookMVA( readerName, weightfile) ;
01997      }
01998      else if (iTest==3) { // multiple reader
01999         reader[iTest] = new TMVA::Reader( *_VariableNames, readerOption );
02000         reader2 = new TMVA::Reader( variableNames2, readerOption );
02001         reader[iTest]->BookMVA( readerName, weightfile) ;
02002         reader2->BookMVA( readerName2, weightfile2) ;
02003      }
02004      else if (iTest==4) { // multiple reader
02005         reader[iTest] = new TMVA::Reader( readerOption );
02006         for (UInt_t i=0;i<_VariableNames->size();i++)
02007            reader[iTest]->AddVariable( _VariableNames->at(i),&testvar[i]);
02008         reader[iTest] ->BookMVA( readerName, weightfile) ;
02009         reader2 = new TMVA::Reader( readerOption );
02010         for (UInt_t j=0;j<variableNames2.size();j++) reader2->AddVariable( variableNames2.at(j),&dummy[j]);
02011         reader2->BookMVA( readerName2, weightfile2) ;
02012      }
02013      else if (iTest==5) { // multiple reader
02014         reader2 = new TMVA::Reader( readerOption );
02015         for (UInt_t j=0;j<variableNames2.size();j++) reader2->AddVariable( variableNames2.at(j),&dummy[j]);
02016         reader[iTest] = new TMVA::Reader( readerOption );
02017         for (UInt_t i=0;i<_VariableNames->size();i++)
02018            reader[iTest]->AddVariable( _VariableNames->at(i),&testvar[i]);
02019         reader[iTest] ->BookMVA( readerName, weightfile) ;
02020         reader2->BookMVA( readerName2, weightfile2) ;
02021      }
02022      else {
02023         std::cout <<"error, itest not known"<<std::endl;
02024         std::exit(1);
02025      }
02026 
02027      
02028      // run the reader application and compare to test tree  
02029      for (Long64_t ievt=0;ievt<nevt;ievt++) {
02030         testTree->GetEntry(ievt);
02031         if (testTree2) testTree2->GetEntry(ievt);
02032 
02033         for (UInt_t i=0;i<_VariableNames->size();i++){
02034            testvarDouble[i]= testvar[i];
02035            testvarFloat[i]= testvar[i];
02036         }
02037 
02038         if (iTest==0){
02039            if (_methodType==Types::kCuts) 
02040               readerVal = reader[iTest]->EvaluateMVA( readerName, effS );
02041            else readerVal=reader[iTest]->EvaluateMVA( readerName);  
02042         } 
02043         else if (iTest==1){
02044            if (_methodType==Types::kCuts) 
02045               readerVal = reader[iTest]->EvaluateMVA( testvarFloat, readerName, effS );
02046            else readerVal=reader[iTest]->EvaluateMVA( testvarFloat, readerName);  
02047         }
02048         else if (iTest==2){
02049            if (_methodType==Types::kCuts) 
02050               readerVal = reader[iTest]->EvaluateMVA( testvarDouble, readerName, effS );
02051            else readerVal=reader[iTest]->EvaluateMVA( testvarDouble, readerName);  
02052         }
02053         else if (iTest==3 ){
02054            double dummy3 = reader2->EvaluateMVA( testvarDouble, readerName2);
02055            if (_methodType==Types::kCuts) 
02056               readerVal = reader[iTest]->EvaluateMVA( testvarDouble, readerName, effS );
02057            else readerVal=reader[iTest]->EvaluateMVA( testvarDouble, readerName);  
02058            dummy3 += reader2->EvaluateMVA( testvarDouble, readerName2);
02059         }
02060         else if (iTest==4){
02061            double dummy4 = reader2->EvaluateMVA( testvarDouble, readerName);
02062            if (_methodType==Types::kCuts) 
02063               readerVal = reader[iTest]->EvaluateMVA( testvarDouble, readerName, effS );
02064            else readerVal=reader[iTest]->EvaluateMVA( testvarDouble, readerName);  
02065            dummy4 += reader2->EvaluateMVA( testvarDouble, readerName);
02066         }
02067         else if (iTest==5){
02068            double dummy5 = reader2->EvaluateMVA( readerName2);
02069            if (_methodType==Types::kCuts) 
02070               readerVal = reader[iTest]->EvaluateMVA( readerName, effS );
02071            else readerVal=reader[iTest]->EvaluateMVA( readerName);  
02072            dummy5 += reader2->EvaluateMVA( readerName2);
02073         }
02074         else {
02075            std::cout << "ERROR, undefined iTest value "<<iTest<<endl;
02076            exit(1);
02077         }
02078         if (_methodType!=Types::kCuts){
02079            diff = TMath::Abs(readerVal-testTreeVal);
02080            maxdiff = diff > maxdiff ? diff : maxdiff;
02081            sumdiff += diff;
02082         }
02083         if (ievt>0 && iTest ==0 && TMath::Abs(readerVal-previousVal)<1.e-6) stuckCount++; 
02084         if (iTest ==0 ) previousVal=readerVal;
02085      }
02086 
02087   }
02088   Bool_t ok=false;
02089   sumdiff=sumdiff/nevt;
02090   if (_methodType!=Types::kCuts){
02091      test_(maxdiff <1.e-4);
02092      test_(sumdiff <1.e-5);
02093      test_(stuckCount<nevt/10);
02094      if (maxdiff <1.e-4 && sumdiff <1.e-5 && stuckCount<nevt/10) ok=true;
02095   }
02096   if (_methodType==Types::kCuts){
02097      test_(stuckCount<nevt-20);
02098      test_(sumdiff <0.005);
02099      if (stuckCount<nevt-20 && sumdiff <0.005) ok=true;
02100   }
02101   testFile->Close();
02102 
02103   for (int i=0;i<nTest;i++) delete reader[i]; 
02104   if (reader2) delete reader2;
02105 
02106   if (!ok){
02107      cout << "Failure in reader test "<< _methodTitle <<": maxdiff="<<maxdiff<<", sumdiff="<<sumdiff<<" stuckcount="<<stuckCount<<endl;
02108   } 
02109 #ifdef COUTDEBUG
02110   if (ok){
02111      cout << "end of reader test maxdiff="<<maxdiff<<", sumdiff="<<sumdiff<<" stuckcount="<<stuckCount<<endl;
02112   }
02113 #endif
02114 
02115   bool _DoTestCCode=true; 
02116   
02117   // use: grep -A5  'MakeClassSpecific' ../tmva/src/Method*.cxx
02118 
02119   if (_methodType==Types::kCuts  // non-implemented makeclass methods BayesClassifier CFMlpANN Committee Cuts KNN PDERS RuleFit SVM
02120       || _methodType==Types::kBayesClassifier
02121       || _methodType==Types::kCFMlpANN
02122       || _methodType==Types::kCommittee
02123       || _methodType==Types::kCuts
02124       || _methodType==Types::kKNN
02125       || _methodType==Types::kPDERS
02126       || _methodType==Types::kRuleFit
02127       || _methodType==Types::kPDEFoam
02128       || _methodTitle == "BoostedFisher"
02129       ) _DoTestCCode=false;
02130 
02131 #ifndef FULL
02132   _DoTestCCode=false;
02133 #endif
02134 
02135   if (_DoTestCCode){
02136 #ifdef COUTDEBUG
02137      cout << "starting standalone c-code test"<<endl;
02138 #endif
02139      // create generic macro
02140      TString macroName=Form("testmakeclass_%s",_methodTitle.Data());
02141      TString macroFileName=TString("weights/")+macroName+TString(".C");
02142      TString methodTypeName = Types::Instance().GetMethodName(_methodType);
02143      FileStat_t stat2;
02144      if(!gSystem->GetPathInfo(macroFileName.Data(),stat2)) {
02145         gSystem->Unlink(macroFileName.Data());
02146         //gSystem->Exec(Form("rm %s",macroFileName.Data()));
02147      }
02148      ofstream fout( macroFileName );
02149      fout << "// generic macro file to test TMVA reader and standalone C code " << std::endl;
02150      fout << "#include \"TFile.h\""<<std::endl;
02151      fout << "#include \"TTree.h\""<<std::endl;
02152      fout << "#include <vector>"<<std::endl;
02153      fout << Form("#include \"weights/TMVAUnitTesting_%s.class.C\"",_methodTitle.Data()) << std::endl;
02154      fout << Form("bool %s(){",macroName.Data()) << std::endl;
02155      fout << "std::vector<std::string> vars(4);" << std::endl; // fix me 4
02156      fout << "std::vector<double> val(4);" << std::endl;  // fix me 4
02157      fout << "bool ok=true;" << std::endl;  // fix me 4
02158      for (UInt_t i=0;i<_VariableNames->size();i++)
02159         fout << Form("vars[%d]=\"%s\";",i,_VariableNames->at(i).Data()) << std::endl;  
02160      fout << Form("Read%s  aa(vars);", _methodTitle.Data()) << std::endl;
02161      fout << "TFile* testFile = new TFile(\"weights/TMVA.root\");" << std::endl; // fix me hardcode TMVA.root
02162      fout << " TTree* testTree = (TTree*)(testFile->Get(\"TestTree\"));" << std::endl;
02163      fout << Form("vector<float> testvar(%d);",(Int_t) _VariableNames->size()) << std::endl;
02164      fout << Form("vector<double> testvarDouble(%d);", (Int_t) _VariableNames->size()) << std::endl;
02165      for (UInt_t j=0;j<_VariableNames->size();j++)
02166         fout << Form("testTree->SetBranchAddress(\"%s\",&testvar[%d]);",_TreeVariableNames->at(j).Data(),j) << std::endl;
02167      fout << "float testTreeVal,diff,nrm,maxdiff=0.,sumdiff=0.;" << std::endl;
02168      fout << Form("testTree->SetBranchAddress(\"%s\",&testTreeVal);",_methodTitle.Data()) << std::endl;
02169      fout << "Long64_t nevt= TMath::Min((int) testTree->GetEntries(),100);" << std::endl;
02170      fout << "  for (Long64_t ievt=0; ievt<nevt;ievt++) {" << std::endl;
02171      fout << "    testTree->GetEntry(ievt);" << std::endl;
02172      fout << Form("for (UInt_t i=0;i<%d;i++) testvarDouble[i]= testvar[i];",(Int_t) _VariableNames->size()) << std::endl;
02173      fout << "double ccode_val = aa.GetMvaValue(testvarDouble);" << std::endl;
02174 
02175      fout << "diff = TMath::Abs(ccode_val-testTreeVal);" << std::endl;
02176      fout << "nrm = TMath::Max(TMath::Abs(ccode_val),1.);" << std::endl;
02177      fout << "diff = diff/nrm;" << std::endl;
02178      fout << "if (diff>1.2) std::cout << \"ccode_val=\" << ccode_val <<\"testval=\" << testTreeVal <<std::endl;"<<std::endl;
02179      fout << "maxdiff = diff > maxdiff ? diff : maxdiff;" << std::endl;
02180      fout << "sumdiff += diff;" << std::endl;
02181      fout << "}" << std::endl;
02182      fout << "sumdiff=sumdiff/testTree->GetEntries();" << std::endl;
02183      fout << "if (maxdiff >1.e-2) std::cout << \"maxdiff=\"<<maxdiff<< \", sumdiff=\"<<sumdiff<<std::endl;" << std::endl;
02184      fout << "if (sumdiff >2.e-4) ok=false;" << std::endl;
02185      fout << "testFile->Close();" << std::endl;
02186      fout << "if (!ok) {" << std::endl;
02187      fout << "std::cout << \"maxdiff=\"<<maxdiff<< \", sumdiff=\"<<sumdiff<<std::endl;}" << std::endl;
02188      fout << "return ok;" << std::endl;
02189      fout << "}" << std::endl;
02190      
02191      gROOT->ProcessLine(Form(".L %s+",macroFileName.Data()));
02192      test_(gROOT->ProcessLine(Form("%s()",macroName.Data())));
02193   }
02194 
02195 }
02196 // including file tmvaut/RegressionUnitTestWithDeviation.h
02197 #ifndef REGRESSIONUNITTESTWITHDEVIATION_H
02198 #define REGRESSIONUNITTESTWITHDEVIATION_H
02199 
02200 // Author: Christoph Rosemann  Oct 2010
02201 // TMVA unit tests
02202 //
02203 // this class acts as interface to create, train and evaluate the method 
02204 // specified in the constructor
02205 // additionally the regression performance is evaluated
02206 
02207 #include <string>
02208 #include <iostream>
02209 #include <cassert>
02210 
02211 #include "TTree.h"
02212 #include "TString.h"
02213 
02214 #include "TMVA/Factory.h"
02215 #include "TMVA/MethodBase.h"
02216 #include "TMVA/Types.h"
02217 
02218 
02219 
02220 namespace UnitTesting
02221 {
02222   class RegressionUnitTestWithDeviation : public UnitTest
02223   {
02224   public:
02225     RegressionUnitTestWithDeviation(const TMVA::Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
02226                                     double lowFullLimit = 0., double upFullLimit = 10., double low90PercentLimit = 0., double up90PercentLimit = 0.,
02227                                     const std::string & name="", const std::string & filename="", std::ostream* osptr = &std::cout);
02228     virtual ~RegressionUnitTestWithDeviation();
02229     
02230     virtual void run();
02231     
02232   protected:
02233     TTree*          theTree;
02234 
02235   private:
02236     TMVA::Factory* _factory; 
02237     TMVA::MethodBase* _theMethod;
02238     TMVA::Types::EMVA _methodType;
02239     TString _methodTitle;
02240     TString _methodOption;
02241 
02242     double _lowerFullDeviationLimit;
02243     double _upperFullDeviationLimit;
02244     double _lower90PercentDeviationLimit;
02245     double _upper90PercentDeviationLimit;
02246     
02247     double _theFullDeviation;
02248     double _the90PercentDeviation;
02249     
02250     bool DeviationWithinLimits();
02251 
02252     // disallow copy constructor and assignment
02253     RegressionUnitTestWithDeviation(const RegressionUnitTestWithDeviation&);
02254     RegressionUnitTestWithDeviation& operator=(const RegressionUnitTestWithDeviation&);
02255   };
02256 } // namespace UnitTesting
02257 #endif // REGRESSIONUNITTESTWITHDEVIATION_H
02258 // including file tmvaut/RegressionUnitTestWithDeviation.cxx
02259 
02260 #include "TFile.h"
02261 #include "TSystem.h"
02262 #include "TMVA/MethodBase.h"
02263 #include "TMVA/Reader.h"
02264 #include <cstdlib>
02265 
02266 using namespace std;
02267 using namespace UnitTesting;
02268 using namespace TMVA;
02269 
02270 RegressionUnitTestWithDeviation::RegressionUnitTestWithDeviation(const Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
02271                                                                  double lowFullLimit, double upFullLimit,double low90PercentLimit, double up90PercentLimit,
02272                                                                  const std::string & /* xname */ ,const std::string & /* filename */ , std::ostream* /* sptr */) 
02273    : UnitTest(string("Regression_")+(string)methodTitle, __FILE__), _methodType(theMethod) , _methodTitle(methodTitle), _methodOption(theOption), 
02274                                                                                                                                   _lowerFullDeviationLimit(lowFullLimit),  _upperFullDeviationLimit(upFullLimit), _lower90PercentDeviationLimit(low90PercentLimit), _upper90PercentDeviationLimit(up90PercentLimit)
02275 {
02276 }
02277 
02278 
02279 RegressionUnitTestWithDeviation::~RegressionUnitTestWithDeviation()
02280 {
02281 }
02282 
02283 bool RegressionUnitTestWithDeviation::DeviationWithinLimits()
02284 {
02285    return (_the90PercentDeviation<= _upper90PercentDeviationLimit) && (_the90PercentDeviation >= _lower90PercentDeviationLimit) && (_theFullDeviation <= _upperFullDeviationLimit) && (_theFullDeviation>= _lowerFullDeviationLimit);
02286 }
02287 
02288 void RegressionUnitTestWithDeviation::run()
02289 {
02290    // FIXME:: create _this_ file or rather somewhere else?
02291    TString outfileName( "weights/TMVARegUT.root" );
02292    TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
02293 
02294    // FIXME:: if file can't be created do something more?
02295    if(!outputFile)
02296       return;
02297 
02298    // FIXME:: make the factory option mutable?
02299    // absolute silence options:
02300    string factoryOptions( "!V:Silent:Transformations=I;D;P;G,D:AnalysisType=Regression:!Color:!DrawProgressBar" );
02301 
02302    Factory* factory = new Factory( "TMVARegressionUnitTesting", outputFile, factoryOptions );
02303 
02304    factory->AddVariable( "var1", "Variable 1", "units", 'F' ); // fix me
02305    factory->AddVariable( "var2", "Variable 2", "units", 'F' ); // fix me
02306    TString _targetname="fvalue";
02307    factory->AddTarget  ( _targetname.Data() ); // fix me _targetname.Data()
02308 
02309    TFile* input(0);
02310    FileStat_t stat;
02311 
02312    // FIXME:: give the filename of the sample somewhere else?
02313    TString fname = "../tmva/test/tmva_reg_example.root";
02314    if(!gSystem->GetPathInfo(fname,stat)) {
02315       input = TFile::Open( fname );
02316    } else if(!gSystem->GetPathInfo("../"+fname,stat)) {
02317       input = TFile::Open( "../"+fname );
02318    } else {
02319       input = TFile::Open( "http://root.cern.ch/files/tmva_reg_example.root" );
02320    }
02321    if (input == NULL) {
02322       cerr << "broken/inaccessible input file" << endl;
02323    }
02324 
02325    TTree *regTree = (TTree*)input->Get("TreeR");
02326 
02327    Double_t regWeight  = 1.0;
02328    factory->AddRegressionTree( regTree, regWeight );
02329    factory->SetWeightExpression( "var1", "Regression" );
02330    TCut mycut = ""; // for example: TCut mycut = "abs(var1)<0.5 && abs(var2-0.5)<1";
02331 
02332    factory->PrepareTrainingAndTestTree( mycut, "nTrain_Regression=500:nTest_Regression=500:SplitMode=Random:NormMode=NumEvents:!V" );
02333 
02334    factory->BookMethod(_methodType, _methodTitle, _methodOption);
02335 
02336    factory->TrainAllMethods();
02337    factory->TestAllMethods();
02338    factory->EvaluateAllMethods();
02339 
02340    _theMethod = dynamic_cast<TMVA::MethodBase*> (factory->GetMethod(_methodTitle));
02341 
02342    _theMethod->GetRegressionDeviation(0,TMVA::Types::kTesting, _theFullDeviation,_the90PercentDeviation);
02343    if (DeviationWithinLimits()){
02344 #ifdef COUTDEBUG
02345    cout << "deviation, dev90= " << _theFullDeviation << ", " <<  _the90PercentDeviation << endl;
02346    cout << "Full limits " << _lowerFullDeviationLimit      << " "
02347         << _upperFullDeviationLimit
02348         << ", 90% limits "  << _lower90PercentDeviationLimit << " "
02349         << _upper90PercentDeviationLimit << endl;
02350 #endif
02351    }
02352    else {      
02353       cout << "Failure "<<_methodTitle<<", deviation, dev90= " << _theFullDeviation << ", " <<  _the90PercentDeviation << endl;
02354    cout << "Full limits " << _lowerFullDeviationLimit      << " "
02355         << _upperFullDeviationLimit
02356         << ", 90% limits "  << _lower90PercentDeviationLimit << " "
02357         << _upper90PercentDeviationLimit << endl;
02358    }
02359    test_(DeviationWithinLimits());
02360    
02361    outputFile->Close();
02362    delete factory;
02363 
02364    // reader tests
02365 
02366    // setup test tree access
02367    TFile* testFile = new TFile("weights/TMVARegUT.root"); // fix me hardcoded file name
02368    TTree* testTree = (TTree*)(testFile->Get("TestTree"));
02369    const int nTest=3; // 3 reader usages
02370    float testTarget,readerVal=0.;
02371 
02372    vector<TString>* _VariableNames = new std::vector<TString>(0); // fix me, move to constructor
02373    _VariableNames->push_back("var1");
02374    _VariableNames->push_back("var2");
02375 
02376    vector<float>  testvar(_VariableNames->size());
02377    vector<float>  dummy(_VariableNames->size());
02378    vector<float>  dummy2(_VariableNames->size());
02379    vector<float>  testvarFloat(_VariableNames->size());
02380    vector<double> testvarDouble(_VariableNames->size());
02381    for (UInt_t i=0;i<_VariableNames->size();i++)
02382       testTree->SetBranchAddress(_VariableNames->at(i),&testvar[i]);
02383    testTree->SetBranchAddress(_methodTitle.Data(),&testTarget);
02384 
02385    TString readerName = _methodTitle + TString(" method");
02386    TString dir    = "weights/TMVARegressionUnitTesting_";
02387    TString weightfile=dir+_methodTitle+".weights.xml";
02388    double diff, maxdiff = 0., sumdiff=0., previousVal=0.;
02389    int stuckCount=0, nevt= TMath::Min((int) testTree->GetEntries(),50);
02390 
02391    std::vector< TMVA::Reader* > reader(nTest);
02392    for (int iTest=0;iTest<nTest;iTest++){
02393       //std::cout << "iTest="<<iTest<<std::endl;
02394       reader[iTest] = new TMVA::Reader( "!Color:Silent" );
02395       for (UInt_t i=0;i<_VariableNames->size();i++)
02396          reader[iTest]->AddVariable( _VariableNames->at(i),&testvar[i]);
02397 
02398       reader[iTest] ->BookMVA( readerName, weightfile) ;
02399 
02400       // run the reader application and compare to test tree
02401       for (Long64_t ievt=0;ievt<nevt;ievt++) {
02402          testTree->GetEntry(ievt);
02403          for (UInt_t i=0;i<_VariableNames->size();i++){
02404             testvarDouble[i]= testvar[i];
02405             testvarFloat[i]= testvar[i];
02406          }
02407 
02408          if (iTest==0){ readerVal=(reader[iTest]->EvaluateRegression( readerName))[0];}
02409          else if (iTest==1){ readerVal=(reader[iTest]->EvaluateRegression( readerName)).at(0);}
02410          else if (iTest==2){ readerVal=reader[iTest]->EvaluateRegression( 0, readerName);}
02411          else {
02412             std::cout << "ERROR, undefined iTest value "<<iTest<<endl;
02413             exit(1);
02414          }
02415 
02416          diff = TMath::Abs(readerVal-testTarget);
02417          maxdiff = diff > maxdiff ? diff : maxdiff;
02418          sumdiff += diff;
02419          if (ievt>0 && iTest ==0 && TMath::Abs(readerVal-previousVal)<1.e-6) stuckCount++;
02420          //if (ievt<3) std::cout << "i="<<iTest<<", readerVal="<<readerVal<<" testTarget"<<testTarget<<" diff="<<diff<<std::endl;
02421 
02422          if (iTest ==0 ) previousVal=readerVal;
02423       }
02424 
02425    }
02426 
02427    sumdiff=sumdiff/nevt;
02428 
02429    test_(maxdiff <1.e-4);
02430    test_(sumdiff <1.e-5);
02431    test_(stuckCount<nevt/10);
02432 
02433    testFile->Close();
02434 
02435    for (int i=0;i<nTest;i++) delete reader[i];
02436 
02437 #ifdef COUTDEBUG
02438    cout << "end of reader test maxdiff="<<maxdiff<<", sumdiff="<<sumdiff<<" stuckcount="<<stuckCount<<endl;
02439 #endif
02440 }
02441 // including file tmvaut/MethodUnitTestWithComplexData.h
02442 #ifndef METHODUNITTESTCOMPLEXDATA_H
02443 #define METHODUNITTESTCOMPLEXDATA_H
02444 
02445 // Author: Eckhard von Toerne, uses Christoph Rosemann's MethodUnitTest as example  Nov 2010
02446 // TMVA unit tests
02447 //
02448 // this class acts as interface to create, train and evaluate the method 
02449 // specified in the constructor
02450 // as additional argument the limits of the ROC integral can be given 
02451 // to determine the performance
02452 
02453 #include <string>
02454 #include <iostream>
02455 #include <cassert>
02456 
02457 #include "TTree.h"
02458 #include "TString.h"
02459 
02460 #include "TMVA/Factory.h"
02461 #include "TMVA/MethodBase.h"
02462 #include "TMVA/Types.h"
02463 
02464 
02465 
02466 namespace UnitTesting
02467 {
02468    class MethodUnitTestWithComplexData : public UnitTest
02469    {
02470    public:
02471       MethodUnitTestWithComplexData(const TString& treestring, const TString& preparestring, 
02472                                     const TMVA::Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
02473                                     double lowLimit = 0., double upLimit = 1.,
02474                                     const std::string & name="", const std::string & filename="", std::ostream* osptr = &std::cout);
02475       virtual ~MethodUnitTestWithComplexData();
02476       
02477       virtual void run();
02478       
02479    protected:
02480       TTree*  theTree;
02481       
02482    private:
02483       TMVA::Factory* _factory; 
02484       TMVA::MethodBase* _theMethod;
02485       TMVA::Types::EMVA _methodType;
02486       TString _treeString;
02487       TString _prepareString;
02488       TString _methodTitle;
02489       TString _methodOption;
02490       
02491       double _upROCLimit;
02492       double _lowROCLimit;
02493       double _ROCValue;
02494       
02495       bool ROCIntegralWithinInterval();
02496       bool create_data(const char* filename, int nmax=20000);
02497       // disallow copy constructor and assignment
02498       MethodUnitTestWithComplexData(const MethodUnitTestWithComplexData&);
02499       MethodUnitTestWithComplexData& operator=(const MethodUnitTestWithComplexData&);
02500    };
02501 } // namespace UnitTesting
02502 #endif // METHODUNITTESTCOMPLEXDATA_H
02503 // including file tmvaut/MethodUnitTestWithComplexData.cxx
02504 
02505 #include "TFile.h"
02506 #include "TMVA/MethodBase.h"
02507 
02508 using namespace std;
02509 using namespace UnitTesting;
02510 using namespace TMVA;
02511 
02512 MethodUnitTestWithComplexData::MethodUnitTestWithComplexData(const TString& treestring, const TString& preparestring, const Types::EMVA& theMethod, const TString& methodTitle, const TString& theOption,
02513                                                                                                                 double lowLimit, double upLimit,
02514                                                              const std::string & /* xname */ ,const std::string & /* filename */ , std::ostream* /* sptr */) :
02515    UnitTest(string("ComplexData_")+(string)methodTitle+(string)treestring, __FILE__),  _methodType(theMethod) , _treeString(treestring), _prepareString(preparestring), _methodTitle(methodTitle), _methodOption(theOption), _upROCLimit(upLimit), _lowROCLimit(lowLimit)
02516 {
02517 }
02518 
02519 
02520 MethodUnitTestWithComplexData::~MethodUnitTestWithComplexData()
02521 {
02522 }
02523 
02524 bool MethodUnitTestWithComplexData::ROCIntegralWithinInterval()
02525 {
02526    return (_ROCValue <= _upROCLimit) && (_ROCValue >= _lowROCLimit);
02527 }
02528 
02529 void MethodUnitTestWithComplexData::run()
02530 {
02531         // FIXME:: create _this_ file or rather somewhere else?
02532   TString outfileName( "weights/TMVA.root" );
02533   TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
02534 
02535 // FIXME:: if file can't be created do something more?
02536   if(!outputFile)
02537     return;    
02538 
02539 // FIXME:: make the factory option mutable?
02540 // absolute silence options:
02541   string factoryOptions( "!V:Silent:Transformations=I;D;P;G,D:AnalysisType=Classification:!Color:!DrawProgressBar" );
02542 
02543   Factory* factory = new Factory( "TMVAUnitTesting", outputFile, factoryOptions );
02544   
02545   factory->AddVariable( "var0",  "Variable 0", 'F' );
02546   factory->AddVariable( "var1",  "Variable 1", 'F' );
02547   factory->AddVariable( "var2",  "Variable 2", 'F' );
02548   factory->AddVariable( "var3",  "Variable 3", 'F' );
02549   factory->AddSpectator( "is1", 'I' );
02550   factory->AddSpectator( "evtno", 'I' );
02551   
02552   TFile* input(0);
02553 // FIXME:: give the filename of the sample somewhere else?
02554   TString fname = "weights/tmva_complex_data.root"; 
02555   input = TFile::Open( fname );  
02556   if (input == NULL) create_data("weights/tmva_complex_data.root");
02557   input = TFile::Open( fname );  
02558   if (input == NULL) 
02559     {
02560       cerr << "broken/inaccessible input file" << endl;
02561     }
02562   
02563   TTree *sig1     = (TTree*)input->Get("TreeS1");
02564   TTree *sig2     = (TTree*)input->Get("TreeS2");
02565   TTree *sigfull  = (TTree*)input->Get("TreeSFull");
02566   TTree *bgd1     = (TTree*)input->Get("TreeB1");
02567   TTree *bgd2     = (TTree*)input->Get("TreeB2");
02568   TTree *bgdfull  = (TTree*)input->Get("TreeBFull");
02569   
02570   if (_treeString.Contains("sig1"))    factory->AddSignalTree(sig1);  
02571   if (_treeString.Contains("sig2"))    factory->AddSignalTree(sig2);  
02572   if (_treeString.Contains("sigfull")) factory->AddSignalTree(sigfull);
02573   if (_treeString.Contains("bgd1"))    factory->AddBackgroundTree(bgd1);  
02574   if (_treeString.Contains("bgd2"))    factory->AddBackgroundTree(bgd2);  
02575   if (_treeString.Contains("bgdfull")) factory->AddBackgroundTree(bgdfull);
02576 
02577   factory->SetSignalWeightExpression("weight");
02578   factory->SetBackgroundWeightExpression("weight");
02579   
02580   TCut mycuts = "";
02581   TCut mycutb = "";
02582   if (_prepareString=="") _prepareString = "nTrain_Signal=200:nTrain_Background=200:SplitMode=Random:NormMode=NumEvents:!V" ;
02583   factory->PrepareTrainingAndTestTree( mycuts, mycutb, _prepareString);
02584  
02585   factory->BookMethod(_methodType, _methodTitle, _methodOption);
02586 
02587   factory->TrainAllMethods();
02588   factory->TestAllMethods();
02589   factory->EvaluateAllMethods();
02590 
02591   _theMethod = dynamic_cast<TMVA::MethodBase*> (factory->GetMethod(_methodTitle));
02592 
02593   if (_methodType == TMVA::Types::kCuts) {
02594      // ToDo make class variable _theEffi
02595      Double_t err=0.;
02596      Double_t effi = _theMethod->GetEfficiency("Efficiency:0.1", Types::kTesting,err);
02597 #ifdef COUTDEBUG
02598      std::cout << "Cuts Signal effi at for Background effi of 0.1 = " << effi<<" low limit="<<_lowROCLimit<<" high limit="<<_upROCLimit<<std::endl;
02599 #endif
02600      test_(effi <= _upROCLimit && effi>=_lowROCLimit);
02601   }
02602   else {
02603      _ROCValue = _theMethod->GetROCIntegral();
02604 #ifdef COUTDEBUG
02605      std::cout << "ROC integral = "<<_ROCValue <<" low limit="<<_lowROCLimit<<" high limit="<<_upROCLimit<<std::endl;
02606 #endif     
02607      test_(ROCIntegralWithinInterval());
02608   }
02609   outputFile->Close();
02610   delete factory;
02611 }
02612 
02613 bool MethodUnitTestWithComplexData::create_data(const char* filename, int nmax)
02614 {
02615    TFile* dataFile = TFile::Open(filename,"RECREATE");
02616    int nvar = 4;
02617    int nsig = 0, nbgd=0;
02618    Float_t weight=1;
02619    Float_t xvar[100];
02620    int is1,evtno=0;
02621    // create signal and background trees
02622    TTree* treeS1 = new TTree( "TreeS1", "TreeS1", 1 );
02623    TTree* treeB1 = new TTree( "TreeB1", "TreeB1", 1 );
02624    TTree* treeS2 = new TTree( "TreeS2", "TreeS2", 1 );
02625    TTree* treeB2 = new TTree( "TreeB2", "TreeB2", 1 );
02626    TTree* treeSFull = new TTree( "TreeSFull", "TreeSFull", 1 );
02627    TTree* treeBFull = new TTree( "TreeBFull", "TreeBFull", 1 );
02628    for (Int_t ivar=0; ivar<nvar; ivar++) {
02629       treeS1->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02630       treeB1->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02631       treeS2->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02632       treeB2->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02633       treeBFull->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02634       treeSFull->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
02635    }
02636    treeS1->Branch("weight", &weight, "weight/F");
02637    treeB1->Branch("weight", &weight, "weight/F");
02638    treeS2->Branch("weight", &weight, "weight/F");
02639    treeB2->Branch("weight", &weight, "weight/F");
02640    treeSFull->Branch("weight", &weight, "weight/F");
02641    treeBFull->Branch("weight", &weight, "weight/F");
02642 
02643    treeS1->Branch("is1", &is1, "is1/I");
02644    treeB1->Branch("is1", &is1, "is1/I");
02645    treeS2->Branch("is1", &is1, "is1/I");
02646    treeB2->Branch("is1", &is1, "is1/I");
02647    treeSFull->Branch("is1", &is1, "is1/I");
02648    treeBFull->Branch("is1", &is1, "is1/I");
02649 
02650    treeS1->Branch("evtno", &evtno, "evtno/I");
02651    treeB1->Branch("evtno", &evtno, "evtno/I");
02652    treeS2->Branch("evtno", &evtno, "evtno/I");
02653    treeB2->Branch("evtno", &evtno, "evtno/I");
02654    treeSFull->Branch("evtno", &evtno, "evtno/I");
02655    treeBFull->Branch("evtno", &evtno, "evtno/I");
02656 
02657    TRandom R( 100 );
02658    do {
02659       for (Int_t ivar=0; ivar<nvar-1; ivar++) { xvar[ivar]=2.*R.Rndm()-1.;}
02660       Float_t xout = xvar[0]+xvar[1]+xvar[2]*xvar[1]-xvar[0]*xvar[1]*xvar[1];
02661       xvar[3] = xout + 4. *R.Rndm()-2.;
02662       bool is = (TMath::Abs(xout)<0.3);
02663       if (is) is1=1; else is1=0;
02664       bool isSignal;
02665       if (is) isSignal = (R.Rndm()<0.5);
02666       else isSignal =  (xout>0);
02667       //if (nsig<10) cout << "xout = " << xout<< (isSignal? " signal": " bbg")  << endl;
02668       if (isSignal) {
02669          treeSFull->Fill();
02670          if (is) treeS1->Fill();
02671          else treeS2->Fill();
02672          nsig++;
02673          evtno++;
02674       }
02675       else {
02676          treeBFull->Fill();
02677          if (is) treeB1->Fill();
02678          else treeB2->Fill();
02679          nbgd++;
02680          evtno++;
02681       }
02682    } while ( nsig < nmax || nbgd < nmax);
02683 
02684    dataFile->Write();
02685    dataFile->Close();
02686    return true;
02687 }
02688 // including file stressTMVA.cxx
02689 // Authors: Christoph Rosemann, Eckhard von Toerne   July 2010
02690 // TMVA unit tests
02691 
02692 #include "Riostream.h"
02693 #include "TSystem.h"
02694 #include "TROOT.h"
02695 #include "TBenchmark.h"
02696 #include "TApplication.h"
02697 
02698 
02699 
02700 
02701 
02702 
02703 
02704 
02705 
02706 
02707 
02708 #include "TMVA/Types.h"
02709 
02710 using namespace UnitTesting;
02711 using namespace std;
02712 
02713 void addClassificationTests( UnitTestSuite& TMVA_test, bool full=true)
02714 {
02715    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCuts, "CutsGA",
02716                                                       "H:!V:FitMethod=GA:CutRangeMin[0]=-10:CutRangeMax[0]=10:VarProp[1]=FMax:EffSel:Steps=20:Cycles=4:PopSize=300:SC_steps=10:SC_rate=5:SC_factor=0.95" , 0.4, 0.98) );
02717 
02718    if (full){
02719       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCuts, "Cuts",
02720                                                          "!H:!V:FitMethod=MC:EffSel:SampleSize=20000:VarProp=FSmart", 0.4, 0.98) );
02721       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCuts, "CutsD",
02722                                                          "!H:!V:FitMethod=MC:EffSel:SampleSize=20000:VarProp=FSmart:VarTransform=Decorrelate" , 0.6, 0.98) );
02723       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCuts, "CutsPCA", "!H:!V:FitMethod=MC:EffSel:SampleSize=20000:VarProp=FSmart:VarTransform=PCA" , 0.4, 0.98) );
02724       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCuts, "CutsSA",
02725                                                          "!H:!V:FitMethod=SA:EffSel:MaxCalls=15000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" , 0.4, 0.98) );
02726       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLikelihood, "Likelihood",
02727                                                          "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" , 0.7, 0.98) );
02728    }
02729    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLikelihood, "LikelihoodD",
02730                                                       "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" , 0.88, 0.98) );
02731    if (full){
02732       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLikelihood, "LikelihoodPCA",
02733                                                          "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" , 0.88, 0.98) );
02734       TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLikelihood, "LikelihoodMIX",
02735                                                          "!H:!V:!TransformOutput:PDFInterpolSig[0]=KDE:PDFInterpolBkg[0]=KDE:PDFInterpolSig[1]=KDE:PDFInterpolBkg[1]=KDE:PDFInterpolSig[2]=Spline2:PDFInterpolBkg[2]=Spline2:PDFInterpolSig[3]=Spline2:PDFInterpolBkg[3]=Spline2:KDEtype=Gauss:KDEiter=Nonadaptive:KDEborder=None:NAvEvtPerBin=50" , 0.7, 0.98) );
02736    }
02737    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kPDERS, "PDERS",
02738                                                       "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.1:NEventsMin=100:NEventsMax=600" , 0.8, 0.98) );
02739    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kPDERS, "PDERSkNN",
02740                                                                 "!H:!V:VolumeRangeMode=kNN:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" , 0.8, 0.98) );
02741    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kPDERS, "PDERSD",
02742                                                                 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=100:NEventsMax=600:VarTransform=Decorrelate" , 0.88, 0.98) );
02743    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kPDERS, "PDERSPCA",
02744                                                                 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=100:NEventsMax=600:VarTransform=PCA" , 0.88, 0.98) );
02745    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kPDEFoam, "PDEFoam",
02746                                                       "H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0333:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" , 0.8, 0.98) );
02747    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kKNN, "KNN",
02748                                                       "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" , 0.8, 0.98) );
02749    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kHMatrix, "HMatrix", "!H:!V:VarTransform=G" , 0.88, 0.98) );
02750    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFisher, "Fisher",
02751                                                       "H:!V:Fisher:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=60:NsmoothMVAPdf=10" , 0.88, 0.98) );
02752    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFisher, "FisherG", "H:!V:VarTransform=Gauss" , 0.88, 0.98) );
02753    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFisher, "BoostedFisher",
02754                                                       "H:!V:Boost_Num=10:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2", 0.88, 0.98) );
02755    TMVA_test.addTest(new MethodUnitTestWithROCLimits(TMVA::Types::kLD, "LD","!H:!V", 0.88, 0.98) );
02756    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLD, "LD2", "H:!V:VarTransform=None" , 0.88, 0.98) );
02757    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kLD, "LDN", "H:!V:VarTransform=N" , 0.88, 0.98) );
02758 
02759    TString baseFDAstring="!H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):";
02760    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFDA, "FDA_MC",
02761                                                                 baseFDAstring+"FitMethod=MC:SampleSize=5000:Sigma=0.1" , 0.8, 0.98) );
02762    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFDA, "FDA_GA",
02763                                                                 baseFDAstring+"FitMethod=GA:PopSize=100:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" , 0.88, 0.98) );
02764    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kFDA, "FDA_SA",
02765                                                                 baseFDAstring+"FitMethod=SA:MaxCalls=5000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" , 0.88, 0.98) );
02766    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=200:HiddenLayers=N+5:TestRate=5:!UseRegulator" , 0.88, 0.98) );
02767    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=200:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" , 0.88, 0.98) );
02768    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=200:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" , 0.88, 0.98) ); // BFGS training with bayesian regulators
02769    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kCFMlpANN, "CFMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N"  , 0.7, 0.98) ); // n_cycles:#nodes:#nodes:...
02770    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kTMlpANN, "TMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3"  , 0.7, 0.98) ); // n_cycles:#nodes:#nodes:...
02771    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" , 0.88, 0.98) );
02772    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kBDT, "BDTG",
02773                                                       "!H:!V:NTrees=400:BoostType=Grad:Shrinkage=0.30:UseBaggedGrad:GradBaggingFraction=0.6:SeparationType=GiniIndex:nCuts=20:NNodesMax=7" , 0.88, 0.98) );
02774    TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kBDT, "BDT",
02775                                                       "!H:!V:NTrees=400:nEventsMin=100:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=10:PruneMethod=NoPruning" , 0.88, 0.98) );
02776    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kBDT, "BDTB",
02777                                                                 "!H:!V:NTrees=400:nEventsMin=100:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" , 0.8, 0.98) );
02778    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kBDT, "BDTD",
02779                                                                 "!H:!V:NTrees=400:nEventsMin=200:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=10:PruneMethod=NoPruning:VarTransform=Decorrelate" , 0.88, 0.98) );
02780    if (full) TMVA_test.addTest(new MethodUnitTestWithROCLimits( TMVA::Types::kRuleFit, "RuleFit",
02781                                                                 "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.0:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" , 0.88, 0.98) );
02782 }
02783 
02784 void addRegressionTests( UnitTestSuite& TMVA_test, bool full=true)
02785 {
02786    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kLD, "LD", "!H:!V:VarTransform=None",      15., 25.,    10., 20. ));
02787    //                       full low/high , 90 low/high
02788    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kMLP, "MLPBFGSN", "!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=300:HiddenLayers=N+20:TestRate=6:TrainingMethod=BFGS:Sampling=0.3:SamplingEpoch=0.8:ConvergenceImprove=1e-7:ConvergenceTests=15:!UseRegulator:VarTransform=N" , 0.4, 0.8, 0.2, 0.5 ));
02789    if (full) TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kBDT, "BDTG","!H:!V:NTrees=1000::BoostType=Grad:Shrinkage=0.3:!UseBaggedGrad:SeparationType=GiniIndex:nCuts=20:nEventsMin=20:NNodesMax=7" ,  5., 8., 3., 5. ));
02790    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kBDT, "BDTG2","!H:!V:NTrees=2000::BoostType=Grad:Shrinkage=0.1:UseBaggedGrad:GradBaggingFraction=0.5:nCuts=20:MaxDepth=3:NNodesMax=15" ,  2., 5., 1., 3. ));
02791 
02792    if (!full) return;
02793 
02794    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kPDERS, "PDERS", "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=1.0:NEventsMin=10:NEventsMax=60:VarTransform=None", 10., 15., 5., 10. ));
02795    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kPDERS, "PDERSkNN", "!H:!V:VolumeRangeMode=kNN:KernelEstimator=Gauss:GaussSigma=1.0:NEventsMin=10:NEventsMax=60" , 10., 15., 5., 10. ));
02796    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kPDEFoam, "PDEFoam", "!H:!V:MultiTargetRegression=F:TargetSelection=Mpv:TailCut=0.001:VolFrac=0.0333:nActiveCells=500:nSampl=5000:nBin=7:Compress=T:Kernel=Gauss:Nmin=10:VarTransform=None", 10., 15., 4., 6. ));
02797    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kKNN, "KNN",  "nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" , 10., 15., 4., 6. ));
02798    TString baseRegFDAstring="!H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x0*x1+(4)*x0*x0+(5)*x1*x1:ParRanges=(-1,1);(-2,2);(-2,2);(-2,2);(-2,2);(-2,2):";
02799    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kFDA, "FDA_MC", baseRegFDAstring+"FitMethod=MC:SampleSize=1000:Sigma=0.1:VarTransform=N", 15., 25., 10., 20. ));
02800    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kFDA, "FDA_GA", baseRegFDAstring+"FitMethod=GA:PopSize=100:Cycles=3:Steps=30:Trim=True:SaveBestGen=1:VarTransform=Norm",  0.5, 1.0, 0.5 , 0.8 ));
02801    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kFDA, "FDA_MT", baseRegFDAstring+"FitMethod=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" , 100., 250., 100., 200. ));
02802    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kFDA, "FDA_GAMT",baseRegFDAstring+"FitMethod=GA:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:UseImprove:UseMinos:SetBatch:Cycles=1:PopSize=20:Steps=5:Trim" ,  100., 250., 100., 220. ));
02803    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kMLP, "MLPBFGS", "!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=300:HiddenLayers=N+20:TestRate=6:TrainingMethod=BFGS:Sampling=0.3:SamplingEpoch=0.8:ConvergenceImprove=1e-6:ConvergenceTests=15:!UseRegulator" , 0.4, 0.8, 0.2, 0.5 ));
02804    // this does not work satisfactory TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kMLP, "MLP", "!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=300:HiddenLayers=N+20:TestRate=6:TrainingMethod=BP:Sampling=0.3:SamplingEpoch=0.8:ConvergenceImprove=1e-6:ConvergenceTests=15:!UseRegulator" , 0.4, 0.8, 0.2, 0.5 ));
02805    // SVM regression does not work TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" , 0., 1., 0., 1. ));
02806    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kBDT, "BDT","!H:!V",  8., 15., 5., 10. ));
02807    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kBDT, "BDTN","!H:!V:VarTransform=N",  8., 15., 5., 10. ));
02808    TMVA_test.addTest(new RegressionUnitTestWithDeviation( TMVA::Types::kBDT, "BDT2","!H:!V:NTrees=500:nEventsMin=20:BoostType=AdaBoostR2:SeparationType=GiniIndex:nCuts=20:PruneMethod=CostComplexity:PruneStrength=3", 15., 20., 10., 20. ));
02809 }
02810 
02811 void addDataInputTests( UnitTestSuite& TMVA_test, bool full=true)
02812 {
02813    if (!full) return;
02814 
02815    TString lhstring = "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate";
02816 
02817    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_bgd1"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=random:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.48, 0.52) );
02818    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_bgd1"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=block:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.48, 0.52) );
02819    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_bgd1"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=alternate:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.48, 0.52) );
02820 
02821    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sigfull_bgdfull"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=Random:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02822    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sigfull_bgdfull"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=alternate:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02823    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sigfull_bgdfull"), TString("nTrain_Signal=500:nTrain_Background=500:SplitMode=block:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02824 
02825    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("nTrain_Signal=500:nTrain_Background=500:nTest_Signal=500:nTest_Background=500:SplitMode=random:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02826    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("nTrain_Signal=500:nTrain_Background=500:nTest_Signal=500:nTest_Background=500:SplitMode=block:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.2, 0.45) );
02827 
02828    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("nTrain_Signal=500:nTrain_Background=500:nTest_Signal=500:nTest_Background=500:SplitMode=alternate:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.45, 0.52) );
02829 
02830    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("SplitMode=random:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02831    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("SplitMode=alternate:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.95) );
02832    TMVA_test.addTest(new MethodUnitTestWithComplexData(TString("sig1_sig2_bgd1_bgd2"), TString("SplitMode=block:NormMode=NumEvents:!V"), TMVA::Types::kLikelihood, "LikelihoodD", lhstring , 0.9, 0.99) );
02833 }
02834 
02835 void addComplexClassificationTests( UnitTestSuite& TMVA_test, bool full=true )
02836 {
02837    if (!full) return;
02838    TString trees="sigfull_bgdfull";
02839    TString prep="nTrain_Signal=2000:nTrain_Background=2000:nTest_Signal=1000:nTest_Background=1000:!V";
02840    // complex data tests Fisher for comparison
02841    TMVA_test.addTest(new MethodUnitTestWithComplexData( trees, prep,TMVA::Types::kFisher, "Fisher", "H:!V:VarTransform=Gauss", 0.93, 0.945) );
02842    // complex data tests with MLP
02843    TMVA_test.addTest(new MethodUnitTestWithComplexData(trees, prep, TMVA::Types::kMLP, "MLP", "H:!V:RandomSeed=9:NeuronType=tanh:VarTransform=N:NCycles=50:HiddenLayers=N+10:TestRate=5:TrainingMethod=BFGS:!UseRegulator" , 0.955, 0.975) );
02844    TMVA_test.addTest(new MethodUnitTestWithComplexData(trees, prep, TMVA::Types::kMLP, "MLP", "H:!V:RandomSeed=9:NeuronType=tanh:VarTransform=N:NCycles=50:HiddenLayers=N+10:TestRate=5:TrainingMethod=BP:!UseRegulator" , 0.955, 0.975) );
02845    // BDT
02846    TMVA_test.addTest(new MethodUnitTestWithComplexData(trees, prep, TMVA::Types::kBDT, "BDTG8_50", "!H:!V:NTrees=50:BoostType=Grad:Shrinkage=0.30:UseBaggedGrad:GradBaggingFraction=0.6:nCuts=20:NNodesMax=8:SeparationType=GiniIndex" , 0.955, 0.975) );
02847    // SVM
02848    TMVA_test.addTest(new MethodUnitTestWithComplexData(trees, prep, TMVA::Types::kSVM, "SVM", "Gamma=0.4:Tol=0.001" , 0.955, 0.975) );
02849 }
02850 
02851 
02852 int main(int argc, char **argv)
02853 {
02854 
02855    TApplication theApp("App", &argc, argv);
02856    gBenchmark = new TBenchmark();
02857    gBenchmark->Start("stress");
02858    bool full=false;
02859 #ifdef FULL
02860    full=true;
02861    //std::cout << running longer tests<<std::endl;
02862 #endif
02863 
02864    UnitTestSuite TMVA_test("TMVA unit testing");
02865 
02866    TMVA_test.intro();
02867 
02868    TMVA_test.addTest(new utEvent);
02869    TMVA_test.addTest(new utVariableInfo);
02870    TMVA_test.addTest(new utDataSetInfo);
02871    TMVA_test.addTest(new utDataSet);
02872    TMVA_test.addTest(new utFactory);
02873    TMVA_test.addTest(new utReader);
02874 
02875    addClassificationTests(TMVA_test, full);
02876    addRegressionTests(TMVA_test, full);
02877    addDataInputTests(TMVA_test, full);
02878    addComplexClassificationTests(TMVA_test, full);
02879 
02880    // run all
02881    TMVA_test.run();
02882 
02883 #ifdef COUTDEBUG
02884    long int nFail = TMVA_test.report();
02885    cout << "Total number of failures: " << nFail << endl;
02886    cout << "************************************************************************************************" << endl;
02887 #else
02888    TMVA_test.report();
02889 #endif
02890 
02891 #ifndef NOCLEANUP
02892    //FileStat_t stat;
02893    //if(!gSystem->GetPathInfo("./weights",stat)) {
02894 #ifdef WIN32
02895    gSystem->Exec("erase /f /q weights\\*.*");
02896 #else
02897    gSystem->Exec("rm -rf weights/*");
02898 #endif
02899 #endif
02900    gBenchmark->Stop("stress");
02901    Bool_t UNIX = strcmp(gSystem->GetName(), "Unix") == 0;
02902    printf("******************************************************************\n");
02903    if (UNIX) {
02904       TString sp = gSystem->GetFromPipe("uname -a");
02905       sp.Resize(60);
02906       printf("*  SYS: %s\n",sp.Data());
02907       if (strstr(gSystem->GetBuildNode(),"Linux")) {
02908          sp = gSystem->GetFromPipe("lsb_release -d -s");
02909          printf("*  SYS: %s\n",sp.Data());
02910       }
02911       if (strstr(gSystem->GetBuildNode(),"Darwin")) {
02912          sp  = gSystem->GetFromPipe("sw_vers -productVersion");
02913          sp += " Mac OS X ";
02914          printf("*  SYS: %s\n",sp.Data());
02915       }
02916    } else {
02917       const char *os = gSystem->Getenv("OS");
02918       if (!os) printf("*  SYS: Windows 95\n");
02919       else     printf("*  SYS: %s %s \n",os,gSystem->Getenv("PROCESSOR_IDENTIFIER"));
02920    }
02921 
02922    //printf("******************************************************************\n");
02923    //gBenchmark->Print("stress");
02924    Float_t ct = gBenchmark->GetCpuTime("stress");
02925    printf("******************************************************************\n");
02926    printf("*  CPUTIME   =%6.1f   *  Root%-8s  %d/%d\n",ct       ,gROOT->GetVersion(),gROOT->GetVersionDate(),gROOT->GetVersionTime());
02927    printf("******************************************************************\n");
02928 }

Generated on Tue Jul 5 15:15:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1