Multigraph legends

Hello to everyone!

I am trying to create a legend on a multigraph, however it’s not working.
I tried to create with two ways: firstly with TMultigraph and later with TGraph.
The code I am using in both ways is:

Multigraph

# include "TCanvas.h"
# include "TROOT.h"
# include "TGraphErrors.h"
# include "TF1.h"
# include "TLegend.h"
# include "TArrow.h"
# include "TLatex.h"

void DriftConst(){
TGraph *gr1=new TGraph("DriftConst700V.txt");
TGraph *gr2=new TGraph("DriftConst750V.txt");
TGraph *gr3=new TGraph("DriftConst850V.txt");
TMultiGraph *mg=new TMultiGraph();
mg->Add(gr1);
mg->Add(gr2);
mg->Add(gr3);

mg.SetTitle("Constant Drift Voltage;Vmesh[V];Gain");

gROOT->SetStyle("Plain");
gr1.SetMarkerStyle(kFullCircle);
gr2.SetMarkerStyle(kOpenSquare);
gr3.SetMarkerStyle(kFullTriangleUp);
gr1.SetMarkerColor(kBlue);
gr1.SetLineColor(kBlue);
gr2.SetMarkerColor(kRed);
gr2.SetLineColor(kRed);
gr3.SetMarkerColor(kGreen);
gr3.SetLineColor(kGreen);

TLegend leg(.1,.7,.3,.9);
leg.SetFillColor(0);
//mg.SetFillColor(0);<------------compiler doesn't like this particular command
leg.AddEntry(&gr1,"Vdrift=700V");
leg.AddEntry(&gr2,"Vdrift=750V");
leg.AddEntry(&gr3,"Vdrift=850V");
leg.DrawClone("Same");

mg.DrawClone("APE");
}

The plots are there but no legend appear.

Second way

# include "TLegend.h"

void multi(){
TGraph *gr1=new TGraph("testtxt.txt");
TGraph *gr2=new TGraph("test1txt.txt");
TCanvas *c1=new TCanvas("c1", "Two Graphs", 200,10,600,400);
gr1->SetLineColor(4);
gr1->Draw("AC*");

gr2->SetLineWidth(3);
gr2->SetMarkerStyle(21);
gr2->SetLineColor(2);
gr2->Draw("CP");

TLegend leg(.1,.7,.3,.9,"Test Multi");
leg.SetFillColor(0);
//graph.SetFillColor(0);
leg.AddEntry(&gr1,"Exp. Points");
leg.AddEntry(&gr2,"Th. Law");
leg.DrawClone("Same");
}

Any ideas on what may be wrong?

Thank you in advance!

Try something like this:

TLegend *leg = new TLegend(0.1, 0.7, 0.3, 0.9);
leg->SetFillColor(0);
leg->SetHeader("test legend");
leg->AddEntry(gr1, "graph 1", "lp");
leg->AddEntry(gr2, "graph 2", "lp");
leg->AddEntry(gr3, "graph 3", "lp");
leg->Draw();

Thank you very much for you time!

Using your code, produces the same error.

*** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f95355e027e in waitpid () from /lib/libc.so.6
#1  0x00007f95355777e9 in ?? () from /lib/libc.so.6
#2  0x00007f95372b7024 in TUnixSystem::StackTrace() ()
   from /home/thanos/root/lib/libCore.so
#3  0x00007f95372b5de3 in TUnixSystem::DispatchSignals(ESignals) ()
   from /home/thanos/root/lib/libCore.so
#4  <signal handler called>
#5  0x00007f953559e2f2 in fgetpos64 () from /lib/libc.so.6
#6  0x00007f9536791661 in G__interpret_func ()
   from /home/thanos/root/lib/libCint.so
#7  0x00007f953677fa5c in G__getfunction ()
   from /home/thanos/root/lib/libCint.so
#8  0x00007f953675d1fe in G__getitem () from /home/thanos/root/lib/libCint.so
#9  0x00007f95367622f6 in G__getexpr () from /home/thanos/root/lib/libCint.so
#10 0x00007f953676b2b8 in G__calc_internal ()
   from /home/thanos/root/lib/libCint.so
#11 0x00007f95367ede0d in G__process_cmd ()
   from /home/thanos/root/lib/libCint.so
#12 0x00007f9537271f06 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/thanos/root/lib/libCore.so
#13 0x00007f953726f823 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /home/thanos/root/lib/libCore.so
#14 0x00007f95371c6906 in TApplication::ExecuteFile(char const*, int*, bool) ()
   from /home/thanos/root/lib/libCore.so
#15 0x00007f95371c44c3 in TApplication::ProcessLine(char const*, bool, int*) ()
   from /home/thanos/root/lib/libCore.so
#16 0x00007f953649a4ab in TRint::HandleTermInput() ()
   from /home/thanos/root/lib/libRint.so
#17 0x00007f95372b427e in TUnixSystem::CheckDescriptors() ()
   from /home/thanos/root/lib/libCore.so
#18 0x00007f95372b4573 in TUnixSystem::DispatchOneEvent(bool) ()
   from /home/thanos/root/lib/libCore.so
#19 0x00007f953722ab26 in TSystem::InnerLoop() ()
   from /home/thanos/root/lib/libCore.so
#20 0x00007f953722ce0b in TSystem::Run() ()
   from /home/thanos/root/lib/libCore.so
#21 0x00007f95371c216f in TApplication::Run(bool) ()
   from /home/thanos/root/lib/libCore.so
#22 0x00007f953649c3c5 in TRint::Run(bool) ()
   from /home/thanos/root/lib/libRint.so
#23 0x00000000004011ac in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007f953559e2f2 in fgetpos64 () from /lib/libc.so.6
===========================================================


Root > Function multi() busy flag cleared

I erased the Legend and the problem is still there(using the second way)…

The weird thing is that the code comes from the users guide!

Note that there is a difference between “gr1” and “&gr1”. Try:

root [0] TGraph *gr1=new TGraph("testtxt.txt");
root [1] std::cout << "gr=" << gr1 << " &gr1=" << &gr1 << std::endl;

Note also that there is a difference between the member access operator “.” and “->” (CINT often disregards it, call it a feature or a bug, whatever you like more).
Finally, in your first example, first draw the multigraph then the legend.

note that

C1->BuildLegend();

(where C1 is the canvas) scan the MultiGraphs automatically to build the legend.

I tried to draw the multigraph first, nothing else happens, though.
I get the multiplot but not the legend.
The error that occurs is

*** glibc detected *** /home/thanos/root/bin/root.exe: malloc(): memory corruption: 0x00007fec68353010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x77806)[0x7fec65cfa806]
/lib/libc.so.6(+0x7b928)[0x7fec65cfe928]
/lib/libc.so.6(__libc_malloc+0x6e)[0x7fec65cff7de]
/usr/lib/libstdc++.so.6(_Znwm+0x1d)[0x7fec6678824d]
/home/thanos/root/lib/libCint.so(_ZNSt11_Deque_baseIiSaIiEE17_M_initialize_mapEm+0x38)[0x7fec66e68708]
/home/thanos/root/lib/libCint.so(G__exec_statement+0x1c8)[0x7fec66f27708]
/home/thanos/root/lib/libCint.so(G__interpret_func+0x2c17)[0x7fec66edea47]
/home/thanos/root/lib/libCint.so(G__getfunction+0x197c)[0x7fec66ecca5c]
/home/thanos/root/lib/libCint.so(G__getitem+0x86e)[0x7fec66eaa1fe]
/home/thanos/root/lib/libCint.so(G__getexpr+0x45a6)[0x7fec66eaf2f6]
/home/thanos/root/lib/libCint.so(G__calc_internal+0x3e8)[0x7fec66eb82b8]
/home/thanos/root/lib/libCint.so(G__process_cmd+0x316d)[0x7fec66f3ae0d]
/home/thanos/root/lib/libCore.so(_ZN5TCint11ProcessLineEPKcPN12TInterpreter10EErrorCodeE+0x536)[0x7fec679bef06]
/home/thanos/root/lib/libCore.so(_ZN5TCint16ProcessLineSynchEPKcPN12TInterpreter10EErrorCodeE+0xf3)[0x7fec679bc823]
/home/thanos/root/lib/libCore.so(_ZN12TApplication11ExecuteFileEPKcPib+0x826)[0x7fec67913906]
/home/thanos/root/lib/libCore.so(_ZN12TApplication11ProcessLineEPKcbPi+0x823)[0x7fec679114c3]
/home/thanos/root/lib/libRint.so(_ZN5TRint15HandleTermInputEv+0x22b)[0x7fec66be74ab]
/home/thanos/root/lib/libCore.so(_ZN11TUnixSystem16CheckDescriptorsEv+0x14e)[0x7fec67a0127e]
/home/thanos/root/lib/libCore.so(_ZN11TUnixSystem16DispatchOneEventEb+0xd3)[0x7fec67a01573]
/home/thanos/root/lib/libCore.so(_ZN7TSystem9InnerLoopEv+0x16)[0x7fec67977b26]
/home/thanos/root/lib/libCore.so(_ZN7TSystem3RunEv+0x7b)[0x7fec67979e0b]
/home/thanos/root/lib/libCore.so(_ZN12TApplication3RunEb+0x1f)[0x7fec6790f16f]
/home/thanos/root/lib/libRint.so(_ZN5TRint3RunEb+0x2a5)[0x7fec66be93c5]
/home/thanos/root/bin/root.exe(main+0x4c)[0x4011ac]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7fec65ca1c4d]
/home/thanos/root/bin/root.exe[0x401079]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:07 527613                             /home/thanos/root/bin/root.exe
00601000-00602000 r--p 00001000 08:07 527613                             /home/thanos/root/bin/root.exe
00602000-00603000 rw-p 00002000 08:07 527613                             /home/thanos/root/bin/root.exe
01d12000-0292c000 rw-p 00000000 00:00 0                                  [heap]
7fec58000000-7fec58021000 rw-p 00000000 00:00 0 
7fec58021000-7fec5c000000 ---p 00000000 00:00 0 
7fec5f9f0000-7fec5fa86000 r-xp 00000000 08:07 527447                     /home/thanos/root/lib/libHistPainter.so
7fec5fa86000-7fec5fc86000 ---p 00096000 08:07 527447                     /home/thanos/root/lib/libHistPainter.so
7fec5fc86000-7fec5fc88000 r--p 00096000 08:07 527447                     /home/thanos/root/lib/libHistPainter.so
7fec5fc88000-7fec5fc8a000 rw-p 00098000 08:07 527447                     /home/thanos/root/lib/libHistPainter.so
7fec5fc8a000-7fec5fc8b000 rw-p 00000000 00:00 0 
7fec5fc8b000-7fec5fcb0000 r-xp 00000000 08:05 921742                     /lib/libpng12.so.0.42.0
7fec5fcb0000-7fec5feb0000 ---p 00025000 08:05 921742                     /lib/libpng12.so.0.42.0
7fec5feb0000-7fec5feb1000 r--p 00025000 08:05 921742                     /lib/libpng12.so.0.42.0
7fec5feb1000-7fec5feb2000 rw-p 00026000 08:05 921742                     /lib/libpng12.so.0.42.0
7fec5feb2000-7fec5ff12000 r-xp 00000000 08:05 1049498                    /usr/lib/libtiff.so.4.3.2
7fec5ff12000-7fec60111000 ---p 00060000 08:05 1049498                    /usr/lib/libtiff.so.4.3.2
7fec60111000-7fec60113000 r--p 0005f000 08:05 1049498                    /usr/lib/libtiff.so.4.3.2
7fec60113000-7fec60114000 rw-p 00061000 08:05 1049498                    /usr/lib/libtiff.so.4.3.2
7fec60114000-7fec60137000 r-xp 00000000 08:05 1051064                    /usr/lib/libjpeg.so.62.0.0
7fec60137000-7fec60336000 ---p 00023000 08:05 1051064                    /usr/lib/libjpeg.so.62.0.0
7fec60336000-7fec60337000 r--p 00022000 08:05 1051064                    /usr/lib/libjpeg.so.62.0.0
7fec60337000-7fec60338000 rw-p 00023000 08:05 1051064                    /usr/lib/libjpeg.so.62.0.0
7fec60338000-7fec603e1000 r-xp 00000000 08:07 527502                     /home/thanos/root/lib/libASImage.so
7fec603e1000-7fec605e0000 ---p 000a9000 08:07 527502                     /home/thanos/root/lib/libASImage.so
7fec605e0000-7fec605e3000 r--p 000a8000 08:07 527502                     /home/thanos/root/lib/libASImage.so
7fec605e3000-7fec605e6000 rw-p 000ab000 08:07 527502                     /home/thanos/root/lib/libASImage.so
7fec605e6000-7fec605f4000 rw-p 00000000 00:00 0 
7fec605f4000-7fec605f9000 r-xp 00000000 08:05 1050470                    /usr/lib/libXfixes.so.3.1.0
7fec605f9000-7fec607f8000 ---p 00005000 08:05 1050470                    /usr/lib/libXfixes.so.3.1.0
7fec607f8000-7fec607f9000 r--p 00004000 08:05 1050470                    /usr/lib/libXfixes.so.3.1.0
7fec607f9000-7fec607fa000 rw-p 00005000 08:05 1050470                    /usr/lib/libXfixes.so.3.1.0
7fec607fa000-7fec60803000 r-xp 00000000 08:05 1050462                    /usr/lib/libXcursor.so.1.0.2
7fec60803000-7fec60a02000 ---p 00009000 08:05 1050462                    /usr/lib/libXcursor.so.1.0.2
7fec60a02000-7fec60a03000 r--p 00008000 08:05 1050462                    /usr/lib/libXcursor.so.1.0.2
7fec60a03000-7fec60a04000 rw-p 00009000 08:05 1050462                    /usr/lib/libXcursor.so.1.0.2
7fec60a04000-7fec60a2a000 r-xp 00000000 08:05 917578                     /lib/libexpat.so.1.5.2
7fec60a2a000-7fec60c2a000 ---p 00026000 08:05 917578                     /lib/libexpat.so.1.5.2
7fec60c2a000-7fec60c2c000 r--p 00026000 08:05 917578                     /lib/libexpat.so.1.5.2
7fec60c2c000-7fec60c2d000 rw-p 00028000 08:05 917578                     /lib/libexpat.so.1.5.2
7fec60c2d000-7fec60c36000 r-xp 00000000 08:05 1050490                    /usr/lib/libXrender.so.1.3.0
7fec60c36000-7fec60e35000 ---p 00009000 08:05 1050490                    /usr/lib/libXrender.so.1.3.0
7fec60e35000-7fec60e36000 r--p 00008000 08:05 1050490                    /usr/lib/libXrender.so.1.3.0
7fec60e36000-7fec60e37000 rw-p 00009000 08:05 1050490                    /usr/lib/libXrender.so.1.3.0
7fec60e37000-7fec60e6a000 r-xp 00000000 08:05 1050747                    /usr/lib/libfontconfig.so.1.4.4
7fec60e6a000-7fec6106a000 ---p 00033000 08:05 1050747                    /usr/lib/libfontconfig.so.1.4.4
7fec6106a000-7fec6106b000 r--p 00033000 08:05 1050747                    /usr/lib/libfontconfig.so.1.4.4thanos@thanos-laptop:~/Desktop/testRoot/DriftConst$

I used the commands

root [0] TGraph *gr1=new TGraph("testtxt.txt");
root [1] std::cout << "gr=" << gr1 << " &gr1=" << &gr1 << std::endl;

and I get

gr=0x14a2f40 &gr1=0x1468d70

I also used

C1->BuildLegend();

and again no legend and no multigraph as well… :confused:

void multigraphleg()
{
   TCanvas *c = new TCanvas("c","c",600, 400);

   TMultiGraph * mg = new TMultiGraph("mg","mg");

   const Int_t size = 10;
	        
   double x[size];
   double y1[size];
   double y2[size];
   double y3[size];

   for ( int i = 0; i <  size ; ++i ) {
      x[i] = i;
      y1[i] = size - i;
      y2[i] = size - 0.5 * i;
      y3[i] = size - 0.6 * i;
   }

   TGraph * gr1 = new TGraph( size, x, y1 );
   gr1->SetName("gr1");
   gr1->SetTitle("graph 1");
   gr1->SetMarkerStyle(21);
   gr1->SetDrawOption("AP");
   gr1->SetLineColor(2);
   gr1->SetLineWidth(4);
   gr1->SetFillStyle(0);

   TGraph * gr2 = new TGraph( size, x, y2 );
   gr2->SetName("gr2");
   gr2->SetTitle("graph 2");
   gr2->SetMarkerStyle(22);
   gr2->SetMarkerColor(2);
   gr2->SetDrawOption("P");
   gr2->SetLineColor(3);
   gr2->SetLineWidth(4);
   gr2->SetFillStyle(0);

   TGraph * gr3 = new TGraph( size, x, y3 );
   gr3->SetName("gr3");
   gr3->SetTitle("graph 3");
   gr3->SetMarkerStyle(23);
   gr3->SetLineColor(4);
   gr3->SetLineWidth(4);
   gr3->SetFillStyle(0);

   mg->Add( gr1 );
   mg->Add( gr2 );

   gr3->Draw("ALP");
   mg->Draw("LP");
   c->BuildLegend();

   c->Print("multigraphleg.gif");
}
1 Like

It’s working!!!

I used your code with my .txt files and it works just like a charm!!!

Thank you very much!! :smiley: