Using Texec to show a TH1F from a TCanvas pad on another TCanvas

Hi,

I would like to make use of TExec (or AddExec()) to show a histogram (TH1F) plotted on a single pad of a TCanvas divided into several pads on another (larger) TCanvas for easier inspection.

What I tried so far is the following code:

TCanvas *c2 = 0;

void show()
{
  auto event = gPad->GetEvent();  
  if (event != 11)  return; 

  TObject *select = gPad->GetSelected();
  if (!select) return;
  if (!select->InheritsFrom(TH1::Class())) return;

  TH1F *h = (TH1F*)select; 
  
  c2->cd();
  h->Draw();
  c2->Update();
}

void testexec()
{
  TCanvas *c1 = new TCanvas("c1","c1",10,10,800,600);
  c1->Divide(4,3);

  c2 = new TCanvas("c2","c2",900,20,500,500);
  
  TH1F *h[12];
  for (int i=0; i<12; ++i) {
    h[i] = new TH1F(Form("h%d",i), Form("h%d",i), 100,0,1);
    for (int j=0; j<10000; ++j) h[i]->Fill(gRandom->Gaus(0.1+0.05*i,0.01*i+0.05));
    
    c1->cd(i+1);
    h[i]->Draw();
    gPad->AddExec("ex","show()");
  }
}

This doesn’t do anything, i.e. nothing happens when clicking on the histograms. If I comment out the ‘InheritsFrom’ line, strange stuff is drawn on c2, and the histograms on c1 vanish.

Could somebody help me with that?

Best regards and thanks,
Klaus


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/06
Platform: Not Provided
Compiler: Not Provided


Do you “click on the histograms” when the “mouse pointer” is changed into an “arrow tip” (i.e., not a “cross”)?

BTW. Use: h->DrawCopy();

Thanks for the hint.

When I use DrawCopy and click with the arrow tip (i.e. I’m exactly on the histogram line), it works. How could I get it working when the pointer is a cross? Is this the pad itself, from which I could retrieve the histogram?

You would need to “scan” the list returned by: gPad->GetListOfPrimitives()

That works, thanks! In case somebody is interested here the code:

void show()
{
  if (gPad->GetEvent() != 11)  return; 
  TH1 *h = 0;
  
  // histogram was clicked
  TObject *select = gPad->GetSelected();
  if (!select) return;
  if (select->InheritsFrom(TH1::Class())) h = (TH1F*)select;
  
  // pad was clicked
  if (h==0) {  
    TIter next(gPad->GetListOfPrimitives());
    while (TObject *obj = next()) 
      if (obj->InheritsFrom(TH1::Class())) h = (TH1F*) obj;
  }

  if (h!=0) {
    c2->cd();
    h->DrawCopy();
    c2->Update();
  }
}

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