How to access buffer of object in root file

I’m trying to write a script to compare arbitrary root files and determine if the objects within are equivalent, apart from the timestamps and I am thinking about compare the objects’ buffer in root file. I have tried two different methods to access the buffer of objects, but none of them works.

(1) TFile::ReadBuffer()

    TFile f1("f1");
    TFile f2("f2");
    
    TIter next_1(f1.GetListOfKeys());
    TIter next_2(f2.GetListOfKeys());
    TKey *k_1;
    TKey *k_2;

    k_1=(TKey*)next_1();
    k_2=(TKey*)next_2();
    
    Long64_t f1_seek_key = k_1->GetSeekKey();
    Long64_t f2_seek_key = k_2->GetSeekKey();


    Int_t nbytes_1 = k_1->GetNbytes();
    Int_t nbytes_2 = k_2->GetNbytes();

    char *buf_1 = new char[nbytes_1];    
    char *buf_2 = new char[nbytes_2];

    f1.ReadBuffer(buf_1, f1_seek_key, nbytes_1);
    f2.ReadBuffer(buf_2, f2_seek_key, nbytes_2);

buf_1 and buf_2 arrays are empty after calling ReadBuffer()

(2) TKey::GetBuffer()

TFile f1("f1");
    TFile f2("f2");
    
    TIter next_1(f1.GetListOfKeys());
    TIter next_2(f2.GetListOfKeys());
    TKey *k_1;
    TKey *k_2;

    k_1=(TKey*)next_1();
    k_2=(TKey*)next_2();
    
    char *buf_1 = k_1->GetBuffer();    
    char *buf_2 = k_2->GetBuffer();

The program was crashed when I tried to access contents in buf_1 and buf_2 and I got error message
segmentation violation.

What is the right way to read buffer of object from root file?

Hi,

The first option ‘should’ have worked. Do you have a running example showing its failure?

Cheers,
Philippe.

Hi Philippe,
Thanks for your reply. The code is shown below.


#include <iostream>
#include "TFile.h"
#inlcude "TKey.h"
#include "TCollection.h"
#include "TBuffer.h"

int main()
{
	TFile f1("f1");
	TFile f2("f2");
    
	TIter next_1(f1.GetListOfKeys());
	TIter next_2(f2.GetListOfKeys());
	TKey *k_1;
	TKey *k_2;

	k_1=(TKey*)next_1();
	k_2=(TKey*)next_2();
    
	Long64_t f1_seek_key = k_1->GetSeekKey();
	Long64_t f2_seek_key = k_2->GetSeekKey();


	Int_t nbytes_1 = k_1->GetNbytes();
	Int_t nbytes_2 = k_2->GetNbytes();

	char *buf_1 = new char[nbytes_1];    
	char *buf_2 = new char[nbytes_2];

	f1.ReadBuffer(buf_1, f1_seek_key, nbytes_1);
	f2.ReadBuffer(buf_2, f2_seek_key, nbytes_2);

	if (buf_1[0]) printf("success\n");
	else printf("fail\n");
}

f1 and f2 are two small root files. The program will print “fail”, thus I assume that the buf_1 is empty