Compression ratio using TFile depending on how objects creat

I am writing some objects using TFile. For some reason I don’t understand I get different compression ratios depending on whether the objects I am writing are created on the stack or the heap.
For example, if I use the following code:

bx_event* bx= new bx_event();
bx->set_event(prim, secondary);
bx->Write();

I find that my files are compressed with a very high factor (e.g., 44)

However, if I use the following code

bx_event bx;
bx.set_event(prim, secondary);
bx.Write();

I find my files have a very low compression of approximately 3.93.

Why should I get different behavior if I create my objects on the heap and write them or on the stack?

Do you have any ideas?

Thanks.

Jon

Are you initializing all the members of your class?

Rene

Hi Rene,

Thanks for your quick reply. I think I am initializing all members of the class. In each of the two scenarios I sent to you earlier I believe I am performing the same operations. THe only difference is that in one case the object is created on the heap and the other on the stack. The initialization occurs in two places: (1) the constructor (this should be called in heap example from bx_event * bx= new bx_event() and in the heap example from bx_event bx); (2) the function set_event. This should be called (in heap scenario) by bx_event->set_event(a, b)) and (in stack example) by bx_event.set_event(a,b)). I don’t understand whyI should get different behavior.

I have found that if I manually turn off compression that the sizes of the objects written to disk are the same in both scenarios!

Any thoughts?

Thanks.

Jon

Could you send me the shortest possible running script showing this problem?

Rene

Hi Rene,

I am compiling not running scripts. Here are the relevant commands:

TFile *f = new TFile(“simdata.root”, “RECREATE”);
// f->SetCompressionLevel(0);

bx_event *bx = new bx_event;
bx_event->set_event(prim,secondary); //a and b are pointers to arrays with data
bx_event->Write();

//compare the above with:

bx_event b;
b.set_event(prim, secondary);

f->Close();

}

Here is the implementation file of the class bx_event so you can see what is getting called:

#include “bx_event.h”
#include <TROOT.h>
ClassImp(bx_event)

bx_event::bx_event()
{
num_points=512;
primary = new char[512];
proportional = new Double_t[512];
}

bx_event::bx_event(Int_t a){
num_points=512;
primary=new char[num_points];
proportional=new Double_t[num_points];
};

void bx_event::set_event(char *a, Double_t *b)
{
memcpy(primary, a, sizeof(char) * 512);
memcpy(proportional, b, sizeof(Double_t) * 512);

};

void bx_event::draw_event(TCanvas *canvas)
{

Double_t x[num_points], y_prim[num_points], y_sec[num_points];
for (Int_t i=0; i<num_points; i++) {
x[i]=i;
y_prim[i]=(Double_t) primary[i];
y_sec[i]=proportional[i];
}

canvas->cd();
TGraph *gr_prim=new TGraph(num_points, x, y_prim);
TGraph *gr_sec= new TGraph(num_points, x, y_sec);

gr_prim->Draw(“AC”);

}

Thanks for all of your help.

Best,

Jon

Your code does not compile (you did not send the include file).
You should not allocate objects in the default constructor.
See the files in the attachements and add
your missing bits.

Rene
bx_event.h (381 Bytes)
cx.cxx (950 Bytes)