00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef LWH_HistogramFactory_H
00010 #define LWH_HistogramFactory_H
00011
00012
00013
00014
00015 #include "AIHistogramFactory.h"
00016 #include "Histogram1D.h"
00017 #include "Tree.h"
00018 #include <string>
00019 #include <stdexcept>
00020
00021 namespace LWH {
00022
00023 using namespace AIDA;
00024
00031 class HistogramFactory: public IHistogramFactory {
00032
00033 public:
00034
00038 HistogramFactory(Tree & t)
00039 : tree(&t) {}
00040
00044 virtual ~HistogramFactory() {}
00045
00051 bool destroy(IBaseHistogram * hist) {
00052 IManagedObject * mo = dynamic_cast<IManagedObject *>(hist);
00053 if ( !mo ) return false;
00054 return tree->rm(tree->findPath(*mo));
00055 }
00056
00060 ICloud1D * createCloud1D(const std::string &, const std::string &,
00061 int = -1, const std::string & = "") {
00062 return error<ICloud1D>("ICloud1D");
00063 }
00064
00068 ICloud1D * createCloud1D(const std::string &) {
00069 return error<ICloud1D>("ICloud1D");
00070 }
00071
00075 ICloud1D * createCopy(const std::string &, const ICloud1D &) {
00076 return error<ICloud1D>("ICloud1D");
00077 }
00078
00082 ICloud2D * createCloud2D(const std::string &, const std::string &, int = -1,
00083 const std::string & = "") {
00084 return error<ICloud2D>("ICloud2D");
00085 }
00086
00087
00091 ICloud2D * createCloud2D(const std::string &) {
00092 return error<ICloud2D>("ICloud2D");
00093 }
00094
00098 ICloud2D * createCopy(const std::string &, const ICloud2D &) {
00099 return error<ICloud2D>("ICloud2D");
00100 }
00101
00105 ICloud3D * createCloud3D(const std::string &, const std::string &, int = -1,
00106 const std::string & = "") {
00107 return error<ICloud3D>("ICloud3D");
00108 }
00109
00113 ICloud3D * createCloud3D(const std::string &) {
00114 return error<ICloud3D>("ICloud3D");
00115 }
00116
00120 ICloud3D * createCopy(const std::string &, const ICloud3D &) {
00121 return error<ICloud3D>("ICloud3D");
00122 }
00123
00140 IHistogram1D *
00141 createHistogram1D(const std::string & path, const std::string & title,
00142 int nBins, double lowerEdge, double upperEdge,
00143 const std::string & = "") {
00144 Histogram1D * hist = new Histogram1D(nBins, lowerEdge, upperEdge);
00145 hist->setTitle(title);
00146 if ( !tree->insert(path, hist) ) {
00147 delete hist;
00148 hist = 0;
00149 throw std::runtime_error("LWH could not create histogram '"
00150 + title + "'." );
00151 }
00152 return hist;
00153 }
00154
00167 IHistogram1D *
00168 createHistogram1D(const std::string & pathAndTitle,
00169 int nBins, double lowerEdge, double upperEdge) {
00170 std::string title = pathAndTitle.substr(pathAndTitle.rfind('/') + 1);
00171 return createHistogram1D(pathAndTitle, title, nBins, lowerEdge, upperEdge);
00172 }
00173
00174
00187 IHistogram1D *
00188 createHistogram1D(const std::string & path, const std::string & title,
00189 const std::vector<double> & binEdges,
00190 const std::string & = "") {
00191 Histogram1D * hist = new Histogram1D(binEdges);
00192 hist->setTitle(title);
00193 if ( !tree->insert(path, hist) ) {
00194 delete hist;
00195 hist = 0;
00196 throw std::runtime_error("LWH could not create histogram '"
00197 + title + "'." );
00198 }
00199 return hist;
00200 }
00201
00212 IHistogram1D *
00213 createCopy(const std::string & path, const IHistogram1D & hist) {
00214 Histogram1D * h = new Histogram1D(dynamic_cast<const Histogram1D &>(hist));
00215 h->setTitle(path.substr(path.rfind('/') + 1));
00216 if ( !tree->insert(path, h) ) {
00217 delete h;
00218 h = 0;
00219 throw std::runtime_error("LWH could not create a copy of histogram '"
00220 + hist.title() + "'." );
00221 }
00222 return h;
00223 }
00224
00228 IHistogram2D * createHistogram2D(const std::string &, const std::string &,
00229 int, double, double, int, double, double,
00230 const std::string & = "") {
00231 return error<IHistogram2D>("IHistogram2D");
00232 }
00233
00237 IHistogram2D * createHistogram2D(const std::string &,
00238 int, double, double, int, double, double) {
00239 return error<IHistogram2D>("IHistogram2D");
00240 }
00241
00245 IHistogram2D * createHistogram2D(const std::string &, const std::string &,
00246 const std::vector<double> &,
00247 const std::vector<double> &,
00248 const std::string & = "") {
00249 return error<IHistogram2D>("IHistogram2D");
00250 }
00251
00255 IHistogram2D * createCopy(const std::string &, const IHistogram2D &) {
00256 return error<IHistogram2D>("IHistogram2D");
00257 }
00258
00262 IHistogram3D * createHistogram3D(const std::string &, const std::string &,
00263 int, double, double, int, double, double,
00264 int, double, double,
00265 const std::string & = "") {
00266 return error<IHistogram3D>("IHistogram3D");
00267 }
00268
00272 IHistogram3D * createHistogram3D(const std::string &, int, double, double,
00273 int, double, double, int, double, double) {
00274 return error<IHistogram3D>("IHistogram3D");
00275 }
00276
00280 IHistogram3D * createHistogram3D(const std::string &, const std::string &,
00281 const std::vector<double> &,
00282 const std::vector<double> &,
00283 const std::vector<double> &,
00284 const std::string & = "") {
00285 return error<IHistogram3D>("IHistogram3D");
00286 }
00287
00291 IHistogram3D * createCopy(const std::string &, const IHistogram3D &) {
00292 return error<IHistogram3D>("IHistogram3D");
00293 }
00294
00298 IProfile1D * createProfile1D(const std::string &, const std::string &,
00299 int, double, double, const std::string & = "") {
00300 return error<IProfile1D>("IProfile1D");
00301 }
00302
00306 IProfile1D * createProfile1D(const std::string &, const std::string &,
00307 int, double, double, double, double,
00308 const std::string & = "") {
00309 return error<IProfile1D>("IProfile1D");
00310 }
00311
00315 IProfile1D * createProfile1D(const std::string &, const std::string &,
00316 const std::vector<double> &,
00317 const std::string & = "") {
00318 return error<IProfile1D>("IProfile1D");
00319 }
00320
00324 IProfile1D * createProfile1D(const std::string &, const std::string &,
00325 const std::vector<double> &, double, double,
00326 const std::string & = "") {
00327 return error<IProfile1D>("IProfile1D");
00328 }
00329
00333 IProfile1D * createProfile1D(const std::string &, int, double, double) {
00334 return error<IProfile1D>("IProfile1D");
00335 }
00336
00340 IProfile1D * createProfile1D(const std::string &,
00341 int, double, double, double, double) {
00342 return error<IProfile1D>("IProfile1D");
00343 }
00344
00348 IProfile1D * createCopy(const std::string &, const IProfile1D &) {
00349 return error<IProfile1D>("IProfile1D");
00350 }
00351
00355 IProfile2D * createProfile2D(const std::string &, const std::string &,
00356 int, double, double, int, double, double,
00357 const std::string & = "") {
00358 return error<IProfile2D>("IProfile2D");
00359 }
00360
00364 IProfile2D * createProfile2D(const std::string &, const std::string &,
00365 int, double, double, int,
00366 double, double, double, double,
00367 const std::string & = "") {
00368 return error<IProfile2D>("IProfile2D");
00369 }
00370
00374 IProfile2D * createProfile2D(const std::string &, const std::string &,
00375 const std::vector<double> &,
00376 const std::vector<double> &,
00377 const std::string & = "") {
00378 return error<IProfile2D>("IProfile2D");
00379 }
00380
00384 IProfile2D * createProfile2D(const std::string &, const std::string &,
00385 const std::vector<double> &,
00386 const std::vector<double> &,
00387 double, double, const std::string & = "") {
00388 return error<IProfile2D>("IProfile2D");
00389 }
00390
00394 IProfile2D * createProfile2D(const std::string &, int, double, double,
00395 int, double, double) {
00396 return error<IProfile2D>("IProfile2D");
00397 }
00398
00402 IProfile2D * createProfile2D(const std::string &, int, double, double,
00403 int, double, double, double, double) {
00404 return error<IProfile2D>("IProfile2D");
00405 }
00406
00410 IProfile2D * createCopy(const std::string &, const IProfile2D &) {
00411 return error<IProfile2D>("IProfile2D");
00412 }
00413
00425 Histogram1D * add(const std::string & path,
00426 const Histogram1D & hist1, const Histogram1D & hist2) {
00427 if ( !checkBins(hist1, hist2) ) return 0;
00428 Histogram1D * h = new Histogram1D(hist1);
00429 h->setTitle(path.substr(path.rfind('/') + 1));
00430 h->add(hist2);
00431 if ( !tree->insert(path, h) ) return 0;
00432 return h;
00433 }
00434
00446 IHistogram1D * add(const std::string & path,
00447 const IHistogram1D & hist1, const IHistogram1D & hist2) {
00448 return add(path, dynamic_cast<const Histogram1D &>(hist1),
00449 dynamic_cast<const Histogram1D &>(hist2));
00450 }
00451
00463 Histogram1D * subtract(const std::string & path,
00464 const Histogram1D & h1, const Histogram1D & h2) {
00465 if ( !checkBins(h1, h2) ) return 0;
00466 Histogram1D * h = new Histogram1D(h1);
00467 h->setTitle(path.substr(path.rfind('/') + 1));
00468 for ( int i = 0; i < h->ax->bins() + 2; ++i ) {
00469 h->sum[i] += h2.sum[i];
00470 h->sumw[i] -= h2.sumw[i];
00471 h->sumw2[i] += h2.sumw2[i];
00472 }
00473 if ( !tree->insert(path, h) ) return 0;
00474 return h;
00475 }
00476
00488 IHistogram1D * subtract(const std::string & path, const IHistogram1D & hist1,
00489 const IHistogram1D & hist2) {
00490 return subtract(path, dynamic_cast<const Histogram1D &>(hist1),
00491 dynamic_cast<const Histogram1D &>(hist2));
00492 }
00493
00505 Histogram1D * multiply(const std::string & path,
00506 const Histogram1D & h1, const Histogram1D & h2) {
00507 if ( !checkBins(h1, h2) ) return 0;
00508 Histogram1D * h = new Histogram1D(h1);
00509 h->setTitle(path.substr(path.rfind('/') + 1));
00510 for ( int i = 0; i < h->ax->bins() + 2; ++i ) {
00511 h->sum[i] *= h2.sum[i];
00512 h->sumw[i] *= h2.sumw[i];
00513 h->sumxw[i] *= h2.sumw[i];
00514 h->sumw2[i] += h1.sumw[i]*h1.sumw[i]*h2.sumw2[i] +
00515 h2.sumw[i]*h2.sumw[i]*h1.sumw2[i];
00516 }
00517 if ( !tree->insert(path, h) ) return 0;
00518 return h;
00519 }
00520
00532 IHistogram1D * multiply(const std::string & path, const IHistogram1D & hist1,
00533 const IHistogram1D & hist2) {
00534 return multiply(path, dynamic_cast<const Histogram1D &>(hist1),
00535 dynamic_cast<const Histogram1D &>(hist2));
00536 }
00537
00549 Histogram1D * divide(const std::string & path,
00550 const Histogram1D & h1, const Histogram1D & h2) {
00551 if ( !checkBins(h1, h2) ) return 0;
00552 Histogram1D * h = new Histogram1D(h1);
00553 h->setTitle(path.substr(path.rfind('/') + 1));
00554 for ( int i = 0; i < h->ax->bins() + 2; ++i ) {
00555 if ( h2.sum[i] == 0 || h2.sumw[i] == 0.0 ) {
00556 h->sum[i] = 0;
00557 h->sumw[i] = h->sumw2[i] = 0.0;
00558 continue;
00559 }
00560 h->sum[i] /= h2.sum[i];
00561 h->sumw[i] /= h2.sumw[i];
00562 h->sumxw[i] /= h2.sumw[i];
00563 h->sumw2[i] = h1.sumw2[i]/(h2.sumw[i]*h2.sumw[i]) +
00564 h1.sumw[i]*h1.sumw[i]*h2.sumw2[i]/
00565 (h2.sumw[i]*h2.sumw[i]*h2.sumw[i]*h2.sumw[i]);
00566 }
00567 if ( !tree->insert(path, h) ) return 0;
00568 return h;
00569 }
00570
00582 IHistogram1D * divide(const std::string & path, const IHistogram1D & hist1,
00583 const IHistogram1D & hist2) {
00584 return divide(path, dynamic_cast<const Histogram1D &>(hist1),
00585 dynamic_cast<const Histogram1D &>(hist2));
00586 }
00587
00591 bool checkBins(const Histogram1D & h1, const Histogram1D & h2) const {
00592 if ( h1.ax->upperEdge() != h2.ax->upperEdge() ||
00593 h1.ax->lowerEdge() != h2.ax->lowerEdge() ||
00594 h1.ax->bins() != h2.ax->bins() ) return false;
00595 if ( h1.fax && h2.fax ) return true;
00596 for ( int i = 0; i < h1.ax->bins(); ++i ) {
00597 if ( h1.ax->binUpperEdge(i) != h2.ax->binUpperEdge(i) ||
00598 h1.ax->binLowerEdge(i) != h2.ax->binLowerEdge(i) ) return false;
00599 }
00600 return true;
00601 }
00602
00606 IHistogram2D * add(const std::string &,
00607 const IHistogram2D &, const IHistogram2D &) {
00608 return error<IHistogram2D>("2D histograms");
00609 }
00610
00614 IHistogram2D * subtract(const std::string &,
00615 const IHistogram2D &, const IHistogram2D &) {
00616 return error<IHistogram2D>("2D histograms");
00617 }
00618
00622 IHistogram2D * multiply(const std::string &,
00623 const IHistogram2D &, const IHistogram2D &) {
00624 return error<IHistogram2D>("2D histograms");
00625 }
00626
00630 IHistogram2D * divide(const std::string &,
00631 const IHistogram2D &, const IHistogram2D &) {
00632 return error<IHistogram2D>("2D histograms");
00633 }
00634
00638 IHistogram3D * add(const std::string &,
00639 const IHistogram3D &, const IHistogram3D &) {
00640 return error<IHistogram3D>("3D histograms");
00641 }
00642
00646 IHistogram3D * subtract(const std::string &,
00647 const IHistogram3D &, const IHistogram3D &) {
00648 return error<IHistogram3D>("3D histograms");
00649 }
00650
00654 IHistogram3D * multiply(const std::string &,
00655 const IHistogram3D &, const IHistogram3D &) {
00656 return error<IHistogram3D>("3D histograms");
00657 }
00658
00662 IHistogram3D * divide(const std::string &,
00663 const IHistogram3D &, const IHistogram3D &) {
00664 return error<IHistogram3D>("3D histograms");
00665 }
00666
00671 IHistogram1D * projectionX(const std::string &, const IHistogram2D &) {
00672 return error<IHistogram1D>("2D histograms");
00673 }
00674
00679 IHistogram1D * projectionY(const std::string &, const IHistogram2D &) {
00680 return error<IHistogram1D>("2D histograms");
00681 }
00682
00687 IHistogram1D * sliceX(const std::string &, const IHistogram2D &, int) {
00688 return error<IHistogram1D>("2D histograms");
00689 }
00690
00695 IHistogram1D * sliceY(const std::string &, const IHistogram2D &, int) {
00696 return error<IHistogram1D>("2D histograms");
00697 }
00698
00703 IHistogram1D * sliceX(const std::string &, const IHistogram2D &, int, int) {
00704 return error<IHistogram1D>("2D histograms");
00705 }
00706
00711 IHistogram1D * sliceY(const std::string &, const IHistogram2D &, int, int) {
00712 return error<IHistogram1D>("2D histograms");
00713 }
00714
00719 IHistogram2D * projectionXY(const std::string &, const IHistogram3D &) {
00720 return error<IHistogram2D>("2D histograms");
00721 }
00722
00727 IHistogram2D * projectionXZ(const std::string &, const IHistogram3D &) {
00728 return error<IHistogram2D>("2D histograms");
00729 }
00730
00735 IHistogram2D * projectionYZ(const std::string &, const IHistogram3D &) {
00736 return error<IHistogram2D>("2D histograms");
00737 }
00738
00744 IHistogram2D * sliceXY(const std::string &, const IHistogram3D &, int, int) {
00745 return error<IHistogram2D>("2D histograms");
00746 }
00747
00753 IHistogram2D * sliceXZ(const std::string &, const IHistogram3D &, int, int) {
00754 return error<IHistogram2D>("2D histograms");
00755 }
00756
00762 IHistogram2D * sliceYZ(const std::string &, const IHistogram3D &, int, int) {
00763 return error<IHistogram2D>("2D histograms");
00764 }
00765
00766
00767 private:
00768
00770 template <typename T>
00771 static T * error(std::string feature) {
00772 throw std::runtime_error("LWH cannot handle " + feature + ".");
00773 }
00774
00776 Tree * tree;
00777
00778 };
00779
00780 }
00781
00782 #endif