yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
Scatter3D.cc
Go to the documentation of this file.
1 #include "YODA/Scatter3D.h"
2 #include "YODA/Histo2D.h"
3 #include "YODA/Profile2D.h"
4 #include "YODA/Exceptions.h"
5 #include <sstream>
6 #include "yaml-cpp/yaml.h"
7 #ifdef YAML_NAMESPACE
8 #define YAML YAML_NAMESPACE
9 #endif
10 
11 namespace YODA {
12 
13 
14  Scatter3D mkScatter(const Histo2D& h, bool usefocus, bool binareadiv) {
15  Scatter3D rtn;
16  for (const std::string& a : h.annotations()) rtn.setAnnotation(a, h.annotation(a));
17  rtn.setAnnotation("Type", h.type());
18 
19  for (size_t i = 0; i < h.numBins(); ++i) {
20  const HistoBin2D& b = h.bin(i);
21 
23 
24  double x = b.xMid();
25  if (usefocus) {
26  try {
27  x = b.xFocus();
28  } catch (const LowStatsError& lse) {
29  x = b.xMid();
30  }
31  }
32  const double exminus = x - b.xMin();
33  const double explus = b.xMax() - x;
34 
35  double y = b.yMid();
36  if (usefocus) {
37  try {
38  y = b.yFocus();
39  } catch (const LowStatsError& lse) {
40  y = b.yMid();
41  }
42  }
43  const double eyminus = y - b.yMin();
44  const double eyplus = b.yMax() - y;
45 
47 
48  double z;
49  try {
50  z = b.sumW();
51  } catch (const Exception&) { // LowStatsError or WeightError
52  z = std::numeric_limits<double>::quiet_NaN();
53  }
54  if (binareadiv) z /= b.xWidth()*b.yWidth();
55  const double ez = b.relErr() * z;
56 
57 
58  Point3D pt(x, y, z, exminus, explus, eyminus, eyplus, ez, ez);
59  pt.setParent(&rtn);
60  rtn.addPoint(pt);
61  }
62 
63  assert(h.numBins() == rtn.numPoints());
64  return rtn;
65  }
66 
67 
68  Scatter3D mkScatter(const Profile2D& h, bool usefocus, bool usestddev) {
69  Scatter3D rtn;
70  for (const std::string& a : h.annotations())
71  rtn.setAnnotation(a, h.annotation(a));
72  rtn.setAnnotation("Type", h.type());
73  for (size_t i = 0; i < h.numBins(); ++i) {
74  const ProfileBin2D& b = h.bin(i);
75 
77 
78  double x = b.xMid();
79  if (usefocus) {
80  try {
81  x = b.xFocus();
82  } catch (const LowStatsError& lse) {
83  x = b.xMid();
84  }
85  }
86  const double exminus = x - b.xMin();
87  const double explus = b.xMax() - x;
88 
89  double y = b.yMid();
90  if (usefocus) {
91  try {
92  y = b.yFocus();
93  } catch (const LowStatsError& lse) {
94  y = b.yMid();
95  }
96  }
97  const double eyminus = y - b.yMin();
98  const double eyplus = b.yMax() - y;
99 
101 
102 
103  double z;
104  try {
105  z = b.mean();
106  } catch (const LowStatsError& lse) {
107  z = std::numeric_limits<double>::quiet_NaN();
108  }
109  double ez;
110  try {
111  ez = usestddev ? b.stdDev() : b.stdErr();
112  } catch (const LowStatsError& lse) {
113  ez = std::numeric_limits<double>::quiet_NaN();
114  }
115 
116  rtn.addPoint(x, y, z, exminus, explus, eyminus, eyplus, ez, ez);
117  }
118 
119  return rtn;
120  }
121 
122 
124  if (this->_variationsParsed) { return; }
125  if (!(this->hasAnnotation("ErrorBreakdown"))) { return; }
126  YAML::Node errorBreakdown;
127  errorBreakdown = YAML::Load(this->annotation("ErrorBreakdown"));
128  if (errorBreakdown.size()) {
129  for (size_t thisPointIndex = 0; thisPointIndex < this->numPoints(); ++thisPointIndex) {
130  Point3D& thispoint = this->_points[thisPointIndex];
131  YAML::Node variations = errorBreakdown[thisPointIndex];
132  for (const auto& variation : variations) {
133  const std::string variationName = variation.first.as<std::string>();
134  double eyp = variation.second["up"].as<double>();
135  double eym = variation.second["dn"].as<double>();
136  thispoint.setZErrs(eym,eyp,variationName);
137  }
138  }
139  this->_variationsParsed = true;
140  }
141  }
142 
143 
145  std::vector<std::string> Scatter3D::variations() const {
146  std::vector<std::string> vecvariations;
147  for (auto& point : this->_points) {
148  for (auto& it : point.errMap()) {
149  // if the variation is not already in the vector, add it!
150  if (std::find(vecvariations.begin(), vecvariations.end(), it.first) == vecvariations.end()) {
151  vecvariations.push_back(it.first);
152  }
153  }
154  }
155  return vecvariations;
156  }
157 
158 }
double yFocus() const
The mean y position in the bin, or the y midpoint if that is not available.
Definition: Bin2D.h:220
A Bin1D specialised for handling profile-type information.
Definition: ProfileBin2D.h:23
double yMax() const
Upper y limit of the bin (exclusive).
Definition: Bin2D.h:171
double xMin() const
Lower x limit of the bin (inclusive).
Definition: Bin2D.h:150
double stdErr() const
The standard error on the mean.
Definition: ProfileBin2D.h:134
double yMin() const
Lower y limit of the bin (inclusive).
Definition: Bin2D.h:166
double xWidth() const
Width of the bin in x.
Definition: Bin2D.h:193
Generic unspecialised YODA runtime error.
Definition: Exceptions.h:20
double stdDev() const
The std deviation of the z distribution about the mean.
Definition: ProfileBin2D.h:124
double xMax() const
Upper x limit of the bin (exclusive).
Definition: Bin2D.h:155
double xMid() const
Middle of the bin in x.
Definition: Bin2D.h:177
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter3D.h:203
ProfileBin2D & bin(size_t index)
Access a bin by index (non-const)
Definition: Profile2D.h:282
void setParent(Scatter *parent)
Definition: Point.h:114
void setZErrs(double ez, std::string source="")
Set symmetric z error (alias)
Definition: Point3D.h:318
void parseVariations()
Definition: Scatter3D.cc:123
std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter3D.cc:145
Point3D & point(size_t index)
Get a reference to the point with index index.
Definition: Scatter3D.h:221
A two-dimensional profile histogram.
Definition: Profile2D.h:32
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:18
Errors relating to insufficient (effective) statistics.
Definition: Exceptions.h:72
double mean() const
The mean of the z distribution.
Definition: ProfileBin2D.h:119
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
double xFocus() const
The mean x position in the bin, or the x midpoint if that is not available.
Definition: Bin2D.h:215
size_t numBins() const
Number of bins of this axis (not counting under/over flow)
Definition: Profile2D.h:298
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:13
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:25
A Bin2D specialised for handling histogram-type information.
Definition: HistoBin2D.h:21
const std::map< std::string, std::pair< double, double > > & errMap() const
Get error map for direction i.
Definition: Point3D.h:478
virtual std::string type() const
Get name of the analysis object type.
double sumW() const
The sum of weights.
Definition: Bin2D.h:316
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
HistoBin2D & bin(size_t index)
Access a bin by index (non-const version)
Definition: Histo2D.h:294
double relErr() const
The relative size of the error (same for either volume or height errors)
Definition: HistoBin2D.h:116
double yWidth() const
Width of the bin in y.
Definition: Bin2D.h:198
A two-dimensional histogram.
Definition: Histo2D.h:32
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
size_t numBins() const
Number of bins.
Definition: Histo2D.h:311
double yMid() const
Middle of the bin in y.
Definition: Bin2D.h:182
void addPoint(const Point3D &pt)
Insert a new point.
Definition: Scatter3D.h:240
std::vector< std::string > annotations() const