JupyterLabでROOTカーネルを使う¶
JupyterLabを用いた計算で紹介した通り, Jupyter LabにROOTのカーネルを食わせるといつもコマンドラインで使っているインタプリタがJupyterLabで使える.
cp -r /root_installed_dir/etc/root/notebook/kernels/root $Users/Library/Jupyter/kernels
詳しくはここへ
これでROOTのカーネルが追加されているか確認するには,
jupyter kernelspec list
コマンドを使えばいい.
jupyter kernelspec list
Available kernels:
root /Users/fujiie/Library/Jupyter/kernels/root
wolframlanguage12.1 /Users/fujiie/Library/Jupyter/kernels/wolframlanguage12.1
python3 /usr/local/share/jupyter/kernels/python3
カーネルの削除は
jupyter kernelspec uninstall KERNEL_NAME
ちなみにこいつの素晴らしい点はClangが使えること.
つまりはc++のコードをインタープリタのように使える. ただ下手に書くと実行時にメモリーリークしてお亡くなりになるから注意が必要.
In [1]:
#include<sstream>
#include<iostream>
In [2]:
std::cerr<<"Hello World!"<<std::endl;
Hello World!
In [3]:
%jsroot on
TH1D *h1 = new TH1D("h1","h1",100,-5,5);
TF1 *f1 = new TF1("f1","[0]*TMath::Gaus(x,[1],[2])",-5, 5);
f1->SetParameters(1,0,1);
h1->FillRandom("f1", 1000);
TCanvas *canv = new TCanvas("canv","canv");
h1->Draw("eh");
h1->Fit(f1,"R");
canv->Draw();
**************************************** Minimizer is Minuit2 / Migrad Chi2 = 75.7525 NDf = 56 Edm = 2.20442e-08 NCalls = 143 p0 = 39.8365 +/- 1.69128 p1 = -0.0523074 +/- 0.0313946 p2 = -0.929008 +/- 0.0257699
適当にROOTのサンプルプログラムをコピペして動作確認してみる.
In [4]:
void fillrandom() {
TCanvas *c1 = new TCanvas("c1","The FillRandom example",200,10,700,900);
c1->Draw();
auto pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95);
auto pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45);
pad1->Draw();
pad2->Draw();
pad1->cd();
gBenchmark->Start("fillrandom");
//
// A function (any dimension) or a formula may reference
// an already defined formula
//
auto form1 = new TFormula("form1","abs(sin(x)/x)");
auto sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
sqroot->SetParameters(10,4,1,20);
pad1->SetGridx();
pad1->SetGridy();
pad1->GetFrame()->SetBorderMode(-1);
pad1->GetFrame()->SetBorderSize(5);
sqroot->SetLineColor(4);
sqroot->SetLineWidth(6);
sqroot->Draw();
auto lfunction = new TPaveLabel(5,39,9.8,46,"The sqroot function");
lfunction->Draw();
c1->Update();
//
// Create a one dimensional histogram (one float per bin)
// and fill it following the distribution in function sqroot.
//
pad2->cd();
pad2->GetFrame()->SetBorderMode(-1);
pad2->GetFrame()->SetBorderSize(5);
auto h1f = new TH1F("h1f","Test random numbers",200,0,10);
h1f->SetFillColor(45);
h1f->FillRandom("sqroot",10000);
h1f->Draw();
c1->Update();
//
// Open a ROOT file and save the formula, function and histogram
//
// TFile myfile("fillrandom.root","RECREATE");
// form1->Write();
// sqroot->Write();
// h1f->Write();
gBenchmark->Show("fillrandom");
}
In [5]:
%jsroot on
fillrandom();
fillrandom: Real Time = 0.02 seconds Cpu Time = 0.02 seconds
In [6]:
//final update
system("date '+%b %d %Y (%a)'");
12 27 2023 (水)
In [ ]: