Logo ROOT   6.18/05
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
30Class that contains all the data information.
31
32*/
33
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
54 : fLogger( new MsgLogger("DataInputHandler", kINFO) )
55{
56 fExplicitTrainTest["Signal"] = fExplicitTrainTest["Background"] = kFALSE;
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// destructor
61
63{
64 delete fLogger;
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// add a *className* tree to the dataset to be used as input
69
71 const TString& className,
72 Double_t weight,
73 const TCut& cut,
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
85 const TString& className,
86 Double_t weight,
87 const TCut& cut,
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)) {
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
119{
120 AddTree( tr, "Signal", weight, "", tt );
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// add a background tree to the dataset to be used as input
125
127{
128 AddTree( tr, "Background", weight, "", tt );
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// add a signal tree to the dataset to be used as input
133
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
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
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
172void 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
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
195std::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
207UInt_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
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}
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
double Double_t
Definition: RtypesCore.h:55
A specialized string object used for TTree selections.
Definition: TCut.h:25
void ClearTreeList(const TString &className)
UInt_t GetEntries() const
return number of entries in tree
TTree * ReadInputTree(const TString &dataFile)
create trees from these ascii files
std::map< std::string, Bool_t > fExplicitTrainTest
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
std::vector< TString > * GetClassList() const
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 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 ..
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
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
@ kMaxTreeType
Definition: Types.h:146
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
A TTree represents a columnar dataset.
Definition: TTree.h:71
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition: TTree.cxx:8622
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from filename.
Definition: TTree.cxx:7301
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8851
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Log(Double_t x)
Definition: TMath.h:748
Definition: tree.py:1
auto * tt
Definition: textangle.C:16