#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Riostream.h"
#include "TROOT.h"
#include "TPaveStats.h"
#include "TPaveLabel.h"
#include "TVirtualPad.h"
#include "TStyle.h"
#include "TClass.h"
#include "TLatex.h"
ClassImp(TPaveStats)
const UInt_t kTakeStyle = BIT(17);
TPaveStats::TPaveStats(): TPaveText()
{
fParent = 0;
fOptFit = gStyle->GetOptFit();
fOptStat = gStyle->GetOptStat();
}
TPaveStats::TPaveStats(Double_t x1, Double_t y1,Double_t x2, Double_t y2, Option_t *option)
:TPaveText(x1,y1,x2,y2,option)
{
fParent = 0;
fOptFit = gStyle->GetOptFit();
fOptStat = gStyle->GetOptStat();
SetFitFormat(gStyle->GetFitFormat());
SetStatFormat(gStyle->GetStatFormat());
}
TPaveStats::~TPaveStats()
{
if ( fParent && !fParent->TestBit(kInvalidObject)) fParent->RecursiveRemove(this);
}
Int_t TPaveStats::GetOptFit() const
{
if (TestBit(kTakeStyle)) return gStyle->GetOptFit();
return fOptFit;
}
Int_t TPaveStats::GetOptStat() const
{
if (TestBit(kTakeStyle)) return gStyle->GetOptStat();
return fOptStat;
}
void TPaveStats::SaveStyle()
{
gStyle->SetOptFit(fOptFit);
gStyle->SetOptStat(fOptStat);
gStyle->SetFitFormat(fFitFormat.Data());
gStyle->SetStatFormat(fStatFormat.Data());
}
void TPaveStats::SetFitFormat(const char *form)
{
fFitFormat = form;
}
void TPaveStats::SetOptFit(Int_t fit)
{
fOptFit = fit;
ResetBit(kTakeStyle);
}
void TPaveStats::SetOptStat(Int_t stat)
{
fOptStat = stat;
ResetBit(kTakeStyle);
}
void TPaveStats::SetStatFormat(const char *form)
{
fStatFormat = form;
}
void TPaveStats::Paint(Option_t *option)
{
TPave::ConvertNDCtoPad();
TPave::PaintPave(fX1,fY1,fX2,fY2,GetBorderSize(),option);
if (!fLines) return;
TString typolabel;
Double_t y2ref = TMath::Max(fY1,fY2);
Double_t x1ref = TMath::Min(fX1,fX2);
Double_t x2ref = TMath::Max(fX1,fX2);
Double_t dx = TMath::Abs(fX2 - fX1);
Double_t dy = TMath::Abs(fY2 - fY1);
Double_t titlesize=0;
Double_t textsize = GetTextSize();
Int_t nlines = GetSize();
if (nlines == 0) nlines = 5;
Int_t print_name = fOptStat%10;
Double_t y1 = gPad->GetY1();
Double_t y2 = gPad->GetY2();
Float_t margin = fMargin*dx;
Double_t yspace = dy/Double_t(nlines);
Double_t textsave = textsize;
TObject *line;
TLatex *latex, *latex_tok;
TIter next(fLines);
Double_t longest = 0, titlelength = 0;
Double_t w, wtok[2];
char *st, *sl=0;
if (textsize == 0) {
textsize = 0.92*yspace/(y2 - y1);
titlesize = textsize;
wtok[0] = 0; wtok[1] = 0;
while ((line = (TObject*) next())) {
if (line->IsA() == TLatex::Class()) {
latex = (TLatex*)line;
Int_t nchs = strlen(latex->GetTitle());
sl = new char[nchs+1];
strlcpy(sl, latex->GetTitle(),nchs+1);
if (strpbrk(sl, "=") !=0 && print_name == 0) {
st = strtok(sl, "=");
Int_t itok = 0;
while ( st !=0 ) {
latex_tok = new TLatex(0.,0.,st);
Style_t tfont = latex->GetTextFont();
if (tfont == 0) tfont = GetTextFont();
latex_tok->SetTextFont(tfont);
latex_tok->SetTextSize(textsize);
w = latex_tok->GetXsize();
if (w > wtok[itok]) wtok[itok] = w;
st = strtok(0, "=");
++itok;
delete latex_tok;
}
} else if (strpbrk(sl, "|") !=0) {
} else {
print_name = 0;
Style_t tfont = latex->GetTextFont();
if (tfont == 0) latex->SetTextFont(GetTextFont());
latex->SetTextSize(titlesize);
titlelength = latex->GetXsize()+2.*margin;
if (titlelength > 0.98*dx) titlesize *= 0.98*dx/titlelength;
latex->SetTextFont(tfont);
}
delete [] sl; sl = 0;
}
}
longest = wtok[0]+wtok[1]+2.*margin;
if (longest > 0.98*dx) textsize *= 0.98*dx/longest;
SetTextSize(textsize);
} else {
titlesize = textsize;
}
Double_t ytext = y2ref + 0.5*yspace;
Double_t xtext = 0;
print_name = fOptStat%10;
next.Reset();
while ((line = (TObject*) next())) {
if (line->IsA() == TLatex::Class()) {
latex = (TLatex*)line;
ytext -= yspace;
Double_t xl = latex->GetX();
Double_t yl = latex->GetY();
Short_t talign = latex->GetTextAlign();
Color_t tcolor = latex->GetTextColor();
Style_t tfont = latex->GetTextFont();
Size_t tsize = latex->GetTextSize();
if (tcolor == 0) latex->SetTextColor(GetTextColor());
if (tfont == 0) latex->SetTextFont(GetTextFont());
if (tsize == 0) latex->SetTextSize(GetTextSize());
Int_t nchs = strlen(latex->GetTitle());
sl = new char[nchs+1];
strlcpy(sl, latex->GetTitle(),nchs+1);
if (strpbrk(sl, "=") !=0 && print_name == 0) {
st = strtok(sl, "=");
Int_t halign = 12;
while ( st !=0 ) {
typolabel = st;
latex->SetTextAlign(halign);
if (halign == 12) xtext = x1ref + margin;
if (halign == 32) {
xtext = x2ref - margin;
typolabel = typolabel.Strip();
typolabel.ReplaceAll("-","#minus");
}
latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
latex->GetTextSize(),
typolabel.Data());
st = strtok(0, "=");
halign = 32;
}
} else if (strpbrk(sl, "|") !=0) {
Double_t yline1 = ytext+yspace/2.;
Double_t yline2 = ytext-yspace/2.;
Double_t xline1 = dx/3+x1ref;
Double_t xline2 = 2*dx/3+x1ref;
gPad->PaintLine(x1ref,yline1,x2ref,yline1);
gPad->PaintLine(xline1,yline1,xline1,yline2);
gPad->PaintLine(xline2,yline1,xline2,yline2);
st = strtok(sl, "|");
Int_t theIndex = 0;
while ( st !=0 ) {
latex->SetTextAlign(22);
if (theIndex == 0) xtext = 0.5*(x1ref+xline1);
if (theIndex == 1) xtext = 0.5*(x1ref+x2ref);
if (theIndex == 2) xtext = 0.5*(xline2+x2ref);
typolabel = st;
typolabel.ReplaceAll("-", "#minus");
latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
latex->GetTextSize(),
typolabel.Data());
theIndex++;
st = strtok(0, "|");
}
} else {
print_name = 0;
latex->SetTextAlign(22);
xtext = 0.5*(x1ref+x2ref);
latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
titlesize,
sl);
gPad->PaintLine(x1ref,y2ref-yspace,x2ref,y2ref-yspace);
}
delete [] sl;
latex->SetTextAlign(talign);
latex->SetTextColor(tcolor);
latex->SetTextFont(tfont);
latex->SetTextSize(tsize);
latex->SetX(xl);
latex->SetY(yl);
}
}
SetTextSize(textsave);
if (fLabel.Length() > 0) {
Double_t x1,x2;
dy = gPad->GetY2() - gPad->GetY1();
x1 = x1ref + 0.25*dx;
x2 = x2ref - 0.25*dx;
y1 = y2ref - 0.02*dy;
y2 = y2ref + 0.02*dy;
TPaveLabel *title = new TPaveLabel(x1,y1,x2,y2,fLabel.Data(),GetDrawOption());
title->SetFillColor(GetFillColor());
title->SetTextColor(GetTextColor());
title->SetTextFont(GetTextFont());
title->Paint();
delete title;
}
}
void TPaveStats::SavePrimitive(ostream &out, Option_t * )
{
char quote = '"';
out<<" "<<endl;
if (gROOT->ClassSaved(TPaveStats::Class())) {
out<<" ";
} else {
out<<" "<<ClassName()<<" *";
}
if (fOption.Contains("NDC")) {
out<<"ptstats = new "<<ClassName()<<"("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
<<","<<quote<<fOption<<quote<<");"<<endl;
} else {
out<<"ptstats = new "<<ClassName()<<"("<<fX1<<","<<fY1<<","<<fX2<<","<<fY2
<<","<<quote<<fOption<<quote<<");"<<endl;
}
if (strcmp(GetName(),"TPave")) {
out<<" ptstats->SetName("<<quote<<GetName()<<quote<<");"<<endl;
}
if (fBorderSize != 4) {
out<<" ptstats->SetBorderSize("<<fBorderSize<<");"<<endl;
}
SaveFillAttributes(out,"ptstats",19,1001);
SaveLineAttributes(out,"ptstats",1,1,1);
SaveTextAttributes(out,"ptstats",22,0,1,62,0);
SaveLines(out,"ptstats");
out<<" ptstats->SetOptStat("<<GetOptStat()<<");"<<endl;
out<<" ptstats->SetOptFit("<<GetOptFit()<<");"<<endl;
out<<" ptstats->Draw();"<<endl;
}
void TPaveStats::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
UInt_t R__s, R__c;
Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
if (R__v > 2) {
R__b.ReadClassBuffer(TPaveStats::Class(), this, R__v, R__s, R__c);
return;
}
TPaveText::Streamer(R__b);
R__b >> fOptFit;
R__b >> fOptStat;
if (R__v > 1 || R__b.GetVersionOwner() == 22304) {
fFitFormat.Streamer(R__b);
fStatFormat.Streamer(R__b);
} else {
SetFitFormat();
SetStatFormat();
}
R__b.CheckByteCount(R__s, R__c, TPaveStats::IsA());
} else {
R__b.WriteClassBuffer(TPaveStats::Class(),this);
}
}
void TPaveStats::UseCurrentStyle()
{
if (gStyle->IsReading()) {
SetOptStat(gStyle->GetOptStat());
SetOptFit(gStyle->GetOptFit());
SetStatFormat(gStyle->GetStatFormat());
SetFitFormat(gStyle->GetFitFormat());
SetBorderSize(gStyle->GetStatBorderSize());
SetFillColor(gStyle->GetStatColor());
SetFillStyle(gStyle->GetStatStyle());
SetTextFont(gStyle->GetStatFont());
SetTextSize(gStyle->GetStatFontSize());
SetTextColor(gStyle->GetStatTextColor());
SetX2NDC(gStyle->GetStatX());
SetY2NDC(gStyle->GetStatY());
SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
SetY1NDC(gStyle->GetStatY()-gStyle->GetStatH());
} else {
gStyle->SetOptStat(GetOptStat());
gStyle->SetOptFit(GetOptFit());
gStyle->SetStatFormat(GetStatFormat());
gStyle->SetFitFormat(GetFitFormat());
gStyle->SetStatBorderSize(GetBorderSize());
gStyle->SetTextColor(GetTextColor());
gStyle->SetStatColor(GetFillColor());
gStyle->SetStatStyle(GetFillStyle());
gStyle->SetTextFont(GetTextFont());
gStyle->SetStatFontSize(GetTextSize());
gStyle->SetStatTextColor(GetTextColor());
gStyle->SetStatX(GetX2NDC());
gStyle->SetStatY(GetY2NDC());
gStyle->SetStatW(GetX2NDC()-GetX1NDC());
gStyle->SetStatH(GetY2NDC()-GetY1NDC());
}
}