ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
utilities.cpp
Go to the documentation of this file.
1 #include "core/utilities.hpp"
2 
3 #include <cstdlib>
4 #include <cstring>
5 #include <cstdint>
6 #include <cmath>
7 
8 #include <sstream>
9 #include <string>
10 #include <vector>
11 #include <memory>
12 
13 #include <unistd.h>
14 #include <glob.h>
15 #include <libgen.h>
16 
17 #include "TMath.h"
18 #include "TCanvas.h"
19 #include "TVector2.h"
20 #include "TH1D.h"
21 #include "RooStats/RooStatsUtils.h"
22 
23 using namespace std;
24 
26 
27 set<string> Glob(const string &pattern){
28  glob_t glob_result;
29  glob(pattern.c_str(), GLOB_TILDE, nullptr, &glob_result);
30  set<string> ret;
31  for(size_t i=0; i<glob_result.gl_pathc; ++i){
32  ret.emplace(realpath(glob_result.gl_pathv[i], nullptr));
33  }
34  globfree(&glob_result);
35  return ret;
36 }
37 
38 string Basename(const string &filename){
39  vector<char> c(filename.cbegin(), filename.cend());
40  c.push_back(0);
41  return string(basename(&c.at(0)));
42 }
43 
44 bool Contains(const string &str, const string &pat){
45  return str.find(pat) != string::npos;
46 }
47 
48 bool StartsWith(const string &str, const string &pat){
49  return str.find(pat) == 0;
50 }
51 
52 void ReplaceAll(string &str, const string &orig, const string &rep){
53  size_t loc = 0;
54  while ((loc = str.find(orig, loc)) != string::npos) {
55  str.replace(loc, orig.length(), rep);
56  loc += rep.length();
57  }
58 }
59 
60 string CopyReplaceAll(string str, const string &orig, const string &rep){
61  ReplaceAll(str, orig, rep);
62  return str;
63 }
64 
65 string execute(const string &cmd){
66  FILE *pipe = popen(cmd.c_str(), "r");
67  if(!pipe) throw runtime_error("Could not open pipe.");
68  const size_t buffer_size = 128;
69  char buffer[buffer_size];
70  string result = "";
71  while(!feof(pipe)){
72  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
73  }
74 
75  pclose(pipe);
76  return result;
77 }
78 
79 string CodeToPlainText(string code){
80  ReplaceAll(code, "((leps_pt[0]>30&&leps_eta[0]<2.1&&leps_eta[0]>-2.1)||(leps_pt[1]>30&&leps_eta[1]<2.1&&leps_eta[1]>-2.1))", "SLtrig");
81  ReplaceAll(code, "(mumu_m*(mumu_m>0&&mumu_pt1>30)+elel_m*(elel_m>0&&elel_pt1>30))>80&&(mumu_m*(mumu_m>0&&mumu_pt1>30)+elel_m*(elel_m>0&&elel_pt1>30))<100", "zmasswindow");
82  ReplaceAll(code, ".", "p");
83  ReplaceAll(code, "(", "");
84  ReplaceAll(code, ")", "");
85  ReplaceAll(code, "[", "");
86  ReplaceAll(code, "]", "");
87  ReplaceAll(code, "{", "");
88  ReplaceAll(code, "}", "");
89  ReplaceAll(code, "+", "p");
90  ReplaceAll(code, "-", "m");
91  ReplaceAll(code, "*", "x");
92  ReplaceAll(code, "/", "d");
93  ReplaceAll(code, "%", "_");
94  ReplaceAll(code, "!", "n");
95  ReplaceAll(code, "&&", "_");
96  ReplaceAll(code, "||", "_");
97  ReplaceAll(code, "==", "");
98  ReplaceAll(code, "<=", "le");
99  ReplaceAll(code, ">=", "ge");
100  ReplaceAll(code, ">", "g");
101  ReplaceAll(code, "<", "l");
102  ReplaceAll(code, "=", "");
103  ReplaceAll(code, "&", "_");
104  ReplaceAll(code, "|", "_");
105  ReplaceAll(code, "^", "_");
106  ReplaceAll(code, "~", "_");
107  ReplaceAll(code, "___", "__");
108  for(size_t i = 0; i < code.size(); ++i){
109  if(isalnum(code.at(i)) || code.at(i) == '.' || code.at(i) == '_') continue;
110  code = code.substr(0,i)+code.substr(i+1);
111  }
112 
113  return code;
114 }
115 
116 string CodeToRootTex(string code){
117 <<<<<<< HEAD
118 =======
119  ReplaceAll(code, " ", "");
120 
121 >>>>>>> b1f888b55e7125d5a772cc4a7067bd650781e35f
122  ReplaceAll(code, "ht1l_stmin2l", "1l: H_{T}>500, 2l: H_{T} + p_{T}^{l,min}");
123  ReplaceAll(code, "ht1l_stmax2l", "1l: H_{T}>500, 2l: H_{T} + p_{T}^{l,max}");
124  ReplaceAll(code, "ht1l_stave2l", "1l: H_{T}>500, 2l: H_{T} + p_{T}^{l,ave}");
125  ReplaceAll(code, "st", "S_{T}");
126 
127 <<<<<<< HEAD
128  ReplaceAll(code, "met>150&&met<=200", "150<met<=200");
129 =======
130  ReplaceAll(code, "met>100&&met<=150", "100<met<=150");
131  ReplaceAll(code, "met>150&&met<=200", "150<met<=200");
132  ReplaceAll(code, "met>200&&met<=300", "200<met<=300");
133 >>>>>>> b1f888b55e7125d5a772cc4a7067bd650781e35f
134  ReplaceAll(code, "met>200&&met<=350", "200<met<=350");
135  ReplaceAll(code, "met>350&&met<=500", "350<met<=500");
136  ReplaceAll(code, "njets>=5&&njets<=7", "5<=njets<=7");
137  ReplaceAll(code, "njets>=6&&njets<=8", "6<=njets<=8");
138  ReplaceAll(code, "nbm>=1&&nbm<=2", "1<=nbm<=2");
139 
140  ReplaceAll(code, "1==1", "Full Sample");
141  ReplaceAll(code, "el_tks_chg*lep_charge<0", "OS");
142  ReplaceAll(code, "mu_tks_chg*lep_charge<0", "OS");
143  ReplaceAll(code, "had_tks_chg*lep_charge<0", "OS");
144  ReplaceAll(code, "Sum$(abs(mc_id)==11)","N^{true}_{e}");
145  ReplaceAll(code, "Sum$(abs(mc_id)==13)","N^{true}_{#mu}");
146  ReplaceAll(code, "Sum$(genels_pt>0)", "N^{true}_{e}");
147  ReplaceAll(code, "Sum$(genmus_pt>0)", "N^{true}_{#mu}");
148  ReplaceAll(code, "Sum$(mus_sigid&&mus_miniso<0.2)","N_{#mu}^{10}");
149  ReplaceAll(code, "Sum$(els_sigid&&els_miniso<0.1)","N_{e}^{10}");
150  ReplaceAll(code, "nvmus==1&&nmus==1&&nvels==0","1 #mu");
151  ReplaceAll(code, "nvmus10==0&&nvels10==0", "0 leptons");
152  ReplaceAll(code, "(nmus+nels)", "N_{lep}");
153  ReplaceAll(code, "(nels+nmus)", "N_{lep}");
154  ReplaceAll(code, "nveto", "N^{veto}_{tks}");
155  ReplaceAll(code, "(nvmus+nvels)", "N^{veto}_{lep}");
156  ReplaceAll(code, "nvmus==2&&nmus>=1","N_{#mu}#geq1, N^{veto}_{#mu}=2");
157  ReplaceAll(code, "nvels==2&&nels>=1","N_{e}#geq1, N^{veto}_{e}=2");
158  ReplaceAll(code, "(nvmus>=2||nvels>=2)","N^{veto}_{lep} #geq 2");
159  ReplaceAll(code, "(mumu_m*(mumu_m>0)+elel_m*(elel_m>0))>80&&(mumu_m*(mumu_m>0)+elel_m*(elel_m>0))<100",
160  "80<m_{ll}<100");
161  ReplaceAll(code, "mumuv_m>80&&mumuv_m<100",
162  "80<m_{ll}<100");
163  ReplaceAll(code, "elelv_m>80&&elelv_m<100",
164  "80<m_{ll}<100");
165  ReplaceAll(code, "onht>350&&onmet>100&&","");
166  ReplaceAll(code, "jets_islep[0]==0","");
167  ReplaceAll(code, "(nels==0&&nmus==1)","N_{#mu}=1");
168  ReplaceAll(code, "(nels==1&&nmus==0)","N_{#font[12]{e}}=1");
169  ReplaceAll(code, "Max$(abs(els_eta)*(els_sigid&&els_miniso<0.1&&els_pt>20))<1.479","barrel #font[12]{e}");
170  ReplaceAll(code, "Max$(abs(els_eta)*(els_sigid&&els_miniso<0.1&&els_pt>20))>1.479","endcap #font[12]{e}");
171 
172  ReplaceAll(code, "!low_dphi", "high #Delta#phi");
173  ReplaceAll(code, "hig_drmax", "#DeltaR^{max}_{bb}");
174  ReplaceAll(code, "ntks", "N_{tks}");
175  ReplaceAll(code, "nleps", "N_{lep}");
176  ReplaceAll(code, "nvleps", "N_{lep}");
177  ReplaceAll(code, "nmus", "N_{#mu}");
178  ReplaceAll(code, "nels", "N_{e}");
179  ReplaceAll(code, "nvmus", "N^{veto}_{#mu}");
180  ReplaceAll(code, "nvels", "N^{veto}_{e}");
181  ReplaceAll(code, "ntruleps", "N^{true}_{lep}");
182  ReplaceAll(code, "_ra2b", "^{ra2b}");
183  ReplaceAll(code, "npv", "N_{PV}");
184  ReplaceAll(code, "mumu_pt1", "p_{T}^{#mu}");
185  ReplaceAll(code, "elel_pt1", "p_{T}^{e}");
186 
187  ReplaceAll(code, "abs(mc_id)==1000006", "stop");
188  ReplaceAll(code, "abs(mc_id)==1000022", "LSP");
189 
190  ReplaceAll(code, "onmet", "MET^{on}");
191  ReplaceAll(code, "onht", "H_{T}^{on}");
192  ReplaceAll(code, "njets30","N_{jets}^{30}");
193  ReplaceAll(code, "els_pt","p^{e}_{T}");
194  ReplaceAll(code, "mus_pt","p^{#mu}_{T}");
195  ReplaceAll(code, "fjets_nconst","N_{const}^{fat jet}");
196  ReplaceAll(code, "fjets_30_m[0]","m(J_{1})");
197  ReplaceAll(code, "fjets_m[0]","m(J_{1})");
198  ReplaceAll(code, "(fjets_pt*cosh(fjets_eta))","p_{fatjet}");
199  ReplaceAll(code, "fjets_pt","p^{fatjet}_{T}");
200  ReplaceAll(code, "jets_pt","p^{jet}_{T}");
201  ReplaceAll(code, "mus_reliso","RelIso");
202  ReplaceAll(code, "els_reliso","RelIso");
203  ReplaceAll(code, "mus_miniso_tr15","MiniIso");
204  ReplaceAll(code, "els_miniso_tr15","MiniIso");
205  ReplaceAll(code, "njets","N_{jets}");
206  ReplaceAll(code, "abs(lep_id)==13&&","");
207  ReplaceAll(code, ">=", " #geq ");
208  ReplaceAll(code, ">", " > ");
209  ReplaceAll(code, "<=", " #leq ");
210  ReplaceAll(code, "<", " < ");
211  ReplaceAll(code, "&&", ", ");
212  ReplaceAll(code, "==", " = ");
213  ReplaceAll(code, "met", "E_{T}^{miss}");
214  ReplaceAll(code, "ht_hlt", "H_{T}^{HLT}");
215  ReplaceAll(code, "mht", "MHT");
216  ReplaceAll(code, "ht", "H_{T}");
217  ReplaceAll(code, "mt", "m_{T}");
218  ReplaceAll(code, "ntks_chg==0", " ITV");
219  ReplaceAll(code, "nbm","N_{b}");
220  ReplaceAll(code, "nbl","N_{b,l}");
221  ReplaceAll(code, "mj", " M_{J}");
222 
223  ReplaceAll(code, "el_tks_mt", "Track m_{T}");
224  ReplaceAll(code, "mu_tks_mt", "Track m_{T}");
225  ReplaceAll(code, "had_tks_mt", "Track m_{T}");
226  ReplaceAll(code, "el_tks_pt", "Track p_{T}");
227  ReplaceAll(code, "mu_tks_pt", "Track p_{T}");
228  ReplaceAll(code, "had_tks_pt", "Track p_{T}");
229  ReplaceAll(code, "el_tks_miniso", "Track miniso");
230  ReplaceAll(code, "mu_tks_miniso", "Track miniso");
231  ReplaceAll(code, "had_tks_miniso", "Track miniso");
232  ReplaceAll(code, "el_tks_chg_miniso", "Track charged miniso");
233  ReplaceAll(code, "mu_tks_chg_miniso", "Track charged miniso");
234  ReplaceAll(code, "had_tks_chg_miniso", "Track charged miniso");
235  ReplaceAll(code, "jetsys_nob_pt", "ISR p_{T}");
236  ReplaceAll(code, "(", "");
237  ReplaceAll(code, ")", "");
238 
239  return code;
240 }
241 
242 string CodeToLatex(string code){
243  code = CodeToRootTex(code);
244  ReplaceAll(code, "#", "\\");
245  return code;
246 }
247 
248 vector<string> Tokenize(const string& input,
249  const string& tokens){
250  char* ipt(new char[input.size()+1]);
251  memcpy(ipt, input.data(), input.size());
252  ipt[input.size()]=static_cast<char>(0);
253  char* ptr(strtok(ipt, tokens.c_str()));
254  vector<string> output(0);
255  while(ptr!=NULL){
256  output.push_back(ptr);
257  ptr=strtok(NULL, tokens.c_str());
258  }
259  return output;
260 }
261 
262 string MakeDir(string prefix){
263  prefix += "XXXXXX";
264  char *dir_name = new char[prefix.size()];
265  if(dir_name == nullptr) throw runtime_error("Could not allocate directory name");
266  strcpy(dir_name, prefix.c_str());
267  mkdtemp(dir_name);
268  prefix = dir_name;
269  delete[] dir_name;
270  return prefix;
271 }
272 
274  double entries = h.GetEntries();
275  int nbins = h.GetNbinsX();
276  double low = h.GetBinLowEdge(1);
277  double high = h.GetBinLowEdge(nbins+1);
278  double width = (high-low)/nbins;
279  for(int bin = 1; bin <= nbins; ++bin){
280  double content = h.GetBinContent(bin);
281  double error = h.GetBinError(bin);
282  double this_width = h.GetBinWidth(bin);
283  double scale = width/this_width;
284  h.SetBinContent(bin, content*scale);
285  h.SetBinError(bin, error*scale);
286  }
287  h.SetEntries(entries);
288 }
289 
290 void Normalize(TH1D &h, double normalization, bool norm_per_avg_width){
291  int nbins = h.GetNbinsX();
292  double low = h.GetBinLowEdge(1);
293  double high = h.GetBinLowEdge(nbins+1);
294  double width = (high-low)/nbins;
295  if(norm_per_avg_width) normalization *= width;
296  double integral = h.Integral("width");
297  h.Scale(normalization/integral);
298 }
299 
300 void MergeOverflow(TH1D &h, bool merge_underflow, bool merge_overflow){
301  if(merge_underflow){
302  h.SetBinContent(1, h.GetBinContent(0)+h.GetBinContent(1));
303  h.SetBinContent(0, 0.);
304  h.SetBinError(1, hypot(h.GetBinError(0), h.GetBinError(1)));
305  h.SetBinError(0, 0.);
306  }
307  int nbins = h.GetNbinsX();
308  if(merge_overflow){
309  h.SetBinContent(nbins, h.GetBinContent(nbins)+h.GetBinContent(nbins+1));
310  h.SetBinContent(nbins+1, 0.);
311  h.SetBinError(nbins, hypot(h.GetBinError(nbins), h.GetBinError(nbins+1)));
312  h.SetBinError(nbins+1, 0.);
313  }
314 }
315 
316 string FixedDigits(double x, int n_digits){
317  int digits_left = max(floor(log10(x))+1., 0.);
318  int digits_right = max(n_digits-digits_left, 0);
319 
320  double multiplier = pow(10., digits_right);
321 
322  ostringstream oss;
323  oss << setprecision(numeric_limits<double>::digits10) << round(x*multiplier)/multiplier << flush;
324  string out = oss.str();
325  if(out.substr(0,2) == "0."){
326  out = out.substr(1);
327  }
328  return out;
329 }
330 
331 string FullTitle(const TH1 &h){
332  return string(h.GetTitle())
333  +";"+h.GetXaxis()->GetTitle()
334  +";"+h.GetYaxis()->GetTitle();
335 }
336 
337 TString HoursMinSec(float fseconds){
338  long seconds = round(fseconds);
339  int minutes((seconds/60)%60), hours(seconds/3600);
340  TString hhmmss("");
341  if(hours<10) hhmmss += "0";
342  hhmmss += hours; hhmmss += ":";
343  if(minutes<10) hhmmss += "0";
344  hhmmss += minutes; hhmmss += ":";
345  if((seconds%60)<10) hhmmss += "0";
346  hhmmss += seconds%60;
347 
348  return hhmmss;
349 }
350 
351 TString AddCommas(double num){
352  TString result(""); result += num;
353  int posdot(result.First('.'));
354  if(posdot==-1) posdot = result.Length();
355  for(int ind(posdot-3); ind > 0; ind -= 3)
356  result.Insert(ind, ",");
357  return result;
358 }
359 
360 
361 TString RoundNumber(double num, int decimals, double denom){
362  if(denom==0 || !isfinite(num) || !isfinite(denom)) return " - ";
363  double neg = 1; if(num*denom<0) neg = -1;
364  num /= neg*denom; num += 0.5*pow(10.,-decimals);
365  if(abs(num) > 1e16) return "-";
366  long num_int = static_cast<long>(num);
367  long num_dec = static_cast<long>((1+num-num_int)*pow(10.,decimals));
368  TString s_dec = ""; s_dec += num_dec; s_dec.Remove(0,1);
369  TString result="";
370  if(neg<0) result+="-";
371  result+= num_int;
372  if(decimals>0) {
373  result+="."; result+=s_dec;
374  }
375 
376  TString afterdot = result;
377  afterdot.Remove(0,afterdot.First(".")+1);
378  for(int i=0; i<decimals-afterdot.Length(); i++)
379  result += "0";
380  if(result.Length()>15) cout<<"num "<<num<<", denom "<<denom<<" ----> "<<result<<endl;
381  return result;
382 }
383 
384 // Code from http://www.hongliangjie.com/2012/12/19/how-to-generate-gamma-random-variables/
385 // Parameter b could be theta...
386 double gsl_ran_gamma(const double a, const double b, TRandom3 &rand){
387  if (a < 1){
388  double u = rand.Uniform(1);
389  return gsl_ran_gamma(1.0 + a, b, rand) * pow (u, 1.0 / a);
390  }
391 
392  double x, v, u;
393  double d = a - 1.0 / 3.0;
394  double c = (1.0 / 3.0) / sqrt (d);
395 
396  while (1) {
397  do {
398  x = rand.Gaus(0, 1.0);
399  v = 1.0 + c * x;
400  }
401  while (v <= 0);
402 
403  v = v * v * v;
404  u = rand.Uniform(1);
405 
406  if (u < 1 - 0.0331 * x * x * x * x)
407  break;
408 
409  if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
410  break;
411  }
412 
413  return b * d * v;
414 }
415 
416 double intGaus(double mean, double sigma, double minX, double maxX){
417  return (TMath::Erf((maxX-mean)/sigma/sqrt(2.))-TMath::Erf((minX-mean)/sigma/sqrt(2.)))/2.;
418 }
419 
420 float deltaR(float eta1, float phi1, float eta2, float phi2){
421  return hypot(TVector2::Phi_mpi_pi(phi2-phi1), eta2-eta1);
422 }
423 
424 // Finds significance of observation Nbkg, for an expected background Nbkg+Eup_bkg-Edown_bkg
425 // The mean of the Poisson is Nbkg convolved with the asymmetric lognormal(Eup_bkg, Edown_bkg)
426 double Significance(double Nobs, double Nbkg, double Eup_bkg, double Edown_bkg){
427  double precision = 0.03; // Desired precision on the p-value
428  double Nmin = 1/pow(precision,2), Nmax = 5e7; // Nmax controls max sigmas achievable (5e7->5.5 sigma)
429  double Nbelow=0, Nabove=0, Nequal=0;
430  if(Edown_bkg<0) Edown_bkg = Eup_bkg; // If down uncertainty not specified, symmetric lognormal
431  if(Edown_bkg>Nbkg) {
432  cout<<"Down uncertainty ("<<Edown_bkg<<") has to be smaller than Nbkg ("<<Nbkg<<")"<<endl;
433  return -999.;
434  }
435  //if(Nobs==Nbkg) return 0.;
436  TRandom3 rand(1234);
437  double mu, valG;
438  while( (min(Nbelow,Nabove)+Nequal)<Nmin && (Nbelow+Nequal+Nabove)<Nmax){
439  // Convolving expected bkg with log-normal
440  mu = Nbkg;
441  valG = rand.Gaus(0,1);
442  if(mu==0) mu = fabs(valG)*Eup_bkg; // Apply 2-sided Gaussian uncertainty
443  else if(valG>=0) mu *= exp(valG*log(1+Eup_bkg/Nbkg));
444  else if(Edown_bkg<0.8*Nbkg) mu *= exp(-valG*log(1-Edown_bkg/Nbkg));
445  else mu = max(0., mu + valG*Edown_bkg);
446  // Finding if toy above the observed yield
447  double valPois = rand.PoissonD(mu);
448  if(valPois>Nobs) Nabove++;
449  else if(valPois==Nobs) Nequal++;
450  else Nbelow++;
451  }
452 
453  if(Nabove+Nequal==0){
454  cout<<"No toys above or at Nobs="<<Nobs<<" for Nbkg "<<Nbkg<<"+"<<Eup_bkg<<"-"<<Edown_bkg<<". Returning "
455  <<RooStats::PValueToSignificance(1/Nmax)<<endl;
456  return RooStats::PValueToSignificance(1/Nmax);
457  }
458  if(Nbelow+Nequal==0){
459  cout<<"No toys below or at Nobs="<<Nobs<<" for Nbkg "<<Nbkg<<"+"<<Eup_bkg<<"-"<<Edown_bkg<<". Returning "
460  <<RooStats::PValueToSignificance(1-1/Nmax)<<endl;
461  return RooStats::PValueToSignificance(1-1/Nmax);
462  }
463  return RooStats::PValueToSignificance((Nabove+Nequal/2.)/(Nbelow+Nequal+Nabove));
464 }
465 
466 // yields[Nobs][Nsam] has the entries for each sample for each observable going into kappa
467 // weights[Nobs][Nsam] has the average weight of each observable for each sample
468 // powers[Nobs] defines kappa = Product_obs{ Sum_sam{yields[sam][obs]*weights[sam][obs]}^powers[obs] }
469 double calcKappa(vector<vector<float> > &entries, vector<vector<float> > &weights,
470  vector<float> &powers, float &mSigma, float &pSigma, bool do_data,
471  bool verbose, double syst, bool do_plot, int nrep){
472  TRandom3 rand(1234);
473  int nbadk(0);
474  vector<float> fKappas;
475  double mean(0.), bignum(1e10);
476  // Doing kappa variations
477  for(int rep(0), irep(0); rep < nrep; rep++) {
478  fKappas.push_back(1.);
479  bool Denom_is0(false);
480  for(unsigned obs(0); obs < powers.size(); obs++) {
481  float observed(0.);
482  for(unsigned sam(0); sam < entries[obs].size(); sam++) {
483  // With a flat prior, the expected average of the Poisson with N observed is Gamma(N+1,1)
484  // Rounding the expected yield for data stats
485  if(do_data) observed += entries[obs][sam]*weights[obs][sam];
486  else observed += gsl_ran_gamma(entries[obs][sam]+1,1,rand)*weights[obs][sam];
487  } // Loop over samples
488  //if(do_data) observed = gsl_ran_gamma(static_cast<int>(0.5+observed)+1,1,rand);
489  if(do_data) observed = gsl_ran_gamma(observed+1,1,rand);
490  if(observed <= 0 && powers[obs] < 0) Denom_is0 = true;
491  else fKappas[irep] *= pow(observed, powers[obs]);
492  } // Loop over number of observables going into kappa
493 
494  if(syst>=0){
495  double factor = exp(rand.Gaus(0,log(1+syst)));
496  fKappas[irep] *= factor;
497  }
498  if(Denom_is0 && fKappas[irep]==0) {
499  fKappas.pop_back();
500  nbadk++;
501  }else {
502  if(Denom_is0) fKappas[irep] = bignum;
503  else mean += fKappas[irep];
504  irep++;
505  }
506  } // Loop over fluctuations of kappa (repetitions)
507  int ntot(nrep-nbadk);
508  mean /= static_cast<double>(ntot);
509 
510  sort(fKappas.begin(), fKappas.end());
511  double gSigma = intGaus(0,1,0,1);
512  int iMedian((nrep-nbadk+1)/2-1);
513  int imSigma(iMedian-static_cast<int>(gSigma*ntot)), ipSigma(iMedian+static_cast<int>(gSigma*ntot));
514  float median(fKappas[iMedian]);
515  mSigma = median-fKappas[imSigma]; pSigma = fKappas[ipSigma]-median;
516 
517  // Finding standard value
518  float stdval(1.);
519  bool infStd(false);
520  for(unsigned obs(0); obs < powers.size(); obs++) {
521  float stdyield(0.);
522  if(verbose) cout<<obs<<": ";
523  for(unsigned sam(0); sam < entries[obs].size(); sam++) {
524  if(verbose) cout<<"Yield"<<sam<<" "<<entries[obs][sam]*weights[obs][sam]
525  <<", N"<<sam<<" "<<entries[obs][sam]
526  <<", avW"<<sam<<" "<<weights[obs][sam]<<". ";
527  stdyield += entries[obs][sam]*weights[obs][sam];
528  }
529  if(verbose) cout<<" ==> Total yield "<<stdyield<<endl;
530  if(stdyield <= 0 && powers[obs] < 0) infStd = true;
531  else stdval *= pow(stdyield, powers[obs]);
532  } // Loop over number of observables going into kappa
533  if(infStd) stdval = median;
534  else {
535  int istd(0);
536  for(int rep(0); rep < ntot; rep++)
537  if(fKappas[rep]>stdval) {istd = rep; break;}
538  imSigma = istd-static_cast<int>(gSigma*ntot);
539  ipSigma = istd+static_cast<int>(gSigma*ntot);
540  if(imSigma<0){ // Adjusting the length of the interval in case imSigma has less than 1sigma
541  ipSigma += (-imSigma);
542  imSigma = 0;
543  }
544  if(ipSigma>=ntot){ // Adjusting the length of the interval in case ipSigma has less than 1sigma
545  imSigma -= (ipSigma-ntot+1);
546  ipSigma = ntot-1;
547  }
548  mSigma = stdval-fKappas[imSigma]; pSigma = fKappas[ipSigma]-stdval;
549  }
550 
551  TCanvas can;
552  int nbins(100);
553  double minH(stdval-3*mSigma), maxH(stdval+3*pSigma);
554  if(minH < fKappas[0]) minH = fKappas[0];
555  if(maxH > fKappas[ntot-1]) maxH = fKappas[ntot-1];
556  TH1D histo("h","",nbins, minH, maxH);
557  for(int rep(0); rep < ntot; rep++)
558  histo.Fill(fKappas[rep]);
559  //histo.SetBinContent(1, histo.GetBinContent(1)+nbadk);
560  //histo.SetBinContent(nbins, histo.GetBinContent(nbins)+histo.GetBinContent(nbins+1));
561  histo.Scale(1/histo.Integral());
562  histo.SetMaximum(histo.GetMaximum()*1.2);
563  histo.SetLineWidth(3);
564  histo.Draw();
565  histo.SetXTitle("Expected value");
566  histo.SetYTitle("Probability");
567  histo.Draw();
568  if(do_plot) can.SaveAs("test.eps");
569 
570  double mode(histo.GetBinLowEdge(histo.GetMaximumBin()));
571  if(verbose) cout<<"Std kappa = "<<stdval<<"+"<<pSigma<<"-"<<mSigma<<". Mean = "<<mean
572  <<". Mode = "<<mode<<". Median = "<<median<<endl;
573 
574  return stdval;
575 }
double Significance(double Nobs, double Nbkg, double Eup_bkg, double Edown_bkg)
Definition: utilities.cpp:426
string MakeDir(string prefix)
Definition: utilities.cpp:262
void MergeOverflow(TH1D &h, bool merge_underflow, bool merge_overflow)
Definition: utilities.cpp:300
string CodeToLatex(string code)
Definition: utilities.cpp:242
TString RoundNumber(double num, int decimals, double denom)
Definition: utilities.cpp:361
string CodeToRootTex(string code)
Definition: utilities.cpp:116
void AdjustDensityForBinWidth(TH1D &h)
Definition: utilities.cpp:273
STL namespace.
string FixedDigits(double x, int n_digits)
Definition: utilities.cpp:316
void Normalize(TH1D &h, double normalization, bool norm_per_avg_width)
Definition: utilities.cpp:290
void ReplaceAll(string &str, const string &orig, const string &rep)
Definition: utilities.cpp:52
double intGaus(double mean, double sigma, double minX, double maxX)
Definition: utilities.cpp:416
vector< string > Tokenize(const string &input, const string &tokens)
Definition: utilities.cpp:248
TString AddCommas(double num)
Definition: utilities.cpp:351
float deltaR(float eta1, float phi1, float eta2, float phi2)
Definition: utilities.cpp:420
double gsl_ran_gamma(const double a, const double b, TRandom3 &rand)
Definition: utilities.cpp:386
TString HoursMinSec(float fseconds)
Definition: utilities.cpp:337
string CodeToPlainText(string code)
Definition: utilities.cpp:79
set< string > Glob(const string &pattern)
Definition: utilities.cpp:27
std::mutex root_mutex
Definition: utilities.cpp:25
bool StartsWith(const string &str, const string &pat)
Definition: utilities.cpp:48
double calcKappa(vector< vector< float > > &entries, vector< vector< float > > &weights, vector< float > &powers, float &mSigma, float &pSigma, bool do_data, bool verbose, double syst, bool do_plot, int nrep)
Definition: utilities.cpp:469
string CopyReplaceAll(string str, const string &orig, const string &rep)
Definition: utilities.cpp:60
string FullTitle(const TH1 &h)
Definition: utilities.cpp:331
string Basename(const string &filename)
Definition: utilities.cpp:38
bool Contains(const string &str, const string &pat)
Definition: utilities.cpp:44
string execute(const string &cmd)
Definition: utilities.cpp:65