HOMEROOT
見栄えを整える

●フォントの設定

基本的にはDrawした後ではなく、root起動直後(Canvas生成前)に設定するのがよい。
root [2] int fontid=132;
root [3] gStyle->SetStatFont(fontid);
root [4] gStyle->SetLabelFont(fontid,"XYZ");
root [5] gStyle->SetLabelFont(fontid,"");
root [6] gStyle->SetTitleFont(fontid,"XYZ");
root [7] gStyle->SetTitleFont(fontid,"");
root [8] gStyle->SetTextFont(fontid);
root [9] gStyle->SetLegendFont(fontid);
fontidはフォント番号で一覧はここ → https://root.cern.ch/doc/master/classTAttText.html
Drawしたあとに設定を反映する場合は

root [10] c1->UseCurrentStyle();

とすれば出来るがうまくいかないこともある

1行にまとめたversion
int fontid=132; gStyle->SetStatFont(fontid); gStyle->SetLabelFont(fontid,"XYZ"); gStyle->SetLabelFont(fontid,""); gStyle->SetTitleFont(fontid,"XYZ"); gStyle->SetTitleFont(fontid,""); gStyle->SetTextFont(fontid); gStyle->SetLegendFont(fontid);

その他はこの辺を参照→ https://root.cern.ch/doc/master/classTLatex.html

●凡例の設置

leg = new TLegend(0.5, 0.6, 0.89, 0.89,"Legend");
leg.AddEntry(gr1,"Coherent Scattering","l");
leg.AddEntry(gr2,"Incoherent Scattering","l");
leg.AddEntry(gr3,"Photoelectric Absorption","l");
leg.AddEntry(gr4,"Pair Prod. in N Field","l");
leg.AddEntry(gr5,"Pair Prod. in E Field","l");
leg.AddEntry(gr6,"Total Attenuation","l");
leg.Draw();
参考
https://www-he.scphys.kyoto-u.ac.jp/member/n-kota/dokuwiki/doku.php?id=ja:root:tlegend
http://d.hatena.ne.jp/teruaki_enoto/20100817/1282077668



●軸タイトルの設定

frame->SetTitle("Ge Photon Cross Section;Photon Energy (MeV);Cross Section (cm^{2}/g);");

位置の調整はよくわからんがこれで出来る

frame->GetYaxis()->SetTitleOffset(1.3);
frame->GetXaxis()->SetTitleOffset(1.3);

frameはヒストグラム名。

●文字入力

ギリシャ文字は#etaなど#をつける。上付き^{2},下付き_{a}

ヒストグラム中に文字を書き込む
TLatex latex; latex.SetTextAlign(11); latex.DrawLatex(50,-1.15,"#scale[5]{#it{#beta}3}");
latex.SetTextAlign(11) の数字部分についてはここを参照
http://nucl.phys.s.u-tokyo.ac.jp/niikura/index.php?id=root-fonts


●ヒストグラム中に点を打つ

root [2] TGraph *points = new TGraph;
root [3] points->SetPoint(0 , 23.20144, -24.50345);
root [4] points->SetMarkerSize(5);
root [5] points->SetMarkerColor(1);
root [6] points->SetMarkerStyle(5);
root [7] points->Draw("P");
styleの種類は https://root.cern.ch/doc/master/classTAttMarker.html

●2次元ヒストグラムのz軸の色表現をグラデーションにする

https://www-he.scphys.kyoto-u.ac.jp/member/n-kota/dokuwiki/doku.php?id=ja:root:color#creategradientcolortable

const Int_t NRGBs = 5; const Int_t NCont = 255;Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };Double_t Red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 }; Double_t Green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };Double_t Blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 }; TColor::CreateGradientColorTable(NRGBs, stops, Red, Green, Blue, NCont);gStyle->SetNumberContours(NCont);

●軸の範囲を変更する

hist->GetXaxis()->SetRangeUser(first,last);

z軸を1から最大値にする場合
h2->GetZaxis()->SetRangeUser(1,h2->GetMaximum());//簡易版
h2->GetZaxis()->SetRangeUser(1,h2->GetBinContent(h2->GetMaximumBin()));//厳密版

●キャンバスの生成、TGraph用フレームの生成

TCanvas *c2 = new TCanvas("c2", "c2", 800+2, 900+27);
TH1F *frame = gPad->DrawFrame( -40, 0, 40, 90);
// 括弧内は ( 横軸 min, 縦軸 min , 横軸 max, 縦軸 max ) の順。


●TreeからTGraphを作る

参考: https://www-he.scphys.kyoto-u.ac.jp/member/n-kota/dokuwiki/doku.php?id=ja:root:wantto9

tree->Draw("X:Y",Cut);
const Int_t N = tree->GetSelectedRows();
TGraph *graph = new TGraph(N, tree->GetV2(), tree->GetV1());

※ 例えばdataというtreeがあった時に、この見本をコピペして使うためには、
TTree *tree = data;
とすればよい。


●GRETINA Mode2の反応位置分布をプロットする

int fontid=132; gStyle->SetStatFont(fontid); gStyle->SetLabelFont(fontid,"XYZ"); gStyle->SetLabelFont(fontid,""); gStyle->SetTitleFont(fontid,"XYZ"); gStyle->SetTitleFont(fontid,""); gStyle->SetTextFont(fontid); gStyle->SetLegendFont(fontid); //フォント設定 const Int_t NRGBs = 5; const Int_t NCont = 255;Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };Double_t Red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 }; Double_t Green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };Double_t Blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 }; TColor::CreateGradientColorTable(NRGBs, stops, Red, Green, Blue, NCont);gStyle->SetNumberContours(NCont); //z軸のグラデーション TCanvas *c2 = new TCanvas("c2", "c2",900+2,800+27); //xz又はyz面用(横長) Tmode2->Draw("Y:Z>>h1(180,0,90,160,-40,40)","","colz"); TH2F *h2= h1; h2->GetZaxis()->SetRangeUser(1,h2->GetBinContent(h2->GetMaximumBin()));h2->SetStats(0);h2->SetTitle(0);h2->Draw("colz");gPad->Update();gPad->SetLogz(1);TPaletteAxis *pal= (TPaletteAxis*)h2->GetListOfFunctions()->FindObject("palette");pal->SetX2NDC(0.93);h2->SetTickLength(0.015,"Z"); h2->SetTitle(";#font[12]{z} [mm];#font[12]{y} [mm];");h2->Draw("colz");


●テンプレートマクロ

{
//開くファイル
TFile *_file0 = TFile::Open("../NS18/anadir/q2p1_basis_xt.root");

//基本の線の太さ
int dLineWid=1;

gStyle->SetLineWidth(dLineWid);
gStyle->SetGridWidth(dLineWid);

//基本フォント設定
Int_t fontid=132; //論文用 132、スライド用 62
Int_t italic=12; //イタリック用フォント 通常12 太文字32

gStyle->SetStatFont(fontid); 
gStyle->SetLabelFont(fontid,"XYZ"); 
gStyle->SetLabelFont(fontid,""); 
gStyle->SetTitleFont(fontid,"XYZ"); 
gStyle->SetTitleFont(fontid,""); 
gStyle->SetTextFont(fontid); 
gStyle->SetLegendFont(fontid); 

//z軸のグラデーション
const Int_t NRGBs = 3; 
const Int_t NCont = 240;
Double_t stops[NRGBs] = { 0.00, 0.5, 1};
Double_t Red[NRGBs]   = { 0.00, 0.00, 0.87}; 
Double_t Green[NRGBs] = { 0.00, 0.81, 1.00};
Double_t Blue[NRGBs]  = { 0.51, 1.00, 0.12}; 
TColor::CreateGradientColorTable(NRGBs, stops, Red, Green, Blue, NCont);
gStyle->SetNumberContours(NCont); 

//キャンバスの準備
TCanvas *c2 = new TCanvas("c2", "c2", 700+2, 700+27);

//フレームの枠線の太さ
c2->SetFrameLineWidth(dLineWid);

//キャンバスの余白、分割設定
//c2->SetMargin(0,0,0,0);
//c2->Divide(3,3,0,0);
//c2->cd(1)->SetGrid();

//ヒストグラムのDraw
data->Draw("Y:X>>h1(160,-40,40,260,-40,40)","k==1&&Seg<6","L");

//スタイルの設定
h1->SetMarkerStyle(7);
h1->SetMarkerColor(2);

TH2F *hist= h1; //名前の変更

//軸ラベル(目盛りの数字)の大きさ
hist->SetLabelSize(0.045,"xyz");

//軸タイトルの位置・大きさ
hist->GetXaxis()->SetTitleOffset(1.05);
hist->GetYaxis()->SetTitleOffset(1.05);
hist->SetTitleSize(0.045,"xy");

//軸タイトル内容(全体,x, y, z)
hist->SetTitle(Form(";#font[%d]{x} [mm];#font[%d]{y} [mm];",italic,italic));

hist->SetStats(0);

hist->Draw("L");

//hist->GetXaxis()->SetNdivisions(303,false);//目盛間隔
//hist->GetYaxis()->SetNdivisions(404);//目盛間隔

//保存ファイル名、コピー時書き換え忘れ注意
//c2->SaveAs("outimg/wfExam1.pdf");

}


●フォント埋め込みPDFを作る

ROOTの機能としてフォント埋め込みが実は出来るらしいが、どうもうまくいかなかった。 (生成されたPDFファイルが正常に再生されなかった)
うまくいった方法は、Inkscapeを使うやり方で、はじめにROOTでsvg形式で作って、

inkscape -f filename.svg -A filename.pdf -z

とすればフォント埋め込みPDFが出来る。 数式とかギリシャ文字を使うときはやっといたほうが良いかも。 マクロ中で一括でやってもらうには、
c2->SaveAs("filename.svg"); #include <stdlib.h> system("inkscape -f filename.svg -A filename.pdf -z");
とかにすればよい。

●HTML中にROOTの図を載せる

ROOTの図をベクタ形式のままHTMLに貼るにはSVG形式で保存、 挿入タグは
<object type="image/svg+xml" data="unnamed.svg" width="698" height="473"></object>
SVGは古いブラウザには対応していないらしいので、もしかしたら一部の人には見えないかもしれないというのと、 ブラウザ側の解釈で若干表示が変わる可能性があることに注意。(特に文字部分)

unnamed.svg


比較用 unnamed.gif