Skip to content

Commit c18d805

Browse files
authored
Merge pull request #119 from mfmceneaney/add_bootstrapping
Add bootstrapping
2 parents e5947c0 + 7f5e537 commit c18d805

11 files changed

Lines changed: 611 additions & 111 deletions

saga/include/analysis.h

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@ vector<double> fitAsym(
14991499
* @param workspace_title Title of workspace in which to work
15001500
* @param dataset_name Dataset name
15011501
* @param dataset_title Dataset title
1502+
* @param weight_name Name of weight variable, ignored if empty
15021503
* @param categories_as_float List of category variables to include as asymmetry fit variables in dataset
15031504
* @param helicity Name of helicity variable
15041505
* @param helicity_states Map of state names to helicity values
@@ -1591,6 +1592,7 @@ void getKinBinnedAsym(
15911592
// parameters passed to data::createDataset()
15921593
string dataset_name,
15931594
string dataset_title,
1595+
string weight_name,
15941596
vector<string> categories_as_float,
15951597
string helicity,
15961598
map<string,int> helicity_states,
@@ -1820,6 +1822,7 @@ void getKinBinnedAsym(
18201822
ws,
18211823
dataset_name,
18221824
dataset_title,
1825+
weight_name,
18231826
categories_as_float,
18241827
helicity,
18251828
helicity_states,
@@ -1901,6 +1904,7 @@ void getKinBinnedAsym(
19011904
saga::signal::applySPlot(
19021905
ws,
19031906
dataset_name,
1907+
weight_name,
19041908
Form("%s_%s",massfit_sgYield_name.c_str(),scheme_binid.c_str()),//NOTE: getGenAsymPdf() renames these variables to ensure workspace uniqueness
19051909
Form("%s_%s",massfit_bgYield_name.c_str(),scheme_binid.c_str()),
19061910
Form("%s_%s",massfit_pdf_name.c_str(),scheme_binid.c_str()),
@@ -1976,6 +1980,7 @@ void getKinBinnedAsym(
19761980
ws_sg, //NOTE: Use separate sideband workspace for dataset, variable, and pdf name uniqueness.
19771981
dataset_name,
19781982
dataset_title,
1983+
weight_name,
19791984
categories_as_float,
19801985
helicity,
19811986
helicity_states,
@@ -2050,6 +2055,7 @@ void getKinBinnedAsym(
20502055
ws_sb, //NOTE: Use separate sideband workspace for dataset, variable, and pdf name uniqueness.
20512056
dataset_name,
20522057
dataset_title,
2058+
weight_name,
20532059
categories_as_float,
20542060
helicity,
20552061
helicity_states,
@@ -2113,49 +2119,47 @@ void getKinBinnedAsym(
21132119
LOG_DEBUG(Form("[%s]: Extracting results...", method_name.c_str()));
21142120
int nbinvars = binvars.size();
21152121
int nparams = asymfitpar_inits.size();
2116-
double xs[nbinvars];
2117-
double exs[nbinvars];
2122+
vector<double> xs;
2123+
vector<double> exs;
21182124
int count;
21192125

2120-
double ys[nparams];
2121-
double eys[nparams];
2122-
double ys_sb[nparams];
2123-
double eys_sb[nparams];
2124-
double depols[nparams];
2125-
double edepols[nparams];
2126-
double ys_corrected[nparams];
2127-
double eys_corrected[nparams];
2128-
double rawasyms[(const int)rawasymvars.size()];
2129-
double rawasymerrs[(const int)rawasymvars.size()];
2126+
vector<double> ys;
2127+
vector<double> eys;
2128+
vector<double> ys_sb;
2129+
vector<double> eys_sb;
2130+
vector<double> depols;
2131+
vector<double> edepols;
2132+
vector<double> rawasyms;
2133+
vector<double> rawasymerrs;
21302134

21312135
// Get asymmetry fit bin data
21322136
int k = 0;
21332137
count = (int)asymfit_result[k++];
21342138
for (int idx=0; idx<binvars.size(); idx++) {
2135-
xs[idx] = asymfit_result[k++];
2136-
exs[idx] = asymfit_result[k++];
2139+
xs.push_back(asymfit_result[k++]);
2140+
exs.push_back(asymfit_result[k++]);
21372141
}
21382142
for (int idx=0; idx<depolvars.size(); idx++) {
2139-
depols[idx] = asymfit_result[k++];
2140-
edepols[idx] = asymfit_result[k++];
2143+
depols.push_back(asymfit_result[k++]);
2144+
edepols.push_back(asymfit_result[k++]);
21412145
}
21422146
for (int idx=0; idx<rawasymvars.size(); idx++) {
2143-
rawasyms[idx] = asymfit_result[k++];
2144-
rawasymerrs[idx] = asymfit_result[k++];
2147+
rawasyms.push_back(asymfit_result[k++]);
2148+
rawasymerrs.push_back(asymfit_result[k++]);
21452149
}
21462150
for (int idx=0; idx<nparams; idx++) {
2147-
ys[idx] = asymfit_result[k++];
2148-
eys[idx] = asymfit_result[k++];
2151+
ys.push_back(asymfit_result[k++]);
2152+
eys.push_back(asymfit_result[k++]);
21492153
}
21502154
if (use_binned_sb_bgfracs) {
21512155
for (int idx=0; idx<nparams; idx++) {
2152-
ys_sb[idx] = asymfit_result[k++];
2153-
eys_sb[idx] = asymfit_result[k++];
2156+
ys_sb.push_back(asymfit_result[k++]);
2157+
eys_sb.push_back(asymfit_result[k++]);
21542158
}
21552159
}
21562160

2157-
21582161
// Get mass fit bin data
2162+
LOG_DEBUG(Form("[%s]: Extracting mass fit results...", method_name.c_str()));
21592163
double int_sg_pdf_val;
21602164
double int_sg_pdf_err;
21612165
double int_bg_pdf_val;
@@ -2178,7 +2182,7 @@ void getKinBinnedAsym(
21782182
if (massfit_result.size()>0) {
21792183

21802184
// Start counter
2181-
int m = 1; //NOTE: Ignore count which should first entry
2185+
int m = 1; //NOTE: Ignore count which should be first entry
21822186

21832187
// Add signal region integration values
21842188
int_sg_pdf_val = massfit_result[m++];
@@ -2223,7 +2227,7 @@ void getKinBinnedAsym(
22232227
double epsilon, epsilon_err;
22242228
if (use_sb_subtraction) {
22252229
LOG_DEBUG(Form("[%s]: Applying sideband subtraction...", method_name.c_str()));
2226-
int k2 = 1 + binvars.size() + depolvars.size();
2230+
int k2 = 1 + 2 * (binvars.size() + depolvars.size() + rawasymvars.size());
22272231
epsilon = eps_bg_pdf;
22282232
epsilon_err = eps_bg_pdf_err;
22292233
for (int idx=0; idx<nparams; idx++) {
@@ -2238,13 +2242,8 @@ void getKinBinnedAsym(
22382242
if (use_average_depol) {
22392243
LOG_DEBUG(Form("[%s]: Dividing out depolarization factors...", method_name.c_str()));
22402244
for (int idx=0; idx<nparams; idx++) {
2241-
ys_corrected[idx] = ys[idx] / depols[idx];
2242-
eys_corrected[idx] = eys[idx] / depols[idx];
2243-
}
2244-
} else {
2245-
for (int idx=0; idx<nparams; idx++) {
2246-
ys_corrected[idx] = ys[idx];
2247-
eys_corrected[idx] = eys[idx];
2245+
ys[idx] = ys[idx] / depols[idx];
2246+
eys[idx] = eys[idx] / depols[idx];
22482247
}
22492248
}
22502249

@@ -2260,8 +2259,6 @@ void getKinBinnedAsym(
22602259
out << " ys_sb["<< idx <<"] = " << ys_sb[idx] << "\n";
22612260
out << " eys_sb["<< idx <<"] = " << eys_sb[idx] << "\n";
22622261
}
2263-
out << " ys_corrected["<< idx <<"] = " << ys_corrected[idx] << "\n";
2264-
out << " eys_corrected["<< idx <<"] = " << eys_corrected[idx] << "\n";
22652262
}
22662263
out << "---------------------------\n";
22672264

@@ -2283,8 +2280,8 @@ void getKinBinnedAsym(
22832280
csvout << rawasymerrs[rr] << csv_separator.c_str();
22842281
}
22852282
for (int aa=0; aa<nparams; aa++) {
2286-
csvout << ys_corrected[aa] << csv_separator.c_str();//NOTE: This is the default naming from analysis::fitAsym()
2287-
csvout << eys_corrected[aa];
2283+
csvout << ys[aa] << csv_separator.c_str();//NOTE: This is the default naming from analysis::fitAsym()
2284+
csvout << eys[aa];
22882285
if (aa<nparams-1 || single_massfit || use_binned_sb_bgfracs) csvout << csv_separator.c_str();
22892286
else csvout << endl;//NOTE: IMPORTANT!
22902287
}

saga/include/bins.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// Local Includes
2020
#include <log.h>
2121
#include <util.h>
22+
#include <data.h>
2223

2324
#pragma once
2425

@@ -753,13 +754,15 @@ map<string,map<int,string>> getBinCutsMapBatch(
753754
* @param bincuts Map of unique integer bin identifiers to bin cuts
754755
* @param binvars List of bin variable names
755756
* @param mc_suffix Suffix for forming the truth variable names
757+
* @param weight_name Name of the weight variable, ignored if empty
756758
*/
757759
void getBinMigration(
758760
ROOT::RDF::RInterface<ROOT::Detail::RDF::RJittedFilter, void> frame,
759761
string scheme_name,
760762
map<int,string> bincuts,
761763
vector<string> binvars,
762-
string mc_suffix = "_mc"
764+
string mc_suffix = "_mc",
765+
string weight_name = ""
763766
) {
764767

765768
string method_name = "getBinMigration";
@@ -801,7 +804,7 @@ void getBinMigration(
801804
// Get generated count
802805
LOG_DEBUG(Form("[%s]: Filtering frame with generated bin cut %s", method_name.c_str(), bincut_gen.c_str()));
803806
auto frame_filtered = frame.Filter(bincut_gen.c_str());
804-
double count_gen = (double)*frame_filtered.Count();
807+
double count_gen = saga::data::get_weighted_count<double>(frame_filtered,weight_name);
805808

806809
// Loop reconstructed bins
807810
for (auto it_rec = bincuts.begin(); it_rec != bincuts.end(); ++it_rec) {
@@ -812,7 +815,8 @@ void getBinMigration(
812815

813816
// Get reconstructed count
814817
LOG_DEBUG(Form("[%s]: Filtering frame with reconstructed bin cut %s", method_name.c_str(), bincut_rec.c_str()));
815-
double count_rec = (double)*frame_filtered.Filter(bincut_rec.c_str()).Count();
818+
auto frame_filtered_bincut_rec = frame_filtered.Filter(bincut_rec.c_str());
819+
double count_rec = saga::data::get_weighted_count<double>(frame_filtered_bincut_rec,weight_name);
816820

817821
// Compute bin migration fraction
818822
double mig = count_rec / count_gen; //NOTE: f[i->j] = [# generated in i AND reconstructed in j] / [# generated in bin i]
@@ -840,12 +844,14 @@ void getBinMigration(
840844
* @param scheme_name Bin scheme name, csv file will be named `<scheme_name>_kinematics.csv`
841845
* @param bincuts Map of unique integer bin identifiers to bin cuts
842846
* @param kinvars List of kinematic variable names
847+
* @param weight_name Name of the weight variable, ignored if empty
843848
*/
844849
void getBinKinematics(
845850
ROOT::RDF::RInterface<ROOT::Detail::RDF::RJittedFilter, void> frame,
846851
string scheme_name,
847852
map<int,string> bincuts,
848-
vector<string> kinvars
853+
vector<string> kinvars,
854+
string weight_name
849855
) {
850856

851857
string method_name = "getBinKinematics";
@@ -881,16 +887,16 @@ void getBinKinematics(
881887
auto frame_filtered = frame.Filter(bincut.c_str());
882888

883889
// Get bin count
884-
int count = (int)*frame_filtered.Count();
890+
int count = saga::data::get_weighted_count<int>(frame_filtered,weight_name);
885891

886892
// Set CSV column data
887893
// COLS: bin,{kinvar,kinvar_err}
888894
LOG_DEBUG(Form("[%s]: Writing csv data...", method_name.c_str()));
889895
csvout << bin << csv_separator.c_str();
890896
csvout << count; if (kinvars.size()>0) { csvout << csv_separator.c_str(); }
891897
for (int idx=0; idx<kinvars.size(); idx++) {
892-
double binvar_mean = (double)*frame_filtered.Mean(kinvars[idx].c_str());
893-
double binvar_err = (double)*frame_filtered.StdDev(kinvars[idx].c_str());
898+
double binvar_mean = saga::data::get_weighted_mean<double>(frame_filtered,kinvars[idx],weight_name);
899+
double binvar_err = saga::data::get_weighted_stddev<double>(frame_filtered,kinvars[idx],weight_name,binvar_mean);
894900
csvout << binvar_mean << csv_separator.c_str();
895901
csvout << binvar_err;
896902
if (idx<kinvars.size()-1) csvout << csv_separator.c_str();

0 commit comments

Comments
 (0)