Access TCutG's polygon points

Hello,

I am interested in accessing the polygon points of a saved TCutG in python.
Either by using pyroot, uproot or directly parsing the cut file.

python 3.10
Root 6.08.6 (for technical reasons)

Thank you very much in advance!

From the TCutG documentation, a TCutG can be handled like a TGraph, so you can use methods like GetN, GetPoint, GetPointX, GetPointY, GetX, GetY, etc. For example:

 {
  TCutG *cutg = new TCutG("mycut",6);
  cutg->SetVarX("y");
  cutg->SetVarY("x");
  cutg->SetPoint(0,-0.3586207,1.509534);
  cutg->SetPoint(1,-1.894181,-0.529661);
  cutg->SetPoint(2,0.07780173,-1.21822);
  cutg->SetPoint(3,-1.0375,-0.07944915);
  cutg->SetPoint(4,0.756681,0.1853814);
  cutg->SetPoint(5,-0.3586207,1.509534);
  cutg->Draw();
  double x,y;  // for ROOT 6.08
  for (int i=0; i<cutg->GetN(); ++i) {
    cutg->GetPoint(i,x,y);
    cout << "i=" << i << " (x,y) = " << x << " , " << y << endl;
  }
/*
  // newer ROOT (>=6.20)
  for (int i=0; i<cutg->GetN(); ++i) {
    cout << "i=" << i << " (x,y) = " << cutg->GetPointX(i) << " , " << cutg->GetPointY(i) << endl;
  }
*/
}

EDIT: In ROOT 6.08, TGraph does not have GetPointX and GetPointY, but it has GetPoint.

Dear dastudillo,

thank you very much for your help. Unfortunately, it does not have a GetPoint method (or anything close) if I check them with dir(object).

But maybe my way of loading the cut file is wrong. I treat it as a TFile and run

f = ROOT.TFile.Open('cut.cuts','READ')

assuming my saved cut is called cut.cuts and has a cut named cut. I can then do

f.Get('cut')

but also this does not have any of the methods you mentioned. Is is possible that opening it via TFile.Open() is not correct?

Thank you very much!

How exactly are you saving the cut in a file? and getting it? It’s better to post the actual lines of code you are using.
By the way, if you are creating the cut graphically (by clicking on a canvas), it might be simpler to just save the canvas as a .C file and get the code of the TCutG from there (from which you can see all the points), and then just add that code to any other macro, or save that piece of code (removing anything other than the TCutG code, of course) alone, to include in other macros.

Any ROOT version:
cout << "i=" << i << " (x,y) = " << cutg->GetX()[i] << " , " << cutg->GetY()[i] << endl;

@TB-IKP Try: root -b -q cut.cuts -e 'gFile->ls();'

Dear dastudillo and Wile_E_Coyote,

thank you very much for your help and please excuse the late reply. I am using the GUI to create my 2d cuts and save them with the SaveTo option. To make things worse, I do not even use plain ROOT but another program which is based upon ROOT 6.08. Unfortunately, it redefines the TCutG class (I know :frowning:) so @Wile_E_Coyote’s answer does not work (at least not straight out of the box).

But I played around a little bit and your suggestions help me to extract the points I need! The only improvement would be to directly load the cut file into python and extract the data. But this is not necessary because basically I can get the information that I need.

Thank you!

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