Logo ROOT   6.12/07
Reference Guide
DataSetFactory.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Eckhard von Toerne, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : DataSetFactory *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Contains all the data information *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
17  * Eckhard von Toerne <evt@physik.uni-bonn.de> - U. of Bonn, Germany *
18  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
19  * *
20  * Copyright (c) 2006: *
21  * CERN, Switzerland *
22  * MPI-K Heidelberg, Germany *
23  * *
24  * Redistribution and use in source and binary forms, with or without *
25  * modification, are permitted according to the terms listed in LICENSE *
26  * (http://tmva.sourceforge.net/LICENSE) *
27  **********************************************************************************/
28 
29 #ifndef ROOT_TMVA_DataSetFactory
30 #define ROOT_TMVA_DataSetFactory
31 
32 //////////////////////////////////////////////////////////////////////////
33 // //
34 // DataSetFactory //
35 // //
36 // Class that contains all the data information //
37 // //
38 //////////////////////////////////////////////////////////////////////////
39 
40 #include <vector>
41 #include <stdlib.h>
42 
43 #include "TString.h"
44 #include "TTree.h"
45 #include "TCut.h"
46 #include "TTreeFormula.h"
47 #include "TMatrixDfwd.h"
48 #include "TPrincipal.h"
49 #include "TRandom3.h"
50 
51 #include "TMVA/Types.h"
52 #include "TMVA/VariableInfo.h"
53 #include "TMVA/Event.h"
54 
55 namespace TMVA {
56 
57  class DataSet;
58  class DataSetInfo;
59  class DataInputHandler;
60  class TreeInfo;
61  class MsgLogger;
62 
63  // =============== maybe move these elswhere (e.g. into the tools )
64 
65  // =============== functors =======================
66 
68  public:
70 
72 
74 
75  static constexpr UInt_t min() { return 0; }
76  static constexpr UInt_t max() { return kMaxUInt; }
77 
78  private:
79  TRandom3 fRandom; // random generator
80  };
81 
82  // delete-functor (to be used in e.g. for_each algorithm)
83  template<class T>
85  {
87  delete p;
88  return *this;
89  }
90  };
91 
92  template<class T>
94  {
95  return DeleteFunctor_t<const T>();
96  }
97 
98 
99  template< typename T >
100  class Increment {
102  public:
103  Increment( T start ) : value( start ){ }
105  return value++;
106  }
107  };
108 
109 
110 
111  template <typename F>
112  class null_t
113  {
114  private:
115  // returns argF
116  public:
117  typedef F argument_type;
118  F operator()(const F& argF) const
119  {
120  return argF;
121  }
122  };
123 
124  template <typename F>
125  inline null_t<F> null() {
126  return null_t<F>();
127  }
128 
129 
130 
131  template <typename F, typename G, typename H>
132  class compose_binary_t : public std::binary_function<typename G::argument_type,
133  typename H::argument_type,
134  typename F::result_type>
135  {
136  private:
137  const F& f; // f(g(argG),h(argH))
138  const G& g;
139  const H& h;
140  public:
141  compose_binary_t(const F& _f, const G& _g, const H& _h) : f(_f), g(_g), h(_h)
142  {
143  }
144 
145  typename F::result_type operator()(const typename G::argument_type& argG,
146  const typename H::argument_type& argH) const
147  {
148  return f(g(argG),h(argH));
149  }
150  };
151 
152  template <typename F, typename G, typename H>
153  inline compose_binary_t<F,G,H> compose_binary(const F& _f, const G& _g, const H& _h) {
154  return compose_binary_t<F,G,H>(_f,_g,_h);
155  }
156 
157 
158 
159 
160  template <typename F, typename G>
161  class compose_unary_t : public std::unary_function<typename G::argument_type,
162  typename F::result_type>
163  {
164  private:
165  const F& f; // f(g(argG))
166  const G& g;
167  public:
168  compose_unary_t(const F& _f, const G& _g) : f(_f), g(_g)
169  {
170  }
171 
172  typename F::result_type operator()(const typename G::argument_type& argG) const
173  {
174  return f(g(argG));
175  }
176  };
177 
178  template <typename F, typename G>
179  inline compose_unary_t<F,G> compose_unary(const F& _f, const G& _g) {
180  return compose_unary_t<F,G>(_f,_g);
181  }
182 
183  // =============== functors =======================
184 
185 
186  // =========================================================
187 
188 
189  class DataSetFactory:public TObject {
190 
191  typedef std::vector<Event* > EventVector;
192  typedef std::vector< EventVector > EventVectorOfClasses;
193  typedef std::map<Types::ETreeType, EventVectorOfClasses > EventVectorOfClassesOfTreeType;
194  typedef std::map<Types::ETreeType, EventVector > EventVectorOfTreeType;
195 
196  typedef std::vector< Double_t > ValuePerClass;
197  typedef std::map<Types::ETreeType, ValuePerClass > ValuePerClassOfTreeType;
198 
199  class EventStats {
200  public:
212  nTrainingEventsRequested(0),
213  nTestingEventsRequested(0),
214  TrainTestSplitRequested(0),
215  nInitialEvents(0),
216  nEvBeforeCut(0),
217  nEvAfterCut(0),
218  nWeEvBeforeCut(0),
219  nWeEvAfterCut(0),
220  nNegWeights(0),
221  varAvLength(0)
222  {}
223  ~EventStats() { delete[] varAvLength; }
224  Float_t cutScaling() const { return Float_t(nEvAfterCut)/nEvBeforeCut; }
225  };
226 
227  typedef std::vector< int > NumberPerClass;
228  typedef std::vector< EventStats > EvtStatsPerClass;
229 
230  public:
231 
232  ~DataSetFactory();
233 
234  DataSetFactory();
235 
236  DataSet* CreateDataSet( DataSetInfo &, DataInputHandler& );
237  protected:
238 
239 
240  DataSet* BuildInitialDataSet( DataSetInfo&, TMVA::DataInputHandler& );
241  DataSet* BuildDynamicDataSet( DataSetInfo& );
242 
243  // ---------- new versions
244  void BuildEventVector ( DataSetInfo& dsi,
245  DataInputHandler& dataInput,
246  EventVectorOfClassesOfTreeType& eventsmap,
247  EvtStatsPerClass& eventCounts);
248 
249  DataSet* MixEvents ( DataSetInfo& dsi,
250  EventVectorOfClassesOfTreeType& eventsmap,
251  EvtStatsPerClass& eventCounts,
252  const TString& splitMode,
253  const TString& mixMode,
254  const TString& normMode,
255  UInt_t splitSeed);
256 
257  void RenormEvents ( DataSetInfo& dsi,
258  EventVectorOfClassesOfTreeType& eventsmap,
259  const EvtStatsPerClass& eventCounts,
260  const TString& normMode );
261 
262  void InitOptions ( DataSetInfo& dsi,
263  EvtStatsPerClass& eventsmap,
264  TString& normMode, UInt_t& splitSeed,
265  TString& splitMode, TString& mixMode);
266 
267 
268  // ------------------------
269 
270  // auxiliary functions to compute correlations
271  TMatrixD* CalcCorrelationMatrix( DataSet*, const UInt_t classNumber );
272  TMatrixD* CalcCovarianceMatrix ( DataSet*, const UInt_t classNumber );
273  void CalcMinMax ( DataSet*, DataSetInfo& dsi );
274 
275  // resets branch addresses to current event
276  void ResetBranchAndEventAddresses( TTree* );
277  void ResetCurrentTree() { fCurrentTree = 0; }
278  void ChangeToNewTree( TreeInfo&, const DataSetInfo & );
279  Bool_t CheckTTreeFormula( TTreeFormula* ttf, const TString& expression, Bool_t& hasDollar );
280 
281  // verbosity
282  Bool_t Verbose() { return fVerbose; }
283 
284  // data members
285 
286  // verbosity
287  Bool_t fVerbose; // Verbosity
288  TString fVerboseLevel; // VerboseLevel
289 
290  // Printing
291  Bool_t fCorrelations; // Whether to print correlations or not
292 
293  Bool_t fScaleWithPreselEff; // how to deal with requested #events in connection with preselection cuts
294 
295  // the event
296  TTree* fCurrentTree; // the tree, events are currently read from
297  UInt_t fCurrentEvtIdx; // the current event (to avoid reading of the same event)
298 
299  // the formulas for reading the original tree
300  std::vector<TTreeFormula*> fInputFormulas; // input variables
301  std::vector<TTreeFormula*> fTargetFormulas; // targets
302  std::vector<TTreeFormula*> fCutFormulas; // cuts
303  std::vector<TTreeFormula*> fWeightFormula; // weights
304  std::vector<TTreeFormula*> fSpectatorFormulas; // spectators
305 
306  MsgLogger* fLogger; //! message logger
307  MsgLogger& Log() const { return *fLogger; }
308  public:
310  };
311 }
312 
313 #endif
F operator()(const F &argF) const
const UInt_t kMaxUInt
Definition: RtypesCore.h:98
std::vector< EventVector > EventVectorOfClasses
Random number generator class based on M.
Definition: TRandom3.h:27
UInt_t operator()(UInt_t n=kMaxUInt)
std::vector< TTreeFormula * > fInputFormulas
float Float_t
Definition: RtypesCore.h:53
double T(double x)
Definition: ChebyshevPol.h:34
std::vector< TTreeFormula * > fCutFormulas
TH1 * h
Definition: legend2.C:5
std::vector< Double_t > ValuePerClass
virtual void SetSeed(ULong_t seed=0)
Set the random generator sequence if seed is 0 (default value) a TUUID is generated and used to fill ...
Definition: TRandom3.cxx:207
#define H(x, y, z)
compose_unary_t< F, G > compose_unary(const F &_f, const G &_g)
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
compose_unary_t(const F &_f, const G &_g)
#define G(x, y, z)
DeleteFunctor_t & operator()(const T *p)
compose_binary_t(const F &_f, const G &_g, const H &_h)
std::vector< int > NumberPerClass
std::map< Types::ETreeType, EventVectorOfClasses > EventVectorOfClassesOfTreeType
null_t< F > null()
#define ClassDef(name, id)
Definition: Rtypes.h:320
MsgLogger & Log() const
message logger
std::vector< TTreeFormula * > fWeightFormula
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:341
Class that contains all the data information.
DeleteFunctor_t< const T > DeleteFunctor()
Class that contains all the data information.
Definition: DataSetInfo.h:60
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:58
Class that contains all the data information.
Definition: DataSet.h:69
#define F(x, y, z)
Class that contains all the data information.
std::vector< TTreeFormula * > fSpectatorFormulas
static constexpr UInt_t max()
unsigned int UInt_t
Definition: RtypesCore.h:42
std::vector< TTreeFormula * > fTargetFormulas
std::vector< Event *> EventVector
F::result_type operator()(const typename G::argument_type &argG) const
RandomGenerator(UInt_t seed)
double Double_t
Definition: RtypesCore.h:55
compose_binary_t< F, G, H > compose_binary(const F &_f, const G &_g, const H &_h)
std::map< Types::ETreeType, EventVector > EventVectorOfTreeType
F::result_type operator()(const typename G::argument_type &argG, const typename H::argument_type &argH) const
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Mother of all ROOT objects.
Definition: TObject.h:37
Abstract ClassifierFactory template that handles arbitrary types.
std::vector< EventStats > EvtStatsPerClass
static constexpr UInt_t min()
A TTree object has a header with a name and a title.
Definition: TTree.h:70
std::map< Types::ETreeType, ValuePerClass > ValuePerClassOfTreeType
const Int_t n
Definition: legend1.C:16
static constexpr double g