TH2::SetShowProjectionX() draw style


I’m wondering if there’s a way to set the draw style of the TH1 histograms that are created and displayed from a TH2::SetShowProjectionX or TH2::SetShowProjectionY. Often ROOT defaults to drawing with error bars and this is not necessarily the best for me. I can change the behavior for by calling TH1::SetDrawOption for the TH1 that is created, but this resets as soon as I change the projected region.

Default behavior (with error bars)
Screen Shot 2021-10-29 at 12.20.53 PM

Desired behavior (after TH1::SetDrawOption(“hist”))
Screen Shot 2021-10-29 at 12.21.13 PM


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.24/00
_Platform: macOS
_Compiler: clang 12.0.0

Maybe @couet has a better idea, but what about using TH2::ProjectionX(), like in the DynamicSlice.C tutorial?

That’s certainly possible - but this is something that I want to do interactively fairly regularly, rather than running a custom function/script every time. I was hoping that there was some global option for default TH1 draw style or something that would be simple to change

So let’s see if @couet has a solution

May be:


Unfortunately doesn’t seem to make any difference. I should say these are histograms that don’t have sqrt(N) errors - I’ve called hist->SetBinError() for each bin.

Ok, so in that case the only way is to use the option HIST

Try to first execute: yourTH2->SetOption("HIST"); // SetOption instead of SetDrawOption

This doesn’t work either - the 2D histogram draws with the HIST style, but the projections are still with error bars

Can you provide a small reproducer ?

Yeah sure, this should work. Just run and then graphically SetShowProjectionX on the canvas. (354 Bytes)

I see. I guess there is nothing we can easily do at that point. See the painting code of SetShowProjectionX. At the end of this method the projection drawing is done with:

      hp->SetYTitle("Number of Entries");

hp the the TH1D holding the projection. As you see hp is drawn without any option. Therefore if it has errors (which is apparently the case) they will be drawn.
Note that drawing the TH2D with option HIST has no effect in that case, expect drawing it which option HISTwhich is a bit ugly . COL is better for this TH2D.

Ok, that’s a shame. Obviously it would involve changing the source, but would it be an idea to pass a draw option through SetShowProjectionX to the hp->Draw() call?

Yes that would be ultimattly what should be done if you want to use SetShowProjectionX. I guess @moneta has his word to says in a such change.

You can also do it in two steps.

  1. Call ProjectionX on your TH2D
  2. Draw the returned TH1D (equivalent of hp) with the option you need.

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