ROOT 提供了许多图形类,如TGraph图表;TGraphErrors具有对称误差线的图表;TGraph2D由三个数组 X、Y 和 Z 组成的图表等。
图表的使用
TGraph支持批量创建和运算,TGraph的创建方法有:
// 创建了一个名字是name,标题是title,有n个坐标为x,y的点:TGraph *gr = new TGraph("name","title", number, x, y)TGraph* gr = new TGraph("name","title", number, x, y)TGraph *gr = new TGraph(number, x, y)auto gr = new TGraph(number, x, y)auto gr = new TGraph()// 使用AddPoint()函数添加坐标点auto dt = new TGraph2D()// 二维图表,其他形式同理
绘制图表前需要先定义坐标数组,然后使用TGraph构造图形。e.g.
int n =20;doublex[n],y[n];for(int i=0; i<n; i++){x[i]= i*0.1;y[i]=10*sin(x[i]+0.2);} auto gr = new TGraph(n, x, y);
auto gr = new TGraph();
for (int i=0; i<20; i++) gr->AddPoint(i*0.1, 10*sin(i*0.1+0.2));
auto gr = new TGraph();
for (int i=0; i<20; i++) gr->AddPoint(i*0.1, 10*sin(i*0.1+0.2));
gr->Draw();
int directdata(){
TGraph *gr = new TGraph("mydata.txt");
gr->Draw("AC*"); // 绘出 axis,坐标点以 curve 链接,坐标点形状为 * 型
}
void graphpalettecolor () {
gStyle->SetOptTitle(kFALSE);
gStyle->SetPalette(kSolar);
double x[5] = {1,2,3,4,5};
double y1[5] = {1.0,2.0,1.0,2.5,3.0};
double y2[5] = {1.1,2.1,1.1,2.6,3.1};
double y3[5] = {1.2,2.2,1.2,2.7,3.2};
double y4[5] = {1.3,2.3,1.3,2.8,3.3};
double y5[5] = {1.4,2.4,1.4,2.9,3.4};
TGraph *g1 = new TGraph(5,x,y1); g1->SetTitle("Graph with a red star");
TGraph *g2 = new TGraph(5,x,y2); g2->SetTitle("Graph with a circular marker");
TGraph *g3 = new TGraph(5,x,y3); g3->SetTitle("Graph with an open square marker");
TGraph *g4 = new TGraph(5,x,y4); g4->SetTitle("Graph with a blue star");
TGraph *g5 = new TGraph(5,x,y5); g5->SetTitle("Graph with a full square marker");
g1->SetLineWidth(3); g1->SetMarkerColor(kRed);
g2->SetLineWidth(3); g2->SetMarkerStyle(kCircle);
g3->SetLineWidth(3); g3->SetMarkerStyle(kOpenSquare);
g4->SetLineWidth(3); g4->SetMarkerColor(kBlue);
g5->SetLineWidth(3); g5->SetMarkerStyle(kFullSquare);
g1->Draw("CA*x+ry PLC PFC");
g2->Draw("PCx+ry PLC PFC");
g3->Draw("PC PLC PFC");
g4->Draw("*C PLC PFC");
g5->Draw("PC PLC PFC");
gPad->BuildLegend();
}
{
TCanvas* canvas = new TCanvas("canvas");
TH1F* histo = new TH1F("histo","test 1",10,0.,10.);
histo->SetFillColor(2);
histo->Fill(2.);
histo->Draw();
canvas->Print("plots.pdf(","Title:One bin filled");
histo->Fill(4.);
histo->Draw();
canvas->Print("plots.pdf","Title:Two bins filled");
histo->Fill(6.);
histo->Draw();
canvas->Print("plots.pdf","Title:Three bins filled");
histo->Fill(8.);
histo->Draw();
canvas->Print("plots.pdf","Title:Four bins filled");
histo->Fill(8.);
histo->Draw();
canvas->Print("plots.pdf)","Title:The fourth bin content is 2");
}
TFile *file = new TFile("tree.root", "RECREATE");
TTree *tree = new TTree("tree", "Example Tree");
Float_t x, y;
tree->Branch("x", &x, "x/F");
tree->Branch("y", &y, "y/F");
for (Int_t i = 0; i < 100; i++) {
x = gRandom->Gaus(0, 1);
y = gRandom->Gaus(0, 1);
tree->Fill();
}
tree->Write(); // 将树保存到ROOT文件
file->Close();
{
auto c = new TCanvas("c","Graph2D example",0,0,700,600); // 画布距离左上角的距离(0,0)
double x, y, z, P = 6.;
int np = 200;
auto dt = new TGraph2D();
auto r = new TRandom();
for (int N=0; N<np; N++) {
x = 2*P*(r->Rndm(N))-P;
y = 2*P*(r->Rndm(N))-P;
z = (sin(x)/x)*(sin(y)/y)+0.2;
dt->SetPoint(N,x,y,z);
}
dt->Draw("tri1 p0");
}
void gr006_scatter()
{
auto canvas = new TCanvas();
canvas->SetRightMargin(0.14);
gStyle->SetPalette(kBird, 0, 0.6); // define a transparent palette
const int n = 175;
double x[n];
double y[n];
double c[n];
double s[n];
// Define four random data sets
auto r = new TRandom();
for (int i=0; i<n; i++) {
x[i] = 100*r->Rndm(i);
y[i] = 200*r->Rndm(i);
c[i] = 300*r->Rndm(i);
s[i] = 400*r->Rndm(i);
}
auto scatter = new TScatter(n, x, y, c, s);
scatter->SetMarkerStyle(20);
scatter->SetTitle("Scatter plot title;X title;Y title;Z title");
// an alternative way to zoom the Z-axis:
// scatter->GetHistogram()->SetMinimum(10);
// scatter->GetHistogram()->SetMaximum(200);
scatter->Draw("A");
}