Re: [ROOT] Problem storing/reading TString in TTree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jan 22 2001 - 08:51:06 MET


Hi Christian,

The TTree split mode should not be used in several cases.
See the list of restrictions described in the Users Guide or at URL:
   http://root.cern.ch/root/HowtoWriteTree.html
  look at section: Making a Tree with many branches

In your example, the solution is to replace:


vTree->Branch("vBranch","TestClass",&vTest,256000,1);
by
vTree->Branch("vBranch","TestClass",&vTest,256000,0);

I am currently removing all the restrictions to the TTree split mode.
The split and no-split mode will use the same algorithm using the new
TStreamerInfo class. For more info, see:
   http://root.cern.ch/root/SchemaEvolution.html
The new algorithm should be available in teh coming few weeks.

Rene Brun

cstrato@EUnet.at wrote:
> 
> Dear Rooters
> 
> I would like to store names as TString in a treebranch, but have
> problems in reading them back.
> 
> I have a class "TestClass" which I use in TBranch. The following
> code in macro1 seems to work but in macro2 I can only read back
> the number vFloat but not the name vName:
> 
> ---------macro1.C---------------
> {
>    TFile *vFile = new TFile("test.root","RECREATE");
>    TTree *vTree = new TTree("vTree","data from file name.txt");
>    TestClass *vTest = new TestClass();
>    TBranch *vBranch =
> vTree->Branch("vBranch","TestClass",&vTest,256000,1);
> 
>    ifstream vInput.open("name.txt", ios::in);
> 
> // I tested the following three definitions
> // which one should I use?
>    char* vName = new char[8];
>    char vName[8];
>    TString  vName;
> 
>    for (Int_t i=0;i<1000;i++) {
>       vInput >> vFloat >> vName;
>       if (!vInput.good()) break;
> 
>       vTest->FSetFloat(vFloat);
>       vTest->FSetName(vName);
> 
>       vTree->Fill();
>       vTest->Clear();
>    }
> 
>    vInput.close();
>    vTree->Write();
> 
>    delete vTest;
>    delete [] vName;
> }
> 
> ---------macro2.C---------------
> {
>    TFile *vFile = new TFile("test.root","READ");
>    TTree *vTree = (TTree*)vFile->Get("vTree");
>    TestClass *vTest = new TestClass();
>    vTree->SetBranchAddress("vBranch",&vTest);
> 
> // none of the following three definitions seems to work
>    char* vName = new char[8];
>    char vName[8];
>    TString  vName;
> 
>    for (Int_t i=0;i<1000;i++) {
>       vTree->GetEntry(i);
> 
>       vFloat = vTest->FGetFloat();
>       vName = vTest->FGetName();
> 
> // this prints vFloat correctly, but random chars for vName
>       printf("vFloat=%.3lf,vName=%s\n", vFloat, vName);
> // this prints vFloat correctly, but nothing for vName
>       cout << "vFloat= " << vFloat  << " vName= " << vName << endl;
>    }
> 
>    delete vTest;
>    delete [] vName;
> }
> 
> ---------TestClass--------------
> class TestClass: public TObject {
> 
>    private:
>       TString       fName;
>       Float_t       fFloat;
> 
>    public :
> 
>       TestClass();
>       ~TestClass();
> 
>       void FSetFloat(Float_t vFloat) {fFloat = vFloat;}
> // first test to store TString
>       void FSetName(const char *vName) {fName = vName;}
> // second test to store TString: should I use this function?
>       void FSetName(const char *vName) {fName = TString(vName,8);}
> 
>       Float_t FGetFloat() const {return fFloat;}
>       const char* FGetName() const {return fName;}
> 
>       ClassDef(TestClass,1) //TestClass
> };
> 
> //--------------------
> #include "TestClass.h"
> 
> ClassImp(TestClass);
> 
> TestClass::TestClass(): TObject()
> {
> }
> 
> TestClass::~TestClass()
> {
> }//Destructor
> //--------------------
> 
> Could you please tell me what I am doing wrong? I tested differnt
> ways to read back TString, but none of them seem to work.
> 
> Thank you in advance for your help.
> 
> Best regards
> Christian Stratowa
> Vienna, Austria
> 
> My system: PowerBook running LinuxPPC 2000
>            root 3.00/01  22 December 2000



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:34 MET