8 #include "boost/format.hpp"
9 #include "RooFitResult.h"
10 #include "RooRealVar.h"
11 #include "RooDataHist.h"
12 #include "RooAbsReal.h"
13 #include "RooAbsData.h"
22 std::unique_ptr<RooArgSet> all_vars(pdf->getParameters(RooArgSet()));
24 std::set<std::string> names;
25 RooFIter dat_it = dat_vars->fwdIterator();
26 RooAbsArg *dat_arg =
nullptr;
27 while((dat_arg = dat_it.next())) {
28 names.insert(dat_arg->GetName());
33 RooFIter vars_it = all_vars->fwdIterator();
34 RooAbsArg *vars_arg =
nullptr;
35 while((vars_arg = vars_it.next())) {
36 if (!names.count(vars_arg->GetName())) {
37 result_set.add(*vars_arg);
47 std::vector<ch::Parameter> params;
48 params.resize(res.floatParsFinal().getSize());
49 for (
int i = 0; i < res.floatParsFinal().getSize(); ++i) {
50 RooRealVar
const* var =
51 dynamic_cast<RooRealVar const*
>(res.floatParsFinal().at(i));
52 params[i].set_name(std::string(var->GetName()));
53 params[i].set_val(var->getVal());
54 params[i].set_err_d(var->getErrorLo());
55 params[i].set_err_u(var->getErrorHi());
61 RooFitResult
const& res) {
62 std::vector<ch::Parameter> params;
63 params.resize(res.floatParsFinal().getSize());
64 RooArgList
const& rands = res.randomizePars();
65 for (
int i = 0; i < res.floatParsFinal().getSize(); ++i) {
66 RooRealVar
const* var =
dynamic_cast<RooRealVar const*
>(rands.at(i));
67 params[i].set_name(std::string(var->GetName()));
68 params[i].set_val(var->getVal());
69 params[i].set_err_d(var->getErrorLo());
70 params[i].set_err_u(var->getErrorHi());
85 boost::replace_all(pattern,
"$BINID",
86 boost::lexical_cast<std::string>(obj->
bin_id()));
87 boost::replace_all(pattern,
"$BIN", obj->
bin());
88 boost::replace_all(pattern,
"$PROCESS", obj->
process());
89 boost::replace_all(pattern,
"$MASS", obj->
mass());
90 boost::replace_all(pattern,
"$ERA", obj->
era());
91 boost::replace_all(pattern,
"$CHANNEL", obj->
channel());
92 boost::replace_all(pattern,
"$ANALYSIS", obj->
analysis());
97 boost::replace_all(parse_rules,
"$ANALYSIS",
"(?<ANALYSIS>\\w+)");
98 boost::replace_all(parse_rules,
"$ERA",
"(?<ERA>\\w+)");
99 boost::replace_all(parse_rules,
"$CHANNEL",
"(?<CHANNEL>\\w+)");
100 boost::replace_all(parse_rules,
"$BINID",
"(?<BINID>\\w+)");
101 boost::replace_all(parse_rules,
"$MASS",
"(?<MASS>\\w+)");
102 boost::regex rgx(parse_rules);
103 boost::smatch matches;
104 boost::regex_search(input->
bin(), matches, rgx);
105 if (matches.str(
"ANALYSIS").length())
107 if (matches.str(
"ERA").length())
108 input->
set_era(matches.str(
"ERA"));
109 if (matches.str(
"CHANNEL").length())
111 if (matches.str(
"BINID").length())
112 input->
set_bin_id(boost::lexical_cast<int>(matches.str(
"BINID")));
113 if (matches.str(
"MASS").length())
114 input->
set_mass(matches.str(
"MASS"));
121 TGraph
TGraphFromTable(std::string filename, std::string
const& x_column, std::string
const& y_column) {
123 if (lines.size() == 0)
throw std::runtime_error(
FNERROR(
"Table is empty"));
124 std::vector<std::string> fields;
125 boost::split(fields, lines[0], boost::is_any_of(
"\t "),
126 boost::token_compress_on);
127 auto x_it = std::find(fields.begin(), fields.end(), x_column);
128 if (x_it == fields.end())
129 throw std::runtime_error(
FNERROR(
"No column with label " + x_column));
130 auto y_it = std::find(fields.begin(), fields.end(), y_column);
131 if (y_it == fields.end())
132 throw std::runtime_error(
FNERROR(
"No column with label " + y_column));
133 unsigned x_col(x_it - fields.begin());
134 unsigned y_col(y_it - fields.begin());
135 TGraph res(lines.size() - 1);
136 for (
unsigned i = 1; i < lines.size(); ++i) {
137 std::vector<std::string> words;
138 boost::split(words, lines[i], boost::is_any_of(
"\t "),
139 boost::token_compress_on);
140 if (words.size() != fields.size()) {
141 FNLOG(std::cout) <<
"Skipped this line:\n" << lines[i] <<
"\n";
144 res.SetPoint(i, boost::lexical_cast<double>(words.at(x_col)),
145 boost::lexical_cast<double>(words.at(y_col)));
154 std::vector<std::vector<std::string>>
const& in) {
155 return Join<std::string>(in);
158 RooDataHist
TH1F2Data(TH1F
const& hist, RooRealVar
const& x,
159 std::string
const& name) {
160 TH1F shape(
"tmp",
"tmp", hist.GetNbinsX(), 0.,
161 static_cast<float>(hist.GetNbinsX()));
162 for (
int i = 1; i <= hist.GetNbinsX(); ++i) {
163 shape.SetBinContent(i, hist.GetBinContent(i));
165 RooDataHist dh(name.c_str(), name.c_str(),
166 RooArgList(x), RooFit::Import(shape,
false));
171 TH1::AddDirectory(0);
172 TH1F shape(
"tmp",
"tmp", hist.GetNbinsX(), 0.,
173 static_cast<float>(hist.GetNbinsX()));
174 for (
int i = 1; i <= hist.GetNbinsX(); ++i) {
175 shape.SetBinContent(i, hist.GetBinContent(i));
176 shape.SetBinError(i, hist.GetBinError(i));
184 for (
int x = 1; x <= res.GetNbinsX(); ++x) {
185 res.SetBinContent(x, src.GetBinContent(x));
186 res.SetBinError(x, src.GetBinError(x));
192 std::vector<unsigned> vec) {
193 unsigned n = vec.size();
194 std::vector<unsigned> idx(n, 0);
195 std::vector<std::vector<unsigned>> result;
198 if (std::find(vec.begin(), vec.end(), 0) != vec.end()) {
201 result.push_back(idx);
202 bool exit_loop =
false;
203 while (exit_loop ==
false) {
205 for (
unsigned i = 0; i < n; ++i) {
206 if ((idx[i] + 1) == vec[i]) {
216 result.push_back(idx);
226 std::vector<std::string> files;
228 file.open(file_name.c_str());
229 if (!file.is_open()) {
230 throw std::runtime_error(
231 FNERROR(
"File " + file_name +
" could not be opened"));
233 std::string line =
"";
234 while (std::getline(file, line)) {
235 files.push_back(line);
242 std::istringstream iss(str);
244 iss >> std::noskipws >> f;
246 return iss.eof() && !iss.fail();
250 std::string
const& fmt) {
251 std::set<double> mass_set;
252 std::vector<std::string> tokens;
253 boost::split(tokens, input, boost::is_any_of(
","));
254 for (
auto const& t : tokens) {
255 std::vector<std::string> sub_tokens;
256 boost::split(sub_tokens, t, boost::is_any_of(
"-:"));
257 if (sub_tokens.size() == 1) {
258 double mass_val = boost::lexical_cast<double>(sub_tokens[0]);
259 mass_set.insert(mass_val);
260 }
else if (sub_tokens.size() == 3) {
261 double lo = boost::lexical_cast<double>(sub_tokens[0]);
262 double hi = boost::lexical_cast<double>(sub_tokens[1]);
263 double step = boost::lexical_cast<double>(sub_tokens[2]);
265 throw std::runtime_error(
266 "[MassesFromRange] High mass is smaller than low mass!");
268 while (start < hi + 0.001) {
269 mass_set.insert(start);
274 std::vector<std::string> result;
275 for (
auto const& m : mass_set) {
276 result.push_back((boost::format(fmt) % m).str());
282 std::string
const& fmt) {
283 std::set<double> mass_set;
284 std::vector<std::string> tokens;
285 boost::split(tokens, input, boost::is_any_of(
","));
286 for (
auto const& t : tokens) {
287 std::vector<std::string> sub_tokens;
288 boost::split(sub_tokens, t, boost::is_any_of(
":|"));
289 if (sub_tokens.size() == 1) {
290 double mass_val = boost::lexical_cast<double>(sub_tokens[0]);
291 mass_set.insert(mass_val);
292 }
else if (sub_tokens.size() == 3) {
293 double lo = boost::lexical_cast<double>(sub_tokens[0]);
294 double hi = boost::lexical_cast<double>(sub_tokens[1]);
295 double step = boost::lexical_cast<double>(sub_tokens[2]);
297 throw std::runtime_error(
298 "[ValsFromRange] High mass is smaller than low mass!");
300 while (start < hi + 1E-4) {
301 mass_set.insert(start);
306 std::vector<std::string> result;
307 for (
auto const& m : mass_set) {
308 result.push_back((boost::format(fmt) % m).str());
314 boost::filesystem::path p_to) {
315 p_from = boost::filesystem::absolute(p_from);
316 p_to = boost::filesystem::absolute(p_to);
317 boost::filesystem::path ret;
318 boost::filesystem::path::const_iterator itrFrom(p_from.begin()),
321 for (boost::filesystem::path::const_iterator toEnd(p_to.end()),
322 fromEnd(p_from.end());
323 itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo;
326 for (boost::filesystem::path::const_iterator fromEnd(p_from.end());
327 itrFrom != fromEnd; ++itrFrom) {
328 if ((*itrFrom) !=
".") ret /=
"..";
332 for (boost::filesystem::path::const_iterator toEnd(p_to.end());
333 itrTo != toEnd; ++itrTo) {
340 bool has_negative =
false;
341 for (
int i = 1; i <= h->GetNbinsX(); ++i) {
342 if (h->GetBinContent(i) < 0.) {
350 for (
int i = 1; i <= h->GetNbinsX(); ++i) {
351 if (h->GetBinContent(i) < 0.) {
352 h->SetBinContent(i, 0.);
void ForEachObj(Function func)
virtual void set_analysis(std::string const &analysis)
virtual void set_mass(std::string const &mass)
virtual std::string const & process() const
virtual void set_bin(std::string const &bin)
virtual std::string const & bin() const
virtual int bin_id() const
virtual std::string const & analysis() const
virtual std::string const & era() const
virtual std::string const & mass() const
virtual void set_era(std::string const &era)
virtual std::string const & channel() const
virtual void set_channel(std::string const &channel)
virtual void set_bin_id(int const &bin_id)
RooDataHist TH1F2Data(TH1F const &hist, RooRealVar const &x, std::string const &name)
void SetStandardBinName(ch::Object *obj, std::string pattern)
std::vector< std::string > ParseFileLines(std::string const &file_name)
TH1F RestoreBinning(TH1F const &src, TH1F const &ref)
TH1F RebinHist(TH1F const &hist)
boost::filesystem::path make_relative(boost::filesystem::path p_from, boost::filesystem::path p_to)
Determine the relative path from one file to another.
std::vector< ch::Parameter > ExtractFitParameters(RooFitResult const &res)
bool HasNegativeBins(TH1 const *h)
TGraph TGraphFromTable(std::string filename, std::string const &x_column, std::string const &y_column)
std::vector< std::vector< unsigned > > GenerateCombinations(std::vector< unsigned > vec)
void SetStandardBinNames(CombineHarvester &cb, std::string const &pattern="$ANALYSIS_$CHANNEL_$BINID_$ERA")
std::vector< std::string > MassesFromRange(std::string const &input, std::string const &fmt="%.0f")
Generate a vector of mass values using ranges and intervals specified in a string.
std::vector< ch::Parameter > ExtractSampledFitParameters(RooFitResult const &res)
std::vector< std::string > ValsFromRange(std::string const &input, std::string const &fmt="%.0f")
Generate a vector of values using ranges and intervals specified in a string.
std::vector< std::string > JoinStr(std::vector< std::vector< std::string >> const &in)
bool is_float(std::string const &str)
void ZeroNegativeBins(TH1 *h)
RooArgSet ParametersByName(RooAbsReal const *pdf, RooArgSet const *dat_vars)
void SetFromBinName(ch::Object *input, std::string parse_rules)