ROOT Version: 6.18 Platform: Not Provided Compiler: Not Provided
Good day
I am trying to combine two canvases obtained from a cube into one canvas. So from my cube I do this:
root [1] ggdt_cube->GetYaxis()->SetRangeUser(386.24,406.24)
root [2] TH2D* matrix3 = static_cast<TH2D*>(ggdt_cube->Project3D(“zx”))
root [3] matrix3->Draw(“colz”)
and then I Project onto Y axis to get a 1D histogram plotted on a canvas with a default name c1.
Now I follow the same process but with a different gate : ggdt_cube->GetYaxis()->SetRangeUser(1114.24,113424)
and still get a D histogram plotted on a canvas with a default name c1.
I am trying to plot these on the same canvas but using Draw(“same”) does not work since c1 is the default name in both cases. Please help.
Hi @lum_totzin,
can you provide a minimal reproducer for your problem?
using Draw(“same”) does not work since c1 is the default name in both cases
Drawing two histograms on the same canvas is done by drawing the first with Draw(options..) and the second with Draw("SAME") – the name of the canvas they are drawn onto should be the same in both cases if you want them to appear on the same canvas. I probably misunderstand your comment.
I then project onto the matrix to get a 1D histogram. Now when I save this as a rootfile it saves as canvas e.g c1.
I then do the same procedure with a different gate i.e.
I gate a different histogram that I want to plot together with the first one again when I save this as a rootfile it saves a canvas c1. And the challange is that I cannot plot the two into one canvas.
It returns this:
Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_8:1:
/home/lmsebi/Documents/Cube2020/Convertcanv.C:9:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
htwo->Draw();
If the error message points to htwo->Draw(), it probably means that htwo is a nullptr.
You should double-check that GetPrimitive returns what you expect (i.e. not a nullptr).
If you can share gate1124Proj396.root with me I can also try to retrieve that histogram from the canvas and see what the right invocation is.
Ok as you see from c1->GetListOfPrimitives()->Print() the name of the histogram is actually "ggdt_cube_zx_py__1".
For easier access you might want to save directly the TH1 to file rather than saving the canvas. To save a histogram to a file, in general this is sufficient:
My simple example code writes an empty histogram, it was just to show how to write histograms to file.
As you can see the histogram is created but I don’t fill it with anything.
It should definitely be saved to file though, i.e. the file myfile.root should contain object myhisto.
P.S.
You can add some h.Fill(0.5); h.Fill(0.3); calls to fill the histogram with some values for testing purposes.
P.P.S.
But again, to solve your original problem, you can extract the histogram from the cavas with GetPrimitive("ggdt_cube_zx_py__1")
Actually this still gives an error
warning: null passed to a callee that requires a non-null argument [-Wnonnull]
htwo = (TH1F*)c1->GetPrimitive(“ggdt_cube_zx_py__1”);