Logo ROOT   master
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 <assert.h>
24 
25 class RooArgSet;
26 
27 namespace BatchHelpers {
28 
29 /// A class to store batches of data points that can be accessed via RooSpan.
30 class BatchData {
31  public:
32  /// Status of the batch. Make sure that everything that is readable has
33  /// a status >= kReady.
36 
38  _ownedBatches(),
39  _foreignData(nullptr)
40  {
41 
42  }
43 
44  /// Discard all storage.
45  void clear() {
46  _ownedBatches.clear();
47  }
48 
49  Status_t status(std::size_t begin, const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified) const;
50 
51  bool setStatus(std::size_t begin, std::size_t size, Status_t stat,
52  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
53 
54  /// Mark all batches dirty. This will trigger recomputations.
55  void markDirty() {
56  for (auto& elm : _ownedBatches) {
57  if (elm.second.status != kReadyAndConstant)
58  elm.second.status = kDirty;
59  }
60  }
61 
62 
63  RooSpan<const double> getBatch(std::size_t begin, std::size_t maxSize,
64  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified) const;
65 
66  RooSpan<double> makeWritableBatchUnInit(std::size_t begin, std::size_t batchSize,
67  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
68 
69  RooSpan<double> makeWritableBatchInit(std::size_t begin, std::size_t batchSize, double value,
70  const RooArgSet* const normSet = nullptr, Tag_t ownerTag = kUnspecified);
71 
72  void attachForeignStorage(const std::vector<double>& vec);
73 
74  void print(std::ostream& os, const std::string& indent) const;
75 
76 
77 
78  private:
79 
80  struct Batch {
81  std::size_t begin;
82  std::vector<double> data;
84 
85  bool inBatch(std::size_t evt) const {
86  return begin <= evt && evt < begin + data.size();
87  }
88 
89  RooSpan<const double> makeSpan(std::size_t evt, std::size_t batchSize) const {
90  assert(inBatch(evt));
91  return RooSpan<const double>(data.data() + (evt-begin), std::min(batchSize, data.size()));
92  }
93  };
94 
95  /// Key type of map that holds the batch storage.
96  using Key_t = std::tuple<std::size_t, const RooArgSet* const, Tag_t>;
97 
98  // A small benchmark of map vs. unordered map showed that a map finds elements faster up to 1000 elements.
99  // The usual size should be <~= 10 elements.
100  /// Storage for batch data.
101  using Map_t = std::map<Key_t, Batch>;
102 
103 
104  Map_t::const_iterator findEnclosingBatch(std::size_t evt,
105  const RooArgSet* const normSet, Tag_t ownerTag) const;
106  RooSpan<const double> createSpanInsideExistingBatch(std::size_t begin, std::size_t batchSize,
107  const RooArgSet* const normSet, Tag_t ownerTag) const;
108 
109 
111  const std::vector<double>* _foreignData;
112 };
113 
114 }
115 
116 #endif /* ROOFIT_ROOFITCORE_INC_BATCHDATA_H_ */
A simple container to hold a batch of data values.
Definition: RooSpan.h:32
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
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:58
std::vector< double > data
Definition: BatchData.h:82
std::tuple< std::size_t, const RooArgSet *const, Tag_t > Key_t
Key type of map that holds the batch storage.
Definition: BatchData.h:96
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:148
void attachForeignStorage(const std::vector< double > &vec)
Attach a foreign storage. Batches coming from this storage will be read only.
Definition: BatchData.cxx:160
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:30
void print(std::ostream &os, const std::string &indent) const
Print to given output stream.
Definition: BatchData.cxx:170
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:118
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:204
std::map< Key_t, Batch > Map_t
Storage for batch data.
Definition: BatchData.h:101
Status_t
Status of the batch.
Definition: BatchData.h:34
void clear()
Discard all storage.
Definition: BatchData.h:45
RooSpan< const double > makeSpan(std::size_t evt, std::size_t batchSize) const
Definition: BatchData.h:89
static void indent(ostringstream &buf, int indent_level)
A class to store batches of data points that can be accessed via RooSpan.
Definition: BatchData.h:30
bool inBatch(std::size_t evt) const
Definition: BatchData.h:85
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:223
void markDirty()
Mark all batches dirty. This will trigger recomputations.
Definition: BatchData.h:55
const std::vector< double > * _foreignData
Definition: BatchData.h:111
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:80