Dear ROOT experts,
I wrote back in time a functor for Covariance calculation between 2 colums in a RDataFrame
Here the C++ code and the example usage .
Now what i would like to do is to expand the functor to actually create a
TMatrixSymD object and pass Any set of columns.
Can anyone give me some hint how to expand the code i already have for this purpose?
Thanks
Renato
/**
* T is the type of the Scalar Column used
* Example of usage with RDataFrame
* auto covi = Covariance<double>();
* auto covXY = df.Book<double,double>(std::move(covi), {"xColumn", "yColumn"} ) );
*/
template< typename T>
class Covariance : public ROOT::Detail::RDF::RActionImpl<Covariance<T>>{
public :
using Covariance_t = T;
using Result_t = Covariance_t;
private :
std::vector<Covariance_t> _xyproductSUM; //one per data processing slot
std::vector<Covariance_t> _xStatsSUM; //one per data processing slot
std::vector<Covariance_t> _yStatsSUM; //one per data processing slot
std::vector<int> _nEntries; //one per data processing slot
std::shared_ptr<Covariance_t> _covariance;
public :
Covariance( ){
const auto nSlots = ROOT::IsImplicitMTEnabled() ? ROOT::GetImplicitMTPoolSize() : 1;
for (auto i : ROOT::TSeqU(nSlots)){
_xyproductSUM.emplace_back(0.);
_xStatsSUM.emplace_back(0.);
_yStatsSUM.emplace_back(0.);
_nEntries.emplace_back(0);
(void)i;
}
_covariance = std::make_shared<double>(0.);
}
Covariance( Covariance &&)= default;
Covariance( const Covariance &) = delete;
std::shared_ptr<Covariance_t> GetResultPtr() const {
return _covariance;
}
void Initialize() {}
void InitTask(TTreeReader *, unsigned int) {}
template <typename... ColumnTypes>
void Exec(unsigned int slot, ColumnTypes... values){
std::array<double, sizeof...(ColumnTypes)> valuesArr{static_cast<double>(values)...};
_nEntries[slot] ++;
_xyproductSUM[slot] += valuesArr[0]*valuesArr[1];
_xStatsSUM[slot] += valuesArr[0];
_yStatsSUM[slot] += valuesArr[1];
}
void Finalize(){
for( auto slot : ROOT::TSeqU(1, _xyproductSUM.size())){
_xyproductSUM[0] += _xyproductSUM[slot];
_xStatsSUM[0] += _xStatsSUM[slot];
_yStatsSUM[0] += _yStatsSUM[slot];
_nEntries[0] += _nEntries[slot];
}
/*
*/
*_covariance = (1./( _nEntries[0]-1.)) * ( ( _xyproductSUM[0] ) - 1./(_nEntries[0]) * ( (_xStatsSUM[0])) * ( (_yStatsSUM[0])) ) ;
}
std::string GetActionName(){
return "Covariance";
}
};
Please read tips for efficient and successful posting and posting code
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided