ROOTファイルを操作したい(TFile

1#include <TFile.h>
2
3TFile *fin = new TFile("input.root", "read");
4TH1D *h1 = (TH1D*)fin->Get("histogram_name");
5fin->Close();

TFileはROOTファイル(.root)の読み込みと書き込みを行うクラスです。 オブジェクト(ヒストグラム、TTree、キャンバスなど)を保存・復元できます。

1import ROOT
2
3fin = ROOT.TFile("input.root", "read")
4h1 = fin.Get("histogram_name")
5fin.Close()

メソッドのシグネチャ

1TFile(
2    const char *fname,
3    Option_t *option = "",
4    const char *ftitle = "",
5    Int_t compress = 505
6)

引数と戻り値

引数:

  • fname - ファイル名

  • option - ファイルアクセスモード(read, update, recreate, create)

  • ftitle - ファイルのタイトル説明(オプション)

  • compress - 圧縮レベル(デフォルト: 505)

戻り値:

  • TFileオブジェクト

ファイルアクセスモード

モード

説明

"read" または "r"

読み込みモード(ファイルが存在する必要があります)

"update" または "u"

既存ファイルを更新モード

"recreate" または "w"

新規作成(既存ファイルは上書き)

"create" または "c"

新規作成(既存ファイルはエラー)

主要なメソッド

  • Get(const char *namecycle) - 名前を指定してオブジェクトを取得

  • Write() - 現在のディレクトリに含まれるすべてのオブジェクトを書き込み

  • Close() - ファイルを閉じる

  • cd(const char *path = "") - ROOTのディレクトリを変更

  • cd(Int_t slot) - スロット番号を指定してディレクトリを変更

  • Flush() - バッファーをディスクに書き込み(ファイルは開いたまま)

ファイルを読み込みたい(TFile

1#include <TFile.h>
2
3TFile *fin = new TFile("input.root", "read");
4TH1D *h1 = (TH1D*)fin->Get("h1");
5fin->Close();

ROOTファイルから名前を指定してオブジェクトを取得します。 キャストして正しい型を指定することが重要です。

1import ROOT
2
3fin = ROOT.TFile("input.root", "read")
4h1 = fin.Get("h1")
5fin.Close()

ファイルに書き込みたい(TFile

1#include <TFile.h>
2
3TFile *fout = new TFile("output.root", "recreate");
4tree->Write();
5hist->Write();
6canvas->Write();
7fout->Close();

TFileを作成してから、各オブジェクトのWrite()メソッドを呼び出して保存します。 最後にClose()でファイルを閉じます。

1import ROOT
2
3fout = ROOT.TFile("output.root", "recreate")
4tree.Write()
5hist.Write()
6canvas.Write()
7fout.Close()

ファイルの有無を確認したい(TFile

 1#include <TFile.h>
 2#include <TSystem.h>
 3
 4TString ifn = "input.root";
 5FileStat_t info;
 6
 7if (gSystem->GetPathInfo(ifn.Data(), info) != 0) {
 8    printf("File '%s' does not exist.\n", ifn.Data());
 9} else {
10    printf("File exists. Size: %lld bytes\n", info.fSize);
11    TFile *fin = new TFile(ifn.Data(), "read");
12}

gSystem->GetPathInfo()を使用してファイルの存在確認とメタ情報取得ができます。 戻り値が0の場合はファイルが存在します。

FileStat_tの主要な要素

  • fSize - ファイルサイズ(バイト)

  • fMtime - 最終変更時刻(Unix timestamp)

  • fIsLink - シンボリックリンクかどうか(kTRUEまたはkFALSE

  • fDev - デバイスID

  • fUid - ユーザID

  • fGid - グループID

複数のファイルに書き込みたい(TFile

 1#include <TFile.h>
 2
 3TFile *fout1 = new TFile("output1.root", "recreate");
 4TFile *fout2 = new TFile("output2.root", "recreate");
 5
 6// fout2が現在のディレクトリ
 7tree1->Write();  // fout2に書き込まれる
 8
 9// fout1に切り替え
10fout1->cd();
11tree2->Write();  // fout1に書き込まれる
12
13fout1->Close();
14fout2->Close();

複数のTFileを開いている場合、cd()メソッドで対象ファイルを切り替えてからWrite()を実行します。 ROOTの内部にはディレクトリ構造があり、cd()でそのディレクトリに移動します。

 1import ROOT
 2
 3fout1 = ROOT.TFile("output1.root", "recreate")
 4fout2 = ROOT.TFile("output2.root", "recreate")
 5
 6tree1.Write()  # fout2に書き込まれる
 7
 8fout1.cd()
 9tree2.Write()  # fout1に書き込まれる
10
11fout1.Close()
12fout2.Close()

ファイルの上書き確認をしたい(TFile

 1#include <TFile.h>
 2#include <TSystem.h>
 3#include <cstdio>
 4
 5TString ofn = "output.root";
 6FileStat_t info;
 7
 8if (gSystem->GetPathInfo(ofn.Data(), info) == 0) {
 9    fprintf(stderr, "Error: File '%s' already exists.\n", ofn.Data());
10    fprintf(stderr, "Overwrite? [y/n] > ");
11
12    int readch = getchar();
13    while (readch != '\n' && readch != EOF) readch = getchar();
14
15    if (readch == 'y' || readch == 'Y') {
16        fprintf(stderr, "Overwriting '%s'.\n", ofn.Data());
17        TFile *fout = new TFile(ofn.Data(), "recreate");
18    } else {
19        fprintf(stderr, "Aborted.\n");
20        return 1;
21    }
22} else {
23    TFile *fout = new TFile(ofn.Data(), "recreate");
24}

ファイルがすでに存在する場合、ユーザーに上書き確認を促します。

ファイルパスの指定方法

相対パスと絶対パスが使用できます。

相対パス:

1TFile *f = new TFile("data/input.root", "read");

絶対パス(推奨):

1TFile *f = new TFile("/home/user/data/input.root", "read");

ホームディレクトリの指定(非推奨):

1// ~ はサポートされていません
2// 代わりに絶対パスを使用してください

関連するクラス

  • TTree - ROOT形式のデータツリー

  • TH1 - ヒストグラムクラス

  • TCanvas - 描画キャンバス

  • RDataFrame - 現代的なデータ分析フレームワーク

参考資料