Redraw TH1 and omit the empty histos

Hi

Probably, it has been already asked, but the proposed solutions (SetUserRange) could not work for me - What I want to do, is this - I do have collection of TH1 , and want to redraw all of them in a dynamic way so to omit the empty parts of the histos - For example, how is it possible to get rid of the empty left and right bins like in the attached histo ? I dont really care keeping or not the initial number of bins.

thanks

Alex


Why do SetRange and SetRangeUser not work? I recommend

h.GetXaxis()->SetRange(h.FindFirstBinAbove(0),h.FindLastBinAbove(0))

You may need to use -> instead of . depending on how you do object/pointer stuff.

Thanks! This indeed works ! Btw, is there a similar way to do this for a TStack ?

Cheers

-a

For a THStack you will need to loop over the contained histograms, or use the “last” histogram which contains the sum. See this discussion regarding the same issue in PyROOT:

Jean-François

Hi Jean-Francois

Although this works in principle, the problem is that only the last histo of the stack (the sum) is drawn, meaning that you loose the color-code due to the “stacking” in the first place - In other words, how would be possible to change the range dynamically, but still get the full collection of the TStack in the final plot ?

thanks

Alex

You should use the “SUM” only for the purpose of finding the first and the last nonzero bins. Draw the “full” THStack (e.g. with “NOSTACK”) and then “limit” the x-axis according to the found nonzero bins.

Hi

Sorry for my ignorance, is that exact part I do not know how to do, “limit the xaxis” - How can this be possible ? Can you provide a working example

Doing something like

gives

Error: illegal pointer to class object GetXaxis() 0x0

-a

See: [url]SetAxisRange(0.0,1.0,"Y"); is ineffective

Note: you MUST draw the THStack BEFORE you can retrieve its x-axis. You might also make sure that it is really drawn by calling “gPad->Modified(); gPad->Update();”.