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

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:

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:

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 if anyone wishes to reproduce the error. I’ve also anexed the full macro used here.
LowBeToF.C (7.9 KB)


ROOT Version: 6.18/04
Platform: 18.04.4 LTS
Compiler: Not Provided


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

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 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");
}

1 Like

@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.

I guess the ROOT version you are using does not have GetPointX.