ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 #include <vector>
28 #include <iostream>
29 
30 #include "TMVA/DataInputHandler.h"
31 #include "TMVA/MsgLogger.h"
32 #include "TMVA/Types.h"
33 #include "TEventList.h"
34 #include "TCut.h"
35 #include "TFile.h"
36 #include "TROOT.h"
37 
38 #ifndef ROOT_TMVA_Configurable
39 #include "TMVA/Configurable.h"
40 #endif
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// constructor
44 
46  : fLogger( new MsgLogger("DataInputHandler", kINFO) )
47 {
48  fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// destructor
53 
55 {
56  delete fLogger;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// add a *className* tree to the dataset to be used as input
61 
63  const TString& className,
64  Double_t weight,
65  const TCut& cut,
67 {
68  TTree * tr = ReadInputTree(fn);
69  tr->SetName( TString("Tree")+className );
70  AddTree( tr, className, weight, cut, tt );
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// add tree of *className* events for tt (Training;Testing..) type as input ..
75 
77  const TString& className,
78  Double_t weight,
79  const TCut& cut,
81 {
82  if (!tree) Log() << kFATAL << "Zero pointer for tree of class " << className.Data() << Endl;
83  if (tree->GetEntries()==0) Log() << kFATAL << "Encountered empty TTree or TChain of class " << className.Data() << Endl;
84  if (fInputTrees[className.Data()].empty()) {
85  // on the first tree (of the class) check if explicit treetype is given
86  fExplicitTrainTest[className.Data()] = (tt != Types::kMaxTreeType);
87  }
88  else {
89  // if the first tree has a specific type, all later tree's must also have one
90  if (fExplicitTrainTest[className.Data()] != (tt!=Types::kMaxTreeType)) {
91  if (tt==Types::kMaxTreeType)
92  Log() << kFATAL << "For the tree " << tree->GetName() << " of class " << className.Data()
93  << " you did "<< (tt==Types::kMaxTreeType?"not ":"") << "specify a type,"
94  << " while you did "<< (tt==Types::kMaxTreeType?"":"not ") << "for the first tree "
95  << fInputTrees[className.Data()][0].GetTree()->GetName() << " of class " << className.Data()
96  << Endl;
97  }
98  }
99  if (cut.GetTitle()[0] != 0) {
100  fInputTrees[className.Data()].push_back(TreeInfo( tree->CopyTree(cut.GetTitle()), className, weight, tt ));
101  }
102  else {
103  fInputTrees[className.Data()].push_back(TreeInfo( tree, className, weight, tt ));
104  }
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// add a signal tree to the dataset to be used as input
109 
111 {
112  AddTree( tr, "Signal", weight, "", tt );
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// add a background tree to the dataset to be used as input
117 
119 {
120  AddTree( tr, "Background", weight, "", tt );
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// add a signal tree to the dataset to be used as input
125 
127 {
128  TTree * tr = ReadInputTree(fn);
129  tr->SetName("TreeS");
130  AddTree( tr, "Signal", weight, "", tt );
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// add a background tree to the dataset to be used as input
135 
137 {
138  TTree * tr = ReadInputTree(fn);
139  tr->SetName("TreeB");
140  AddTree( tr, "Background", weight, "", tt );
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// create trees from these ascii files
145 
147 {
148  TTree* tr = new TTree( "tmp", dataFile );
149  std::ifstream in(dataFile);
150  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;
151  if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
152  in.close();
153 
154  tr->ReadFile( dataFile );
155 
156  return tr;
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// define the input trees for signal and background from single input tree,
161 /// containing both signal and background events distinguished by the type
162 /// identifiers: SigCut and BgCut
163 
164 void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
165 {
166  if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
167 
168  AddTree( inputTree, "Signal", 1.0, SigCut );
169  AddTree( inputTree, "Background", 1.0, BgCut );
170 }
171 
172 
173 ////////////////////////////////////////////////////////////////////////////////
174 
176 {
177  try {
178  fInputTrees.find(className)->second.clear();
179  }
180  catch(int) {
181  Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
182  }
183 }
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 
187 std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
188 {
189  std::vector< TString >* ret = new std::vector< TString >();
190  for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++ ){
191  ret->push_back( it->first );
192  }
193  return ret;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// return number of entries in tree
198 
199 UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
200 {
201  UInt_t entries = 0;
202  std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
203  for (;tiIt != tiV.end(); tiIt++) entries += tiIt->GetEntries();
204  return entries;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// return number of entries in tree
209 
211 {
212  UInt_t number = 0;
213  for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++) {
214  number += GetEntries( it->second );
215  }
216  return number;
217 }
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
std::vector< TString > * GetClassList() const
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Basic string class.
Definition: TString.h:137
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual TTree * CopyTree(const char *selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Copy a tree with selection.
Definition: TTree.cxx:3472
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
DataInputHandler()
constructor
const char * Data() const
Definition: TString.h:349
TText * tt
Definition: textangle.C:16
TTree * ReadInputTree(const TString &dataFile)
create trees from these ascii files
A specialized string object used for TTree selections.
Definition: TCut.h:27
std::map< std::string, Bool_t > fExplicitTrainTest
list of input trees per class (classname is given as first parameter in the map)
void ClearTreeList(const TString &className)
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 ..
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
tuple tree
Definition: tree.py:24
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition: TTree.cxx:8064
double Double_t
Definition: RtypesCore.h:55
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter= ' ')
Create or simply read branches from filename.
Definition: TTree.cxx:6801
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...
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
TString()
TString default ctor.
Definition: TString.cxx:87
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Definition: math.cpp:60
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8269
UInt_t GetEntries() const
return number of entries in tree