Dear expert,
I want to save the TH2Poly into a tree and read them in the other place.
The saving procedure works fun. But when I try to read the histograms, memory
occupy grows very fast.
Here are three simple scripts to show the problem:
testTH2Poly.C – Create a TH2Poly, fill it and then delete, no memory leak.
#include "TH2Poly.h"
#include "TRandom.h"
#include <iostream>
void testTH2Poly()
{
for(int i = 0; i < 1000; ++i)
{
TH2Poly* th2 = new TH2Poly();
th2->Honeycomb(0, 0, 1, 50, 50);
for(int j = 0; j < 1000; ++j)
{
th2->Fill(gRandom->Gaus(0, 20), gRandom->Gaus(0, 20));
}
delete th2;
std::cout << i << std::endl;
}
}
genTH2Poly.C – Create a TH2Poly, fill it and then save into a tree, no memory leak.
#include "TH2Poly.h"
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include <iostream>
void genTH2Poly()
{
TFile* fout = new TFile("TestTH2Poly.root","recreate");
TTree* tree = new TTree("test","");
TH2Poly* th2 = new TH2Poly();
tree->Branch("TH2", &th2);
for(int i = 0; i < 1000; ++i)
{
delete th2;
th2 = new TH2Poly();
th2->Honeycomb(0, 0, 1, 50, 50);
for(int j = 0; j < 1000; ++j)
{
th2->Fill(gRandom->Gaus(20, 10), gRandom->Gaus(20, 10));
}
tree->Fill();
std::cout << i << std::endl;
}
fout->cd();
tree->AutoSave();
fout->Close();
}
readTH2Poly.C – Loop all the TH2Poly from a tree created by genTH2Poly.C, and do nothing. Even I have enable SetAutoDelete(), the memory occupy still grows very fast. Loop a 130M file causes 1.4G memory usage.
#include "TH2Poly.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
void readTH2Poly()
{
TFile* fin = new TFile("TestTH2Poly.root");
TTree* tree = (TTree*)fin->Get("test");
TH2Poly* th2 = nullptr;
TBranch* br_poly = nullptr;
tree->SetBranchAddress("TH2", &th2, &br_poly);
br_poly->SetAutoDelete(true);
Long64_t numOfEntries = tree->GetEntries();
for(Long64_t entry = 0; entry < numOfEntries; ++entry)
{
tree->GetEntry(entry);
std::cout << entry << std::endl;
br_poly->DropBaskets("all");
}
}
Do you have any idea about that?
Best regards,
Ruiting
ROOT Version: v6.22/08
Platform: Linux
Compiler: cling/g++