BinnedHistogram.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #include "Rivet/Tools/BinnedHistogram.hh"
00003 #include "Rivet/RivetBoost.hh"
00004 #include "Rivet/RivetAIDA.hh"
00005 
00006 namespace Rivet {
00007 
00008 
00009   template<typename T>
00010   const BinnedHistogram<T>& BinnedHistogram<T>::addHistogram(const T& binMin, 
00011                                                              const T& binMax, 
00012                                                              AIDA::IHistogram1D *histo){
00013     if (binMin > binMax) {
00014       throw Error
00015         ("Cannot add a binned histogram where the lower bin edge is above the upper edge");
00016     } 
00017     _histosByUpperBound[binMax] = histo;
00018     _histosByLowerBound[binMin] = histo;
00019     bool found = false;
00020     foreach (AIDA::IHistogram1D* hist, _histos) {
00021       if (hist == histo) {
00022         found = true;
00023         break;
00024       }
00025     }
00026     
00027     if (!found){
00028       _histos.push_back(histo);
00029     }
00030     
00031     return *this;
00032   }
00033 
00034 
00035 
00036   template<typename T>
00037   AIDA::IHistogram1D* BinnedHistogram<T>::fill(const T& bin,
00038                                                      const T& val,
00039                                                      double weight) {
00040 
00041     typename map<T, AIDA::IHistogram1D*>::iterator histIt =
00042       _histosByUpperBound.upper_bound(bin);
00043     //check that the bin is not out of range
00044     if (histIt == _histosByUpperBound.end()) {
00045       return 0;
00046     }
00047  
00048     AIDA::IHistogram1D* histo = histIt->second;
00049     histIt = _histosByLowerBound.lower_bound(bin);
00050 
00051     // No need to check going beyond the upper bound if we already passed above
00052     // (given that upper bound > lower bound is checked)
00053     // Check it is not before the start of the map
00054     if (histIt == _histosByLowerBound.begin()) {
00055       return 0;
00056     }
00057     // By-lower-bound actually gives us the iterator one above the nearest element,
00058     // so decrement it. This is safe because we already checked we're not at the start!
00059     --histIt;
00060  
00061     if (histo != histIt->second) {
00062       return 0;
00063     }
00064     
00065     histo->fill(val, weight);
00066     
00067     return histo;
00068   }
00069 
00070 
00071 
00072   // Template declarations for the compiler.
00073   template class BinnedHistogram<double>;
00074   template class BinnedHistogram<int>;
00075   template class BinnedHistogram<float>;
00076 
00077 
00078 }