00001
00002
00003
00004
00005
00006
00007 #include "AIDAHistoFactory.h"
00008 #include <assert.h>
00009 #include "Interfaces/IAnnotation.h"
00010 #include "Interfaces/IAnnotationFactory.h"
00011 #include "Interfaces/IHistoManager.h"
00012 #include "Interfaces/IHistogram1D.h"
00013 #include "Interfaces/IHistogram2D.h"
00014 #include "Interfaces/IHistogram3D.h"
00015 #include "Interfaces/IProfileHistogram.h"
00016 #include "CHBook/CHBookFile.h"
00017 #include "CHBook/CHBookFileManager.h"
00018 #include "AIDAHistogram1D.h"
00019 #include "AIDAHistogram2D.h"
00020 #include "AIDAProfileHist.h"
00021 #include "AIDAHist1DVar.h"
00022 #include "AIDADynHist1D.h"
00023
00024 #include "HistoParameters/HistoParameters.h"
00025
00026 using std::vector;
00027
00028 using Anaphe::IAnnotation;
00029 using Anaphe::IAnnotationFactory;
00030 using Anaphe::IHistoManager;
00031 using Anaphe::IHistogram1D;
00032 using Anaphe::IHistogram2D;
00033 using Anaphe::IHistogram3D;
00034 using Anaphe::IProfileHistogram;
00035
00036 using Anaphe::CHBookFile;
00037 using Anaphe::theCHBookFileManager;
00038
00039 using Anaphe::AIDA_HBook::AIDAHistoFactory;
00040 using Anaphe::AIDA_HBook::AIDAAxis;
00041
00042 using Anaphe::AIDA_HBook::AIDAHistogram1D;
00043 using Anaphe::AIDA_HBook::AIDAHistogram2D;
00044 using Anaphe::AIDA_HBook::AIDAProfileHist;
00045 using Anaphe::AIDA_HBook::AIDADynHist1D;
00046 using Anaphe::AIDA_HBook::AIDAHist1DVar;
00047
00049
00050
00051
00053
00054 Anaphe::IHistoFactory* createIHistoFactory()
00055 {
00056 return new AIDAHistoFactory();
00057 }
00058
00059
00060
00062
00063
00064
00066
00067 AIDAHistoFactory::AIDAHistoFactory(void)
00068 : manager (0),
00069 annFactory (0),
00070 fileManager(0),
00071 file (0)
00072 {
00073 annFactory = createIAnnotationFactory();
00074 fileManager = theCHBookFileManager();
00075 assert(annFactory && fileManager);
00076 }
00077
00078 AIDAHistoFactory::~AIDAHistoFactory(void)
00079 {
00080 delete annFactory;
00081 if (fileManager && file) fileManager->releaseFile(file);
00082 }
00083
00084
00086
00087
00088
00090
00091
00092 Anaphe::IHistogram1D * AIDAHistoFactory::create1D(const Anaphe::HistoParameters1D &hp)
00093 {
00094 IAnnotation* ann = annFactory->create();
00095 ann->add("ID", hp.id(), true);
00096 AIDAHistogram1D* h =
00097 new AIDAHistogram1D(hp, ann, this);
00098 delete ann;
00099 return h;
00100 }
00101
00102 Anaphe::IHistogram2D * AIDAHistoFactory::create2D(const Anaphe::HistoParameters2D &hp)
00103 {
00104 IAnnotation* ann = annFactory->create();
00105 ann->add("ID", hp.id(), true);
00106 AIDAHistogram2D* h =
00107 new AIDAHistogram2D(hp, ann, this);
00108 delete ann;
00109 return h;
00110 }
00111
00112 Anaphe::IHistogram1D*
00113 AIDAHistoFactory::create1D(const char* label, const char* title,
00114 int nBinsx, double xmin, double xmax,
00115 const char* options)
00116 {
00117 IAnnotation* ann = annFactory->create();
00118 ann->add("ID", label, true);
00119 AIDAHistogram1D* h =
00120 new AIDAHistogram1D(title, nBinsx, xmin, xmax, ann, this);
00121 delete ann;
00122 return h;
00123 }
00124
00125
00126
00127
00128 Anaphe::IHistogram2D*
00129 AIDAHistoFactory::create2D(const char* label, const char* title,
00130 int nBinsx, double xmin, double xmax,
00131 int nBinsy, double ymin, double ymax,
00132 const char* options)
00133 {
00134 IAnnotation* ann = annFactory->create();
00135 assert(ann);
00136 ann->add("ID", label, true);
00137 AIDAHistogram2D* h =
00138 new AIDAHistogram2D(title, nBinsx, xmin, xmax,
00139 nBinsy, ymin, ymax, ann, this);
00140 delete ann;
00141 return h;
00142 }
00143
00144
00145
00146
00147 Anaphe::IHistogram3D*
00148 AIDAHistoFactory::create3D(const char* label, const char* title,
00149 int nBinsx, double xmin, double xmax,
00150 int nBinsy, double ymin, double ymax,
00151 int nBinsz, double zmin, double zmax,
00152 const char* options)
00153 {
00154 notInHBOOKMessage("create3D");
00155 return 0;
00156 }
00157
00158
00159
00160
00161 Anaphe::IProfileHistogram*
00162 AIDAHistoFactory::createProfile(const char* label, const char* title,
00163 int nBinsx,
00164 double xmin, double xmax,
00165 const char* options)
00166 {
00167 double ymin = -1e10;
00168 double ymax = 1e10;
00169 IAnnotation* ann = annFactory->create();
00170 ann->add("ID", label, true);
00171 AIDAProfileHist* h =
00172 new AIDAProfileHist(title, nBinsx, xmin, xmax, ymin, ymax, ann, this);
00173 delete ann;
00174 return h;
00175 }
00176
00177
00178
00179
00180 Anaphe::IHistogram1D*
00181 AIDAHistoFactory::dynamic1D(const char* label, const char* title,
00182 const int nBinsx)
00183 {
00184 IAnnotation* ann = annFactory->create();
00185 ann->add("ID", label, true);
00186 AIDADynHist1D* h = new AIDADynHist1D(title, nBinsx, ann, this);
00187 delete ann;
00188 return h;
00189 }
00190
00191
00192
00193
00194 Anaphe::IHistogram1D*
00195 AIDAHistoFactory::create1DVar(const char* label, const char *title,
00196 std::vector<float> binEdges,
00197 const char* options)
00198 {
00199 IAnnotation* ann = annFactory->create();
00200 ann->add("ID", label, true);
00201 AIDAHist1DVar* h = new AIDAHist1DVar(title, binEdges, ann, this);
00202 delete ann;
00203 return h;
00204 }
00205
00206
00207
00208
00209 Anaphe::IHistogram2D*
00210 AIDAHistoFactory::create2DVar(const char* label, const char* title,
00211 std::vector<float> binEdgeX,
00212 std::vector<float> binEdgeY,
00213 const char* options)
00214 {
00215 notInHBOOKMessage("create2DVar");
00216 return 0;
00217 }
00218
00219
00220
00221
00222 Anaphe::IHistogram1D* AIDAHistoFactory::load1D(const char* label)
00223 {
00224 if (!file) return 0;
00225 if (!file->isOpen()) { notOpenMessage(); return 0; }
00226 int id;
00227 if (!getIntFromLabel(label,id)) return 0;
00228 const CHBookHisto* retrievedHBookHisto = file->load1D(id);
00229 if (!retrievedHBookHisto) return 0;
00230 AIDAHistogram1D* newHisto = new AIDAHistogram1D(retrievedHBookHisto,this);
00231 return newHisto;
00232 }
00233
00234
00235
00236
00237 Anaphe::IHistogram2D* AIDAHistoFactory::load2D(const char* label)
00238 {
00239 if (!file) return 0;
00240 if (!file->isOpen()) { notOpenMessage(); return 0; }
00241 int id;
00242 if (!getIntFromLabel(label,id)) return 0;
00243 const CHBookHisto2* retrievedHBookHisto = file->load2D(id);
00244 if (!retrievedHBookHisto) return 0;
00245 AIDAHistogram2D* newHisto = new AIDAHistogram2D(retrievedHBookHisto,this);
00246 return newHisto;
00247 }
00248
00249
00250
00251
00252 Anaphe::IHistogram3D* AIDAHistoFactory::load3D(const char* label)
00253 {
00254 notInHBOOKMessage("load3D");
00255 return 0;
00256 }
00257
00258
00259
00260
00261 Anaphe::IProfileHistogram* AIDAHistoFactory::loadProf(const char* label)
00262 {
00263 if (!file) return 0;
00264 if (!file->isOpen()) { notOpenMessage(); return 0; }
00265 int id;
00266 if (!getIntFromLabel(label,id)) return 0;
00267 const CHBookProfile* retrievedHBookHisto = file->loadProfile(id);
00268 if (!retrievedHBookHisto) return 0;
00269 AIDAProfileHist* newHisto = new AIDAProfileHist(retrievedHBookHisto,this);
00270 return newHisto;
00271 }
00272
00273
00274
00275
00276 void AIDAHistoFactory::scratchHisto(const char* label)
00277 {
00278 if (!file) return;
00279 if (!file->isOpen()) { notOpenMessage(); return; }
00280 int id;
00281 if (!getIntFromLabel(label,id)) return;
00282 file->scratch(id);
00283 }
00284
00285
00286
00287
00288 bool AIDAHistoFactory::store1D(IHistogram1D* h)
00289 {
00290 if (!h || !file) return false;
00291 if (!file->isOpen()) { notOpenMessage(); return false; }
00292 bool flag = false;
00293 AIDAHistogram1D* ahf = dynamic_cast<AIDAHistogram1D*>(h);
00294 AIDAHist1DVar* ahv = dynamic_cast<AIDAHist1DVar*>(h);
00295 AIDADynHist1D* ahd = dynamic_cast<AIDADynHist1D*>(h);
00296 if (ahf) {
00297 const CHBookHisto* phbh = ahf->representation();
00298 if (phbh) flag = file->store1D(*phbh);
00299 }
00300 else if (ahv) {
00301 const CHBookHisto* phbh = ahv->representation();
00302 if (phbh) flag = file->store1D(*phbh);
00303 }
00304 else if (ahd) {
00305 const CHBookHisto* phbh = ahd->representation();
00306 if (phbh) flag = file->store1D(*phbh);
00307 }
00308 return flag;
00309 }
00310
00311
00312
00313
00314 bool AIDAHistoFactory::store2D(IHistogram2D* h)
00315 {
00316 if (!h || !file) return false;
00317 if (!file->isOpen()) { notOpenMessage(); return false; }
00318 AIDAHistogram2D* ah = dynamic_cast<AIDAHistogram2D*>(h);
00319 if (!ah) return false;
00320 const CHBookHisto2* phbh = ah->representation();
00321 if (!phbh) return false;
00322 return file->store2D(*phbh);
00323 }
00324
00325
00326
00327
00328 bool AIDAHistoFactory::store3D(IHistogram3D* h)
00329 {
00330 notInHBOOKMessage("store3D");
00331 return 0;
00332 }
00333
00334
00335
00336
00337 bool AIDAHistoFactory::storeProf(IProfileHistogram* h)
00338 {
00339 if (!h || !file) return false;
00340 if (!file->isOpen()) { notOpenMessage(); return false; }
00341 AIDAProfileHist* ah = dynamic_cast<AIDAProfileHist*>(h);
00342 if (!ah) return false;
00343 const CHBookProfile* phbh = ah->representation();
00344 if (!phbh) return false;
00345 return file->storeProfile(*phbh);
00346 }
00347
00348
00349
00350
00351 void AIDAHistoFactory::selectStore(const char* filename,
00352 const char* topDir)
00353 {
00354 if (!filename || !fileManager) return;
00355 else {
00356 if (file) fileManager->releaseFile(file);
00357 file = fileManager->getFile(std::string(filename));
00358 if (!file) { badFileMessage(filename); return; }
00359 else file->allowOverwrites(true);
00360 }
00361 }
00362
00363
00364
00365
00366 void AIDAHistoFactory::mkdir(const char* dirname)
00367 {
00368 if (!file || !dirname) return;
00369 if (!file->isOpen()) { notOpenMessage(); return; }
00370 else file->mkdir(std::string(dirname));
00371 }
00372
00373
00374
00375
00376 void AIDAHistoFactory::rmdir(const char* dirname)
00377 {
00378 if (!file || !dirname) return;
00379 if (!file->isOpen()) { notOpenMessage(); return; }
00380 else file->rmdir(std::string(dirname));
00381 }
00382
00383
00384
00385
00386 void AIDAHistoFactory::cd(const char* dirname)
00387 {
00388 if (!file || !dirname) return;
00389 if (!file->isOpen()) { notOpenMessage(); return; }
00390 else file->cd(std::string(dirname));
00391 }
00392
00393
00394
00395
00396 void AIDAHistoFactory::ls(void) const
00397 {
00398 if (!file) return;
00399 if (!file->isOpen()) { notOpenMessage(); return; }
00400 else file->ls(std::cout);
00401 }
00402
00403
00404
00405
00406 void AIDAHistoFactory::pwd(void) const
00407 {
00408 if (!file) return;
00409 if (!file->isOpen()) { notOpenMessage(); return; }
00410 else std::cout << file->pwd() << std::endl;
00411 }
00412
00413
00414
00415
00416 void AIDAHistoFactory::setManager(IHistoManager* m)
00417 {
00418 manager = m;
00419 }
00420
00421
00422
00423
00425
00426
00427
00429
00430 Anaphe::IHistogram1D*
00431 AIDAHistoFactory::create1D(std::string title,
00432 int nBinsx, double xmin, double xmax,
00433 const int ID)
00434 {
00435 return AIDAHistogramFactory::create1D(title,nBinsx,xmin,xmax,ID);
00436 }
00437
00438
00439
00440
00441 Anaphe::IHistogram2D*
00442 AIDAHistoFactory::create2D(std::string title,
00443 int nBinsx, double xmin, double xmax,
00444 int nBinsy, double ymin, double ymax,
00445 const int ID)
00446 {
00447 return AIDAHistogramFactory::create2D(title,nBinsx,xmin,xmax,
00448 nBinsy,ymin,ymax,ID);
00449 }
00450
00451
00452
00453
00454 void AIDAHistoFactory::unregister(IHistogram* h)
00455 {
00456 AIDAHistogramFactory::unregister(h);
00457 }
00458
00459
00460
00461
00462 void AIDAHistoFactory::destroy(IHistogram * h)
00463 {
00464 AIDAHistogramFactory::destroy(h);
00465 }
00466
00467
00468
00469
00471
00472
00473
00475
00476 void AIDAHistoFactory::notDoneMessage(const char* func) const
00477 {
00478 std::cout << "AIDAHistoFactory::"
00479 << func
00480 << "(...) is not yet implemented. Sorry!"
00481 << std::endl;
00482 }
00483
00484
00485
00486
00487 void AIDAHistoFactory::notInHBOOKMessage(const char* func) const
00488 {
00489 std::cout << "AIDAHistoFactory::"
00490 << func
00491 << "(...) is not implemented because "
00492 << "it doesn't exist in HBOOK"
00493 << std::endl;
00494 }
00495
00496
00497
00498
00499 void AIDAHistoFactory::notOpenMessage(void) const
00500 {
00501 std::cout << std::endl
00502 << "No file open - use selectStore(\"filename\") first"
00503 << std::endl << std::endl;
00504 }
00505
00506
00507
00508
00509 void AIDAHistoFactory::badFileMessage(const char* filename) const
00510 {
00511 if (!filename) return;
00512 else std::cout << std::endl
00513 << "ERROR - file \"" << filename
00514 << "\" could not be opened"
00515 << std::endl << std::endl;
00516 }
00517
00518
00519
00520
00521 bool AIDAHistoFactory::getIntFromLabel(const char* label, int& result) const
00522 {
00523 result = 0;
00524 if (!label || strlen(label) < 1) return false;
00525 std::string s(label);
00526 for (unsigned int i = 0; i < s.length(); ++i) {
00527 if (s[i] == '0') s.erase(0,1);
00528 else break;
00529 }
00530 if (s.length() < 1) return false;
00531 for (unsigned int i = 0; i < s.length(); ++i) {
00532 if (!isdigit(s[i])) return false;
00533 }
00534 result = atoi(label);
00535 return (result > 0);
00536 }
00537
00538
00539
00540