Search code examples
c++stringtreeroot-framework

write/read string in a TTree (cern root)


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?


Solution

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