ROOT に TEfficiency というクラスがあることを知った。使い方は 公式リファレンス に書いてあるけど、日本語で書いてある情報が見つからなかったので軽くまとめておく。
これまでカット前のヒストグラム TH1 *horg
とカット後の TH1 *hcut
があったとき、Clone して Divide してとかやっていたけど、TEfficiency を使うと以下の様に書けるので、少し楽かも。
TEfficiency *heff;
if (TEfficiency::CheckConsistency(*hcut, *horg, "w")){
heff = new TEfficiency(*hcut, *horg);
heff->SetTitle("Fraction of female;Age;Fraction");
}
注意するべきなのは TEfficiency (const TH1 &passed, const TH1 &total)
と CheckConsistency (const TH1 &pass, const TH1 &total, Option_t *opt="")
の引数 (参照渡し?)。
実際に遊んでみる。政府のページ からオープンデータを適当に探したら、鯖江市の人口分布 が見つかったのでそれを使う。上に書いたコードのように TEfficiency::CheckConsistency()
で各ビンの値がカット前とカット後で値が逆転してないかとかチェックするんだけど、元データに1行変なのが紛れ込んでいたので、オーバーフローに変な値が入っていて最初動かなかった。
if (line.substr(0, 3) == "334") continue;
を加えて特定の行をスキップして解決。コードは以下。
void fillHistos(TH1 *hcut, TH1 *horg);
void teff() {
TEfficiency *heff;
TH1I *hcut = new TH1I("hcut", "after cut (female only)", 120, 0, 120);
TH1I *horg = new TH1I("horg", "before cut", 120, 0, 120);
fillHistos(hcut, horg);
if (TEfficiency::CheckConsistency(*hcut, *horg, "w")){
heff = new TEfficiency(*hcut, *horg);
heff->SetTitle("Fraction of female;Age;Fraction");
}
// Drawing
TH1I *haxis = new TH1I("haxis", "Population in Sabae city (black: total, red: female);Age;Number", 120, 0, 120);
haxis->SetMaximum(1500);
haxis->SetMinimum(0);
TCanvas *c1 = new TCanvas("c1", "c1");
c1->Divide(1,2);
c1->cd(1);
haxis->Draw();
horg->Draw("samesh");
hcut->Draw("samesh");
c1->cd(2);
heff->Draw();
}
void fillHistos(TH1 *hcut, TH1 *horg){
hcut->Sumw2();
horg->Sumw2();
hcut->SetLineColor(kRed);
horg->SetLineColor(kBlack);
ifstream fin("chikunenreibetujinko.txt");
string line;
while (getline(fin, line)){
if (line.substr(0, 1) == "#") continue;
if (line.substr(0, 3) == "334") continue; // ignore strange line
int iline, area, age, nMale, nFemale, nTotal;
string areaname;
istringstream iss;
iss.str(line);
iss >> iline >> area >> areaname >> age >> nMale >> nFemale >> nTotal;
for (int i = 0; i< nTotal; i++) {horg->Fill(age);}
for (int i = 0; i< nFemale; i++) {hcut->Fill(age);}
}
}
下が TEfficiency を plot したもの。これは便利そう。