Problem in fitting (in function!)

But, somehow, when I run the modified macro, I get the surface like this…more dense. I am using root 5.34.36 in ubuntu.

yes your root version does not have transparency on screen. Sorry.
Try may be to generate a PDF file ? the image might be transparent there.

yeah, generating pdf is helping. Thanks.

@couet Your statement is not correct. You are probably using Mac OS X / Cocoa. On Linux / X11, by default there is no screen transparency regardless of the ROOT version (one needs to switch to “GL”).

@Sandy Right in the beginning of your macro (before any canvas is created) add:
gStyle->SetCanvasPreferGL(kTRUE);

2 Likes

Yes … I know … it was a too short answer. Thanks to have completed my post.

#include "TF2.h"
#include "TH2.h"
#include "TGraph2DErrors.h"
#include "TAxis.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"

Double_t g2(Double_t *x, Double_t *par) {
   Double_t r1 = Double_t((x[0]-par[1])/par[2]);
   Double_t r2 = Double_t((x[1]-par[3])/par[4]);
   return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
}
Double_t fun2(Double_t *x, Double_t *par) {
   Double_t *p1 = &par[0];
   Double_t *p2 = &par[5];
   Double_t *p3 = &par[10];
   Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
   return result;
}

void example_graph() {
   gStyle->SetCanvasPreferGL(kTRUE);
   gStyle->SetOptStat(0);
   gStyle->SetOptTitle(0);

   TCanvas *c = new TCanvas();

   const Int_t npar = 15;
   Double_t f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
   TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
   f2->SetParameters(f2params);
   //Create an histogram and fill it randomly with f2
   TH2F *h2 = new TH2F("h2","From f2",40,-10,10,40,-10,10);
   Int_t nentries = 100000;
   h2->FillRandom("f2",nentries);
   TGraph2DErrors *g =
      new TGraph2DErrors((h2->GetNbinsX() * h2->GetNbinsY()));
   for (int i = 1; i <= h2->GetNbinsX(); i++)
      for (int j = 1; j <= h2->GetNbinsY(); j++) {
         g->SetPoint( ((i - 1) * h2->GetNbinsY() + (j - 1)),
                      h2->GetXaxis()->GetBinCenter(i), // x
                      h2->GetYaxis()->GetBinCenter(j), // y
                      h2->GetBinContent(i, j) ); // z
#if 1 /* 0 or 1 */
         g->SetPointError( ((i - 1) * h2->GetNbinsY() + (j - 1)),
                           0., // x
                           0., // y
                           h2->GetBinError(i, j) ); // z
#endif /* 0 or 1 */
      }
   delete h2; // no longer needed
#if 1 /* 0 or 1 */
   Double_t zmin = 1.; // e.g. -1. or 0. or 1.
   for (int i = (g->GetN() - 1); i >= 0; i--)
      if (g->GetZ()[i] <= zmin) g->RemovePoint(i); // remove if "z" <= "zmin"
#endif /* 0 or 1 */
   //Fit g with original function f2
   Float_t ratio = 4*nentries/100000;
   f2params[ 0] *= ratio;
   f2params[ 5] *= ratio;
   f2params[10] *= ratio;
   f2->SetParameters(f2params);
   g->Fit("f2","N");
   f2->SetNpx(80);
   f2->SetNpy(80);
   f2->SetMaximum(800);
   f2->SetLineColorAlpha(kBlack, 0.1);
   f2->SetTitle("some Thing;some X;some Y;some Z");
#if 1 /* 0 or 1 */
   gStyle->SetPalette(55, 0, 0.1); // 55 = kRainBow
   f2->Draw("surf2 fb"); // must be drawn first (due to a bug in ROOT)
#else /* 0 or 1 */
   f2->Draw("surf fb"); // must be drawn first (due to a bug in ROOT)
#endif /* 0 or 1 */
   // g->SetMaximum(800);
   g->SetMarkerStyle(kCircle); g->SetMarkerSize(0.5); // "p", "p0", "pcol"
   g->SetMarkerColorAlpha(kBlack, 0.5); // "p", "p0"
   g->SetLineColorAlpha(kBlack, 0.5); // "err"
   g->Draw("err p fb same"); // "p" or "p0" or "pcol"
}
1 Like

Yes, this works. Thank you very much @Wile_E_Coyote and @couet

  1. BTW, is it possible to set error bars (E, E1, or E2) with p, p0 or pcol option?
  2. Is it not possible to make transparent “surf4” so that the tail of one Gaussian 2D surface can be seen under another? You people have shown example with “surf” option.
  1. You could try to create and draw a TGraph2DErrors (instead of a TGraph2D). I modified the macro in my previous post so that you can play with it if you like (the “hidden surfaces” can also be made visible now with “surf2”), but it seems to me that in this case it is also o.k. to use a histogram drawn with “E” (as @couet shows).

  2. ROOT “removes” hidden lines and surfaces so you will not see the “hidden tail” with “surf” (unless @couet knows how to block this default behavior).

1 Like
   h2->SetMarkerStyle(20);
   h2->SetMarkerSize(0.5);

You can try to define a transparent color palette

   gStyle->SetPalette(kCherry,0,0.1);
   f2->Draw("surf2  fb same");

The first one is not helping.

The second one is OK. But, it fails when I use multi-palette in same pad. I’ve used TExec as you have shown in https://root.cern.ch/doc/master/multipalette_8C.html.

I don’t know why.

May be post again the macro you have so I can have a look at it.

Yeah, here it is…

I have modified your example only.

multi.C (755 Bytes)

This one is ok:

void multi() {
   gStyle->SetCanvasPreferGL(kTRUE);
   auto c  = new TCanvas("c","c",0,0,1000,800);

   auto frame = new TH2F ("frame","Example of two surface on the same plot",20,1,3,20,1,3);
   frame->SetMaximum(1.2);

   auto f1 = new TF2("f1","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);
   auto f2 = new TF2("f2","0.1+(2-(x-3)*(x-3))*(2-(y-3)*(y-3))",2,4,2,4);

   f1->SetLineWidth(1);
   f1->SetLineColor(kBlue);
   f2->SetLineWidth(1);
   f2->SetLineColor(kBlack);

   frame->Draw("lego0");
   TExec *ex1 = new TExec("ex1","gStyle->SetPalette(kBird, 0, 0.5);");
   ex1->Draw();
   f1->Draw("surf2 same ");

   TExec *ex2 = new TExec("ex2","gStyle->SetPalette(75, 0, 0.5);");
   ex2->Draw();
   f2->Draw("surf2 same");
}

With your modifications, I am getting this.

with an error- “Error: Symbol kBird is not defined in current scope (tmpfile):1:”

I tried with kRed too. But, no good.

Why I don’t know.

This macro multi.C (766 Bytes) gives me this picture:

That’s exactly what I want, but, I am getting the previous figure I posted. I am using ROOT 5.34/36 version in Ubuntu.

I have included:
#include “TStyle.h”
#include “TColor.h”
#include “TF2.h”
#include “TExec.h”
#include “TCanvas.h”

Do I need to include some more?

You would need ROOT 6 …

ROOT 6 is available on Ubuntu … I assume you can easily get it.

Is there no other option?

Can I install root 6 beside root 5?

No, some functionalities are in ROOT 6 only

Yes. you can change from a version to an other by “sourcing” $ROOTSYS/bin/thisroot.sh