Skip to content

Commit

Permalink
1. Reduce usage of Ostap::Utils::Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Nov 22, 2023
1 parent 75497e6 commit c6109e8
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 76 deletions.
1 change: 1 addition & 0 deletions ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
1. Slight optimisation in `Ostap::Math::ChebyshedSum`
1. Further optimisation in `Ostap::Math::ChebyshedSum`
1. add new test `ostap/math/tests/test_math.poly.py`
1. Reduce usage of `Ostap::Utils::Iterator`

## Backward incompatible:

Expand Down
8 changes: 7 additions & 1 deletion source/include/Ostap/Iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class TCollection ; // ROOT
class RooLinkedList ; // RooFit
class RooAbsCollection ; // RooFit
// ============================================================================
// ROOT
// ============================================================================
#include "RVersion.h"
// ============================================================================
namespace Ostap
{
// ==========================================================================
Expand All @@ -32,8 +36,10 @@ namespace Ostap
{
public:
// ======================================================================
#if ROOT_VERSION_CODE < ROOT_VERSION(6,31,0)
/// standard constructor: create and keep the ietrator
Iterator ( const RooAbsCollection& collection ) ;
Iterator ( const RooAbsCollection& collection ) ; // only for ROOT < 6.31
#endif
/// standard constructor: create and keep the ietrator
Iterator ( const TCollection& collection ) ;
/// standard constructor: create and keep the ietrator
Expand Down
2 changes: 1 addition & 1 deletion source/src/FormulaVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace
//
#if ROOT_VERSION_CODE < ROOT_VERSION(6,18,0)
//
Ostap::Utils::Iterator tmp ( variables ) ;
Ostap::Utils::Iterator tmp ( variables ) ; // only for ROOT < 6.18
RooAbsArg* c = 0 ;
while ( c = (RooAbsArg*) tmp.next() )
{ if ( c && actual.contains ( *c ) ) { used.add ( *c ) ; } }
Expand Down
25 changes: 10 additions & 15 deletions source/src/Funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// ============================================================================
#include "Ostap/Funcs.h"
#include "Ostap/Formula.h"
#include "Ostap/Iterator.h"
#include "Ostap/StatusCode.h"
#include "Ostap/HFuncs.h"
#include "Ostap/FormulaVar.h"
Expand All @@ -22,6 +21,7 @@
// ============================================================================
#include "Exception.h"
#include "local_utils.h"
#include "local_roofit.h"
// ============================================================================
/** @file
* Implementation file for classes from namespace Ostap::Functions
Expand Down Expand Up @@ -190,9 +190,10 @@ bool Ostap::Functions::FuncRooFormula::make_formula () const
{ throw Ostap::Exception ( "Invalid RooArgSet",
"Ostap::Function::FuncRooFormula" ,
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
//
::copy ( *varset , varlst ) ;
//
m_formula = std::make_unique<Ostap::FormulaVar> ( m_name , m_expression , varlst , false ) ;
//
Expand Down Expand Up @@ -759,8 +760,7 @@ bool Ostap::Functions::FuncRoo1D::make_xvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_xvar = std::make_unique<Ostap::FormulaVar> ( m_xvar_exp , varlst , false ) ;
//
Expand Down Expand Up @@ -826,8 +826,7 @@ bool Ostap::Functions::FuncRoo2D::make_xvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_xvar = std::make_unique<Ostap::FormulaVar> ( m_xvar_exp , varlst , false ) ;
//
Expand All @@ -849,8 +848,7 @@ bool Ostap::Functions::FuncRoo2D::make_yvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_yvar = std::make_unique<Ostap::FormulaVar> ( m_yvar_exp , varlst , false ) ;
//
Expand Down Expand Up @@ -926,8 +924,7 @@ bool Ostap::Functions::FuncRoo3D::make_xvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_xvar = std::make_unique<Ostap::FormulaVar> ( m_xvar_exp , varlst , false ) ;
//
Expand All @@ -949,8 +946,7 @@ bool Ostap::Functions::FuncRoo3D::make_yvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_yvar = std::make_unique<Ostap::FormulaVar> ( m_yvar_exp , varlst , false ) ;
//
Expand All @@ -972,8 +968,7 @@ bool Ostap::Functions::FuncRoo3D::make_zvar () const
Ostap::StatusCode(705) ) ; }
//
RooArgList varlst ;
Ostap::Utils::Iterator iter ( *varset ) ;
while ( RooAbsArg* a = iter.static_next<RooAbsArg>() ) { varlst.add ( *a ) ; }
::copy ( *varset , varlst ) ;
//
m_zvar = std::make_unique<Ostap::FormulaVar> ( m_zvar_exp , varlst , false ) ;
//
Expand Down
32 changes: 8 additions & 24 deletions source/src/HistoProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "Ostap/Formula.h"
#include "Ostap/FormulaVar.h"
#include "Ostap/HistoProject.h"
#include "Ostap/Iterator.h"
#include "Ostap/ProgressBar.h"
#include "Ostap/Notifier.h"
#include "Ostap/Exception.h"
Expand All @@ -34,6 +33,7 @@
#include "Exception.h"
#include "local_math.h"
#include "local_utils.h"
#include "local_roofit.h"
// ============================================================================
/** @file
* Implementation file for class Ostap::HistoProject
Expand Down Expand Up @@ -136,9 +136,7 @@ namespace
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -253,9 +251,7 @@ namespace
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -392,9 +388,7 @@ namespace
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst );
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -552,9 +546,7 @@ namespace
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -2435,9 +2427,7 @@ Ostap::HistoProject::project
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -2528,10 +2518,7 @@ Ostap::HistoProject::project2
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
//
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down Expand Up @@ -2636,10 +2623,7 @@ Ostap::HistoProject::project3
RooArgList alst ;
const RooArgSet* aset = data->get() ;
if ( 0 == aset ) { return 0 ; } // RETURN
//
Ostap::Utils::Iterator iter ( *aset );
RooAbsArg* coef = 0 ;
while ( ( coef = (RooAbsArg*) iter.next() ) ){ alst.add ( *coef ); }
::copy ( *aset , alst ) ;
//
// convert expressions into FormulaVar
const bool with_cuts = !selection.empty() ;
Expand Down
2 changes: 2 additions & 0 deletions source/src/Iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// ============================================================================
// standard constructor: create and keep the ietrator
// ============================================================================
#if ROOT_VERSION_CODE < ROOT_VERSION(6,31,0)
Ostap::Utils::Iterator::Iterator ( const RooAbsCollection& collection )
: m_iterator ( collection.createIterator() )
{}
#endif
// ============================================================================
// standard constructor: create and keep the ietrator
// ============================================================================
Expand Down
4 changes: 2 additions & 2 deletions source/src/MoreRooFit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ Double_t Ostap::MoreRooFit::Minimal::evaluate () const
// ==========================================================================
#if ROOT_VERSION_CODE < ROOT_VERSION(6,18,0)
//
Ostap::Utils::Iterator tmp ( this->m_vars ) ;
Ostap::Utils::Iterator tmp ( this->m_vars ) ; // only for ROOT < 6.18
RooAbsArg* c = 0 ;
while ( c = (RooAbsArg*) tmp.next() )
{
Expand Down Expand Up @@ -1538,7 +1538,7 @@ Double_t Ostap::MoreRooFit::Maximal::evaluate () const
// ==========================================================================
#if ROOT_VERSION_CODE < ROOT_VERSION(6,18,0)
//
Ostap::Utils::Iterator tmp ( this->m_vars ) ;
Ostap::Utils::Iterator tmp ( this->m_vars ) ; // only for ROOT < 6.18
RooAbsArg* c = 0 ;
while ( c = (RooAbsArg*) tmp.next() )
{
Expand Down
1 change: 0 additions & 1 deletion source/src/PDFs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// ============================================================================
// Local
// ============================================================================
#include "Ostap/Iterator.h"
#include "Ostap/Integrator.h"
#include "Ostap/HistoHash.h"
#include "Ostap/PDFs.h"
Expand Down
1 change: 0 additions & 1 deletion source/src/PDFs2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// ============================================================================
#include "Ostap/StatusCode.h"
#include "Ostap/PDFs2D.h"
#include "Ostap/Iterator.h"
// ============================================================================
// ROOT
// ============================================================================
Expand Down
1 change: 0 additions & 1 deletion source/src/PDFs3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// ============================================================================
#include "Ostap/StatusCode.h"
#include "Ostap/PDFs3D.h"
#include "Ostap/Iterator.h"
// ============================================================================
// ROOT
// ============================================================================
Expand Down
28 changes: 19 additions & 9 deletions source/src/PyPdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
// ============================================================================
#include "Ostap/PyPdf.h"
#include "Ostap/PyVar.h"
#include "Ostap/Iterator.h"
// ============================================================================
// Local
// ============================================================================
#include "CallPython.h"
#include "local_roofit.h"
// ============================================================================
/** @file
* Implementation file for class Ostap::Models::PyPdf
Expand Down Expand Up @@ -66,8 +66,7 @@ Ostap::Models::PyPdf::PyPdf
"PyPdf::consructor" ,
Ostap::StatusCode(400) ) ;
//
Ostap::Utils::Iterator it ( variables ) ;
while ( RooAbsReal* v = it.static_next<RooAbsReal>() ) { m_varlist.add ( *v ) ; }
::copy_real ( variables , m_variables , "Variable is not RooAbsReal" , "Ostap::Functions::PyPdf::PyPdf" );
//
Py_XINCREF ( m_self ) ;
}
Expand All @@ -88,8 +87,7 @@ Ostap::Models::PyPdf::PyPdf
, m_varlist ( "!varlist" , "All variables(list)" , this )
{
//
Ostap::Utils::Iterator it ( variables ) ;
while ( RooAbsReal* v = it.static_next<RooAbsReal>() ) { m_varlist.add ( *v ) ; }
::copy_real ( variables , m_varlist , "Variable is not RooAbsReal" , "Ostap::Functions::PyPdf::PyPdf" );
//
}
// ============================================================================
Expand Down Expand Up @@ -522,8 +520,7 @@ Ostap::Models::PyPdf2::PyPdf2
"PyPdf2::consructor" ,
Ostap::StatusCode(400) ) ;
//
Ostap::Utils::Iterator it ( variables ) ;
while ( RooAbsReal* v = it.static_next<RooAbsReal>() ) { m_varlist.add ( *v ) ; }
::copy_real ( variables , m_varlist , "Variable is not RooAbsReal" , "Ostap::Functions::PyPdf2::PyPdf2" );
//
if ( m_function ) { Py_XINCREF ( m_function ) ; }
m_arguments = PyTuple_New ( m_varlist.getSize() ) ;
Expand Down Expand Up @@ -574,11 +571,24 @@ Double_t Ostap::Models::PyPdf2::evaluate() const
"PyPdf2::evaluate" ,
Ostap::StatusCode(500) ) ;
//
Ostap::Utils::Iterator it ( m_varlist ) ;
unsigned short index = 0 ;
//
#if ROOT_VERSION_CODE < ROOT_VERSION(6,18,0)
//
Ostap::Utils::Iterator it ( m_varlist ) ; // onlyu for ROOT < 6.18
while ( RooAbsReal* v = it.static_next<RooAbsReal>() )
{
PyObject* pv = PyFloat_FromDouble ( v->getVal() ) ;
//
#else
//
for ( auto* vv : m_varlist )
{
RooAbsReal* v = static_cast<RooAbsReal*>( vv ) ;
//
#endif
//
const double value = v->getVal() ;
PyObject* pv = PyFloat_FromDouble ( value ) ;
if ( 0 != PyTuple_SetItem ( m_arguments , index , pv ) )
{
PyErr_Print () ;
Expand Down
Loading

0 comments on commit c6109e8

Please sign in to comment.