Hello,
I am trying to save a 2D array data[2][20] in a TTree t. When I do
everything is fine.
But I would like to store arrays that can have variable size. So when I do
t->Branch("n",&n,"n/I");
t->Branch("data",data,"data[2][n]/b");
I don’t get the data that I expect.
I presume that a variable-size array is allowed, right? I appreciate some help to solve this problem.
Examples of writing and reading the data are given below.
Cheers,
Roberval
WriteData.C
void WriteData()
{
unsigned char data[2][20];
int nbytes = 20;
int n;
TFile f("tree.root", "recreate");
TTree *t = new TTree("T","tree");
t->Branch("n",&n,"n/I");
t->Branch("data",data,"data[2][20]/b");
// Emulate the ADC (dummy data)
unsigned char dummy_data[2][20];
for ( int i = 0 ; i < nbytes ; ++i )
{
dummy_data[0][i] = 0x89+i;
dummy_data[1][i] = 0x8D+i;
}
// Copy dummy data in the ADC format, 32-bit words
// adcData is the block of data readout from ADC
// [board][20bytes*8channels]; int has 4 bytes
int adcData[2][40];
for ( int i = 0 ; i < 8 ; ++i ) // loop over channels
{
memcpy(&adcData[0][i*(nbytes/4)],&dummy_data[0][0],nbytes);
memcpy(&adcData[1][i*(nbytes/4)],&dummy_data[1][0],nbytes);
}
// Loop over the 8 channels of an ADC
// Store channel data as an entry in the ntuple
for ( int i = 0 ; i < 8 ; ++i )
{
memcpy(&data[0][0],&adcData[0][i*nbytes/4],nbytes);
memcpy(&data[1][0],&adcData[1][i*nbytes/4],nbytes);
n = nbytes;
t -> Fill();
}
t -> Write();
}
ReadData.C
void ReadData()
{
unsigned char data[2][20];
int n;
TFile f("tree.root", "old");
TTree *t = (TTree*) f.Get("T");
t->SetBranchAddress("n",&n);
t->SetBranchAddress("data",data);
int x[100], y[100];
// Plot for channel 7 of board 1
int channel = 7;
t -> GetEntry(channel);
for ( int i = 0 ; i < n ; ++i)
{
x[i] = int(i);
y[i] = int(data[1][i]);
}
TGraph * gr = new TGraph(n,x,y);
gr -> Draw("AC*");
}