Problem plotting cloned histograms

Hi!

I want to plot cloned histograms and since I’m doing this in a huge loop with a lot of histograms, I don’t want to use unique names for each - that’s why I’m using SetDirectory(0).

This is my code, where histA and histB are some TH2D* which have been loaded from a file or filled somehow:

      TCanvas* c = 0;
      TH1D* htemp = 0;
      c = new TCanvas();
      htemp = histA->ProjectionY("", 0, 1);
      htemp->SetDirectory(0);
      TH1D* histAprojy = (TH1D*)htemp->Clone();
      histAprojy->SetDirectory(0);
      delete htemp;
      histAprojy->Draw();
      htemp = histB->ProjectionY("", 0, 1);
      htemp->SetDirectory(0);
      TH1D* histBprojy = (TH1D*)htemp->Clone();
      histBprojy->SetDirectory(0);
      delete htemp;
      histBprojy->Draw("same");

But it’s drawing just drawing projection of histB twice and the one of histA not at all.
What am I doing wrong?

I do not see why you get twice the same thing. I suggest a simplication of your script below.

Rene

TCanvas* c = new TCanvas(); TH1D* htemp = 0; htemp = histA->ProjectionY("", 0, 1); htemp->DrawCopy(); delete htemp; htemp = histB->ProjectionY("", 0, 1); htemp->DrawCopy("same");