Hi,
Sure, this one runs, but is very crude, if you have some bins that have 1000 entries and the other has 5, this will not work very well:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
const int num_bins=5;
int mypow(int a, int b);
//should be dependent on text length (at least)
double padding_text=0.02;
void numbers_in_graph(){
//could make a much more interesting example obviously,
//but I can't think of a simpler one that is usable in practice
double x[num_bins], e_y[num_bins];
for(int i=0; i<num_bins; i++){
x[i]=(double)i+0.5;
e_y[i]=pow(2,i);
}
srand (time(NULL));
double y[num_bins]={0}, n[num_bins]={0};
for(int i=0; i<1000; i++){
double rand_tmp=2.*(rand()/(double)RAND_MAX);
int bin = rand()%num_bins;
y[bin]+=rand_tmp;
n[bin]++;
}
double max_th2 = y[0];
double min_th2 = y[0];
for (i = 1; i < num_bins; i++) {
max_th2=std::max(y[i],max_th2);
min_th2=std::min(y[i],min_th2);
}
TH2F* aux_th2=new TH2F("aux_th2","",1,0,num_bins,1,0.75*min_th2,1.25*max_th2);
aux_th2->SetStats(0);
TGraphErrors* graph=new TGraphErrors(num_bins, x, y, 0, e_y);
TCanvas* canvas=new TCanvas("canvas","",600,480);
canvas->cd();
double margin_bottom=0.1, margin_top=0.1, margin_left=0.1, margin_right=0.1;
gPad->SetBottomMargin(margin_bottom);
gPad->SetRightMargin(margin_top);
gPad->SetTopMargin(margin_left);
gPad->SetLeftMargin(margin_right);
aux_th2->Draw();
graph->Draw("PL");
//The reason this is written in this convoluted manner
//(GetPoint instead of using defined vectors, etc)
//is because it was inside a function
TText* text[num_bins];
double max_x=aux_th2->GetXaxis()->GetXmax();
double min_x=aux_th2->GetXaxis()->GetXmin();
double max_y=aux_th2->GetYaxis()->GetXmax();
double min_y=aux_th2->GetYaxis()->GetXmin();
for (int ibin = 0; ibin < num_bins; ibin++){
double xp, yp, eyp;
graph->GetPoint(ibin,xp,yp);
eyp=graph->GetErrorY(ibin);
//draw numbers starting at the bottom of the th2d ( 1-margin_bottom) and
//going only up to the top (1.-margin_bottom-margin_up) and add a margin
//so it doesn't plot on top of the line
double perc_y = (1-margin_bottom)-(1.-margin_bottom-margin_top)*(max_y-(yp+eyp))/(max_y-min_y)+padding_text;
//same thing but this time the margin is for the length of the number
//future: make it length dependent
double perc_x = (1-margin_left)-(1.-margin_left-margin_right)*(max_x-xp)/(max_x-min_x)-padding_text;
if(perc_x>1 || perc_y>1 || perc_x<0 || perc_y<0){
cout << "error gets out of frame -> redefine th2" << endl;
continue;
}
int num=n[ibin];
if(num>0){
const int ni=int(log(num)/log(10)+1);
char* num_string=new char[ni];
num_string[ni]='\0';
for (int ii=ni-1, j=0; ii>=0; ii--, j++){
num_string[j]=int(num%int(mypow(10, ii+1)))/int(mypow(10, ii))+'0';
}
text[ibin]=new TText(perc_x,perc_y,num_string);
}
else
text[ibin]=new TText(perc_x,perc_y,"0");
text[ibin]->SetNDC(true);
text[ibin]->Draw();
}
return;
}
int mypow(int a, int b)
{
if (b==0) return 1;
if (a==0) return 0;
return a*mypow(a, b-1);
}