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 : Fri Jun 08 2001 - 11:50:55 MEST