[ROOT] ガウス分布を描画するテンプレート

更新日:2022年11月20日(初版作成日:2021年10月17日)

サンプルヒストグラム ©SKラボラ

目的

シンプソンのパラドックスになるような分布のグラフを作る。
そのような分布で簡単にイメージできるのは、A高校(共学、緑線)、B高校(工業高校、黒線)の技術授業の成績分布である。
それぞれ、男子、女子の点数分布をガウス分布と仮定する。
一般的に、技術の授業は、男子(青系統の色で示す、女子は赤系統の色)の方が点数が高い。

このような状況では、「B高校の平均点は、A高校よりも高く」なるが、男子、女子別に見ると、「A高校生は、B高校生よりも平均点が高い」となる。

実行

% make
% ./sample

プログラムとMakefile

  1. #include "TApplication.h"
  2. #include "TCanvas.h"
  3. #include "TH1.h"
  4. #include "TF1.h"
  5. #include "TGraph.h"
  6. #include "TLatex.h"
  7. #include "TROOT.h"
  8. #include "TStyle.h"
  9.  
  10. int main (int argc, char **argv)
  11. {
  12. using namespace std;
  13. TApplication app("app", &argc, argv);
  14. //
  15. cout << "Drawing graphs." << endl;
  16.  
  17. gROOT->SetStyle("Plain");
  18. gStyle->SetFrameFillColor(0);
  19. gStyle->SetCanvasColor(0);
  20. gStyle->SetPaperSize(40, 52); // for pdf
  21. gStyle->SetPalette(1);
  22. //
  23. //
  24. //////////////////////////////////////////////////////////////////////////////////////
  25. // canvas size
  26. int canvas_y_size = 500;
  27. double silver_ratio = 1.4142;
  28. //
  29. TCanvas *c = new TCanvas("c", "c", 0, 0, (int)(double)canvas_y_size*silver_ratio, canvas_y_size);
  30. TPad *p = new TPad("p", "p", 0.05, 0.05, 1.0, 1.0);
  31. p->SetFillStyle(4000);
  32. p->Draw();
  33. p->cd();
  34. //
  35. //
  36. TF1 *f1 = new TF1("f1", "gaus", 0, 100);
  37. f1->SetParameters(9, 70, 5);
  38. f1->Draw();
  39.  
  40. f1->SetTitle("");
  41. c->Update();
  42. p->Update();
  43.  
  44. TAxis *x_axis = f1->GetXaxis();
  45. x_axis->SetLabelSize(0.03);
  46. x_axis->SetLabelOffset(0.02);
  47. TAxis *y_axis = f1->GetYaxis();
  48. y_axis->SetLabelSize(0.03);
  49.  
  50. //
  51. f1->SetLineWidth(10);
  52. f1->SetLineColor(1);
  53. f1->SetFillColor(7);
  54. f1->SetFillStyle(1001);
  55. f1->Draw("LF2");
  56. double int1 = f1->Integral(0, 100);
  57. cout << int1 << endl;
  58. //
  59. TF1 *f2 = new TF1("f1", "gaus", 0, 100);
  60. f2->SetParameters(5, 80, 5);
  61. f2->SetLineWidth(10);
  62. f2->SetLineColor(3);
  63. f2->SetFillColor(4);
  64. f2->SetFillStyle(1001);
  65. f2->Draw("LF2same");
  66.  
  67. TF1 *f3 = new TF1("f1", "gaus", 0, 100);
  68. f3->SetParameters(5, 40, 5);
  69. f3->SetLineWidth(10);
  70. f3->SetLineColor(3);
  71. f3->SetFillColor(2);
  72. f3->SetFillStyle(1001);
  73. f3->Draw("LF2same");
  74. double int3 = f3->Integral(0, 100);
  75. cout << int3 << endl;
  76.  
  77. TF1 *f4 = new TF1("f1", "gaus", 0, 100);
  78. f4->SetParameters(1, 30, 5);
  79. f4->SetLineWidth(10);
  80. f4->SetLineColor(1);
  81. f4->SetFillColor(6);
  82. f4->SetFillStyle(1001);
  83. f4->Draw("LF2same");
  84. double int4 = f4->Integral(0, 100);
  85. cout << int4 << endl;
  86.  
  87.  
  88. c->cd();
  89. TLatex *c_title = new TLatex (0.03, 0.95, Form("Points distribution"));
  90. c_title->Draw();
  91. //
  92. TLatex *x_axis_title = new TLatex (0.50, 0.03, Form("Points"));
  93. x_axis_title->Draw();
  94. //
  95. TLatex *y_axis_title = new TLatex (0.07, 0.31, Form("Number of students"));
  96. y_axis_title->SetTextAngle(90);
  97. y_axis_title->Draw();
  98.  
  99. c->Update();
  100. p->cd();
  101. p->Update();
  102. //
  103. app.Run();
  104. }

sample.cc

  1. source = sample.cc
  2. object = sample.o
  3. target = sample
  4.  
  5. CC = g++ -O2
  6. CFLAGS = -Wall
  7.  
  8. ROOTCONFIG := root-config
  9. ROOTCFLAGS := $(shell $(ROOTCONFIG) --cflags)
  10. ROOTLDFLAGS := $(shell $(ROOTCONFIG) --ldflags)
  11. ROOTLIBS := $(shell $(ROOTCONFIG) --libs)
  12. ROOTGLIBS := $(shell $(ROOTCONFIG) --glibs)
  13.  
  14. ALLROOTLIBS = $(ROOTLIBS) $(ROOTGLIBS) $(HASTHREAD)
  15. ALLROOTFLAGS = $(ROOTCFLAGS) $(ROOTLDFLAGS)
  16.  
  17. $target : $(object)
  18. $(CC) -o $(target) $(ALLROOTLIBS) -lRooFit -lRooFitCore -lFoam -lMinuit -lGui $(object)
  19.  
  20. $(object): $(source)
  21. $(CC) -c $(CFLAGS) $(ALLROOTFLAGS) $(source)
  22.  
  23. .PHONY: clean
  24. clean:
  25. rm $(target) $(object)

Makefile (コピペの場合は、行頭のタブに注意)

プログラムの備考

  1. TF1 *f1 = new TF1("f1", "gaus", 0, 100);
  2. f1->SetParameters(9, 70, 5);
  3. ...
  4. f1->SetLineWidth(10);
  5. f1->SetLineColor(1);
  6. f1->SetFillColor(7);
  7. f1->SetFillStyle(1001);
  8. f1->Draw("LF2");
  9. double int1 = f1->Integral(0, 100);

上記は、ガウス分布を描画する部分を引き抜いたもの。
一行目がガウス分布をx座標0から100の間で描画する、という宣言。
二行目は、そのガウス分布で使用するパラメータ、順にp0、p1、p2で、以下の定義。

  1. \begin{align}
  2. \large p_0 e^{-\frac{1}{2}(\dfrac{x - p_1}{p_2})^2} \tag{1}
  3. \end{align}

あとは、グラフのデコレーション。
順に線の長さ、色、塗りつぶしの色、スタイル。
LF2はline colorとfill color。詳しくは、以下のリンクを参照。

THistPainter Class Reference

参考

ROOT
TF1クラス(英語)
TColor(色情報)

改訂履歴

2022年11月20日:参考にROOTを追加、体裁調整
2022年10月25日:体裁調整
通し番号:11(管理用)

タイトルとURLをコピーしました