HistogramGenerator

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#ifndef HistogramGenerator_H
#define HistogramGenerator_H
#include "FastSimulation/Utilities/interface/BaseNumericalRandomGenerator.h"

#include "TH1.h"

/** Numerical Random Generator for Gamma distribution.
 *  Copy of LandauFluctuations
 */

class RandomEngine;
class TAxis;

class HistogramGenerator : public BaseNumericalRandomGenerator {
public:
  /// Constructor : initialization of the Random Generator
  HistogramGenerator(TH1* histo)
      : BaseNumericalRandomGenerator(histo->GetXaxis()->GetXmin(), histo->GetXaxis()->GetXmax(), 100000, 3),
        myHisto(histo),
        theXaxis(histo->GetXaxis()),
        nbins(histo->GetXaxis()->GetNbins()) {
    //    std::cout << "Old xmin/xmax = " << xmin << " " << xmax << std::endl;
    // Drop lowest and highest empty bins
    double du = (xmax - xmin) / (float)nbins;
    // Restrict xmin to meaningful values
    while (function(xmin) <= 0.)
      xmin += du;
    // Restrict xmax to meaningful values
    while (function(xmax) <= 0.)
      xmax -= du;

    if (xmin != histo->GetXaxis()->GetXmin())
      xmin -= du;
    if (xmax != histo->GetXaxis()->GetXmax())
      xmax += du;

    //    std::cout << "New xmin/xmax = " << xmin << " " << xmax << std::endl;

    //    std::cout <<" Init " << std::endl;
    initialize();
  }

  /// Default destructor
  ~HistogramGenerator() override {}

  /// The probability density function implementation
  double function(double x) override { return ersatzt(x); }

private:
  /// Pointer to the histogram
  TH1* myHisto;

  /// the axis
  TAxis* theXaxis;

  /// n bins
  int nbins;

  /// Gamma Function
  double ersatzt(double x);
};
#endif