Anaphe Home Page Reference Documentation

Main Page     Namespaces     Classes     Source Code    

AIDAHistoManager.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // 
00004 // Copyright (C) CERN, Geneva 
00005 // 
00006 // implementation file for class AIDAHistoManager 
00007 // 
00008 
00009 
00010 
00011 
00012 #include "AIDAHistoManager.h"
00013 #include <assert.h>
00014 #include "Interfaces/IAnnotation.h"
00015 #include "Interfaces/IHistogram1D.h"
00016 #include "Interfaces/IHistogram2D.h"
00017 #include "Interfaces/IHistogram3D.h"
00018 #include "Interfaces/IProfileHistogram.h"
00019 #include "Interfaces/IHistoFactory.h"
00020 
00021 
00022 using Anaphe::IAnnotation;
00023 using Anaphe::IHistogramFactory;
00024 using Anaphe::IHistoFactory;
00025 using Anaphe::IHistogram1D;
00026 using Anaphe::IHistogram2D;
00027 using Anaphe::IHistogram3D;
00028 using Anaphe::IProfileHistogram;
00029 
00030 using Anaphe::AIDA_HBook::AIDAHistoManager;
00031 
00033 //                               //
00034 // pseudo-new function for proxy //
00035 //                               //
00037 
00038 Anaphe::IHistoManager* createIHistoManager(void) 
00039 {
00040   return new AIDAHistoManager();
00041 }
00042 
00043 
00044 
00045 
00046 
00048 //                             //
00049 // constructors and destructor //
00050 //                             //
00052 
00053 AIDAHistoManager::AIDAHistoManager(IHistoFactory* f)
00054   : hFact         (f),
00055     ownsFactory   (false),
00056     overwriteHisto(true),
00057     warnOverwrite (true)
00058 {
00059   if (!hFact) {
00060     hFact = static_cast<IHistoFactory*>((*createIHistoFactory)());
00061     ownsFactory = true;
00062   }
00063   assert(hFact);
00064   if (hFact) hFact->setManager(this);
00065   else crisisMessage("AIDAHistoManager");
00066 }
00067 
00068 
00069 
00070 
00071 AIDAHistoManager::~AIDAHistoManager(void)
00072 {
00073   cleanUp();
00074   if (ownsFactory) delete hFact;
00075 }
00076 
00077 
00078 
00080 //                //
00081 // public methods //
00082 //                //
00084 
00085 bool AIDAHistoManager::unregister(IHistogram* h) 
00086 {
00087   if (!h) return false;
00088   for (map1DIt i = histo1DList.begin(); i != histo1DList.end(); ++i) {
00089     if (i->second == h) {
00090       histo1DList.erase(i);
00091       return true;
00092     }
00093   }
00094   for (map2DIt i = histo2DList.begin(); i != histo2DList.end(); ++i) {
00095     if (i->second == h) {
00096       histo2DList.erase(i);
00097       return true;
00098     }
00099   }
00100   for (mapProfIt i = histoProfList.begin(); i != histoProfList.end(); ++i) {
00101     if (i->second == h) {
00102       histoProfList.erase(i);
00103       return true;
00104     }
00105   }
00106   return true;
00107 }
00108 
00109 
00110 
00111 
00112 bool AIDAHistoManager::register1D(IHistogram1D* h) 
00113 {
00114   if (h == 0) return false;
00115   AIDA_STD::string id = h->annotation()->find("ID");
00116   if (!checkAndDeleteHisto(id)) return false;
00117   histo1DList[id] = h; 
00118   return true;
00119 }
00120 
00121 
00122 
00123 
00124 bool AIDAHistoManager::register2D(IHistogram2D* h) 
00125 {
00126   if (h == 0) return false;
00127   AIDA_STD::string id = h->annotation()->find("ID");
00128   if (!checkAndDeleteHisto(id)) return false;
00129   histo2DList[id] = h; 
00130   return true;
00131 }
00132 
00133 
00134 
00135 
00136 bool AIDAHistoManager::register3D(IHistogram3D*)
00137 {
00138   notInHBOOKMessage("register3D"); 
00139   return false;
00140 }
00141 
00142 
00143 
00144 
00145 bool AIDAHistoManager::registerProf(IProfileHistogram* h) 
00146 {
00147   //ND Profile histograms should have a range in y, surely?
00148   if (h == 0) return false;
00149   AIDA_STD::string id = h->annotation()->find("ID");
00150   if (!checkAndDeleteHisto(id)) return false;
00151   histoProfList[id] = h; 
00152   return true;
00153 }
00154 
00155 
00156 
00157 
00158 IHistogram1D* AIDAHistoManager::retrieveHisto1D(const char* label) 
00159 {
00160   AIDA_STD::string id = label;
00161   map1DIt i = histo1DList.find(id);
00162   if (i != histo1DList.end()) return i->second;
00163   else return 0;
00164 }
00165 
00166 
00167 
00168 
00169 IHistogram2D* AIDAHistoManager::retrieveHisto2D(const char* label) 
00170 {
00171   AIDA_STD::string id = label;
00172   map2DIt i = histo2DList.find(id);
00173   if (i != histo2DList.end()) return i->second;
00174   else return 0;
00175 }
00176 
00177 
00178 
00179 
00180 IHistogram3D* AIDAHistoManager::retrieveHisto3D(const char* label) 
00181 {
00182   notInHBOOKMessage("retrieveHisto3D"); 
00183   return 0;
00184 }
00185 
00186 
00187 
00188 
00189 IProfileHistogram* AIDAHistoManager::retrieveProf(const char* label) 
00190 {
00191   //ND Profile histograms should have a range in y, surely?
00192   AIDA_STD::string id = label;
00193   mapProfIt i = histoProfList.find(id);
00194   if (i != histoProfList.end()) return i->second;
00195   else return 0;
00196 }
00197 
00198 
00199 
00200 
00201 void AIDAHistoManager::deleteHisto(const char* label) 
00202 {
00203   // all histos?
00204   if (label == 0 || atoi(label) == 0) cleanUp();
00205   else { // just one
00206     bool tmp = overwriteHisto;
00207     overwriteHisto = true;
00208     if (!checkAndDeleteHisto(label)) 
00209       AIDA_STD::cerr << "Error deleting histo \"" << label 
00210                      << "\" !" << AIDA_STD::endl;
00211     overwriteHisto = tmp;
00212   }
00213 }
00214 
00215 
00216 
00217 
00218 void AIDAHistoManager::list(AIDA_STD::ostream& s) 
00219 {
00220   // write list of histos.
00221   s << "\n";
00222   if (histo1DList.empty() && histo2DList.empty()) {
00223     s << "No histograms stored\n\n";
00224     return;
00225   }
00226   if (!histo1DList.empty()) {
00227     s << "1D histograms:\n";
00228     for (map1DIt i = histo1DList.begin(); i != histo1DList.end(); ++i)
00229       formPrint(s,i->first,i->second->title());
00230     s << "\n";
00231   }
00232   if (!histo2DList.empty()) {
00233     s << "2D histograms:\n";
00234     for (map2DIt i = histo2DList.begin(); i != histo2DList.end(); ++i)
00235       formPrint(s,i->first,i->second->title());
00236     s << "\n";
00237   }
00238   if (!histoProfList.empty()) {
00239     s << "Profile histograms:\n";
00240     for (mapProfIt i = histoProfList.begin(); i != histoProfList.end(); ++i) 
00241       formPrint(s,i->first,i->second->title());
00242     s << "\n";
00243   }
00244 }
00245 
00246 
00247 
00248 
00249 void AIDAHistoManager::disableOverwrite(void) 
00250 {
00251   overwriteHisto = false; 
00252 }
00253 
00254 
00255 
00256 
00257 void AIDAHistoManager::enableOverwrite(void) 
00258 {
00259   overwriteHisto = true; 
00260 }
00261 
00262 
00263 
00264 
00265 void AIDAHistoManager::disableWarnOverwrite(void) 
00266 {
00267   warnOverwrite = false; 
00268 }
00269 
00270 
00271 
00272 
00273 void AIDAHistoManager::enableWarnOverwrite(void) 
00274 {
00275   warnOverwrite = true; 
00276 }
00277 
00278 
00279 
00280 
00281 void AIDAHistoManager::setFactory(IHistoFactory* newFactory) 
00282 {
00283   if (ownsFactory) delete hFact;
00284   hFact = newFactory;
00285 }
00286 
00287 
00288 
00289 
00290 IHistogram1D* 
00291 AIDAHistoManager::create1D(const char* label, const char* title,
00292                            int nBinsx, double xmin, double xmax,
00293                            const char* options) 
00294 {
00295   if (!hFact) { crisisMessage("create1D"); return 0; }
00296   IHistogram1D* h = 
00297     hFact->create1D(label, title, nBinsx, xmin, xmax, options);
00298   if (!register1D(h)) {
00299     hFact->destroy(h);
00300     if (warnOverwrite) registerErrorMessage();
00301     return 0;
00302   }
00303   else return h;
00304 }
00305 
00306 
00307 
00308 
00309 IHistogram2D* 
00310 AIDAHistoManager::create2D(const char* label, const char* title,
00311                            int nBinsx, double xmin, double xmax,
00312                            int nBinsy, double ymin, double ymax,
00313                            const char* options) 
00314 {
00315   if (!hFact) { crisisMessage("create2D"); return 0; }
00316   IHistogram2D* h = 
00317     hFact->create2D(label, title, nBinsx, xmin, xmax, 
00318                     nBinsy, ymin, ymax, options);
00319   if (!register2D(h)) {
00320     hFact->destroy(h);
00321     if (warnOverwrite) registerErrorMessage();
00322     return 0;
00323   }
00324   else return h;
00325 }
00326 
00327 
00328 
00329 
00330 IHistogram3D* 
00331 AIDAHistoManager::create3D(const char* label, const char* title,
00332                            int nBinsx, double xmin, double xmax,
00333                            int nBinsy, double ymin, double ymax,
00334                            int nBinsz, double zmin, double zmax,
00335                            const char* options) 
00336 {
00337   notInHBOOKMessage("create3D"); 
00338   return 0;
00339 }
00340 
00341 
00342 
00343 
00344 IProfileHistogram* 
00345 AIDAHistoManager::createProfile(const char* label, const char* title,
00346                                 int nBinsx, double xmin, double xmax,
00347                                 const char* options) 
00348 {
00349   //ND Profile histograms should have a range in y, surely?
00350   if (!hFact) { crisisMessage("createProfile"); return 0; }
00351   IProfileHistogram* h = 
00352     hFact->createProfile(label, title, nBinsx, xmin, xmax, options);
00353   if (!registerProf(h)) {
00354     hFact->destroy(h);
00355     if (warnOverwrite) registerErrorMessage();
00356     return 0;
00357   }
00358   else return h;
00359 }
00360 
00361 
00362 
00363 
00364 IHistogram1D* 
00365 AIDAHistoManager::createDynamic1D(const char* label, const char* title, 
00366                                   const int nBinsx) 
00367 {
00368   if (!hFact) { crisisMessage("createDynamic1D"); return 0; }
00369   IHistogram1D* h = 
00370     hFact->dynamic1D(label, title, nBinsx);
00371   if (!register1D(h)) {
00372     hFact->destroy(h);
00373     if (warnOverwrite) registerErrorMessage();
00374     return 0;
00375   }
00376   else return h;
00377 }
00378 
00379 
00380 
00381 
00382 IHistogram1D* 
00383 AIDAHistoManager::create1DVar(const char* label, const char* title, 
00384                               AIDA_STD::vector<float> binEdge,
00385                               const char* options) 
00386 {
00387   if (!hFact) { crisisMessage("create1DVar"); return 0; }
00388   IHistogram1D* h = 
00389     hFact->create1DVar(label, title, binEdge, options);
00390   if (!register1D(h)) {
00391     hFact->destroy(h);
00392     if (warnOverwrite) registerErrorMessage();
00393     return 0;
00394   }
00395   else return h;
00396 }
00397 
00398 
00399 
00400 
00401 IHistogram2D* 
00402 AIDAHistoManager::create2DVar(const char* label, const char* title, 
00403                               AIDA_STD::vector<float> binEdgeX,
00404                               AIDA_STD::vector<float> binEdgeY,
00405                               const char* options) 
00406 {
00407   notInHBOOKMessage("create2DVar"); 
00408   return 0;
00409 }
00410 
00411 
00412 
00413 
00414 IHistogram1D* AIDAHistoManager::load1D(const char* label) 
00415 {
00416   if (!hFact) { crisisMessage("load1D"); return 0; }
00417   IHistogram1D* h = hFact->load1D(label); 
00418   if (!h) return 0;
00419   if (!register1D(h)) {
00420     hFact->destroy(h);
00421     if (warnOverwrite) registerErrorMessage();
00422     return 0;
00423   }
00424   else return h;
00425 }
00426 
00427 
00428 
00429 
00430 IHistogram2D* AIDAHistoManager::load2D(const char* label) 
00431 {
00432   if (!hFact) { crisisMessage("load2D"); return 0; }
00433   IHistogram2D* h = hFact->load2D(label); 
00434   if (!h) return 0;
00435   if (!register2D(h)) {
00436     hFact->destroy(h);
00437     if (warnOverwrite) registerErrorMessage();
00438     return 0;
00439   }
00440   else return h;
00441 }
00442 
00443 
00444 
00445 
00446 IHistogram3D* AIDAHistoManager::load3D(const char* label) 
00447 {
00448   notInHBOOKMessage("load3D"); 
00449   return 0;
00450 }
00451 
00452 
00453 
00454 
00455 IProfileHistogram* AIDAHistoManager::loadProf(const char* label) 
00456 {
00457   //ND Profile histograms should have a range in y, surely?
00458   if (!hFact) { crisisMessage("loadProf"); return 0; }
00459   IProfileHistogram* h = hFact->loadProf(label); 
00460   if (!h) return 0;
00461   if (!registerProf(h)) {
00462     hFact->destroy(h);
00463     if (warnOverwrite) registerErrorMessage();
00464     return 0;
00465   }
00466   else return h;
00467 }
00468 
00469 
00470 
00471 
00472 void AIDAHistoManager::scratchHisto(const char* label) 
00473 {
00474   if (!hFact) { crisisMessage("scratchHisto"); return; }
00475   hFact->scratchHisto(label); 
00476 }
00477 
00478 
00479 
00480 
00481 void AIDAHistoManager::store(const char* label) 
00482 {
00483   if (!hFact) { crisisMessage("store"); return; }
00484   AIDA_STD::string id = label;
00485   if      (in1DList(id))   hFact->store1D(histo1DList[id]);
00486   else if (in2DList(id))   hFact->store2D(histo2DList[id]);
00487   else if (inProfList(id)) hFact->storeProf(histoProfList[id]);
00488   else histoNotFoundMessage(label);
00489 }
00490 
00491 
00492 
00493 
00494 void AIDAHistoManager::selectStore(const char* name, 
00495                                    const char* topDir) 
00496 {
00497   if (!hFact) { crisisMessage("selectStore"); return; }
00498   hFact->selectStore(name, topDir); 
00499 }
00500 
00501 
00502 
00503 
00504 void AIDAHistoManager::mkdir(const char* name) 
00505 {
00506   if (!hFact) { crisisMessage("mkdir"); return; }
00507   hFact->mkdir(name); 
00508 }
00509 
00510 
00511 
00512 
00513 void AIDAHistoManager::rmdir(const char* name) 
00514 {
00515   if (!hFact) { crisisMessage("rmdir"); return; }
00516   hFact->rmdir(name); 
00517 }
00518 
00519 
00520 
00521 
00522 void AIDAHistoManager::cd(const char* name) 
00523 {
00524   if (!hFact) { crisisMessage("cd"); return; }
00525   hFact->cd(name); 
00526 }
00527 
00528 
00529 
00530 
00531 void AIDAHistoManager::ls(void) const 
00532 {
00533   if (!hFact) { crisisMessage("ls"); return; }
00534   hFact->ls(); 
00535 }
00536 
00537 
00538 
00539 
00540 void AIDAHistoManager::pwd(void) const 
00541 {
00542   if (!hFact) { crisisMessage("pwd"); return; }
00543   hFact->pwd(); 
00544 }
00545 
00546 
00547 
00549 //                 //
00550 // private methods //
00551 //                 //
00553 
00554 void AIDAHistoManager::notDoneMessage(const AIDA_STD::string& fname) const
00555 {
00556   AIDA_STD::cout << "AIDAHistoManager::"
00557                  << fname
00558                  << "(...) not yet implemented - sorry!"
00559                  << AIDA_STD::endl;
00560 }
00561 
00562 
00563 
00564 
00565 void AIDAHistoManager::notInHBOOKMessage(const AIDA_STD::string& fname) const
00566 {
00567   AIDA_STD::cout << "AIDAHistoManager::"
00568                  << fname
00569                  << "(...) is not implemented because HBOOK "
00570                  << "does not have the required functionality"
00571                  << AIDA_STD::endl;
00572 }
00573 
00574 
00575 
00576 
00577 void AIDAHistoManager::registerErrorMessage(void) const
00578 {
00579   AIDA_STD::cout << "ERROR when trying to register. No histo created." 
00580                  << AIDA_STD::endl;
00581 }
00582 
00583 
00584 
00585 
00586 void AIDAHistoManager::histoNotFoundMessage(const AIDA_STD::string& label) const
00587 {
00588   AIDA_STD::cout << "No histogram with label "
00589                  << label << " found" << AIDA_STD::endl;
00590 }
00591 
00592 
00593 
00594 
00595 void AIDAHistoManager::noOverwriteMessage(const AIDA_STD::string& label) const
00596 {
00597   AIDA_STD::cout << "ERROR: found histogram with ID \""<< label 
00598                  << "\" and overwriting is disabled."  << AIDA_STD::endl;
00599 }
00600 
00601 
00602 
00603 
00604 void AIDAHistoManager::deletionSuccessfulMessage(const AIDA_STD::string& label) const
00605 {
00606   AIDA_STD::cout << "INFO: histogram with ID \"" << label
00607                  << "\" has been deleted."       << AIDA_STD::endl;
00608 }
00609 
00610 
00611 
00612 
00613 void AIDAHistoManager::crisisMessage(const AIDA_STD::string& featureName) const
00614 {
00615   AIDA_STD::cerr << "AIDAHistoManager::" << featureName
00616                  << "(...): ERROR! factory = 0"
00617                  << AIDA_STD::endl;
00618 }
00619 
00620 
00621 
00622 
00623 void AIDAHistoManager::cleanUp(void)
00624 {
00625   if (!hFact) { crisisMessage("cleanUp"); return; }
00626   // 1D
00627   for (map1DIt i1 = histo1DList.begin(); i1 != histo1DList.end(); ++i1)    
00628     hFact->destroy(i1->second);
00629   histo1DList.clear();
00630   // 2D
00631   for (map2DIt i2 = histo2DList.begin(); i2 != histo2DList.end(); ++i2)
00632     hFact->destroy(i2->second);
00633   histo2DList.clear();
00634   // Profiles
00635   for (mapProfIt i3 = histoProfList.begin(); i3 != histoProfList.end(); ++i3)
00636     hFact->destroy(i3->second);
00637   histoProfList.clear();
00638 }
00639 
00640 
00641 
00642 
00643 bool AIDAHistoManager::checkAndDeleteHisto(const AIDA_STD::string& label)
00644 {
00645   bool retval = false;
00646   if (!hFact) { crisisMessage("checkAndDeleteHisto"); return false; }
00647   map1DIt i1 = histo1DList.find(label);
00648   if (i1 == histo1DList.end()) return true;
00649   else {
00650     retval = removeWithWarnings(label,i1->second);
00651     if (retval) histo1DList.erase(i1);
00652     return retval;
00653   }
00654   map2DIt i2 = histo2DList.find(label);
00655   if (i2 == histo2DList.end()) return true;
00656   else {
00657     retval = removeWithWarnings(label,i2->second);
00658     if (retval) histo2DList.erase(i2);
00659     return retval;
00660   }
00661   mapProfIt i3 = histoProfList.find(label);
00662   if (i3 == histoProfList.end()) return true;
00663   else {
00664     retval = removeWithWarnings(label,i3->second);
00665     if (retval) histoProfList.erase(i3);
00666     return retval;
00667   }
00668 }
00669 
00670 
00671 
00672 
00673 bool AIDAHistoManager::in1DList(const AIDA_STD::string& label) const
00674 {
00675   for (map1DCIt i = histo1DList.begin(); 
00676        i != histo1DList.end(); ++i)
00677     if (i->first == label) return true;
00678   return false;
00679 }
00680 
00681 
00682 
00683 
00684 bool AIDAHistoManager::in2DList(const AIDA_STD::string& label) const
00685 {
00686   for (map2DCIt i = histo2DList.begin(); 
00687        i != histo2DList.end(); ++i)
00688     if (i->first == label) return true;
00689   return false;
00690 }
00691 
00692 
00693 
00694 
00695 bool AIDAHistoManager::inProfList(const AIDA_STD::string& label) const
00696 {
00697   for (mapProfCIt i = histoProfList.begin(); 
00698        i != histoProfList.end(); ++i)
00699     if (i->first == label) return true;
00700   return false;
00701 }
00702 
00703 
00704 
00705 
00706 void AIDAHistoManager::formPrint(AIDA_STD::ostream& s, 
00707                                  const AIDA_STD::string& label, 
00708                                  const AIDA_STD::string& title) const
00709 {
00710   s.width(9);
00711   s << atoi(label.c_str()) << AIDA_STD::flush;
00712   s.width(0);
00713   s << "  \"" << title << "\"\n";
00714 }
00715 
00716 
00717 
00718 
00719 bool AIDAHistoManager::removeWithWarnings(const AIDA_STD::string& label,
00720                                           IHistogram* histo)
00721 {
00722   if (!overwriteHisto) {
00723     if (warnOverwrite) noOverwriteMessage(label);
00724     return false;
00725   }
00726   else {
00727     hFact->destroy(histo);
00728     if (warnOverwrite) deletionSuccessfulMessage(label);
00729     return true;
00730   }
00731 }
00732 


Anaphe documentation generated by Doxygen (www.doxygen.org)