Process data with saved cuts as cut.C

main macro use_cutg.C:

static TCutG *cutg = new TCutG("cutg",6);

void use_cutg()
{
   gROOT->ProcessLine(".x xy.C");
   cutg->Draw();
}

sub macro xy.C defining the cut:

{
   cutg->SetVarX("X");
   cutg->SetVarY("Y");
   cutg->SetTitle("cutg");
   cutg->SetFillColor(1);
   cutg->SetPoint(0,-2.61572,1.1489);
   cutg->SetPoint(1,-0.253134,4.13603);
   cutg->SetPoint(2,2.66393,1.5625);
   cutg->SetPoint(3,-0.084378,-2.38971);
   cutg->SetPoint(4,-2.68804,1.05699);
   cutg->SetPoint(5,-2.61572,1.1489);
}

try:

root[0]  .x use_cutg.C

It will define the cut in xy.C and draw it in use_cutg.C

Thanks :slightly_smiling_face:

Is there any way to add a circular cut while filling a 2D: like the following:

TEllipse *ellipse = new TEllipse(180.0,180.0,5.0,5.0,0.0,360.0,0.0);

The Cut has to be a TGraph.
You will need to define yourself a circle.

Suppose I have made something like this:

   const Double_t tm=0., tmx=TMath::TwoPi();
   const Int_t n = 500;
   const Double_t st = (tmx-tm)/(n-1);
   std::vector<Double_t> xArr(n), yArr(n);
   for(Int_t i = 0; i < n; ++i)
   {
      xArr[i] = 2.0-3.0*sin(tm + i * st);
      yArr[i] = 2.0-3.0*cos(tm + i * st);
   }
   TGraph *gr = new TGraph(n, &xArr[0], &yArr[0]);

Then how to put it together with the previous cut? Like the following?

tree->Draw("Y:X>>sum","cutg && gr"," ");

when I do so, it is giving an error:
Error in TTreeFormula::Compile: Bad numerical expression : “gr”

{
   TCutG *cutg2 = new TCutG();
   const Double_t tm=0., tmx=TMath::TwoPi();
   const Int_t n = 500;
   const Double_t st = (tmx-tm)/(n-1);
   for (Int_t i = 0; i < n; ++i) cutg2->SetPoint(i, 2.0-3.0*sin(tm + i * st), 2.0-3.0*cos(tm + i * st));
}

Again it is showing:

Error in TTreeFormula::Compile: Bad numerical expression : “cutg2”

Is it because I am doing without leaf information!!

Yes you should add SetVArX/Y

I have added.
cutg2->SetVarX(“x”);
cutg2->SetVarY(“y”);
How do I use it in determining ‘r’ (x^2+y^2=r^2)?

I am not sure I understand your question. The 2nd cut (cutg2) is defined as a circle with the little macro you wrote. You can use this cut on x and y in the Draw command. Seems to me you have all you need ? …

How can I use ‘x’ and ‘y’ tree variable inside cutg2?

   TCutG *cutg2 = new TCutG("cutg2",500);
   cutg2->SetVarX("x");
   cutg2->SetVarY("y");
   double r = sqrt(pow(x,2.0)+pow(y,2.0)); 

How to implement this?? so that I can use leaf information in creating the gate and add it further in a 2D. e.g.

tree->Draw("Y:X>>sum","cutg && cutg2"," ");

I am not sure it will answer you question but in tree->Drawyou can use expression of the tree variables. For instance;

tree->Draw("sqrt(pow(x,2.0)+pow(y,2.0))", "cutg && cutg2", " ");

I want to put the circular cut (cutg2) which uses “sqrt(pow(x,2.0)+pow(y,2.0))” [where x and y are tree variables] and use this cutg2 while drawing different 2D.

TCut

Expressions of tree variables (in your case the radius of the circle) can also be put directly in the selection field of tree->Draw . They can also be encapsulated in a TCut as @Wile_E_Coyote suggested.

That means the small macro for cutg2 is no longer useful in such case where I want to use tree variable in writing an equation inside cutg!

That’s right. There is several ways (as often with ROOT) to get the result.

Ok…according to the current suggestion, when I do:

tree->Draw(“X:Y>>xy”, “cutg && (TMath::sqrt(TMath::pow((p1-2.0),2.0)+TMath::pow((p2-2.0),2.0))=3.0)”, " ");

(note: p1 and p2 are other two tree variables)

it gives again "Error in TTreeFormula::Compile: Bad numerical expression : “TMath::sqrt(TMath::pow((p1-2.0),2.0)+TMath::pow((p2-2.0),2.0))=3.0"”

"TMath::Abs(TMath::Sqrt(TMath::Power(p1 - 2., 2.) + TMath::Power(p2 - 2., 2.)) - 3.) < 0.1"

Here is a test with the ntuple produced by hsimple.C:

TCutG *cutg = new TCutG("cutg",6);
cutg->SetVarX("px");
cutg->SetVarY("py");
cutg->SetTitle("cutg");
cutg->SetFillColor(1);
cutg->SetPoint(0,-2.61572,1.1489);
cutg->SetPoint(1,-0.253134,4.13603);
cutg->SetPoint(2,2.66393,1.5625);
cutg->SetPoint(3,-0.084378,-2.38971);
cutg->SetPoint(4,-2.68804,1.05699);
cutg->SetPoint(5,-2.61572,1.1489);
ntuple->Draw("px:py","cutg && TMath::Abs(TMath::Sqrt(TMath::Power(px - 2., 2.) + TMath::Power(py - 2., 2.)) - 3.) < 0.1", "colz")