The good old X-axis range problem

I am sure that I’ve ran into this problem before and I have not yet found a viable solution, some related posts are here:

I’ve seen many replies that actually do not solve the problem apparently since ROOT3.

Bottom line, I have not yet seen a solution. I’ll describe the situation.

I have a TH1 in a root file.
TFile* file=…
TH1* dummyhist=(TH1*)file->Get(…);

Now, this dummy histogram has values from -15000 to 50000, but I only care about seeing 0 to 3000.

I’ve tried
dummyhist->GetXaxis()->SetRange/SetRangeUser (nothing)
dummyhist->GetXaxis()->SetLimits (label changes but not the position of actual values).

I cannot think of a way to resolve other than to redeclare:
TH1* dummy2=new TH1(“contents and binning as demanded”)
for… fill dummy 1 elements into dummy 2 with significant over/underflow

Does anyone have real a solution for this?

_ROOT Version:6.14.0.
Platform: Not Provided
Compiler: Not Provided

The following works for me, right after opening hsimple.root on ROOT 6.16.00

root [1] hpx->Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [2] hpx->GetXaxis()->SetRangeUser(0,2)
root [3] gPad->Modified()
root [4] 

Note that before doing gPad->Modified() you don’t see the changes.
Having said that, I have also found cases when no matter how many Modifieds or Updates on pads or canvases, the axis doesn’t change, so I’m not very sure what’s going on :slight_smile:

Thank you dastudillo.
I am half-successful with this method. I don’t know why but the histogram shifts 0 just fine, but the upper range seems limited to the maximum of the data.

I’m pretty sure you can’t SetRange(User) beyond the range of actual bins in the histogram. The reasoning could be that that would misrepresent the under/overflow bin contents. If you want a very “zoomed out” view you need to make a histogram with bins that go as far as you want to see.

FWIW: I usually make my histograms with a much larger range than necessary, and later constrain the range to only have the bins which have nonzero content. The TH1 methods “FindFirstBinAbove” and “FindLastBinAbove” are useful for this.

I’m currently using what I call a ghosthist method
TF1* ghosthist= new TF1(“ghost”, range that I want);
ghosthist->SetTitle(dummyhist->GetTitle() )
… Take out the properties

I should also add that I am just writing a constructor to generalize these, so I can use it easier next time.
Something like

TH1* shift_Histogram(TH1* input hist){
TH1* shifted_histogram
return shifted_histogram

void draw_shifted_Histogram(TH1* input hist, double xmin, double xmax){
TH1* ghost= new TH1(new range)

I am wondering why the ROOT maintainers haven’t provided such constructors yet.

On request, I can provide the header and cxx ready to be compiled into ROOT.

SetRangeUser cannot extend the range beyond the histogram limits.
May be this can help:

Thank you couet. I am aware of the master document for the class.

I am just pointing out an existing deficiency in ROOT which hasn’t been resolved in a decade…

May be @moneta can comment.

Thank you Wlie_E_Coyote, I use the “ghost histogram” in very much the same manner.

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