Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
DataInputHandler.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : DataInputHandler *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * *
18  * Copyright (c) 2006: *
19  * CERN, Switzerland *
20  * MPI-K Heidelberg, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 /*! \class TMVA::DataInputHandler
28 \ingroup TMVA
29 
30 Class that contains all the data information.
31 
32 */
33 
34 #include "TMVA/DataInputHandler.h"
35 
36 #include "TMVA/DataLoader.h"
37 #include "TMVA/MsgLogger.h"
38 #include "TMVA/Types.h"
39 #include "TEventList.h"
40 #include "TCut.h"
41 #include "TFile.h"
42 #include "TROOT.h"
43 #include "TTree.h"
44 
45 #include "TMVA/Configurable.h"
46 
47 #include <vector>
48 #include <iostream>
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// constructor
52 
53 TMVA::DataInputHandler::DataInputHandler()
54  : fLogger( new MsgLogger("DataInputHandler", kINFO) )
55 {
56  fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// destructor
61 
62 TMVA::DataInputHandler::~DataInputHandler()
63 {
64  delete fLogger;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// add a *className* tree to the dataset to be used as input
69 
70 void TMVA::DataInputHandler::AddTree( const TString& fn,
71  const TString& className,
72  Double_t weight,
73  const TCut& cut,
74  Types::ETreeType tt )
75 {
76  TTree * tr = ReadInputTree(fn);
77  tr->SetName( TString("Tree")+className );
78  AddTree( tr, className, weight, cut, tt );
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// add tree of *className* events for tt (Training;Testing..) type as input ..
83 
84 void TMVA::DataInputHandler::AddTree( TTree* tree,
85  const TString& className,
86  Double_t weight,
87  const TCut& cut,
88  Types::ETreeType tt )
89 {
90  if (!tree) Log() << kFATAL << "Zero pointer for tree of class " << className.Data() << Endl;
91  if (tree->GetEntries()==0) Log() << kFATAL << "Encountered empty TTree or TChain of class " << className.Data() << Endl;
92  if (fInputTrees[className.Data()].empty()) {
93  // on the first tree (of the class) check if explicit treetype is given
94  fExplicitTrainTest[className.Data()] = (tt != Types::kMaxTreeType);
95  }
96  else {
97  // if the first tree has a specific type, all later tree's must also have one
98  if (fExplicitTrainTest[className.Data()] != (tt!=Types::kMaxTreeType)) {
99  if (tt==Types::kMaxTreeType)
100  Log() << kFATAL << "For the tree " << tree->GetName() << " of class " << className.Data()
101  << " you did "<< (tt==Types::kMaxTreeType?"not ":"") << "specify a type,"
102  << " while you did "<< (tt==Types::kMaxTreeType?"":"not ") << "for the first tree "
103  << fInputTrees[className.Data()][0].GetTree()->GetName() << " of class " << className.Data()
104  << Endl;
105  }
106  }
107  if (cut.GetTitle()[0] != 0) {
108  fInputTrees[className.Data()].push_back(TreeInfo( tree->CopyTree(cut.GetTitle()), className, weight, tt ));
109  }
110  else {
111  fInputTrees[className.Data()].push_back(TreeInfo( tree, className, weight, tt ));
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// add a signal tree to the dataset to be used as input
117 
118 void TMVA::DataInputHandler::AddSignalTree( TTree* tr, Double_t weight, Types::ETreeType tt )
119 {
120  AddTree( tr, "Signal", weight, "", tt );
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// add a background tree to the dataset to be used as input
125 
126 void TMVA::DataInputHandler::AddBackgroundTree( TTree* tr, Double_t weight, Types::ETreeType tt )
127 {
128  AddTree( tr, "Background", weight, "", tt );
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// add a signal tree to the dataset to be used as input
133 
134 void TMVA::DataInputHandler::AddSignalTree( const TString& fn, Double_t weight, Types::ETreeType tt )
135 {
136  TTree * tr = ReadInputTree(fn);
137  tr->SetName("TreeS");
138  AddTree( tr, "Signal", weight, "", tt );
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// add a background tree to the dataset to be used as input
143 
144 void TMVA::DataInputHandler::AddBackgroundTree( const TString& fn, Double_t weight, Types::ETreeType tt )
145 {
146  TTree * tr = ReadInputTree(fn);
147  tr->SetName("TreeB");
148  AddTree( tr, "Background", weight, "", tt );
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// create trees from these ascii files
153 
154 TTree* TMVA::DataInputHandler::ReadInputTree( const TString& dataFile )
155 {
156  TTree* tr = new TTree( "tmp", dataFile );
157  std::ifstream in(dataFile);
158  tr->SetDirectory(0); Log() << kWARNING << "Watch out, I (Helge) made the Tree not associated to the current directory .. Hopefully that does not have unwanted consequences" << Endl;
159  if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
160  in.close();
161 
162  tr->ReadFile( dataFile );
163 
164  return tr;
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// define the input trees for signal and background from single input tree,
169 /// containing both signal and background events distinguished by the type
170 /// identifiers: SigCut and BgCut
171 
172 void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
173 {
174  if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
175 
176  AddTree( inputTree, "Signal", 1.0, SigCut );
177  AddTree( inputTree, "Background", 1.0, BgCut );
178 }
179 
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 
183 void TMVA::DataInputHandler::ClearTreeList( const TString& className )
184 {
185  try {
186  fInputTrees.find(className)->second.clear();
187  }
188  catch(int) {
189  Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
190  }
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 
195 std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
196 {
197  std::vector< TString >* ret = new std::vector< TString >();
198  for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it ){
199  ret->push_back( it->first );
200  }
201  return ret;
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// return number of entries in tree
206 
207 UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
208 {
209  UInt_t entries = 0;
210  std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
211  for (;tiIt != tiV.end();++tiIt) entries += tiIt->GetEntries();
212  return entries;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// return number of entries in tree
217 
218 UInt_t TMVA::DataInputHandler::GetEntries() const
219 {
220  UInt_t number = 0;
221  for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it) {
222  number += GetEntries( it->second );
223  }
224  return number;
225 }