ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
utilities.hpp
Go to the documentation of this file.
1 #ifndef H_UTILITIES
2 #define H_UTILITIES
3 
4 #include <string>
5 #include <vector>
6 #include <iostream>
7 #include <iomanip>
8 #include <sstream>
9 #include <limits>
10 #include <mutex>
11 #include <set>
12 #include <numeric>
13 
14 #include "TH1D.h"
15 #include "TRandom3.h"
16 
17 #define ERROR(x) do{throw std::runtime_error(std::string("Error in file ")+__FILE__+" at line "+std::to_string(__LINE__)+" (in "+__func__+"): "+x);}while(false)
18 #define DBG(x) do{std::cerr << "In " << __FILE__ << " at line " << __LINE__ << " (in function " << __func__ << "): " << x << std::endl;}while(false)
19 
20 namespace Multithreading{
21  extern std::mutex root_mutex;
22 }
23 
24 std::set<std::string> Glob(const std::string &pattern);
25 std::string Basename(const std::string &filename);
26 
27 bool Contains(const std::string &str, const std::string &pat);
28 bool StartsWith(const std::string &str, const std::string &pat);
29 void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep);
30 std::string CopyReplaceAll(const std::string &str, const std::string &orig, const std::string &rep);
31 
32 std::string execute(const std::string &cmd);
33 
34 std::string CodeToPlainText(std::string code);
35 std::string CodeToLatex(std::string code);
36 std::string CodeToRootTex(std::string code);
37 
38 template<typename T>
39 std::vector<std::size_t> SortPermutation(const std::vector<T>& vec){
40  std::vector<std::size_t> p(vec.size());
41  std::iota(p.begin(), p.end(), 0);
42  std::sort(p.begin(), p.end(),
43  [&](std::size_t i, std::size_t j){ return vec[i] < vec[j]; });
44  return p;
45 }
46 template<typename T>
47 std::vector<T> ApplyPermutation(const std::vector<T>& vec, const std::vector<std::size_t>& perm){
48  if(vec.size() != perm.size()) ERROR("Bad permutation: vector and perm have to have the same size");
49  std::vector<T> vsorted(vec.size());
50  for(std::size_t ind=0; ind<vec.size(); ind++) vsorted[ind] = vec[perm[ind]];
51  return vsorted;
52 }
53 
54 std::vector<std::string> Tokenize(const std::string& input,
55  const std::string& tokens=" ");
56 
57 std::string MakeDir(std::string prefix);
58 
59 void AdjustDensityForBinWidth(TH1D &h);
60 void Normalize(TH1D &h, double normalization, bool norm_per_avg_x);
61 
62 void MergeOverflow(TH1D &h, bool merge_underflow, bool merge_overflow);
63 
64 std::string FixedDigits(double x, int n_digits);
65 
66 std::string FullTitle(const TH1 &h);
67 
68 template<typename T>
69 void Append(T &collection, const typename T::value_type &value){
70  collection.insert(collection.end(), value);
71 }
72 
73 template<typename T>
74 std::string ToString(const T& x){
75  std::ostringstream oss;
76  oss << std::setprecision(std::numeric_limits<double>::digits10) << x << std::flush;
77  return oss.str();
78 }
79 
80 template<typename T>
81 std::string ToLongString(const T& x){
82  std::ostringstream oss;
83  oss << std::setprecision(std::numeric_limits<double>::max_digits10) << x << std::flush;
84  return oss.str();
85 }
86 
87 TString HoursMinSec(float fseconds);
88 TString AddCommas(double num);
89 TString RoundNumber(double num, int decimals, double denom=1.);
90 
91 double Significance(double Nobs, double Nbkg, double Eup_bkg, double Edown_bkg=-1.);
92 double gsl_ran_gamma (const double a, const double b, TRandom3 &rand);
93 double intGaus(double mean, double sigma, double minX, double maxX);
94 float deltaR(float eta1, float phi1, float eta2, float phi2);
95 // yields[Nobs][Nsam] has the entries for each sample for each observable going into kappa
96 // weights[Nobs][Nsam] has the average weight of each observable for each sample
97 // powers[Nobs] defines kappa = Product_obs{ Sum_sam{yields[sam][obs]*weights[sam][obs]}^powers[obs] }
98 double calcKappa(std::vector<std::vector<float> > &entries, std::vector<std::vector<float> > &weights,
99  std::vector<float> &powers, float &mSigma, float &pSigma, bool do_data=false,
100  bool verbose=false, double syst=-1., bool do_plot=false, int nrep=100000);
101 
102 
103 #endif
void Append(T &collection, const typename T::value_type &value)
Definition: utilities.hpp:69
bool StartsWith(const std::string &str, const std::string &pat)
Definition: utilities.cpp:48
TString HoursMinSec(float fseconds)
Definition: utilities.cpp:337
void Normalize(TH1D &h, double normalization, bool norm_per_avg_x)
Definition: utilities.cpp:290
double Significance(double Nobs, double Nbkg, double Eup_bkg, double Edown_bkg=-1.)
Definition: utilities.cpp:426
std::string CodeToPlainText(std::string code)
Definition: utilities.cpp:79
std::string FullTitle(const TH1 &h)
Definition: utilities.cpp:331
double gsl_ran_gamma(const double a, const double b, TRandom3 &rand)
Definition: utilities.cpp:386
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:52
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:44
TString AddCommas(double num)
Definition: utilities.cpp:351
double intGaus(double mean, double sigma, double minX, double maxX)
Definition: utilities.cpp:416
std::string execute(const std::string &cmd)
Definition: utilities.cpp:65
std::vector< T > ApplyPermutation(const std::vector< T > &vec, const std::vector< std::size_t > &perm)
Definition: utilities.hpp:47
double calcKappa(std::vector< std::vector< float > > &entries, std::vector< std::vector< float > > &weights, std::vector< float > &powers, float &mSigma, float &pSigma, bool do_data=false, bool verbose=false, double syst=-1., bool do_plot=false, int nrep=100000)
Definition: utilities.cpp:469
std::string ToLongString(const T &x)
Definition: utilities.hpp:81
std::string CodeToRootTex(std::string code)
Definition: utilities.cpp:116
#define ERROR(x)
Definition: utilities.hpp:17
std::string CodeToLatex(std::string code)
Definition: utilities.cpp:242
std::string Basename(const std::string &filename)
Definition: utilities.cpp:38
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:361
float deltaR(float eta1, float phi1, float eta2, float phi2)
Definition: utilities.cpp:420
std::mutex root_mutex
Definition: utilities.cpp:25
void MergeOverflow(TH1D &h, bool merge_underflow, bool merge_overflow)
Definition: utilities.cpp:300
void AdjustDensityForBinWidth(TH1D &h)
Definition: utilities.cpp:273
std::set< std::string > Glob(const std::string &pattern)
Definition: utilities.cpp:27
std::vector< std::size_t > SortPermutation(const std::vector< T > &vec)
Definition: utilities.hpp:39
std::string FixedDigits(double x, int n_digits)
Definition: utilities.cpp:316
std::string MakeDir(std::string prefix)
Definition: utilities.cpp:262
std::string ToString(const T &x)
Definition: utilities.hpp:74
std::string CopyReplaceAll(const std::string &str, const std::string &orig, const std::string &rep)
std::vector< std::string > Tokenize(const std::string &input, const std::string &tokens=" ")
Definition: utilities.cpp:248