部門 > ROOT > つくったヒストグラムをROOTファイルに書きこむ

ROOT User's Guide(邦訳)のInput/Outputの章に載っている。その最初のペイジから引用すると、
TObjArray Hlist(0);
// create an array of Histograms
TH1F* h;
// create a pointer to a histogram
// make and fill 15 histograms and add them to the object array
for (Int_t i = 0; i < 15; i++) {
sprintf(name,"h%d",i);
sprintf(title,"histo nr:%d",i);
h = new TH1F(name,title,100,-4,4);
Hlist.Add(h);
h->FillRandom("gaus",1000);
}
// open a file and write the array to the file
TFile f("demo.root","recreate");
Hlist->Write();
f.Close();
 
}
 
と書くことでdemo.rootというファイルに15のTH1Fクラスのヒストグラムを用意し、そこにガウシアンで乱数を入れることができる。ここの部分をFill(hogehoge)とすれば好きな変数のヒストグラムができる。

ちなみに、TFile f("demo.root","recreate");っていうのはHlistをWriteする直前がいいみたい。あんまり前でやっておくと、Segmentation violation になる気がする。なんでかは知らない。

タグ:

ROOT
最終更新:2011年11月08日 00:21