Error when attempting to use TCutG::IsInside function with TTree::Draw

I’m working on a script that attempts to select events from a tree using TTree->Draw() and a TCutG object. I do this by using TCutG->IsInside(x,y). I should then be able to plot 2D histograms (TH2F in my case) that show only data points that fall within the cut (as in the example here) along with, once that is working, other data associated with those selected events.

However, I am running into an issue using the IsInside function, as seen here:

tree->Draw(Form(Branch.CoordY:Branch.CoordX>>histo%d(2048,0,8192,2048,0,8192)", p), Form(“Branch.blah == %d” && (mycut->IsInside(Branch.CoordX, Branch.CoordY))", p), “goff”);

(Variable names shortened for clarity; the actual ones are pretty long).

Branch.CoordX and Branch.CoordY are floating-point numbers while mycut is a TCutG object created by a loop that loads coordinates from a file and then uses mycut->SetPoint() to turn those coordinates into a cut. The TCutG creation loop works fine in another script I have that draws it on the screen, so as far as I know the cut itself is fine.

However, this function results in a bad numerical expression/failure to compile, so I am assuming that I am likely not calling IsInside correctly. I am able to create a 2D histogram successfully if I only do the check on the value of the ‘blah’ variable.

I call goff because this code exists inside a loop that creates a bunch of histograms based on the value of another variable (hence the use of Form() in the second argument of Draw()) and then later on in the script they are written to disk, but I’m pretty sure that the presence of the goff option is not the problem. I include it here just in case.

Have I made a simple typo somewhere or have I misunderstood the proper use of the IsInside() function? What is the best way to go about doing this?

I got the idea to do the test this way from this old mailing list post.

Try: tree->Draw(TString::Format("Branch.CoordY:Branch.CoordX>>histo%d(2048,0,8192,2048,0,8192)", p), TString::Format("Branch.blah == %d && (mycut->IsInside(Branch.CoordX, Branch.CoordY))", p), "goff");

Unfortunately, doing this gives the same error.

Is there another way to perform this same test? Or, what is it about the IsInside function that is causing the draw to fail? It is strange that a static part of the Form() (or Format()) call is what is causing this to fail.

I am looking through other similar posts and it seems I’m not the only person to have issues with using cut objects in the Draw function.

To provide a little more detail, my loop first creates a TCutG object. Then it checks to see if point coordinates exist for the appropriate value of ‘blah’ (it’s a detector number, actually) and if they do, it then retrieves them from the file and uses TCutG->SetPoint() to create the cut, which is a closed polygon. Once that is done, it attempts to execute the TTree::Draw() call, which is the step that fails. It is then supposed to delete the TCutG object so that the next run of the loop won’t accidentally re-use the existing one from the previous iteration of the loop.

Then, once this selector is working, I plan to modify it to plot other data from the appropriate events on other types of histograms. This is intended to be a gate.

Maybe you need something like this: mycut->SetName("mycut"); mycut->SetVarX("Branch.CoordX"); mycut->SetVarY("Branch.CoordY"); tree->Draw(TString::Format("Branch.CoordY:Branch.CoordX>>histo%d(2048,0,8192,2048,0,8192)", p), TString::Format("Branch.blah == %d && mycut", p), "goff");

Aaaaand… success! Thanks!