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");
}
*** 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 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.