-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizeBins.C
100 lines (90 loc) · 2.96 KB
/
optimizeBins.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//File: optimizeBins.C
//Info: Takes input migration histo for a 1D variable and calculates bin edges with at least N% on the diagonal.
//Author: David Last [email protected]/[email protected]
//C++ includes
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <unordered_map>
#include <bitset>
#include <time.h>
#include <sys/stat.h>
//ROOT includes
#include "TInterpreter.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TH2F.h"
#include "THStack.h"
#include "TFile.h"
#include "TTree.h"
#include "TKey.h"
#include "TDirectory.h"
#include "TSystemDirectory.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TString.h"
#include "TLorentzVector.h"
#include "TVector3.h"
#include "TLegend.h"
#include "TMath.h"
#include "TColor.h"
#include "TParameter.h"
//PlotUtils includes??? Trying anything at this point...
#include "PlotUtils/MnvH2D.h"
using namespace std;
using namespace PlotUtils;
pair<double,double> combineSums(MnvH2D* h, int currBin, int nextBin, double currSum, double currTotal){
double retSum = currSum;
double retTotal = currTotal;
for (int iBin=1; iBin <= h->GetNbinsX(); ++iBin){
retTotal += h->GetBinContent(iBin, nextBin);
if (iBin < nextBin && iBin >= currBin){
retSum += h->GetBinContent(iBin, nextBin);
}
else if (iBin == nextBin){
for (int iBinY=currBin; iBinY <= nextBin; ++iBinY){
retSum += h->GetBinContent(iBin, iBinY);
}
}
}
pair<double,double> retPair = make_pair(retSum, retTotal);
return retPair;
}
void optimizeBins(TString inFileName, TString varName, double nPercent) {
TFile* inFile = new TFile(inFileName, "READ");
MnvH2D* h = (MnvH2D*)(inFile->Get(varName+"_migration")->Clone());
cout << "Total Y bins: " << h->GetNbinsY() << endl;
cout << "Total X bins: " << h->GetNbinsX() << endl;
pair<double,double> sums = make_pair(0.0, 0.0);
bool combineBins = false;
int lastBin = 1;
for (int iBinY = 1; iBinY <= h->GetNbinsY(); ++iBinY){
if (!combineBins){
sums = combineSums(h,iBinY,iBinY,0.0,0.0);
lastBin = iBinY;
}
else{
sums = combineSums(h, lastBin, iBinY, sums.first, sums.second);
}
// cout << "Current diagonal bin sum: " << sums.first << ", current row Sum: " << sums.second << endl;
double percent = (sums.second > 0.0) ? 100.0*(sums.first/sums.second) : 0.0;
//cout << "Percent: " << percent << endl;
if (percent >= nPercent){
combineBins = false;
cout << "Combine bin upper bound: " << h->GetYaxis()->GetBinUpEdge(iBinY) << ", with diagonal percent of row: " << percent <<", and total bin content: " << sums.first << endl;
}
else{
combineBins = true;
//cout << "Not done combining yet." << endl;
}
}
double percent = (sums.second > 0.0) ? 100.0*(sums.first/sums.second) : 0.0;
cout << "Final percent: " << percent << ", and total bin content: " << sums.first << endl;
delete h;
}