User function as selection formula?

Hello,

I’d like to encode some complex selection function in private code and use it for drawing and such:

select.C:
#include “tree.h” // from MakeClass
bool select() {
return x+y>0; // x,y tree members. This example is not exactly complex
}

and in ROOT:
.L select.C
tree->Draw(“x”,“select()”)

this crashes. Is a near solution available?

thanks for your help,
Maarten

If you have the files tree.h and tree.C generated by MakeClass, the best solution is to do

root > .L tree.C (or tree.C+) root > tree t root > t.Loop()
and in the Loop function you create your histogram(s) and fill/draw them.

An alternative is to use a Selector

Rene

Thanks very much;

I know this solution, but wanted to know whether what I ask is possible or not…
This would allow me to pass selection functions as strings in parameter files, have them interpreted as TTreeFormulas, and drive my analysis in a completely configurable way.

cheers,
Maarten

well! if you just want to do that, use a combination of TCuts where each TCut is a string with your selections.

Rene

Hi,

The following should also work:// Select.C bool select(int x, int y) { // instead of int use the real type of x and y return x+y>0; // x,y tree members. This example is not exactly complex }and.L select.C tree->Draw("x","select(x,y)");Cheers,
Philippe.

Hi Philippe,

I have a similar problem and the proposed solution gives me the “Error in TTreeFormula::Compile: Bad numerical expression” error. Is this supposed to work as written?

Thanks,

Marcos

Hi,

[quote] Is this supposed to work as written?[/quote]Yes (except for replacing the ‘x’ and ‘y’ in the Draw command with the name of your branches).

Cheers,
Philippe.

Hello Philippe,

Thanks for your fast reply. I was already using the branch names but I can’t get it to work. Here’s a piece of the code I’m using. I’m building the call to the function with values I read from an STL vector and put intro a string stream.
LlhFit_Azimuth and LlhFit_Zenith are the branches names:

     double deltaAzi = 10 * degree;
     double deltaZen = 10 * degree;
     double currentZen = moonEvents[index].zen;
     double currentAzi = moonEvents[index].azi;

     ostringstream CutStr;
     
     CutStr << "isEventInWindow(LlhFit_Azimuth," << currentAzi << ",";
     CutStr << "LlhFit_Zenith, " << currentZen;
     CutStr << ", " << deltaAzi;
     CutStr << ", " << deltaZen << ")";
    
     TCut myCut(CutStr.str().c_str());
     
     tree->Draw(">>elist", myCut);
     TEventList *elist = (TEventList*)gDirectory->Get("elist");
     int Npass = elist->GetN(); 

An example of CutStr that I got from a cout is:

isEventInWindow(PoleMuonLlhFit_Azimuth,0.627381,PoleMuonLlhFit_Zenith, 1.29479, 0.174533, 0.174533)

which looks OK. The actual function that returns the boolean is here:

bool isEventInWindow(float eventAzi, float myAzi, float eventZen, float myZen, float aziWindow, float zenWindow) {
   return phiDiff(eventAzi,myAzi) < aziWindow && fabs(eventZen-myZen) < zenWindow;
}

Is there anything that catches your eye that could be causing the error? If this is not illustrative I’ll try to provide a working example.

Thanks again,

Marcos

Hi,

I do not see any obvious problem, so indeed a running example reproducing the issue would help solve this issue :slight_smile:

Cheers,
Philippe.

Hi Philippe,

I have a small code, a Makefile and a sample root file with data in it here:
http://icecube.wisc.edu/~santander/rootTest/

Let me know if that’s good enough to troubleshoot the problem. It may very well be a stupid mistake of my part, but I’m not able to catch it. Many thanks again!

Marcos

PS: BTW, I’m using version 5.24/00b running on a RHEL 4.0 64 bits machine.

Hi,

The problem is that since you compile your code but do not generate a dictionary (in particular for isEventInWindow), TTreeFormula has no way of knowing what the string ‘isEventInWindow’ represent.
To solve the problem either interpret your code or better yet generate a dictionary for isEventInWindow.

Cheers,
Philippe.

[quote=“pcanal”]Hi,

The problem is that since you compile your code but do not generate a dictionary (in particular for isEventInWindow), TTreeFormula has no way of knowing what the string ‘isEventInWindow’ represent.
To solve the problem either interpret your code or better yet generate a dictionary for isEventInWindow.

Cheers,
Philippe.[/quote]
I’m very interested in this functionality. This is the only obstacle that prevents me from compiling my code.

I’m learning now about how to generate the dictionary. Question: does the function I want to use in my selection formula have to be generated inside a class? Do I need to create a class containing this function in order to be able to put this function in a dictionary?

[quote]Do I need to create a class containing this function in order to be able to put this function in a dictionary?[/quote]No you do not need to. You can generate a dictionary for free standing function.

Cheers,
Philippe.