Hi ROOT experts,
I am trying to get a TH2 plot with COLZ option, but my legend isn’t working. When I leave the default options set, I get this:
mll_v_met__n327360.pdf (19 KB)
So I went through some of the discussions on this subject on this message board, including:
[root-forum.cern.ch/t/retrieve-tpaletteaxis-from-tgraph2d/17847/1
and
[root-forum.cern.ch/t/tpaletteaxis-on-top-of-pad/11282/1
Neither of these solutions seem to be working for me. My code will either not compile, or it will not run. A brief description of my environment: I am programming in C++ using ROOT libraries. My code is quite long, so I will try to copy-paste the important snippets.
This is in my header file (among other things):
TH2D *plot_hist_3D;
TPaletteAxis *pal;
This is in the script:
plot_hist_3D = new TH2D("plot","plot",n_binx,plot_minx,plot_maxx,n_biny,plot_miny,plot_maxy);
plot_hist_3D->GetXaxis()->SetLabelSize(0.05);
plot_hist_3D->GetYaxis()->SetLabelSize(0.05);
plot_hist_3D->GetXaxis()->SetTitleSize(0.05);
plot_hist_3D->GetYaxis()->SetTitleSize(0.05);
plot_hist_3D->GetXaxis()->SetTitleOffset(1.2);
plot_hist_3D->GetYaxis()->SetTitleOffset(1.5);
plot_hist_3D->GetXaxis()->SetLabelColor(1);
plot_hist_3D->GetXaxis()->SetAxisColor(1);
plot_hist_3D->GetXaxis()->SetTitleColor(1);
plot_hist_3D->GetYaxis()->SetLabelColor(1);
plot_hist_3D->GetYaxis()->SetAxisColor(1);
plot_hist_3D->GetYaxis()->SetTitleColor(1);
plot_hist_3D->GetXaxis()->SetTitle(xaxis_text.c_str());
plot_hist_3D->GetYaxis()->SetTitle(yaxis_text.c_str());
plot_hist_3D->SetTitle(title_text.c_str());
<insert code that fills the histogram>
plot_hist_3D->Draw("COLZ");
pal = (TPaletteAxis*)plot_hist_3D->GetListOfFunctions()->FindObject("palette");
pal->SetX1NDC(0.1);
pal->SetX2NDC(0.12);
pal->SetY1NDC(0.1);
pal->SetY2NDC(0.5);
gPad->Update();
I doubt it matters, but I also have a style.h file that contains a lot of my other settings, and includes this:
const Int_t NRGBs = 5;
const Int_t NCont = 255;
Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
gStyle->SetNumberContours(NCont);
When I run with the snippets of above code, the program crashes at the end with this error: (Note that it compiles fine, so I believe that I have included the correct libraries in my C++ code.)
*** Break *** segmentation violation
Generating stack trace…
dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/atos) has __RESTRICT/__restrict section
0x00007fff954ab7e1 in start (in libdyld.dylib) + 0
When I comment out these lines, it runs just fine but gives me the figure I included above.
pal = (TPaletteAxis*)plot_hist_3D->GetListOfFunctions()->FindObject("palette");
pal->SetX1NDC(0.1);
pal->SetX2NDC(0.12);
pal->SetY1NDC(0.1);
pal->SetY2NDC(0.5);
gPad->Update();