Merging many histograms in loop from one root file


I am trying to merge 360 histograms that are saved in on .root file.

The histograms are TH3D named this way:

I was trying to use this example:

void atest() { TH1F *h1 = new TH1F("h1","h1",110,-110,0); TH1F *h2 = new TH1F("h2","h2",220,0,110); TH1F *h3 = new TH1F("h3","h3",330,-55,55); TRandom r; for (Int_t i=0;i<10000;i++) { h1->Fill(r.Gaus(-55,10)); h2->Fill(r.Gaus(55,10)); h3->Fill(r.Gaus(0,10)); } TList *list = new TList; list->Add(h1); list->Add(h2); list->Add(h3); TH1F *h = (TH1F*)h1->Clone("h"); h->Reset(); h->Merge(list); h->Draw(); }
But adding my 360 histograms to the list fills up my memory after looping through 30.

What is the proper way to do this?


  1. you build of list of let say 10 histogram taken from the root file
  2. you merge this list into the resulting histogram
  3. you delete the list and the histograms in it (in memory)
  4. you start again from 1) until you have scan all the histogram in the root file

In the above example, to delete the list and histograms from memory will this work:

delete list;

and also the histograms you put in memory to free the space.