I have a quick question, I have two TH1F and a TLine object in a canvas and i want to change the range of the canvas and save it as pdf. I tried something like:
But it does not work. I attached one of the hundreds of files that I have, could you please attach a piece of code that does the job? Oh and if I want to put a title on the Y axis how can I do that?
On the Y axis, for 1D histograms, you should use the SetMaximum() and SetMinimum() methods of TH1.
One way to see what to do is to set the range interactively on the canvas (using your mouse) and then save the result in a .C file. Then look at the saved .C file. You will see which commands are needed.
I’m afraid each canvas / pad will have a different name for its “frame” histogram and so you need to play a bit with the TList returned by the “TList *l = c->GetListOfPrimitives();” in order to find it (for example, find the first key which name begins with “frame_” or maybe it’s sufficient to find the first key which is a TH1D or maybe it’s simply always the second key in the list). Your attached “file.root” has a “frame_2a3b440”.
Thanks for your help, you were right about setaxisrange. When I try to see the contents of the canvas I get:
root [2] test_can->ls()
Canvas Name=test_can Title= Option=
TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= test_can Title= Option=
OBJ: TList TList Doubly linked list : 0
OBJ: TH1D frame_324f090 : 1 at: 0x7fee0d627cb0
OBJ: TH1F S+B model S+B model : 1 at: 0x7fee0d666f10
OBJ: TH1F S+B model_shaded S+B model : 1 at: 0x7fee0d673800
OBJ: TH1F B Model B Model : 1 at: 0x7fee0d674170
OBJ: TH1F B Model_shaded B Model : 1 at: 0x7fee0d674ac0
TLine X1=-0.000051 Y1=0.000000 X2=-0.000051 Y2=0.937242
OBJ: TLegend TPave X1= 0.550000 Y1=0.752000 X2=0.950000 Y2=0.950000
OBJ: TH1D frame_324f090 : 1 at: 0x7fee0d627cb0
So it has a list that stores two histograms a legend and a line, but furthermore a frame whose name is different for each file. If I use the B Model or S+M model or any model histogram it does not work, but if I use the frame_* it does work. You were also right about playing with the TList, I got the list, the first element in the list seems to be the frame_* object, I set the range on it and it worked, as you see in the code below.
Canvases hold the graphics objects do be drawn. It is a display list, not a directory holding histograms. So the order is important. In this canvas it is very likely that the first histogram “frame…” has been put there to define the frame and the other histogram have been drawn in that frame using the option “SAME”… so it is the first histogram “frame…” which defines the axis limits and titles for all the others in the list. That’s why when you act on any other histogram but “frame…” it has no effect.