ROOT TTree中的Python字符串数组
发布时间:2020-12-13 22:55:00 所属栏目:Linux 来源:网络整理
导读:我正在与CERN的pyROOT模块做一些工作,我正在尝试将一个字符串数组存储为二叉树中的一个叶子.为了做到这一点,我必须传递一个数组,显然,使用不是列表或字典,而是数组模块.该模块支持标准C数组,字符,整数等,但有没有人知道我可以嵌套它们的方式,以便有一个字符
我正在与CERN的pyROOT模块做一些工作,我正在尝试将一个字符串数组存储为二叉树中的一个叶子.为了做到这一点,我必须传递一个数组,显然,使用不是列表或字典,而是数组模块.该模块支持标准C数组,字符,整数等,但有没有人知道我可以嵌套它们的方式,以便有一个字符串数组,或者,有效地,一个字符数组数组?或者我走得太远了,我需要从键盘退一步:)?
码: import ROOT rowtree = ROOT.TTree("rowstor","rowtree") ROOT.gROOT.ProcessLine( "struct runLine { Char_t test[20]; Char_t test2[20]; };" ); from ROOT import runLine newline = runLine() rowtree.Branch("test1",newline,"test/C:test2") newline.test = ["AbcDefgHijkLmnOp","aaaaaaaaaaaaaaaaaaa"] rowtree.Fill() 错误: python branchtest Traceback (most recent call last): File "branchtest",line 14,in <module> newline.test = ["AbcDefgHijkLmnOp","aaaaaaaaaaaaaaaaaaa"] TypeError: expected string or Unicode object,list found 我想知道是否可以将此示例中显示的列表转换为字符串数组. 解决方法
char数组和Python字符串的Python列表是两个非常不同的东西.
如果你想要一个包含char数组(一个字符串)的分支,那么我建议使用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包(也参见存储库on 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′)的输出是:*********************************************************************** * Row * Instance * char_array * strings * *********************************************************************** * 0 * 0 * aaaaaaaaaaaaaaaaaaaa * Hello * * 0 * 1 * aaaaaaaaaaaaaaaaaaaa * world! * *********************************************************************** (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |