Home How to implement a formula pattern in C++?

# How to implement a formula pattern in C++?

user3307
1#
user3307 Published in March 24, 2018, 12:35 am

# Disclaimer

Firstly, I am very sorry for the title of this question. It was not easy for me to formulate what I want in short. If you know how to change it - please, advice.

# Problem description

## Preliminaries

I have a huge amount of structured data which is a tree (ROOT TTree object) of variables of different types. Elementary unit of that data is so called entry or event. So different entries contain different values of the same variables. The latter is not the most clear statement. Think of that tree in the following way: it is like a matrix where rows are entry numbers (0, 1, 2, ... ) and columns are variable names (energy, momentum, theta, ...).

## What we need

We need to perform a global selection on those events. I.e. we have to decide which events are for the further analysis and which are not. In order to reach this we have developed several algorithms which we call selectors. These selectors use different parameters of an event (variables) to decide if it passed or not. The procedure or algorithm behind the current selector we will call Formula and variables that this algorithm uses inside - variables.

We are doing our selection event by event. I.e. each event goes through each selector and if it fails (not passed) for some of them (selectors) the next event take a turn.

## Programming issues

Each selector is represented by a class derived from an abstract base class:

 class Selector:
{
protected :
bool status;

public :
virtual bool Formula() = 0;//must be overridden for a specific Selector
};


You see that pure function Formula takes no argument. It is reasonable because we do not know what arguments it should take until we know what selector we use. So information of variables could be stored in class data members. For example, in this way

class SpecificSelector : public Selector
{
private :
std::vector<?> variables;//or std::array

public :
void AddVariable ( /*pointer to a variable*/ ) { variables.push_back( &variable ); }
bool Formula() { return ( *variables[0] + *variables[1] ) > 1 ? true : false; }
};


But wait. Variables could be any (reasonable) type. So we have to know what is what. May be so

class SpecificSelector : public Selector
{
private :
std::vector<int* > intVariables;//or std::array
std::vector<float* > floatVariables;
public :
void AddIntVariable ( /*pointer to a variable*/ ) { intVariables.push_back( &variable ); }
void AddFloatVariable ( /*pointer to a variable*/ ) { floatVariables.push_back( &variable ); }
bool Formula() { return ( *intVariables[0] + *intVariables[1] ) > 1 ? true : false; }
};


But types could be more exotic. Arrays, for example.

Maybe it is a place for templates here? What is a way to reach the desired? How to implement such kind of Formula member function.

We think the problem is very common despite we presented it in such specific way.

 You need to login account before you can post.
Processed in 0.248617 second(s) , Gzip On .