TGraph TMarker display issues

Hello,

I've got a strange issue using pyroot. I am trying to color the dots of a TGraph using a loop and TMarker, however, with the attached code, I never see the TGraph, and the TMarker show up individually on the canvas. Ie, the dots are painted sequentially, but only the last tmarker in the loop is saved in the output png. The translated code works great in a C macro, however, I can't get it to work in python. Any ideas? Thanks,

Zig



test.py (785 Bytes)

Can you also post the C++ code ?

Sorry, I’ve just cut down that first uploaded code to the relevant parts so that it plots…

Zig



test.C (832 Bytes)

Yes it looks ok. I have just cleaned it up a little bit:

void test() {
  TCanvas *c1 = new TCanvas("c1","Core Deviation Comparison",200,10,900,700);

  const int nhit = 10;
  double xpmt[nhit], ypmt[nhit], charge[nhit];
  for ( Int_t i = 0; i < nhit; i++)
  {
    xpmt[i] = (double)i;
    ypmt[i] = .5;
    charge[i] = (double)i/2;
  }

  TGraph *event = new TGraph(nhit,xpmt,ypmt);
  event->GetXaxis()->SetTitle("X (m)");
  event->GetYaxis()->SetTitle("Y (m)");
  event->SetTitle("TEST");
  event->Draw("AL");
 
  Double_t *x = event->GetX();
  Double_t *y = event->GetY();
 
  for (Int_t j = 0; j < nhit; j++) {
    TMarker *m = new TMarker(x[j],y[j],8);
    double q = log10(charge[j]);
    m->SetMarkerSize(q*4);
    m->SetMarkerColor(11+q);
    m->Draw();
  }

  c1->Update();
  c1->Print("CTEST.png");
}

Yeah, I know it works just fine in C. It’s just that I have a project in python which I need to execute basically the exact same code but even the simple example I posted first in python doesn’t reproduce the plot like the C code example. Any ideas?

Zig

Hi,

the TMarker in the loop gets deleted, as its reference gets overwritten in each subsequent iteration, so you only see the last. To keep all markers around, store them in a list fo the duration necessary. Easiest is to add a data member to the TGraph (to bind their life times):[code]view._markers = []

… later, in loop

view._markers.append(mark)[/code]
Cheers,
Wim

Thanks wlav! That worked exactly as I was hoping. Why does it work in C via the same deleting of the TMarker?

Cheers,

Zig

Hi,

there is no (explicit) deletion of the markers in the C++ code. As per the script, markers are allocated on the heap and ownership is assumed to be taken by the current pad (which will indeed delete them on its destruction). This is the case for virtually all graphics objects.

Cheers,
Wim