在 TTree(cern 根)中写入/读取字符串
将字符串保存到 TTree 后,
std::string fProjNameIn, fProjNameOut;
TTree *tTShowerHeader;
tTShowerHeader = new TTree("tTShowerHeader","Parameters of the Shower");
tTShowerHeader->Branch("fProjName",&fProjNameIn);
tTShowerHeader->Fill();
我尝试执行以下操作
fProjNameOut = (std::string) tTShowerHeader->GetBranch("fProjName");
,但无法编译,但
std::cout << tTShowerHeader->GetBranch("fProjName")->GetClassName() << std::endl;
告诉我,此分支的类型为 string
是否有一种标准方法可以从中读取 std::string根树?
after saving a string into a TTree
std::string fProjNameIn, fProjNameOut;
TTree *tTShowerHeader;
tTShowerHeader = new TTree("tTShowerHeader","Parameters of the Shower");
tTShowerHeader->Branch("fProjName",&fProjNameIn);
tTShowerHeader->Fill();
I'm trying to do the following
fProjNameOut = (std::string) tTShowerHeader->GetBranch("fProjName");
which does not compile, though
std::cout << tTShowerHeader->GetBranch("fProjName")->GetClassName() << std::endl;
tells me, this Branch is of type string
is there a standard way to read a std::string from a root tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案如下。
假设您有一个 ROOT 文件,并且想要将 std::string 保存到其中。
要访问它:
要打印到标准输出:
希望这会有所帮助。
The solution follows below.
Imagine you have a ROOT file and you want to save an std::string to it.
To access it:
To print to standard output:
Hope this helps.
您正在调用
tTShowerHeader->GetBranch("fProjName")
-> 并且它会编译。这意味着tTShowerHeader->GetBranch()
的返回类型是一个指针。此外,您在该指针上调用
GetClassName()
并进行编译,因此它是指向类类型的指针。更重要的是,
std::string
确实没有有GetClassName()
方法,因此它不是 <代码>std::string*。事实上,它似乎是TBranch *
。您必须找到合适的方法来提供文本。PS:忘记在 C++ 中使用 C 风格的强制转换。 C 风格的强制转换是邪恶的,因为它会根据不同的类型做不同的事情。使用受限制的
static_cast
、dynamic_cast
、const_cast
或函数式类型转换(如果确实需要,还可以使用reinterpret_cast
,但这应该是极其罕见的)。You are calling
tTShowerHeader->GetBranch("fProjName")
-> and it compiles. That means that return type oftTShowerHeader->GetBranch()
is a pointer.Moreover, you are calling
GetClassName()
on that pointer and it compiles, so it's a pointer to a class type.Even more, the
std::string
does not have aGetClassName()
method, so it's not astd::string*
. Indeed, it seems it isTBranch *
. You must find appropriate method that will give you the text.PS: Unlearn to use C-style cast in C++. C-style cast is evil, because it will do different things depending on what the type happens to be. Use the restricted
static_cast
,dynamic_cast
,const_cast
or function-style casts instead (andreinterpret_cast
if you really need that, but that should be extremely rare).好吧,这花了一段时间,但我弄清楚了如何从树中获取信息。您不能直接返回信息,只能通过给定的变量返回信息。
在 root 中,TTree 类存储用于输入的变量的地址。使用 GetEntry() 将使用存储在 TTree 中的信息填充同一变量。
您还可以使用 tTSowerHeader->Print() 显示每个分支的整体数量。
Ok, this took a while but I figured out how to get the information from the tree. You cannot directly return the information, it can only be returned through the variable it was given in.
In root the TTree class stores the address to the varriable used for input into it. Using GetEntry() will fill the same variable with the information stored in the TTree.
You can also use tTShowerHeader->Print() to display the number of entires for each branch.