将字符串保存到 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;
告诉我,这个 Branch 的类型是 string
是否有从根树读取 std::string 的标准方法?
最佳答案
您正在调用 tTShowerHeader->GetBranch("fProjName")
-> 并编译。这意味着 tTShowerHeader->GetBranch()
的返回类型是一个指针。
此外,您正在该指针上调用 GetClassName()
并且它会编译,因此它是一个指向类类型的指针。
此外,std::string
确实不 有一个GetClassName()
方法,所以它不> 一个 std::string*
。确实如此seems it is TBranch *
.你必须 find appropriate method that will give you the text .
PS:忘记在 C++ 中使用 C 风格的转换。 C 风格的强制转换是邪恶的,因为它会根据恰好是什么类型做不同的事情。使用受限制的 static_cast
、dynamic_cast
、const_cast
或函数式转换(如果您确实需要,还可以使用 reinterpret_cast
,但这应该是极其罕见的)。
关于c++ - 在 TTree(cern 根)中写入/读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010587/