char 配列と Python 文字列の Python リストは、2 つのまったく異なるものです。
char 配列 (1 つの文字列) を含むブランチが必要な場合は、Python の組み込みbytearray
型を使用することをお勧めします。
import ROOT
# create an array of bytes (chars) and reserve the last byte for null
# termination (last byte remains zero)
char_array = bytearray(21)
# all bytes of char_array are zeroed by default here (all b'\x00')
# create the tree
tree = ROOT.TTree('tree', 'tree')
# add a branch for char_array
tree.Branch('char_array', char_array, 'char_array[21]/C')
# set the first 20 bytes to characters of a string of length 20
char_array[:21] = 'a' * 20
# important to keep the last byte zeroed for null termination!
tree.Fill()
tree.Scan('', '', 'colsize=21')
の出力tree.Scan('', '', 'colsize=21')
は次のとおりです。
************************************
* Row * char_array *
************************************
* 0 * aaaaaaaaaaaaaaaaaaaa *
************************************
したがって、ツリーがバイトを正しく受け入れていることがわかります。
文字列のリストを保存したい場合は、次を使用することをお勧めしますstd::vector<std::string>
。
import ROOT
strings = ROOT.vector('string')()
tree = ROOT.TTree('tree', 'tree')
tree.Branch('strings', strings)
strings.push_back('Hello')
strings.push_back('world!')
tree.Fill()
tree.Scan()
の出力tree.Scan()
は次のとおりです。
***********************************
* Row * Instance * strings *
***********************************
* 0 * 0 * Hello *
* 0 * 1 * world! *
***********************************
ループstrings.clear()
では、次のエントリに文字列の新しいリストを入力する前に行う必要があります。
現在、rootpyパッケージ ( githubのリポジトリも参照) は、Python でツリーを作成するためのより優れた方法を提供します。以下は、rootpy を使用して「より使いやすい」方法で char 配列を使用する方法の例です。
from rootpy import stl
from rootpy.io import TemporaryFile
from rootpy.tree import Tree, TreeModel, CharArrayCol
class Model(TreeModel):
# define the branches you want here
# with branchname = branchvalue
char_array = CharArrayCol(21)
# the dictionary is compiled and cached for later
# if not already available
strings = stl.vector('string')
# create the tree inside a temporary file
with TemporaryFile():
# all branches are created automatically according to your model above
tree = Tree('tree', model=Model)
tree.char_array = 'a' * 20
# attemping to set char_array with a string of length 21 or longer will
# result in a ValueError being raised.
tree.strings.push_back('Hello')
tree.strings.push_back('world!')
tree.Fill()
tree.Scan('', '', 'colsize=21')
の出力tree.Scan('', '', 'colsize=21')
は次のとおりです。
***********************************************************************
* Row * Instance * char_array * strings *
***********************************************************************
* 0 * 0 * aaaaaaaaaaaaaaaaaaaa * Hello *
* 0 * 1 * aaaaaaaaaaaaaaaaaaaa * world! *
***********************************************************************
TreeModel
ここで、rootpy で s を使用する別の例を参照してください。
https://github.com/rootpy/rootpy/blob/master/examples/tree/model_simple.py