[ROOT] Dynamic arrays in classes and trees

From: Ben Morgan (B.Morgan@sheffield.ac.uk)
Date: Wed Feb 27 2002 - 12:05:55 MET


Hi ROOTers,
           I'm trying to write a class to hold raw data from my
experiment. One of the data members of this class is a 2D array with one
dimension dynamically allocated. I have a small test code working
(included below) on a RH6.2 PC using ROOT 3.01/06 which compiles fine and
also works in a test application which just generates and prints out the
values in the array. However, when I try and store this object in a TTree, the
application falls over with a segmentation fault when trying to store the
first entry - and it's not clear from the inspecting the core in gdb what
happened.
	I understand from other postings that one cannot store
multidimensional arrays with more than one dimension dynamically
allocated, but is one o.k. (and are there any plans to support
multidimensional dynamic arrays within trees as I'd really like both
dimensions to be dynamic?)? It may just be my coding, but the array is
created without any (obvious) problem and the values printed to the
screen. The 'data' class is based on the Event example in the manual and I
have already written similar classes, albeit with one dimensional dynamic
arrays. Thanks,

Ben Morgan.

//Class data.h
#ifndef __data_h
#define __data_h

#include<iostream.h>
#include "TObject.h"
#include "TRandom.h"

class data : public TObject
{
 private:
  Int_t fNwire;
  Int_t fNchan;
  Short_t *array[10];  //[fNwire]

 public:
  data();
  virtual ~data();
  void SetWC(Int_t a, Int_t b) {fNwire=a; fNchan=b; SetArray();}
  void SetArray();
  void Display();

  ClassDef(data,1)
};

#endif


//Class data.C ------------------------------------------------------------
#include "data.h"
ClassImp(data)

data::data()
{
  fNwire=0;
  fNchan=0;
 for(Int_t i=0; i<10; i++)
    {
      array[i]=0;
    }

}

data::~data()
{
  delete [] array;
}

void data::SetArray()
{
 for(Int_t i=0; i<10; i++)
    {
      delete array[i];
    }


  for(Int_t i=0; i<10; i++)
    {
      array[i] = new Short_t[fNwire];
      for(Int_t j=0; j<fNwire; j++)
	{
	  array[i][j] = 4;
	}
    }
}

void data::Display()
{
  cout<<"\n";
  for (Int_t i=0; i<10; i++)
    {
      for(Int_t j=0; j<fNwire; j++)
	{
	  cout<<array[i][j]<<" ";
	}
      cout<<endl;
    }
}
//-------------------------------------------------------------------------


The Application:
#include <iostream.h>
#include <stdlib.h>

#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"

#include "data.h"

int main()
{

  TFile *dfile = new TFile("datatree.root", "RECREATE");
  TTree *tree = new TTree("RawData", "A Tree with data Objects");
  data *d1 = new data();
  tree->Branch("data_branch","data", &d1,64000,2);

  for(Int_t i=0; i<100; i++)
    {
      d1->SetWC(10,10);
      d1->Display();
      tree->Fill();
    }
  tree->Print();
  dfile->Write();
  dfile->Close();

  delete d1;

  return 0;
}



-- 
-------------------------------------------------------------------------------
Mr. Ben Morgan
Postgraduate Student
University of Sheffield
Department of Physics & Astronomy
Hicks Building
Hounsfield Road
Sheffield  S3 7RH
-------------------------------------------------------------------------------



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