Logo ROOT  
Reference Guide
BracketAdapter.h
Go to the documentation of this file.
1 // Author: Stephan Hageboeck, CERN 25 Feb 2019
2 
3 /*****************************************************************************
4  * RooFit
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2019, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 #ifndef ROOFIT_BATCHCOMPUTE_BRACKETADAPTER_H
18 #define ROOFIT_BATCHCOMPUTE_BRACKETADAPTER_H
19 
20 #include "RooSpan.h"
21 
22 #include <vector>
23 
24 namespace RooBatchCompute {
25 
26 ///Little adapter that gives a bracket operator to types that don't
27 ///have one. It completely ignores the index and returns a constant.
28 template <class T = double>
30  public:
31 
32  constexpr BracketAdapter(T payload) noexcept :
33  _payload{payload} { }
34 
35  constexpr BracketAdapter(RooSpan<const T> payload) noexcept :
36  _payload{payload[0]} { }
37 
38  constexpr double operator[](std::size_t) const {
39  return _payload;
40  }
41 
42  constexpr operator double() const {
43  return _payload;
44  }
45 
46  constexpr bool isBatch() const noexcept {
47  return false;
48  }
49 
50  private:
51  const T _payload;
52 };
53 
54 
56  public:
57  /// Construct adapter from a fallback value and a batch of values.
58  /// - If `batch.size() == 0`, always return `payload`.
59  /// - Else, return `batch[i]`.
60  BracketAdapterWithMask(double payload, const RooSpan<const double>& batch) noexcept :
61  _isBatch(!batch.empty()),
62  _payload(payload),
63  _pointer(batch.empty() ? &_payload : batch.data()),
64  _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
65  {
66  }
67 
68  /// Construct adapter from a batch of values.
69  /// - If `batch.size() == 1`, always return the value at `batch[0]`.
70  /// - Else, return `batch[i]`.
72  _isBatch(batch.size() > 1),
73  _payload(batch[0]),
74  _pointer(batch.data()),
75  _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
76  {
77  assert(batch.size() > 0);
78  }
79 
81  _isBatch(other._isBatch),
82  _payload(other._payload),
83  _pointer(other._isBatch ? other._pointer : &_payload),
84  _mask(other._mask)
85  {
86  }
87 
89 
90  inline double operator[](std::size_t i) const noexcept {
91  return _pointer[ i & _mask];
92  }
93 
94  inline bool isBatch() const noexcept {
95  return _isBatch;
96  }
97 
98  private:
99  const bool _isBatch;
100  const double _payload;
101  const double* __restrict const _pointer;
102  const size_t _mask;
103 };
104 
105 }
106 
107 #endif /* ROOFIT_BATCHCOMPUTE_BRACKETADAPTER_H */
RooBatchCompute::BracketAdapterWithMask::_pointer
const double *__restrict const _pointer
Definition: BracketAdapter.h:101
RooBatchCompute::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(double payload, const RooSpan< const double > &batch) noexcept
Construct adapter from a fallback value and a batch of values.
Definition: BracketAdapter.h:60
RooBatchCompute::BracketAdapterWithMask::_isBatch
const bool _isBatch
Definition: BracketAdapter.h:99
RooBatchCompute::BracketAdapterWithMask::isBatch
bool isBatch() const noexcept
Definition: BracketAdapter.h:94
RooSpan::size
constexpr std::span< T >::index_type size() const noexcept
Definition: RooSpan.h:121
RooBatchCompute::BracketAdapter::operator[]
constexpr double operator[](std::size_t) const
Definition: BracketAdapter.h:38
size
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
RooBatchCompute::BracketAdapter::_payload
const T _payload
Definition: BracketAdapter.h:51
RooBatchCompute::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(const BracketAdapterWithMask &other) noexcept
Definition: BracketAdapter.h:80
double
double
Definition: Converters.cxx:939
RooBatchCompute::BracketAdapterWithMask::_mask
const size_t _mask
Definition: BracketAdapter.h:102
RooBatchCompute::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(RooSpan< const double > batch)
Construct adapter from a batch of values.
Definition: BracketAdapter.h:71
RooBatchCompute::BracketAdapter::BracketAdapter
constexpr BracketAdapter(T payload) noexcept
Definition: BracketAdapter.h:32
RooBatchCompute::BracketAdapterWithMask::operator=
BracketAdapterWithMask & operator=(const BracketAdapterWithMask &other)=delete
RooBatchCompute::BracketAdapterWithMask::operator[]
double operator[](std::size_t i) const noexcept
Definition: BracketAdapter.h:90
RooBatchCompute
Namespace for dispatching RooFit computations to various backends.
Definition: BracketAdapter.h:24
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
RooBatchCompute::BracketAdapter
Little adapter that gives a bracket operator to types that don't have one.
Definition: BracketAdapter.h:29
RooSpan.h
RooBatchCompute::BracketAdapter::BracketAdapter
constexpr BracketAdapter(RooSpan< const T > payload) noexcept
Definition: BracketAdapter.h:35
RooBatchCompute::BracketAdapterWithMask
Definition: BracketAdapter.h:55
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
RooBatchCompute::BracketAdapter::isBatch
constexpr bool isBatch() const noexcept
Definition: BracketAdapter.h:46
RooBatchCompute::BracketAdapterWithMask::_payload
const double _payload
Definition: BracketAdapter.h:100