Inset for same plot (histogram + TGraph)

Dear ROOT experts,

I superimposed a stack of histograms and a TGraph on the same plot with two different vertical axes. Now I want to zoom in into a particular x axis range and place it as an inset figure. I have seen many examples of an inset which is different from the actual main plot but none with the same situation as mine.
How can I create an inset of the same plot but zoomed into a certain x axis range?

Thank you,
Amin


_ROOT Version: 6.08/06
_Platform: Ubuntu 16.04
Compiler: Not Provided


{
  TCanvas *c1 = new TCanvas("c1","c1",200,10,700,500);

  TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);

  gRandom->SetSeed();
  Float_t px, py;
  for (Int_t i = 0; i < 25000; i++) {
     gRandom->Rannor(px,py);
     hpx->Fill(px);
  }
  hpx->Draw();


  TPad *p1 = new TPad("p1","p1",.15,.3,.5,.7);
  p1->Draw();
  p1->cd();
  TH1F *hc = (TH1F *)hpx->DrawCopy();
  hc->GetXaxis()->SetRangeUser(0,1);
}

Dear @couet,

Thank you for the reply. I should have given more info as this doesn’t exactly work for me.
I have a bunch of background histograms which I read from a separate ROOT file. I draw them (with no stack) and then draw the signal histogram on top (also read from a ROOT file). The structure of the code looks like this:

THStack *hs = new THStack("hs",title);
.
.
hs->Add(h[iprocess]);
TCanvas *c = new TCanvas("c","Stacked Background Hists");
hs->Draw("HIST NOSTACK");
.
.
TH1 *signalh = (TH1F*)signalf->Get(histoname);
signalh->Draw("HIST SAME");

THStack does not have the DrawCopy() option. Is there a way around that?

Thank you,
Amin

What do you want in the inset ? signalh ?

Hi @couet,

I want both signalh and the hs (stack).

Thank you,
Amin

So the stack hs should appear in the main plot and in the inset ? that’s it ?

Hi @couet,

Yes that is correct.

{
   TCanvas *c1 = new TCanvas("c1","c1",200,10,700,500);

   TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);

   THStack *hs = new THStack();
   hs->Add(hpx);

   gRandom->SetSeed();
   Float_t px, py;
   for (Int_t i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      hpx->Fill(px);
   }
   hs->Draw();

   TPad *p1 = new TPad("p1","p1",.15,.3,.5,.7);
   p1->Draw();
   p1->cd();
   p1->DrawFrame(0.,500.,1.,800);
   hpx->Draw("SAME");
}
2 Likes

Thank you very much. It works!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.