CMS 3D CMS Logo

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