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 "TCut.h"
40#include "TTree.h"
41
42#include <vector>
43#include <fstream>
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)) {
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(nullptr);
154 Log() << kWARNING << "Watch out, I (Helge) made the Tree not associated to the current directory .. Hopefully that does not have unwanted consequences" << Endl;
155 if (!in.good()) Log() << kFATAL << "Could not open file: " << dataFile << Endl;
156 in.close();
157
158 tr->ReadFile( dataFile );
159
160 return tr;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// define the input trees for signal and background from single input tree,
165/// containing both signal and background events distinguished by the type
166/// identifiers: SigCut and BgCut
167
169{
170 if (!inputTree) Log() << kFATAL << "Zero pointer for input tree: " << inputTree << Endl;
171
172 AddTree( inputTree, "Signal", 1.0, SigCut );
173 AddTree( inputTree, "Background", 1.0, BgCut );
174}
175
176
177////////////////////////////////////////////////////////////////////////////////
178
180{
181 try {
182 fInputTrees.find(className)->second.clear();
183 }
184 catch(int) {
185 Log() << kINFO << " Clear treelist for class " << className << " failed, since class does not exist." << Endl;
186 }
187}
188
189////////////////////////////////////////////////////////////////////////////////
190
191std::vector< TString >* TMVA::DataInputHandler::GetClassList() const
192{
193 std::vector< TString >* ret = new std::vector< TString >();
194 for ( std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it ){
195 ret->push_back( it->first );
196 }
197 return ret;
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// return number of entries in tree
202
203UInt_t TMVA::DataInputHandler::GetEntries(const std::vector<TreeInfo>& tiV) const
204{
205 UInt_t entries = 0;
206 std::vector<TreeInfo>::const_iterator tiIt = tiV.begin();
207 for (;tiIt != tiV.end();++tiIt) entries += tiIt->GetEntries();
208 return entries;
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// return number of entries in tree
213
215{
216 UInt_t number = 0;
217 for (std::map< TString, std::vector<TreeInfo> >::iterator it = fInputTrees.begin(); it != fInputTrees.end(); ++it) {
218 number += GetEntries( it->second );
219 }
220 return number;
221}
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
const_iterator begin() const
const_iterator end() const
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 * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:386
A TTree represents a columnar dataset.
Definition TTree.h:89
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:3759
virtual Long64_t GetEntries() const
Definition TTree.h:510
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148
auto * tt
Definition textangle.C:16