在 TTree(cern 根)中写入/读取字符串

发布于 2024-11-29 00:15:26 字数 607 浏览 0 评论 0原文

将字符串保存到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名。 2024-12-06 00:15:26

解决方案如下。

假设您有一个 ROOT 文件,并且想要将 std::string 保存到其中。

TTree * a_tree = new TTree("a_tree_name");
std::string a_string("blah");
a_tree->Branch("str_branch_name", &a_string); // at this point, you've saved "blah" into a branch as an std::string

要访问它:

TTree * some_tree = (TTree*)some_file->Get("a_tree_name");
std::string * some_str_pt = new std::string(); 
some_tree->SetBranchAddress("str_branch_name", &some_str_pt);

some_tree->GetEntry(0);

要打印到标准输出:

std::cout << some_str_pt->c_str() << std::endl;

希望这会有所帮助。

The solution follows below.

Imagine you have a ROOT file and you want to save an std::string to it.

TTree * a_tree = new TTree("a_tree_name");
std::string a_string("blah");
a_tree->Branch("str_branch_name", &a_string); // at this point, you've saved "blah" into a branch as an std::string

To access it:

TTree * some_tree = (TTree*)some_file->Get("a_tree_name");
std::string * some_str_pt = new std::string(); 
some_tree->SetBranchAddress("str_branch_name", &some_str_pt);

some_tree->GetEntry(0);

To print to standard output:

std::cout << some_str_pt->c_str() << std::endl;

Hope this helps.

水波映月 2024-12-06 00:15:26

您正在调用 tTShowerHeader->GetBranch("fProjName")-> 并且它会编译。这意味着tTShowerHeader->GetBranch()的返回类型是一个指针

此外,您在该指针上调用 GetClassName() 并进行编译,因此它是指向类类型的指针。

更重要的是,std::string 确实没有GetClassName() 方法,因此它不是 <代码>std::string*。事实上,它似乎是TBranch *。您必须找到合适的方法来提供文本

PS:忘记在 C++ 中使用 C 风格的强制转换。 C 风格的强制转换是邪恶的,因为它会根据不同的类型做不同的事情。使用受限制的 static_castdynamic_castconst_cast 或函数式类型转换(如果确实需要,还可以使用 reinterpret_cast ,但这应该是极其罕见的)。

You are calling tTShowerHeader->GetBranch("fProjName")-> and it compiles. That means that return type of tTShowerHeader->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 a GetClassName() method, so it's not a std::string*. Indeed, it seems it is TBranch *. 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 (and reinterpret_cast if you really need that, but that should be extremely rare).

一瞬间的火花 2024-12-06 00:15:26

好吧,这花了一段时间,但我弄清楚了如何从树中获取信息。您不能直接返回信息,只能通过给定的变量返回信息。

std::string fProjNameIn,  fProjNameOut;
TTree *tTShowerHeader;

fProjnameIn = "Jones";
tTShowerHeader = new TTree("tTShowerHeader","Parameters of the Shower");
tTShowerHeader->Branch("fProjName",&fProjNameIn);
tTShowerHeader->Fill();//at this point the name "Jones" is stored in the Tree

fProjNameIn = 0;//VERY IMPORTANT TO DO (or so I read)
tTShowerHeader->GetBranch("fProjName")->GetEntries();//will return the # of entries
tTShowerHeader->GetBranch("fProjName")->GetEntry(0);//return the first entry
//At this point fProjNameIn is once again equal to "Jones"

在 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.

std::string fProjNameIn,  fProjNameOut;
TTree *tTShowerHeader;

fProjnameIn = "Jones";
tTShowerHeader = new TTree("tTShowerHeader","Parameters of the Shower");
tTShowerHeader->Branch("fProjName",&fProjNameIn);
tTShowerHeader->Fill();//at this point the name "Jones" is stored in the Tree

fProjNameIn = 0;//VERY IMPORTANT TO DO (or so I read)
tTShowerHeader->GetBranch("fProjName")->GetEntries();//will return the # of entries
tTShowerHeader->GetBranch("fProjName")->GetEntry(0);//return the first entry
//At this point fProjNameIn is once again equal to "Jones"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文