Hello everyone, Actually I have 2 problems:
1/In my program I store an Histogram (TH2D) in a TFile after filling it with about 21000000 values at the end of the program a message appears ( Exception: Bad allocation)
2/when I try a lower value and I open the output Root file and I would like to visualize the Histogram, the window goes frozen, it blocks and I have to wait to see the histogram. Is it normal?? or I’m doing some thing wrong?
Thank you in advance.
[quote=“Momos1989”] Is it normal?? or I’m doing some thing wrong?
No, it’s not normal. It’s difficult/not possible to say what you are doing wrong, since you gave no details. 21*10^6 entries means nothing, number of bins you have - is important.
If you manage to have a bad_alloc expection with just 1 histogram, you probably have a huge histogram. Try to do a simple arithmetic yourself: (nBinsX + 2) * (nBinsY + 2) * sizeof(double) and you’ll have a ~ number of bytes you are trying to allocate.
P.S. BTW I can see in another topic created by you, Olivier has already explained you the “magic formula”, so, are you still trying to allocate ~ hundreds GB of memory?
What I try to do is to write a 2-D histogram of about nybins=40 and nxbins=570000 in a root file!!
and I use the instruction Fill(xbin,ybin,w) // for each pair (x,y) I assign a value w
[quote=“Momos1989”]What I try to do is to write a 2-D histogram of about nybins=40 and nxbins=570000 in a root file!!
and I use the instruction Fill(xbin,ybin,w) // for each pair (x,y) I assign a value w[/quote]
Ok, using the formula Olivier an I kindly provided you with, you can estimate, that the size of such a hist is ~170 mb. This can not cause a bad_alloc exception on 99% of modern computers/oses/normal conditions.
So, either your point about bad_alloc is completely irrelevant in this topic, or you have to finally provide us with a minimal working example reproducing you problem or you do the job yourself and find a memory leak in your code. One such a histogram (170 mb) itself can not be the reason for a bad_alloc in 99% of cases nowadays.
Next, about slow drawing. Before you say: “it’s slow”, please, tell what draw option you are using, since this is also important: the standard “scatter plot” uses a “point cloud” to show how “dense” the bin is, and every point’s coord is created with a random number generator. Just imagine ~22000000 * “density” 2D points generated by random number generator and how hard this work will be later for X11/Win GDI to render?
And the “lego” of such sizes is something unimaginable in all respects at all.
And just as a hint - I do not think you have a display with a resolution of 570000 x N pixels, right? So I’m just a little curious - how do you expect this to be useful in any way? Do you expect a bin on a screen be a small fraction of a pixel or what??
Just try to do simple calculations to estimate the complexity.
I tried a code with only the part where I fill the histo and it works perfectly. But in my original code I use many histograms in a loop, at the end of the loop I make a delete histo to the different histograms, and re define them again in the beginning of the loop I redefine them in this way:
Could this be the source of the problem??
Because I can’t define the Histograms before the loop, they use some data in other histograms to specify the lenght and number of bins.
histo3=new TH1D(“histo3”, “histo3”, histo2->GetNbinsX()/2, 0, histo2->GetXaxis()->GetXmax()/2);
No, the “code” you demonstrated while having invalid C++ syntax and unnecessary dynamic memory allocations can not result in a memory leak.
That’s funny: you start from complain about bad_alloc and slow drawing with 2D hist, we’ve explained you that 2D hists with huge number of bins can lead to bad_alloc since they can potentially consume non-existent amount of memory and it impossible to generate and draw billions of points fast - and you, as a reply, show some irrelevant piece of code with a loop and … 1D hists?
Thank you I solved the problem, it occurs because I didn’t made a delete for a pointer at the end of the loop and reusing it again in the coming iteration.
concerning the piece of code I sent, I was using TH1Ds and making some operations and then storing the results in TH2D
Thank you again for your help! I appreciate it!