Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Results.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Results *
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  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
16  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
17  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * *
19  * Copyright (c) 2006: *
20  * CERN, Switzerland *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 /*! \class TMVA::Results
29 \ingroup TMVA
30 Class that is the base-class for a vector of result
31 */
32 
33 #include "TMVA/Results.h"
34 
35 #include "TMVA/MsgLogger.h"
36 #include "TMVA/Types.h"
37 
38 #include "TGraph.h"
39 #include "TH1.h"
40 #include "TH2.h"
41 #include "TList.h"
42 
43 #include <vector>
44 
45 namespace TMVA {
46  class DataSetInfo;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// constructor
51 
52 TMVA::Results::Results( const DataSetInfo* dsi, TString resultsName )
53  : fTreeType(Types::kTraining),
54  fDsi(dsi),
55  fStorage( new TList() ),
56  fHistAlias( new std::map<TString, TObject*> ),
57  fLogger( new MsgLogger(Form("Results%s",resultsName.Data()), kINFO) )
58 {
59  fStorage->SetOwner();
60 }
61 
62 TMVA::Results::Results( )
63 : fTreeType(Types::kTraining),
64 fDsi(0),
65 fStorage( new TList() ),
66 fHistAlias( new std::map<TString, TObject*> ),
67 fLogger( new MsgLogger("Results", kINFO))
68 {
69  fStorage->SetOwner();
70 }
71 
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// destructor
75 
76 TMVA::Results::~Results()
77 {
78  // delete result-histograms
79  delete fStorage;
80  delete fHistAlias;
81  delete fLogger;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 
86 void TMVA::Results::Store( TObject* obj, const char* alias )
87 {
88  TListIter l(fStorage);
89  // check if object is already in list
90  while (void* p = (void*)l()) {
91  if(p==obj)
92  *fLogger << kFATAL << "Histogram pointer " << p << " already exists in results storage" << Endl;
93  }
94 
95  TString as(obj->GetName());
96  if (alias!=0) as=TString(alias);
97  if (fHistAlias->find(as) != fHistAlias->end()) {
98  // alias exists
99  *fLogger << kFATAL << "Alias " << as << " already exists in results storage" << Endl;
100  }
101  if( obj->InheritsFrom(TH1::Class()) ) {
102  ((TH1*)obj)->SetDirectory(0);
103  }
104  fStorage->Add( obj );
105  fHistAlias->insert(std::pair<TString, TObject*>(as,obj));
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Returns a stored object if it exists. If it does not, a nullptr is returned.
110 ///
111 
112 TObject* TMVA::Results::GetObject(const TString & alias) const
113 {
114  std::map<TString, TObject*>::iterator it = fHistAlias->find(alias);
115 
116  if (it != fHistAlias->end()) return it->second;
117 
118  // alias does not exist
119  return nullptr;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Returns true if there is an object stored in the result for a given alias,
124 /// false otherwise.
125 ///
126 
127 Bool_t TMVA::Results::DoesExist(const TString & alias) const
128 {
129  TObject* test = GetObject(alias);
130 
131  return (test != nullptr);
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 
136 TH1* TMVA::Results::GetHist(const TString & alias) const
137 {
138  TH1* out=dynamic_cast<TH1*>(GetObject(alias));
139  if (!out) Log() <<kWARNING << "You have asked for histogram " << alias << " which does not seem to exist in *Results* .. better don't use it " << Endl;
140  return out;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 
145 TH2* TMVA::Results::GetHist2D(const TString & alias) const
146 {
147  TH2* out=dynamic_cast<TH2*>(GetObject(alias));
148  if (!out) Log() <<kWARNING << "You have asked for 2D histogram " << alias << " which does not seem to exist in *Results* .. better don't use it " << Endl;
149  return out;
150 }
151 ////////////////////////////////////////////////////////////////////////////////
152 
153 TGraph* TMVA::Results::GetGraph(const TString & alias) const
154 {
155  return (TGraph*)GetObject(alias);
156 }
157 
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// delete all stored histograms
161 
162 void TMVA::Results::Delete(Option_t *)
163 {
164  fStorage->Delete();
165  fHistAlias->clear();
166 }