TMultiGraph

Dear ROOT experts,

I’m trying to plot in two differents pads :

  1. one first graph
  2. the same with a second
    using TMultiGraph, in the following script :

#include <stdio.h>
#include <math.h>
#define N 100
Int_t main(void)
{
Int_t i;
Double_t x[N],y[N],pi=acos(-1.);
TCanvas *c1=new TCanvas(“c1”,“c1”,10,10,1200,900);
c1->Divide(2);
TMultiGraph *mgh=new TMultiGraph();
//… Plot of graph #1
for(i=0; i<N; i++){x[i]=(Double_t)i/(N-1)*2.*pi; y[i]=sin(x[i]);}
TGraph *gr=new TGraph(N,x,y);
mgh->Add(gr);
c1->cd(1); mgh->Draw(“AL”);
c1->Update();
//… Plot of graph #1 and graph #2
for(i=0; i<N; i++){x[i]=(Double_t)i/(N-1)*2.*pi; y[i]=cos(x[i]);}
TGraph *gr=new TGraph(N,x,y);
mgh->Add(gr);
c1->cd(2); mgh->Draw(“AL”);
}

The pad cd(1), and not only cd(2), contain the two graphs. Why ? (ROOT 3.05/05)

                                                     Thank You

Dear Naulin,

I am not as much an expert (actually, I am here to get an easy question answered myself :slight_smile: ) but as far as I understand ROOT does a modification of a object already created and drawn on the screen imply, that the existing picture of the object itself is modified - upon the next Update().

So when you are adding a second graph to the existing multigraph and draw the same multigraph again, it will show identical graphs in both pads.

Your intension, however, was to show only the first plot in the first pad (different pictures). To do so, I would copy the multipad with the Clone() method before adding the second Graph.

#include <stdio.h>
#include <math.h>
#define N 100
Int_t multigraph(void)
{
Int_t i;
Double_t x[N],y[N],pi=acos(-1.);
TCanvas *c1=new TCanvas(“c1”,“c1”,10,10,1200,900);
c1->Divide(2);
TMultiGraph *mgh=new TMultiGraph();
//… Plot of graph #1
for(i=0; i<N; i++){
x[i]=(Double_t)i/(N-1)*2.*pi;
y[i]=sin(x[i]);
}
TGraph *gr=new TGraph(N,x,y);
mgh->Add(gr);
c1->cd(1); mgh->Draw(“APL”);
c1->Update();
[color=darkred] TMultiGraph *mgh2= (TMultiGraph *) mgh->Clone(); [/color]
//… Plot of graph #1 and graph #2
for(i=0; i<N; i++){
x[i]=(Double_t)i/(N-1)*2.*pi;
y[i]=cos(x[i]);
}
TGraph *gr=new TGraph(N,x,y);
[color=darkred]mgh2[/color]->Add(gr);
c1->cd(2); [color=darkred]mgh2[/color]->Draw(“APL”);
}

You could try to di it in two ways:

  1. Use DrawClone() instead od Draw() - it internaly create a copy of the object and draw it.
  2. Draw exatrly what are you want to draw - first gr->Draw() (it should draw single graph) then mgr->Draw() (for the whole multigraph)