SetBarOffset not working


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.26/11
_Platform: Scientific Linux 6.7
Compiler: Not Provided



Hi everyone, I have an issue with the command SetBarOffset. I have an histogram, saved in a root file, which you can see in graph1, that I want to plot and shift by -0.5. I wrote a macro that plots this histogram from the root file and has a boolean named “offset_bin” (line 29); when it’s set to true it shift the histogram by the required offset. the code is at line 328.
I tried to use the command Histo1->SetBarOffset(-0.5), and then the option “bars” in the draw command, but what I get is what you can see in graph2, i.e. the histogram gets shifted but it hasn’t the fill/line/color attribute I gave to it before, whereas they seem to work fine in the plot without the shift option (graph1).
I can’t find where the conflict is (if there is one), so I uploaded the entire macro below.
Thanks!

graph1.pdf (14.1 KB)
graph2.pdf (14.2 KB)
graph.C (15.5 KB)

Your macro cannot be run, we’re missing the root filesHistoOutMcDY_1.root and 2.
From what I can see, you have this:

  if(offset_bin == false){
    Histo1->Draw(options.c_str());
  }
  if(offset_bin == true){
    Histo1->SetBarOffset(-0.5);
    Histo1->SetFillColor(kWhite);
    Histo1->Draw("bars col");
    
  }

where options = “col text”. I don’t know which of the above cases doesn’t do what you expect, but in any case:

  • “COL” is not a valid option for 1D histograms, so that doesn’t do anything if Histo1 is 1D. See the plotting options in the THistPainter documentation.
  • If your boolean is false, you are not setting a fill colour as in the other case.

I’m sorry, maybe I didn’t explain my issue too clearly.
I didn’t uploaded the files because I don’t know if I can share them. By the way, I upload here the file HistoOutMcDY_1, which is the one that contains the histogram i’m interested in. you should be able to run it by commenting the line in the macro that contains the second file.
for the drawing options “col text” it was just a test, if I live the string “options” empty I get the same result you see in graph 2.
The part that gives me a problem in the code you reported is the second, when offset_bin == true; you can see the original graph that is contained inside the file linked below in the following picture (original_graph)
original_graph.pdf (13.5 KB)
if offset_bin == false I get the image in graph_1, which is what I want. I don’t set a fill option, but i do set a line option: if you look inside the for loop at line 104, i retrieve the histogram at line 115 and then set a color/line option based on the tipe of graph (for this graph in particular the option at line 132), and this option should be valid regardless of the boolean offset_bin (correct me if I’m wrong) because this if option based on offset_bin doesn’t appear in the for loop.
But instead it seems that when offset_bin == true this option gets bypassed, because the resulting histogram is filled in black, even after I added the line Histo1->SetFillColor(kWhite); above Histo1->Draw("bars col");.
So there is either an error which I can’t find or a conflict between this options that I’m not aware of.

HistoOutMcDY_1.root (427.9 KB)

So you want to read the histogram and just shift it (offset the bars)?
To shift it, you need to draw with one of the “bar” options, as you are trying, but this has the side effect that the fill is black by default. If you change the fill to kWhite, it doesn’t change to white; you can try instead SetFillColor(10), which is a solid white. With “bar”, however, SetLineColor has no effect, so the histogram disappears if you have a white background in the frame (so, use another fill colour --see TColor). If you want the histogram to look like the original (a line and empty/white fill), but shifted, I think you’ll have to create and fill a new histogram with the “shifted” bins.

Option BAR use only filled bars

Ok, I didn’t know that. I will try with the solution suggested by @dastudillo.
Thanks to both of you!