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
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 "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
170void 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
193std::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
205UInt_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}
const Bool_t kFALSE
Definition: RtypesCore.h:101
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
if set to true the user has specified training and testing data explicitly
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:57
@ kMaxTreeType
also used as temporary storage for trees not yet assigned for testing;training...
Definition: Types.h:145
@ kINFO
Definition: Types.h:58
@ kWARNING
Definition: Types.h:59
@ kFATAL
Definition: Types.h:61
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
A TTree represents a columnar dataset.
Definition: TTree.h:79
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition: TTree.cxx:8918
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from filename.
Definition: TTree.cxx:7531
void SetName(const char *name) override
Change the name of this tree.
Definition: TTree.cxx:9157
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:148
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition: TMath.h:753
Definition: tree.py:1
auto * tt
Definition: textangle.C:16