Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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(nullptr);
157 Log() << kWARNING << "Watch out, I (Helge) made the Tree not associated to the current directory .. Hopefully that does not have unwanted consequences" << Endl;
158 if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
159 in.close();
160
161 tr->ReadFile( dataFile );
162
163 return tr;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// define the input trees for signal and background from single input tree,
168/// containing both signal and background events distinguished by the type
169/// identifiers: SigCut and BgCut
170
171void TMVA::DataInputHandler::AddInputTrees(TTree* inputTree, const TCut& SigCut, const TCut& BgCut)
172{
173 if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
174
175 AddTree( inputTree, "Signal", 1.0, SigCut );
176 AddTree( inputTree, "Background", 1.0, BgCut );
177}
178
179
180////////////////////////////////////////////////////////////////////////////////
181
183{
184 try {
185 fInputTrees.find(className)->second.clear();
186 }
187 catch(int) {
188 Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
189 }
190}
191
192////////////////////////////////////////////////////////////////////////////////
193
194std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
195{
196 std::vector< TString >* ret = new std::vector< TString >();
197 for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it ){
198 ret->push_back( it->first );
199 }
200 return ret;
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// return number of entries in tree
205
206UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
207{
208 UInt_t entries = 0;
209 std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
210 for (;tiIt != tiV.end();++tiIt) entries += tiIt->GetEntries();
211 return entries;
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// return number of entries in tree
216
218{
219 UInt_t number = 0;
220 for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it) {
221 number += GetEntries( it->second );
222 }
223 return number;
224}
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
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
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition TTree.cxx:8966
virtual Long64_t ReadFile(const char *filename, const char *branchDescriptor="", char delimiter=' ')
Create or simply read branches from filename.
Definition TTree.cxx:7564
void SetName(const char *name) override
Change the name of this tree.
Definition TTree.cxx:9205
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148
auto * tt
Definition textangle.C:16