Re: array of identical histos read from a file

From: Rene Brun <>
Date: Sun, 01 Oct 2006 14:23:12 +0200

Hi Chiara,

You should not delete the histogram that you store in the TObjArray! Anyhow, what you try to do is very inefficient:   -it takes a lot of space in memory
  -it is difficult to retrieve one single histogram.

You should save your histograms one by one in the file, but even that will be very inefficient. I recommend making a tree instead. See for example a proposal to create your Tree with 160000 histograms. Here I show only one single branch, but you can add as many branches as you like (one branch per histogram type).

void writing(){

   static const Int_t size =160000;
   TFile *fileout = new TFile("outTH1C.root","RECREATE"); //output file    TFile file("inTH1C.root","READ"); //input file    const TH1C *hsample = (TH1C*)file.Get("DeltaTAll90"); // sample histo    fileout->cd();
   TH1C *hciccio=0;
   TTree *T = new TTree("T","Tree with histograms");    T->Branch("h","TH1C",&hciccio);
   for (Int_t i=0;i<size;i++){

      if(i%1000 == 0)printf("i=%d\n",i);
      hciccio = new TH1C(*hsample);
      delete hciccio;


When reading, you can fetch in memory very quickly any histogram from the Tree
with eg

   TFile *f = new TFile("outTH1C.root");    TTree *T = (TTree*)f->Get("T");
   TH1C *h=0;

This technique is minimum 10 times faster than writing them in a directory. Reading is also much much faster and the file produced much smaller.

Rene Brun

Chiara Zampolli wrote:
> Dear rooters,
> sorry for the long (and I hope not too messy) mail, but I have some
> problems working with the HEAD ROOT..
> In order to determine the size of a file containing an array of
> ~160000 histos (i am studying 2 different cases: TH1C and TH1F) which
> will be filled during real data taking, I am trying to make it easier
> simulating them starting from a unique histo I have stored in a file
> (for the time being, it is not important whether the histograms are
> all the same). You find the sample histos in the attached files. For
> this reason, I have tried to follow different ways:
> 1) reading the sample histo from the file where it is stored, and
> copying it 160000, adding each time the histo to the array:
> void writing(){
> static const Int_t size =160000;
> TFile *fileout = new TFile("outTH1C.root","RECREATE"); //output file
> TFile file("inTH1C.root","READ"); //input file
> const TH1C *hsample = (TH1C*)file.Get("DeltaTAll90"); // sample histo
> fileout->cd();
> TObjArray harray;
> harray.SetName("array");
> for (Int_t i=0;i<size;i++){
> TH1C *hciccio = new TH1C(*hsample);
> harray.Add(hciccio);
> delete hciccio;
> hciccio =0;
> }
> cout << "out of the loop, entries in the array = " <<
> harray.GetEntries() << endl; // checking printings
> harray.Write("array",TObject::kSingleKey);
> cout << "written array = " << &harray << endl; //checking printings
> fileout->Close();
> }
> This macro ends with a "*** Break *** segmentation violation...."
> error, at the point where the array has to be written, but i don't
> know why...
> 2) without deleting each time the hciccios, then the macros goes on
> becoming slower and slower each histo is added (~40 mins to fill the
> whole array!!!), but then it crashes as before... Again, why??
> 3) creating an array of histos, instead of a single hciccio:
> TH1C *hciccio[size];
> ...
> for (Int_t i=0;i<size;i++){
> hciccio[i]=new TH1C(*hsample);
> ....
> it crashes again, becoming also this time slower and slower.
> 4) In case I define only one copy of my sample, and then add it to the
> array 160000, the output file is too smal, i.e. ~8 KB for TH1F and a
> bit less for TH1C.
> 5) Since too many problems in trying to use a sample histo occurred, i
> have tried creating an array of 160000 DIFFERENT histograms, with 4000
> entries each. This time, it works. BUT!!!! only if the histograms are
> TH1F, when I try with TH1C, at the point of writing the array into the
> file, it crashes again.. Why?? What's the difference between the two???
> I think that one of the main problem could be that the file becomes
> too big when using a sample histogram (apart from case 4), which is
> quite "weird" for my inexperienced ears, since when creating new ones
> it works and has a reasonable size (~1.6 MB) - obviously only in the
> case of TH1F, in that of TH1C, I don't know, since it does not work...
> Could you help me in solving this problem? It's very lickely that I am
> not following the correct way, but I have no ideas...
> Thank you a lot.
> Best Regards,
> Chiara
Received on Sun Oct 01 2006 - 14:23:05 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:01 MET