PandaRoot
PndMvaTools.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 /* ********************************************
14  * MVA Tools and function definitions. *
15  * Author: M.Babai@rug.nl *
16  * Version: *
17  * License: *
18  * *******************************************
19  */
20 #pragma once
21 #ifndef PND_MVA_TOOLS_H
22 #define PND_MVA_TOOLS_H
23 
24 // C++
25 #include <cstdlib>
26 #include <iostream>
27 #include <fstream>
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 #include <limits>
32 
33 // ROOT
34 #include "TFile.h"
35 #include "TTree.h"
36 
43  // public:
44  // Constructors
45  explicit ClassifierOutPuts() : realLabel("ALABEL"), givenLabel("NOLABEL"), sgValue(0.00), bgValue(0.00), mom(0.00){};
46 
53  explicit ClassifierOutPuts(std::string const &Rlabel, std::string const &Glabel, float sgVal, float bgVal, float p)
54  : realLabel(Rlabel), givenLabel(Glabel), sgValue(sgVal), bgValue(bgVal), mom(p){};
55 
56  // Destructor
57  virtual ~ClassifierOutPuts(){};
58 
59  // Copy Const
61 
62  // Operators.
64  {
65  // check for self-assignment
66  if (this != &ot) {
67  // Copy (deep)
68  this->realLabel = ot.realLabel;
69  this->givenLabel = ot.givenLabel;
70  this->sgValue = ot.sgValue;
71  this->bgValue = ot.bgValue;
72  this->mom = ot.mom;
73  }
74  return (*this);
75  };
76 
77  inline bool operator>(ClassifierOutPuts const &ot) const { return (this->sgValue > ot.sgValue); };
78 
79  inline bool operator<(ClassifierOutPuts const &ot) const { return (this->sgValue < ot.sgValue); };
80 
81  // Variables
82  std::string realLabel; // Original label
83  std::string givenLabel; // Given label
84  float sgValue; // Classifier output for label signal
85  float bgValue; // Classifier output for label background
86  float mom; // Momentum (reco.)
87 
88  // protected:
89  private:
90  //==
91  inline bool operator==(ClassifierOutPuts const &ot) const;
92 };
93 
97 struct ROCPoints {
98  // Constructors
99  explicit ROCPoints() : FP_rate(0.0), TP_rate(0.0), TN_rate(0.0), FN_rate(0.0), fp(0), tp(0), fn(0), tn(0), thr(0.0){};
111  explicit ROCPoints(float const fpr, float const tpr, float const tnr, float const fnr, size_t const nfp, size_t const ntp, size_t const nfn, size_t const ntn, float const curThr)
112  : FP_rate(fpr), TP_rate(tpr), TN_rate(tnr), FN_rate(fnr), fp(nfp), tp(ntp), fn(nfn), tn(ntn), thr(curThr){};
113 
114  // Destructor
115  virtual ~ROCPoints(){};
116 
117  // Copy Const
118  ROCPoints(ROCPoints const &ot) : FP_rate(ot.FP_rate), TP_rate(ot.TP_rate), TN_rate(ot.TN_rate), FN_rate(ot.FN_rate), fp(ot.fp), tp(ot.tp), fn(ot.fn), tn(ot.tn), thr(ot.thr){};
119 
120  // Operators.
122  {
123  // check for self-assignment
124  if (this != &ot) {
125  this->FP_rate = ot.FP_rate;
126  this->TP_rate = ot.TP_rate;
127  this->TN_rate = ot.TN_rate;
128  this->FN_rate = ot.FN_rate;
129  this->fp = ot.fp;
130  this->tp = ot.tp;
131  this->fn = ot.fn;
132  this->tn = ot.tn;
133  this->thr = ot.thr;
134  }
135  return (*this);
136  };
137 
138  // Variables
139  float FP_rate; // False positief rate
140  float TP_rate; // True positief rate
141  float TN_rate; // True negatief rate
142  float FN_rate; // False negatief rate
143  size_t fp; // False positief count
144  size_t tp; // True positief count
145  size_t fn; // False negatief
146  size_t tn; // True negatief
147  float thr; // Treshold value
148 
149  // protected:
150 
151  private:
152  bool operator==(ROCPoints const &ot) const;
153  bool operator>(ROCPoints const &ot) const;
154  bool operator<(ROCPoints const &ot) const;
155 };
156 
157 //____________ Functions and modifiers.
158 //______________________________________________________________
170 void Produce_ROC(std::vector<ClassifierOutPuts> &input, std::string const &SigName, std::string const &BgName, size_t sigCnt, size_t bgCnt, std::vector<ROCPoints> &Roc);
175 void print(std::vector<ClassifierOutPuts> const &OutPutList);
176 
181 void print(std::map<std::string, float> const &ClsMapOut);
182 
190 std::map<std::string, size_t> *readEvents(char const *infile, std::vector<std::string> const &varNames, std::vector<std::string> const &classNames,
191  std::vector<std::pair<std::string, std::vector<float> *>> &Outcontainer);
192 
197 void printRoc(std::vector<ROCPoints> const &RocList);
198 
204 void WriteRocToFile(std::string const &FileName, std::vector<ROCPoints> const &RocList);
205 
206 //____________________ C style function declarations
207 #ifdef __cplusplus
208 extern "C" {
209 #endif
210 // Place your C code and/or C headers here.
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif
216 // interface definition
217 /*
218  Test for GCC > 3.2.0
219  #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 0) ) )
220 */
ClassifierOutPuts(ClassifierOutPuts const &ot)
Definition: PndMvaTools.h:60
size_t fn
Definition: PndMvaTools.h:145
ROCPoints(float const fpr, float const tpr, float const tnr, float const fnr, size_t const nfp, size_t const ntp, size_t const nfn, size_t const ntn, float const curThr)
Definition: PndMvaTools.h:111
std::map< std::string, size_t > * readEvents(char const *infile, std::vector< std::string > const &varNames, std::vector< std::string > const &classNames, std::vector< std::pair< std::string, std::vector< float > *>> &Outcontainer)
virtual ~ClassifierOutPuts()
Definition: PndMvaTools.h:57
ClassifierOutPuts(std::string const &Rlabel, std::string const &Glabel, float sgVal, float bgVal, float p)
Definition: PndMvaTools.h:53
size_t tn
Definition: PndMvaTools.h:146
size_t fp
Definition: PndMvaTools.h:143
float thr
Definition: PndMvaTools.h:147
float TN_rate
Definition: PndMvaTools.h:141
float FN_rate
Definition: PndMvaTools.h:142
ClassifierOutPuts & operator=(ClassifierOutPuts const &ot)
Definition: PndMvaTools.h:63
float TP_rate
Definition: PndMvaTools.h:140
size_t tp
Definition: PndMvaTools.h:144
void WriteRocToFile(std::string const &FileName, std::vector< ROCPoints > const &RocList)
float FP_rate
Definition: PndMvaTools.h:136
bool operator<(ClassifierOutPuts const &ot) const
Definition: PndMvaTools.h:79
virtual ~ROCPoints()
Definition: PndMvaTools.h:115
std::string givenLabel
Definition: PndMvaTools.h:83
std::string realLabel
Definition: PndMvaTools.h:79
void Produce_ROC(std::vector< ClassifierOutPuts > &input, std::string const &SigName, std::string const &BgName, size_t sigCnt, size_t bgCnt, std::vector< ROCPoints > &Roc)
void printRoc(std::vector< ROCPoints > const &RocList)
ROCPoints(ROCPoints const &ot)
Definition: PndMvaTools.h:118
void print(std::vector< ClassifierOutPuts > const &OutPutList)
bool operator>(ClassifierOutPuts const &ot) const
Definition: PndMvaTools.h:77
ROCPoints & operator=(ROCPoints const &ot)
Definition: PndMvaTools.h:121
friend F32vec4 operator==(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:104