00001 00021 #include "jbltools/sfh/CutBinning.h" 00022 #include "jbltools/sfh/FillIterator.h" 00023 #include "jbltools/sfh/BaseCutBase.h" 00024 00025 #include <iostream> 00026 #include <cstring> 00027 00028 static const char *ident="@(#)$Id: CutBinning.C,v 1.4 2005/07/20 10:51:32 blist Exp $"; 00029 00030 00031 CutBinning::CutBinning (int nbins, const BaseCut *cuts[], const char *name) 00032 : BinningFun (nbins, -0.5, nbins-0.5, (name ? std::string(name) : "")), 00033 theCuts(nbins), binNames(nbins), binTitles(nbins), 00034 theFillIterator (0) 00035 { 00036 for (int i = 0; i < nbins; ++i) { 00037 const BaseCut *cut = cuts[i]; 00038 theCuts[i] = cut; 00039 binNames[i] = str(i, 3); 00040 binTitles[i] = cut ? std::string(cut->getName()) : ""; 00041 } 00042 genDefinition(); 00043 } 00044 00045 CutBinning::CutBinning (int nbins, BaseCut *cuts[], const char *name) 00046 : BinningFun (nbins, -0.5, nbins-0.5, (name ? std::string(name) : "")), 00047 theCuts(nbins), binNames(nbins), binTitles(nbins), 00048 theFillIterator (0) 00049 { 00050 for (int i = 0; i < nbins; ++i) { 00051 const BaseCut *cut = cuts[i]; 00052 theCuts[i] = cut; 00053 binNames[i] = str(i, 3); 00054 binTitles[i] = cut ? std::string(cut->getName()) : ""; 00055 } 00056 genDefinition(); 00057 } 00058 00059 CutBinning::CutBinning (int nbins, const BaseCut *cuts[], const char *titles[], const char *name) 00060 : BinningFun (nbins, -0.5, nbins-0.5, (name ? std::string(name) : "")), 00061 theCuts(nbins), binNames(nbins), binTitles(nbins), 00062 theFillIterator (0) 00063 { 00064 for (int i = 0; i < nbins; ++i) { 00065 const BaseCut *cut = cuts[i]; 00066 const char *title = titles[i]; 00067 theCuts[i] = cut; 00068 binNames[i] = str(i, 3); 00069 binTitles[i] = title ? std::string (title) : ""; 00070 } 00071 genDefinition(); 00072 } 00073 CutBinning::CutBinning (int nbins, BaseCut *cuts[], const char *titles[], const char *name) 00074 : BinningFun (nbins, -0.5, nbins-0.5, (name ? std::string(name) : "")), 00075 theCuts(nbins), binNames(nbins), binTitles(nbins), 00076 theFillIterator (0) 00077 { 00078 for (int i = 0; i < nbins; ++i) { 00079 const BaseCut *cut = cuts[i]; 00080 const char *title = titles[i]; 00081 theCuts[i] = cut; 00082 binNames[i] = str(i, 3); 00083 binTitles[i] = title ? std::string (title) : ""; 00084 } 00085 genDefinition(); 00086 } 00087 00088 bool CutBinning::operator() (int i) const { 00089 const BaseCut *cut = (i >= 0 && static_cast<unsigned int>(i) < theCuts.size()) ? theCuts[i] : 0; 00090 return cut ? (*cut)() : true; 00091 } 00092 00093 const char *CutBinning::getBinName(int i) const { 00094 if (i < 0 || i >= getNBins()) return 0; 00095 size_t len = binNames[i].size()+1; 00096 char *result = new char[len]; 00097 strncpy (result, binNames[i].c_str(), len); 00098 return result; 00099 } 00100 00101 const char *CutBinning::getBinTitle(int i) const { 00102 if (i < 0 || i >= getNBins()) return 0; 00103 size_t len = binTitles[i].size()+1; 00104 char *result = new char[len]; 00105 strncpy (result, binTitles[i].c_str(), len); 00106 return result; 00107 } 00108 00109 void CutBinning::genDefinition() { 00110 std::string definition_("("); 00111 int i = 0; 00112 while (i < getNBins()) { 00113 definition_ += binTitles[i]; 00114 if (++i >= getNBins()) break; 00115 definition_ += std::string(", "); 00116 } 00117 definition_ += ")"; 00118 setDefinition (definition_); 00119 } 00120 00121 void CutBinning::checkIterators() { 00122 theFillIterator = 0; 00123 const FillIterator *fi = 0; 00124 for (int i = 0; i < getNBins(); ++i) { 00125 const BaseCut *cut = theCuts[i]; 00126 if (cut && (fi = cut->getIterator())) { 00127 if (!theFillIterator) { 00128 theFillIterator = fi; 00129 } 00130 else if (theFillIterator != fi) { 00131 std::cerr << "CutBinning::checkIterators: cut " << i 00132 << " depends on iterator " << fi->getName() 00133 << ", other cuts depend on " << theFillIterator->getName() 00134 << "!\n (If bothe have the same name, it is a different instance!)\n"; 00135 } 00136 } 00137 } 00138 } 00139 00140 bool CutBinning::hasBinLabels() const { 00141 return true; 00142 } 00143 00144 const char *CutBinning::getBinLabel(int i) const { 00145 return getBinTitle(i); 00146 }