Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr005_apply.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview A macro to demonstrate the functionality of TGraph::Apply() method.
5/// TGraph::Apply applies a function `f` to all the data TGraph points, `f` may be a 1-D function TF1 or 2-d function TF2.
6/// The Y values of the graph are replaced by the new values computed using the function.
7///
8///
9/// The Apply() method can be used as well for TGraphErrors and TGraphAsymmErrors.
10///
11/// \macro_image
12/// \macro_code
13///
14/// \author Miro Helbich
15
16void gr005_apply() {
17
18 const Int_t npoints=3;
19 Double_t xaxis[npoints] = {1.,2.,3.};
20 Double_t yaxis[npoints] = {10.,20.,30.};
21
22 TGraph *gr1 = new TGraph(npoints,xaxis,yaxis);
23 TF2 *ff = new TF2("ff","-1./y"); //Defining the function `f`
24
25 TCanvas *c1 = new TCanvas("c1","c1", 0,0,700,500);
26 c1->Divide(2,1);
27
28 c1->cd(1);
29 gr1->DrawClone("A*"); //Using DrawClone to create a copy of the graph in the canvas.
30 c1->cd(2);
31 gr1->Apply(ff); //Applies the function `f` to all the data TGraph points
32 gr1->Draw("A*");
33 /* Without DrawClone, the modifications to gr1 via Apply(ff) are reflected in the original graph
34 displayed in c1 (the two drawn graphs are not independent). */
35}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
The Canvas class.
Definition TCanvas.h:23
Definition TF2.h:29
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
virtual void Apply(TF1 *f)
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
Definition TObject.cxx:319
return c1
Definition legend1.C:41