Create TH1D's and TH2D's that have non-homogenous bin sizes such that when you use gPad→SetLogx() the bins looks to have the same width.

TH1D* logTH1D(const char* name, const char* title,
        Int_t bins, Double_t xlow, Double_t xhigh) {
    // Create some boundries and widths in the log-realm.
    Axis_t from = TMath::Log10(xlow);
    Axis_t to = TMath::Log10(xhigh);
    Axis_t width = (to - from) / bins;
    Axis_t *new_bins = new Axis_t[bins + 1];

    // The the bin low-edges with their new logarithmically happy selves.
    for (int i = 0; i <= bins; i++)
        new_bins[i] = TMath::Power(10, from + i * width);
    // Create the new histogram.
    TH1D* log_hist = new TH1D(name, title, bins, new_bins);
    // Clear the bins in memory since they were copied to the hist.
    delete new_bins;
    // Smile.
    return log_hist;
}

TH2D* logTH2D(const char* name, const char* title,
        Int_t binsx, Double_t xlow, Double_t xhigh,
        Int_t binsy, Double_t ylow, Double_t yhigh) {
    // Create some boundries and widths in the log-realm.
    Axis_t from = TMath::Log10(xlow);
    Axis_t to = TMath::Log10(xhigh);
    Axis_t width = (to - from) / binsx;
    Axis_t *new_binsx = new Axis_t[binsx + 1];

    // The the bin low-edges with their new logarithmically happy selves.
    for (int i = 0; i <= binsx; i++)
        new_binsx[i] = TMath::Power(10, from + i * width);

    from = TMath::Log10(ylow);
    to = TMath::Log10(yhigh);
    width = (to - from) / binsy;
    Axis_t *new_binsy = new Axis_t[binsy + 1];

    for (int i = 0; i <= binsy; i++)
        new_binsy[i] = TMath::Power(10, from + i * width);

    // Create the new histogram.
    TH2D* log_hist = new TH2D(name, title, binsx, new_binsx, binsy, new_binsy);
    // Clear the bins in memory since they were copied to the hist.
    delete new_binsx;
    delete new_binsy;
    // Smile.
    return log_hist;
}
log_hists.txt · Last modified: 2013/10/02 12:28 by mwoods
Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki |