Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:28:52

0001 // -*- C++ -*-
0002 #include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h"
0003 #include "SimDataFormats/RandomEngine/interface/RandomEngineState.h"
0004 #include "FWCore/Utilities/interface/EDMException.h"
0005 
0006 #include <algorithm>
0007 
0008 namespace edm {
0009 
0010   RandomEngineStates::RandomEngineStates() {}
0011 
0012   RandomEngineStates::~RandomEngineStates() {}
0013 
0014   void RandomEngineStates::getRandomEngineStates(std::vector<RandomEngineState>& states) const {
0015     // First check for data corruption so that the following code
0016     // does not encounter out of range errors.
0017     bool corrupt = false;
0018 
0019     if (moduleLabels_.size() != seedLengths_.size())
0020       corrupt = true;
0021     if (moduleLabels_.size() != stateLengths_.size())
0022       corrupt = true;
0023 
0024     unsigned int sum = 0U;
0025     for (std::vector<unsigned>::const_iterator i = seedLengths_.begin(), iEnd = seedLengths_.end(); i != iEnd; ++i) {
0026       sum += *i;
0027     }
0028     if (sum != seedVectors_.size())
0029       corrupt = true;
0030 
0031     sum = 0U;
0032     for (std::vector<unsigned>::const_iterator i = stateLengths_.begin(), iEnd = stateLengths_.end(); i != iEnd; ++i) {
0033       sum += *i;
0034     }
0035     if (sum != stateVectors_.size())
0036       corrupt = true;
0037 
0038     if (corrupt) {
0039       throw edm::Exception(errors::EventCorruption) << "RandomEngineStates data is corrupted.\n";
0040     }
0041 
0042     // Done with error checks.  Now do the work.
0043 
0044     std::vector<unsigned>::const_iterator seedLength = seedLengths_.begin();
0045     std::vector<unsigned>::const_iterator seedBegin = seedVectors_.begin();
0046     std::vector<unsigned>::const_iterator seedEnd = seedVectors_.begin();
0047 
0048     std::vector<unsigned>::const_iterator stateLength = stateLengths_.begin();
0049     std::vector<unsigned>::const_iterator stateBegin = stateVectors_.begin();
0050     std::vector<unsigned>::const_iterator stateEnd = stateVectors_.begin();
0051 
0052     for (std::vector<std::string>::const_iterator label = moduleLabels_.begin(), labelEnd = moduleLabels_.end();
0053          label != labelEnd;
0054          ++label, ++seedLength, ++stateLength) {
0055       seedBegin = seedEnd;
0056       seedEnd += *seedLength;
0057 
0058       stateBegin = stateEnd;
0059       stateEnd += *stateLength;
0060 
0061       RandomEngineState randomEngineState;
0062       randomEngineState.setLabel(*label);
0063       std::vector<RandomEngineState>::iterator state =
0064           std::lower_bound(states.begin(), states.end(), randomEngineState);
0065 
0066       if (state != states.end() && *label == state->getLabel()) {
0067         if (*seedLength != state->getSeed().size() || *stateLength != state->getState().size()) {
0068           throw edm::Exception(edm::errors::Configuration)
0069               << "When attempting to replay processing with the RandomNumberGeneratorService,\n"
0070               << "the engine type for each module must be the same in the replay configuration\n"
0071               << "and the original configuration.  If this is not the problem, then the data\n"
0072               << "is somehow corrupted or there is a bug because the vector in the data containing\n"
0073               << "the seeds or engine state is the incorrect size for the type of random engine.\n";
0074         }
0075 
0076         state->clearSeedVector();
0077         state->reserveSeedVector(*seedLength);
0078         for (std::vector<unsigned int>::const_iterator i = seedBegin; i != seedEnd; ++i) {
0079           state->push_back_seedVector(*i);
0080         }
0081 
0082         state->clearStateVector();
0083         state->reserveStateVector(*stateLength);
0084         for (std::vector<unsigned int>::const_iterator i = stateBegin; i != stateEnd; ++i) {
0085           state->push_back_stateVector(*i);
0086         }
0087       }
0088     }
0089   }
0090 
0091   void RandomEngineStates::setRandomEngineStates(std::vector<RandomEngineState> const& states) {
0092     moduleLabels_.resize(states.size());
0093     seedLengths_.resize(states.size());
0094     seedVectors_.clear();
0095     stateLengths_.resize(states.size());
0096     stateVectors_.clear();
0097 
0098     std::vector<std::string>::iterator label = moduleLabels_.begin();
0099     std::vector<unsigned>::iterator seedLength = seedLengths_.begin();
0100     std::vector<unsigned>::iterator stateLength = stateLengths_.begin();
0101 
0102     for (std::vector<RandomEngineState>::const_iterator state = states.begin(), iEnd = states.end(); state != iEnd;
0103          ++state, ++label, ++seedLength, ++stateLength) {
0104       *label = state->getLabel();
0105 
0106       std::vector<uint32_t> const& seedVector = state->getSeed();
0107       *seedLength = seedVector.size();
0108 
0109       for (std::vector<uint32_t>::const_iterator j = seedVector.begin(), jEnd = seedVector.end(); j != jEnd; ++j) {
0110         seedVectors_.push_back(*j);
0111       }
0112 
0113       std::vector<uint32_t> const& stateVector = state->getState();
0114       *stateLength = stateVector.size();
0115 
0116       for (std::vector<uint32_t>::const_iterator j = stateVector.begin(), jEnd = stateVector.end(); j != jEnd; ++j) {
0117         stateVectors_.push_back(*j);
0118       }
0119     }
0120   }
0121 
0122   bool RandomEngineStates::isProductEqual(RandomEngineStates const& randomEngineStates) const {
0123     if (moduleLabels_ == randomEngineStates.moduleLabels_ && seedLengths_ == randomEngineStates.seedLengths_ &&
0124         seedVectors_ == randomEngineStates.seedVectors_ && stateLengths_ == randomEngineStates.stateLengths_ &&
0125         stateVectors_ == randomEngineStates.stateVectors_) {
0126       return true;
0127     }
0128     return false;
0129   }
0130 }  // namespace edm