Writing/Reading arrays to TTree and TFile problem

Hi, I’m writing some scalar variables and some arrays to a tree and then to a ROOT file, then opening the file and reading back the arrays, however, I’m finding that some of my arrays are corrupt.

I am using ROOT 3.10/02 on a gentoo linux machine (kernel 2.6.2)

First I create space for the data

  Int_t nHam;
  Float_t HamVrArray[20];
  Float_t HamIdArray[20];
  Float_t HamItArray[20];

Then I set the addresses to the tree

 TTree *tree = new TTree("CfirradTree","Cfirrad Data");
  tree->Branch("nHam", &nHam, "nHam/F");
  tree->Branch("HamVrArray", HamVrArray, "HamVrArray[nHam]/F");
  tree->Branch("HamIdArray", HamIdArray, "HamIdArray[nHam]/F");
  tree->Branch("HamItArray", HamItArray, "HamItArray[nHam]/F");

I also ready a ROOT file

TFile *f = new TFile("CfirradTree.root", "recreate");

I then fill the from an SQL database and call tree->Fill() for each record and tree->Write() when I am all done, I am confident this part is working. Next I see that the data got into the tree ok.

tree->GetEntry(10);
  cout << "=== before ===" << endl;
  for (int j=0; j<nHam; j++) {
    printf("APDid %u v %f id %f it %f",
	   APDid, HamVrArray[j], HamIdArray[j],HamItArray[j]);
    cout << endl;
  }

and it does, it looks like this:

=== before ===
APDid 4487 v 20.000000 id 0.107000 it 98.000000
APDid 4487 v 400.000000 id 2.310000 it 3800.000000
APDid 4487 v 405.000000 id 2.630000 it 4390.000000
APDid 4487 v 410.000000 id 2.940000 it 5130.000000
...

Then I create an entirely new tree from the root file, and read the same data again and it is garbage. Specifically I do this:

 f->Close();
  TFile * f2 = new TFile("CfirradTree.root");

  delete HamVrArray;
  delete HamIdArray;
  delete HamItArray;

  TTree * tree2 = (TTree*)f2->Get("CfirradTree");
  tree2->SetBranchAddress("APDid", &APDid);  
  tree2->SetBranchAddress("nHam", &nHam);
  tree2->SetBranchAddress("HamVrArray", HamVrArray);
  tree2->SetBranchAddress("HamIdArray", HamIdArray);
  tree2->SetBranchAddress("HamItArray", HamItArray);

  tree2->GetEntry(10);
  cout << "=== after ===" << endl;
  for (int j=0; j<nHam; j++) {
    printf("APDid %u v %f id %f it %f",
	   APDid, HamVrArray[j], HamIdArray[j],HamItArray[j]);
    cout << endl;
  }

and I get:

=== after ===
APDid 4487 v 78025493862578276373437251959390208.000000 id 9.989502 it 4572595216328919831338468834803712.000000
APDid 4487 v 0.000000 id 0.000000 it 9.989502
APDid 4487 v 405.000000 id 2.630000 it 4390.000000
...

Did I do something wrong? I have other data, and even other arrays and objects and they are all fine except for this one. I’ve been straining my eyes to see what the difference is between what I did for this data and the other data, but I can’t see a problem. Let me know if there is something wrong.

  • Rick

Hi
In the Branch declaration for nHam, you declare it as a float (“nHam/F”) whereas it is in fact it is a Int_t. Could this cause problems ?
Hope this helps
John

Indeed! That was exactly it. I’m not sure exactly how that ruined the array, but correctly setting it to int fixed it.

Thanks!

  • Ricky