Logo ROOT  
Reference Guide
No Matches
df022_useKahan.C File Reference

Detailed Description

View in nbviewer Open in SWAN Implement a custom action that evaluates a Kahan sum.

This tutorial shows how to implement a Kahan summation custom action.

template <typename T>
class KahanSum final : public ROOT::Detail::RDF::RActionImpl<class KahanSum<T>> {
/// This type is a requirement for every helper.
using Result_t = T;
std::vector<T> fPartialSums;
std::vector<T> fCompensations;
int fNSlots;
std::shared_ptr<T> fResultSum;
void KahanAlgorithm(const T &x, T &sum, T &compensation){
T y = x - compensation;
T t = sum + y;
compensation = (t - sum) - y;
sum = t;
KahanSum(KahanSum &&) = default;
KahanSum(const KahanSum &) = delete;
KahanSum(const std::shared_ptr<T> &r) : fResultSum(r)
static_assert(std::is_floating_point<T>::value, "Kahan sum makes sense only on floating point numbers");
fPartialSums.resize(fNSlots, 0.);
fCompensations.resize(fNSlots, 0.);
std::shared_ptr<Result_t> GetResultPtr() const { return fResultSum; }
void Initialize() {}
void InitTask(TTreeReader *, unsigned int) {}
void Exec(unsigned int slot, T x)
KahanAlgorithm(x, fPartialSums[slot], fCompensations[slot]);
template <typename V=T, typename std::enable_if<ROOT::Internal::RDF::IsDataContainer<V>::value, int>::type = 0>
void Exec(unsigned int slot, const T &vs)
for (auto &&v : vs) {
Exec(slot, v);
void Finalize()
T sum(0) ;
T compensation(0);
for (int i = 0; i < fNSlots; ++i) {
KahanAlgorithm(fPartialSums[i], sum, compensation);
*fResultSum = sum;
std::string GetActionName(){
return "THnHelper";
void df022_useKahan()
// We enable implicit parallelism
auto dd = d.Define("x", "(rdfentry_ %2 == 0) ? 0.00000001 : 100000000.");
auto ptr = std::make_shared<double>();
KahanSum<double> helper(ptr);
auto kahanResult = dd.Book<double>(std::move(helper), {"x"});
auto plainResult = dd.Sum<double>({"x"});
std::cout << std::setprecision(24) << "Kahan: " << *kahanResult << " Classical: " << *plainResult << std::endl;
// Outputs: Kahan: 1000000000.00000011920929 Classical: 1000000000
ROOT::R::TRInterface & r
Definition Object.C:4
#define d(i)
Definition RSha256.hxx:102
int type
Definition TGX11.cxx:121
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:333
double T(double x)
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition TROOT.cxx:525
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:556
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition TROOT.cxx:563
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345
Kahan: 1000000000.00000011920929 Classical: 1000000000
July 2018
Enrico Guiraud, Danilo Piparo (CERN), Massimo Tumolo (Politecnico di Torino)

Definition in file df022_useKahan.C.