40auto tupleSort = [](std::tuple<Float_t, Float_t, Bool_t> _a, std::tuple<Float_t, Float_t, Bool_t> _b) {
41 return std::get<0>(_a) < std::get<0>(_b);
54 const std::vector<Float_t> &mvaWeights)
57 assert(mvaValues.size() == mvaTargets.size());
58 assert(mvaValues.size() == mvaWeights.size());
60 for (
UInt_t i = 0; i < mvaValues.size(); i++) {
61 fMva.emplace_back(mvaValues[i], mvaWeights[i], mvaTargets[i]);
73 assert(mvaValues.size() == mvaTargets.size());
75 for (
UInt_t i = 0; i < mvaValues.size(); i++) {
76 fMva.emplace_back(mvaValues[i], 1, mvaTargets[i]);
88 for (
UInt_t i = 0; i < mvaSignal.size(); i++) {
89 fMva.emplace_back(mvaSignal[i], 1,
kTRUE);
92 for (
UInt_t i = 0; i < mvaBackground.size(); i++) {
93 fMva.emplace_back(mvaBackground[i], 1,
kFALSE);
103 const std::vector<Float_t> &mvaSignalWeights,
const std::vector<Float_t> &mvaBackgroundWeights)
106 assert(mvaSignal.size() == mvaSignalWeights.size());
107 assert(mvaBackground.size() == mvaBackgroundWeights.size());
109 for (
UInt_t i = 0; i < mvaSignal.size(); i++) {
110 fMva.emplace_back(mvaSignal[i], mvaSignalWeights[i],
kTRUE);
113 for (
UInt_t i = 0; i < mvaBackground.size(); i++) {
114 fMva.emplace_back(mvaBackground[i], mvaBackgroundWeights[i],
kFALSE);
125 if(fGraph)
delete fGraph;
140 if (num_points <= 2) {
144 std::vector<Double_t> specificity_vector;
145 std::vector<Double_t> true_negatives;
146 specificity_vector.reserve(fMva.size());
147 true_negatives.reserve(fMva.size());
150 for (
auto &ev : fMva) {
152 auto weight = std::get<1>(ev);
153 auto isSignal = std::get<2>(ev);
155 true_negatives_sum += weight * (!isSignal ? 1. : 0.);
156 true_negatives.push_back(true_negatives_sum);
159 specificity_vector.push_back(0.0);
160 Double_t total_background = true_negatives_sum;
161 for (
auto &tn : true_negatives) {
163 (total_background <= std::numeric_limits<Double_t>::min()) ? (0.0) : (tn / total_background);
164 specificity_vector.push_back(specificity);
166 specificity_vector.push_back(1.0);
168 return specificity_vector;
176 if (num_points <= 2) {
180 std::vector<Double_t> sensitivity_vector;
181 std::vector<Double_t> true_positives;
182 sensitivity_vector.reserve(fMva.size());
183 true_positives.reserve(fMva.size());
186 for (
auto it = fMva.rbegin(); it != fMva.rend(); ++it) {
188 auto weight = std::get<1>(*it);
189 auto isSignal = std::get<2>(*it);
191 true_positives_sum += weight * (isSignal);
192 true_positives.push_back(true_positives_sum);
194 std::reverse(true_positives.begin(), true_positives.end());
196 sensitivity_vector.push_back(1.0);
197 Double_t total_signal = true_positives_sum;
198 for (
auto &tp : true_positives) {
199 Double_t sensitivity = (total_signal <= std::numeric_limits<Double_t>::min()) ? (0.0) : (tp / total_signal);
200 sensitivity_vector.push_back(sensitivity);
202 sensitivity_vector.push_back(0.0);
204 return sensitivity_vector;
219 assert(0.0 <= effB && effB <= 1.0);
221 auto effS_vec = ComputeSensitivity(num_points);
222 auto effB_vec = ComputeSpecificity(num_points);
225 auto complement = [](
Double_t x) {
return 1 -
x; };
226 std::transform(effB_vec.begin(), effB_vec.end(), effB_vec.begin(), complement);
229 std::reverse(effS_vec.begin(), effS_vec.end());
230 std::reverse(effB_vec.begin(), effB_vec.end());
236 return rocSpline.
Eval(effB);
250 auto sensitivity = ComputeSensitivity(num_points);
251 auto specificity = ComputeSpecificity(num_points);
254 for (
UInt_t i = 0; i < sensitivity.size() - 1; i++) {
256 Double_t currFnr = 1 - sensitivity[i];
257 Double_t nextFnr = 1 - sensitivity[i + 1];
259 integral += 0.5 * (nextFnr - currFnr) * (specificity[i] + specificity[i + 1]);
276 if (fGraph !=
nullptr) {
280 auto sensitivity = ComputeSensitivity(num_points);
281 auto specificity = ComputeSpecificity(num_points);
283 fGraph =
new TGraph(sensitivity.size(), &sensitivity[0], &specificity[0]);
A TGraph is an object made of two arrays X and Y with npoints each.
ostringstream derivative to redirect and format output
std::vector< Double_t > ComputeSpecificity(const UInt_t num_points)
ROCCurve(const std::vector< std::tuple< Float_t, Float_t, Bool_t > > &mvas)
Double_t GetEffSForEffB(Double_t effB, const UInt_t num_points=41)
Calculate the signal efficiency (sensitivity) for a given background efficiency (sensitivity).
std::vector< Double_t > ComputeSensitivity(const UInt_t num_points)
Double_t GetROCIntegral(const UInt_t points=41)
Calculates the ROC integral (AUC)
std::vector< std::tuple< Float_t, Float_t, Bool_t > > fMva
TGraph * GetROCCurve(const UInt_t points=100)
Returns a new TGraph containing the ROC curve.
Linear interpolation of TGraph.
virtual Double_t Eval(Double_t x) const
returns linearly interpolated TGraph entry around x
create variable transformations
void mvas(TString dataset, TString fin="TMVA.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)