Change TLegend box dimension

Hello,
I’m drawing several TCanvas with few histograms in a loop which (depending on a variable) draw each histogram in a specific canvas. I don’t know (until the end of the loop) how many histograms goes in each canvas so I don’t know what would be the correct size for the TLegend box. Is there any command to change (after the creation of the TLegend) the legend box?
Code example:

TCanvas *cMdistMass[5];
TLegend *legMass[5];
for(int n=0;n<5;n++) {
  legMass[n] = new TLegend(0.50,0.70,0.88,0.84);
  cMdistMass[n] = new TCanvas(Form("cMdistMass_%d",n),"",0,0,800,800);
}
for(int n=0;n<20;n++) {
  ...
  indxMass = "this is an integer between 0 and 4"
  cMdistMass[indxMass]->cd();
  aHisto->Draw("same");
  legMass[indxMass]->AddEntry(aHisto,"...","LEP");
  ...
}

So in each of the 5 canvas I get 3,4,5, or 6 histograms and the legend should have different dimensions… I could easily solve the problem running twice the loop but I was wondering if I could change the dimension (i.e. the value of the 4 numbers used in the legend ctor) at the end of the loop.
Thanks

Attilio

You can change the X1 coordinate with SetX1(), and similar for X2, Y1, Y2; the TLegend box corners are (X1,Y1) and (X2,Y2), e.g.

legMass[indxMass]->SetX1(0.1);
legMass[indxMass]->SetX2(0.48);
// etc.

(if the above don’t seem to work, try the NDC versions (SetX1NDC() etc).
https://root.cern/doc/master/classTPave.html#a67869ab68e38688d08d5ac4da022b13c

Don’t call the c-tor of TLegend before you know how many histograms will be drawn; count the number of such histograms. Once you know the number of histograms, call the c-tor and parameterize the Y1 of your TLegend according to the number of drawn histograms, e.g.

legMass[n] = new TLegend(0.50,0.84-NDrawnHistograms*0.02,0.88,0.84);

Thanks, I’m sure I can do as yus suggested (meaning I have to loop twice which is something I was trying to avoid) but I’ll try also as dastudillo suggested. Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.