Skip to content

Commit

Permalink
Switch to simplified set-generating methods
Browse files Browse the repository at this point in the history
 - New method doesn't require a template parameter to be specified (the type is deduced automatically)
 - Add SetFromAll method
  • Loading branch information
ajgilbert committed Apr 6, 2015
1 parent 4b821cb commit 7a7c716
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 87 deletions.
2 changes: 1 addition & 1 deletion CombineHarvester/CombinePdfs/src/MorphFunctions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void BuildRooMorphing(RooWorkspace& ws, CombineHarvester& cb,
CombineHarvester cb_bp =
std::move(cb.cp().bin({bin}).process({process}));

vector<string> masses_all = Set2Vec(cb_bp.GenerateSetFromProcs<string>(
vector<string> masses_all = Set2Vec(cb_bp.SetFromProcs(
std::mem_fn(&ch::Process::mass)));

std::sort(masses_all.begin(), masses_all.end(),
Expand Down
9 changes: 2 additions & 7 deletions CombineHarvester/CombinePdfs/test/ParametricMSSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ int main() {
std::cout << " done\n";

// cb.era({"8TeV"}).bin_id({8});
set<string> lm_bins =
cb.GenerateSetFromObs<string>(mem_fn(&ch::Observation::bin));
set<string> lm_bins = cb.SetFromObs(mem_fn(&ch::Observation::bin));

if (create_asimov) {
for (auto const& b : lm_bins) {
Expand Down Expand Up @@ -140,11 +139,7 @@ int main() {
in->set_bin(in->bin() + "_hm");
});

set<string> hm_bins =
cb_hm.GenerateSetFromObs<string>(mem_fn(&ch::Observation::bin));



set<string> hm_bins = cb_hm.SetFromObs(mem_fn(&ch::Observation::bin));

cb.cp().bin_id({8}).VariableRebin(
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
Expand Down
105 changes: 68 additions & 37 deletions CombineHarvester/CombineTools/interface/CombineHarvester.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,6 @@ class CombineHarvester {
*/
/**@{*/
// Set generation
template <typename T>
std::set<T> GenerateSetFromProcs(
std::function<T(ch::Process const*)> func);

template<typename T>
std::set<T> GenerateSetFromObs(
std::function<T(ch::Observation const*)> func);

template <typename T>
std::set<T> GenerateSetFromSysts(
std::function<T(ch::Systematic const*)> func);

std::set<std::string> bin_set();
std::set<int> bin_id_set();
std::set<std::string> process_set();
Expand All @@ -185,18 +173,55 @@ class CombineHarvester {
std::set<std::string> mass_set();
std::set<std::string> syst_name_set();
std::set<std::string> syst_type_set();
/**@}*/

// An alternative way to do the set generation
// template<typename T>
// T unwind(T const& x) { return x; }
/**
* Fill an std::set with the return values from an arbitrary function
*
* This method will loop through all ch::Observation, ch::Process and
* ch::Systematic entries and call the user-supplied function `func`. The
* return value is then inserted into the set.
*
* @tparam T A function (or other callable) that must have a single
* `ch::Object const*` argument.
* @tparam R The return type of the function, which is deduced by using
* `std::result_of`, then `std::decay`. The latter is needed to handle
* functions that return by reference, i.e. turning a type R& into a type R.
*/
template <typename T,
typename R = typename std::decay<
typename std::result_of<T(Object const*)>::type>::type>
std::set<R> SetFromAll(T func);

/**
* Fill an std::set using only the Observation entries
*
* \sa SetFromAll
*/
template <typename T,
typename R = typename std::decay<
typename std::result_of<T(Observation const*)>::type>::type>
std::set<R> SetFromObs(T func);

/**
* Fill an std::set using only the Process entries
*
* \sa SetFromAll
*/
template <typename T,
typename R = typename std::decay<
typename std::result_of<T(Process const*)>::type>::type>
std::set<R> SetFromProcs(T func);

// template<typename T>
// auto SetFromProcs(T func) -> std::set<decltype(unwind(func(nullptr)))> {
// std::set<decltype(unwind(func(nullptr)))> ret;
// for (auto const& item : procs_) ret.insert(func(item.get()));
// return ret;
// };
/**
* Fill an std::set using only the Systematic entries
*
* \sa SetFromAll
*/
template <typename T,
typename R = typename std::decay<
typename std::result_of<T(Systematic const*)>::type>::type>
std::set<R> SetFromSysts(T func);
/**@}*/

/**
* \name Modification
Expand Down Expand Up @@ -446,29 +471,35 @@ void FillHistMappings(std::vector<HistMapping> & mappings);
// ---------------------------------------------------------------
// Template method implementation
// ---------------------------------------------------------------
template<typename T>
std::set<T> CombineHarvester::GenerateSetFromProcs(
std::function<T (ch::Process const*)> func) {
std::set<T> ret;
template <typename T, typename R>
std::set<R> CombineHarvester::SetFromAll(T func) {
std::set<R> ret;
for (auto const& item : obs_) ret.insert(func(item.get()));
for (auto const& item : procs_) ret.insert(func(item.get()));
for (auto const& item : systs_) ret.insert(func(item.get()));
return ret;
}
};

template<typename T>
std::set<T> CombineHarvester::GenerateSetFromObs(
std::function<T (ch::Observation const*)> func) {
std::set<T> ret;
template <typename T, typename R>
std::set<R> CombineHarvester::SetFromObs(T func) {
std::set<R> ret;
for (auto const& item : obs_) ret.insert(func(item.get()));
return ret;
}
};

template <typename T, typename R>
std::set<R> CombineHarvester::SetFromProcs(T func) {
std::set<R> ret;
for (auto const& item : procs_) ret.insert(func(item.get()));
return ret;
};

template<typename T>
std::set<T> CombineHarvester::GenerateSetFromSysts(
std::function<T (ch::Systematic const*)> func) {
std::set<T> ret;
template <typename T, typename R>
std::set<R> CombineHarvester::SetFromSysts(T func) {
std::set<R> ret;
for (auto const& item : systs_) ret.insert(func(item.get()));
return ret;
}
};

template<typename Function>
void CombineHarvester::ForEachObj(Function func) {
Expand Down
16 changes: 6 additions & 10 deletions CombineHarvester/CombineTools/src/CombineHarvester_Datacards.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ void CombineHarvester::FillHistMappings(std::vector<HistMapping> & mappings) {

CombineHarvester ch_signals =
std::move(this->cp().bin({bin}).signals().histograms());
auto sig_proc_set = ch_signals.GenerateSetFromProcs<std::string>(
std::mem_fn(&ch::Process::process));
auto sig_proc_set =
ch_signals.SetFromProcs(std::mem_fn(&ch::Process::process));
for (auto sig_proc : sig_proc_set) {
mappings.push_back({sig_proc, bin, nullptr,
bin + "/" + sig_proc + "$MASS",
Expand Down Expand Up @@ -443,12 +443,9 @@ void CombineHarvester::WriteDatacard(std::string const& name,

std::string dashes(80, '-');

auto bin_set =
this->GenerateSetFromObs<std::string>(std::mem_fn(&ch::Observation::bin));
auto proc_set =
this->GenerateSetFromProcs<std::string>(std::mem_fn(&ch::Process::process));
auto sys_set =
this->GenerateSetFromSysts<std::string>(std::mem_fn(&ch::Systematic::name));
auto bin_set = this->SetFromObs(std::mem_fn(&ch::Observation::bin));
auto proc_set = this->SetFromProcs(std::mem_fn(&ch::Process::process));
auto sys_set = this->SetFromSysts(std::mem_fn(&ch::Systematic::name));
txt_file << "imax " << bin_set.size()
<< " number of bins\n";
txt_file << "jmax " << proc_set.size() - 1
Expand All @@ -460,8 +457,7 @@ void CombineHarvester::WriteDatacard(std::string const& name,
std::vector<HistMapping> mappings;
FillHistMappings(mappings);

auto bins =
this->GenerateSetFromObs<std::string>(std::mem_fn(&ch::Observation::bin));
auto bins = this->SetFromObs(std::mem_fn(&ch::Observation::bin));

auto proc_sys_map = this->GenerateProcSystMap();

Expand Down
44 changes: 22 additions & 22 deletions CombineHarvester/CombineTools/src/CombineHarvester_Filters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,43 +130,43 @@ CombineHarvester & CombineHarvester::data() {

std::set<std::string> CombineHarvester::bin_set() {
std::set<std::string> result =
this->GenerateSetFromObs<std::string>(std::mem_fn(&ch::Observation::bin));
this->SetFromObs(std::mem_fn(&ch::Observation::bin));
std::set<std::string> result2 =
this->GenerateSetFromProcs<std::string>(std::mem_fn(&ch::Process::bin));
this->SetFromProcs(std::mem_fn(&ch::Process::bin));
std::set<std::string> result3 =
this->GenerateSetFromSysts<std::string>(std::mem_fn(&ch::Systematic::bin));
this->SetFromSysts(std::mem_fn(&ch::Systematic::bin));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
return result;
}

std::set<int> CombineHarvester::bin_id_set() {
std::set<int> result =
this->GenerateSetFromObs<int>(std::mem_fn(&ch::Observation::bin_id));
this->SetFromObs(std::mem_fn(&ch::Observation::bin_id));
std::set<int> result2 =
this->GenerateSetFromProcs<int>(std::mem_fn(&ch::Process::bin_id));
this->SetFromProcs(std::mem_fn(&ch::Process::bin_id));
std::set<int> result3 =
this->GenerateSetFromSysts<int>(std::mem_fn(&ch::Systematic::bin_id));
this->SetFromSysts(std::mem_fn(&ch::Systematic::bin_id));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
return result;
}

std::set<std::string> CombineHarvester::process_set() {
std::set<std::string> result = this->GenerateSetFromProcs<std::string>(
std::set<std::string> result = this->SetFromProcs(
std::mem_fn(&ch::Process::process));
std::set<std::string> result2 = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result2 = this->SetFromSysts(
std::mem_fn(&ch::Systematic::process));
result.insert(result2.begin(), result2.end());
return result;
}

std::set<std::string> CombineHarvester::analysis_set() {
std::set<std::string> result = this->GenerateSetFromObs<std::string>(
std::set<std::string> result = this->SetFromObs(
std::mem_fn(&ch::Observation::analysis));
std::set<std::string> result2 = this->GenerateSetFromProcs<std::string>(
std::set<std::string> result2 = this->SetFromProcs(
std::mem_fn(&ch::Process::analysis));
std::set<std::string> result3 = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result3 = this->SetFromSysts(
std::mem_fn(&ch::Systematic::analysis));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
Expand All @@ -175,48 +175,48 @@ std::set<std::string> CombineHarvester::analysis_set() {

std::set<std::string> CombineHarvester::era_set() {
std::set<std::string> result =
this->GenerateSetFromObs<std::string>(std::mem_fn(&ch::Observation::era));
this->SetFromObs(std::mem_fn(&ch::Observation::era));
std::set<std::string> result2 =
this->GenerateSetFromProcs<std::string>(std::mem_fn(&ch::Process::era));
this->SetFromProcs(std::mem_fn(&ch::Process::era));
std::set<std::string> result3 =
this->GenerateSetFromSysts<std::string>(std::mem_fn(&ch::Systematic::era));
this->SetFromSysts(std::mem_fn(&ch::Systematic::era));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
return result;
}

std::set<std::string> CombineHarvester::channel_set() {
std::set<std::string> result = this->GenerateSetFromObs<std::string>(
std::set<std::string> result = this->SetFromObs(
std::mem_fn(&ch::Observation::channel));
std::set<std::string> result2 = this->GenerateSetFromProcs<std::string>(
std::set<std::string> result2 = this->SetFromProcs(
std::mem_fn(&ch::Process::channel));
std::set<std::string> result3 = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result3 = this->SetFromSysts(
std::mem_fn(&ch::Systematic::channel));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
return result;
}

std::set<std::string> CombineHarvester::mass_set() {
std::set<std::string> result = this->GenerateSetFromObs<std::string>(
std::set<std::string> result = this->SetFromObs(
std::mem_fn(&ch::Observation::mass));
std::set<std::string> result2 = this->GenerateSetFromProcs<std::string>(
std::set<std::string> result2 = this->SetFromProcs(
std::mem_fn(&ch::Process::mass));
std::set<std::string> result3 = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result3 = this->SetFromSysts(
std::mem_fn(&ch::Systematic::mass));
result.insert(result2.begin(), result2.end());
result.insert(result3.begin(), result3.end());
return result;
}

std::set<std::string> CombineHarvester::syst_name_set() {
std::set<std::string> result = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result = this->SetFromSysts(
std::mem_fn(&ch::Systematic::name));
return result;
}

std::set<std::string> CombineHarvester::syst_type_set() {
std::set<std::string> result = this->GenerateSetFromSysts<std::string>(
std::set<std::string> result = this->SetFromSysts(
std::mem_fn(&ch::Systematic::type));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using ch::JoinStr;
void AddMSSMSystematics(CombineHarvester & cb) {
CombineHarvester src = cb.cp();

auto signal = Set2Vec(src.cp().signals().GenerateSetFromProcs<std::string>(
auto signal = Set2Vec(src.cp().signals().SetFromProcs(
std::mem_fn(&Process::process)));

src.cp().signals()
Expand Down
13 changes: 5 additions & 8 deletions CombineHarvester/CombineTools/test/NuisanceSummary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ int main(int argc, char* argv[]) {
delete fitresult;
}

auto systematics = cmb.GenerateSetFromSysts<std::string>(
std::mem_fn(&ch::Systematic::name));
auto systematics = cmb.SetFromSysts(std::mem_fn(&ch::Systematic::name));
std::set<string> ungrouped;


Expand Down Expand Up @@ -131,18 +130,16 @@ int main(int argc, char* argv[]) {
std::set<double> cleaned_rates;
ch::CombineHarvester all_syst = cmb.cp()
.syst_name(p.second);
auto chns = all_syst
.GenerateSetFromSysts<string>(std::mem_fn(&ch::Systematic::channel));
auto chns = all_syst.SetFromSysts(std::mem_fn(&ch::Systematic::channel));
for (auto const& c : chns) std::cout << " " << c << " ";
std::cout << "\n";
std::set<string> all_procs;
auto bins = all_syst
.GenerateSetFromSysts<string>(std::mem_fn(&ch::Systematic::bin));
auto bins = all_syst.SetFromSysts(std::mem_fn(&ch::Systematic::bin));
for (auto const& bin : bins) {
ch::CombineHarvester syst_in_bin = all_syst.cp()
.bin({bin});
auto procs = syst_in_bin
.GenerateSetFromSysts<string>(std::mem_fn(&ch::Systematic::process));
auto procs =
syst_in_bin.SetFromSysts(std::mem_fn(&ch::Systematic::process));
for (auto const& proc : procs) {
all_procs.insert(proc);
ch::CombineHarvester syst_in_proc = syst_in_bin.cp()
Expand Down
2 changes: 1 addition & 1 deletion CombineHarvester/CombineTools/test/SOBPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int main(int argc, char* argv[]){
cmb.UpdateParameters(fitparams);
}

auto bins = cmb.GenerateSetFromObs<string>(std::mem_fn(&ch::Observation::bin));
auto bins = cmb.SetFromObs(std::mem_fn(&ch::Observation::bin));
map<string, ch::SOverBInfo> weights;
for (auto const& bin : bins) {
TH1F sig = cmb.cp().bin({bin}).signals().GetShape();
Expand Down

0 comments on commit 7a7c716

Please sign in to comment.