Saving in png with allowed OpenGL.CanvasPreferGL is black figure


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/04
Platform: Linux: 5.4.35-1-MANJARO
Compiler: gcc v: 9.3.0


Hello,
I have one issue with saving a 2D histogram to png format after allowing OpenGL.CanvasPreferGL (= 1) for transparency in system.rootrc (according this link https://root.cern.ch/doc/master/classTAttFill.html). Only outcome is black figure with nothing else. Other formats are OK (pdf, jpg, svg etc.) and also 1D histograms are normally saved in png with transparent filling.
The problem disappears if the option OpenGL.CanvasPreferGL is disabled ( = 0).

Any suggestions how to fix this?

[EDIT]
The problem is for every object saved in the png format, not just Hist2D.

Let me try.

I do not see a black canvas on Mac when I save a 2D histogram drawn with option COLZ. I guess it is fine if you run in batch mode ? … Only for a 2D histogram ? it is weird. Which option are you using to draw the 2D histogram ?

Thank you for the quick reply.
Sorry, it behaves a little bit differently. From my first view it was only Hist2D but now it looks that it it irrelevant what is drawn the result is the same black figure if the option OpenGL.CanvasPreferGL is allowed and it is the format png.

For example for this code:

void Hist1D2D_Transpar_PNG()
{
   TF2 *Func2D = new TF2("Func2D","xygaus",-4,4,-4,4);
   Double_t Params_1[] = {100,-1.4,1.9,1.1,2};
   Func2D->SetParameters(Params_1);
   TH2F *Hist2D = new TH2F("Hist2D","Hist2D",20,-4,4,20,-4,4);   
   Hist2D->FillRandom("Func2D",100000);

   TF1 *Func1D = new TF1("Func1D","gaus",-4,4);
   Double_t Params_2[] = {100,-1.4,1.9};
   Func1D->SetParameters(Params_2);      
   TH1F *Hist1D = new TH1F("Hist1D","Hist1D",20,-4,4);
   Hist1D->FillRandom("Func1D",100000);

   Hist2D->SetMarkerColorAlpha(kRed, 0.1);
   Hist1D->SetFillColorAlpha(kBlue, 0.1);

   TCanvas *Canvas_1 = new TCanvas("Canvas_1", "Canvas_1", 1200,600);
   Canvas_1->Divide(2,1);

   Canvas_1->cd(1);
   Hist2D->Draw();
   
   Canvas_1->cd(2);
   Hist1D->Draw();

   //Canvas_1->SaveAs("Hist1D2D_TransparentON.png");
   //Canvas_1->SaveAs("Hist1D2D_TransparentON.pdf");
   //Canvas_1->SaveAs("Hist1D2D_TransparentON.svg");

   Canvas_1->SaveAs("Hist1D2D_TransparentOFF.png");
   Canvas_1->SaveAs("Hist1D2D_TransparentOFF.pdf");
   Canvas_1->SaveAs("Hist1D2D_TransparentOFF.svg");   
}

If OpenGL.CanvasPreferGL is 1 then this is the result:


Hist1D2D_TransparentON.pdf (975.5 KB)

If OpenGL.CanvasPreferGL is 0 then:


Hist1D2D_TransparentOFF.pdf (975.6 KB)

I wanted also ask, how is it possible that for pdf it is always possible to use this transparent filling although the option OpenGL.CanvasPreferGL is 0 (it can be seen it the pdf above)?

Sorry, I am not sure what you mean by the batch mode - just a script not compiled/interpreted code? (If so then yes, I run it as a batch mode - only .L …cpp then Hist1D2D_Transpar_PNG())

That’s pdf or png ? it is seems you are posting pdf files now. You talked about png before …

Your macro with CanvasPreferGL ON gives me the right pdf.

It is a little bit confusing but I am posting both of them, the upper one from the first two (allias for CanvasPreferGL ON) is png and the one below is pdf but just to download - the same for the OFF case which means that only the png figures are directly shown. I just wanted to show that with pdf it is OK and for png it is not (but you need to download the pdf :slight_smile: ).

Yes it is confusing. Have you tried to generate the png in batch mode with preferGL on ? (root -b)

It works in the batch mode. Thanks for this idea, I didn’t know about this option but still this is just a partial solution. Do you have idea why it falls without the batch mode? Because I have encountered a new problem with the batch mode and that is drawing of THstack. In the batch mode, I can not set properties of a THstack (name of axis etc.) because it always crashes and I assume that this has a connection with the batch mode and the fact that a THstack is needed to be drawn before any change is made… Should I make a new topic from this?
For example this code always crashes in the batch mode for me:

void candleplotstack()
{
   gStyle->SetTimeOffset(0);
   TRandom *rng       = new TRandom();
   TDatime *dateBegin = new TDatime(2010,1,1,0,0,0);
   TDatime *dateEnd   = new TDatime(2011,1,1,0,0,0);
   int bins = 1000;
   TH2I *h1 = new TH2I("h1","Machine A",6,dateBegin->Convert(),dateEnd->Convert(),bins,0,1000);
   TH2I *h2 = new TH2I("h2","Machine B",6,dateBegin->Convert(),dateEnd->Convert(),bins,0,1000);
   TH2I *hsum = new TH2I("h4","Sum",6,dateBegin->Convert(),dateEnd->Convert(),bins,0,1000);
   float Rand;
   for (int i = dateBegin->Convert(); i < dateEnd->Convert(); i+=86400*30) {
      for (int j = 0; j < 1000; j++) {
         Rand = rng->Gaus(500+sin(i/10000000.)*100,50); h1->Fill(i,Rand); hsum->Fill(i,Rand);
         Rand = rng->Gaus(500+sin(i/12000000.)*100,50); h2->Fill(i,Rand); hsum->Fill(i,Rand);
      }
   }
   h2->SetLineColor(kRed);
   hsum->SetFillColor(kGreen);
   TCanvas *c1 = new TCanvas();
   THStack *hs = new THStack("hs","Machine A+B");
   hs->Add(h1);
   hs->Add(h2,"candle2");
   hs->Add(hsum, "violin1");
   hs->Draw("candle3");
   hs->GetXaxis()->SetNdivisions(410);
   
   gPad->SetGrid(1,0);
   hs->GetXaxis()->SetTimeDisplay(1);
   hs->GetXaxis()->SetTimeFormat("%m/%y");
   hs->GetXaxis()->SetTitle("Date [month/year]");
   c1->Modified();
   gPad->BuildLegend(0.75,0.75,0.95,0.95,"");
}

with this error/warning:

warning: invalid memory pointer passed to a callee:
   hs->GetXaxis()->SetNdivisions(410);

Before this line, try to add: gPad->Update();

Thanks for reply but no change - I added this before the hs->GetXaxis()->SetNdivisions(410); line and it is still the same error/warning.

I do not see a problem with your macro:

$ root candleplotstack.C 
   ------------------------------------------------------------------
  | Welcome to ROOT 6.21/01                        https://root.cern |
  | (c) 1995-2019, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Apr 24 2020, 13:30:37                      |
  | From heads/master@v6-19-01-3820-gf8f7fdc503                      |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] 
Processing candleplotstack.C...
root [1] 

Is this in the batch mode? (just to be sure)

Ah I see you are still with OPenGl … yes I get the error with OpenGL in batch mode

In batch mode you do not need to turn OPenGL on anyway.

Thanks, now it works: in the batch mode with OPenGL off and it is still transparent. Do you have some suggestions for the interactive mode?

On linux the only way to have transparency is to go to GL. What you can do is to leave OpenGL OFF in the system.rootrc file and put the following line in you rootlogon.C file.

if (!gROOT->IsBatch()) gStyle->SetCanvasPreferGL(true);

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