Logo ROOT  
Reference Guide
BatchData.h
Go to the documentation of this file.
1 // Author: Stephan Hageboeck, CERN 10 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 #ifndef ROOFIT_ROOFITCORE_INC_BATCHDATA_H_
18 #define ROOFIT_ROOFITCORE_INC_BATCHDATA_H_
19 
20 #include "RooSpan.h"
21 #include <map>
22 #include <tuple>
23 #include <cassert>
24 #include <vector>
25 #include <string>
26 
27 class RooArgSet;
28 
29 namespace BatchHelpers {
30 
31 /// A class to store batches of data points that can be accessed via RooSpan.
32 class BatchData {
33  public:
34  /// Status of the batch. Make sure that everything that is readable has
35  /// a status >= kReady.
38 
40  _ownedBatches(),
41  _foreignData(nullptr)
42  {
43 
44  }
45 
46  /// Discard all storage.
47  void clear() {
48  _ownedBatches.clear();
49  }
50 
51  Status_t status(std::size_t begin, const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified) const;
52 
53  bool setStatus(std::size_t begin, std::size_t size, Status_t stat,
54  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
55 
56  /// Mark all batches dirty. This will trigger recomputations.
57  void markDirty() {
58  for (auto& elm : _ownedBatches) {
59  if (elm.second.status != kReadyAndConstant)
60  elm.second.status = kDirty;
61  }
62  }
63 
64 
65  RooSpan<const double> getBatch(std::size_t begin, std::size_t maxSize,
66  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified) const;
67 
68  RooSpan<double> makeWritableBatchUnInit(std::size_t begin, std::size_t batchSize,
69  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
70 
71  RooSpan<double> makeWritableBatchInit(std::size_t begin, std::size_t batchSize, double value,
72  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
73 
74  void attachForeignStorage(const std::vector<double>& vec);
75 
76  void print(std::ostream& os, const std::string& indent) const;
77 
78 
79 
80  private:
81 
82  struct Batch {
83  std::size_t begin;
84  std::vector<double> data;
86 
87  bool inBatch(std::size_t evt) const {
88  return begin <= evt && evt < begin + data.size();
89  }
90 
91  RooSpan<const double> makeSpan(std::size_t evt, std::size_t batchSize) const {
92  assert(inBatch(evt));
93  return RooSpan<const double>(data.data() + (evt-begin), std::min(batchSize, data.size()));
94  }
95  };
96 
97  /// Key type of map that holds the batch storage.
98  using Key_t = std::tuple<std::size_t, const RooArgSet* const, Tag_t>;
99 
100  // A small benchmark of map vs. unordered map showed that a map finds elements faster up to 1000 elements.
101  // The usual size should be <~= 10 elements.
102  /// Storage for batch data.
103  using Map_t = std::map<Key_t, Batch>;
104 
105 
106  Map_t::const_iterator findEnclosingBatch(std::size_t evt,
107  const RooArgSet* const normSet, Tag_t ownerTag) const;
108  RooSpan<const double> createSpanInsideExistingBatch(std::size_t begin, std::size_t batchSize,
109  const RooArgSet* const normSet, Tag_t ownerTag) const;
110 
111 
113  const std::vector<double>* _foreignData;
114 };
115 
116 }
117 
118 #endif /* ROOFIT_ROOFITCORE_INC_BATCHDATA_H_ */
BatchHelpers::BatchData::kReadyAndConstant
@ kReadyAndConstant
Definition: BatchData.h:36
BatchHelpers::BatchData::Status_t
Status_t
Status of the batch.
Definition: BatchData.h:36
BatchHelpers::BatchData::attachForeignStorage
void attachForeignStorage(const std::vector< double > &vec)
Attach a foreign storage. Batches coming from this storage will be read only.
Definition: BatchData.cxx:172
BatchHelpers::BatchData::createSpanInsideExistingBatch
RooSpan< const double > createSpanInsideExistingBatch(std::size_t begin, std::size_t batchSize, const RooArgSet *const normSet, Tag_t ownerTag) const
Create a span pointing to existing batch memory.
Definition: BatchData.cxx:235
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
BatchHelpers::BatchData::kReady
@ kReady
Definition: BatchData.h:36
BatchHelpers::BatchData::Batch::status
Status_t status
Definition: BatchData.h:85
BatchHelpers::BatchData::Tag_t
Tag_t
Definition: BatchData.h:37
BatchHelpers::BatchData::makeWritableBatchInit
RooSpan< double > makeWritableBatchInit(std::size_t begin, std::size_t batchSize, double value, const RooArgSet *const normSet=nullptr, Tag_t ownerTag=kUnspecified)
Make a batch and return a span pointing to the pdf-local memory.
Definition: BatchData.cxx:160
BatchHelpers::BatchData::_foreignData
const std::vector< double > * _foreignData
Definition: BatchData.h:113
BatchHelpers::BatchData::kWriting
@ kWriting
Definition: BatchData.h:36
BatchHelpers::BatchData::Batch::data
std::vector< double > data
Definition: BatchData.h:84
BatchHelpers::BatchData::findEnclosingBatch
Map_t::const_iterator findEnclosingBatch(std::size_t evt, const RooArgSet *const normSet, Tag_t ownerTag) const
Find the batch that contains the event with number evt.
Definition: BatchData.cxx:216
BatchHelpers::BatchData::Key_t
std::tuple< std::size_t, const RooArgSet *const, Tag_t > Key_t
Key type of map that holds the batch storage.
Definition: BatchData.h:98
BatchHelpers::BatchData::kUnspecified
@ kUnspecified
Definition: BatchData.h:37
BatchHelpers
Definition: BatchData.h:29
BatchHelpers::BatchData::kgetLogVal
@ kgetLogVal
Definition: BatchData.h:37
BatchHelpers::BatchData
A class to store batches of data points that can be accessed via RooSpan.
Definition: BatchData.h:32
BatchHelpers::BatchData::markDirty
void markDirty()
Mark all batches dirty. This will trigger recomputations.
Definition: BatchData.h:57
BatchHelpers::BatchData::BatchData
BatchData()
Definition: BatchData.h:39
BatchHelpers::BatchData::Batch::makeSpan
RooSpan< const double > makeSpan(std::size_t evt, std::size_t batchSize) const
Definition: BatchData.h:91
BatchHelpers::BatchData::Batch
Definition: BatchData.h:82
BatchHelpers::BatchData::setStatus
bool setStatus(std::size_t begin, std::size_t size, Status_t stat, const RooArgSet *const normSet=nullptr, Tag_t ownerTag=kUnspecified)
Set the status of a batch with the given properties.
Definition: BatchData.cxx:70
BatchHelpers::BatchData::print
void print(std::ostream &os, const std::string &indent) const
Print to given output stream.
Definition: BatchData.cxx:182
BatchHelpers::BatchData::kNoBatch
@ kNoBatch
Definition: BatchData.h:36
BatchHelpers::BatchData::Batch::inBatch
bool inBatch(std::size_t evt) const
Definition: BatchData.h:87
BatchHelpers::BatchData::clear
void clear()
Discard all storage.
Definition: BatchData.h:47
BatchHelpers::BatchData::_ownedBatches
Map_t _ownedBatches
Definition: BatchData.h:112
BatchHelpers::BatchData::kgetVal
@ kgetVal
Definition: BatchData.h:37
RooSpan.h
BatchHelpers::BatchData::getBatch
RooSpan< const double > getBatch(std::size_t begin, std::size_t maxSize, const RooArgSet *const normSet=nullptr, Tag_t ownerTag=kUnspecified) const
Retrieve an existing batch.
Definition: BatchData.cxx:92
BatchHelpers::BatchData::Batch::begin
std::size_t begin
Definition: BatchData.h:83
BatchHelpers::BatchData::status
Status_t status(std::size_t begin, const RooArgSet *const normSet=nullptr, Tag_t ownerTag=kUnspecified) const
Return the status of the batch starting at begin.
Definition: BatchData.cxx:42
BatchHelpers::BatchData::makeWritableBatchUnInit
RooSpan< double > makeWritableBatchUnInit(std::size_t begin, std::size_t batchSize, const RooArgSet *const normSet=nullptr, Tag_t ownerTag=kUnspecified)
Make a batch and return a span pointing to the pdf-local memory.
Definition: BatchData.cxx:130
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:33
RooArgSet
Definition: RooArgSet.h:28
BatchHelpers::BatchData::Map_t
std::map< Key_t, Batch > Map_t
Storage for batch data.
Definition: BatchData.h:103
BatchHelpers::BatchData::kDirty
@ kDirty
Definition: BatchData.h:36