Frame variable bin width

Dear Experts,

I have a simple question:
I have a histogram defined with variable bin width, now I want to define TFrame with the same variable bin width. But the frame drawn with wrong x-axis binning.
Here are a part of the definition of the histogram and the frame

const int NMbins = 5;
 double Mbins[6]={0.,25.,50.,200., 500.,1000.};
  TH1F *hPFMET  = new TH1F("hPFMET","",NMbins, Mbins);

Now in a new macro I want to create a Frame with the same variable binning to draw this histogram on top, what I did

const int NMbins = 5;
double Mbins[6]={0.,25.,50.,200., 500.,1000.};
hframe= new TH2F("hframe","hframe",NMbins,Mbins,1000, 0.000001, 100000.);//
 

I get my histogram but the x axis has a different binning.

Do you have any ideas how to solve?

Frames have no real “binning”.

{
  const int NMbins = 5;
  double Mbins[(NMbins + 1)] = {0., 25., 50., 200., 500., 1000.};
  TH1F *hPFMET = new TH1F("hPFMET", "My Fantastic Result", NMbins, Mbins);
  TF1 *f = ((TF1*)(gROOT->GetFunction("gaus")));
  if (f) f->SetParameters(1., 150., 50.);
  hPFMET->FillRandom("gaus", 10000);
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 2);
  c->cd(1);
  hPFMET->Draw();
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
  gPad->GetFrame()->Print();
  gPad->GetFrame()->Dump();
  c->cd(2);
  TH1F *frame = gPad->DrawFrame(Mbins[0],
                                hPFMET->GetMinimum(),
                                Mbins[NMbins],
                                hPFMET->GetMaximum(),
                                hPFMET->GetTitle());
  frame->SetBins(NMbins, Mbins); // set the same bins as "hPFMET"
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
  gPad->GetFrame()->Print();
  gPad->GetFrame()->Dump();
  c->cd(0);
  std::cout << "hPFMET : " << hPFMET->GetNbinsY() << " x " << hPFMET->GetNbinsX() << std::endl;
  std::cout << "frame : " << frame->GetNbinsY() << " x " << frame->GetNbinsX() << std::endl;
}

See also:

Dear Wile,

Thanks for your reply, I tried the first proposed solution but I see that the x-axis range didn’t change. I run your macro and attached the plot.
rebinning_frame.pdf (14.4 KB)

Your plot looks perfectly fine.
I have no idea what you’re trying to achieve.
If you want the y-axis from 0.000001 to 100000., simply instead of hPFMET->GetMinimum() use 1.e-6 and instead of hPFMET->GetMaximum() use 1.e5 (note also that frame->SetBins(NMbins, Mbins); call is “optional” so not really required).

Maybe I didn’t explain my question well. I want that the binning in X-axis to be written just the 5 bins we defined not the 20 GeV bin as in the plot attached before.

@couet In this case, you will probably need to play with: TGaxis

Let me try to understand: you have a variable bin size histogram and you what to see the labels at the bin edges only ?

Yes I have a histogram of variable bin width in different root files, I stacked all the files together as in the attached plot.I want to change the X-axis to be exactly as the bins I set in the histogram.the bins are {0.,25.,50.,200., 500.,1000.}.

Hi raly,

in your posts I see 3 (different) issues:

  1. “I get my histogram but the x axis has a different binning.”
    Wiles answer shows binning as expected.

  2. " want that the binning in X-axis to be written just the 5 bins"
    I guess you want labels at 0, 25, 50, 200, 500, 1000
    Think thats not implemented currently in TGaxis but could
    easily done “by hand” (TLatex)

  3. “I have a histogram of variable bin width in different root files”
    If you want to change their binning each new bin edge m u s t coincide
    with one in the original (bins can not be divided)
    I assume you want to put the hist in your plot pTmiss into one
    with (0, 50, 200, 500, 1000) binning:, thats not possible without
    violating your data, you must go back to the original data (TTree)

Otto

Concerning point 2:
cl.C (656 Bytes)
more details in doc for TGaxis::ChangeLabel
Otto