Re: [ROOT] Arrays of strings

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jun 12 2002 - 22:57:03 MEST


Hi Christian,

Your logic seems OK. What is the problem?

Rene Brun

On Wed, 12 Jun 2002, cstrato wrote:

> Dear Rooters
> 
> Suppose, I have a table containing first name, last name and comment 
> fields,
> which I would like to store in a TTree, using a class TPerson.
> This is straight foreward, however, for certain purposes I need to read 
> into
> memory the complete table first (e.g. in order to sort the table 
> alphabetically
> using a hash table).
> For this reason I have to create temporary arrays of TString.
> 
> The macro shown below seems to work until now, however, I am wondering if
> I do the initialization of the arrays of TStrings correctly, or if there
> is a better way?
> The problem is that the names in the table do not have the same length,
> especially, the comment field can vary from few characters to more than 1000
> characters. With more than 100,000 records to import, this could cause 
> hidden
> memory problems.
> 
> I know this is not necessarily a question for roottalk, nevertheless, it
> would be great if someone could give me an advice.
> 
> Thank you in advance for your help.
> 
> Best regards
> Christian
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> C.h.r.i.s.t.i.a.n. .S.t.r.a.t.o.w.a
> V.i.e.n.n.a.         .A.u.s.t.r.i.a
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 
> //______________________________________________________________________________
> class TPerson: public TNamed {
>    private:
>       TString      fFirstName;
>       TString      fLastName;
>       TString      fComment;
>    public :
>       TPerson();
>       TPerson(const char *name,const char *title);
>       virtual ~TPerson();
> }
> 
> //______________________________________________________________________________
> Int_t XGeneChip::ReadNames(ifstream &input)
> {
>    char nextline[4096];
>    Int_t i;
>    Int_t err = 0;
> 
> // Create local arrays to store data from input
>    TString *arrFirst = 0;
>    TString *arrLast  = 0;
>    TString *arrComm  = 0;
> 
>    // initialize memory for local arrays
>    Int_t size = 100000;
>    if (!(arrFirst = new TString[size])) {err = -3;}
>    if (!(arrLast  = new TString[size])) {err = -3;}
>    if (!(arrComm  = new TString[size])) {err = -3;}
> 
>    if (err == 0) {
>    // Initialize local arrays
>       for (i=0; i<size; i++) {
>          arrFirst[i] = "NA";
>          arrLast[i]  = "NA";
>          arrComm[i]  = "NA";
>       }//for_i
> 
>    // Read data
>       TString first, last, comm;
>       for (i=0; i<size; i++) {
>          input.getline(nextline, 4096)
>          if (input.fail()) break;
>          // read fields
> //         input >> first >> last >> comm;  //does not work!!!
>          first = strtok(&nextline[0], "\t");
>          last  = strtok(NULL, "\t");
>          comm  = strtok(NULL, "\t");
>          arrFirst[idx] = first;
>          arrLast[idx] = last;
>          arrComm[idx] = comm;
>       }//for_i
> 
>    // Do something with the data in memory
>    // e.g. sort data alphabetically
> 
>    // Init tree
>       tree = etc;
>       TPerson *name = 0;
>       etc
> 
>    // Fill tree
>       for (i=0; i<size; i++) {
>          idx = index[i];
>          name->SetFirst(arrFirst[idx]);
>          name->SetLast(arrLast[idx]);
>          name->SetComment(Comm[idx]);
>          tree->Fill();
>       }//for_i
> 
>    // Write tree to file
>       tree->Write("", TObject::kOverwrite);
>       tree->Delete("");
>       tree = 0;
> 
>       SafeDelete(name);
>    }//if
> 
> // Clean up
>    if (arrFirst) {delete [] arrFirst; arrFirst = 0;}
>    if (arrLast)  {delete [] arrLast;  arrLast  = 0;}
>    if (arrComm)  {delete [] arrComm;  arrComm  = 0;}
> 
>    return err;
> }//ReadNames
> 
> 
> 
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:56 MET