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