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

时间:2020-12-26 12:40:11

after saving a string into a TTree

在将一个字符串保存为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?

是否有一种标准的方法来读取std::根目录树中的字符串?

3 个解决方案

#1


1  

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.

在root中,TTree类将地址存储到用于输入的varriable中。使用GetEntry()将用存储在TTree中的信息填充相同的变量。您还可以使用tTShowerHeader->Print()来显示每个分支的entires数量。

#2


2  

You are calling tTShowerHeader->GetBranch("fProjName")-> and it compiles. That means that return type of tTShowerHeader->GetBranch() is a pointer.

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

Moreover, you are calling GetClassName() on that pointer and it compiles, so it's a pointer to a class type.

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

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.

更重要的是,std::string没有GetClassName()方法,所以它不是std::string*。事实上,它似乎是TBranch *。你必须找到合适的方法给你文本。

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

PS:在c++中不学习使用C风格的cast。C-style cast是邪恶的,因为它会根据不同的类型做不同的事情。使用受限制的static_cast、dynamic_cast、const_cast或functionalstyle类型的转换(如果确实需要的话,可以重新解释,但这应该是非常罕见的)。

#3


1  

The solution follows below.

解决方案如下所示。

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

假设您有一个根文件,并希望保存std::字符串。

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.

希望这个有帮助。

#1


1  

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.

在root中,TTree类将地址存储到用于输入的varriable中。使用GetEntry()将用存储在TTree中的信息填充相同的变量。您还可以使用tTShowerHeader->Print()来显示每个分支的entires数量。

#2


2  

You are calling tTShowerHeader->GetBranch("fProjName")-> and it compiles. That means that return type of tTShowerHeader->GetBranch() is a pointer.

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

Moreover, you are calling GetClassName() on that pointer and it compiles, so it's a pointer to a class type.

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

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.

更重要的是,std::string没有GetClassName()方法,所以它不是std::string*。事实上,它似乎是TBranch *。你必须找到合适的方法给你文本。

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

PS:在c++中不学习使用C风格的cast。C-style cast是邪恶的,因为它会根据不同的类型做不同的事情。使用受限制的static_cast、dynamic_cast、const_cast或functionalstyle类型的转换(如果确实需要的话,可以重新解释,但这应该是非常罕见的)。

#3


1  

The solution follows below.

解决方案如下所示。

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

假设您有一个根文件,并希望保存std::字符串。

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.

希望这个有帮助。