Skip to content

Instantly share code, notes, and snippets.

@pn11
Created August 9, 2016 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pn11/e9d17de9fda37b91b1b730474d270a34 to your computer and use it in GitHub Desktop.
Save pn11/e9d17de9fda37b91b1b730474d270a34 to your computer and use it in GitHub Desktop.
TEfficiency Class in ROOT

TEfficiency Class in ROOT

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 したもの。これは便利そう。

Reference

TEfficiency

Open data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment