Skip to content

Commit

Permalink
1. simplify Ostap::Utils:::getWeight for 6.26<=ROOT
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Dec 7, 2023
1 parent 37c8be2 commit f373e72
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
1 change: 1 addition & 0 deletions ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. add conversion to int for `RooAbsCategory`
1. add iterator/contains/len functions for `RooAbsDataStore`
1. add some simple utilities for goodness-of-fit studies `ostap.stats.gof`
1. simplify `Ostap::Utils:::getWeight` for 6.26<=ROOT

## Backward incompatible:

Expand Down
18 changes: 13 additions & 5 deletions source/include/Ostap/GetWeight.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// ============================================================================
// Forward declarations
// ============================================================================
class RooAbsReal ;
class RooAbsData ;
class RooDataSet ;
// ============================================================================
Expand All @@ -24,7 +25,13 @@ namespace Ostap
* @param data (INPUT) dataset
* @return the name of weigth variable, if and when possible
*/
std::string getWeight ( const RooAbsData* data ) ;
std::string getWeight ( const RooAbsData* data ) ;
// ========================================================================
/** get the weight variable from data set (if and when possible)
* @param data (INPUT) dataset
* @return the weigth variable, if and when possible
*/
const RooAbsReal* getWeightVar ( const RooAbsData* data ) ;
// ========================================================================
/** weight errors are stored for the weighted data set?
* @param data (INPUT) dataset
Expand All @@ -34,7 +41,7 @@ namespace Ostap
* @see RooAbsData
* @see RooAbsArg::getAttribute
*/
bool storeError ( const RooAbsData* data ) ;
bool storeError ( const RooAbsData* data ) ;
// ========================================================================
/** weigth errors are stored for the weighted data set?
* @param data (INPUT) dataset
Expand All @@ -43,16 +50,17 @@ namespace Ostap
* @see RooAbsData
* @see RooAbsArg::getAttribute
*/
bool storeAsymError ( const RooAbsData* data ) ;
bool storeAsymError ( const RooAbsData* data ) ;
// ========================================================================
/** make un unweighted dataset from weighted one
* @param weighted_data (INPUT) input dataset
# @param weigth_var (INPUT) provde (new) name of weight variable
* @return unweighted dataset + name of weight variable, if and when possible
*/
std::pair<RooDataSet*,std::string>
unweight ( const RooAbsData* weighted_data ,
const std::string& weight_var = "" ) ;
unweight
( const RooAbsData* weighted_data ,
const std::string& weight_var = "" ) ;
// ========================================================================
} // The end of namespace Ostap::Utils
// ==========================================================================
Expand Down
70 changes: 46 additions & 24 deletions source/src/GetWeight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
namespace
{
// ==========================================================================
#if ROOT_VERSION_CODE < ROOT_VERSION ( 6 , 26 , 0 )
// ==========================================================================
/// helper class to get the protected info
class AuxDataSet : public RooDataSet
{
Expand All @@ -40,7 +42,7 @@ namespace
public:
// =======================================================================
/// get the weight variable
std::string wgtVar() const
std::string wgtName () const
{ return RooDataSet::_wgtVar ? RooDataSet::_wgtVar->GetName() : "" ; }
// ========================================================================
/// store error on weight ?
Expand All @@ -52,8 +54,12 @@ namespace
bool storeAsymError () const
{ return RooDataSet::_wgtVar ? RooDataSet::_wgtVar->getAttribute ( "StoreAsymError" ) : false ; }
// ========================================================================
const RooAbsReal* wgtVar () const { return _wgtVar ; }
// ========================================================================
} ;
// ==========================================================================
#endif
// ==========================================================================
static_assert ( std::numeric_limits<double>::is_specialized ,
"std::numeric_limits<double> is not specialized" ) ;
// ==========================================================================
Expand All @@ -67,16 +73,38 @@ namespace
* @return weigth variable, when possible
*/
// ============================================================================
std::string Ostap::Utils::getWeight ( const RooAbsData* data )
const RooAbsReal* Ostap::Utils::getWeightVar ( const RooAbsData* data )
{
if ( nullptr == data || !data->isWeighted() ) { return "" ; }
if ( nullptr == data || !data->isWeighted() ) { return nullptr ; }
const RooDataSet* ds = dynamic_cast<const RooDataSet*>( data ) ;
if ( nullptr == ds ) { return "" ; }
if ( nullptr == ds ) { return nullptr ; }
//
#if ROOT_VERSION_CODE < ROOT_VERSION ( 6 , 26 , 0 )
//
std::unique_ptr<RooAbsData> cloned { ds->emptyClone() } ;
AuxDataSet aux { *dynamic_cast<RooDataSet*>( cloned.get() ) } ;
//
return aux.wgtVar() ;
//
#else
//
return ds->weightVar() ;
//
#endif
//
}
// ============================================================================
/* get the weight variable from data set (if possible)
* @param data (INPUT) dataset
* @return weigth variable, when possible
*/
// ============================================================================
std::string Ostap::Utils::getWeight ( const RooAbsData* data )
{
// weight var
const RooAbsReal* wvar = getWeightVar ( data ) ;
if ( nullptr == wvar ) { return "" ; }
return wvar->GetName() ;
}
// ============================================================================
/* weight errors are stored for the weighted data set?
Expand All @@ -88,16 +116,14 @@ std::string Ostap::Utils::getWeight ( const RooAbsData* data )
* @see RooAbsArg::getAttribute
*/
// ============================================================================
bool Ostap::Utils::storeError ( const RooAbsData* data )
bool Ostap::Utils::storeError ( const RooAbsData* data )
{
if ( nullptr == data || !data->isWeighted() ) { return false ; }
const RooDataSet* ds = dynamic_cast<const RooDataSet*>( data ) ;
if ( nullptr == ds ) { return false ; }
//
std::unique_ptr<RooAbsData> cloned { ds->emptyClone() } ;
AuxDataSet aux { *dynamic_cast<RooDataSet*>( cloned.get() ) } ;
//
return aux.storeError() ;
// weight var
const RooAbsReal* wvar = getWeightVar ( data ) ;
if ( nullptr == wvar ) { return false ; }
return
wvar->getAttribute ( "StoreError" ) ||
wvar->getAttribute ( "StoreAsymError" ) ;
}
// ============================================================================
/* weigth errors are stored for the weighted data set?
Expand All @@ -109,17 +135,13 @@ bool Ostap::Utils::storeError ( const RooAbsData* data )
*/
// ============================================================================
bool Ostap::Utils::storeAsymError ( const RooAbsData* data )
{
if ( nullptr == data || !data->isWeighted() ) { return false ; }
const RooDataSet* ds = dynamic_cast<const RooDataSet*>( data ) ;
if ( nullptr == ds ) { return false ; }
//
std::unique_ptr<RooAbsData> cloned { ds->emptyClone() } ;
AuxDataSet aux { *dynamic_cast<RooDataSet*>( cloned.get() ) } ;
{
// weight var
const RooAbsReal* wvar = getWeightVar ( data ) ;
if ( nullptr == wvar ) { return false ; }
return wvar->getAttribute ( "StoreAsymError" ) ;
//
return aux.storeAsymError() ;
}

// ============================================================================
/* make un unweighted dataset from weighted one
* @param weighted_data (INPUT) input dataset
Expand All @@ -131,8 +153,8 @@ Ostap::Utils::unweight
( const RooAbsData* weighted_data ,
const std::string& weight_var )
{
if ( nullptr == weighted_data ) { return std::make_pair ( nullptr, "" ) ; } // RETURN
if ( ! weighted_data->isWeighted() ) { return std::make_pair ( nullptr, "" ) ; } // RETURN
if ( nullptr == weighted_data ) { return std::make_pair ( nullptr, "" ) ; } // RETURN
if ( !weighted_data->isWeighted() ) { return std::make_pair ( nullptr, "" ) ; } // RETURN
//
std::string wvar = weight_var ;
//
Expand Down

0 comments on commit f373e72

Please sign in to comment.