Hi Christian, Why don't you just store it as a 1dimensional array with in addition the # of columns so that you can easily obtain a[i][j] = b[i*nrColumns+j] Eddy > From: cstrato@EUnet.at > X-Authentication-Warning: newmint.cern.ch: Host endjinn.austria.eu.net [193.81.13.2] claimed to be relay2.austria.eu.net > Date: Fri, 29 Dec 2000 19:57:44 +0100 > X-Accept-Language: en > MIME-Version: 1.0 > To: "roottalk@pcroot.cern.ch" <roottalk@pcroot.cern.ch> > Subject: [ROOT] non-constant 2-dimensional arrays? > Content-Transfer-Encoding: 8bit > X-Filter-Version: 1.3 (ram) > > Dear Rooters > > I would like to read tables containing (x, y, value) in each line, > as an array ar[][]. However, the number of rows and columns would > always be different. > > In an example given by Rene Brun in the mail from Sun Feb 13 2000 > (http://root.cern.ch/root/roottalk/roottalk00/0367.html) > he has written “I assume that your array has a constant size in x and y” > > and has presented the file “array.C” > > Is there an easy way to generalize this example, when the dimensions > are not constant? > > As a first attempt I have modified the file “array.C” in a way that > it uses class TMatrix instead of a 2-dim array (see below). > > Since I do not need matrix calculations, but only a 2-dim array, > especially > arrays with the size of about 1000 x 1000, my question is, is the use > of class TMatrix the right way or does there exist a better solution? > > Thank you in advance > Christian Stratowa > Vienna, Austria > > > //----------file array.C > #include "TMatrix.h" > #include <iostream.h> > #include <math.h> > > void fillarray(Int_t nrow,Int_t ncol) > { > TFile f("test.root","recreate"); > TTree T("T","a simple test with an array"); > TMatrix *ma = new TMatrix(nrow,ncol); > T.Branch("ma",&ma,64000,0); > for (Int_t i=0;i<nrow;i++) { > for (Int_t j=0;j<ncol;j++) { > ma(i,j) = gRandom->Gaus(i,j); > } > } > T.Fill(); > T.Write(); > } > > void readarray() > { > Int_t nrow; > Int_t ncol; > TMatrix *ma; > > TFile *f = new TFile("test.root"); > TTree *T = (TTree*)f->Get("T"); > T->SetBranchAddress("ma",&ma); > TH1F *hed = new TH1F("hed","ma values",100,-100,100); > T->GetEntry(); > nrow = ma->GetNrows(); > ncol = ma->GetNcols(); > for (Int_t i=0;i<nrow;i++) { > for (Int_t j=0;j<ncol;j++) { > hed->Fill(ma(i,j)); > } > } > printf("nrows = %i, ncols = %i \n",nrow,ncol); > hed->Draw(); > } > > Eddy A.J.M. Offermann Renaissance Technologies Corp. Route 25A, East Setauket NY 11733 e-mail: eddy@rentec.com http://www.rentec.com
This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:40 MET