Logo ROOT  
Reference Guide
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 "TTree.h"
42 
43 #include "TMVA/Configurable.h"
44 
45 #include <vector>
46 #include <fstream>
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// constructor
50 
52  : fLogger( new MsgLogger("DataInputHandler", kINFO) )
53 {
54  fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// destructor
59 
61 {
62  delete fLogger;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// add a *className* tree to the dataset to be used as input
67 
69  const TString& className,
70  Double_t weight,
71  const TCut& cut,
73 {
74  TTree * tr = ReadInputTree(fn);
75  tr->SetName( TString("Tree")+className );
76  AddTree( tr, className, weight, cut, tt );
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// add tree of *className* events for tt (Training;Testing..) type as input ..
81 
83  const TString& className,
84  Double_t weight,
85  const TCut& cut,
87 {
88  if (!tree) Log() << kFATAL << "Zero pointer for tree of class " << className.Data() << Endl;
89  if (tree->GetEntries()==0) Log() << kFATAL << "Encountered empty TTree or TChain of class " << className.Data() << Endl;
90  if (fInputTrees[className.Data()].empty()) {
91  // on the first tree (of the class) check if explicit treetype is given
92  fExplicitTrainTest[className.Data()] = (tt != Types::kMaxTreeType);
93  }
94  else {
95  // if the first tree has a specific type, all later tree's must also have one
96  if (fExplicitTrainTest[className.Data()] != (tt!=Types::kMaxTreeType)) {
98  Log() << kFATAL << "For the tree " << tree->GetName() << " of class " << className.Data()
99  << " you did "<< (tt==Types::kMaxTreeType?"not ":"") << "specify a type,"
100  << " while you did "<< (tt==Types::kMaxTreeType?"":"not ") << "for the first tree "
101  << fInputTrees[className.Data()][0].GetTree()->GetName() << " of class " << className.Data()
102  << Endl;
103  }
104  }
105  if (cut.GetTitle()[0] != 0) {
106  fInputTrees[className.Data()].push_back(TreeInfo( tree->CopyTree(cut.GetTitle()), className, weight, tt ));
107  }
108  else {
109  fInputTrees[className.Data()].push_back(TreeInfo( tree, className, weight, tt ));
110  }
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// add a signal tree to the dataset to be used as input
115 
117 {
118  AddTree( tr, "Signal", weight, "", tt );
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// add a background tree to the dataset to be used as input
123 
125 {
126  AddTree( tr, "Background", weight, "", tt );
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// add a signal tree to the dataset to be used as input
131 
133 {
134  TTree * tr = ReadInputTree(fn);
135  tr->SetName("TreeS");
136  AddTree( tr, "Signal", weight, "", tt );
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// add a background tree to the dataset to be used as input
141 
143 {
144  TTree * tr = ReadInputTree(fn);
145  tr->SetName("TreeB");
146  AddTree( tr, "Background", weight, "", tt );
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// create trees from these ascii files
151 
153 {
154  TTree* tr = new TTree( "tmp", dataFile );
155  std::ifstream in(dataFile);
156  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;
157  if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
158  in.close();
159 
160  tr->ReadFile( dataFile );
161 
162  return tr;
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// define the input trees for signal and background from single input tree,
167 /// containing both signal and background events distinguished by the type
168 /// identifiers: SigCut and BgCut
169 
170 void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
171 {
172  if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
173 
174  AddTree( inputTree, "Signal", 1.0, SigCut );
175  AddTree( inputTree, "Background", 1.0, BgCut );
176 }
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 
182 {
183  try {
184  fInputTrees.find(className)->second.clear();
185  }
186  catch(int) {
187  Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
188  }
189 }
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 
193 std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
194 {
195  std::vector< TString >* ret = new std::vector< TString >();
196  for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it ){
197  ret->push_back( it->first );
198  }
199  return ret;
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// return number of entries in tree
204 
205 UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
206 {
207  UInt_t entries = 0;
208  std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
209  for (;tiIt != tiV.end();++tiIt) entries += tiIt->GetEntries();
210  return entries;
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// return number of entries in tree
215 
217 {
218  UInt_t number = 0;
219  for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it) {
220  number += GetEntries( it->second );
221  }
222  return number;
223 }
TCut
A specialized string object used for TTree selections.
Definition: TCut.h:25
tt
auto * tt
Definition: textangle.C:16
TMVA::DataInputHandler::AddSignalTree
void AddSignalTree(TTree *tr, Double_t weight=1.0, Types::ETreeType tt=Types::kMaxTreeType)
add a signal tree to the dataset to be used as input
Definition: DataInputHandler.cxx:116
TString::Data
const char * Data() const
Definition: TString.h:369
tree
Definition: tree.py:1
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
TMVA::DataInputHandler::DataInputHandler
DataInputHandler()
constructor
Definition: DataInputHandler.cxx:51
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
DataLoader.h
TMVA::DataInputHandler::GetClassList
std::vector< TString > * GetClassList() const
Definition: DataInputHandler.cxx:193
TTree.h
TString
Basic string class.
Definition: TString.h:136
TMVA::DataInputHandler::~DataInputHandler
~DataInputHandler()
destructor
Definition: DataInputHandler.cxx:60
DataInputHandler.h
TMVA::DataInputHandler::AddTree
void AddTree(TTree *tree, const TString &className, Double_t weight=1.0, const TCut &cut="", Types::ETreeType tt=Types::kMaxTreeType)
add tree of className events for tt (Training;Testing..) type as input ..
Definition: DataInputHandler.cxx:82
MsgLogger.h
TMVA::TreeInfo
Definition: DataInputHandler.h:52
TMVA::DataInputHandler::ClearTreeList
void ClearTreeList(const TString &className)
Definition: DataInputHandler.cxx:181
TMVA::DataInputHandler::AddInputTrees
void AddInputTrees(TTree *inputTree, const TCut &SigCut, const TCut &BgCut)
define the input trees for signal and background from single input tree, containing both signal and b...
Definition: DataInputHandler.cxx:170
TMVA::Types::ETreeType
ETreeType
Definition: Types.h:144
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::TString
TString()
TString default ctor.
Definition: TString.cxx:87
TMVA::DataInputHandler::fExplicitTrainTest
std::map< std::string, Bool_t > fExplicitTrainTest
Definition: DataInputHandler.h:130
TCut.h
Types.h
Configurable.h
TMVA::DataInputHandler::ReadInputTree
TTree * ReadInputTree(const TString &dataFile)
create trees from these ascii files
Definition: DataInputHandler.cxx:152
TTree::SetDirectory
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition: TTree.cxx:8797
TMVA::Types::kMaxTreeType
@ kMaxTreeType
Definition: Types.h:147
TMVA::Endl
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
unsigned int
TMVA::DataInputHandler::AddBackgroundTree
void AddBackgroundTree(TTree *tr, Double_t weight=1.0, Types::ETreeType tt=Types::kMaxTreeType)
add a background tree to the dataset to be used as input
Definition: DataInputHandler.cxx:124
TTree::ReadFile
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from filename.
Definition: TTree.cxx:7451
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::MsgLogger
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
TTree::SetName
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:9036
TMVA::DataInputHandler::GetEntries
UInt_t GetEntries() const
return number of entries in tree
Definition: DataInputHandler.cxx:216
TEventList.h