Logo ROOT  
Reference Guide
BatchData.cxx
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN 12 Apr 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#include "BatchData.h"
18
19#include <ostream>
20#include <iomanip>
21#include <iostream>
22
23namespace BatchHelpers {
24
25RooSpan<const double> BatchData::getBatch(std::size_t begin, std::size_t size) const {
26 if (_foreignData) {
27 if (begin >= _foreignData->size())
28 return {};
29
30 const double* dataBegin = &*(_foreignData->begin()+begin);
31 const std::size_t maxSize = std::min(size, _foreignData->size() - begin);
32 return RooSpan<const double>(dataBegin, maxSize);
33 }
34
35 const auto item = _ownedBatches.find(begin);
36 if (item == _ownedBatches.end()) {
37 // If requesting a batch inside another, a slower search algorithm must be used
38 return findSpanInsideExistingBatch(begin, size);
39 }
40
41 const auto& batch = item->second;
42 const std::size_t maxSize = std::min(size, batch.data.size() - (begin-batch.begin));
43
44 return RooSpan<const double>(batch.data.data(), maxSize);
45}
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Make a batch and return a span pointing to the pdf-local memory.
50/// The batch status is switched to `kWriting`, but the batch is not initialised.
51/// If a batch at this start point exists, the storage will be resized to fit the required
52/// size.
53///
54/// \param[in] begin Begin of the batch.
55/// \param[in] batchSize Size of the batch.
56/// \return An uninitialised RooSpan starting at event `begin`.
57
58RooSpan<double> BatchData::makeWritableBatchUnInit(std::size_t begin, std::size_t batchSize) {
59 auto item = _ownedBatches.find(begin);
60 if (item == _ownedBatches.end()) {
61 auto inserted = _ownedBatches.insert(std::make_pair(begin, Batch{begin, std::vector<double>(batchSize), kWriting}));
62 return RooSpan<double>(inserted.first->second.data);
63 }
64
65 Batch& batch = item->second;
66 batch.status = kWriting;
67 if (batch.data.size() != batchSize) {
68 batch.data.resize(batchSize);
69 }
70
71 return RooSpan<double>(batch.data);
72}
73
74
75////////////////////////////////////////////////////////////////////////////////
76/// Make a batch and return a span pointing to the pdf-local memory.
77/// Calls makeWritableBatchUnInit() and initialises the memory.
78///
79/// \param[in] begin Begin of the batch.
80/// \param[in] batchSize End of the batch (not included)
81/// \param[in] value Value to initialise with (defaults to 0.).
82/// \return An initialised RooSpan starting at event `begin`.
83RooSpan<double> BatchData::makeWritableBatchInit(std::size_t begin, std::size_t batchSize, double value) {
84 auto batch = makeWritableBatchUnInit(begin, batchSize);
85 for (auto& elm : batch) {
86 elm = value;
87 }
88
89 return batch;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// Attach a foreign storage. Batches coming from this storage will be read only.
94void BatchData::attachForeignStorage(const std::vector<double>& vec) {
95 clear();
96
97 _foreignData = &vec;
98 _ownedBatches.clear();
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Print to given output stream.
103void BatchData::print(std::ostream& os, const std::string& indent) const {
104 os << indent << "Batch data access";
105 if (_ownedBatches.empty() && !_foreignData) {
106 os << " not initialised." << std::endl;
107 return;
108 }
109
110 using std::setw;
111
112 os << " with " << (_foreignData ? "(foreign)" : "(owned)") << " data:";
113 os << "\n" << indent << std::right << std::setw(8) << "Batch #" << std::setw(8) << "Start"
114 << std::setw(7) << "Status";
115
116 unsigned int i=0;
117 for (auto item : _ownedBatches) {
118 auto startPoint = item.first;
119 const Batch& batch = item.second;
120
121 os << "\n" << indent
122 << std::setw(8) << i << std::setw(8) << startPoint
123 << std::setw(7) << batch.status << ": {";
124 for (unsigned int j=0; j < 5 && j < batch.data.size(); ++j) {
125 os << batch.data[j] << ", ";
126 }
127 os << "...}";
128 }
129 os << std::resetiosflags(std::ios::adjustfield) << std::endl;
130}
131
132
133
134}
static void indent(ostringstream &buf, int indent_level)
RooSpan< const double > findSpanInsideExistingBatch(std::size_t begin, std::size_t batchSize) const
Definition: BatchData.h:154
RooSpan< const double > getBatch(std::size_t begin, std::size_t batchSize) const
Definition: BatchData.cxx:25
void clear()
Discard all storage.
Definition: BatchData.h:40
void print(std::ostream &os, const std::string &indent) const
Print to given output stream.
Definition: BatchData.cxx:103
RooSpan< double > makeWritableBatchInit(std::size_t begin, std::size_t batchSize, double value)
Make a batch and return a span pointing to the pdf-local memory.
Definition: BatchData.cxx:83
const std::vector< double > * _foreignData
Definition: BatchData.h:164
void attachForeignStorage(const std::vector< double > &vec)
Attach a foreign storage. Batches coming from this storage will be read only.
Definition: BatchData.cxx:94
RooSpan< double > makeWritableBatchUnInit(std::size_t begin, std::size_t batchSize)
Make a batch and return a span pointing to the pdf-local memory.
Definition: BatchData.cxx:58
A simple container to hold a batch of data values.
Definition: RooSpan.h:32
std::vector< double > data
Definition: BatchData.h:121