Problem in fitting (in function!)

Dear @couet,

(1) I have seen only surf4 option works if somebody wants not to have net like surface. But, still what I am seeing that those fit surfaces can’t be made transparent.

(2) If there are more than one fits then without transparent 2D surfaces, it will not be understandable.

(3) For simple testing, that’s why I request you to check the upper points in an already set example (mentioned above-https://root.cern/doc/v612/classTHistPainter.html)

@Sandy @Wile_E_Coyote thank for the recap.
Can you post the latest instance of the macro you ended up ?
That will avoid me to take pieces here and there …

For me,

if you try those in the attached one which was done by you people only, that will do.

My query again:

To show those three fit surfaces (should be transparent) separately.

example.C (1.3 KB)

something like that ?

Yes, more than that…

  1. If the net like structure goes away, that would be better.

  2. there are three peaks and if those three are fitted separately
    then to show those three fit surfaces with transparent monocolour.

  3. And if possible the histogram with pol and E2 option.

The net is the fit and the lego the histogram … you have only one surface in you example … you want 3 ?

Ok … not sure I can do better than that with the example you sent me:

  1. Can I see your modifications?

  2. Is it possible to change the colour of the surface net from red to blue?

#include "TF2.h"
#include "TH2.h"
#include "TCutG.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() {

   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);
   //Fit h2 with original function f2
   Float_t ratio = 4*nentries/100000;
   f2params[ 0] *= ratio;
   f2params[ 5] *= ratio;
   f2params[10] *= ratio;
   f2->SetParameters(f2params);
   h2->Fit("f2","N");
   f2->SetNpx(80);
   f2->SetNpy(80);
   h2->SetMaximum(800);
   h2->Draw("E fb");
   f2->SetMaximum(800);
   f2->SetLineColorAlpha(kBlue,0.1);
   f2->Draw("surf  fb same");
}

1 Like

That’s great. Thanks @couet.

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");