$ROOTSYS/root/tutorials
$ root -l macro.cc
または
$ root -l
root[0] .L macro.cc // .Lでマクロを読み込み macro()を定義
root[1] macro();
void test(int a, TH1D *hist){
cout << a << endl;
hist->Draw();
}
実行はTH1Dを持ったrootファイルを用意して
root -l test.root
hist->Draw();
int macro(int i){
std::cout << i << std::endl;
return 0;
}
引数の数字を標準出力
$root -l
root[0] .L macro(3)
$ g++ `root-config --cflags --libs` macro.cc -o macro
$ ./macro
cd $HOME
touch .rootlogon.C
.rootlogonを適宜編集。root開始時にcoutが実行される。
#.rootlogon.C
{cout << here is a test << endl}
TARGET = macro
SRCS = $(TARGET).cc
OBJS = $(TARGET).o
ROOTCFLAGS = $(shell root-config --cflags)
ROOTLIBS = $(shell root-config --libs)
ROOTGLIBS = $(shell root-config --glibs)
CXXFLAGS = $(ROOTCFLAGS) -Wall -fPIC
CXXLIBS = $(ROOTLIBS)
CC = g++
$(TARGET): $(OBJS)
$(CC) $(CXXLIBS) $(OBJS) -o $@
.cc.o:
$(CC) $(CXXFLAGS) -c $<
clean:
rm -f $(TARGET) $(OBJS)
これでmakeできる。
$ make
$ ./macro
expo —> exp([0]+[1]*x)
polN —> [0]+[1]*x+[2]*x**2+…+[N]*x**N
gaus —> [0]*exp(-0.5*( (x-[1])/[2])**2)
gausn —> [0]*exp(-0.5* ( (x-[1])/[2])**2) / (sqrt(2*pi)*[2]) )
Prerequisites for ROOTをひとまず全部入れる https://root.cern.ch/build-prerequisites
ROOTをインストールしようとしたらcmakeのバージョンが古いとのエラーメッセージ。cmakeはyum install cmakeでインストールしたが、そのままでは古い模様。
$cmake ../root-6.14.04/
CMake Error at CMakeLists.txt:3 (cmake_minimum_required):
CMake 3.4.3 or higher is required. You are running version 2.8.12.2
古いバージョンのcmakeを削除したのちcmakeの最新版を持ってきてインストール。
$ yum remove cmake
$ tar zxvf cmake-3.12.1.tar.gz
$ cd cmake-3.12.1
$ ./bootstrap --prefix=/usr/local
$ make
$ make install
$ emacs .bash_profile
PATH=/usr/local/bin
まっさらなサーバだったのでbootstrapしたときにc++のコンパイラがないetcが言われたので別途yumでインストール。
$ cmake --version
cmake version 3.12.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
ROOT6 (6.14) でrootを開くとiostreamあたりで下記のようなエラーが出た。
$ root
ERROR in cling::CIFactory::createCI(): cannot extract standard library include paths!
Invoking:
LC_ALL=C /Library/Developer/CommandLineTools/usr/bin/c++ -O2 -DNDEBUG -xc++ -E -v /dev/null 2>&1 >/dev/null | awk '/^#include </,/^End of search/{if (!/^#include </ && !/^End of search/){ print }}' | GREP_OPTIONS= grep -E "(c|g)\+\+"
Results was:
With exit code 256
input_line_1:1:10: fatal error: 'new' file not found
#include <new>
^~~~~
input_line_3:36:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
------------------------------------------------------------
| Welcome to ROOT 6.14/04 http://root.cern.ch |
| (c) 1995-2017, The ROOT Team |
| Built for macosx64 |
| From tag v6-14-14, 13 December 2017 |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------
input_line_9:1:10: fatal error: 'iostream' file not found
#include <iostream>
Xcode 9.4.1あたりのstringの取扱とROOT6との不一致で生まれるらしい。Xcode10にアップグレードして
$ xcode-select install
としたらエラーメッセージが消えた。 コマンドラインツールがインストールされてなかったのかもしれない。(ちなみにxcode9でコマンドラインツールをGUIからインストールしてもうまくいかなかった。)
大抵は環境変数が正しく設定されていないケース。
.bashrcに
source /usr/local/bin/thisroot.sh
TFile output("output.root","recreate");
TDirectory *sub = output.mkdir("sub");
sub->cd()
TH1D *h1 =....
h1->Fill(val);
output.Write();
サブディレクトリにアクセス
root[0] .ls
root [1] sub->cd()
root[2] h1->Draw()
TFile file;
file.Open("output.root");
TH1D *h1;
h=(TH1D*)gDirectory->Get("subdir/h1");
TCanvas *c1;
c1->Draw();
h->Draw();