<commit_before>// @(#)root/hist:$Id: TGraph2D.cxx,v 1.00
// Author: <NAME>

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TMath.h"
#include "TH2.h"
#include "TF2.h"
#include "TList.h"
#include "TGraph2D.h"
#include "TGraphDelaunay.h"
#include "TVirtualPad.h"
#include "TVirtualFitter.h"
#include "TPluginManager.h"
#include "TClass.h"
#include "TSystem.h"
#include <stdlib.h>

#include "HFitInterface.h"
#include "Fit/DataRange.h"
#include "Math/MinimizerOptions.h"

ClassImp(TGraph2D)


//______________________________________________________________________________
/* Begin_Html
<center><h2>Graph 2D class</h2></center>
A Graph2D is a graphics object made of three arrays X, Y and Z with the same
number of points each.
<p>
This class has different constructors:
<ol>
<p><li> With an array's dimension and three arrays x, y, and z:
<pre>
   TGraph2D *g = new TGraph2D(n, x, y, z);
</pre>
    x, y, z arrays can be doubles, floats, or ints.

<p><li> With an array's dimension only:
<pre>
   TGraph2D *g = new TGraph2D(n);
</pre>
   The internal arrays are then filled with SetPoint. The following line
   fills the the internal arrays at the position "i" with the values x,y,z.
<pre>
   g->SetPoint(i, x, y, z);
</pre>
<p><li> Without parameters:
<pre>
   TGraph2D *g = new TGraph2D();
</pre>
   again SetPoint must be used to fill the internal arrays.
<p><li> From a file:
<pre>
   TGraph2D *g = new TGraph2D("graph.dat");
</pre>
   Arrays are read from the ASCII file "graph.dat" according to a specifies
   format. The format's default value is "%lg %lg %lg"
</ol>

Note that in any of these three cases, SetPoint can be used to change a data
point or add a new one. If the data point index (i) is greater than the
current size of the internal arrays, they are automatically extended.
<p>
Specific drawing options can be used to paint a TGraph2D:

<table border=0>

<tr><th valign=top>"TRI"</th><td>
The Delaunay triangles are drawn using filled area.
An hidden surface drawing technique is used. The surface is
painted with the current fill area color. The edges of each
triangles are painted with the current line color.
</td></tr>

<tr><th valign=top>"TRIW</th><td>
The Delaunay triangles are drawn as wire frame
</td></tr>

<tr><th valign=top>"TRI1</th><td>
The Delaunay triangles are painted with color levels. The edges
of each triangles are painted with the current line color.
</td></tr>

<tr><th valign=top>"TRI2</th><td>
the Delaunay triangles are painted with color levels.
</td></tr>

<tr><th valign=top>"P"  </th><td>
Draw a marker at each vertex
</td></tr>

<tr><th valign=top>"P0" </th><td>
Draw a circle at each vertex. Each circle background is white.
</td></tr>

<tr><th valign=top>"PCOL" </th><td>
Draw a marker at each vertex. The color of each marker is
defined according to its Z position.
</td></tr>

<tr><th valign=top>"CONT" </th><td>
Draw contours.
</td></tr>

<tr><th valign=top>"LINE" </th><td>
Draw a 3D polyline.
</td></tr>

</table>

A TGraph2D can be also drawn with ANY options valid to draw a 2D histogram.
<p>
When a TGraph2D is drawn with one of the 2D histogram drawing option,
a intermediate 2D histogram is filled using the Delaunay triangles
technique to interpolate the data set.
<p>
TGraph2D linearly interpolate a Z value for any (X,Y) point given some
existing (X,Y,Z) points. The existing (X,Y,Z) points can be randomly
scattered. The algorithm works by joining the existing points to make
Delaunay triangles in (X,Y). These are then used to define flat planes
in (X,Y,Z) over which to interpolate. The interpolated surface thus takes
the form of tessellating triangles at various angles. Output can take the
form of a 2D histogram or a vector. The triangles found can be drawn in 3D.
<p>
This software cannot be guaranteed to work under all circumstances. They
were originally written to work with a few hundred points in an XY space
with similar X and Y ranges.
<p>
Example:

End_Html
Begin_Macro(source)
{
   TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,400);
   Double_t x, y, z, P = 6.;
   Int_t np = 200;
   TGraph2D *dt = new TGraph2D();
   TRandom *r = new TRandom();
   for (Int_t N=0; N<np; N++) {
      x = 2*P*(r->Rndm(N))-P;
      y = 2*P*(r->Rndm(N))-P;
      z = (sin(x)/x)*(sin(y)/y)+0.2;
      dt->SetPoint(N,x,y,z);
   }
   gStyle->SetPalette(1);
   dt->Draw("surf1");
   return c;
}
End_Macro
Begin_Html

2D graphs can be fitted as shown by the following example:

End_Html
Begin_Macro(source)
../../../tutorials/fit/graph2dfit.C
End_Macro
Begin_Html

Example showing the PCOL option.

End_Html
Begin_Macro(source)
{
   TCanvas *c1 = new TCanvas("c1","Graph2D example",0,0,600,400);
   Double_t P = 5.;
   Int_t npx  = 20 ;
   Int_t npy  = 20 ;
   Double_t x = -P;
   Double_t y = -P;
   Double_t z;
   Int_t k = 0;
   Double_t dx = (2*P)/npx;
   Double_t dy = (2*P)/npy;
   TGraph2D *dt = new TGraph2D(npx*npy);
   dt->SetNpy(41);
   dt->SetNpx(40);
   for (Int_t i=0; i<npx; i++) {
      for (Int_t j=0; j<npy; j++) {
         z = sin(sqrt(x*x+y*y))+1;
         dt->SetPoint(k,x,y,z);
         k++;
         y = y+dx;
      }
      x = x+dx;
      y = -P;
   }
   gStyle->SetPalette(1);
   dt->SetMarkerStyle(20);
   dt->Draw("pcol");
   return c1;
}
End_Macro
Begin_Html

<h3>Definition of Delaunay triangulation (After B. Delaunay)</h3>
For a set S of points in the Euclidean plane, the unique triangulation DT(S)
of S such that no point in S is inside the circumcircle of any triangle in
DT(S). DT(S) is the dual of the Voronoi diagram of S.
If n is the number of points in S, the Voronoi diagram of S is the partitioning
of the plane containing S points into n convex polygons such that each polygon
contains exactly one point and every point in a given polygon is closer to its
central point than to any other. A Voronoi diagram is sometimes also known as
a Dirichlet tessellation.

<img src="gif/dtvd.gif">
<commit_msg>fix a typo<commit_after>// @(#)root/hist:$Id: TGraph2D.cxx,v 1.00
// Author: <NAME>

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TMath.h"
#include "TH2.h"
#include "TF2.h"
#include "TList.h"
#include "TGraph2D.h"
#include "TGraphDelaunay.h"
#include "TVirtualPad.h"
#include "TVirtualFitter.h"
#include "TPluginManager.h"
#include "TClass.h"
#include "TSystem.h"
#include <stdlib.h>

#include "HFitInterface.h"
#include "Fit/DataRange.h"
#include "Math/MinimizerOptions.h"

ClassImp(TGraph2D)


//______________________________________________________________________________
/* Begin_Html
<center><h2>Graph 2D class</h2></center>
A Graph2D is a graphics object made of three arrays X, Y and Z with the same
number of points each.
<p>
This class has different constructors:
<ol>
<p><li> With an array's dimension and three arrays x, y, and z:
<pre>
   TGraph2D *g = new TGraph2D(n, x, y, z);
</pre>
    x, y, z arrays can be doubles, floats, or ints.

<p><li> With an array's dimension only:
<pre>
   TGraph2D *g = new TGraph2D(n);
</pre>
   The internal arrays are then filled with SetPoint. The following line
   fills the the internal arrays at the position "i" with the values x,y,z.
<pre>
   g->SetPoint(i, x, y, z);
</pre>
<p><li> Without parameters:
<pre>
   TGraph2D *g = new TGraph2D();
</pre>
   again SetPoint must be used to fill the internal arrays.
<p><li> From a file:
<pre>
   TGraph2D *g = new TGraph2D("graph.dat");
</pre>
   Arrays are read from the ASCII file "graph.dat" according to a specifies
   format. The format's default value is "%lg %lg %lg"
</ol>

Note that in any of these three cases, SetPoint can be used to change a data
point or add a new one. If the data point index (i) is greater than the
current size of the internal arrays, they are automatically extended.
<p>
Specific drawing options can be used to paint a TGraph2D:

<table border=0>

<tr><th valign=top>"TRI"</th><td>
The Delaunay triangles are drawn using filled area.
An hidden surface drawing technique is used. The surface is
painted with the current fill area color. The edges of each
triangles are painted with the current line color.
</td></tr>

<tr><th valign=top>"TRIW</th><td>
The Delaunay triangles are drawn as wire frame
</td></tr>

<tr><th valign=top>"TRI1</th><td>
The Delaunay triangles are painted with color levels. The edges
of each triangles are painted with the current line color.
</td></tr>

<tr><th valign=top>"TRI2</th><td>
the Delaunay triangles are painted with color levels.
</td></tr>

<tr><th valign=top>"P"  </th><td>
Draw a marker at each vertex
</td></tr>

<tr><th valign=top>"P0" </th><td>
Draw a circle at each vertex. Each circle background is white.
</td></tr>

<tr><th valign=top>"PCOL" </th><td>
Draw a marker at each vertex. The color of each marker is
defined according to its Z position.
</td></tr>

<tr><th valign=top>"CONT" </th><td>
Draw contours.
</td></tr>

<tr><th valign=top>"LINE" </th><td>
Draw a 3D polyline.
</td></tr>

</table>

A TGraph2D can be also drawn with ANY options valid to draw a 2D histogram.
<p>
When a TGraph2D is drawn with one of the 2D histogram drawing option,
a intermediate 2D histogram is filled using the Delaunay triangles
technique to interpolate the data set.
<p>
TGraph2D linearly interpolate a Z value for any (X,Y) point given some
existing (X,Y,Z) points. The existing (X,Y,Z) points can be randomly
scattered. The algorithm works by joining the existing points to make
Delaunay triangles in (X,Y). These are then used to define flat planes
in (X,Y,Z) over which to interpolate. The interpolated surface thus takes
the form of tessellating triangles at various angles. Output can take the
form of a 2D histogram or a vector. The triangles found can be drawn in 3D.
<p>
This software cannot be guaranteed to work under all circumstances. They
were originally written to work with a few hundred points in an XY space
with similar X and Y ranges.
<p>
Example:

End_Html
Begin_Macro(source)
{
   TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,400);
   Double_t x, y, z, P = 6.;
   Int_t np = 200;
   TGraph2D *dt = new TGraph2D();
   TRandom *r = new TRandom();
   for (Int_t N=0; N<np; N++) {
      x = 2*P*(r->Rndm(N))-P;
      y = 2*P*(r->Rndm(N))-P;
      z = (sin(x)/x)*(sin(y)/y)+0.2;
      dt->SetPoint(N,x,y,z);
   }
   gStyle->SetPalette(1);
   dt->Draw("surf1");
   return c;
}
End_Macro
Begin_Html

2D graphs can be fitted as shown by the following example:

End_Html
Begin_Macro(source)
../../../tutorials/fit/graph2dfit.C
End_Macro
Begin_Html

Example showing the PCOL option.

End_Html
Begin_Macro(source)
{
   TCanvas *c1 = new TCanvas("c1","Graph2D example",0,0,600,400);
   Double_t P = 5.;
   Int_t npx  = 20 ;
   Int_t npy  = 20 ;
   Double_t x = -P;
   Double_t y = -P;
   Double_t z;
   Int_t k = 0;
   Double_t dx = (2*P)/npx;
   Double_t dy = (2*P)/npy;
   TGraph2D *dt = new TGraph2D(npx*npy);
   dt->SetNpy(41);
   dt->SetNpx(40);
   for (Int_t i=0; i<npx; i++) {
      for (Int_t j=0; j<npy; j++) {
         z = sin(sqrt(x*x+y*y))+1;
         dt->SetPoint(k,x,y,z);
         k++;
         y = y+dy;
      }
      x = x+dx;
      y = -P;
   }
   gStyle->SetPalette(1);
   dt->SetMarkerStyle(20);
   dt->Draw("pcol");
   return c1;
}
End_Macro
Begin_Html

<h3>Definition of Delaunay triangulation (After <NAME>)</h3>
For a set S of points in the Euclidean plane, the unique triangulation DT(S)
of S such that no point in S is inside the circumcircle of any triangle in
DT(S). DT(S) is the dual of the Voronoi diagram of S.
If n is the number of points in S, the Voronoi diagram of S is the partitioning
of the plane containing S points into n convex polygons such that each polygon
contains exactly one point and every point in a given polygon is closer to its
central point than to any other. A Voronoi diagram is sometimes also known as
a Dirichlet tessellation.

<img src="gif/dtvd.gif">

{'fraction_non_alphanumeric': 0.1401774595267746, 'fraction_numerical': 0.015566625155666251, 'mean_word_length': 3.4475597092419523, 'pattern_counts': {'":': 0, '<': 196, '<?xml version=': 0, '>': 214, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4481252', 'n_tokens_mistral': 4545, 'n_tokens_neox': 4326, 'n_words': 1657}