#include "TPad.h" #include "TMath.h" #include "TPaveLabel.h" #include "TH2F.h" #include "TString.h" #include using namespace std; //________________________________________________________________________ class MyTPaveLabel : public TPaveLabel { protected: public: MyTPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t y2, const char *label, Option_t *option = ""); void ConvertNDCtoPad(); void Paint(Option_t *option); ~MyTPaveLabel() {}; ClassDef(MyTPaveLabel,1) //PaveLabel. A Pave with a label }; ClassImp(MyTPaveLabel) MyTPaveLabel::MyTPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t y2, const char *label, Option_t *option) :TPaveLabel(x1,y1,x2,y2,label,option) { fOption = option; fOption.ToUpper(); } //______________________________________________________________________________ void MyTPaveLabel::Paint(Option_t *option) { // Paint this pavelabel with its current attributes. // Convert from NDC to pad coordinates // Note: dont use the one from TPave ConvertNDCtoPad(); TPaveLabel::PaintPaveLabel(fX1, fY1, fX2, fY2, TPaveLabel::GetLabel(), strlen(option)?option:TPaveLabel::GetOption()); } //______________________________________________________________________________ void MyTPaveLabel::ConvertNDCtoPad() { // Convert pave coordinates from NDC to Pad coordinates. Double_t dpx = gPad->GetX2() - gPad->GetX1(); Double_t dpy = gPad->GetY2() - gPad->GetY1(); Double_t xp1 = gPad->GetX1(); Double_t yp1 = gPad->GetY1(); // Check if pave initialisation has been done. // This operation cannot take place in the Pave constructor because // the Pad range may not be known at this time. if (!fInit) { fInit = 1; if (fOption.Contains("NDC")) { fX1NDC = fX1; fY1NDC = fY1; fX2NDC = fX2; fY2NDC = fY2; fX1 = xp1 + fX1NDC*dpx; fY1 = yp1 + fY1NDC*dpy; fX2 = xp1 + fX2NDC*dpx; fY2 = yp1 + fY2NDC*dpy; } else { if (gPad->GetLogx()) { if (fX1 > 0) fX1 = TMath::Log10(fX1); if (fX2 > 0) fX2 = TMath::Log10(fX2); } if (gPad->GetLogy()) { if (fY1 > 0) fY1 = TMath::Log10(fY1); if (fY2 > 0) fY2 = TMath::Log10(fY2); } fX1NDC = (fX1-xp1)/dpx; fY1NDC = (fY1-yp1)/dpy; fX2NDC = (fX2-xp1)/dpx; fY2NDC = (fY2-yp1)/dpy; } } else { if (!fOption.Contains("FIX") || fOption.Contains("NDC")) { //new fX1 = xp1 + fX1NDC*dpx; fY1 = yp1 + fY1NDC*dpy; fX2 = xp1 + fX2NDC*dpx; fY2 = yp1 + fY2NDC*dpy; } else { //new // keep original lower left, keep size //new Double_t sizeX= dpx * (fX2NDC-fX1NDC); //new Double_t sizeY= dpy * (fY2NDC-fY1NDC); //new fX2 = fX1 + sizeX; //new fY2 = fY1 + sizeY; //new } } } //_______________________________________________________________________ void testpl(){ TH2F *hist = new TH2F("name", "title", 100, 0, 100, 100, 0, 100); hist->Draw(); MyTPaveLabel* label_fixed = new MyTPaveLabel(40,50,60,60,"label_{fixed}", "FIX"); label_fixed->Draw(); }