CombineHarvester
Utilities.h
Go to the documentation of this file.
1 #ifndef CombineTools_Utilities_h
2 #define CombineTools_Utilities_h
3 #include <algorithm>
4 #include <vector>
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <sstream>
9 #include "boost/algorithm/string.hpp"
10 #include "boost/lexical_cast.hpp"
11 #include "boost/regex.hpp"
12 #include "boost/filesystem.hpp"
13 #include "TGraph.h"
14 #include "RooFitResult.h"
15 #include "RooArgSet.h"
20 
21 namespace ch { class CombineHarvester; }
22 
23 namespace ch {
24 RooArgSet ParametersByName(RooAbsReal const* pdf, RooArgSet const* dat_vars);
25 
26 // ---------------------------------------------------------------------------
27 // Paramter extraction/manipulation
28 // ---------------------------------------------------------------------------
29 std::vector<ch::Parameter> ExtractFitParameters(RooFitResult const& res);
30 std::vector<ch::Parameter> ExtractSampledFitParameters(RooFitResult const& res);
31 
32 
33 // ---------------------------------------------------------------------------
34 // Property matching & editing
35 // ---------------------------------------------------------------------------
37  CombineHarvester& cb,
38  std::string const& pattern = "$ANALYSIS_$CHANNEL_$BINID_$ERA");
39 
40 void SetStandardBinName(ch::Object* obj, std::string pattern);
41 
42 template<class T, class U>
43 bool MatchingProcess(T const& first, U const& second) {
44  if (first.bin() == second.bin() &&
45  first.process() == second.process() &&
46  first.signal() == second.signal() &&
47  first.analysis() == second.analysis() &&
48  first.era() == second.era() &&
49  first.channel() == second.channel() &&
50  first.bin_id() == second.bin_id() &&
51  first.mass() == second.mass()) {
52  return true;
53  } else {
54  return false;
55  }
56 }
57 
58 template<class T, class U>
59 void SetProperties(T * first, U const* second) {
60  first->set_bin(second->bin());
61  first->set_process(second->process());
62  first->set_signal(second->signal());
63  first->set_analysis(second->analysis());
64  first->set_era(second->era());
65  first->set_channel(second->channel());
66  first->set_bin_id(second->bin_id());
67  first->set_mass(second->mass());
68  first->set_all_attributes(second->all_attributes());
69 }
70 
71 void SetFromBinName(ch::Object *input, std::string parse_rules);
72 
73 // ---------------------------------------------------------------------------
74 // Rate scaling
75 // ---------------------------------------------------------------------------
76 TGraph TGraphFromTable(std::string filename, std::string const& x_column,
77  std::string const& y_column);
78 
79 // ---------------------------------------------------------------------------
80 // Misc
81 // ---------------------------------------------------------------------------
82 template<class T>
83 std::vector<T> Join(std::vector<std::vector<T>> const& in) {
84  unsigned size = 0;
85  for (auto const& x : in) size += x.size();
86  std::vector<T> res(size);
87  unsigned i = 0;
88  for (auto const& x : in) {
89  for (auto const& y : x) {
90  res[i] = y;
91  ++i;
92  }
93  }
94  return res;
95 }
96 
97 std::vector<std::string> JoinStr(
98  std::vector<std::vector<std::string>> const& in);
99 
100 template<class T>
101 std::vector<T> Set2Vec(std::set<T> const& in) {
102  return std::vector<T>(in.begin(), in.end());
103 }
104 
105 RooDataHist TH1F2Data(TH1F const& hist, RooRealVar const& x,
106  std::string const& name);
107 
108 TH1F RebinHist(TH1F const& hist);
109 
110 TH1F RestoreBinning(TH1F const& src, TH1F const& ref);
111 
112 
113 std::vector<std::vector<unsigned>> GenerateCombinations(
114  std::vector<unsigned> vec);
115 
116 std::vector<std::string> ParseFileLines(std::string const& file_name);
117 
118 
119 bool is_float(std::string const& str);
120 
143 std::vector<std::string> MassesFromRange(std::string const& input,
144  std::string const& fmt = "%.0f");
145 
164 std::vector<std::string> ValsFromRange(std::string const& input,
165  std::string const& fmt = "%.0f");
166 
167 bool HasNegativeBins(TH1 const* h);
168 
169 void ZeroNegativeBins(TH1 *h);
170 
171 // ---------------------------------------------------------------------------
172 // Tuple Printing
173 // ---------------------------------------------------------------------------
174 // Most of this is just implementation detail, so we'll hide it away in a
175 // namespace:
176 namespace tupleprint {
177 // Have stolen tuple printing routines from here:
178 // http://en.cppreference.com/w/cpp/utility/tuple/tuple_cat
179 template <class Tuple, std::size_t N>
180 struct TuplePrinter {
181  static void print(const Tuple &t, std::stringstream & str) {
183  str << ", " << std::get<N - 1>(t);
184  }
185 };
186 
187 template <class Tuple>
188 struct TuplePrinter<Tuple, 1> {
189  static void print(const Tuple & t, std::stringstream & str) {
190  str << std::get<0>(t);
191  }
192 };
193 
194 // Have to add this because we do make use of empty tuples
195 // Would lead to infinite recursion otherwise
196 template <class Tuple>
197 struct TuplePrinter<Tuple, 0> {
198  static void print(const Tuple & /*t*/, std::stringstream & /*str*/) { }
199 };
200 }
201 
213 template <class... Args>
214 std::string Tuple2String(const std::tuple<Args...> &t) {
215  std::stringstream str;
216  str << "(";
217  tupleprint::TuplePrinter<decltype(t), sizeof...(Args)>::print(t, str);
218  str << ")";
219  return str.str();
220 }
221 
222 // ---------------------------------------------------------------------------
223 // Filesystem Additions
224 // ---------------------------------------------------------------------------
240 boost::filesystem::path make_relative(boost::filesystem::path p_from,
241  boost::filesystem::path p_to);
242 }
243 
244 #endif
Definition: Algorithm.h:10
void SetProperties(T *first, U const *second)
Definition: Utilities.h:59
RooDataHist TH1F2Data(TH1F const &hist, RooRealVar const &x, std::string const &name)
Definition: Utilities.cc:158
void SetStandardBinName(ch::Object *obj, std::string pattern)
Definition: Utilities.cc:84
std::vector< std::string > ParseFileLines(std::string const &file_name)
Definition: Utilities.cc:224
TH1F RestoreBinning(TH1F const &src, TH1F const &ref)
Definition: Utilities.cc:181
TH1F RebinHist(TH1F const &hist)
Definition: Utilities.cc:170
boost::filesystem::path make_relative(boost::filesystem::path p_from, boost::filesystem::path p_to)
Determine the relative path from one file to another.
Definition: Utilities.cc:313
std::vector< ch::Parameter > ExtractFitParameters(RooFitResult const &res)
Definition: Utilities.cc:46
std::vector< T > Set2Vec(std::set< T > const &in)
Definition: Utilities.h:101
std::vector< T > Join(std::vector< std::vector< T >> const &in)
Definition: Utilities.h:83
bool HasNegativeBins(TH1 const *h)
Definition: Utilities.cc:339
std::string Tuple2String(const std::tuple< Args... > &t)
Format any std::tuple as a string.
Definition: Utilities.h:214
TGraph TGraphFromTable(std::string filename, std::string const &x_column, std::string const &y_column)
Definition: Utilities.cc:121
std::vector< std::vector< unsigned > > GenerateCombinations(std::vector< unsigned > vec)
Definition: Utilities.cc:191
void SetStandardBinNames(CombineHarvester &cb, std::string const &pattern="$ANALYSIS_$CHANNEL_$BINID_$ERA")
Definition: Utilities.cc:78
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.
Definition: Utilities.cc:249
std::vector< ch::Parameter > ExtractSampledFitParameters(RooFitResult const &res)
Definition: Utilities.cc:60
bool MatchingProcess(T const &first, U const &second)
Definition: Utilities.h:43
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.
Definition: Utilities.cc:281
std::vector< std::string > JoinStr(std::vector< std::vector< std::string >> const &in)
Definition: Utilities.cc:153
bool is_float(std::string const &str)
Definition: Utilities.cc:241
void ZeroNegativeBins(TH1 *h)
Definition: Utilities.cc:349
RooArgSet ParametersByName(RooAbsReal const *pdf, RooArgSet const *dat_vars)
Definition: Utilities.cc:18
void SetFromBinName(ch::Object *input, std::string parse_rules)
Definition: Utilities.cc:96
static void print(const Tuple &, std::stringstream &)
Definition: Utilities.h:198
static void print(const Tuple &t, std::stringstream &str)
Definition: Utilities.h:189
static void print(const Tuple &t, std::stringstream &str)
Definition: Utilities.h:181