Superimposing N Graphs

Hi. I searched among the tutorial root for the superimposing of graphs and i found how to superimpose two graphs ftp://root.cern.ch/root/doc/ROOTUsersGuideHTML/ch04s02.html. Now i’m creating a script that
would superimpose a variable number of graphs . is there a way to declare only one graph and use it for draw all the data but changing markers and color of the line?
thank you

TMultiGraph

thank you. I’ m working with multigraph and i’m trying to set a title to x and y axes

ADPG->GetXaxis()->SetTitle("Thete (Grad) ");
ADPG->GetYaxis()->SetTitle("Sezione d'urto (mb/sr) ");

but the code produces a segmentation violation . If I delet this two lines it works.

what is ADPG ?

see 4th example here:
root.cern.ch/root/htmldoc/TMultiGraph.html

ADPG is a multigraph object. I used precisely those examples. I’m also trying to build a legend but when i open the root file in TBrowser i see only the data points .

[code]#include<TGraph.h>
#include<TMultiGraph.h>
#include<TCanvas.h>
#include
#include
#include
#include
#include <TFile.h>
#include <TAxis.h>
#include <math.h>
#include <TPad.h>
#include
#include <TAxis.h>
#include <TNamed.h>
#include <TLegend.h>

using namespace std;

int main (){
int in,in1,in2,i,L,E,A,N;int d,f,h;string g;double sezione;double f1,f2,f3,f4;string name1,name2,name3,name4,name;
vector IMPO;
vector molt;
fstream ADP,ADN,ADALPHA,sez;
cout<<" Quante combinazioni vuoi graficare? "<< endl;
cin >> N;

TFile *GRAFICI = new TFile(“GRAFICI.root”,“RECREATE”);

////////// 4 graph a testa
TGraph *g1P = new TGraph();

g1P->SetLineWidth(3);
g1P->SetMarkerStyle(21);
g1P->SetLineColor(2);
TGraph *g2P = new TGraph();

g2P->SetLineWidth(3);
g2P->SetMarkerStyle(21);
g2P->SetLineColor(3);
TGraph *g3P = new TGraph();

g3P->SetLineWidth(3);
g3P->SetMarkerStyle(21);
g3P->SetLineColor(4);
TGraph *g4P = new TGraph();

g4P->SetLineWidth(3);
g4P->SetMarkerStyle(21);
g4P->SetLineColor(4);
////////// NEUTRONI
TGraph *g1N = new TGraph();
g1N->SetLineWidth(3);
g1N->SetMarkerStyle(21);
g1N->SetLineColor(2);
TGraph *g2N = new TGraph();

g2N->SetLineWidth(3);
g2N->SetMarkerStyle(21);
g2N->SetLineColor(3);
TGraph *g3N = new TGraph();

g3N->SetLineWidth(3);
g3N->SetMarkerStyle(21);
g3N->SetLineColor(4);
TGraph *g4N = new TGraph();

g4N->SetLineWidth(3);
g4N->SetMarkerStyle(21);
g4N->SetLineColor(4);
////////// ALPHA
TGraph *g1ALPHA = new TGraph();
g1ALPHA->SetLineWidth(3);
g1ALPHA->SetMarkerStyle(21);
g1ALPHA->SetLineColor(2);
TGraph *g2ALPHA = new TGraph();

g2ALPHA->SetLineWidth(3);
g2ALPHA->SetMarkerStyle(21);
g2ALPHA->SetLineColor(3);
TGraph *g3ALPHA = new TGraph();

g3ALPHA->SetLineWidth(3);
g3ALPHA->SetMarkerStyle(21);
g3ALPHA->SetLineColor(4);
TGraph *g4ALPHA = new TGraph();

g4ALPHA->SetLineWidth(3);
g4ALPHA->SetMarkerStyle(21);
g4ALPHA->SetLineColor(4);

/////////////////////
TMultiGraph *ADPG = new TMultiGraph();
TMultiGraph *ADNG= new TMultiGraph();
TMultiGraph *ADALPHAG = new TMultiGraph();

for(i=0;i<N;i++){
cout<<" Inserire Energia,Momento,A del grafico n° " << i+1 << endl;
cin>> E >> L >> A ;
cout<<" Inserire nome del grafico"<<endl;
cin>> name ; if(i==0){name1=name;}
if(i==1){name2=name;}
if (i==2){name3=name;}
if (i==3){name4=name;}TCanvas *c = new TCanvas(“c”,“c”,600, 400);

ostringstream oss1,oss2,oss3;
oss1 << E;oss2<<L;oss3<<A;

//g1P->SetTitle(name1);
//g2P->SetTitle(name2);
//g3P->SetTitle(name3);
//g4P->SetTitle(name4);

string E1 = oss1.str();string L1 = oss2.str();string A1 = oss3.str();
cout<< E1 << A1 << L1<< endl;
cout<<" controllo1 " <<endl;
ADP.open(A1 +""+ E1 +"" + L1 +"/ADP");
ADN.open(A1 +""+ E1 +"" + L1 +"/ADN");
ADALPHA.open(A1 +""+ E1 +"" + L1 +"/ADALPHA");
// in=0;in1=0;in2=0;
while (!ADP.eof()){
ADP>>f1>>f2>>f3>>f4;
cout<<f1<<f4<<endl;
if(i==0) {g1P-> SetPoint(g1P->GetN(),f1,f4);}
if(i==1) {g2P-> SetPoint(g2P->GetN(),f1,f4);}
if(i==2) {g3P-> SetPoint(g3P->GetN(),f1,f4);}
if(i==3) {g4P-> SetPoint(g4P->GetN(),f1,f4);}
in++;
}
while (!ADN.eof()){
ADN>>f1>>f2>>f3>>f4;
cout<<f1<<f4<<endl;
if(i==0) {g1N-> SetPoint(g1N->GetN(),f1,f4);}
if(i==1) {g2N-> SetPoint(g2N->GetN(),f1,f4);}
if(i==2) {g3N-> SetPoint(g3N->GetN(),f1,f4);}
if(i==3) {g4N-> SetPoint(g4N->GetN(),f1,f4);}
in1++;
}
while (!ADALPHA.eof()){
ADALPHA>>f1>>f2>>f3>>f4;
cout<<f1<<f4<<endl;
if(i==0) {g1ALPHA-> SetPoint(g1ALPHA->GetN(),f1,f4);}
if(i==1) {g2ALPHA-> SetPoint(g2ALPHA->GetN(),f1,f4);}
if(i==2) {g3ALPHA-> SetPoint(g3ALPHA->GetN(),f1,f4);}
if(i==3) {g4ALPHA-> SetPoint(g4ALPHA->GetN(),f1,f4);}
in2++;
}
cout<<“controllo 3”<<endl;
ADP.close();
ADN.close();
ADALPHA.close();

if(i==0) {
ADPG->Add(g1P);
ADNG->Add(g1N);
ADALPHAG->Add(g1ALPHA);
}

cout<<“controllo 4”<<endl;
if(i==1) {ADPG->Add(g2P);ADNG->Add(g2N);ADALPHAG->Add(g2ALPHA);}
if(i==2) {ADPG->Add(g3P);ADNG->Add(g3N);ADALPHAG->Add(g3ALPHA);}
if(i==3) {ADPG->Add(g4P);ADNG->Add(g4N);ADALPHAG->Add(g4ALPHA);}

}
cout<<“controllo 2” <<endl;
ADNG->Draw(“AP”);
ADNG->Write(0,0,0);
GRAFICI->Close();

ADPG->Draw(“AP”);ADALPHAG->Draw(“AP”);
//ADPG->GetXaxis()->SetTitle("Thete (Grad) ");
//ADPG->GetYaxis()->SetTitle("Sezione d’urto (mb/sr) ");

ADNG->Write(0,0,0);ADALPHAG->Write(0,0,0);

} //chiusura for
[/code]

what should I answer to the question:

it asks the number of graph to put together in the multigraph

translate.google.com/

I get:

 Quante combinazioni vuoi graficare? 
3
 Inserire Energia,Momento,A del grafico n° 1
Error: operator> not defined for basic_istream<char,char_traits<char> > mg.C:103:
*** Interpreter error recovered ***

Ah, … Better with Aclic… but still I do not understand the questio:

root [0] .x mg.C++
Info in <TMacOSXSystem::ACLiC>: creating shared library /Users/couet/Downloads/./mg_C.so
 Quante combinazioni vuoi graficare? 
2
 Inserire Energia,Momento,A del grafico n° 1

Please give me what I should enter.

I’m afraid we cannot run this code because it needs 3 “data files”.

Try to replace:
TMultiGraph *ADPG = new TMultiGraph();
with:
TMultiGraph *ADPG = new TMultiGraph(“ADPG”, ";Thete (Grad) ;Sezione d’urto (mb/sr) ");
or:
TMultiGraph *ADPG = new TMultiGraph(“ADPG”, "Something Unknown;Thete (Grad) ;Sezione d’urto (mb/sr) ");

Also, change:
ADP.open(A1 +""+ E1 +"" + L1 +"/ADP");
ADN.open(A1 +""+ E1 +"" + L1 +"/ADN");
ADALPHA.open(A1 +""+ E1 +"" + L1 +"/ADALPHA");
into:
ADP.open((A1 +""+ E1 +"" + L1 +"/ADP").c_str());
ADN.open((A1 +""+ E1 +"" + L1 +"/ADN").c_str());
ADALPHA.open((A1 +""+ E1 +"" + L1 +"/ADALPHA").c_str());

May be you can make a small standalone prototype reproducing your problem.

using this TMultiGraph *ADPG = new TMultiGraph(“ADPG”, ";Thete (Grad) ;Sezione d’urto (mb/sr) "); it works ( i’ve only to set in a better way the title on the y axis) .
I’m trying now to set the legend using the example of root but when i open the root file in TBrowser it does not appear.

Again an example would be useful.

[code]ADNG->Draw(“AP”);
TLegend *leg = new TLegend(0.1,0.7,0.48,0.9);
leg->SetHeader(“The Legend Title”);
leg->AddEntry(“f1”,“Function abs(#frac{sin(x)}{x})”,“l”);
leg->AddEntry(“gr”,“Graph with error bars”,“lep”);
leg->Draw();
ADNG->Write(0,0,0);

c1->Write();
GRAFICI->Write();
GRAFICI->Close();[/code]

ADNG is a multigraph object , c1 is a canvas and GRAFICI is a TFile ( the entrys in the legend object are to try the code)
In this way it creates two object in the root file. One is the multigraph without the legend and the other is the multigraph with the legend but without the color line between the data points

At first guess I think, the “color line” is the “f1” and it’s probably missing in the ROOT file.

Solved, i used “AP” option

ADNG->Draw(“ALP”); … or … ADNG->Draw(“ACP”);

thank you :smiley: