/[PAMELA software]/PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h
ViewVC logotype

Diff of /PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by pam-fi, Fri Sep 25 15:36:45 2009 UTC revision 1.8 by pam-fi, Thu Aug 12 14:37:03 2010 UTC
# Line 106  public: Line 106  public:
106     */     */
107    void SetYAxis(TString label, unsigned int nBins, float min, float max, bool logBinning = false);    void SetYAxis(TString label, unsigned int nBins, float min, float max, bool logBinning = false);
108    
109      /*! @brief Sets the ROOT histogram's title.
110       *
111       * @param title The histogram title as it will appear on the histogram itself.
112       */
113      void SetTitle(TString &title) {
114        _title = title;
115      }
116    /*! @brief Sets up the histogram    /*! @brief Sets up the histogram
117     *     *
118     * This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis() and #SetYAxis().     * This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis() and #SetYAxis().
119     *     *
120     * @param events Pointer to PamLevel2 events (unused).     * @param events Pointer to PamLevel2 events (unused).
121     */     */
   
   /*! @brief Sets the ROOT histogram's title. */  
   void SetTitle(TString &title){  
     _title = title;  
   }  
122    void Setup(PamLevel2 *events) {    void Setup(PamLevel2 *events) {
123      CollectionAction::Setup(events);      CollectionAction::Setup(events);
124      _InitHistos();      _InitHistos();
# Line 141  public: Line 143  public:
143      return _histo;      return _histo;
144    }    }
145    
146    /*! Fills the ROOT and the vector histogram. */    /*! @brief Returns a pointer to the ROOT histogram.
147       *
148       * @return A pointer to the root histogram
149       */
150      TH2 *GetRootHisto() {
151        return _rootHisto;
152      }
153    
154      /*! Fills the ROOT and the vector histogram.
155       *
156       * @param xValue The value of the X coordinate associated to the event.
157       * @param yValue The value of the Y coordinate associated to the event.
158       * @param weight The weight which will be applied to the event.
159       */
160    void Fill(double xValue, double yValue, double weight = 1.);    void Fill(double xValue, double yValue, double weight = 1.);
161    
162    /*! @brief Gets the X overflow histogram.    /*! @brief Gets the X overflow histogram.
# Line 236  public: Line 251  public:
251    
252  protected:  protected:
253    
254      /*! @brief The vector containing the limits of the X bins(from lower to higher). */
255    std::vector<float> _xBins;    std::vector<float> _xBins;
256      /*! @brief The vector containing the limits of the Y bins(from lower to higher). */
257    std::vector<float> _yBins;    std::vector<float> _yBins;
258      /*! @brief A matrix containing the value of the histogram for each X-Y bin. */
259    SimpleMatrix<HistoType> _histo;    SimpleMatrix<HistoType> _histo;
260      /*! @brief The ROOT histogram. */
261    TH2 *_rootHisto;    TH2 *_rootHisto;
262    
263      /*! @brief Base name of the output file. */
264      TString _outFileBase;
265      /*! @brief Output file open mode (UPDATE or RECREATE, see documentation of TFile). */
266      TString _mode;
267      /*! @brief Title for the ROOT histogram. */
268      TString _title;
269      /*! @brief X axis label for the ROOT histogram. */
270      TString _xLabel;
271      /*! @brief Y axis label for the ROOT histogram. */
272      TString _yLabel;
273    
274  private:  private:
275    
276    vector<HistoType> _xUnderflow, _xOverflow, _yUnderflow, _yOverflow;    vector<HistoType> _xUnderflow, _xOverflow, _yUnderflow, _yOverflow;
277    HistoType _xUnderYUnderflow, _xOverYOverflow, _xUnderYOverflow, _xOverYUnderflow;    HistoType _xUnderYUnderflow, _xOverYOverflow, _xUnderYOverflow, _xOverYUnderflow;
   TString _outFileBase;  
   TString _mode;  
   TString _title, _xLabel, _yLabel;  
278    bool _outRoot;    bool _outRoot;
279    bool _outText;    bool _outText;
   
280    void _CreateHisto();    void _CreateHisto();
281    void _InitHistos();    void _InitHistos();
282  };  };
# Line 262  void Histo2DAction<HistoType>::_CreateHi Line 288  void Histo2DAction<HistoType>::_CreateHi
288    _rootHisto = NULL;    _rootHisto = NULL;
289  }  }
290    
291    // Specializations for _CreateHistos(). See Histo2DAction.cpp
292    template<>
293    void Histo2DAction<Int_t>::_CreateHisto();
294    
295    template<>
296    void Histo2DAction<Float_t>::_CreateHisto();
297    
298    template<>
299    void Histo2DAction<Double_t>::_CreateHisto();
300    
301  template<class HistoType>  template<class HistoType>
302  void Histo2DAction<HistoType>::_InitHistos() {  void Histo2DAction<HistoType>::_InitHistos() {
303    
304    _CreateHisto();    _CreateHisto();
305      if (_xBins.size() < 2) // SetXAxis not called by the main program, or wrongly filled (only 1 bin limit)
306    if (_xBins.size() < 2){      SetXAxis("Default X", 10, 0., 1.);
307      _xBins.resize(2);    if (_yBins.size() < 2) // SetYAxis not called by the main program, or wrongly filled (only 1 bin limit)
308      _xBins[0] = 0.;      SetYAxis("Default Y", 10, 0., 1.);
     _xBins[1] = 1.;  
   }  
   
   if (_yBins.size() < 2){  
     _yBins.resize(2);  
     _yBins[0] = 0.;  
     _yBins[1] = 1.;  
   }  
309    
310    if (_rootHisto) {    if (_rootHisto) {
311      Double_t *auxXArray = new Double_t[_xBins.size()];      Double_t *auxXArray = new Double_t[_xBins.size()];
# Line 305  void Histo2DAction<HistoType>::_InitHist Line 333  void Histo2DAction<HistoType>::_InitHist
333    /* The row index (first) corresponds to the position on the vertical (Y) axis. */    /* The row index (first) corresponds to the position on the vertical (Y) axis. */
334    _histo.Resize(_yBins.size() - 1, _xBins.size() - 1);    _histo.Resize(_yBins.size() - 1, _xBins.size() - 1);
335    
336    _xUnderflow.resize(_yBins.size());    _xUnderflow.resize(_yBins.size() - 1);
337    _xOverflow.resize(_yBins.size());    _xOverflow.resize(_yBins.size() - 1);
338    _yUnderflow.resize(_xBins.size());    _yUnderflow.resize(_xBins.size() - 1);
339    _yOverflow.resize(_xBins.size());    _yOverflow.resize(_xBins.size() - 1);
340    
341  }  }
342    
343  template<class HistoType>  template<class HistoType>
344  Histo2DAction<HistoType>::~Histo2DAction() {  Histo2DAction<HistoType>::~Histo2DAction() {
345    
346      delete _rootHisto;
347      _rootHisto = NULL;
348  }  }
349    
350  template<class HistoType>  template<class HistoType>
351  Histo2DAction<HistoType>::Histo2DAction(const char *actionName, TString title, TString outFileBase, TString mode,  Histo2DAction<HistoType>::Histo2DAction(const char *actionName, TString title, TString outFileBase, TString mode,
352      bool outRoot, bool outText) :      bool outRoot, bool outText) :
353    CollectionAction(actionName), _xBins(0), _yBins(0), _histo(0, 0), _rootHisto(NULL), _xUnderflow(0), _xOverflow(0),    CollectionAction(actionName), _xBins(0), _yBins(0), _histo(0, 0), _rootHisto(NULL), _outFileBase(outFileBase), _mode(
354        _yUnderflow(0), _yOverflow(0), _outFileBase(outFileBase), _mode(mode), _title(title), _xLabel(""), _yLabel(""),        mode), _title(title), _xLabel(""), _yLabel(""), _xUnderflow(0), _xOverflow(0), _yUnderflow(0), _yOverflow(0),
355        _outRoot(outRoot), _outText(outText) {        _xUnderYUnderflow((HistoType) 0), _xOverYOverflow((HistoType) 0), _xUnderYOverflow((HistoType) 0),
356          _xOverYUnderflow((HistoType) 0), _outRoot(outRoot), _outText(outText) {
357    
358  }  }
359    
# Line 444  void Histo2DAction<HistoType>::SetYAxis( Line 476  void Histo2DAction<HistoType>::SetYAxis(
476  }  }
477    
478  template<class HistoType>  template<class HistoType>
479  inline void Histo2DAction<HistoType>::Fill(double xValue, double yValue, double weight) {  void Histo2DAction<HistoType>::Fill(double xValue, double yValue, double weight) {
480    
481    _rootHisto->Fill(xValue, yValue, weight);    _rootHisto->Fill(xValue, yValue, weight);
482    
# Line 457  inline void Histo2DAction<HistoType>::Fi Line 489  inline void Histo2DAction<HistoType>::Fi
489      UOflow = true;      UOflow = true;
490    }    }
491    
492    if (xValue > _xBins.back()) {    if (xValue >= _xBins.back()) {
493      xBin = _xBins.size();      xBin = _xBins.size();
494      UOflow = true;      UOflow = true;
495    }    }
# Line 467  inline void Histo2DAction<HistoType>::Fi Line 499  inline void Histo2DAction<HistoType>::Fi
499      UOflow = true;      UOflow = true;
500    }    }
501    
502    if (yValue > _yBins.back()) {    if (yValue >= _yBins.back()) {
503      yBin = _yBins.size();      yBin = _yBins.size();
504      UOflow = true;      UOflow = true;
505    }    }
# Line 501  inline void Histo2DAction<HistoType>::Fi Line 533  inline void Histo2DAction<HistoType>::Fi
533          return;          return;
534        }        }
535        else {        else {
536          if (yBin == (int)_yBins.size()) {          if (yBin == (int) _yBins.size()) {
537            _xUnderYOverflow += (HistoType) weight;            _xUnderYOverflow += (HistoType) weight;
538            return;            return;
539          }          }
# Line 512  inline void Histo2DAction<HistoType>::Fi Line 544  inline void Histo2DAction<HistoType>::Fi
544        }        }
545      }      }
546    
547      if (xBin == (int)_xBins.size()) {      if (xBin == (int) _xBins.size()) {
548        if (yBin == -1) {        if (yBin == -1) {
549          _xOverYUnderflow += (HistoType) weight;          _xOverYUnderflow += (HistoType) weight;
550          return;          return;
551        }        }
552        else {        else {
553          if (yBin ==(int) _yBins.size()) {          if (yBin == (int) _yBins.size()) {
554            _xOverYOverflow += (HistoType) weight;            _xOverYOverflow += (HistoType) weight;
555            return;            return;
556          }          }
# Line 534  inline void Histo2DAction<HistoType>::Fi Line 566  inline void Histo2DAction<HistoType>::Fi
566        return;        return;
567      }      }
568    
569      if (yBin == (int)_yBins.size()) {      if (yBin == (int) _yBins.size()) {
570        _yOverflow[xBin] += (HistoType) weight;        _yOverflow[xBin] += (HistoType) weight;
571        return;        return;
572      }      }
# Line 559  void Histo2DAction<HistoType>::Finalize( Line 591  void Histo2DAction<HistoType>::Finalize(
591        ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out);        ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out);
592        for (unsigned int i = 0; i < _histo.GetNRows(); i++) {        for (unsigned int i = 0; i < _histo.GetNRows(); i++) {
593          for (unsigned int j = 0; j < _histo.GetNCols(); j++) {          for (unsigned int j = 0; j < _histo.GetNCols(); j++) {
594            outTextFile << _histo[i][j] << "  ";            outTextFile << setw(7) << _histo[i][j] << "  ";
595          }          }
596          outTextFile << "\n";          outTextFile << "\n";
597        }        }
       outTextFile << endl;  
598        outTextFile.close();        outTextFile.close();
599      }      }
600    }    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.23