将字符串保存到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?

最佳答案

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

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"

在根目录下,TTree类将地址存储到变量中,以用于输入该变量。使用GetEntry()将用存储在TTree中的信息填充相同的变量。
您也可以使用tTShowerHeader-> Print()显示每个分支的整数个数。

10-06 09:28