Drawing a selected range of bins

Hello!

I am trying to superimpose a plot in a 3D histogram.
The command, h25->Project3D(“z”)->Draw();, works fine;
the 1D projection of the histogram is plotted.
But the command,
h24->Project3D(“z”)->Draw(“x>0 && 0<y<20” “same”);,
does not work.
This command just plot another one instead of superimposing to the previous one.
Furthermore, all the events are plotted instead of events in 0<y<20.

There is no example of using Project3D in the tutorials directory in root.
I hope to get some help on this problem.

Best Regards,
Sangryul Ro

I tried it many ways.
It is slightly different from the tree/ntuple1.C in root tutorial area.
It is very difficult problem now.
Cheers

I do not understand what you want to do with:

h24->Project3D("z")->Draw("x>0 && 0<y<20" "same");, 

Draw() with selection is valid only on trees/ntuples not on histograms.

With h24->Project3D(“z”)->Draw(“x>0 && 0<y<20” “same”);, I want to super impose an one dimensional plot containing events in 0<y<20 to the previows one.
Cheers

use SetRange or SetRangeUser (see TAxis):
root.cern.ch/root/html/TAxis.html#TAxis:SetRange

   h->GetXaxis()->SetRange(21,34);
   h->GetYaxis()->SetRange(10,26);

It’s not the range problem.
Even the code, h25->Project3D(“z”)->Draw(“same”);, does not work.
The plot is not overlayed to the previous one,
h24->Project3D(“z”)->Draw();.
It just replaces the previous one.
This seems because of the projection of 3D histogram.
The commands,
h20->Draw();
h21->Draw(“same”);, works fine; the second plot overlays to the previous one.

Cheers

can you send a small running example showing the problem ?

The running example is simple.

The commands for 1D histogram,
h20->Draw();
h21->Draw(“same”);
, works fine; second plot overlays to the previous one.
But the commands for 3D histogram,
h24->Project3D(“z”)->Draw();
h25->Project3D(“z”)->Draw(“same”);
, does not work.
The second plot is not overlayed to the previous one.

Cheers

It works for me. Try:

void proj3D()
{
   TCanvas *c1 = new TCanvas("c1","demo bin labels",0,0,500,500);
   TH3F *h1 = new TH3F("h1","h2",30,-3,3,30,-3,3,30,-3,3);
   TH3F *h2 = new TH3F("h2","h3",30,-3,3,30,-3,3,30,-3,3);
   Double_t x,y,z;
   gRandom->SetSeed();
   for (Int_t i=0;i<15000;i++) {
      x = gRandom->Rndm();
      y = gRandom->Rndm();
      z = gRandom->Rndm();
      h1->Fill(x+1,y+1,z+1,1);
      h2->Fill(x-1,y-1,z-1,1);
   }
                                                                                
   TH1 *hpx1 = h1->Project3D("z"); hpx1->SetLineColor(kRed);
   TH1 *hpx2 = h2->Project3D("z"); hpx2->SetLineColor(kBlue);
   hpx1->Draw();
   hpx2->Draw("same");
}

Thanks for your effort.
A strange behavior is found with my two root files containing two different 3D histograms .

TH1 *hpx1 = h24->Project3D(“z”); and hpx1->Draw(); works fine.
A one dimensional histogram is ploted.
After then TH1 *hpx2 = h25->Project3D(“z”); replaces the previous plot, eventhough hpx2->Draw(); is not issued.
The hpx1 is not drawable afterwards; hpx2 is drawn always.

Cheers

As I said earlier, if you want more help, send a small running example reproducing the problem…

I now attach a rootlogon.C file and two root files.
After the rootlogon.C, hpx2->Draw(); or hpx1->Draw(); has no effect.
Cheers
analysis2.root (53.1 KB)
analysis1.root (53.6 KB)
rootlogon.C (564 Bytes)

Your problem comes from the fact that the TH3 histograms have the same name. So when you load the 2nd one it replaces the 1st one. Do:

{
   TCanvas *c = new TCanvas("c", "AngDetPHbbIM",1);
   TFile fh("analysis1.root");
   TFile fq("analysis2.root");
                                                                                
   TH3 *h24  = (TH3*)fh.Get("ScatDetPHbIM");
   h24->SetName("abc");
   TH1 *hpx1 = h24->Project3D("z");
   hpx1->SetLineColor(kRed);
   hpx1->Draw();
                                                                                
   TH3 *h25 = (TH3*)fq.Get("ScatDetPHbIM");
   TH1 *hpx2 = h25->Project3D("z");
   hpx2->SetLineColor(kBlue);
   hpx2->Draw("same");
}

Thanks very much.
It works now.
There was no problem with TH1 histograms eventhough
they had the same name though.
The SetRange function is also useful;
it enabled to select some range of bins.

Cheers