29 #ifndef ROOT_TMVA_Tools
30 #define ROOT_TMVA_Tools
44 #if __cplusplus > 199711L
92 static Tools& Instance();
93 static void DestroyInstance();
96 template <
typename T> Double_t Mean(Long64_t n,
const T *a,
const Double_t *w=0);
97 template <
typename Iterator,
typename WeightIterator> Double_t Mean ( Iterator first, Iterator last, WeightIterator w);
99 template <
typename T> Double_t RMS(Long64_t n,
const T *a,
const Double_t *w=0);
100 template <
typename Iterator,
typename WeightIterator> Double_t RMS(Iterator first, Iterator last, WeightIterator w);
104 void ComputeStat(
const std::vector<TMVA::Event*>&,
105 std::vector<Float_t>*,
106 Double_t&, Double_t&, Double_t&,
107 Double_t&, Double_t&, Double_t&, Int_t signalClass,
108 Bool_t norm = kFALSE );
111 inline Double_t ComputeVariance( Double_t sumx2, Double_t sumx, Int_t nx );
114 TH1* projNormTH1F( TTree* theTree,
const TString& theVarName,
115 const TString& name, Int_t nbins,
116 Double_t xmin, Double_t xmax,
const TString& cut );
119 Double_t NormHist( TH1* theHist, Double_t norm = 1.0 );
122 TList* ParseFormatLine( TString theString,
const char * sep =
":" );
125 std::vector<Int_t>* ParseANNOptionString( TString theOptions, Int_t nvar,
126 std::vector<Int_t>* nodes );
129 TMatrixD* GetSQRootMatrix( TMatrixDSym* symMat );
133 std::vector<TMatrixDSym*>* CalcCovarianceMatrices(
const std::vector<Event*>& events, Int_t maxCls, VariableTransformBase* transformBase=0 );
134 std::vector<TMatrixDSym*>* CalcCovarianceMatrices(
const std::vector<const Event*>& events, Int_t maxCls, VariableTransformBase* transformBase=0 );
138 const TMatrixD* GetCorrelationMatrix(
const TMatrixD* covMat );
141 Bool_t CheckSplines(
const TH1*,
const TSpline* );
144 Double_t NormVariable( Double_t x, Double_t xmin, Double_t xmax );
147 Double_t GetSeparation( TH1* S, TH1* B )
const;
148 Double_t GetSeparation(
const PDF& pdfS,
const PDF& pdfB )
const;
151 std::vector<Double_t> MVADiff( std::vector<Double_t>&, std::vector<Double_t>& );
152 void Scale( std::vector<Double_t>&, Double_t );
153 void Scale( std::vector<Float_t>&, Float_t );
157 void UsefulSortDescending( std::vector< std::vector<Double_t> >&, std::vector<TString>* vs = 0 );
158 void UsefulSortAscending ( std::vector< std::vector<Double_t> >&, std::vector<TString>* vs = 0 );
160 void UsefulSortDescending( std::vector<Double_t>& );
161 void UsefulSortAscending ( std::vector<Double_t>& );
163 Int_t GetIndexMaxElement ( std::vector<Double_t>& );
164 Int_t GetIndexMinElement ( std::vector<Double_t>& );
167 Bool_t ContainsRegularExpression(
const TString& s );
168 TString ReplaceRegularExpressions(
const TString& s,
const TString& replace =
"+" );
171 void FormattedOutput(
const std::vector<Double_t>&,
const std::vector<TString>&,
172 const TString titleVars,
const TString titleValues, MsgLogger& logger,
173 TString format =
"%+1.3f" );
174 void FormattedOutput(
const TMatrixD&,
const std::vector<TString>&, MsgLogger& logger );
175 void FormattedOutput(
const TMatrixD&,
const std::vector<TString>& vert,
const std::vector<TString>& horiz,
178 void WriteFloatArbitraryPrecision( Float_t val, std::ostream& os );
179 void ReadFloatArbitraryPrecision ( Float_t& val, std::istream& is );
182 TString GetXTitleWithUnit(
const TString& title,
const TString& unit );
183 TString GetYTitleWithUnit(
const TH1& h,
const TString& unit, Bool_t normalised );
187 Double_t GetMutualInformation(
const TH2F& );
191 Double_t GetCorrelationRatio(
const TH2F& );
192 TH2F* TransposeHist (
const TH2F& );
195 Bool_t CheckForSilentOption (
const TString& )
const;
196 Bool_t CheckForVerboseOption(
const TString& )
const;
199 const TString& Color(
const TString& );
202 enum EWelcomeMessage { kStandardWelcomeMsg = 1,
203 kIsometricWelcomeMsg,
209 kOriginalWelcomeMsgColor,
210 kOriginalWelcomeMsgBW };
213 enum ECitation { kPlainText = 1,
218 void TMVAWelcomeMessage();
219 void TMVAWelcomeMessage( MsgLogger& logger, EWelcomeMessage m = kStandardWelcomeMsg );
220 void TMVAVersionMessage( MsgLogger& logger );
221 void ROOTVersionMessage( MsgLogger& logger );
223 void TMVACitation( MsgLogger& logger, ECitation citType = kPlainText );
227 std::vector<TString> SplitString(
const TString& theOpt,
const char separator )
const;
230 const TString fRegexp;
231 mutable MsgLogger* fLogger;
232 MsgLogger& Log()
const {
return *fLogger; }
233 #if __cplusplus > 199711L
234 static std::atomic<Tools*> fgTools;
236 static Tools* fgTools;
241 TString StringFromInt ( Long_t i );
242 TString StringFromDouble ( Double_t d );
243 void WriteTMatrixDToXML (
void* node,
const char* name, TMatrixD* mat );
244 void WriteTVectorDToXML (
void* node,
const char* name, TVectorD* vec );
245 void ReadTMatrixDFromXML(
void* node,
const char* name, TMatrixD* mat );
246 void ReadTVectorDFromXML(
void* node,
const char* name, TVectorD* vec );
247 Bool_t HistoHasEquidistantBins(
const TH1& h);
249 Bool_t HasAttr (
void* node,
const char* attrname );
251 inline void ReadAttr (
void* node,
const char* , T& value );
252 void ReadAttr (
void* node,
const char* attrname, TString& value );
253 void ReadAttr(
void *node,
const char *,
float &value);
254 void ReadAttr(
void *node,
const char *,
int &value);
255 void ReadAttr(
void *node,
const char *,
short &value);
258 void AddAttr (
void* node,
const char* ,
const T& value, Int_t precision = 16 );
259 void AddAttr (
void* node,
const char* attrname,
const char* value );
260 void* AddChild (
void* parent,
const char* childname,
const char* content = 0,
bool isRootNode =
false );
261 Bool_t AddRawLine (
void* node,
const char * raw );
262 Bool_t AddComment (
void* node,
const char* comment );
264 void* GetParent(
void* child);
265 void* GetChild (
void* parent,
const char* childname=0 );
266 void* GetNextChild(
void* prevchild,
const char* childname=0 );
267 const char* GetContent (
void* node );
268 const char* GetName (
void* node );
270 TXMLEngine& xmlengine() {
return *fXMLEngine; }
271 int xmlenginebuffersize() {
return fXMLBufferSize;}
272 void SetXMLEngineBufferSize(
int buffer) { fXMLBufferSize = buffer; }
273 TXMLEngine* fXMLEngine;
275 TH1* GetCumulativeDist( TH1* h);
279 int fXMLBufferSize = 10000000;
281 Double_t GetYMean_binX(
const TH2& , Int_t bin_x );
306 template <
typename TRandomLike,
typename UIntType = UInt_t, UIntType max_val = kMaxUInt>
307 class RandomGenerator {
309 using result_type = UIntType;
311 RandomGenerator(UIntType s = 0) { fRandom.SetSeed(s); }
313 static constexpr UIntType min() {
return 0; }
314 static constexpr UIntType max() {
return max_val; }
316 void seed(UIntType s = 0) { fRandom.SetSeed(s); }
318 UIntType operator()() {
return fRandom.Integer(max()); }
320 void discard(
unsigned long long z)
323 for (
unsigned long long i = 0; i < z; ++i)
337 template<
typename T>
void TMVA::Tools::ReadAttr(
void* node,
const char* attrname, T& value )
340 const char *val = xmlengine().GetAttr(node, attrname);
342 const char *nodename = xmlengine().GetNodeName(node);
343 Log() << kFATAL <<
"Trying to read non-existing attribute '" << attrname <<
"' from xml node '" << nodename <<
"'"
346 std::stringstream s(val);
355 void TMVA::Tools::AddAttr(
void* node,
const char* attrname,
const T& value, Int_t precision )
358 s.precision( precision );
359 s << std::scientific << value;
360 AddAttr( node, attrname, s.str().c_str() );
366 inline Double_t TMVA::Tools::ComputeVariance( Double_t sumx2, Double_t sumx, Int_t nx )
369 return (sumx2 - ((sumx*sumx)/static_cast<Double_t>(nx)))/
static_cast<Double_t
>(nx-1);