TClonesArray question

Hi, all,

I’m having difficulty storing data to a TClonesArray filled with a user-defined class. A simple program that reproduces the error is shown below. (In the actual program, this is part of some code to store the TClonesArray along with other things in a tree.) In this simple code, I try to store data to a TClonesArray and read it back out, but this output doesn’t match what I tried to store. No error messages are generated. Any help would be most appreciated.

Best wishes,

class calcclass : public TObject
  double Eprecal;
  double Ecal;
  int debugoutput() {
    cerr<<"calcclass----\nEprecal: "<<Eprecal<<"\nEcal   : "<<Ecal<<endl;
    return 0;

int clonetest() {
    calcclass calcobj;
    TClonesArray *calcarr = new TClonesArray("calcclass",40);

    for (int i=0; i<20; i++) {

      for (int c=0; c<4; c++) {
        cerr << i << "\t" << c << endl;
        cerr << "object Eprecal: " << calcobj.Eprecal << endl;
        cerr << "object Ecal   : " << calcobj.Ecal << endl;
        new((*calcarr)[c]) calcobj;
        calcclass *objectptr = (calcclass*)calcarr->At(c);
        cerr << "clones Eprecal: " << objectptr->Eprecal << endl;
        cerr << "clones Ecal   : " << objectptr->Ecal << endl;

    return 0;


I am not sure what error you see. However, you ought to replace:calcarr->Delete();with calcarr->Clear();

In newer version of ROOT you can also improve the code by replacingnew((*calcarr)[c]) calcobj; calcclass *objectptr = (calcclass*)calcarr->At(c);withcalcclass *objectptr = (calcclass*)calcarr->ConstructedAt(c);