00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00046 fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
00047 }
00048
00049
00050 TMVA::DataInputHandler::~DataInputHandler()
00051 {
00052
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
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
00082 fExplicitTrainTest[className.Data()] = ( tt != Types::kMaxTreeType);
00083 }
00084 else {
00085
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
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
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
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
00153
00154
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
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
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 }