CMS 3D CMS Logo

RandomEngineStates.cc
Go to the documentation of this file.
1 // -*- C++ -*-
5 
6 #include <algorithm>
7 
8 namespace edm {
9 
11 
13 
14  void RandomEngineStates::getRandomEngineStates(std::vector<RandomEngineState>& states) const {
15  // First check for data corruption so that the following code
16  // does not encounter out of range errors.
17  bool corrupt = false;
18 
19  if (moduleLabels_.size() != seedLengths_.size())
20  corrupt = true;
21  if (moduleLabels_.size() != stateLengths_.size())
22  corrupt = true;
23 
24  unsigned int sum = 0U;
25  for (std::vector<unsigned>::const_iterator i = seedLengths_.begin(), iEnd = seedLengths_.end(); i != iEnd; ++i) {
26  sum += *i;
27  }
28  if (sum != seedVectors_.size())
29  corrupt = true;
30 
31  sum = 0U;
32  for (std::vector<unsigned>::const_iterator i = stateLengths_.begin(), iEnd = stateLengths_.end(); i != iEnd; ++i) {
33  sum += *i;
34  }
35  if (sum != stateVectors_.size())
36  corrupt = true;
37 
38  if (corrupt) {
39  throw edm::Exception(errors::EventCorruption) << "RandomEngineStates data is corrupted.\n";
40  }
41 
42  // Done with error checks. Now do the work.
43 
44  std::vector<unsigned>::const_iterator seedLength = seedLengths_.begin();
45  std::vector<unsigned>::const_iterator seedBegin = seedVectors_.begin();
46  std::vector<unsigned>::const_iterator seedEnd = seedVectors_.begin();
47 
48  std::vector<unsigned>::const_iterator stateLength = stateLengths_.begin();
49  std::vector<unsigned>::const_iterator stateBegin = stateVectors_.begin();
50  std::vector<unsigned>::const_iterator stateEnd = stateVectors_.begin();
51 
52  for (std::vector<std::string>::const_iterator label = moduleLabels_.begin(), labelEnd = moduleLabels_.end();
53  label != labelEnd;
54  ++label, ++seedLength, ++stateLength) {
55  seedBegin = seedEnd;
56  seedEnd += *seedLength;
57 
58  stateBegin = stateEnd;
59  stateEnd += *stateLength;
60 
61  RandomEngineState randomEngineState;
62  randomEngineState.setLabel(*label);
63  std::vector<RandomEngineState>::iterator state =
64  std::lower_bound(states.begin(), states.end(), randomEngineState);
65 
66  if (state != states.end() && *label == state->getLabel()) {
67  if (*seedLength != state->getSeed().size() || *stateLength != state->getState().size()) {
69  << "When attempting to replay processing with the RandomNumberGeneratorService,\n"
70  << "the engine type for each module must be the same in the replay configuration\n"
71  << "and the original configuration. If this is not the problem, then the data\n"
72  << "is somehow corrupted or there is a bug because the vector in the data containing\n"
73  << "the seeds or engine state is the incorrect size for the type of random engine.\n";
74  }
75 
76  state->clearSeedVector();
77  state->reserveSeedVector(*seedLength);
78  for (std::vector<unsigned int>::const_iterator i = seedBegin; i != seedEnd; ++i) {
79  state->push_back_seedVector(*i);
80  }
81 
82  state->clearStateVector();
83  state->reserveStateVector(*stateLength);
84  for (std::vector<unsigned int>::const_iterator i = stateBegin; i != stateEnd; ++i) {
85  state->push_back_stateVector(*i);
86  }
87  }
88  }
89  }
90 
91  void RandomEngineStates::setRandomEngineStates(std::vector<RandomEngineState> const& states) {
92  moduleLabels_.resize(states.size());
93  seedLengths_.resize(states.size());
94  seedVectors_.clear();
95  stateLengths_.resize(states.size());
96  stateVectors_.clear();
97 
98  std::vector<std::string>::iterator label = moduleLabels_.begin();
99  std::vector<unsigned>::iterator seedLength = seedLengths_.begin();
100  std::vector<unsigned>::iterator stateLength = stateLengths_.begin();
101 
102  for (std::vector<RandomEngineState>::const_iterator state = states.begin(), iEnd = states.end(); state != iEnd;
103  ++state, ++label, ++seedLength, ++stateLength) {
104  *label = state->getLabel();
105 
106  std::vector<uint32_t> const& seedVector = state->getSeed();
107  *seedLength = seedVector.size();
108 
109  for (std::vector<uint32_t>::const_iterator j = seedVector.begin(), jEnd = seedVector.end(); j != jEnd; ++j) {
110  seedVectors_.push_back(*j);
111  }
112 
113  std::vector<uint32_t> const& stateVector = state->getState();
114  *stateLength = stateVector.size();
115 
116  for (std::vector<uint32_t>::const_iterator j = stateVector.begin(), jEnd = stateVector.end(); j != jEnd; ++j) {
117  stateVectors_.push_back(*j);
118  }
119  }
120  }
121 
122  bool RandomEngineStates::isProductEqual(RandomEngineStates const& randomEngineStates) const {
123  if (moduleLabels_ == randomEngineStates.moduleLabels_ && seedLengths_ == randomEngineStates.seedLengths_ &&
124  seedVectors_ == randomEngineStates.seedVectors_ && stateLengths_ == randomEngineStates.stateLengths_ &&
125  stateVectors_ == randomEngineStates.stateVectors_) {
126  return true;
127  }
128  return false;
129  }
130 } // namespace edm
void setLabel(const std::string &value)
std::vector< unsigned > seedLengths_
std::vector< unsigned > seedVectors_
bool isProductEqual(RandomEngineStates const &randomEngineStates) const
char const * label
void getRandomEngineStates(std::vector< RandomEngineState > &states) const
std::vector< std::string > moduleLabels_
HLT enums.
std::vector< unsigned > stateLengths_
std::vector< unsigned > stateVectors_
void setRandomEngineStates(std::vector< RandomEngineState > const &states)