TBranch not filling correctly

Hi everyone,

I am trying to write a variable array branch to a cloned root file. Another program clones the trees but without one branch on one tree that is to be written with the current program. After much trial and error, the code below successfully extracts the data from one tree and prints to the terminal. However, when simply assigning this data to a variable array to be written to a new branch in another file, an incorrect histogram is produced. A portion of the code is reproduced below:

TFile *file = new TFile("originalFile.root", "READ");
TTree *tree = (TTree*)file->Get("TCluster");

TFile *File = new TFile("clonedFile.root","update");
TTree *Tree = (TTree*)File->Get("TCluster");

//Number of entries
int N = tree->GetEntries();
cout << "Number of entries: " << N << endl;

int nclust, maximum;
int clustNum[N];

tree->SetBranchAddress("nclust", &nclust);

for (int i = 0; i < N; i++){
   tree->GetEntry(i);
   clustNum[i] = nclust;
}

maximum = clustNum[0];

//Find maximum to create arrays
for (int a = 1; a < N; a++){
   if (clustNum[a] > maximum){
      maximum = clustNum[a];
   }	
} 

Float_t clustADC[maximum];
Float_t clustADCs[maximum];

TBranch *Branch = Tree->Branch("clustADCs", clustADCs, "clustADCs[nclust]/F");
tree->SetBranchAddress("clustADCs", clustADC);

int pos = 0;
for (int i = 0; i< N; i++){		
   for (int j = 0; j < clustNum[i]; j++){
   tree->GetEntry(pos);
   cout << clustADC[j] << endl;
   clustADCs[j] = clustADC[j];
   cout << clustADCs[j] << endl;
   }
   Branch->Fill();
}

Attached are histograms displaying this change (both with the same range and binning). Below is the histogram from the original file:


And the new histogram written to the new file:

Does anyone know why this is occurring or what could be causing this?

Thank you,

Stephen


ROOT Version: 6.14/00
Platform: MacOS High Sierra, v.10.13.4
Compiler: gcc version 5.1.0


@pcanal can help you I guess.

i am not quite sure but for the looping you may have meant:

for (int i = 0; i< N; i++){		
   tree->GetEntry(i);  
   for (int j = 0; j < clustNum[i]; j++) {
      cout << clustADC[j] << endl;
      clustADCs[j] = clustADC[j];
      cout << clustADCs[j] << endl;
   }
   Branch->Fill();
}

in the original it seems that ‘pos’ is always zero and thus on the first entry of the input tree is read.

Hi @pcanal,

I must have copied the code over incorrectly; pos is incremented for every j value, so the loop looks like this:

int pos = 0;
for (int i = 0; i< N; i++){		
   for (int j = 0; j < clustNum[i]; j++) {
      tree->GetEntry(pos);
      cout << clustADC[j] << endl;
      clustADCs[j] = clustADC[j];
      cout << clustADCs[j] << endl;
      pos++;
   }
   Branch->Fill();
}

This does in fact return the correct values, and the values from the original tree (clustADC) are being assigned to the new variable array clustADCs. I suspect there might be an issue with Branch->Fill() but I am not too sure.

Thank you,

Stephen

With that new code pos varies from 0 to sum of clustNum[i] of i ranging from 0 to N. However the valid values for tree->GetEntry are only from 0 to N …

Now going back to the original description: "I am trying to write a variable array branch to a cloned root file. ", I believe that this code is closer to what you need:

Float_t clustADCs[maximum];

Tree->SetBranchAddress("nclust", &nclust);
TBranch *Branch = Tree->Branch("clustADCs", clustADCs, "clustADCs[nclust]/F");
tree->SetBranchAddress("clustADCs", clustADCs); // Important: same data pointer as above.

for (int i = 0; i< N; i++) {
   tree->GetEntry( i );
   Branch->Fill();  // With newer version of ROOT use Branch->BackFill();
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.