Anaphe Home Page Reference Documentation

Main Page     Namespaces     Classes     Source Code    

AIDAHistogram1D.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // 
00004 // Copyright (C) CERN, Geneva 
00005 // 
00006 
00007 
00008 
00009 
00010 #include "AIDAHistogram1D.h"
00011 #include <assert.h>
00012 #include "HepUtilities/SimpleTokenizer.h"
00013 #include "Interfaces/IAnnotation.h"
00014 #include "Interfaces/IAnnotationFactory.h"
00015 #include "Interfaces/IHistogramFactory.h"
00016 #include "CHBook/CHBookHisto.h"
00017 #include "Builder.h"
00018 #include "AIDAAxis.h"
00019 #include "HistoParameters/HistoParameters.h"
00020 #if defined __i386__ || defined __sun
00021 # include <strstream>
00022 #else
00023 # include <sstream>
00024 #endif
00025 
00026 
00027 
00028 
00029 using Anaphe::IAxis;
00030 using Anaphe::AIDA_HBook::AIDAHistogram1D;
00031 using Anaphe::AIDA_HBook::AIDAAxis;
00032 using Anaphe::HistoParameters1D;
00033 using Anaphe::HistoParameters2D;
00034 using Anaphe::CHBookHisto;
00035 
00036 
00037 
00038 
00039 
00041 //                 //
00042 // Con/Destructors //
00043 //                 //
00045 
00046 AIDAHistogram1D::AIDAHistogram1D(const char* name, 
00047                                  const int& nBinsX, 
00048                                  const double& xMin, 
00049                                  const double& xMax,
00050                                  const Anaphe::IAnnotation* a, 
00051                                  const Anaphe::IHistogramFactory* f)
00052   : axis    (0), 
00053     ann     (0), 
00054     factory (const_cast<Anaphe::IHistogramFactory*>(f)),
00055     histo   (0), 
00056     ownsHist(true)
00057 {
00058   Anaphe::IAnnotationFactory* af = createIAnnotationFactory();
00059   assert (af);
00060   if   (a) ann = af->create(a); 
00061   else     ann = af->create();
00062   delete af;
00063   int id = 1;
00064   std::string label = ann->find("ID");
00065   if (label != "") id = getIntFromString(label);
00066   histo = new CHBookHisto(id, name, nBinsX, xMin, xMax);
00067   assert(histo);
00068   if (label == "") ann->add("ID", getStringFromInt(histo->id()));
00069   axis  = new AIDAAxis(histo, 0, nBinsX, xMin, xMax);
00070   assert(axis);
00071 }
00072 
00073 
00074 
00075 
00076 AIDAHistogram1D::AIDAHistogram1D(const Anaphe::HistoParameters1D & hp, 
00077                                  const Anaphe::IAnnotation * a, 
00078                                  const Anaphe::IHistogramFactory * f) : 
00079 
00080   axis    (0),
00081   ann     (0),
00082   factory (const_cast<Anaphe::IHistogramFactory*>(f)),
00083   histo   (0),
00084   ownsHist(true)
00085 {
00086   /* 
00087   std::cerr << "title : " << hp.title() << endl;
00088   std::cerr << "nbX   : " << hp.nbX() << endl;
00089   std::cerr << "lowX  : " << hp.lowX() << endl;
00090   std::cerr << "highX : " << hp.highX() << endl;
00091   */
00092 
00093   Anaphe::IAnnotationFactory* af = createIAnnotationFactory();
00094   assert (af);
00095   if   (a) ann = af->create(a); 
00096   else     ann = af->create();
00097   delete af;
00098   int id = 1;
00099   std::string label = ann->find("ID");
00100   if (label != "") id = getIntFromString(label);
00101 
00102   Anaphe::AIDA_HBook::Builder builder;
00103   builder.noWarnings(true);
00104   histo = builder.buildFrom(hp, id);
00105   assert(histo != 0);
00106 
00107   if (label == "") ann->add("ID", getStringFromInt(histo->id()));
00108   axis  = new AIDAAxis(histo, 0, hp.nbX(), hp.lowX(), hp.highX());
00109   assert(axis);
00110 
00111 }
00112 
00113 AIDAHistogram1D::AIDAHistogram1D(const CHBookHisto* h, 
00114                                  const Anaphe::IHistogramFactory* f)
00115   : axis    (0),
00116     ann     (0),
00117     factory (const_cast<Anaphe::IHistogramFactory*>(f)),
00118     histo   (const_cast<CHBookHisto*>(h)),
00119     ownsHist(true)
00120 {
00121   assert(histo);
00122   Anaphe::IAnnotationFactory* af = createIAnnotationFactory();
00123   assert(af);
00124   ann = af->create();
00125   delete af;
00126   int id = histo->id();
00127   ann->add("ID", getStringFromInt(id));
00128   int    nb = histo->bins();
00129   double lo = histo->lowerEdge();
00130   double hi = histo->upperEdge();
00131   axis = new AIDAAxis(histo, 0, nb, lo, hi);
00132   assert(axis);
00133 }
00134 
00135 
00136 
00137 
00138 AIDAHistogram1D::~AIDAHistogram1D()
00139 {
00140   //  //-ap: this needs more design. if the factory has been deleted before, 
00141   //  //the application will crash
00142   //
00143   //  if (myFactory != 0) {
00144   //    myFactory->unregister(this);
00145   //  }
00146   //
00147   //  //-ap end
00148   delete axis;
00149   delete ann;
00150   if (ownsHist) delete histo;
00151 }
00152 
00153 
00154 
00155 
00157 //                 //
00158 // Public Methods  //
00159 //                 //
00161 
00165 std::string AIDAHistogram1D::title() const
00166 {
00167   if (!histo) { crisisMessage("title"); return ""; }
00168   else return std::string(histo->getTitle());
00169 }
00170 
00171 
00172 
00173 
00174 void AIDAHistogram1D::setTitle(const std::string& t)
00175 {
00176   if (!histo) { crisisMessage("setTitle"); return; }
00177   else histo->setTitle(const_cast<char*>(t.c_str()));
00178 }
00179 
00180 
00181 
00182 
00184 Anaphe::IAnnotation* AIDAHistogram1D::annotation()
00185 {
00186   return ann; 
00187 }
00188 
00189 
00190 
00191 
00193 int AIDAHistogram1D::dimensions() const
00194 {
00195   return 1;
00196 }
00197 
00198 
00199 
00200 
00202 void AIDAHistogram1D::reset()
00203 {
00204   if (!histo) { crisisMessage("reset"); return; }
00205   else histo->reset();
00206 }
00207 
00208 
00209 
00210 
00212 int AIDAHistogram1D::entries(void) const
00213 {
00214   if (!histo) { crisisMessage("entries"); return -1; }
00215   else {
00216     // extraEntries() might give strange numbers so check it!
00217     int noent = allEntries() - extraEntries();
00218     return (noent >= 0 ? noent : 0);
00219   }
00220 }
00221 
00222 
00223 
00224 
00227 int AIDAHistogram1D::allEntries(void) const
00228 {
00229   if (!histo) { crisisMessage("allEntries"); return -1; }
00230   else return histo->entries();
00231 }
00232 
00233 
00234 
00235 
00237 int AIDAHistogram1D::extraEntries(void) const
00238 {
00239   // HBOOK doesn't record the #entries in o/uflow bins, only the contents,
00240   // so after filling with non-unit weights you will get nonsense!
00241   if (!histo) { crisisMessage("extraEntries"); return -1; }
00242   else return static_cast<int>((histo->outOfRangeContent()));
00243 }
00244 
00245 
00246 
00247 
00250 double AIDAHistogram1D::equivalentBinEntries(void) const
00251 {
00252   if (!histo) { crisisMessage("equivalentBinEntries"); return -1; }
00253   else return histo->equivalentEntries();
00254 }
00255 
00256 
00257 
00258 
00260 double AIDAHistogram1D::sumBinHeights(void) const
00261 {
00262   if (!histo) { crisisMessage("sumBinHeights"); return -1; }
00263   else return histo->sum();
00264 }
00265 
00266 
00267 
00268 
00270 double AIDAHistogram1D::sumAllBinHeights(void) const
00271 {
00272   if (!histo) { crisisMessage("sumAllBinHeights"); return -1; }
00273   else return (sumBinHeights() + sumExtraBinHeights());
00274 }
00275 
00276 
00277 
00278 
00280 double AIDAHistogram1D::sumExtraBinHeights(void) const
00281 {
00282   if (!histo) { crisisMessage("sumExtraBinHeights"); return -1; }
00283   else return (histo->outOfRangeContent());
00284 }
00285 
00286 
00287 
00288 
00290 int AIDAHistogram1D::binEntries(int index) const
00291 {
00292   return static_cast<int>(binHeight(index));
00293 }
00294 
00295 
00296 
00297 
00299 double AIDAHistogram1D::binHeight(int index) const
00300 {
00301   if (!histo) { crisisMessage("binHeight"); return -1; }
00302   else {
00303     int i = index;
00304     if      (i == UNDERFLOW_BIN) i = -1;
00305     else if (i ==  OVERFLOW_BIN) i = histo->bins();
00306     return histo->binContent(i);
00307   }
00308 }
00309 
00310 
00311 
00312 
00314 double AIDAHistogram1D::binError(int index) const
00315 {
00316   if (!histo) { crisisMessage("binError"); return -1; }
00317   else {
00318     int i = index;
00319     if      (i == UNDERFLOW_BIN) i = -1;
00320     else if (i ==  OVERFLOW_BIN) i = histo->bins();
00321     return histo->binError(i);
00322   }
00323 }
00324 
00325 
00326 
00327 
00330 double AIDAHistogram1D::mean(void) const
00331 {
00332   if (!histo) { crisisMessage("mean"); return -1; }
00333   else return histo->mean();
00334 }
00335 
00336 
00337 
00338 
00341 double AIDAHistogram1D::rms(void) const
00342 {
00343   if (!histo) { crisisMessage("rms"); return -1; }
00344   else return histo->sigma();
00345 }
00346 
00347 
00348 
00349 
00351 double AIDAHistogram1D::minBinHeight(void) const
00352 {
00353   if (!histo) { crisisMessage("minBinHeight"); return -1; }
00354   else return histo->min();
00355 }
00356 
00357 
00358 
00359 
00361 int AIDAHistogram1D::minBin(void) const
00362 {
00363   if (!histo) { crisisMessage("minBin"); return -1; }
00364   else return histo->minBin();
00365 }
00366 
00367 
00368 
00369 
00371 double AIDAHistogram1D::maxBinHeight(void) const
00372 {
00373   if (!histo) { crisisMessage("maxBinHeight"); return -1; }
00374   else return histo->max();
00375 }
00376 
00377 
00378 
00379 
00381 int AIDAHistogram1D::maxBin(void) const
00382 {
00383   if (!histo) { crisisMessage("maxBin"); return -1; }
00384   else return histo->maxBin();
00385 }
00386 
00387 
00388 
00389 
00391 IAxis* AIDAHistogram1D::xAxis() const
00392 {
00393   return axis;
00394 }
00395 
00396 
00397 
00399 int AIDAHistogram1D::coordToIndex(double coord) const
00400 {
00401   return xAxis()->coordToIndex(coord);
00402 }
00403 
00404 
00405 
00406 
00408 const CHBookHisto* AIDAHistogram1D::representation(void) const
00409 {
00410   return histo;
00411 }
00412 
00413 
00414 
00416 std::ostream& AIDAHistogram1D::print(std::ostream& s) const
00417 {
00418   if (!histo) crisisMessage("print");
00419   else histo->print();
00420   return s;
00421 }
00422 
00423 
00424 
00425 
00427 std::ostream& AIDAHistogram1D::write(std::ostream& s) const
00428 {
00429   //ND
00430   notYetMessage("write");
00431   return s;
00432 }
00433 
00434 
00435 
00436 
00438 int AIDAHistogram1D::write(const char* file_name) const
00439 {
00440   //ND
00441   notYetMessage("write");
00442   return -1;
00443 }
00444 
00445 
00446 
00447 
00448 void AIDAHistogram1D::fill(double x, double weight)
00449 {
00450   if (!histo) { crisisMessage("fill"); return; }
00451   else {
00452     // CC-5.2 complains about the need for lvalues in the fill() method ...
00453     float xx = static_cast<float>(x);
00454     float ww = static_cast<float>(weight);
00455     histo->fill(xx, ww);
00456   }
00457 }
00458 
00459 
00460 
00461 
00463 //                 //
00464 // Private Methods //
00465 //                 //
00467 
00470 int AIDAHistogram1D::checkIndex(int index) const
00471 {
00472   AIDAAxis* ax = dynamic_cast<AIDAAxis*>(axis);
00473   if (ax) return ax->checkIndex(index);
00474   else return -1;
00475 }
00476 
00477 
00478 
00479 
00480 int AIDAHistogram1D::getIntFromString(const std::string& str) const 
00481 {
00482   int ans = 0;
00483   if (str.length() < 1) return false;
00484   std::string s = str;
00485   for (unsigned int i = 0; i < s.length(); ++i) {
00486     if (s[i] == '0') s.erase(0,1);
00487     else break;
00488   }
00489   if (s.length() < 1) return false;
00490   for (unsigned int i = 0; i < s.length(); ++i) {
00491     if (!isdigit(s[i])) return false;
00492   }
00493   ans = atoi(s.c_str());
00494   return (ans > 0) ? ans : 0;
00495 }
00496 
00497 
00498 
00499 
00500 std::string AIDAHistogram1D::getStringFromInt(const int& i) const
00501 {
00502   std::ostrstream ostr;
00503   ostr << i << std::ends;
00504   return ostr.str();
00505 }
00506 
00507 
00508 
00509 
00510 void AIDAHistogram1D::notYetMessage(const std::string& featureName) const
00511 {
00512   std::cerr << "AIDAHistogram1D::" << featureName 
00513                  << "(...) not yet implemented, sorry ..." 
00514                  << std::endl;
00515   std::cerr << "    If you really need this feature, please contact "
00516                  << "Andreas.Pfeiffer@cern.ch " << std::endl;
00517   std::cerr << "    to discuss the details of the implementation." 
00518                  << std::endl;
00519 }
00520 
00521 
00522 
00523 
00524 void AIDAHistogram1D::crisisMessage(const std::string& featureName) const
00525 {
00526   std::cerr << "AIDAHistogram1D::" << featureName
00527                  << "(...): ERROR - histo = 0"
00528                  << std::endl;
00529 }
00530 
00531 
00532 


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