00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string.h>
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015
00016 #include "Riostream.h"
00017 #include "TROOT.h"
00018 #include "TPaveStats.h"
00019 #include "TPaveLabel.h"
00020 #include "TVirtualPad.h"
00021 #include "TStyle.h"
00022 #include "TClass.h"
00023 #include "TLatex.h"
00024
00025 ClassImp(TPaveStats)
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 const UInt_t kTakeStyle = BIT(17);
00202
00203
00204
00205 TPaveStats::TPaveStats(): TPaveText()
00206 {
00207
00208
00209
00210
00211 fParent = 0;
00212 fOptFit = gStyle->GetOptFit();
00213 fOptStat = gStyle->GetOptStat();
00214 }
00215
00216
00217
00218 TPaveStats::TPaveStats(Double_t x1, Double_t y1,Double_t x2, Double_t y2, Option_t *option)
00219 :TPaveText(x1,y1,x2,y2,option)
00220 {
00221
00222
00223
00224
00225 fParent = 0;
00226 fOptFit = gStyle->GetOptFit();
00227 fOptStat = gStyle->GetOptStat();
00228 SetFitFormat(gStyle->GetFitFormat());
00229 SetStatFormat(gStyle->GetStatFormat());
00230 }
00231
00232
00233
00234 TPaveStats::~TPaveStats()
00235 {
00236
00237
00238
00239
00240 if ( fParent && !fParent->TestBit(kInvalidObject)) fParent->RecursiveRemove(this);
00241 }
00242
00243
00244
00245 Int_t TPaveStats::GetOptFit() const
00246 {
00247
00248
00249
00250
00251 if (TestBit(kTakeStyle)) return gStyle->GetOptFit();
00252 return fOptFit;
00253 }
00254
00255
00256
00257 Int_t TPaveStats::GetOptStat() const
00258 {
00259
00260
00261
00262
00263 if (TestBit(kTakeStyle)) return gStyle->GetOptStat();
00264 return fOptStat;
00265 }
00266
00267
00268
00269 void TPaveStats::SaveStyle()
00270 {
00271
00272
00273
00274
00275 gStyle->SetOptFit(fOptFit);
00276 gStyle->SetOptStat(fOptStat);
00277 gStyle->SetFitFormat(fFitFormat.Data());
00278 gStyle->SetStatFormat(fStatFormat.Data());
00279 }
00280
00281
00282
00283 void TPaveStats::SetFitFormat(const char *form)
00284 {
00285
00286
00287
00288
00289 fFitFormat = form;
00290 }
00291
00292
00293
00294 void TPaveStats::SetOptFit(Int_t fit)
00295 {
00296
00297
00298
00299
00300 fOptFit = fit;
00301 ResetBit(kTakeStyle);
00302 }
00303
00304
00305
00306 void TPaveStats::SetOptStat(Int_t stat)
00307 {
00308
00309
00310
00311
00312 fOptStat = stat;
00313 ResetBit(kTakeStyle);
00314 }
00315
00316
00317
00318 void TPaveStats::SetStatFormat(const char *form)
00319 {
00320
00321
00322
00323
00324 fStatFormat = form;
00325 }
00326
00327
00328
00329 void TPaveStats::Paint(Option_t *option)
00330 {
00331
00332
00333
00334
00335 TPave::ConvertNDCtoPad();
00336 TPave::PaintPave(fX1,fY1,fX2,fY2,GetBorderSize(),option);
00337
00338 if (!fLines) return;
00339 Double_t y2ref = TMath::Max(fY1,fY2);
00340 Double_t x1ref = TMath::Min(fX1,fX2);
00341 Double_t x2ref = TMath::Max(fX1,fX2);
00342 Double_t dx = TMath::Abs(fX2 - fX1);
00343 Double_t dy = TMath::Abs(fY2 - fY1);
00344 Double_t titlesize=0;
00345 Double_t textsize = GetTextSize();
00346 Int_t nlines = GetSize();
00347 if (nlines == 0) nlines = 5;
00348 Int_t print_name = fOptStat%10;
00349
00350
00351 Double_t y1 = gPad->GetY1();
00352 Double_t y2 = gPad->GetY2();
00353 Float_t margin = fMargin*dx;
00354 Double_t yspace = dy/Double_t(nlines);
00355 Double_t textsave = textsize;
00356 TObject *line;
00357 TLatex *latex, *latex_tok;
00358 TIter next(fLines);
00359 Double_t longest = 0, titlelength = 0;
00360 Double_t w, wtok[2];
00361 char *st, *sl=0;
00362 if (textsize == 0) {
00363 textsize = 0.92*yspace/(y2 - y1);
00364 titlesize = textsize;
00365 wtok[0] = 0; wtok[1] = 0;
00366 while ((line = (TObject*) next())) {
00367 if (line->IsA() == TLatex::Class()) {
00368 latex = (TLatex*)line;
00369 Int_t nchs = strlen(latex->GetTitle());
00370 sl = new char[nchs+1];
00371 strlcpy(sl, latex->GetTitle(),nchs+1);
00372 if (strpbrk(sl, "=") !=0 && print_name == 0) {
00373 st = strtok(sl, "=");
00374 Int_t itok = 0;
00375 while ( st !=0 ) {
00376 latex_tok = new TLatex(0.,0.,st);
00377 Style_t tfont = latex->GetTextFont();
00378 if (tfont == 0) tfont = GetTextFont();
00379 latex_tok->SetTextFont(tfont);
00380 latex_tok->SetTextSize(textsize);
00381 w = latex_tok->GetXsize();
00382 if (w > wtok[itok]) wtok[itok] = w;
00383 st = strtok(0, "=");
00384 ++itok;
00385 delete latex_tok;
00386 }
00387 } else if (strpbrk(sl, "|") !=0) {
00388 } else {
00389 print_name = 0;
00390 Style_t tfont = latex->GetTextFont();
00391 if (tfont == 0) latex->SetTextFont(GetTextFont());
00392 latex->SetTextSize(titlesize);
00393 titlelength = latex->GetXsize()+2.*margin;
00394 if (titlelength > 0.98*dx) titlesize *= 0.98*dx/titlelength;
00395 latex->SetTextFont(tfont);
00396 }
00397 delete [] sl; sl = 0;
00398 }
00399 }
00400 longest = wtok[0]+wtok[1]+2.*margin;
00401 if (longest > 0.98*dx) textsize *= 0.98*dx/longest;
00402 SetTextSize(textsize);
00403 } else {
00404 titlesize = textsize;
00405 }
00406 Double_t ytext = y2ref + 0.5*yspace;
00407 Double_t xtext = 0;
00408 print_name = fOptStat%10;
00409
00410
00411
00412 next.Reset();
00413 while ((line = (TObject*) next())) {
00414 if (line->IsA() == TLatex::Class()) {
00415 latex = (TLatex*)line;
00416 ytext -= yspace;
00417 Double_t xl = latex->GetX();
00418 Double_t yl = latex->GetY();
00419 Short_t talign = latex->GetTextAlign();
00420 Color_t tcolor = latex->GetTextColor();
00421 Style_t tfont = latex->GetTextFont();
00422 Size_t tsize = latex->GetTextSize();
00423 if (tcolor == 0) latex->SetTextColor(GetTextColor());
00424 if (tfont == 0) latex->SetTextFont(GetTextFont());
00425 if (tsize == 0) latex->SetTextSize(GetTextSize());
00426
00427 Int_t nchs = strlen(latex->GetTitle());
00428 sl = new char[nchs+1];
00429 strlcpy(sl, latex->GetTitle(),nchs+1);
00430
00431 if (strpbrk(sl, "=") !=0 && print_name == 0) {
00432 st = strtok(sl, "=");
00433 Int_t halign = 12;
00434 while ( st !=0 ) {
00435 latex->SetTextAlign(halign);
00436 if (halign == 12) xtext = x1ref + margin;
00437 if (halign == 32) {
00438 xtext = x2ref - margin;
00439
00440 char *stc;
00441 stc=st+strlen(st)-1;
00442 while (*stc == ' ') {
00443 *stc = '\0';
00444 --stc;
00445 }
00446 }
00447 latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
00448 latex->GetTextSize(),
00449 st);
00450 st = strtok(0, "=");
00451 halign = 32;
00452 }
00453
00454 } else if (strpbrk(sl, "|") !=0) {
00455 Double_t yline1 = ytext+yspace/2.;
00456 Double_t yline2 = ytext-yspace/2.;
00457 Double_t xline1 = dx/3+x1ref;
00458 Double_t xline2 = 2*dx/3+x1ref;
00459 gPad->PaintLine(x1ref,yline1,x2ref,yline1);
00460 gPad->PaintLine(xline1,yline1,xline1,yline2);
00461 gPad->PaintLine(xline2,yline1,xline2,yline2);
00462 st = strtok(sl, "|");
00463 Int_t theIndex = 0;
00464 while ( st !=0 ) {
00465 latex->SetTextAlign(22);
00466 if (theIndex == 0) xtext = 0.5*(x1ref+xline1);
00467 if (theIndex == 1) xtext = 0.5*(x1ref+x2ref);
00468 if (theIndex == 2) xtext = 0.5*(xline2+x2ref);
00469 latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
00470 latex->GetTextSize(),
00471 st);
00472 theIndex++;
00473 st = strtok(0, "|");
00474 }
00475
00476 } else {
00477 print_name = 0;
00478 latex->SetTextAlign(22);
00479 xtext = 0.5*(x1ref+x2ref);
00480 latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
00481 titlesize,
00482 sl);
00483 gPad->PaintLine(x1ref,y2ref-yspace,x2ref,y2ref-yspace);
00484 }
00485 delete [] sl;
00486
00487 latex->SetTextAlign(talign);
00488 latex->SetTextColor(tcolor);
00489 latex->SetTextFont(tfont);
00490 latex->SetTextSize(tsize);
00491 latex->SetX(xl);
00492 latex->SetY(yl);
00493 }
00494 }
00495 SetTextSize(textsave);
00496
00497
00498 if (fLabel.Length() > 0) {
00499 Double_t x1,x2;
00500 dy = gPad->GetY2() - gPad->GetY1();
00501 x1 = x1ref + 0.25*dx;
00502 x2 = x2ref - 0.25*dx;
00503 y1 = y2ref - 0.02*dy;
00504 y2 = y2ref + 0.02*dy;
00505 TPaveLabel *title = new TPaveLabel(x1,y1,x2,y2,fLabel.Data(),GetDrawOption());
00506 title->SetFillColor(GetFillColor());
00507 title->SetTextColor(GetTextColor());
00508 title->SetTextFont(GetTextFont());
00509 title->Paint();
00510 delete title;
00511 }
00512 }
00513
00514
00515
00516 void TPaveStats::SavePrimitive(ostream &out, Option_t * )
00517 {
00518
00519
00520
00521
00522 char quote = '"';
00523 out<<" "<<endl;
00524 if (gROOT->ClassSaved(TPaveStats::Class())) {
00525 out<<" ";
00526 } else {
00527 out<<" "<<ClassName()<<" *";
00528 }
00529 if (fOption.Contains("NDC")) {
00530 out<<"ptstats = new "<<ClassName()<<"("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
00531 <<","<<quote<<fOption<<quote<<");"<<endl;
00532 } else {
00533 out<<"ptstats = new "<<ClassName()<<"("<<fX1<<","<<fY1<<","<<fX2<<","<<fY2
00534 <<","<<quote<<fOption<<quote<<");"<<endl;
00535 }
00536 if (strcmp(GetName(),"TPave")) {
00537 out<<" ptstats->SetName("<<quote<<GetName()<<quote<<");"<<endl;
00538 }
00539 if (fBorderSize != 4) {
00540 out<<" ptstats->SetBorderSize("<<fBorderSize<<");"<<endl;
00541 }
00542 SaveFillAttributes(out,"ptstats",19,1001);
00543 SaveLineAttributes(out,"ptstats",1,1,1);
00544 SaveTextAttributes(out,"ptstats",22,0,1,62,0);
00545 SaveLines(out,"ptstats");
00546 out<<" ptstats->SetOptStat("<<GetOptStat()<<");"<<endl;
00547 out<<" ptstats->SetOptFit("<<GetOptFit()<<");"<<endl;
00548 out<<" ptstats->Draw();"<<endl;
00549 }
00550
00551
00552
00553 void TPaveStats::Streamer(TBuffer &R__b)
00554 {
00555
00556
00557
00558
00559 if (R__b.IsReading()) {
00560 UInt_t R__s, R__c;
00561 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
00562 if (R__v > 2) {
00563 R__b.ReadClassBuffer(TPaveStats::Class(), this, R__v, R__s, R__c);
00564 return;
00565 }
00566
00567 TPaveText::Streamer(R__b);
00568 R__b >> fOptFit;
00569 R__b >> fOptStat;
00570 if (R__v > 1 || R__b.GetVersionOwner() == 22304) {
00571 fFitFormat.Streamer(R__b);
00572 fStatFormat.Streamer(R__b);
00573 } else {
00574 SetFitFormat();
00575 SetStatFormat();
00576 }
00577 R__b.CheckByteCount(R__s, R__c, TPaveStats::IsA());
00578
00579
00580 } else {
00581 R__b.WriteClassBuffer(TPaveStats::Class(),this);
00582 }
00583 }
00584
00585
00586
00587 void TPaveStats::UseCurrentStyle()
00588 {
00589
00590
00591
00592
00593 if (gStyle->IsReading()) {
00594 SetOptStat(gStyle->GetOptStat());
00595 SetOptFit(gStyle->GetOptFit());
00596 SetStatFormat(gStyle->GetStatFormat());
00597 SetFitFormat(gStyle->GetFitFormat());
00598 SetBorderSize(gStyle->GetStatBorderSize());
00599 SetFillColor(gStyle->GetStatColor());
00600 SetFillStyle(gStyle->GetStatStyle());
00601 SetTextFont(gStyle->GetStatFont());
00602 SetTextSize(gStyle->GetStatFontSize());
00603 SetTextColor(gStyle->GetStatTextColor());
00604 SetX2NDC(gStyle->GetStatX());
00605 SetY2NDC(gStyle->GetStatY());
00606 SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
00607 SetY1NDC(gStyle->GetStatY()-gStyle->GetStatH());
00608 } else {
00609 gStyle->SetOptStat(GetOptStat());
00610 gStyle->SetOptFit(GetOptFit());
00611 gStyle->SetStatFormat(GetStatFormat());
00612 gStyle->SetFitFormat(GetFitFormat());
00613 gStyle->SetStatBorderSize(GetBorderSize());
00614 gStyle->SetTextColor(GetTextColor());
00615 gStyle->SetStatColor(GetFillColor());
00616 gStyle->SetStatStyle(GetFillStyle());
00617 gStyle->SetTextFont(GetTextFont());
00618 gStyle->SetStatFontSize(GetTextSize());
00619 gStyle->SetStatTextColor(GetTextColor());
00620 gStyle->SetStatX(GetX2NDC());
00621 gStyle->SetStatY(GetY2NDC());
00622 gStyle->SetStatW(GetX2NDC()-GetX1NDC());
00623 gStyle->SetStatH(GetY2NDC()-GetY1NDC());
00624 }
00625 }