CERN ROOT 直方图 例1:建立新坐标轴,把两个不同值域的图像画在一起

文章展示了如何利用ROOT库在图形界面TCanvas上创建并绘制直方图,然后计算直方图的积分,并在同一图表上以对数坐标轴显示积分曲线。通过TH1F类处理直方图,用TGaxis创建右侧的坐标轴来展示积分的数值。
摘要由CSDN通过智能技术生成

首先应该考虑对数坐标,然而有些情况下对数坐标并不适合表达含义,下面给一个例子。

一个ROOT官方例子,实现了对直方图的积分和在同一个图上增加坐标轴画出积分曲线。

TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
 
//create/fill draw h1
gStyle->SetOptStat(kFALSE);//不画出右上角数据框图
TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
Int_t i;
for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
h1->Draw();
c1->Update();//更新画布,此时已经有一个直方图在画布上
 
//create hint1 filled with the bins integral of h1
TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
Float_t sum = 0;
for (i=1;i<=100;i++) 
{
    sum += h1->GetBinContent(i);//不断把h1的计数累加到sum上
    hint1->SetBinContent(i,sum);//积分值作为bin的高度
}
 
//scale hint1 to the pad coordinates
Float_t rightmax = 1.1*hint1->GetMaximum();//rightmax是h1最大值的1.1倍
Float_t scale = gPad->GetUymax()/rightmax;//参数scale为h1图中y轴最高点和rightmax的比值
hint1->SetLineColor(kRed);
hint1->Scale(scale);//将hint1放缩,最后的结果是y轴最高点为hint1最大值的1.1倍
hint1->Draw("same");
 
//draw an axis on the right side
TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");//510代表
axis->SetLineColor(kRed);
axis->SetLabelColor(kRed);
axis->Draw();

c1->Draw();

 其中TGaxis


TGaxis::TGaxis	(	
Double_t 	xmin,//轴的位置
Double_t 	ymin,
Double_t 	xmax,
Double_t 	ymax,
Double_t 	wmin,//轴的起始值
Double_t 	wmax,//轴的最大值
Int_t 	ndiv = 510,
Option_t * 	chopt = "",
Double_t 	gridlength = 0 
)	

xmin : X origin coordinate in user's coordinates space.
xmax : X end axis coordinate in user's coordinates space.
ymin : Y origin coordinate in user's coordinates space.
ymax : Y end axis coordinate in user's coordinates space.
wmin : Lowest value for the tick mark labels written on the axis.
wmax : Highest value for the tick mark labels written on the axis.
ndiv : Number of divisions.
ndiv=N1 + 100*N2 + 10000*N3
N1=number of 1st divisions.
N2=number of 2nd divisions.
N3=number of 3rd divisions. e.g.:
ndiv=0 --> no tick marks.
ndiv=2 --> 2 divisions, one tick mark in the middle of the axis.

chopt = "+": tick marks are drawn on Positive side. (default)
chopt ="-": tick mark are drawn on the negative side.
chopt = "+-": tick marks are drawn on both sides of the axis.
chopt = "U": Unlabelled axis, default is labeled.

chopt = "R": labels are Right adjusted on tick mark.(default is centered)
chopt = "L": labels are Left adjusted on tick mark.
chopt = "C": labels are Centered on tick mark.
chopt = "M": In the Middle of the divisions.

需要注意的:

510:把坐标轴分成十等分然后在每个间隔分成5等分,但当ROOT觉得这样的分割会出现小数时,会自动的把间隔进行微调,在本例中就调整为了405;

+L:+-决定坐标轴短线的朝向,LR决定短线与数字的相对位置,L:短线在数字左边

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值