Is TH1D construction from a TProfile forbidden?

Dear all,

I have found problems when initializing a TH1D from a TProfile:

TH1D* pHist = new TProfile("Profile", "Test profile", 0., 10., 10); TH1D H(*pHist);

creates memory corruption (try putting it in a loop in case just one time is not enough).
I had expected that to be supported. Reading the code it looks to me that it can’t be supported by design.

Also, any good workaround?

Hi,

you cannot construct a TH1D from a TProfile, but you can use the project method.

TH1D * h1 = pHist->ProjectionX();

See also root.cern.ch/root/htmldoc/TProfi … rojectionX

Best Regards

Lorenzo

So the “best” way for creating a static histogram involves a temporary object?

TH1D* pHTemp = pHProfile->ProjectionX(); TH1D Hist(*pHTemp); delete * pHTemp;

or, for an existing TH1,

TH1D* pHTemp = pHProfile->ProjectionX(); pHTemp->Copy(Hist); delete pHTemp;

Hi,
I have not understood your question and what you want to do. Your object “hist” is not static, it is just not allocated from the heap.

If you want to re-use an existing histogram object, you will need to use the Copy method after projecting in a temporary one. You cannot avoid the creation of the temporary histogram.

Best Regards

Lorenzo

Yes, I’m not sure how I’d call it; it’s not static, it’s not on the heap… will I call it “on the stack”, or local?

Anyway you answered me saying that I can’t avoid creating a temporary object “on the heap”.