キャンバスの表示を更新したい(TCanvas::Update)
1#include <TCanvas.h>
2#include <TGraph.h>
3
4TCanvas *c = new TCanvas("c", "Canvas", 800, 600);
5TGraph *g = new TGraph();
6
7for (int i = 0; i < 100; i++) {
8 g->SetPoint(i, i, i * i);
9 if (i % 10 == 0) {
10 g->Draw("AL");
11 c->Update(); // キャンバスを更新して表示
12 }
13}
TCanvas::Updateでキャンバスの表示を強制的に更新できます。
ループ処理中にリアルタイムで描画を反映させたい場合に使います。
1from ROOT import TCanvas, TGraph
2
3c = TCanvas("c", "Canvas", 800, 600)
4g = TGraph()
5
6for i in range(100):
7 g.SetPoint(i, i, i * i)
8 if i % 10 == 0:
9 g.Draw("AL")
10 c.Update()
グラフを段階的に描画したい
1#include <TCanvas.h>
2#include <TGraph.h>
3#include <TSystem.h>
4#include <TMath.h>
5
6TCanvas *c = new TCanvas("c", "Canvas", 800, 600);
7TGraph *g = new TGraph();
8
9for (int i = 0; i < 100; i++) {
10 g->SetPoint(i, i * 0.1, TMath::Sin(i * 0.1));
11 if (i % 10 == 0) {
12 g->Draw("AL");
13 c->Update();
14 gSystem->Sleep(100); // 100ミリ秒待機
15 }
16}
17g->Draw("AL");
18c->Update();
gSystem->Sleep()と組み合わせることで、グラフが描かれていく様子を確認できます。
ヒストグラムを動的に更新したい
1#include <TCanvas.h>
2#include <TH1D.h>
3#include <TRandom.h>
4
5TCanvas *c = new TCanvas("c", "Canvas", 800, 600);
6TH1D *h = new TH1D("h", "Distribution", 100, -3, 3);
7h->Draw();
8
9for (int i = 0; i < 10000; i++) {
10 h->Fill(gRandom->Gaus(0, 1));
11 if (i % 1000 == 0) c->Update(); // 1000個ごとに更新
12}
13c->Update();
Updateの呼び出しが多すぎると処理が遅くなります。適切な間隔で呼ぶことが大切です。
アニメーションを作りたい
1#include <TCanvas.h>
2#include <TGraph.h>
3#include <TSystem.h>
4#include <TMath.h>
5
6TCanvas *c = new TCanvas("c", "Animation", 800, 600);
7TGraph *g = new TGraph();
8
9for (int frame = 0; frame < 100; frame++) {
10 g->Clear();
11 for (int i = 0; i < 50; i++) {
12 double x = i * 0.1;
13 g->SetPoint(i, x, TMath::Sin(x + frame * 0.1));
14 }
15 g->SetTitle(Form("Frame %d", frame));
16 g->Draw("AL");
17 c->Update();
18 gSystem->Sleep(16); // 約60fps
19}
関連メソッド
TCanvas - キャンバスの基本
TCanvas::Divide - キャンバスを分割する
TCanvas::Print - 複数ページのPDFに保存する