# Using TCutG with TTree::Draw() returns "bad numerical expression" error

**URL:** <https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167>\
**Category:** Newbie\
**Created:** [April 29, 2020, 3:42pm UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167 "2020-04-29T15:42:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Galdiano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/galdiano/32/19626_2.png) [@Galdiano](https://root-forum.cern.ch/u/Galdiano)\
**Post date:** [April 29, 2020, 3:42pm UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167/1 "2020-04-29T15:42:49Z")

</div>

Hello there!

I’m currently trying two draw a 2D histogram while applying a TCutG to it. This TCutG is generated by static casting a TGraph object save in a separate root file. The code is as follows:

```auto
void GraphCutTest( int run ){

  TFile* cut_file = new TFile("LowBeCut.root", "UPDATE");
  TCutG* graph_cut = nullptr;
  TString cut_name = Form("lowBecut_%d", run);
  
  if ( cut_file -> IsOpen() ){
    graph_cut = static_cast<TCutG*>( cut_file -> Get(cut_name) );
    cut_file -> Close();
  }

  TFile* data_file = new TFile(Form("runs/run_%d.root", run), "UPDATE");
  TTree* reco; data_file -> GetObject("reco", reco);

  graph_cut -> SetName("graph_cut");
  graph_cut -> SetVarX("baseline_mean[31] - ymin[31]");
  graph_cut -> SetVarY("baseline_mean[30] - ymin[30]");

  reco -> Draw("baseline_mean[30] - ymin[30]:baseline_mean[31] - ymin[31] >> hist", "graph_cut", "colz");
}

```

However, while the macro loads properly, running it returns the following:

```auto
Error in <TTreeFormula::Compile>: Bad numerical expression : "graph_cut"
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {baseline_mean[30] - ymin[30]:baseline_mean[31] - ymin[31] ,graph_cut}

```

I’m not sure what is going on here. I’ve looked around and I feel I’ve implemented the TCutG object appropriately. Any help would be deeply appreciated.

Here’s a [link with the files used in the code](https://mega.nz/folder/pxwkGAxJ#_SXm1sVGlDZMzzI-t0h6Ig) if anyone wishes to reproduce the error. I’ve also anexed the full macro used here.  
[LowBeToF.C](https://root-forum.cern.ch/uploads/short-url/bdk9ZmIA5IAlHXKlmnR9BwE177M.C) (7.9 KB)

* * *

_ROOT Version:_ 6.18/04  
_Platform:_ 18.04.4 LTS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [April 30, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167/2 "2020-04-30T13:52:03Z")

</div>

@couet or @moneta maybe you could help here please?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 30, 2020, 3:13pm UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167/4 "2020-04-30T15:13:03Z")

</div>

I could reproduce this behaviour with the standard ntuple in hsimple.root. The way to fix it was to write the macro the following way:

```auto
{
  auto hsimple = new TFile("hsimple.root");
  auto ntuple = (TNtuple*)hsimple->Get("ntuple");

  auto *g = new TGraph();
  g->SetPoint(0,0.,0.);
  g->SetPoint(1,1.,1.);
  g->SetPoint(2,0.,1.);
  g->SetPoint(3,0.,0.);

  auto cg = new TCutG("cg");
  for (int i=0; i<g->GetN(); i++) cg->SetPoint(i, g->GetPointX(i), g->GetPointY(i));

  cg->SetVarX("px");
  cg->SetVarY("py");

  ntuple->Draw("py:px","cg","col");
}

```

 ![Screenshot 2020-04-30 at 17.11.32](https://root-forum.cern.ch/uploads/default/original/3X/8/1/816285f77b434d2001c3ef26686956f3b38e24ec.png)

---

<div class="post-metadata">

**Author:** ![Galdiano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/galdiano/32/19626_2.png) [@Galdiano](https://root-forum.cern.ch/u/Galdiano)\
**Post date:** [April 30, 2020, 9:49pm UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167/5 "2020-04-30T21:49:26Z")

</div>

@couet Thank you very much, this solved my problem. However, I had to slightly change the code, because I was getting an error stating `no member named 'GetPointX' in 'TGraph'`. Do you know why that may be the case?

Anyway, the problem is solved and I can progress with my analysis. Thank you very much.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 4, 2020, 10:00am UTC](https://root-forum.cern.ch/t/using-tcutg-with-ttree-draw-returns-bad-numerical-expression-error/39167/6 "2020-05-04T10:00:18Z")

</div>

I guess the ROOT version you are using does not have [GetPointX](https://root.cern/doc/master/classTGraph.html#a00072523988fdbdee7fa533a678ee27e).
