Warning, file /include/root/TMVA/Tools.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifndef ROOT_TMVA_Tools
0030 #define ROOT_TMVA_Tools
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #include <vector>
0041 #include <string>
0042 #include <sstream>
0043 #include <iostream>
0044 #include <atomic>
0045
0046 #include "TXMLEngine.h"
0047
0048 #include "TMatrixDSymfwd.h"
0049
0050 #include "TMatrixDfwd.h"
0051
0052 #include "TVectorDfwd.h"
0053
0054 #include "TMVA/Types.h"
0055
0056 #include "TMVA/VariableTransformBase.h"
0057
0058 #include "TString.h"
0059
0060 #include "TMVA/MsgLogger.h"
0061
0062 class TList;
0063 class TTree;
0064 class TH1;
0065 class TH2;
0066 class TH2F;
0067 class TSpline;
0068 class TXMLEngine;
0069
0070 namespace TMVA {
0071
0072 class Event;
0073 class PDF;
0074 class MsgLogger;
0075
0076 class Tools {
0077
0078 private:
0079
0080 Tools();
0081
0082 public:
0083
0084
0085 ~Tools();
0086
0087
0088 static Tools& Instance();
0089 static void DestroyInstance();
0090
0091
0092 template <typename T> Double_t Mean(Long64_t n, const T *a, const Double_t *w=0);
0093 template <typename Iterator, typename WeightIterator> Double_t Mean ( Iterator first, Iterator last, WeightIterator w);
0094
0095 template <typename T> Double_t RMS(Long64_t n, const T *a, const Double_t *w=0);
0096 template <typename Iterator, typename WeightIterator> Double_t RMS(Iterator first, Iterator last, WeightIterator w);
0097
0098
0099
0100 void ComputeStat( const std::vector<TMVA::Event*>&,
0101 std::vector<Float_t>*,
0102 Double_t&, Double_t&, Double_t&,
0103 Double_t&, Double_t&, Double_t&, Int_t signalClass,
0104 Bool_t norm = kFALSE );
0105
0106
0107 inline Double_t ComputeVariance( Double_t sumx2, Double_t sumx, Int_t nx );
0108
0109
0110 TH1* projNormTH1F( TTree* theTree, const TString& theVarName,
0111 const TString& name, Int_t nbins,
0112 Double_t xmin, Double_t xmax, const TString& cut );
0113
0114
0115 Double_t NormHist( TH1* theHist, Double_t norm = 1.0 );
0116
0117
0118 TList* ParseFormatLine( TString theString, const char * sep = ":" );
0119
0120
0121 std::vector<Int_t>* ParseANNOptionString( TString theOptions, Int_t nvar,
0122 std::vector<Int_t>* nodes );
0123
0124
0125 TMatrixD* GetSQRootMatrix( TMatrixDSym* symMat );
0126
0127
0128
0129 std::vector<TMatrixDSym*>* CalcCovarianceMatrices( const std::vector<Event*>& events, Int_t maxCls, VariableTransformBase* transformBase=nullptr );
0130 std::vector<TMatrixDSym*>* CalcCovarianceMatrices( const std::vector<const Event*>& events, Int_t maxCls, VariableTransformBase* transformBase=nullptr );
0131
0132
0133
0134 const TMatrixD* GetCorrelationMatrix( const TMatrixD* covMat );
0135
0136
0137 Bool_t CheckSplines( const TH1*, const TSpline* );
0138
0139
0140 Double_t NormVariable( Double_t x, Double_t xmin, Double_t xmax );
0141
0142
0143 Double_t GetSeparation( TH1* S, TH1* B ) const;
0144 Double_t GetSeparation( const PDF& pdfS, const PDF& pdfB ) const;
0145
0146
0147 std::vector<Double_t> MVADiff( std::vector<Double_t>&, std::vector<Double_t>& );
0148 void Scale( std::vector<Double_t>&, Double_t );
0149 void Scale( std::vector<Float_t>&, Float_t );
0150
0151
0152
0153 void UsefulSortDescending( std::vector< std::vector<Double_t> >&, std::vector<TString>* vs = nullptr );
0154 void UsefulSortAscending ( std::vector< std::vector<Double_t> >&, std::vector<TString>* vs = nullptr );
0155
0156 void UsefulSortDescending( std::vector<Double_t>& );
0157 void UsefulSortAscending ( std::vector<Double_t>& );
0158
0159 Int_t GetIndexMaxElement ( std::vector<Double_t>& );
0160 Int_t GetIndexMinElement ( std::vector<Double_t>& );
0161
0162
0163 Bool_t ContainsRegularExpression( const TString& s );
0164 TString ReplaceRegularExpressions( const TString& s, const TString& replace = "+" );
0165
0166
0167 void FormattedOutput( const std::vector<Double_t>&, const std::vector<TString>&,
0168 const TString titleVars, const TString titleValues, MsgLogger& logger,
0169 TString format = "%+1.3f" );
0170 void FormattedOutput( const TMatrixD&, const std::vector<TString>&, MsgLogger& logger );
0171 void FormattedOutput( const TMatrixD&, const std::vector<TString>& vert, const std::vector<TString>& horiz,
0172 MsgLogger& logger );
0173
0174 void WriteFloatArbitraryPrecision( Float_t val, std::ostream& os );
0175 void ReadFloatArbitraryPrecision ( Float_t& val, std::istream& is );
0176
0177
0178 TString GetXTitleWithUnit( const TString& title, const TString& unit );
0179 TString GetYTitleWithUnit( const TH1& h, const TString& unit, Bool_t normalised );
0180
0181
0182
0183 Double_t GetMutualInformation( const TH2F& );
0184
0185
0186
0187 Double_t GetCorrelationRatio( const TH2F& );
0188 TH2F* TransposeHist ( const TH2F& );
0189
0190
0191 Bool_t CheckForSilentOption ( const TString& ) const;
0192 Bool_t CheckForVerboseOption( const TString& ) const;
0193
0194
0195 const TString& Color( const TString& );
0196
0197
0198 enum EWelcomeMessage { kStandardWelcomeMsg = 1,
0199 kIsometricWelcomeMsg,
0200 kBlockWelcomeMsg,
0201 kLeanWelcomeMsg,
0202 kLogoWelcomeMsg,
0203 kSmall1WelcomeMsg,
0204 kSmall2WelcomeMsg,
0205 kOriginalWelcomeMsgColor,
0206 kOriginalWelcomeMsgBW };
0207
0208
0209 enum ECitation { kPlainText = 1,
0210 kBibTeX,
0211 kLaTeX,
0212 kHtmlLink };
0213
0214 void TMVAWelcomeMessage();
0215 void TMVAWelcomeMessage( MsgLogger& logger, EWelcomeMessage m = kStandardWelcomeMsg );
0216 void TMVAVersionMessage( MsgLogger& logger );
0217 void ROOTVersionMessage( MsgLogger& logger );
0218
0219 void TMVACitation( MsgLogger& logger, ECitation citType = kPlainText );
0220
0221
0222
0223 std::vector<TString> SplitString( const TString& theOpt, const char separator ) const;
0224
0225
0226 const TString fRegexp;
0227 mutable MsgLogger* fLogger;
0228 MsgLogger& Log() const { return *fLogger; }
0229 static std::atomic<Tools*> fgTools;
0230
0231
0232
0233 TString StringFromInt ( Long_t i );
0234 TString StringFromDouble ( Double_t d );
0235 void WriteTMatrixDToXML ( void* node, const char* name, TMatrixD* mat );
0236 void WriteTVectorDToXML ( void* node, const char* name, TVectorD* vec );
0237 void ReadTMatrixDFromXML( void* node, const char* name, TMatrixD* mat );
0238 void ReadTVectorDFromXML( void* node, const char* name, TVectorD* vec );
0239 Bool_t HistoHasEquidistantBins(const TH1& h);
0240
0241 Bool_t HasAttr ( void* node, const char* attrname );
0242 template<typename T>
0243 inline void ReadAttr ( void* node, const char* , T& value );
0244 void ReadAttr ( void* node, const char* attrname, TString& value );
0245 void ReadAttr(void *node, const char *, float &value);
0246 void ReadAttr(void *node, const char *, int &value);
0247 void ReadAttr(void *node, const char *, short &value);
0248
0249 template<typename T>
0250 void AddAttr ( void* node, const char* , const T& value, Int_t precision = 16 );
0251 void AddAttr ( void* node, const char* attrname, const char* value );
0252 void* AddChild ( void* parent, const char* childname, const char* content = nullptr, bool isRootNode = false );
0253 Bool_t AddRawLine ( void* node, const char * raw );
0254 Bool_t AddComment ( void* node, const char* comment );
0255
0256 void* GetParent( void* child);
0257 void* GetChild ( void* parent, const char* childname=nullptr );
0258 void* GetNextChild( void* prevchild, const char* childname=nullptr );
0259 const char* GetContent ( void* node );
0260 const char* GetName ( void* node );
0261
0262 TXMLEngine& xmlengine() { return *fXMLEngine; }
0263 int xmlenginebuffersize() { return fXMLBufferSize;}
0264 void SetXMLEngineBufferSize(int buffer) { fXMLBufferSize = buffer; }
0265 TXMLEngine* fXMLEngine;
0266
0267 TH1* GetCumulativeDist( TH1* h);
0268
0269 private:
0270
0271 int fXMLBufferSize = 10000000;
0272
0273 Double_t GetYMean_binX( const TH2& , Int_t bin_x );
0274
0275 };
0276
0277 Tools& gTools();
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 template <typename TRandomLike, typename UIntType = UInt_t, UIntType max_val = kMaxUInt>
0299 class RandomGenerator {
0300 public:
0301 using result_type = UIntType;
0302
0303 RandomGenerator(UIntType s = 0) { fRandom.SetSeed(s); }
0304
0305 static constexpr UIntType min() { return 0; }
0306 static constexpr UIntType max() { return max_val; }
0307
0308 void seed(UIntType s = 0) { fRandom.SetSeed(s); }
0309
0310 UIntType operator()() { return fRandom.Integer(max()); }
0311
0312 void discard(unsigned long long z)
0313 {
0314 double r;
0315 for (unsigned long long i = 0; i < z; ++i)
0316 r = fRandom.Rndm();
0317 (void) r;
0318 }
0319
0320 private:
0321 TRandomLike fRandom;
0322 };
0323
0324 }
0325
0326
0327
0328
0329 template<typename T> void TMVA::Tools::ReadAttr( void* node, const char* attrname, T& value )
0330 {
0331
0332 const char *val = xmlengine().GetAttr(node, attrname);
0333 if (!val) {
0334 const char *nodename = xmlengine().GetNodeName(node);
0335 Log() << kFATAL << "Trying to read non-existing attribute '" << attrname << "' from xml node '" << nodename << "'"
0336 << Endl;
0337 }
0338 std::stringstream s(val);
0339
0340 s >> value;
0341 }
0342
0343
0344
0345
0346 template<typename T>
0347 void TMVA::Tools::AddAttr( void* node, const char* attrname, const T& value, Int_t precision )
0348 {
0349 std::stringstream s;
0350 s.precision( precision );
0351 s << std::scientific << value;
0352 AddAttr( node, attrname, s.str().c_str() );
0353 }
0354
0355
0356
0357
0358 inline Double_t TMVA::Tools::ComputeVariance( Double_t sumx2, Double_t sumx, Int_t nx )
0359 {
0360 if (nx<2) return 0;
0361 return (sumx2 - ((sumx*sumx)/static_cast<Double_t>(nx)))/static_cast<Double_t>(nx-1);
0362 }
0363
0364 #endif