Ntupleを使ってみた。

インタプリタモードで条件に合わせてグラフをプロット出来るクラス。Ntupleを使ってみた。参考は前回と一緒。

ソースは以下

  1 #include "TApplication.h"
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <math.h>
  5 #include <TROOT.h>
  6 #include <TStyle.h>
  7 #include <TNtuple.h> /* header file for Ntuple */
  8 #include <TFile.h>
  9
 10 int main(int argc, char **argv){
 11     //TApplication theApp("App", &argc, argv);
 12
 13     gROOT->SetStyle("Plain");
 14     gStyle->SetOptStat("neiuoMR");
 15     gStyle->SetPalette(1);
 16     //
 17     double N; /* factor */
 18     double mu; /* mean */
 19     double sigma; /* standard deviation */
 20     //
 21     double x1;
 22     double x1_max, x1_min;
 23     double x2;
 24     double x2_max, x2_min;
 25     double x3;
 26     double x3_max, x3_min;
 27
 28     double y1_ref; /* reference value */
 29     double y1_try; /* trial value */
 30     double y3_ref; /* reference value */
 31     double y3_try; /* trial value */
 32
 33     mu= 1.0;
 34     sigma= 0.5;
 35     N= 1./ sqrt(2.*M_PI) / sigma;
 36     //
 37     x1_max=+3.0;
 38     x1_min=-1.0;
 39     x2_max=+20.;
 40     x2_min=-15.;
 41     x3_max=+3.0;
 42     x3_min=-1.0;
 43     //
 44     TNtuple *nt;
 45     nt= new TNtuple("nt","ntuple sample", "x1:x2:x3");
 46     //
 47     for(int i=0; i<1e5; i++){
 48     x1=(double)rand()/RAND_MAX * (x1_max - x1_min) + x1_min;
 49     y1_ref= N * exp(-(x1-mu)*(x1-mu)/(2.*sigma*sigma));
 50     y1_try= (double)rand()/RAND_MAX * N;
 51     //
 52     x3=(double)rand()/RAND_MAX * (x1_max - x1_min) + x1_min;
 53     y3_ref= N * exp(-(x3-mu)*(x3-mu)/(2.*sigma*sigma));
 54     y3_try= (double)rand()/RAND_MAX * N;
 55     //
 56     x2=(double)rand()/RAND_MAX * (x2_max - x2_min) + x2_min;
 57     //
 58     if(y1_try<y1_ref && y3_try<y3_ref){
 59         nt->Fill(x1, x2, x3);
 60     }/* if(y_try */
 61     //
 62     }/* for(int i */
 63
 64     TFile *rootfile= new TFile("Ntuple.root","RECREATE","HogeNT output file");
 65     nt->Write();
 66     rootfile->Close();
 67     //
 68     return 0;
 69 }

三次元のプロットを作ってみた。
同じようにmakeし、.rootファイルを作ってみる。

インタプリタモードでは、

root [] nt->Draw(“x1”); <enter>
とか、
root [] nt->Draw(“x1:x2”); <enter>
とか、
root [] nt->Draw(“x1:x2:x3”); <enter>
とかとか。

root [] nt->Draw(“x1:x3”,“x1>1”); <enter>
なんて、条件を引数にとることもできる。
さらに、
root [] nt->Draw(“x1:x2”, “x1>1”, “lego2”); <enter>
とかとか、プロットオプションも第三引数に指定することもできるので、本当いろいろ出来るのである。