Wrong axis when initializing a TH3D

Hi experts,
I am trying to make some TH3D. Yet as I have a quite specific axis from another histogram, cannot initialize the histogram with linear spacing. What I did is the following:

TH2D* MMKvsBeamEnergy = new TH2D("MMKvsBeamEnergy", "MMKvsBeamEnergy", 400, 0, 2000, Ax->GetNbins(), Ax->GetXbins()->GetArray());
TH3D* MMKvsMMKPionvsBeamEnergy = new TH3D("MMKvsMMKPionvsBeamEnergy", "MMKvsMMKPionvsBeamEnergy", MMKvsBeamEnergy->GetXaxis()->GetNbins(), MMKvsBeamEnergy->GetXaxis()->GetXbins()->GetArray(), MMKvsBeamEnergy->GetXaxis()->GetNbins(), MMKvsBeamEnergy->GetXaxis()->GetXbins()->GetArray(), Ax->GetNbins(), Ax->GetXbins()->GetArray());

The Ax Axis is obtained by:

TFile* fAx = TFile::Open("../Data/Real.root");
TH1* Histo = dynamic_cast<TH1*>(fAx->FindObjectAny("PGamma_Flux_photon_number_flumo"));
TAxis* Ax = Histo->GetXaxis();

Now my problem is the following:
Somehow all my TH3D I initialize in this way have x- and y-Axis going from 0 to 1. I do not understand how this could be and what to change. Is this a bug or is there a workaround?
Note: I tried to initialize the x- and y-axis linear, but there seems to be no implementation for this combination.


Problem is here.
When you create histogram with linear scale: 4000 bins from 0 to 2000, such histogram does not have custom bins array in the axis object. Just check:


It should return 0 in your case.
You need to provide custom bins yourself in TH3D constructor.
Same problem can be with Histo->GetXaxis() object.


Thank you very much!
This explains the problem and makes a solution possible. Just in case someone in the future will experience the same problem:
You simply have to create a linear array with the following function:

TArrayD* createArray(Int_t numberEntries, Double_t first, Double_t last){
    TArrayD *newArray = new TArrayD(numberEntries);
    Double_t step = (last-first)/(numberEntries-1);
    for(Int_t i=0; i<numberEntries; i++){
    return newArray;

This Array can then be used and the problem is away.
Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.