TString as a leaf in a tree

Ok,

 Well, I would like to store a single TString in a root tree.  I thought that this would be a simple operation but for some reason it is proving to be quite the bear.  I have done some searching and all the solutions seem to have a TString wrapped by a custom class so that other information can be stored.  I would like to have just the TString stored.  I need nothing else.  I have tried the following block of code...
#include <TTree.h>
#include <TFile.h>
#include <TString.h>
#include <iostream>

using namespace std;

int main() {

  TFile output("output.root", "RECREATE");
  TTree *tree = new TTree("output", "output2");

  TString *test_string = new TString();

  tree->Branch("haha", &test_string);

  *test_string = "This is a test";

  tree->Fill();

  tree->Write();
  output.Close();

  return(0);
}

So, when I compile it is goes super clean, and then when I run it, it runs fine… The problem is when I try to read it… I get a segmentation fault no matter what I do. Is there something fundamentally wrong with what I am trying to do?

Justace

[quote] Is there something fundamentally wrong with what I am trying to do? [/quote]Nothing … except maybe using a version of ROOT too old?
(You need ROOT v5.22 or up).

Cheers,
Philippe.

Well,

 I am running 5.22 right now.  That is what I used to generate the root file with the above code.  Below is the output when I try to read the information from the root file....
  *******************************************                                                                                                                                 
  *                                         *                                                                                                                                 
  *        W E L C O M E  to  R O O T       *                                                                                                                                 
  *                                         *                                                                                                                                                                      
  *   Version  5.22/00b       14 May 2009   *                                                                                                                                                                      
  *                                         *                                                                                                                                                                      
  *  You are welcome to visit our Web site  *                                                                                                                                                                      
  *          http://root.cern.ch            *                                                                                                                                                                      
  *                                         *                                                                                                                                                                      
  *******************************************                                                                                                                                                                      
                                                                                                                                                                                                                   
ROOT 5.22/00b (branches/v5-22-00-patches@28611, Jul 26 2009, 02:34:00 on linux)                                                                                                                                    
                                                                                                                                                                                                                   
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008                                                                                                                                                          
Type ? for help. Commands must be C++ statements.                                                                                                                                                                  
Enclose multiple statements between { }.                                                                                                                                                                           
root [0] TFile input("output.root", "READ");                                                                                                                                                                       
root [1] input.ls()                                                                                                                                                                                                
TFile**         output.root                                                                                                                                                                                        
 TFile*         output.root                                                                                                                                                                                        
  KEY: TTree    output;1        output2                                                                                                                                                                            
root [2] output->Print()                                                                                                                                                                                           
******************************************************************************                                                                                                                                     
*Tree    :output    : output2                                                *                                                                                                                                     
*Entries :        1 : Total =            1475 bytes  File  Size =        568 *                                                                                                                                     
*        :          : Tree compression factor =   1.00                       *                                                                                                                                     
******************************************************************************                                                                                                                                     
*Branch  :haha                                                               *                                                                                                                                     
*Entries :        1 : BranchElement (see below)                              *                                                                                                                                     
*............................................................................*                                                                                                                                     
*Br    0 :fData     :                                                        *                                                                                                                                     
*Entries :        1 : Total  Size=        582 bytes  File Size  =        104 *                                                                                                                                     
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *                                                                                                                                     
*............................................................................*                                                                                                                                     
root [3] TString *tmp = new TString();                                                                                                                                                                             
root [4] output->SetBranchAddress("haha", &tmp);                                                                                                                                                                   
root [5] output->GetEntry(0);                                                                                                                                                                                      
*** glibc detected *** /usr/bin/root.exe: free(): invalid pointer: 0xb7f94058 ***                                                                                                                                  
======= Backtrace: =========                                                                                                                                                                                       
/lib/libc.so.6[0xb6e967c4]                                                                                                                                                                                         
/lib/libc.so.6(cfree+0xdc)[0xb6e9af2c]                                                                                                                                                                             
/usr/lib/gcc/i686-pc-linux-gnu/4.3.3/libstdc++.so.6(_ZdlPv+0x21)[0xb7078f51]
/usr/lib/gcc/i686-pc-linux-gnu/4.3.3/libstdc++.so.6(_ZdaPv+0x1d)[0xb7078fad]
/usr/lib/root/libRIO.so(_ZN13TStreamerInfo10ReadBufferIPPcEEiR7TBufferRKT_iiii+0x3b9c)[0xb6cdd49c]
/usr/lib/root/libTree.so(_ZN14TBranchElement10ReadLeavesER7TBuffer+0x457)[0xb6955227]
/usr/lib/root/libTree.so(_ZN7TBranch8GetEntryExi+0x1c4)[0xb6948d54]
/usr/lib/root/libTree.so(_ZN14TBranchElement8GetEntryExi+0x1c2)[0xb6957912]
/usr/lib/root/libTree.so(_ZN14TBranchElement8GetEntryExi+0x145)[0xb6957895]
/usr/lib/root/libTree.so(_ZN5TTree8GetEntryExi+0xbc)[0xb6995ddc]
/usr/lib/root/libTree.so[0xb6a1b1da]
/usr/lib/root/libCint.so.5.22(_ZN4Cint19G__ExceptionWrapperEPFiP8G__valuePKcP8G__paramiES1_PcS5_i+0x46)[0xb729fa66]
/usr/lib/root/libCint.so.5.22(G__execute_call+0x5c)[0xb735b95c]
/usr/lib/root/libCint.so.5.22(G__call_cppfunc+0x206)[0xb735cad6]
/usr/lib/root/libCint.so.5.22(G__interpret_func+0x1bfc)[0xb73370ac]
/usr/lib/root/libCint.so.5.22(G__getfunction+0x21ec)[0xb732545c]
/usr/lib/root/libCint.so.5.22(G__getstructmem+0x358)[0xb740b888]
/usr/lib/root/libCint.so.5.22(G__getvariable+0x40c6)[0xb7404fa6]
/usr/lib/root/libCint.so.5.22(G__getitem+0x80)[0xb72f73f0]
/usr/lib/root/libCint.so.5.22(G__getitem+0x434)[0xb72f77a4]
/usr/lib/root/libCint.so.5.22(G__getexpr+0x45d3)[0xb72fd103]
/usr/lib/root/libCint.so.5.22(G__exec_statement+0x9ccd)[0xb738bd5d]
/usr/lib/root/libCint.so.5.22[0xb72e21c2]
/usr/lib/root/libCint.so.5.22(G__exec_tempfile_fp+0x19)[0xb72e2499]
/usr/lib/root/libCint.so.5.22(G__process_cmd+0x1105)[0xb7393585]
/usr/lib/root/libCore.so.5.22(_ZN5TCint11ProcessLineEPKcPN12TInterpreter10EErrorCodeE+0x374)[0xb7b35974]
/usr/lib/root/libCore.so.5.22(_ZN12TApplication11ProcessLineEPKcbPi+0x7b2)[0xb7a5a6e2]
/usr/lib/root/libRint.so.5.22(_ZN5TRint15HandleTermInputEv+0x1d6)[0xb70c0d66]
/usr/lib/root/libRint.so.5.22(_ZN17TTermInputHandler6NotifyEv+0x25)[0xb70c0825]
/usr/lib/root/libRint.so.5.22(_ZN17TTermInputHandler10ReadNotifyEv+0x14)[0xb70c3014]
/usr/lib/root/libCore.so.5.22(_ZN11TUnixSystem16CheckDescriptorsEv+0x1bb)[0xb7b49b6b]
/usr/lib/root/libCore.so.5.22(_ZN11TUnixSystem16DispatchOneEventEb+0x113)[0xb7b49ed3]
/usr/lib/root/libCore.so.5.22(_ZN7TSystem9InnerLoopEv+0x21)[0xb7abd471]
/usr/lib/root/libCore.so.5.22(_ZN7TSystem3RunEv+0x8b)[0xb7ac028b]
/usr/lib/root/libCore.so.5.22(_ZN12TApplication3RunEb+0x37)[0xb7a581f7]
/usr/lib/root/libRint.so.5.22(_ZN5TRint3RunEb+0x3d3)[0xb70c2af3]
/usr/bin/root.exe(main+0x85)[0x8048ff5]
/lib/libc.so.6(__libc_start_main+0xe5)[0xb6e40a65]
/usr/bin/root.exe[0x8048e81]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:07 2413029    /usr/bin/root.exe
0804a000-0804b000 r--p 00001000 08:07 2413029    /usr/bin/root.exe
0804b000-0804c000 rw-p 00002000 08:07 2413029    /usr/bin/root.exe
0804c000-085ca000 rw-p 0804c000 00:00 0          [heap]
b68f8000-b6aab000 r-xp 00000000 08:07 2437879    /usr/lib/root/libTree.so.5.22
b6aab000-b6ab3000 r--p 001b2000 08:07 2437879    /usr/lib/root/libTree.so.5.22
b6ab3000-b6ab5000 rw-p 001ba000 08:07 2437879    /usr/lib/root/libTree.so.5.22
b6ab5000-b6ab7000 rw-p b6ab5000 00:00 0
b6ab7000-b6baf000 r-xp 00000000 08:07 2437758    /usr/lib/root/libNet.so.5.22
b6baf000-b6bb4000 r--p 000f8000 08:07 2437758    /usr/lib/root/libNet.so.5.22
b6bb4000-b6bb5000 rw-p 000fd000 08:07 2437758    /usr/lib/root/libNet.so.5.22
b6bb5000-b6bb7000 rw-p b6bb5000 00:00 0
b6bb7000-b6bf9000 r-xp 00000000 08:07 2437875    /usr/lib/root/libThread.so.5.22
b6bf9000-b6bfb000 r--p 00041000 08:07 2437875    /usr/lib/root/libThread.so.5.22
b6bfb000-b6bfc000 rw-p 00043000 08:07 2437875    /usr/lib/root/libThread.so.5.22
b6bfc000-b6bfd000 rw-p b6bfc000 00:00 0
b6bfd000-b6d71000 r-xp 00000000 08:07 2437797    /usr/lib/root/libRIO.so.5.22
b6d71000-b6d74000 r--p 00173000 08:07 2437797    /usr/lib/root/
...

output.root (5.22 KB)

D0 is running an old version of root for their p21 software release. I thought it was at least in the 5.00 and greater region but I guess they reverted… How would I need to modify my code to use this older version of root? Thanks for the pointers…

  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version  4.04/02b       3 June 2005   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

Compiled on 7 March 2007 for linux with thread support.

CINT/ROOT C/C++ Interpreter version 5.15.169, Mar 14 2005
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.

Hi,

With v4.04 you will need to wrap the string in another class or use TObjString.

Cheers,
Philippe.

Humm… I tried that. I will go and try that again. Do you see why I get the seg fault from above?

Hi,

Humm the TString are not supposed to be split try:tree->Branch("haha", &test_string,16000,0);

Cheers,
Philippe

Thanks, It looks like that was the issue… At least for the case on my local computer. Now to see if it works at D0…