Logo ROOT   6.08/07
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 #include "TMVA/DataInputHandler.h"
28 
29 #include "TMVA/DataLoader.h"
30 #include "TMVA/MsgLogger.h"
31 #include "TMVA/Types.h"
32 #include "TEventList.h"
33 #include "TCut.h"
34 #include "TFile.h"
35 #include "TROOT.h"
36 #include "TTree.h"
37 
38 #ifndef ROOT_TMVA_Configurable
39 #include "TMVA/Configurable.h"
40 #endif
41 
42 #include <vector>
43 #include <iostream>
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// constructor
47 
49  : fLogger( new MsgLogger("DataInputHandler", kINFO) )
50 {
51  fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// destructor
56 
58 {
59  delete fLogger;
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// add a *className* tree to the dataset to be used as input
64 
66  const TString& className,
67  Double_t weight,
68  const TCut& cut,
70 {
71  TTree * tr = ReadInputTree(fn);
72  tr->SetName( TString("Tree")+className );
73  AddTree( tr, className, weight, cut, tt );
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// add tree of *className* events for tt (Training;Testing..) type as input ..
78 
80  const TString& className,
81  Double_t weight,
82  const TCut& cut,
84 {
85  if (!tree) Log() << kFATAL << "Zero pointer for tree of class " << className.Data() << Endl;
86  if (tree->GetEntries()==0) Log() << kFATAL << "Encountered empty TTree or TChain of class " << className.Data() << Endl;
87  if (fInputTrees[className.Data()].empty()) {
88  // on the first tree (of the class) check if explicit treetype is given
89  fExplicitTrainTest[className.Data()] = (tt != Types::kMaxTreeType);
90  }
91  else {
92  // if the first tree has a specific type, all later tree's must also have one
93  if (fExplicitTrainTest[className.Data()] != (tt!=Types::kMaxTreeType)) {
94  if (tt==Types::kMaxTreeType)
95  Log() << kFATAL << "For the tree " << tree->GetName() << " of class " << className.Data()
96  << " you did "<< (tt==Types::kMaxTreeType?"not ":"") << "specify a type,"
97  << " while you did "<< (tt==Types::kMaxTreeType?"":"not ") << "for the first tree "
98  << fInputTrees[className.Data()][0].GetTree()->GetName() << " of class " << className.Data()
99  << Endl;
100  }
101  }
102  if (cut.GetTitle()[0] != 0) {
103  fInputTrees[className.Data()].push_back(TreeInfo( tree->CopyTree(cut.GetTitle()), className, weight, tt ));
104  }
105  else {
106  fInputTrees[className.Data()].push_back(TreeInfo( tree, className, weight, tt ));
107  }
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// add a signal tree to the dataset to be used as input
112 
114 {
115  AddTree( tr, "Signal", weight, "", tt );
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// add a background tree to the dataset to be used as input
120 
122 {
123  AddTree( tr, "Background", weight, "", tt );
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// add a signal tree to the dataset to be used as input
128 
130 {
131  TTree * tr = ReadInputTree(fn);
132  tr->SetName("TreeS");
133  AddTree( tr, "Signal", weight, "", tt );
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// add a background tree to the dataset to be used as input
138 
140 {
141  TTree * tr = ReadInputTree(fn);
142  tr->SetName("TreeB");
143  AddTree( tr, "Background", weight, "", tt );
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// create trees from these ascii files
148 
150 {
151  TTree* tr = new TTree( "tmp", dataFile );
152  std::ifstream in(dataFile);
153  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;
154  if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
155  in.close();
156 
157  tr->ReadFile( dataFile );
158 
159  return tr;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// define the input trees for signal and background from single input tree,
164 /// containing both signal and background events distinguished by the type
165 /// identifiers: SigCut and BgCut
166 
167 void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
168 {
169  if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
170 
171  AddTree( inputTree, "Signal", 1.0, SigCut );
172  AddTree( inputTree, "Background", 1.0, BgCut );
173 }
174 
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 
179 {
180  try {
181  fInputTrees.find(className)->second.clear();
182  }
183  catch(int) {
184  Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
185  }
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 
190 std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
191 {
192  std::vector< TString >* ret = new std::vector< TString >();
193  for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++ ){
194  ret->push_back( it->first );
195  }
196  return ret;
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// return number of entries in tree
201 
202 UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
203 {
204  UInt_t entries = 0;
205  std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
206  for (;tiIt != tiV.end(); tiIt++) entries += tiIt->GetEntries();
207  return entries;
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// return number of entries in tree
212 
214 {
215  UInt_t number = 0;
216  for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); it++) {
217  number += GetEntries( it->second );
218  }
219  return number;
220 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
UInt_t GetEntries() const
return number of entries in tree
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:3535
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
TText * tt
Definition: textangle.C:16
std::map< TString, std::vector< TreeInfo > > fInputTrees
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
void ClearTreeList(const TString &className)
MsgLogger & Log() const
message logger
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 void SetDirectory(TDirectory *dir)
Change the tree&#39;s directory.
Definition: TTree.cxx:8326
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:7036
virtual Long64_t GetEntries() const
Definition: TTree.h:393
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: tree.py:1
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:88
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8531
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
std::vector< TString > * GetClassList() const
const char * Data() const
Definition: TString.h:349