Analyze data in procedural or OO style?

Dear all,

I am not a professional programmer. I have heard the concept of procedural and object-oriented programming, and that ROOT is an object-oriented framework for physics analysis. I naively thought my analysis codes based on ROOT should be programmed using OO style.

So I wrote my program with 3 base classes: “Detector”, “Event” and “Analyzer”. There are 3 main functions in the “Analyzer”:

Analyzer::SetEvent(Event *evt);
Analyzer::SetDetector(Detector *det);

The idea behind this setup is simple: an analysis can be performed given the hardware information saved in the “Detector” and an “Event” from data.

The problem of this setup is that there are many getters/setters in the “Detector” and “Event” classes. Getters are used in function “Analyzer::Process()” to get individual piece of information from the detector and an event. This means that I basically have a procedural program taking the form of an object-oriented program. The “Detector” and “Event” classes behave like global variables in a procedural program, while the “Analyzer” class is just a collection of functions in a procedural program.

I can let the “Analyzer” inherits from the “Detector” to remove function “Analyzer::SetDetector” and all getters in the “Detector”. But I still have many getters in “Event”. I cannot find a way to go around it.

So I started to ask myself, is a physics analysis better to be programmed in a procedural style or OO style? We take data, and pass the data to a serious of analyses. It seems natural to mimic this process in programming, meaning that procedural programming is a natural choice for physics data analysis.

I feel like that ROOT defines many objects used in an analysis, for example, TVector3, TClonesArray, etc. One can utilize OO to realize those objects. But once they are established, they should be used in an specific analysis realized in procedural programming style.

And it seems to me that the use of TTree forces one to program in procedural style. He gets all variables saved in a tree by calling TTree::GetEntry(ievt) at the beginning of the main event loop, processes these variables in a serious of functions, and finally saves selected events in the end of the loop. The tree is essentially a place to hold the global variables for procedural programming.

To summarize my understanding, ROOT can be written by OO, but an specific analysis should be programmed in the traditional procedural style. Can somebody with more experience help me to understand this issue better?



Dear all,

I am considering the following solution.

“Detector” <- - - - - “Event” <- - - - - “Analyzer”

Namely, let “Event” inherit from “Detector”, and “Analyzer” inherit from “Event”.

“Event” needs hardware information in “Detector” to place its data, for example, to place a hit in an event to a sensor in the detector where it comes. “Event” can inherit the sensor information from “Detector”.

“Analyzer” contains functions to calculate additional parameters, given hit information inherited from “Event” and sensor information inherited from “Detector”. The functions and additional parameters can be an analyzer’s private members.

This looks like an OO solution to me. Since data members and the functions to operate on them are placed in the same class. Functions do not need to get access to input or output through lots of setters and getters anymore. What do you think?

There are still some aspects of this solution which bothers me.

First of all, in a user’s event loop, he loads data from a tree to his “Event” object. Then he needs to copy all protected members in this “Event” object to his “Analyzer” object. The latter will then be able to use the data to calculate additional parameters. Is there a way in C++ or ROOT to let the “Analyzer” object directly get all protected data members in an event in the tree?

Secondly, if the user has several analyzers, how can they share the save set of protected members in the “Event” object?

Finally, when the user save several analyzers back to the tree at the end of the event loop, data in the event are duplicated in the tree. Is there a way to prevent this?