00001
00029 #include "jbltools/sfh/IntFunBinning.h"
00030 #include "jbltools/sfh/IntFunBase.h"
00031
00032 #include <cassert>
00033 #include <cmath>
00034 #include <cstring>
00035 #include <cstdio>
00036
00037 static const char *ident="@(#)$Id: IntFunBinning.C,v 1.4 2006/06/30 11:05:00 blist Exp $";
00038
00039 IntFunBinning::IntFunBinning (const IntFunPoR& fun_,
00040 int xlow_,
00041 int xhigh_,
00042 const char *varname_)
00043 : BinningFun (xhigh_-xlow_+1, xlow_-0.5, xhigh_+0.5),
00044 fun(fun_.pif),
00045 varname(0),
00046 varnamelength(0)
00047 { assert (getNBins() >= 1);
00048 assert (getNBins() < 999);
00049 initvarname (varname_);
00050 }
00051
00052
00053
00054 void IntFunBinning::initvarname (const char *varname_) {
00055 if (!varname_) varname_ = "X";
00056 varnamelength = strlen (varname_);
00057 varname = new char[varnamelength+1];
00058 strncpy (varname, varname_, varnamelength+1);
00059 varname[varnamelength] = '\0';
00060 }
00061
00062
00063 int IntFunBinning::operator() () const {
00064 if (!fun) return -1;
00065 return getBin ((*fun)());
00066 }
00067
00068
00069
00070
00071 const char *IntFunBinning::getBinTitle(int i) const {
00072 size_t size = 20+varnamelength;
00073 char *title = new char [size];
00074 if (i < 0 || i > getNBins()) {
00075 snprintf (title, size, "illegal bin number!");
00076 } else if (i == getNBins()) {
00077 snprintf (title, size, "%s = %-3d-%-3d",
00078 varname, (int)(getLowerEdge()+0.51), (int)(getUpperEdge()-0.49));
00079 } else {
00080 assert (i >= 0 && i < getNBins());
00081 snprintf (title, size, "%s = %-3d",
00082 varname, (int)(0.5+0.5*(getLowerBinEdge(i)+getUpperBinEdge(i))));
00083 }
00084 return title;
00085 }
00086
00087
00088 const char *IntFunBinning::getBinName(int i) const{
00089 size_t size = 4;
00090 char *name = new char [size];
00091 if (i < 0 || i > getNBins()) {
00092 snprintf (name, size, "999");
00093 } else {
00094 assert (i >= 0 && i <= getNBins());
00095 snprintf (name, size, "%03d", i);
00096 }
00097 return name;
00098 }
00099
00100 IntFunBinning::~IntFunBinning() {
00101 delete[] varname;
00102 varname = 0;
00103 }
00104
00105 const FillIterator *IntFunBinning::getIterator() const {
00106 assert (fun);
00107 return fun->getIterator();
00108 }
00109