I am looking for a way to remove an entry from a TLegend via a script. How can I accomplish this? I’ve tried RecursiveRemove, but that doesn’t seem to do it (see MWE below). Note that DeleteEntry does not work for me because it requires mouse interaction, and I need to do this in batch mode.
int testroot()
{
TCanvas* can = new TCanvas("","",1000,1000);
TLegend* leg = new TLegend(0.3,0.3,0.7,0.7);
TH1F* hist = new TH1F("hist","",100,0,1);
hist->SetMarkerStyle(20);
hist->SetMarkerSize(2);
hist->SetMarkerColor(kRed);
leg->AddEntry(hist,"the histogram","p");
hist->Draw("P HIST");
leg->Draw();
can->SaveAs("beforeRemove.pdf");
leg->RecursiveRemove(hist);
can->Clear();
hist->Draw("P HIST");
leg->Draw();
can->SaveAs("afterRemove.pdf");
return 0;
}
I am trying to guess a bit the ultimate goal you are trying to accomplish, please bear with me if I write something in error.
Would deleting or letting go out of scope the histogram you do not need work for you?
int testroot()
{
TCanvas* can = new TCanvas("","",1000,1000);
TLegend* leg = new TLegend(0.3,0.3,0.7,0.7);
TH1F* hist = new TH1F("hist","",100,0,1);
hist->SetMarkerStyle(20);
hist->SetMarkerSize(2);
hist->SetMarkerColor(kRed);
TH1F* hist2 = new TH1F("hist2","",100,0,1);
hist2->SetMarkerStyle(30);
hist2->SetMarkerSize(3);
hist2->SetMarkerColor(kBlue);
leg->AddEntry(hist,"the histogram","p");
leg->AddEntry(hist2,"the histogram2","p");
hist->Draw("P HIST");
leg->Draw();
can->SaveAs("beforeRemove.pdf");
delete hist2;
can->Clear();
hist->Draw("P HIST");
leg->Draw();
can->SaveAs("afterRemove.pdf");
return 0;
}