Observable2D.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 HEPfit Collaboration
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  */
7 
8 #include "Observable2D.h"
9 #include <TFile.h>
10 #include <fstream>
11 #include <TROOT.h>
12 #include <limits>
13 
14 Observable2D::Observable2D(const std::string name_i,
15  const std::string thname_i,
16  const std::string thname2_i,
17  const std::string label_i,
18  const std::string label2_i,
19  const bool tMCMC_i,
20  const double min_i,
21  const double max_i,
22  const double min2_i,
23  const double max2_i,
24  ThObservable * tho_i,
25  ThObservable * tho2_i)
26 : Observable(name_i, thname_i, label_i, tMCMC_i, min_i, max_i, tho_i),
27  bin_min(2, 0.),
28  bin_max(2, 0.)
29 {
30  thname2 = thname2_i;
31  label2 = label2_i;
32  min2 = min2_i;
33  max2 = max2_i;
34  tho2 = tho2_i;
35  obsType2 = "";
36  filepath = "";
37  iterationNo2 = std::numeric_limits<int>::max();
38 }
39 
41 : Observable(o1d),
42  bin_min(2, 0.),
43  bin_max(2, 0.)
44 {
45  thname2 = "";
46  label2 = "";
47  min2 = 0.;
48  max2 = 0.;
49  tho2 = NULL;
50  obsType2 = "";
51  filepath = "";
52  iterationNo2 = std::numeric_limits<int>::max();
53 }
54 
56 : Observable(),
57  bin_min(2, 0.),
58  bin_max(2, 0.)
59 {
60  thname2 = "";
61  label2 = "";
62  min2 = 0.;
63  max2 = 0.;
64  tho2 = NULL;
65  obsType2 = "";
66  filepath = "";
67  iterationNo2 = std::numeric_limits<int>::max();
68 }
69 
71 : Observable(orig.name, orig.thname, orig.label, orig.tMCMC, orig.min, orig.max, orig.tho),
72  bin_min(orig.bin_min),
73  bin_max(orig.bin_max)
74 {
75 
76  distr = orig.distr;
77  filename = orig.filename;
78  histoname = orig.histoname;
79  ave = orig.ave;
80  errg = orig.errg;
81  errf = orig.errf;
82 
83  thname2 = orig.thname2;
84  label2 = orig.label2;
85  min2 = orig.min2;
86  max2 = orig.max2;
87  tho2 = orig.tho2;
88  filepath = orig.filepath;
90 }
91 
93 {}
94 
96 {
98  return thValue2;
99  } else {
102  return thValue2;
103  }
104 }
105 
106 void Observable2D::setLikelihoodFromHisto(std::string filename, std::string histoname)
107 {
108  this->filename = filename;
109  this->histoname = histoname;
110  TFile *lik2 = new TFile((filename + ".root").c_str(), "read");
111  TH2D *htmp2 = (TH2D*) (lik2->Get(histoname.c_str()));
112  if (htmp2 == NULL)
113  throw std::runtime_error("ERROR: nonexistent histogram called "
114  + histoname
115  + " in " + filename + ".root");
116  inhisto2d = (TH2D *) htmp2->Clone((filename + "/" + histoname).c_str());
117  inhisto2d->SetDirectory(gROOT);
118  std::cout << "added 2D input histogram " << name << std::endl;
119  setMin(inhisto2d->GetXaxis()->GetXmin());
120  setMax(inhisto2d->GetXaxis()->GetXmax());
121  setMin2(inhisto2d->GetYaxis()->GetXmin());
122  setMax2(inhisto2d->GetYaxis()->GetXmax());
123  lik2->Close();
124  delete lik2;
125 }
126 
127 double Observable2D::computeWeight(double th1, double th2)
128 {
129  double logprob;
130  if (distr.compare("file") == 0) {
131  int i = inhisto2d->FindBin(th1, th2);
132  if (inhisto2d->IsBinOverflow(i) || inhisto2d->IsBinUnderflow(i))
133  logprob = log(0.);
134  else
135  logprob = log(inhisto2d->GetBinContent(i));
136  //logprob = log(h->GetBinContent(h->GetXaxis()->FindBin(th1),h->GetYaxis()->FindBin(th2)));
137  } else if (distr.compare("weight") == 0) {
139  } else
140  throw std::runtime_error("ERROR: 2D MonteCarloEngine::Weight() called without file for "
141  + name);
142  return (logprob);
143 }
144 
145 int Observable2D::ParseObservable2D(std::string& type,
146  boost::tokenizer<boost::char_separator<char> >* tok,
147  boost::tokenizer<boost::char_separator<char> >::iterator& beg,
148  std::string& infilename,
149  std::ifstream& ifile,
150  int lineNo,
151  int rank)
152 {
153  if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1);
154  if (std::distance(tok->begin(), tok->end()) < 12) {
155  setName(*beg);
156  ++beg;
157  if (std::distance(tok->begin(), tok->end()) < 4) {
158  if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + name + " in " + infilename + " at line number" + boost::lexical_cast<std::string>(lineNo));
159  else sleep (2);
160  }
161  std::string toMCMC = *beg;
162  if (toMCMC.compare("MCMC") == 0)
163  setTMCMC(true);
164  else if (toMCMC.compare("noMCMC") == 0)
165  setTMCMC(false);
166  else {
167  if (rank == 0) throw std::runtime_error("ERROR: wrong MCMC flag in Observable2D" + name + " at line number:" + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ".\n");
168  else sleep(2);
169  }
170 
171  ++beg;
172  setDistr(*beg);
173  if (distr.compare("file") == 0) {
174  if (std::distance(tok->begin(), tok->end()) < 6) {
175  if(rank == 0) throw std::runtime_error("ERROR: lack of information on "+ *beg + " in " + infilename);
176  else sleep (2);
177  }
178  setFilename(filepath + *(++beg));
179  setHistoname(*(++beg));
180  }
181 
182  std::vector<double> min(2, 0.);
183  std::vector<double> max(2, 0.);
184  std::vector<double> ave(2, 0.);
185  std::vector<double> errg(2, 0.);
186  std::vector<double> errf(2, 0.);
187  std::vector<std::string> thname(2, "");
188  std::vector<std::string> label(2, "");
189  std::vector<std::string> type2D(2, "");
190  std::string line;
191  size_t pos = 0;
192  boost::char_separator<char> sep(" \t");
193  for (int i = 0; i < 2; i++) {
194  IsEOF = getline(ifile, line).eof();
195  if (line.empty() || line.at(0) == '#') {
196  if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in Observable2D please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
197  else sleep(2);
198  }
199  lineNo++;
200  boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
201  beg = mytok.begin();
202  type2D[i] = *beg;
203  if (type2D[i].compare("Observable") != 0 && type2D[i].compare("BinnedObservable") != 0 && type2D[i].compare("FunctionObservable") != 0) {
204  if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", expecting an Observable or BinnedObservable or FunctionObservable type here...\n");
205  else sleep(2);
206  }
207  ++beg;
208  thname[i] = *beg;
209  ++beg;
210  label[i] = *beg;
211  while ((pos = label[i].find("~", pos)) != std::string::npos)
212  label[i].replace(pos++, 1, " ");
213  ++beg;
214  min[i] = atof((*beg).c_str());
215  ++beg;
216  max[i] = atof((*beg).c_str());
217  if (distr.compare("weight") == 0) {
218  ++beg;
219  ave[i] = atof((*beg).c_str());
220  ++beg;
221  errg[i] = atof((*beg).c_str());
222  ++beg;
223  errf[i] = atof((*beg).c_str());
224  if (errg[i] == 0. && errg[i] == 0.) {
225  if (rank == 0) throw std::runtime_error("ERROR: The Gaussian and flat error in weight for " + name + " cannot both be 0. in the " + infilename + " file, line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
226  else sleep(2);
227  }
228  } else if (distr.compare("noweight") == 0 || distr.compare("file") == 0) {
229  if (type2D[i].compare("BinnedObservable") == 0 || type2D[i].compare("FunctionObservable") == 0) {
230  ++beg;
231  ++beg;
232  ++beg;
233  }
234  } else {
235  if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name + " in file " + infilename + ".\n");
236  else sleep(2);
237  }
238  if (type2D[i].compare("BinnedObservable") == 0) {
239  ++beg;
240  bin_min[i] = atof((*beg).c_str());
241  ++beg;
242  bin_max[i] = atof((*beg).c_str());
243  } else if (type2D[i].compare("FunctionObservable") == 0) {
244  ++beg;
245  bin_min[i] = atof((*beg).c_str());
246  ++beg;
247  }
248  }
249  setObsType(type2D[0]);
250  obsType2 = type2D[1];
251  setThname(thname[0]);
252  thname2 = thname[1];
253  setLabel(label[0]);
254  label2 = label[1];
255  setMin(min[0]);
256  min2 = min[1];
257  setMax(max[0]);
258  max2= max[1];
259  setAve(ave[0]);
260  ave2 = ave[1];
261  setErrg(errg[0]);
262  errg2 = errg[1];
263  setErrf(errf[0]);
264  errf2 = errf[1];
265  if (distr.compare("file") == 0) {
267  if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl;
268  }
269  return lineNo;
270  } else {
271  beg = ParseObservable(type, tok, beg, filepath, filename, rank);
272  ++beg;
273  std::string distr = *beg;
274  if (distr.compare("file") == 0) {
275  if (std::distance(tok->begin(), tok->end()) < 14) {
276  if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + infilename + ".\n");
277  else sleep(2);
278  }
279  setFilename(filepath + *(++beg));
280  setHistoname(*(++beg));
282  if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl;
283  } else if (distr.compare("noweight") == 0) {
284  } else {
285  if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name);
286  else sleep(2);
287  }
288  setDistr(distr);
289  ++beg;
290  thname2 = *beg;
291  ++beg;
292  std::string label = *beg;
293  size_t pos = 0;
294  while ((pos = label.find("~", pos)) != std::string::npos)
295  label.replace(pos, 1, " ");
296  label2 = label;
297  ++beg;
298  min2 = atof((*beg).c_str());
299  ++beg;
300  max2 = atof((*beg).c_str());
301  ++beg;
302  if (beg != tok->end())
303  if (rank == 0) std::cout << "WARNING: unread information in observable2D " << name << std::endl;
304  return lineNo;
305  }
306 }
virtual void setLikelihoodFromHisto(std::string filename, std::string histoname)
A set method to set the likelihood from which the experimental likelihood of the observable will be r...
std::string label2
A label for the second observable.
Definition: Observable2D.h:292
void setErrf(double errf)
A set method to fix the flat error of the observable.
Definition: Observable.h:166
void setErrg(double errg)
A set method to fix the gaussian error of the observable.
Definition: Observable.h:184
int ParseObservable2D(std::string &type, boost::tokenizer< boost::char_separator< char > > *tok, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &infilename, std::ifstream &ifile, int lineNo, int rank)
double ave2
The average value of the second observable.
Definition: Observable2D.h:295
virtual double computeThValue()=0
A member to be overloaded by the respective theory observable. class that calculates the value of the...
std::string obsType2
Type of the second Observable. 0: Observable, 1: HiggsObservable, 2: BinnedObservable, 3: FunctionObservable.
Definition: Observable2D.h:298
ThObservable * tho2
A pointer to an object of the ThObservable class.
Definition: Observable2D.h:299
double min
The minimum value of the observable.
Definition: Observable.h:449
double max
The maximum valus of the observable.
Definition: Observable.h:450
virtual double computeWeight()
A method to compute the weight associated with the observable.
Definition: Observable.h:112
double min2
The minimum value of the second observable.
Definition: Observable2D.h:293
virtual double computeWeight()
A method to compute the weight associated with the observable.
Definition: Observable2D.h:212
std::string histoname
The name of the histogram for the observable.
Definition: Observable.h:445
void setMin(double min)
A set method to fix the minimum value for the observable.
Definition: Observable.h:295
A class for analyzing observables pairwise.
Definition: Observable2D.h:24
A class for a model prediction of an observable.
Definition: ThObservable.h:22
double errf2
the flat error of the second observable.
Definition: Observable2D.h:297
double ave
The average value of the observable.
Definition: Observable.h:446
boost::tokenizer< boost::char_separator< char > >::iterator & ParseObservable(std::string &type, boost::tokenizer< boost::char_separator< char > > *tok, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &filepath, std::string &infilename, int rank)
The parser for Observables.
Definition: Observable.cpp:184
int getIterationNo() const
std::vector< double > bin_min
The minimum value of the bin.
Definition: Observable2D.h:301
void setThname(std::string thname)
A set method to fix the name of the observable as listed in ThFactory class.
Definition: Observable.h:349
double errg
The gaussian error of the observable.
Definition: Observable.h:447
double errg2
The gaussian error of the second observable.
Definition: Observable2D.h:296
void setAve(double ave)
A set method to fix the average value of the observable.
Definition: Observable.h:130
double thValue2
The theory value of the second observable.
Definition: Observable2D.h:305
A class for observables.
Definition: Observable.h:28
Observable2D()
The default constructor.
double errf
the flat error of the observable.
Definition: Observable.h:448
void setMax2(double max2)
A set method to fix the maximum value for the second observable.
Definition: Observable2D.h:128
void setName(std::string name)
A set method to fix the name for the observable.
Definition: Observable.h:313
int iterationNo2
Counts the iteration to help with caching.
Definition: Observable2D.h:304
std::string label
A label for the observable.
Definition: Observable.h:442
std::string thname2
The name for the second oservable as fixed in the ThObservable() class.
Definition: Observable2D.h:291
void setMin2(double min2)
A set method to fix the minimum value for the second observable.
Definition: Observable2D.h:146
std::string thname
The name for the oservable as fixed in the ThObservable class.
Definition: Observable.h:441
void setFilename(std::string filename_i)
Definition: Observable.h:198
void setDistr(std::string distr)
A set method to fix the name of the distribution of the observable.
Definition: Observable.h:148
bool IsEOF
A bolean that is true if the end of file is reached.
Definition: Observable2D.h:306
double max2
The maximum valus of the second observable.
Definition: Observable2D.h:294
double computeTheoryValue2()
A method to access the computed theory value of the second observable.
complex log(const complex &z)
void setMax(double max)
A set method to fix the maximum value for the observable.
Definition: Observable.h:277
void setTMCMC(bool tMCMC)
A set method to fix the observable's inclusion in the MCMC listing.
Definition: Observable.h:331
std::string name
A name for the observable.
Definition: Observable.h:440
std::string filename
The name of the file containing the experimental likelihood for the observable.
Definition: Observable.h:444
void setLabel(std::string label)
A set method to fix the label for the observable.
Definition: Observable.h:259
std::vector< double > bin_max
The maximum value of the bin.
Definition: Observable2D.h:302
const StandardModel & getModel()
A get method to get the model.
Definition: ThObservable.h:97
TH2D * inhisto2d
2D Histogram containing the experimental likelihood for the observable.
Definition: Observable2D.h:300
void setHistoname(std::string histoname_i)
A set method to set the name of the histogram containing the likelihood.
Definition: Observable.h:241
std::string distr
The name of the distribution of the the observable.
Definition: Observable.h:443
virtual ~Observable2D()
The default destructor.
void setObsType(std::string &obsType_s)
A set method to set the Observable type.
Definition: Observable.h:367
std::string filepath
The path to the file being parsed.
Definition: Observable2D.h:303