Problem with drawing cloned histogram

Dear ROOTers
I have problem with drawing histogram. Inside of my class i have assignment operator where i make clone of histogram:

	fHisto = (TH1*)other.fHisto->Clone();

Later in macro I made something like

MyClass *A = new MyClassA();
MyClass B = *A;
delete A;
B->GetHistogram()->Draw();

However when I try to draw such histogram I get only white space with title. I call TCanvas::Update - but it doesn’t help. What is more strange - histogram contain data but those data cannot be drawn, also in fit panel new histograms are not available in list. Is it possible that this is some conflict of object names?

Hi,

this is more subtle than that: this is a C++ matter.
In your code you are not invoking an assignment operator (operator=) but a copy constructor (MyClass(const MyClass&)). Did you implement one?

Cheers,
Danilo

Hi,
I have also copy ctor that works in the same way :

	if(other.fHisto)
		fHisto = (TH1*)other.fHisto->Clone();

… and still have the same issue :frowning:

Hi,

this is an example I quickly put together: is it doing what you are trying to achieve?

// includes only necessary for aclic. Aclic necessary only when using ROOT5
#include "TH1F.h"
#include <iostream>

class MyClass{
public:
   MyClass():fHisto(new TH1F("h","h",64,-2,2)){
      fHisto->FillRandom("gaus");
   }
   MyClass(const MyClass& other){
      fHisto = (TH1*)other.fHisto->Clone();
   }
   ~MyClass(){
      cout << this << " " << fHisto << endl;
      delete fHisto;
   }
   TH1* GetHistogram() const{return fHisto;}
private:
   TH1* fHisto = nullptr;
};

void test(){
   auto A = new MyClass();
   MyClass B = *A;
   delete A;
   B.GetHistogram()->DrawClone();
}

Yes, this is what I want to get, but I would like to use “Draw” instead of “DrawClone”. I also don’t know why my code (witch is almost like yours) don’t draw content of such histogram even with DrawClone (I only see white space with title and frames).

Hi,

the DrawClone is there just to let the primitive (the histo in this case) survive after the scope of the test function.

Danilo