9 #ifndef LWH_Histogram1D_H 10 #define LWH_Histogram1D_H 15 #include "AIHistogram1D.h" 16 #include "ManagedObject.h" 42 : fax(new
Axis(n, lo, up)), vax(0),
43 sum(n + 2), sumw(n + 2), sumw2(n + 2), sumxw(n + 2), sumx2w(n + 2) {
52 sum(edges.size() + 1), sumw(edges.size() + 1), sumw2(edges.size() + 1),
53 sumxw(edges.size() + 1), sumx2w(edges.size() + 1) {
61 : IBaseHistogram(h), IHistogram(h), IHistogram1D(h),
ManagedObject(h),
62 fax(0), vax(0), sum(h.sum), sumw(h.sumw), sumw2(h.sumw2),
63 sumxw(h.sumxw), sumx2w(h.sumx2w) {
65 if ( vax ) ax = vax =
new VariAxis(*hvax);
66 else ax = fax =
new Axis(dynamic_cast<const Axis &>(*h.
ax));
104 throw std::runtime_error(
"LWH cannot handle annotations");
111 throw std::runtime_error(
"LWH cannot handle annotations");
127 sum = std::vector<int>(ax->bins() + 2);
128 sumw = std::vector<double>(ax->bins() + 2);
129 sumxw = std::vector<double>(ax->bins() + 2);
130 sumx2w = std::vector<double>(ax->bins() + 2);
131 sumw2 = std::vector<double>(ax->bins() + 2);
142 for (
int i = 2; i < ax->bins() + 2; ++i ) si += sum[i];
154 return entries() + extraEntries();
162 return sum[0] + sum[1];
173 for (
int i = 2; i < ax->bins() + 2; ++i ) {
188 for (
int i = 2; i < ax->bins() + 2; ++i ) sw += sumw[i];
198 return sumBinHeights() + sumExtraBinHeights();
206 return sumw[0] + sumw[1];
215 double minw = sumw[2];
216 for (
int i = 3; i < ax->bins() + 2; ++i ) minw = std::min(minw, sumw[i]);
226 double maxw = sumw[2];
227 for (
int i = 3; i < ax->bins() + 2; ++i ) maxw = std::max(maxw, sumw[i]);
238 bool fill(
double x,
double weight = 1.) {
239 int i = ax->coordToIndex(x) + 2;
242 sumxw[i] += x*weight;
243 sumx2w[i] += x*x*weight;
244 sumw2[i] += weight*weight;
245 return weight >= 0 && weight <= 1;
255 return sumw[i] != 0.0? sumxw[i]/sumw[i]:
256 ( vax? vax->binMidPoint(index): fax->binMidPoint(index) );
266 return sumw[i] == 0.0 || sum[i] < 2? ax->binWidth(index):
267 std::sqrt(std::max(sumw[i]*sumx2w[i] - sumxw[i]*sumxw[i], 0.0))/sumw[i];
277 return sum[index + 2];
287 return sumw[index + 2];
297 return std::sqrt(sumw2[index + 2]);
307 for (
int i = 2; i < ax->bins() + 2; ++i ) {
311 return s != 0.0? sx/s: 0.0;
322 for (
int i = 2; i < ax->bins() + 2; ++i ) {
327 return s != 0.0? std::sqrt(std::max(s*sx2 - sx*sx, 0.0))/s:
328 ax->upperEdge() - ax->lowerEdge();
347 return ax->coordToIndex(coord);
356 if ( ax->upperEdge() != h.
ax->upperEdge() ||
357 ax->lowerEdge() != h.
ax->lowerEdge() ||
358 ax->bins() != h.
ax->bins() )
return false;
359 for (
int i = 0; i < ax->bins() + 2; ++i ) {
361 sumw[i] += h.
sumw[i];
362 sumxw[i] += h.
sumxw[i];
364 sumw2[i] += h.
sumw2[i];
374 bool add(
const IHistogram1D & hist) {
375 return add(dynamic_cast<const Histogram1D &>(hist));
383 for (
int i = 0; i < ax->bins() + 2; ++i ) {
400 double oldintg = sumAllBinHeights();
401 if ( oldintg == 0.0 )
return;
402 for (
int i = 0; i < ax->bins() + 2; ++i ) {
403 double fac = intg/oldintg;
404 if ( i >= 2 ) fac /= (ax->binUpperEdge(i - 2) - ax->binLowerEdge(i - 2));
417 double intg = sumw[0] + sumw[1];
418 for (
int i = 2; i < ax->bins() + 2; ++i )
419 intg += sumw[i]*(ax->binUpperEdge(i - 2) - ax->binLowerEdge(i - 2));
427 void *
cast(
const std::string &)
const {
434 bool writeXML(std::ostream & os, std::string path, std::string name) {
435 os <<
" <histogram1d name=\"" << name
436 <<
"\"\n title=\"" << title()
437 <<
"\" path=\"" << path
438 <<
"\">\n <axis max=\"" << ax->upperEdge()
439 <<
"\" numberOfBins=\"" << ax->bins()
440 <<
"\" min=\"" << ax->lowerEdge()
441 <<
"\" direction=\"x\"";
444 for (
int i = 0, N = ax->bins() - 1; i < N; ++i )
445 os <<
" <binBorder value=\"" << ax->binUpperEdge(i) <<
"\"/>\n";
450 os <<
" <statistics entries=\"" << entries()
451 <<
"\">\n <statistic mean=\"" << mean()
452 <<
"\" direction=\"x\"\n rms=\"" << rms()
453 <<
"\"/>\n </statistics>\n <data1d>\n";
454 for (
int i = 0; i < ax->bins() + 2; ++i )
if ( sum[i] ) {
455 os <<
" <bin1d binNum=\"";
456 if ( i == 0 ) os <<
"UNDERFLOW";
457 else if ( i == 1 ) os <<
"OVERFLOW";
459 os <<
"\" entries=\"" << sum[i]
460 <<
"\" height=\"" << sumw[i]
461 <<
"\"\n error=\"" << std::sqrt(sumw2[i])
462 <<
"\" error2=\"" << sumw2[i]
463 <<
"\"\n weightedMean=\"" << binMean(i - 2)
464 <<
"\" weightedRms=\"" << binRms(i - 2)
467 os <<
" </data1d>\n </histogram1d>" << std::endl;
476 bool writeFLAT(std::ostream & os, std::string path, std::string name) {
477 os <<
"# " << path <<
"/" << name <<
" " << ax->lowerEdge()
478 <<
" " << ax->bins() <<
" " << ax->upperEdge()
479 <<
" \"" << title() <<
" \"" << std::endl;
480 for (
int i = 2; i < ax->bins() + 2; ++i )
481 os << 0.5*(ax->binLowerEdge(i - 2) + ax->binUpperEdge(i - 2)) <<
" " 482 << sumw[i] <<
" " << sqrt(sumw2[i]) <<
" " << sum[i] << std::endl;
std::vector< double > sumx2w
The weighted x-square-values.
double minBinHeight() const
Minimum height of the in-range bins, i.e.
double mean() const
The mean of the whole IHistogram1D.
std::string name() const
Get the Histogram's title.
const IAxis & axis() const
Get the x axis of the IHistogram1D.
bool writeFLAT(std::ostream &os, std::string path, std::string name)
Write out the histogram in a flat text file suitable for eg.
bool fill(double x, double weight=1.)
Fill the IHistogram1D with a value and the corresponding weight.
bool scale(double s)
Scale the contents of this histogram with the given factor.
VariAxis * vax
Pointer (possibly null) to a axis with fixed bin width.
double binHeight(int index) const
Total height of the corresponding bin (ie the sum of the weights in this bin).
std::string title() const
Get the Histogram's title.
Histogram1D(int n, double lo, double up)
Standard constructor.
bool writeXML(std::ostream &os, std::string path, std::string name)
Write out the histogram in the AIDA xml format.
void normalize(double intg)
Scale the given histogram so that the integral over all bins (including overflow) gives intg...
const IAnnotation & annotation() const
Not implemented in LWH.
int dimension() const
Get the Histogram's dimension.
std::vector< double > sumw
The weights.
double binMean(int index) const
The weighted mean of a bin.
IAnnotation * anno
dummy pointer to non-existen annotation.
virtual ~Histogram1D()
Destructor.
double sumAllBinHeights() const
Sum of the heights of all the IHistogram's bins, i.e in-range bins, UNDERFLOW and OVERFLOW...
Histogram1D(const Histogram1D &h)
Copy constructor.
Histogram1D(const std::vector< double > &edges)
Standard constructor for variable bin width.
std::vector< int > sum
The counts.
int binEntries(int index) const
Number of entries in the corresponding bin (ie the number of times fill was called for this bin)...
double rms() const
The RMS of the whole IHistogram1D.
void * cast(const std::string &) const
Not implemented in LWH.
double sumExtraBinHeights() const
Sum of heights in the UNDERFLOW and OVERFLOW bins.
An VariAxis represents a binned histogram axis.
User level interface for factory classes of Histograms (binned, unbinned, and profile).
User level interface to 1D Histogram.
double equivalentBinEntries() const
Number of equivalent entries, i.e.
double binError(int index) const
The error of a given bin.
int extraEntries() const
Number of entries in the UNDERFLOW and OVERFLOW bins.
int allEntries() const
Sum of the entries in all the IHistogram's bins, i.e in-range bins, UNDERFLOW and OVERFLOW...
bool setTitle(const std::string &title)
Set the histogram title.
An Axis represents a binned histogram axis.
bool reset()
Reset the Histogram; as if just created.
IAnnotation & annotation()
Not implemented in LWH.
double sumBinHeights() const
Sum of in-range bin heights in the IHistogram, UNDERFLOW and OVERFLOW bins are excluded.
std::string theTitle
The title.
Axis * fax
Pointer (possibly null) to a axis with fixed bin width.
The LWH namespace contains a Light-Weight Histogram package which implements the most rudimentary his...
bool add(const IHistogram1D &hist)
Add to this IHistogram1D the contents of another IHistogram1D.
double binRms(int index) const
The weighted RMS of a bin.
int entries() const
Get the number of in-range entries in the Histogram.
int coordToIndex(double coord) const
Get the bin number corresponding to a given coordinate along the x axis.
bool add(const Histogram1D &h)
Add to this Histogram1D the contents of another IHistogram1D.
std::vector< double > sumxw
The weighted x-values.
double maxBinHeight() const
Maximum height of the in-range bins, i.e.
double integral() const
Return the integral over the histogram bins assuming it has been normalize()d.
std::vector< double > sumw2
The squared weights.