00001
00014 #include "jbltools/sfh/MatrixOfHistograms.h"
00015 #include "jbltools/sfh/SetOfHistograms.h"
00016 #include "jbltools/sfh/RegH1F.h"
00017 #include "jbltools/sfh/BinningFun.h"
00018
00019 #include <TFile.h>
00020 #include <TAxis.h>
00021
00022 #include <iostream>
00023 #include <cassert>
00024
00025 using std::endl;
00026 using std::cout;
00027 using std::cerr;
00028
00029 static const char *ident="@(#)$Id: MatrixOfHistograms.C,v 1.12 2005/07/08 15:01:34 blist Exp $";
00030
00031 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00032 const char* title_,
00033 const ROListPoR& rol,
00034 const BinningFunPoR& binning1_,
00035 const BinningFunPoR& binning2_)
00036 : MatrixOfH (name_, title_, rol, binning1_, binning2_)
00037 {
00038 }
00039
00040 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00041 const char* title_,
00042 Int_t nbinsx,
00043 Axis_t xlow,
00044 Axis_t xup,
00045 const ROListPoR& rol,
00046 const BinningFunPoR& binning1_,
00047 const BinningFunPoR& binning2_)
00048 : MatrixOfH (name_, title_, rol, binning1_, binning2_),
00049 axisbinning (nbinsx, xlow, xup)
00050 {
00051 initHistos ();
00052 }
00053
00054 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00055 const char* title_,
00056 Int_t nbinsx,
00057 const Double_t *xbins,
00058 const ROListPoR& rol,
00059 const BinningFunPoR& binning1_,
00060 const BinningFunPoR& binning2_)
00061 : MatrixOfH (name_, title_, rol, binning1_, binning2_),
00062 axisbinning (nbinsx, xbins)
00063 {
00064 initHistos ();
00065 }
00066
00067 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00068 const char* title_,
00069 Int_t nbinsx,
00070 const Float_t *xbins,
00071 const ROListPoR& rol,
00072 const BinningFunPoR& binning1_,
00073 const BinningFunPoR& binning2_)
00074 : MatrixOfH (name_, title_, rol, binning1_, binning2_),
00075 axisbinning (nbinsx, xbins)
00076 {
00077 initHistos ();
00078 }
00079
00080 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00081 const char* title_,
00082 const Binning& axisbinning_,
00083 const ROListPoR& rol,
00084 const BinningFunPoR& binning1_,
00085 const BinningFunPoR& binning2_)
00086 : MatrixOfH (name_, title_, rol, binning1_, binning2_),
00087 axisbinning (axisbinning_)
00088 {
00089 initHistos ();
00090 }
00091
00092 MatrixOfHistograms::MatrixOfHistograms (const char* name_,
00093 const char* title_,
00094 TFile& file,
00095 const ROListPoR& rol,
00096 const BinningFunPoR& binning1_,
00097 const BinningFunPoR& binning2_)
00098 : MatrixOfH (name_, title_, rol, binning1_, binning2_)
00099 {
00100 const BinningFun *binning1 = getBinning(1);
00101 const BinningFun *binning2 = getBinning(2);
00102 assert (binning1);
00103 assert (binning2);
00104 IndexType nbins1 = binning1->getNBins();
00105 IndexType nbins2 = binning2->getNBins();
00106 for (IndexType i = 0; i < nbins1; i++) {
00107 for (IndexType j = 0; j < nbins2; j++) {
00108 const char *hname = genBinName(i, j);
00109 new RegH1F (hname, file, *this);
00110 delete[] hname;
00111 }
00112 }
00113 cout << "MatrixOfHistograms::MatrixOfHistograms: name=" << getName()
00114 << ", title=" << getTitle()
00115 << "\nnbins1=" << nbins1 << ", nbins2=" << nbins2
00116 << ", entries=" << getEntries()
00117 << "\n read from file " << file.GetName() << endl;
00118
00119 if (rol.prol) rol.prol->registerObject (this);
00120 }
00121
00122
00123 void MatrixOfHistograms::initHisto (IndexType i, IndexType j) {
00124 const char *htitle = genBinTitle(i, j);
00125 const char *hname = genBinName(i, j);
00126 RegH1F * h = new RegH1F (hname, htitle, axisbinning, 0);
00127 registerObject (getBinNumber(i, j), h);
00128 delete[] hname;
00129 delete[] htitle;
00130 }
00131
00132
00133 MatrixOfHistograms::~MatrixOfHistograms () {
00134 }
00135
00136
00137 RegH1F *MatrixOfHistograms::getHisto (IndexType i, IndexType j) {
00138 return dynamic_cast<RegH1F *>(getEntry(getBinNumber (i, j)));
00139 }
00140
00141
00142 RegH1F *MatrixOfHistograms::getSummary(Option_t* option,
00143 const ROListPoR& rol) {
00144 bool dim2 = false;
00145
00146 TString opt = option;
00147 opt.ToLower();
00148 if (opt.Contains("2")) dim2 = true;
00149
00150 int number = 99999;
00151 if (opt.Contains("m")) number = 99998;
00152
00153 const char *title = getTitle();
00154 if (!title) title = "?";
00155 const char *name = getName();
00156 if (!name) name = "?";
00157
00158 int htitlen = strlen (title) + 5 + 1;
00159 char *htitle = new char[htitlen];
00160 snprintf (htitle, htitlen, "%s Summary", title);
00161 int hnamlen = strlen (name) + 1 + strlen ("Summary")+1;
00162 char *hname = new char[hnamlen];
00163 snprintf (hname, hnamlen, "%s%5d", hname, number);
00164 RegH1F *result = 0;
00165
00166 const BinningFun *binning1 = getBinning(1);
00167 const BinningFun *binning2 = getBinning(2);
00168 assert (binning1);
00169 assert (binning2);
00170 IndexType nbins1 = binning1->getNBins();
00171 IndexType nbins2 = binning2->getNBins();
00172
00173 if (dim2) {
00174 cerr << "MatrixOfHistograms::getSummary: 2-dim not yet implemented"
00175 << endl;
00176 } else {
00177 Int_t nbinsx = nbins1*nbins2;
00178 Axis_t xlow = -0.5;
00179 Axis_t xup = nbinsx-0.5;
00180 result = new RegH1F (hname, htitle, nbinsx, xlow, xup, rol);
00181 if (result == 0) {
00182 cerr << "MatrixOfHistograms::getSummary: Cannot create histo!"
00183 << endl;
00184 } else {
00185 for (IndexType i = 0; i < nbins1; i++) {
00186 for (IndexType j = 0; j < nbins1; j++) {
00187 RegH1F *h = getHisto (i, j);
00188 if (h) {
00189 Int_t ibin = i*nbins2 + j + 1;
00190 Stat_t content = 0;
00191 Stat_t error2 = 0;
00192 h->getHistInfo (option, content, error2);
00193 result->SetBinContent (ibin, content);
00194
00195
00196 result->SetBinError (ibin, sqrt(error2));
00197 }
00198 }
00199 }
00200 }
00201 }
00202 delete[] hname;
00203 delete[] htitle;
00204
00205 return result;
00206 }
00207 SetOfHistograms* MatrixOfHistograms::getSet (const char* name, const char* title, int idim,
00208 const ROListPoR& rol,
00209 int lowbin, int highbin) {
00210 BinningFun *binning1 = getBinning(1);
00211 BinningFun *binning2 = getBinning(2);
00212 assert (binning1);
00213 assert (binning2);
00214
00215 SetOfHistograms *result = 0;
00216
00217 IndexType low = (lowbin < 0) ? 0 : lowbin;
00218 IndexType high = (highbin < 0 || highbin < lowbin) ? 0 : highbin;
00219
00220 if (idim == 1) {
00221 if (highbin > binning2->getNBins()-1) high = binning2->getNBins()-1;
00222 result = new SetOfHistograms (name, title, rol, binning1);
00223 for (IndexType i = 0; i < static_cast<IndexType>(binning1->getNBins()); ++i) {
00224 RegH1F *h = getHisto (i, low);
00225 assert (h);
00226 const char *title = result->genBinTitle (i);
00227 const char *name = result->genBinName (i);
00228 RegH1F *sumhist = new RegH1F(name, title, *h, *result);
00229 delete[] title;
00230 delete[] name;
00231 for (IndexType j = low+1; j <= high; ++j) {
00232 sumhist->Add (getHisto (i, j));
00233 }
00234 }
00235 } else if (idim == 2) {
00236 if (highbin > binning1->getNBins()-1) high = binning1->getNBins()-1;
00237 result = new SetOfHistograms (name, title, rol, binning2);
00238 for (IndexType j = 0; j < static_cast<IndexType>(binning2->getNBins()); ++j) {
00239 RegH1F *h = getHisto (low, j);
00240 assert (h);
00241 const char *title = result->genBinTitle (j);
00242 const char *name = result->genBinName (j);
00243 RegH1F *sumhist = new RegH1F(name, title, *h, *result);
00244 delete[] title;
00245 delete[] name;
00246
00247 for (IndexType i = low+1; i <= high; ++i) {
00248 sumhist->Add (getHisto (i, j));
00249 }
00250 }
00251 } else {
00252 cerr << "MatrixOfHistograms::getSet: ERROR: non-existing dimension "
00253 << idim << " requested!" << endl;
00254 }
00255 return result;
00256 }
00257
00258 SetOfHistograms *MatrixOfHistograms::getSummarySet(const char *name, const char *title, int idim,
00259 Option_t* option,
00260 const ROListPoR& rol,
00261 const Float_t *xbinmargins) {
00262
00263 BinningFun *binning1 = getBinning(1);
00264 BinningFun *binning2 = getBinning(2);
00265 assert (binning1);
00266 assert (binning2);
00267
00268 SetOfHistograms *result = 0;
00269 if (idim == 1) {
00270 int nx = binning2->getNBins();
00271 if (xbinmargins == 0) {
00272 result = new SetOfHistograms (name, title, nx, -0.5, nx-0.5, rol, binning1);
00273 }
00274 else {
00275 result = new SetOfHistograms (name, title, nx, xbinmargins, rol, binning1);
00276 }
00277 for (IndexType i = 0; i < static_cast<IndexType>(binning1->getNBins()); ++i) {
00278 RegH1F *target = result->getHisto (i);
00279 for (IndexType j = 0; j < static_cast<IndexType>(binning2->getNBins()); ++j) {
00280 RegH1F *h = getHisto (i, j);
00281 if (target && h) {
00282 Stat_t content = 0;
00283 Stat_t error2 = 0;
00284 h->getHistInfo (option, content, error2);
00285 target->SetBinContent (j+1, content);
00286
00287 target->SetBinError (j+1, sqrt(error2));
00288 }
00289 }
00290 }
00291 } else if (idim == 2) {
00292 IndexType nx = binning1->getNBins();
00293 if (xbinmargins == 0) {
00294 result = new SetOfHistograms (name, title, nx, -0.5, nx-0.5, rol, binning2);
00295 }
00296 else {
00297 result = new SetOfHistograms (name, title, nx, xbinmargins, rol, binning2);
00298 }
00299 for (IndexType j = 0; j < static_cast<IndexType>(binning2->getNBins()); ++j) {
00300 RegH1F *target = result->getHisto (j);
00301 for (IndexType i = 0; i < static_cast<IndexType>(binning1->getNBins()); ++i) {
00302 RegH1F *h = getHisto (i, j);
00303 if (target && h) {
00304 Stat_t content = 0;
00305 Stat_t error2 = 0;
00306 h->getHistInfo (option, content, error2);
00307 target->SetBinContent (i+1, content);
00308
00309 target->SetBinError (i+1, sqrt(error2));
00310 }
00311 }
00312 }
00313 } else {
00314 cerr << "MatrixOfHistograms::getSet: ERROR: non-existing dimension "
00315 << idim << " requested!" << endl;
00316 }
00317 return result;
00318 }
00319
00320