DataInputHandler.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: DataInputHandler.cxx 31458 2009-11-30 13:58:20Z stelzer $
00002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : DataInputHandler                                                      *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation (see header for description)                               *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
00016  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00017  *                                                                                *
00018  * Copyright (c) 2006:                                                            *
00019  *      CERN, Switzerland                                                         *
00020  *      MPI-K Heidelberg, Germany                                                 *
00021  *                                                                                *
00022  * Redistribution and use in source and binary forms, with or without             *
00023  * modification, are permitted according to the terms listed in LICENSE           *
00024  * (http://tmva.sourceforge.net/LICENSE)                                          *
00025  **********************************************************************************/
00026 
00027 #include <vector>
00028 #include <iostream>
00029 
00030 #include "TMVA/DataInputHandler.h"
00031 #include "TMVA/MsgLogger.h"
00032 #include "TEventList.h"
00033 #include "TCut.h"
00034 #include "TFile.h"
00035 #include "TROOT.h"
00036 
00037 #ifndef ROOT_TMVA_Configurable
00038 #include "TMVA/Configurable.h"
00039 #endif
00040 
00041 //_______________________________________________________________________
00042 TMVA::DataInputHandler::DataInputHandler() 
00043    : fLogger( new MsgLogger("DataInputHandler", kINFO) )
00044 {
00045    // constructor
00046    fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
00047 }
00048 
00049 //_______________________________________________________________________
00050 TMVA::DataInputHandler::~DataInputHandler() 
00051 {
00052    // destructor
00053    delete fLogger;
00054 }
00055 
00056 
00057 //_______________________________________________________________________
00058 void TMVA::DataInputHandler::AddTree( const TString& fn, 
00059                                       const TString& className, 
00060                                       Double_t weight, 
00061                                       const TCut& cut, 
00062                                       Types::ETreeType tt  ) 
00063 {
00064    // add a signal tree to the dataset to be used as input
00065    TTree * tr = ReadInputTree(fn);
00066    tr->SetName( TString("Tree")+className );
00067    AddTree( tr, className, weight, cut, tt );
00068 }
00069 
00070 
00071 //_______________________________________________________________________
00072 void TMVA::DataInputHandler::AddTree( TTree* tree, 
00073                                       const TString& className, 
00074                                       Double_t weight, 
00075                                       const TCut& cut, 
00076                                       Types::ETreeType tt ) 
00077 {
00078    if (!tree) Log() << kFATAL << "Zero pointer for tree of class " << className.Data() << Endl;
00079    if (tree->GetEntries()==0) Log() << kFATAL << "Encountered empty TTree or TChain of class " << className.Data() << Endl;
00080    if (fInputTrees[className.Data()].size() == 0) {
00081       // on the first tree (of the class) check if explicit treetype is given
00082       fExplicitTrainTest[className.Data()] = ( tt != Types::kMaxTreeType);
00083    } 
00084    else {
00085       // if the first tree has a specific type, all later tree's must also have one
00086       if (fExplicitTrainTest[className.Data()] != (tt!=Types::kMaxTreeType)) {
00087          if (tt==Types::kMaxTreeType)
00088             Log() << kFATAL << "For the tree " << tree->GetName() << " of class " << className.Data()
00089                   << " you did "<< (tt==Types::kMaxTreeType?"not ":"") << "specify a type,"
00090                   << " while you did "<< (tt==Types::kMaxTreeType?"":"not ") << "for the first tree " 
00091                   << fInputTrees[className.Data()][0].GetTree()->GetName() << " of class " << className.Data()
00092                   << Endl;
00093       }
00094    }
00095    if (cut.GetTitle()[0] != 0) {
00096       fInputTrees[className.Data()].push_back(TreeInfo(tree->CopyTree(cut.GetTitle()), className, weight, tt ));
00097    } 
00098    else {
00099       fInputTrees[className.Data()].push_back(TreeInfo(tree, className, weight, tt ));
00100    }
00101 }
00102 
00103 //_______________________________________________________________________
00104 void TMVA::DataInputHandler::AddSignalTree( TTree* tr, Double_t weight, Types::ETreeType tt ) 
00105 {
00106    AddTree( tr, "Signal", weight, "", tt );
00107 }
00108 
00109 //_______________________________________________________________________
00110 void TMVA::DataInputHandler::AddBackgroundTree( TTree* tr, Double_t weight, Types::ETreeType tt )
00111 {
00112    AddTree( tr, "Background", weight, "", tt );
00113 }
00114 
00115 //_______________________________________________________________________
00116 void TMVA::DataInputHandler::AddSignalTree( const TString& fn, Double_t weight, Types::ETreeType tt ) 
00117 {
00118    // add a signal tree to the dataset to be used as input
00119    TTree * tr = ReadInputTree(fn);
00120    tr->SetName("TreeS");
00121    AddTree( tr, "Signal", weight, "", tt );
00122 }
00123 
00124 //_______________________________________________________________________
00125 void TMVA::DataInputHandler::AddBackgroundTree( const TString& fn, Double_t weight, Types::ETreeType tt )
00126 {
00127    // add a background tree to the dataset to be used as input
00128    TTree * tr = ReadInputTree(fn);
00129    tr->SetName("TreeB");
00130    AddTree( tr, "Background", weight, "", tt );
00131 }
00132 
00133 //_______________________________________________________________________
00134 TTree* TMVA::DataInputHandler::ReadInputTree( const TString& dataFile )
00135 {
00136    // create trees from these ascii files
00137    TTree* tr = new TTree( "tmp", dataFile );
00138   
00139    ifstream in(dataFile);
00140    if (!in.good())
00141       Log() << kFATAL << "Could not open file: " << dataFile << Endl;
00142    in.close();
00143 
00144    tr->ReadFile( dataFile );
00145   
00146    return tr;
00147 }
00148 
00149 //_______________________________________________________________________
00150 void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
00151 {
00152    // define the input trees for signal and background from single input tree,
00153    // containing both signal and background events distinguished by the type 
00154    // identifiers: SigCut and BgCut
00155    if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
00156 
00157    AddTree( inputTree, "Signal",     1.0, SigCut );
00158    AddTree( inputTree, "Background", 1.0, BgCut  );
00159 }
00160 
00161 
00162 //_______________________________________________________________________
00163 void TMVA::DataInputHandler::ClearTreeList( const TString& className ) { 
00164    try{
00165       fInputTrees.find(className)->second.clear();
00166    }
00167    catch(int) {
00168       Log() << kINFO << "   Clear treelist for class " << className << " failed, since class does not exist." << Endl;
00169    }
00170 }
00171 
00172 
00173 //_______________________________________________________________________
00174 std::vector< TString >* TMVA::DataInputHandler::GetClassList() const { 
00175    std::vector< TString >* ret = new std::vector< TString >();
00176    for( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++ ){
00177       ret->push_back( it->first );
00178    }
00179    return ret;
00180 }
00181 
00182 
00183 //_______________________________________________________________________
00184 UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const 
00185 {
00186    // return number of entries in tree
00187    UInt_t entries = 0;
00188    std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
00189    for(;tiIt != tiV.end(); tiIt++)
00190       entries += tiIt->GetEntries();
00191    return entries;
00192 }
00193 
00194 //_______________________________________________________________________
00195 UInt_t TMVA::DataInputHandler::GetEntries() const 
00196 {
00197    // return number of entries in tree
00198    UInt_t number = 0;
00199    for( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++ ){
00200       number += GetEntries( it->second );
00201    }
00202    return number; 
00203 }

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