--- PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h 2009/09/25 15:36:45 1.1 +++ PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h 2010/03/23 17:27:44 1.7 @@ -106,17 +106,19 @@ */ void SetYAxis(TString label, unsigned int nBins, float min, float max, bool logBinning = false); + /*! @brief Sets the ROOT histogram's title. + * + * @param title The histogram title as it will appear on the histogram itself. + */ + void SetTitle(TString &title) { + _title = title; + } /*! @brief Sets up the histogram * * This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis() and #SetYAxis(). * * @param events Pointer to PamLevel2 events (unused). */ - - /*! @brief Sets the ROOT histogram's title. */ - void SetTitle(TString &title){ - _title = title; - } void Setup(PamLevel2 *events) { CollectionAction::Setup(events); _InitHistos(); @@ -141,7 +143,20 @@ return _histo; } - /*! Fills the ROOT and the vector histogram. */ + /*! @brief Returns a pointer to the ROOT histogram. + * + * @return A pointer to the root histogram + */ + TH2 *GetRootHisto() { + return _rootHisto; + } + + /*! Fills the ROOT and the vector histogram. + * + * @param xValue The value of the X coordinate associated to the event. + * @param yValue The value of the Y coordinate associated to the event. + * @param weight The weight which will be applied to the event. + */ void Fill(double xValue, double yValue, double weight = 1.); /*! @brief Gets the X overflow histogram. @@ -236,21 +251,32 @@ protected: + /*! @brief The vector containing the limits of the X bins(from lower to higher). */ std::vector _xBins; + /*! @brief The vector containing the limits of the Y bins(from lower to higher). */ std::vector _yBins; + /*! @brief A matrix containing the value of the histogram for each X-Y bin. */ SimpleMatrix _histo; + /*! @brief The ROOT histogram. */ TH2 *_rootHisto; + /*! @brief Base name of the output file. */ + TString _outFileBase; + /*! @brief Output file open mode (UPDATE or RECREATE, see documentation of TFile). */ + TString _mode; + /*! @brief Title for the ROOT histogram. */ + TString _title; + /*! @brief X axis label for the ROOT histogram. */ + TString _xLabel; + /*! @brief Y axis label for the ROOT histogram. */ + TString _yLabel; + private: vector _xUnderflow, _xOverflow, _yUnderflow, _yOverflow; HistoType _xUnderYUnderflow, _xOverYOverflow, _xUnderYOverflow, _xOverYUnderflow; - TString _outFileBase; - TString _mode; - TString _title, _xLabel, _yLabel; bool _outRoot; bool _outText; - void _CreateHisto(); void _InitHistos(); }; @@ -262,22 +288,24 @@ _rootHisto = NULL; } +// Specializations for _CreateHistos(). See Histo2DAction.cpp +template<> +void Histo2DAction::_CreateHisto(); + +template<> +void Histo2DAction::_CreateHisto(); + +template<> +void Histo2DAction::_CreateHisto(); + template void Histo2DAction::_InitHistos() { _CreateHisto(); - - if (_xBins.size() < 2){ - _xBins.resize(2); - _xBins[0] = 0.; - _xBins[1] = 1.; - } - - if (_yBins.size() < 2){ - _yBins.resize(2); - _yBins[0] = 0.; - _yBins[1] = 1.; - } + if (_xBins.size() < 2) // SetXAxis not called by the main program, or wrongly filled (only 1 bin limit) + SetXAxis("Default X", 10, 0., 1.); + if (_yBins.size() < 2) // SetYAxis not called by the main program, or wrongly filled (only 1 bin limit) + SetYAxis("Default Y", 10, 0., 1.); if (_rootHisto) { Double_t *auxXArray = new Double_t[_xBins.size()]; @@ -305,23 +333,27 @@ /* The row index (first) corresponds to the position on the vertical (Y) axis. */ _histo.Resize(_yBins.size() - 1, _xBins.size() - 1); - _xUnderflow.resize(_yBins.size()); - _xOverflow.resize(_yBins.size()); - _yUnderflow.resize(_xBins.size()); - _yOverflow.resize(_xBins.size()); + _xUnderflow.resize(_yBins.size() - 1); + _xOverflow.resize(_yBins.size() - 1); + _yUnderflow.resize(_xBins.size() - 1); + _yOverflow.resize(_xBins.size() - 1); } template Histo2DAction::~Histo2DAction() { + + delete _rootHisto; + _rootHisto = NULL; } template Histo2DAction::Histo2DAction(const char *actionName, TString title, TString outFileBase, TString mode, bool outRoot, bool outText) : - CollectionAction(actionName), _xBins(0), _yBins(0), _histo(0, 0), _rootHisto(NULL), _xUnderflow(0), _xOverflow(0), - _yUnderflow(0), _yOverflow(0), _outFileBase(outFileBase), _mode(mode), _title(title), _xLabel(""), _yLabel(""), - _outRoot(outRoot), _outText(outText) { + CollectionAction(actionName), _xBins(0), _yBins(0), _histo(0, 0), _rootHisto(NULL), _outFileBase(outFileBase), _mode( + mode), _title(title), _xLabel(""), _yLabel(""), _xUnderflow(0), _xOverflow(0), _yUnderflow(0), _yOverflow(0), + _xUnderYUnderflow((HistoType) 0), _xOverYOverflow((HistoType) 0), _xUnderYOverflow((HistoType) 0), + _xOverYUnderflow((HistoType) 0), _outRoot(outRoot), _outText(outText) { } @@ -444,7 +476,7 @@ } template -inline void Histo2DAction::Fill(double xValue, double yValue, double weight) { +void Histo2DAction::Fill(double xValue, double yValue, double weight) { _rootHisto->Fill(xValue, yValue, weight); @@ -457,7 +489,7 @@ UOflow = true; } - if (xValue > _xBins.back()) { + if (xValue >= _xBins.back()) { xBin = _xBins.size(); UOflow = true; } @@ -467,7 +499,7 @@ UOflow = true; } - if (yValue > _yBins.back()) { + if (yValue >= _yBins.back()) { yBin = _yBins.size(); UOflow = true; } @@ -501,8 +533,10 @@ return; } else { - if (yBin == (int)_yBins.size()) { + if (yBin == (int) _yBins.size()) { _xUnderYOverflow += (HistoType) weight; + cout << "_xUnderYOverflow: " << GetXUnderYOverflow() << ", weight: " << weight << ", (HistoType) weight: " + << (HistoType) weight << endl; return; } else { @@ -512,13 +546,13 @@ } } - if (xBin == (int)_xBins.size()) { + if (xBin == (int) _xBins.size()) { if (yBin == -1) { _xOverYUnderflow += (HistoType) weight; return; } else { - if (yBin ==(int) _yBins.size()) { + if (yBin == (int) _yBins.size()) { _xOverYOverflow += (HistoType) weight; return; } @@ -534,7 +568,7 @@ return; } - if (yBin == (int)_yBins.size()) { + if (yBin == (int) _yBins.size()) { _yOverflow[xBin] += (HistoType) weight; return; } @@ -559,11 +593,10 @@ ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out); for (unsigned int i = 0; i < _histo.GetNRows(); i++) { for (unsigned int j = 0; j < _histo.GetNCols(); j++) { - outTextFile << _histo[i][j] << " "; + outTextFile << setw(7) << _histo[i][j] << " "; } outTextFile << "\n"; } - outTextFile << endl; outTextFile.close(); } }