2.2.4 函数与图形
基本函数表达
root中的函数表达和C语言重合,例如:
sin(x)/x
[0]*sin(x) + [1]*exp(-[2]*x)
x + y**2
x^2 + y^2
[0]*pow([1],4)
2*pi*sqrt(x/y)
gaus(0)*expo(3) + ypol3(5)*x
gausn(0)*expo(3) + ypol3(5)*x
gaus(x, [0..2]) + expo(y, [3..4])
In the last examples above:
gaus(0)
is a substitute for[0]*exp(-0.5*((x-[1])/[2])**2)
and (0) means start numbering parameters at 0gausn(0)
is a substitute for[0]*exp(-0.5*((x-[1])/[2])**2)/(sqrt(2*pi)*[2]))
and (0) means start numbering parameters at 0expo(3)
is a substitute forexp([3]+[4]*x)
pol3(5)
is a substitute forpar[5]+par[6]*x+par[7]*x**2+par[8]*x**3
(PolN
stands for Polynomial of degree N)gaus(x, [0..2])
is a more explicit way of writinggaus(0)
expo(y, [3..4])
is a substitute forexp([3]+[4]*y)
TMath::Landau(x)*sin(x)
TMath::Erf(x)
Formula may contain constants, eg:
sqrt2
e
pi
ln10
infinity
and more.
{
auto fa1 = new TF1("fa1","sin(x)/x",0,10);
fa1->Draw();
}
{
auto f2 = new TF2("f2","sin(x)*sin(y)/(x*y)",0,5,0,5);
f2->Draw();
}
直线
root[] l = new TLine(0.2,0.2,0.8,0.3)
root[] l->Draw()
致密网格
void DrawGrid(TString t, Int_t ngx, Int_t ngy)
{
Double_t x1 = -10;
Double_t x2 = 20;
Double_t y1 = 10;
Double_t y2 = 40;
Double_t xs = (x2-x1)/ngx;
Double_t ys = (y2-y1)/ngy;
Int_t i;
TH1F *h = gPad->DrawFrame(x1, y1, x2, y2);
h->GetXaxis()->SetNdivisions(20);
h->GetYaxis()->SetNdivisions(20);
h->GetYaxis()->SetTickLength(0.);
h->GetXaxis()->SetTickLength(0.);
h->GetXaxis()->SetLabelSize(0.025);
h->GetYaxis()->SetLabelSize(0.025);
h->SetTitle(t.Data());
gPad-> Update();
TLine l;
l.SetLineColor(kGray);
Double_t x = x1+xs;
for (i = 0; i<ngx-1; i++) {
l.DrawLine(x,y1,x,y2);
x = x + xs;
}
Double_t y = y1+xs;
for (i = 0; i<ngy-1; i++) {
l.DrawLine(x1,y,x2,y);
y = y +ys;
}
}
void ofirar()
{
TCanvas *c = new TCanvas("c","c", 800, 800);
Double_t x[10] = {1.,2.,3.,4.,5.,-1.,-2.,-6.,-7.,-8.};
Double_t y[10] = {21.,21.,23.,34.,35.,21.,22.,36.,37.,28.};
TGraph *before = new TGraph(10, x, y);
before->SetTitle("Before");
before->SetMarkerStyle(kFullCircle);
before->SetMarkerColor(kBlue);
DrawGrid("Before/After plot", 100, 100);
before->Draw("P");
// Double_t a[10] = {-1.,2.,-3.,4.,-5.,1.,2.,-6.,7.,-8.};
// Double_t b[10] = {25.,24.,22.,31.,32.,27.,23.,35.,38.,22.};
Double_t a[10] = {-1.,2.,-3.,4.,-5.,1.,2.,-6.,7.,-8.};
Double_t b[10] = {25.,24.,22.,31.,32.,27.,23.,35.,38.,22.};
TGraph *after = new TGraph(10, a, b);
after->SetMarkerStyle(kFullCircle);
after->SetMarkerColor(kRed);
after->Draw("P");
}
箭头
TArrow *ar = new TArrow(4,0.2,4,4.5,0.02,"|>");ar->SetLineWidth(3); ar->SetArrowSize(0.02);ar->Draw();
默认箭头
矩形 TBox
b = new TBox(0.2,0.2,0.8,0.3); b->SetFillColor(5); b->SetFillStyle(3001); b->Draw();
矩形文本框 TPaveLabel
TPaveText *pave = new TPaveText(-3.78,500,-1.2,750);
TText *t1=pave->AddText("You can move");
t1->SetTextColor(4);
t1->SetTextSize(0.05);
pave->AddText("Title and Stats pads");
pave->AddText("X and Y axis");
pave->AddText("You can modify bin contents");
pave->Draw();
c1->Update();
椭圆 TEllipse
TEllipse是很强大的类,它能帮你绘制椭圆、圆、半圆、倾斜的椭圆等,其标准用如下:
TEllipse *ellipse; // 设置PSF于Pad
ellipse = new TEllipse(Ra[i]+w2*3/5.,Dec[i]-w*3/5.,R[i]*1.51/cos(Dec[i]/57.3),R[i]*1.51,0,270,60); // TEllipse(椭圆在Pad中的横坐标,纵坐标,长轴,短轴,phimax,phimin,theta)
ellipse->SetFillStyle(0); // 设置PSF的填充风格为0,意为不填充,默认不填充
ellipse->SetLineWidth(2); // 设置PSF线宽为2
ellipse->SetLineColor(3); // 设置PSF的轮廓色为3,绿色
ellipse->Draw(); // 将PSF添加至画布
蜂巢图
void th2polyHoneycomb(){
TCanvas *C = new TCanvas("C", "C", 1200, 600);
C->Divide(2,1);
TH2Poly *hc1 = new TH2Poly();
hc1->Honeycomb(0, 0, .1, 5, 5);
hc1->SetTitle("Option V (default)");
hc1->SetStats(0);
hc1->Fill(.1, .1, 15.);
hc1->Fill(.4, .4, 10.);
hc1->Fill(.5, .5, 20.);
TH2Poly *hc2 = new TH2Poly();
hc2->Honeycomb(0, 0, .1, 5, 5, "h");
hc2->SetTitle("Option H");
hc2->SetStats(0);
hc2->Fill(.1, .1, 15.);
hc2->Fill(.4, .4, 10.);
hc2->Fill(.5, .5, 20.);
C->cd(1)->SetGrid();
hc1->Draw("colz L");
C->cd(2)->SetGrid();
hc2->Draw("colz L");
}
多边形
polygon
{
Double_t x[5] = {.2,.6,.7,.25,.2};
Double_t y[5] = {.5,.1,.9,.7,.5};
TPolyLine *pline = new TPolyLine(5,x,y);
pline->SetFillColor(38);
pline->SetLineColor(2);
pline->SetLineWidth(4);
pline->Draw("f");
pline->Draw();
}
地图
USAmap
void th2polyUSA()
{
Int_t i, bin;
const Int_t nx = 48;
const char *states [nx] = {
"alabama", "arizona", "arkansas", "california",
"colorado", "connecticut", "delaware", "florida",
"georgia", "idaho", "illinois", "indiana",
"iowa", "kansas", "kentucky", "louisiana",
"maine", "maryland", "massachusetts", "michigan",
"minnesota", "mississippi", "missouri", "montana",
"nebraska", "nevada", "new_hampshire", "new_jersey",
"new_mexico", "new_york", "north_carolina", "north_dakota",
"ohio", "oklahoma", "oregon", "pennsylvania",
"rhode_island", "south_carolina", "south_dakota", "tennessee",
"texas", "utah", "vermont", "virginia",
"washington", "west_virginia", "wisconsin", "wyoming"
};
Double_t pop[nx] = {
4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969,
9829211, 1545801, 12910409, 6423113, 3007856, 2818747, 4314113, 4492076,
1318301, 5699478, 6593587, 9969727, 5266214, 2951996, 5987580, 974989,
1796619, 2643085, 1324575, 8707739, 2009671, 19541453, 9380884, 646844,
11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,
24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270
};
TCanvas *usa = new TCanvas("USA", "USA");
usa->ToggleEventStatus();
Double_t lon1 = -130;
Double_t lon2 = -65;
Double_t lat1 = 24;
Double_t lat2 = 50;
TH2Poly *p = new TH2Poly("USA","USA Population",lon1,lon2,lat1,lat2);
TFile::SetCacheFileDir(".");
TFile *f = TFile::Open("http://root.cern.ch/files/usa.root", "CACHEREAD");
if (!f) {
printf("Cannot access usa.root. Is internet working ?\n");
return;
}
// Define the TH2Poly bins.
TMultiGraph *mg;
TKey *key;
TIter nextkey(gDirectory->GetListOfKeys());
while ((key = (TKey*)nextkey())) {
TObject *obj = key->ReadObj();
if (obj->InheritsFrom("TMultiGraph")) {
mg = (TMultiGraph*)obj;
bin = p->AddBin(mg);
}
}
// Fill TH2Poly.
for (i=0; i<nx; i++) p->Fill(states[i], pop[i]);
gStyle->SetOptStat(11);
p->Draw("colz textn");
}
Last updated