Skip to content

Instantly share code, notes, and snippets.

@pn11
Last active June 1, 2019 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pn11/87ef828155a36a582abf to your computer and use it in GitHub Desktop.
Save pn11/87ef828155a36a582abf to your computer and use it in GitHub Desktop.
ROOTで、自分つくったクラスをTTreeに入れるデモ

自分つくったクラスをTTreeに入れるデモ

Macのみで動作確認。

ファイルリスト

使い方

make myclass

myclass.soができる。

make test

testができるので、

./test

で実行するとtest.rootができる。

できあがったROOT fileを見るにはlibmyclass.soをロードする必要がある。ROOTを起動してから、

.L libmyclass.so;
TFile *f = new TFile("test.root");
tree.Draw("myclass->GetTest1():myclass->GetTest2()");

などとすることでクラスの中身にアクセスできる。

参考リンク

コメント

ググってもちょうど良いものが見つからず苦労した。ライブラリファイルをつくってインクルードしてmakeするのがみそ。-lmyclassオプションでできるはずだけどよくわからないからそのうち。

CXX = g++
CXXFLAGS = -O2 -pipe -Wall -W -Woverloaded-virtual
ROOTCONFIG = root-config
ROOTCFLAGS:= $(shell $(ROOTCONFIG) --cflags)
ROOTLIBS := $(shell $(ROOTCONFIG) --libs)
ROOTGLIBS := $(shell $(ROOTCONFIG) --glibs)
CXXFLAGS+= $(ROOTCFLAGS)
CXXFLAGS+= -g
LIBS = $(ROOTLIBS)
test: test.cc myclass.hh libmyclass.so
$(CXX) $(CXXFLAGS) $(LIBS) $^
mv a.out $@
myclass:
rootcint -f $(@)Dict.cxx -c $(@).hh $(@)LinkDef.hh
$(CXX) `root-config --cflags --libs` -shared $(@)Dict.cxx $(@).cc -o lib$(@).so
#include "myclass.hh"
ClassImp(MyClass);
MyClass::MyClass()
:test1(-1), test2(-1)
{
}
MyClass::~MyClass()
{
}
int MyClass::GetTest1()
{
return test1;
}
int MyClass::GetTest2()
{
return test2;
}
void MyClass::SetTest1(int test)
{
test1 = test;
}
void MyClass::SetTest2(int test)
{
test2 = test;
}
#include <TObject.h>
#include <RooInt.h>
class MyClass: public TObject
{
private:
int test1;
int test2;
public:
MyClass();
~MyClass();
void SetTest1(int test);
void SetTest2(int test);
int GetTest1();
int GetTest2();
ClassDef(MyClass,1);
};
#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class MyClass;
#endif
#include <iostream>
using namespace std;
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TClassTable.h>
#include <TSystem.h>
#include "myclass.hh"
int main(int argc, char** argv)
{
TFile *fout = new TFile("test.root", "recreate");
fout->cd();
TTree *tree = new TTree("tree", "tree");
int eventid;
MyClass *myclass = new MyClass::MyClass();
tree -> SetMaxVirtualSize(518*1024*1024/*byte*/);
tree->Branch("myclass", "MyClass", &myclass, 32000, 2);
// https://root.cern.ch/root/html/TTree.html#TTree:Branch@7
tree->Branch("eventid", &eventid, "eventid/I");
for (int iev = 0; iev < 1000; iev++){// eventloop
myclass = new MyClass::MyClass();
eventid = iev;
myclass->SetTest1(eventid*2);
myclass->SetTest2(eventid*10);
tree->Fill();
delete myclass;
} // event loop
fout->Write();
fout->Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment