diff --git a/IOMC/RandomEngine/src/RandomEngineStateProducer.cc b/IOMC/RandomEngine/src/RandomEngineStateProducer.cc index ff85de5f476b9..42e9bb8064475 100644 --- a/IOMC/RandomEngine/src/RandomEngineStateProducer.cc +++ b/IOMC/RandomEngine/src/RandomEngineStateProducer.cc @@ -1,16 +1,17 @@ #include "IOMC/RandomEngine/src/RandomEngineStateProducer.h" -#include "FWCore/ServiceRegistry/interface/Service.h" -#include "FWCore/Utilities/interface/RandomNumberGenerator.h" -#include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h" + #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/LuminosityBlock.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "FWCore/Utilities/interface/RandomNumberGenerator.h" +#include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h" #include -RandomEngineStateProducer::RandomEngineStateProducer(edm::ParameterSet const& pset) { +RandomEngineStateProducer::RandomEngineStateProducer(edm::ParameterSet const&) { produces("beginLumi"); produces(); } @@ -19,9 +20,9 @@ RandomEngineStateProducer::~RandomEngineStateProducer() { } void -RandomEngineStateProducer::produce(edm::Event& ev, edm::EventSetup const& es) { +RandomEngineStateProducer::produce(edm::Event& ev, edm::EventSetup const&) { edm::Service randomService; - if (randomService.isAvailable()) { + if(randomService.isAvailable()) { std::auto_ptr states(new edm::RandomEngineStates); states->setRandomEngineStates(randomService->getEventCache()); ev.put(states); @@ -29,9 +30,9 @@ RandomEngineStateProducer::produce(edm::Event& ev, edm::EventSetup const& es) { } void -RandomEngineStateProducer::beginLuminosityBlock(edm::LuminosityBlock& lb, edm::EventSetup const& es) { +RandomEngineStateProducer::beginLuminosityBlock(edm::LuminosityBlock& lb, edm::EventSetup const&) { edm::Service randomService; - if (randomService.isAvailable()) { + if(randomService.isAvailable()) { std::auto_ptr states(new edm::RandomEngineStates); states->setRandomEngineStates(randomService->getLumiCache()); lb.put(states, "beginLumi"); diff --git a/IOMC/RandomEngine/src/RandomFilter.cc b/IOMC/RandomEngine/src/RandomFilter.cc index bbbb87daf904e..06ebb9554a1a7 100644 --- a/IOMC/RandomEngine/src/RandomFilter.cc +++ b/IOMC/RandomEngine/src/RandomFilter.cc @@ -1,24 +1,25 @@ #include "IOMC/RandomEngine/src/RandomFilter.h" + #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/ServiceRegistry/interface/Service.h" +#include "FWCore/Utilities/interface/Exception.h" #include "FWCore/Utilities/interface/RandomNumberGenerator.h" + #include "CLHEP/Random/RandFlat.h" -#include "FWCore/Utilities/interface/Exception.h" using namespace edm; RandomFilter::RandomFilter(edm::ParameterSet const& ps) : acceptRate_(ps.getUntrackedParameter("acceptRate")), - flatDistribution_() -{ + flatDistribution_() { Service rng; - if ( ! rng.isAvailable()) { + if(!rng.isAvailable()) { throw cms::Exception("Configuration") << "RandomFilter requires the RandomNumberGeneratorService,\n" "which is not present in the configuration file. You must add\n" "the service in the configuration file or remove the modules that\n" - "require it.\n"; + "require it.\n"; } CLHEP::HepRandomEngine& engine = rng->getEngine(); @@ -26,12 +27,10 @@ RandomFilter::RandomFilter(edm::ParameterSet const& ps) : flatDistribution_.reset(new CLHEP::RandFlat(engine, 0.0, 1.0)); } -RandomFilter::~RandomFilter() -{ +RandomFilter::~RandomFilter() { } -bool RandomFilter::filter(edm::Event& e, edm::EventSetup const& c) -{ +bool RandomFilter::filter(edm::Event&, edm::EventSetup const&) { if (flatDistribution_->fire() < acceptRate_) return true; return false; } diff --git a/IOMC/RandomEngine/src/RandomNumberGeneratorService.cc b/IOMC/RandomEngine/src/RandomNumberGeneratorService.cc index 146d149574015..ceb4dcab6245a 100644 --- a/IOMC/RandomEngine/src/RandomNumberGeneratorService.cc +++ b/IOMC/RandomEngine/src/RandomNumberGeneratorService.cc @@ -2,7 +2,7 @@ // // Package: RandomEngine // Class : RandomNumberGeneratorService -// +// // Implementation: // // @@ -12,29 +12,29 @@ #include "IOMC/RandomEngine/src/RandomNumberGeneratorService.h" -#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h" +#include "DataFormats/Provenance/interface/ModuleDescription.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/LuminosityBlock.h" -#include "DataFormats/Provenance/interface/ModuleDescription.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "SimDataFormats/RandomEngine/interface/RandomEngineState.h" -#include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h" -#include "CLHEP/Random/JamesRandom.h" -#include "CLHEP/Random/RanecuEngine.h" -#include "CLHEP/Random/engineIDulong.h" -#include "FWCore/Utilities/interface/EDMException.h" +#include "FWCore/MessageLogger/interface/JobReport.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" #include "FWCore/ParameterSet/interface/ParameterWildcard.h" - +#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h" #include "FWCore/ServiceRegistry/interface/Service.h" -#include "FWCore/MessageLogger/interface/JobReport.h" +#include "FWCore/Utilities/interface/EDMException.h" #include "IOMC/RandomEngine/src/TRandomAdaptor.h" +#include "SimDataFormats/RandomEngine/interface/RandomEngineState.h" +#include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h" + +#include "CLHEP/Random/engineIDulong.h" +#include "CLHEP/Random/JamesRandom.h" +#include "CLHEP/Random/RanecuEngine.h" #include #include -#include #include +#include namespace edm { namespace service { @@ -55,8 +55,8 @@ namespace edm { eventSeedOffset_(pset.getUntrackedParameter("eventSeedOffset")), failedToFindStatesInLumi_(false) { - if (!restoreFileName_.empty() && !restoreStateLabel_.empty()) { - throw edm::Exception(edm::errors::Configuration) + if(!restoreFileName_.empty() && !restoreStateLabel_.empty()) { + throw Exception(errors::Configuration) << "In the configuration for the RandomNumberGeneratorService both\n" << "restoreFileName and restoreStateLabel were set to nonempty values\n" << "which is illegal. It is impossible to restore the random engine\n" @@ -65,8 +65,8 @@ namespace edm { // The saveFileName must correspond to a file name without any path specification. // Throw if that is not true. - if (!saveFileName_.empty() && (saveFileName_.find("/") != std::string::npos)) { - throw edm::Exception(edm::errors::Configuration) + if(!saveFileName_.empty() && (saveFileName_.find("/") != std::string::npos)) { + throw Exception(errors::Configuration) << "The saveFileName parameter must be a simple file name with no path\n" << "specification. In the configuration, it was given the value \"" << saveFileName_ << "\"\n"; @@ -75,17 +75,16 @@ namespace edm { // Check if the configuration file is still expressed in the old style. // We do this by looking for a PSet named moduleSeeds. This parameter // is unique to an old style cfg file. - if (pset.exists("moduleSeeds")) { + if(pset.exists("moduleSeeds")) { oldStyleConfig(pset); - } - else { + } else { uint32_t initialSeed; VUint32 initialSeedSet; std::string engineName; VString pSets = pset.getParameterNamesForType(); - for (VString::const_iterator it = pSets.begin(), itEnd = pSets.end(); it != itEnd; ++it) { + for(VString::const_iterator it = pSets.begin(), itEnd = pSets.end(); it != itEnd; ++it) { ParameterSet const& modulePSet = pset.getParameterSet(*it); engineName = modulePSet.getUntrackedParameter("engineName", std::string("HepJamesRandom")); @@ -93,26 +92,23 @@ namespace edm { bool initialSeedExists = modulePSet.exists("initialSeed"); bool initialSeedSetExists = modulePSet.exists("initialSeedSet"); - if (initialSeedExists && initialSeedSetExists) { - throw edm::Exception(edm::errors::Configuration) + if(initialSeedExists && initialSeedSetExists) { + throw Exception(errors::Configuration) << "For the module with the label \"" << *it << "\",\n" << "both the parameters \"initialSeed\" and \"initialSeedSet\"\n" << "have been set in the configuration. You must set one or\n" << "the other. It is illegal to set both.\n"; - } - else if (!initialSeedExists && !initialSeedSetExists) { - throw edm::Exception(edm::errors::Configuration) + } else if(!initialSeedExists && !initialSeedSetExists) { + throw Exception(errors::Configuration) << "For the module with the label \"" << *it << "\",\n" << "neither the parameter \"initialSeed\" nor \"initialSeedSet\"\n" << "has been set in the configuration. You must set one or\n" << "the other.\n"; - } - else if (initialSeedExists) { + } else if(initialSeedExists) { initialSeed = modulePSet.getUntrackedParameter("initialSeed"); initialSeedSet.clear(); initialSeedSet.push_back(initialSeed); - } - else if (initialSeedSetExists) { + } else if(initialSeedSetExists) { initialSeedSet = modulePSet.getUntrackedParameter("initialSeedSet"); } seedMap_[*it] = initialSeedSet; @@ -120,9 +116,9 @@ namespace edm { // For the CLHEP::RanecuEngine case, require a seed set containing exactly two seeds. - if (engineName == std::string("RanecuEngine")) { - if (initialSeedSet.size() != 2U) { - throw edm::Exception(edm::errors::Configuration) + if(engineName == std::string("RanecuEngine")) { + if(initialSeedSet.size() != 2U) { + throw Exception(errors::Configuration) << "Random engines of type \"RanecuEngine\" require 2 seeds\n" << "be specified with the parameter named \"initialSeedSet\".\n" << "Either \"initialSeedSet\" was not in the configuration\n" @@ -131,23 +127,23 @@ namespace edm { boost::shared_ptr engine(new CLHEP::RanecuEngine()); engineMap_[*it] = engine; - if (initialSeedSet[0] > maxSeedRanecu || + if(initialSeedSet[0] > maxSeedRanecu || initialSeedSet[1] > maxSeedRanecu) { // They need to fit in a 31 bit integer - throw edm::Exception(edm::errors::Configuration) + throw Exception(errors::Configuration) << "The RanecuEngine seeds should be in the range 0 to 2147483647.\n" << "The seeds passed to the RandomNumberGenerationService from the\n" - "configuration file were " << initialSeedSet[0] << " and " << initialSeedSet[1] + "configuration file were " << initialSeedSet[0] << " and " << initialSeedSet[1] << "\nThis was for the module with label \"" << *it << "\".\n"; } long int seedL[2]; seedL[0] = static_cast(initialSeedSet[0]); seedL[1] = static_cast(initialSeedSet[1]); - engine->setSeeds(seedL,0); + engine->setSeeds(seedL, 0); } // For the other engines, one seed is required else { - if (initialSeedSet.size() != 1U) { - throw edm::Exception(edm::errors::Configuration) + if(initialSeedSet.size() != 1U) { + throw Exception(errors::Configuration) << "Random engines of type \"HepJamesRandom\" and \"TRandom3\n" << "require exactly 1 seed be specified in the configuration.\n" << "There were " << initialSeedSet.size() << " seeds set for the\n" @@ -155,18 +151,17 @@ namespace edm { } long int seedL = static_cast(initialSeedSet[0]); - if (engineName == "HepJamesRandom") { - if (initialSeedSet[0] > maxSeedHepJames) { - throw edm::Exception(edm::errors::Configuration) + if(engineName == "HepJamesRandom") { + if(initialSeedSet[0] > maxSeedHepJames) { + throw Exception(errors::Configuration) << "The CLHEP::HepJamesRandom engine seed should be in the range 0 to 900000000.\n" << "The seed passed to the RandomNumberGenerationService from the\n" - "configuration file was " << initialSeedSet[0] << ". This was for \n" + "configuration file was " << initialSeedSet[0] << ". This was for \n" << "the module with label " << *it << ".\n"; } boost::shared_ptr engine(new CLHEP::HepJamesRandom(seedL)); engineMap_[*it] = engine; - } - else if(engineName == "TRandom3") { + } else if(engineName == "TRandom3") { // There is a dangerous conversion from uint32_t to long // that occurs above. In the next 2 lines we check the @@ -180,44 +175,43 @@ namespace edm { boost::shared_ptr engine(new TRandomAdaptor(seedL)); engineMap_[*it] = engine; - } - else { - throw edm::Exception(edm::errors::Configuration) + } else { + throw Exception(errors::Configuration) << "The random engine name, \"" << engineName << "\", does not correspond to a supported engine.\n" << "This engine was configured for the module with label \"" << *it << "\""; - } + } } } } - activityRegistry.watchPostBeginLumi(this,&RandomNumberGeneratorService::postBeginLumi); + activityRegistry.watchPostBeginLumi(this, &RandomNumberGeneratorService::postBeginLumi); - activityRegistry.watchPreModuleConstruction(this,&RandomNumberGeneratorService::preModuleConstruction); - activityRegistry.watchPostModuleConstruction(this,&RandomNumberGeneratorService::postModuleConstruction); + activityRegistry.watchPreModuleConstruction(this, &RandomNumberGeneratorService::preModuleConstruction); + activityRegistry.watchPostModuleConstruction(this, &RandomNumberGeneratorService::postModuleConstruction); - activityRegistry.watchPreModuleBeginJob(this,&RandomNumberGeneratorService::preModuleBeginJob); - activityRegistry.watchPostModuleBeginJob(this,&RandomNumberGeneratorService::postModuleBeginJob); + activityRegistry.watchPreModuleBeginJob(this, &RandomNumberGeneratorService::preModuleBeginJob); + activityRegistry.watchPostModuleBeginJob(this, &RandomNumberGeneratorService::postModuleBeginJob); - activityRegistry.watchPreModuleBeginRun(this,&RandomNumberGeneratorService::preModuleBeginRun); - activityRegistry.watchPostModuleBeginRun(this,&RandomNumberGeneratorService::postModuleBeginRun); + activityRegistry.watchPreModuleBeginRun(this, &RandomNumberGeneratorService::preModuleBeginRun); + activityRegistry.watchPostModuleBeginRun(this, &RandomNumberGeneratorService::postModuleBeginRun); - activityRegistry.watchPreModuleBeginLumi(this,&RandomNumberGeneratorService::preModuleBeginLumi); - activityRegistry.watchPostModuleBeginLumi(this,&RandomNumberGeneratorService::postModuleBeginLumi); + activityRegistry.watchPreModuleBeginLumi(this, &RandomNumberGeneratorService::preModuleBeginLumi); + activityRegistry.watchPostModuleBeginLumi(this, &RandomNumberGeneratorService::postModuleBeginLumi); - activityRegistry.watchPreModule(this,&RandomNumberGeneratorService::preModule); - activityRegistry.watchPostModule(this,&RandomNumberGeneratorService::postModule); + activityRegistry.watchPreModule(this, &RandomNumberGeneratorService::preModule); + activityRegistry.watchPostModule(this, &RandomNumberGeneratorService::postModule); - activityRegistry.watchPreModuleEndLumi(this,&RandomNumberGeneratorService::preModuleEndLumi); - activityRegistry.watchPostModuleEndLumi(this,&RandomNumberGeneratorService::postModuleEndLumi); + activityRegistry.watchPreModuleEndLumi(this, &RandomNumberGeneratorService::preModuleEndLumi); + activityRegistry.watchPostModuleEndLumi(this, &RandomNumberGeneratorService::postModuleEndLumi); - activityRegistry.watchPreModuleEndRun(this,&RandomNumberGeneratorService::preModuleEndRun); - activityRegistry.watchPostModuleEndRun(this,&RandomNumberGeneratorService::postModuleEndRun); + activityRegistry.watchPreModuleEndRun(this, &RandomNumberGeneratorService::preModuleEndRun); + activityRegistry.watchPostModuleEndRun(this, &RandomNumberGeneratorService::postModuleEndRun); - activityRegistry.watchPreModuleEndJob(this,&RandomNumberGeneratorService::preModuleEndJob); - activityRegistry.watchPostModuleEndJob(this,&RandomNumberGeneratorService::postModuleEndJob); + activityRegistry.watchPreModuleEndJob(this, &RandomNumberGeneratorService::preModuleEndJob); + activityRegistry.watchPostModuleEndJob(this, &RandomNumberGeneratorService::postModuleEndJob); - activityRegistry.watchPostForkReacquireResources(this,&RandomNumberGeneratorService::postForkReacquireResources); + activityRegistry.watchPostForkReacquireResources(this, &RandomNumberGeneratorService::postForkReacquireResources); // the default for the stack is to point to the 'end' of our map which is used to define not set engineStack_.push_back(engineMap_.end()); @@ -230,12 +224,12 @@ namespace edm { RandomNumberGeneratorService::~RandomNumberGeneratorService() { } - CLHEP::HepRandomEngine& + CLHEP::HepRandomEngine& RandomNumberGeneratorService::getEngine() const { - if (currentEngine_ == engineMap_.end()) { - if (currentLabel_ != std::string() ) { - throw edm::Exception(edm::errors::Configuration) + if(currentEngine_ == engineMap_.end()) { + if(currentLabel_ != std::string()) { + throw Exception(errors::Configuration) << "The module with label \"" << currentLabel_ << "\" requested a random number engine from the \n" @@ -248,10 +242,9 @@ namespace edm { " engineName = cms.untracked.string('TRandom3')\n" " )\n" "where you replace \"your_seed\" with a number and add a comma if necessary\n" - "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n"; - } - else { - throw edm::Exception(edm::errors::Unknown) + "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n"; + } else { + throw Exception(errors::Unknown) << "Requested a random number engine from the RandomNumberGeneratorService\n" "when no module was active. This is not supposed to be possible.\n" "Please inform the edm developers about this. It would be helpful to\n" @@ -259,7 +252,7 @@ namespace edm { "happen. Sources are not supposed to be doing that anymore.\n"; } } - return *(currentEngine_->second); + return *(currentEngine_->second); } uint32_t @@ -268,9 +261,9 @@ namespace edm { std::map::const_iterator iter; iter = seedMap_.find(currentLabel_); - if (iter == seedMap_.end()) { - if (currentLabel_ != std::string() ) { - throw edm::Exception(edm::errors::Configuration) + if(iter == seedMap_.end()) { + if(currentLabel_ != std::string()) { + throw Exception(errors::Configuration) << "The module with label \"" << currentLabel_ << "\" requested a random number seed from the \n" @@ -283,10 +276,9 @@ namespace edm { " engineName = cms.untracked.string('TRandom3')\n" " )\n" "where you replace \"your_seed\" with a number and add a comma if necessary\n" - "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n"; - } - else { - throw edm::Exception(edm::errors::Unknown) + "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n"; + } else { + throw Exception(errors::Unknown) << "Requested a random number seed from the RandomNumberGeneratorService\n" "when no module was active. This is not supposed to be possible.\n" "Please inform the edm developers about this. It would be helpful to\n" @@ -298,7 +290,7 @@ namespace edm { } void - RandomNumberGeneratorService::fillDescriptions(ConfigurationDescriptions & descriptions) { + RandomNumberGeneratorService::fillDescriptions(ConfigurationDescriptions& descriptions) { ParameterSetDescription desc; std::string emptyString; @@ -320,7 +312,7 @@ namespace edm { val.addOptionalUntracked >("initialSeedSet")->setComment("New interface only"); val.addOptionalUntracked("engineName",std::string("HepJamesRandom"))->setComment("New interface only"); - ParameterWildcard wnode("*", edm::RequireZeroOrMore, true, val); + ParameterWildcard wnode("*", RequireZeroOrMore, true, val); wnode.setComment("In the new interface, the name of each ParameterSet will be the associated module label. " "In the old deprecated interface there will be one ParameterSet named moduleSeeds"); desc.addNode(wnode); @@ -335,10 +327,10 @@ namespace edm { } void - RandomNumberGeneratorService::postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren) { + RandomNumberGeneratorService::postForkReacquireResources(unsigned childIndex, unsigned /*kMaxChildren*/) { childIndex_ = childIndex; - if (!saveFileName_.empty()) { + if(!saveFileName_.empty()) { std::ostringstream suffix; suffix << "_" << childIndex; saveFileName_ += suffix.str(); @@ -352,25 +344,24 @@ namespace edm { void RandomNumberGeneratorService::preBeginLumi(LuminosityBlock const& lumi) { - if (firstLumi_) { + if(firstLumi_) { // copy state from engines to lumi cache snapShot(lumiCache_); - if (!restoreFileName_.empty()) { + if(!restoreFileName_.empty()) { // copy state from text file to lumi cache readLumiStatesFromTextFile(restoreFileName_); } - } - else { + } else { snapShot(eventCache_); } // copy state from LuminosityBlock to lumi cache - if (!restoreStateLabel_.empty()) { + if(!restoreStateLabel_.empty()) { readFromLuminosityBlock(lumi); } - if (!firstLumi_ || !restoreFileName_.empty() || !restoreStateLabel_.empty()) { + if(!firstLumi_ || !restoreFileName_.empty() || !restoreStateLabel_.empty()) { // copy state from lumi cache to engines restoreFromCache(lumiCache_); } @@ -381,18 +372,18 @@ namespace edm { // in a path in the configuration void - RandomNumberGeneratorService::postBeginLumi(LuminosityBlock const& lumi, EventSetup const& es) { + RandomNumberGeneratorService::postBeginLumi(LuminosityBlock const&, EventSetup const&) { - if (firstLumi_) { + if(firstLumi_) { // reset state with new seeds based on child index startNewSequencesForEvents(); - if (!restoreFileName_.empty()) { + if(!restoreFileName_.empty()) { snapShot(eventCache_); // copy state from text file to event cache readEventStatesFromTextFile(restoreFileName_); } } - if (!firstLumi_ || !restoreFileName_.empty()) { + if(!firstLumi_ || !restoreFileName_.empty()) { // copy state from event cache to engines restoreFromCache(eventCache_); } @@ -402,21 +393,20 @@ namespace edm { void RandomNumberGeneratorService::postEventRead(Event const& event) { // copy from Event to event cache - if (!restoreStateLabel_.empty()) { + if(!restoreStateLabel_.empty()) { snapShot(eventCache_); readFromEvent(event); // copy from event cache to engines restoreFromCache(eventCache_); - } - else { + } else { // copy from engines to event cache snapShot(eventCache_); } // if requested write text file from both caches - if (!saveFileName_.empty()) { + if(!saveFileName_.empty()) { saveStatesToFile(saveFileName_); - if (!saveFileNameRecorded_) { + if(!saveFileNameRecorded_) { std::string fullName = constructSaveFileName(); Service reportSvc; reportSvc->reportRandomStateFile(fullName); @@ -430,25 +420,23 @@ namespace edm { // in a path in the configuration void - RandomNumberGeneratorService::preModuleConstruction(ModuleDescription const& description) - { + RandomNumberGeneratorService::preModuleConstruction(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } - void - RandomNumberGeneratorService::postModuleConstruction(ModuleDescription const& description) - { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + void + RandomNumberGeneratorService::postModuleConstruction(ModuleDescription const& description) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during module construction because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during module construction for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -458,21 +446,21 @@ namespace edm { void RandomNumberGeneratorService::preModuleBeginJob(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } void RandomNumberGeneratorService::postModuleBeginJob(ModuleDescription const& description) { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during beginJob because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during beginJob for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -482,21 +470,21 @@ namespace edm { void RandomNumberGeneratorService::preModuleBeginRun(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } void RandomNumberGeneratorService::postModuleBeginRun(ModuleDescription const& description) { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during beginRun because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during beginRun for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -509,7 +497,7 @@ namespace edm { } void - RandomNumberGeneratorService::postModuleBeginLumi(ModuleDescription const& description) { + RandomNumberGeneratorService::postModuleBeginLumi(ModuleDescription const&) { pop(); } @@ -519,28 +507,28 @@ namespace edm { } void - RandomNumberGeneratorService::postModule(ModuleDescription const& description) { + RandomNumberGeneratorService::postModule(ModuleDescription const&) { pop(); } void RandomNumberGeneratorService::preModuleEndLumi(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } void RandomNumberGeneratorService::postModuleEndLumi(ModuleDescription const& description) { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during endLumi because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during endLumi for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -550,21 +538,21 @@ namespace edm { void RandomNumberGeneratorService::preModuleEndRun(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } void RandomNumberGeneratorService::postModuleEndRun(ModuleDescription const& description) { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during endRun because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during endRun for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -574,21 +562,21 @@ namespace edm { void RandomNumberGeneratorService::preModuleEndJob(ModuleDescription const& description) { push(description.moduleLabel()); - if (enableChecking_ && currentEngine_ != engineMap_.end()) { + if(enableChecking_ && currentEngine_ != engineMap_.end()) { engineStateStack_.push_back(currentEngine_->second->put()); } } void RandomNumberGeneratorService::postModuleEndJob(ModuleDescription const& description) { - if (enableChecking_ && currentEngine_ != engineMap_.end()) { - if (engineStateStack_.back() != currentEngine_->second->put()) { - throw edm::Exception(edm::errors::LogicError) + if(enableChecking_ && currentEngine_ != engineMap_.end()) { + if(engineStateStack_.back() != currentEngine_->second->put()) { + throw Exception(errors::LogicError) << "It is illegal to generate random numbers during endJob because \n" "that makes it very difficult to reproduce the processing of individual\n" "events. Random numbers were generated during endJob for the module with\n" - "class name \"" << description.moduleName() << "\"\n" - "and module label \"" << description.moduleLabel() << "\"\n"; + "class name \"" << description.moduleName() << "\"\n" + "and module label \"" << description.moduleLabel() << "\"\n"; } engineStateStack_.pop_back(); } @@ -610,12 +598,12 @@ namespace edm { std::cout << "\n\nRandomNumberGeneratorService dump\n\n"; std::cout << " Contents of seedMap\n"; - for (std::map >::const_iterator iter = seedMap_.begin(); + for(std::map >::const_iterator iter = seedMap_.begin(); iter != seedMap_.end(); ++iter) { std::cout << " " << iter->first; std::vector seeds = iter->second; - for (std::vector::const_iterator vIter = seeds.begin(); + for(std::vector::const_iterator vIter = seeds.begin(); vIter != seeds.end(); ++vIter) { std::cout << " " << *vIter; @@ -623,38 +611,36 @@ namespace edm { std::cout << "\n"; } std::cout << "\n Contents of engineNameMap\n"; - for (std::map::const_iterator iter = engineNameMap_.begin(); + for(std::map::const_iterator iter = engineNameMap_.begin(); iter != engineNameMap_.end(); ++iter) { std::cout << " " << iter->first << " " << iter->second << "\n"; } std::cout << "\n Contents of engineMap\n"; - for (EngineMap::const_iterator iter = engineMap_.begin(); + for(EngineMap::const_iterator iter = engineMap_.begin(); iter != engineMap_.end(); ++iter) { std::cout << " " << iter->first << " " << iter->second->name() << " "; - if (iter->second->name() == std::string("HepJamesRandom")) { - std::cout << iter->second->getSeed(); - } - else { - std::cout << "Engine does not know original seed"; + if(iter->second->name() == std::string("HepJamesRandom")) { + std::cout << iter->second->getSeed(); + } else { + std::cout << "Engine does not know original seed"; } - std::cout << "\n"; + std::cout << "\n"; } std::cout << "\n"; std::cout << " currentLabel_ = " << currentLabel_ << "\n"; std::cout << " labelStack_ size = " << labelStack_.size() << "\n"; int i = 0; - for (VString::const_iterator iter = labelStack_.begin(); + for(VString::const_iterator iter = labelStack_.begin(); iter != labelStack_.end(); ++iter, ++i) { - std::cout << " " << i << " " << *iter << "\n"; + std::cout << " " << i << " " << *iter << "\n"; } - if (currentEngine_ == engineMap_.end()) { + if(currentEngine_ == engineMap_.end()) { std::cout << " currentEngine points to end\n"; - } - else { + } else { std::cout << " currentEngine_ = " << currentEngine_->first << " " << currentEngine_->second->name() << " " << currentEngine_->second->getSeed() << "\n"; @@ -662,15 +648,14 @@ namespace edm { std::cout << " engineStack_ size = " << engineStack_.size() << "\n"; i = 0; - for (std::vector::const_iterator iter = engineStack_.begin(); + for(std::vector::const_iterator iter = engineStack_.begin(); iter != engineStack_.end(); ++iter, ++i) { - if (*iter == engineMap_.end()) { - std::cout << " " << i << " Points to end of engine map\n"; - } - else { - std::cout << " " << i << " " << (*iter)->first - << " " << (*iter)->second->name() << " " << (*iter)->second->getSeed() << "\n"; + if(*iter == engineMap_.end()) { + std::cout << " " << i << " Points to end of engine map\n"; + } else { + std::cout << " " << i << " " << (*iter)->first + << " " << (*iter)->second->name() << " " << (*iter)->second->getSeed() << "\n"; } } @@ -680,18 +665,16 @@ namespace edm { } void - RandomNumberGeneratorService::push(std::string const& iLabel) - { + RandomNumberGeneratorService::push(std::string const& iLabel) { currentEngine_ = engineMap_.find(iLabel); engineStack_.push_back(currentEngine_); - + labelStack_.push_back(iLabel); currentLabel_ = iLabel; } void - RandomNumberGeneratorService::pop() - { + RandomNumberGeneratorService::pop() { engineStack_.pop_back(); //NOTE: algorithm is such that we always have at least one item in the stacks currentEngine_ = engineStack_.back(); @@ -702,11 +685,11 @@ namespace edm { void RandomNumberGeneratorService::readFromLuminosityBlock(LuminosityBlock const& lumi) { - Handle states; + Handle states; lumi.getByLabel(restoreStateLabel_, "beginLumi", states); - if (!states.isValid()) { + if(!states.isValid()) { failedToFindStatesInLumi_ = true; return; } @@ -717,16 +700,15 @@ namespace edm { void RandomNumberGeneratorService::readFromEvent(Event const& event) { - Handle states; + Handle states; event.getByLabel(restoreStateLabel_, states); - if (!states.isValid()) { - if (failedToFindStatesInLumi_ && backwardCompatibilityRead(event)) { + if(!states.isValid()) { + if(failedToFindStatesInLumi_ && backwardCompatibilityRead(event)) { return; - } - else { - throw edm::Exception(edm::errors::ProductNotFound) + } else { + throw Exception(errors::ProductNotFound) << "The RandomNumberGeneratorService is trying to restore\n" << "the state of the random engines by reading a product from\n" << "the Event with label \"" << restoreStateLabel_ << "\". It\n" @@ -737,8 +719,8 @@ namespace edm { << "a previous process\n"; } } - if (failedToFindStatesInLumi_) { - throw edm::Exception(edm::errors::ProductNotFound) + if(failedToFindStatesInLumi_) { + throw Exception(errors::ProductNotFound) << "The RandomNumberGeneratorService is trying to restore\n" << "the state of the random engines by reading a product from\n" << "the Event and LuminosityBlock with label \"" << restoreStateLabel_ << "\".\n" @@ -755,21 +737,21 @@ namespace edm { Handle > states; event.getByLabel(restoreStateLabel_, states); - if (!states.isValid()) { + if(!states.isValid()) { return false; } - for (std::vector::const_iterator state = states->begin(), + for(std::vector::const_iterator state = states->begin(), iEnd = states->end(); state != iEnd; ++state) { - std::vector::iterator cachedState = + std::vector::iterator cachedState = std::lower_bound(eventCache_.begin(), eventCache_.end(), *state); - if (cachedState != eventCache_.end() && cachedState->getLabel() == state->getLabel()) { - if (cachedState->getSeed().size() != state->getSeed().size() || + if(cachedState != eventCache_.end() && cachedState->getLabel() == state->getLabel()) { + if(cachedState->getSeed().size() != state->getSeed().size() || cachedState->getState().size() != state->getState().size()) { - throw edm::Exception(edm::errors::Configuration) + throw Exception(errors::Configuration) << "In function RandomNumberGeneratorService::backwardCompatibilityRead.\n" << "When attempting to replay processing with the RandomNumberGeneratorService,\n" << "the engine type for each module must be the same in the replay configuration\n" @@ -785,12 +767,11 @@ namespace edm { } void - RandomNumberGeneratorService::snapShot(std::vector & cache) - { + RandomNumberGeneratorService::snapShot(std::vector& cache) { cache.resize(engineMap_.size()); std::vector::iterator state = cache.begin(); - for (EngineMap::const_iterator iter = engineMap_.begin(); + for(EngineMap::const_iterator iter = engineMap_.begin(); iter != engineMap_.end(); ++iter, ++state) { @@ -800,7 +781,7 @@ namespace edm { std::vector stateL = iter->second->put(); state->clearStateVector(); state->reserveStateVector(stateL.size()); - for (std::vector::const_iterator vIter = stateL.begin(); + for(std::vector::const_iterator vIter = stateL.begin(); vIter != stateL.end(); ++vIter) { state->push_back_stateVector(static_cast(*vIter)); @@ -810,7 +791,7 @@ namespace edm { void RandomNumberGeneratorService::restoreFromCache(std::vector const& cache) { - for (std::vector::const_iterator iter = cache.begin(), + for(std::vector::const_iterator iter = cache.begin(), iEnd = cache.end(); iter != iEnd; ++iter) { @@ -818,7 +799,7 @@ namespace edm { std::vector const& engineState = iter->getState(); std::vector engineStateL; - for (std::vector::const_iterator iVal = engineState.begin(), + for(std::vector::const_iterator iVal = engineState.begin(), theEnd = engineState.end(); iVal != theEnd; ++iVal) { engineStateL.push_back(static_cast(*iVal)); @@ -826,8 +807,8 @@ namespace edm { std::vector const& engineSeeds = iter->getSeed(); std::vector engineSeedsL; - for (std::vector::const_iterator iVal = engineSeeds.begin(), - theEnd = engineSeeds.end(); + for(std::vector::const_iterator iVal = engineSeeds.begin(), + theEnd = engineSeeds.end(); iVal != theEnd; ++iVal) { long seedL = static_cast(*iVal); @@ -846,39 +827,36 @@ namespace edm { EngineMap::iterator engine = engineMap_.find(engineLabel); - if (engine != engineMap_.end()) { + if(engine != engineMap_.end()) { seedMap_[engineLabel] = engineSeeds; // We need to handle each type of engine differently because each // has different requirements on the seed or seeds. - if (engineStateL[0] == CLHEP::engineIDulong()) { + if(engineStateL[0] == CLHEP::engineIDulong()) { checkEngineType(engine->second->name(), std::string("HepJamesRandom"), engineLabel); // These two lines actually restore the seed and engine state. engine->second->setSeed(engineSeedsL[0], 0); engine->second->get(engineStateL); - } - else if (engineStateL[0] == CLHEP::engineIDulong()) { + } else if(engineStateL[0] == CLHEP::engineIDulong()) { checkEngineType(engine->second->name(), std::string("RanecuEngine"), engineLabel); // This line actually restores the engine state. engine->second->get(engineStateL); - } - else if (engineStateL[0] == CLHEP::engineIDulong()) { + } else if(engineStateL[0] == CLHEP::engineIDulong()) { checkEngineType(engine->second->name(), std::string("TRandom3"), engineLabel); // This line actually restores the engine state. engine->second->setSeed(engineSeedsL[0], 0); engine->second->get(engineStateL); - } - // This should not be possible because this code should be able to restore - // any kind of engine whose state can be saved. - else { - throw edm::Exception(edm::errors::Unknown) + } else { + // This should not be possible because this code should be able to restore + // any kind of engine whose state can be saved. + throw Exception(errors::Unknown) << "The RandomNumberGeneratorService is trying to restore the state\n" "of the random engines. The state in the event indicates an engine\n" "of an unknown type. This should not be possible unless you are\n" @@ -894,15 +872,14 @@ namespace edm { void RandomNumberGeneratorService::checkEngineType(std::string const& typeFromConfig, std::string const& typeFromEvent, - std::string const& engineLabel) - { - if (typeFromConfig != typeFromEvent) { - throw edm::Exception(edm::errors::Configuration) + std::string const& engineLabel) { + if(typeFromConfig != typeFromEvent) { + throw Exception(errors::Configuration) << "The RandomNumberGeneratorService is trying to restore\n" - << "the state of the random engine for the module \"" + << "the state of the random engine for the module \"" << engineLabel << "\". An\n" << "error was detected because the type of the engine in the\n" - << "input file and the configuration file do not match.\n" + << "input file and the configuration file do not match.\n" << "In the configuration file the type is \"" << typeFromConfig << "\".\nIn the input file the type is \"" << typeFromEvent << "\". If\n" << "you are not generating any random numbers in this module, then\n" @@ -914,17 +891,16 @@ namespace edm { } void - RandomNumberGeneratorService::saveStatesToFile(std::string const& fileName) - { - if (!outFile_.is_open()) { + RandomNumberGeneratorService::saveStatesToFile(std::string const& fileName) { + if(!outFile_.is_open()) { outFile_.open(fileName.c_str(), std::ofstream::out | std::ofstream::trunc); } - if (!outFile_) { - throw edm::Exception(edm::errors::Configuration) + if(!outFile_) { + throw Exception(errors::Configuration) << "Unable to open the file \"" << fileName << "\" to save the state of the random engines.\n"; } - outFile_.seekp( 0, std::ios_base::beg); + outFile_.seekp(0, std::ios_base::beg); outFile_ << "\n"; outFile_ << "\n"; @@ -941,8 +917,8 @@ namespace edm { void RandomNumberGeneratorService::writeStates(std::vector const& v, - std::ofstream & outFile) { - for (std::vector::const_iterator iter = v.begin(), + std::ofstream& outFile) { + for(std::vector::const_iterator iter = v.begin(), iEnd = v.end(); iter != iEnd; ++iter) { @@ -967,19 +943,17 @@ namespace edm { void RandomNumberGeneratorService::writeVector(VUint32 const& v, - std::ofstream & outFile) - { - if (v.empty()) return; + std::ofstream& outFile) { + if(v.empty()) return; size_t numItems = v.size(); - for (size_t i = 0; i < numItems; ++i) { - if (i != 0 && i % 10 == 0) outFile << "\n"; + for(size_t i = 0; i < numItems; ++i) { + if(i != 0 && i % 10 == 0) outFile << "\n"; outFile << std::setw(13) << v[i]; } outFile << "\n"; } - std::string RandomNumberGeneratorService::constructSaveFileName() - { + std::string RandomNumberGeneratorService::constructSaveFileName() { char directory[1500]; std::string fullName(getcwd(directory, sizeof(directory)) ? directory : "/PathIsTooBig"); fullName += "/" + saveFileName_; @@ -1001,33 +975,33 @@ namespace edm { void RandomNumberGeneratorService::readStatesFromFile(std::string const& fileName, - std::vector & cache, + std::vector& cache, std::string const& whichStates) { std::ifstream inFile; inFile.open(fileName.c_str(), std::ifstream::in); - if (!inFile) { - throw edm::Exception(edm::errors::Configuration) + if(!inFile) { + throw Exception(errors::Configuration) << "Unable to open the file \"" << fileName << "\" to restore the random engine states.\n"; } std::string text; inFile >> text; - if (!inFile.good() || text != std::string("")) { - throw edm::Exception(edm::errors::Configuration) + if(!inFile.good() || text != std::string("")) { + throw Exception(errors::Configuration) << "Attempting to read file with random number engine states.\n" - << "File \"" << restoreFileName_ + << "File \"" << restoreFileName_ << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read the file header word.\n"; } bool saveToCache = false; - while (readEngineState(inFile, cache, whichStates, saveToCache)) { } + while(readEngineState(inFile, cache, whichStates, saveToCache)) {} } - bool RandomNumberGeneratorService::readEngineState(std::istream &is, - std::vector & cache, - std::string const& whichStates, - bool & saveToCache) { + bool RandomNumberGeneratorService::readEngineState(std::istream& is, + std::vector& cache, + std::string const& whichStates, + bool& saveToCache) { std::string leading; std::string trailing; std::string moduleLabel; @@ -1041,25 +1015,25 @@ namespace edm { // and end of the data for different sections. is >> leading; - if (!is.good()) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + if(!is.good()) { + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read next field and did not hit the end yet.\n"; } // This marks the end of the file. We are done. - if (leading == std::string("")) return false; + if(leading == std::string("")) return false; // This marks the end of a section of the data - if (leading == std::string("") || + if(leading == std::string("") || leading == std::string("")) { saveToCache = false; return true; } // This marks the beginning of a section - if (leading == std::string("") || + if(leading == std::string("") || leading == std::string("")) { saveToCache = (leading == whichStates); return true; @@ -1068,87 +1042,87 @@ namespace edm { // Process the next engine state is >> moduleLabel >> trailing; - if (!is.good() || + if(!is.good() || leading != std::string("") || trailing != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read a module label when restoring random engine states.\n"; } is >> leading >> seedVectorSize >> trailing; - if (!is.good() || + if(!is.good() || leading != std::string("") || trailing != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read seed vector length when restoring random engine states.\n"; } is >> leading; - if (!is.good() || + if(!is.good() || leading != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read beginning of InitialSeeds when restoring random engine states.\n"; } - readVector(is, seedVectorSize, seedVector); + readVector(is, seedVectorSize, seedVector); is >> trailing; - if (!is.good() || + if(!is.good() || trailing != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read end of InitialSeeds when restoring random engine states.\n"; } is >> leading >> stateVectorSize >> trailing; - if (!is.good() || + if(!is.good() || leading != std::string("") || trailing != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read state vector length when restoring random engine states.\n"; } is >> leading; - if (!is.good() || + if(!is.good() || leading != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read beginning of FullState when restoring random engine states.\n"; } - readVector(is, stateVectorSize, stateVector); + readVector(is, stateVectorSize, stateVector); is >> trailing; - if (!is.good() || + if(!is.good() || trailing != std::string("")) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read end of FullState when restoring random engine states.\n"; } - if (saveToCache) { + if(saveToCache) { RandomEngineState randomEngineState; randomEngineState.setLabel(moduleLabel); - std::vector::iterator state = + std::vector::iterator state = std::lower_bound(cache.begin(), cache.end(), randomEngineState); - if (state != cache.end() && moduleLabel == state->getLabel()) { - if (seedVector.size() != state->getSeed().size() || + if(state != cache.end() && moduleLabel == state->getLabel()) { + if(seedVector.size() != state->getSeed().size() || stateVector.size() != state->getState().size()) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Vectors containing engine state are the incorrect size for the type of random engine.\n"; } state->setSeed(seedVector); @@ -1159,16 +1133,16 @@ namespace edm { } void - RandomNumberGeneratorService::readVector(std::istream &is, unsigned numItems, std::vector & v) { + RandomNumberGeneratorService::readVector(std::istream& is, unsigned numItems, std::vector& v) { v.clear(); v.reserve(numItems); uint32_t data; - for (unsigned i = 0; i < numItems; ++i) { + for(unsigned i = 0; i < numItems; ++i) { is >> data; - if (!is.good()) { - throw edm::Exception(edm::errors::Configuration) - << "File \"" << restoreFileName_ - << "\" is ill-structured or otherwise corrupted.\n" + if(!is.good()) { + throw Exception(errors::Configuration) + << "File \"" << restoreFileName_ + << "\" is ill-structured or otherwise corrupted.\n" << "Cannot read vector when restoring random engine states.\n"; } v.push_back(data); @@ -1178,21 +1152,21 @@ namespace edm { void RandomNumberGeneratorService::startNewSequencesForEvents() { - if (childIndex_ == 0U && eventSeedOffset_ == 0U) return; + if(childIndex_ == 0U && eventSeedOffset_ == 0U) return; - for (EngineMap::const_iterator iter = engineMap_.begin(); + for(EngineMap::const_iterator iter = engineMap_.begin(); iter != engineMap_.end(); ++iter) { uint32_t offset1 = childIndex_; uint32_t offset2 = eventSeedOffset_; - std::string const& moduleLabel = iter->first; - std::string const& engineName = engineNameMap_[moduleLabel]; - VUint32 & seeds = seedMap_[moduleLabel]; + std::string const& moduleLabel = iter->first; + std::string const& engineName = engineNameMap_[moduleLabel]; + VUint32& seeds = seedMap_[moduleLabel]; - if (engineName == std::string("RanecuEngine")) { - assert(seeds.size() == 2U); + if(engineName == std::string("RanecuEngine")) { + assert(seeds.size() == 2U); // Wrap around if the offsets push the seed over the maximum allowed value uint32_t mod = maxSeedRanecu + 1U; offset1 = offset1 % mod; @@ -1203,11 +1177,10 @@ namespace edm { seedL[0] = static_cast(seeds[0]); seedL[1] = static_cast(seeds[1]); iter->second->setSeeds(seedL,0); - } - else { - assert(seeds.size() == 1U); + } else { + assert(seeds.size() == 1U); - if (engineName == "HepJamesRandom") { + if(engineName == "HepJamesRandom") { // Wrap around if the offsets push the seed over the maximum allowed value uint32_t mod = maxSeedHepJames + 1U; offset1 = offset1 % mod; @@ -1217,23 +1190,20 @@ namespace edm { long int seedL = static_cast(seeds[0]); iter->second->setSeed(seedL, 0); - } - else { + } else { assert(engineName == "TRandom3"); // Wrap around if the offsets push the seed over the maximum allowed value // We have to be extra careful with this one because it may also go beyond // the values 32 bits can hold uint32_t max32 = maxSeedTRandom3; - if ((max32 - seeds[0]) >= offset1) { + if((max32 - seeds[0]) >= offset1) { seeds[0] = seeds[0] + offset1; - } - else { + } else { seeds[0] = offset1 - (max32 - seeds[0]) - 1U; } - if ((max32 - seeds[0]) >= offset2) { + if((max32 - seeds[0]) >= offset2) { seeds[0] = seeds[0] + offset2; - } - else { + } else { seeds[0] = offset2 - (max32 - seeds[0]) - 1U; } long seedL = static_cast(seeds[0]); @@ -1257,10 +1227,10 @@ namespace edm { void RandomNumberGeneratorService::oldStyleConfig(ParameterSet const& pset) { VString pSets = pset.getParameterNamesForType(); - for (VString::const_iterator it = pSets.begin(), itEnd = pSets.end(); it != itEnd; ++it) { - if (*it != std::string("moduleSeeds")) { + for(VString::const_iterator it = pSets.begin(), itEnd = pSets.end(); it != itEnd; ++it) { + if(*it != std::string("moduleSeeds")) { throw Exception(errors::Configuration) - << "RandomNumberGeneratorService supports two configuration interfaces.\n" + << "RandomNumberGeneratorService supports two configuration interfaces.\n" << "One is old and deprecated, but still supported for backward compatibility\n" << "reasons. It is illegal to mix parameters using both the old and new service\n" << "interface in the same configuration. It is assumed the old interface is being\n" @@ -1275,7 +1245,7 @@ namespace edm { std::vector seeds; VString names = moduleSeeds.getParameterNames(); - for (VString::const_iterator itName = names.begin(), itNameEnd = names.end(); + for(VString::const_iterator itName = names.begin(), itNameEnd = names.end(); itName != itNameEnd; ++itName) { uint32_t seed = moduleSeeds.getUntrackedParameter(*itName); @@ -1285,8 +1255,8 @@ namespace edm { seedMap_[*itName] = seeds; engineNameMap_[*itName] = std::string("HepJamesRandom"); - if (seed > maxSeedHepJames) { - throw edm::Exception(edm::errors::Configuration) + if(seed > maxSeedHepJames) { + throw Exception(errors::Configuration) << "The CLHEP::HepJamesRandom engine seed should be in the range 0 to 900000000.\n" << "The seed passed to the RandomNumberGenerationService from the\n" "configuration file was " << seed << ". This was for the module\n" diff --git a/IOMC/RandomEngine/src/TRandomAdaptor.cc b/IOMC/RandomEngine/src/TRandomAdaptor.cc index 4d67966e8c452..b0508fb4f8a95 100644 --- a/IOMC/RandomEngine/src/TRandomAdaptor.cc +++ b/IOMC/RandomEngine/src/TRandomAdaptor.cc @@ -5,28 +5,28 @@ #include "TBufferFile.h" -#include #include -#include -#include #include +#include +#include #include +#include namespace edm { -TRandomAdaptor::TRandomAdaptor(std::istream& is) { +TRandomAdaptor::TRandomAdaptor(std::istream&) { Grumble(std::string("Cannot instantiate a TRandom engine from an istream")); } TRandomAdaptor::~TRandomAdaptor() { } -std::ostream & TRandomAdaptor::put (std::ostream& os) const { +std::ostream& TRandomAdaptor::put(std::ostream& os) const { Grumble(std::string("put(std::ostream) not available for TRandom engines")); return os; } -std::vector TRandomAdaptor::put () const { +std::vector TRandomAdaptor::put() const { std::vector v; int32_t itemSize = sizeof(uint32_t); @@ -36,8 +36,8 @@ std::vector TRandomAdaptor::put () const { char* bufferPtr = buffer.Buffer(); int32_t numItems = (buffer.Length() + itemSize - 1) / itemSize; v.reserve(numItems + 1); - v.push_back (CLHEP::engineIDulong()); - for( int i = 0; i < numItems; ++i) { + v.push_back(CLHEP::engineIDulong()); + for(int i = 0; i < numItems; ++i) { // Here we do some ugly manipulations to the data to match the format // of the output of the CLHEP put function (the whole point of this adaptor @@ -47,13 +47,13 @@ std::vector TRandomAdaptor::put () const { // In the case of a 64 bit build the top 32 bits is only padding (all 0's). // Get the next 32 bits of data from the buffer - uint32_t value32 = *reinterpret_cast(bufferPtr + i * itemSize); + uint32_t value32 = *reinterpret_cast(bufferPtr + i * itemSize); - if (i == numItems - 1) { + if(i == numItems - 1) { int nBytes = buffer.Length() % itemSize; - if (nBytes == 1) value32 &= 0xffu; - else if (nBytes == 2) value32 &= 0xffffu; - else if (nBytes == 3) value32 &= 0xffffffu; + if(nBytes == 1) value32 &= 0xffu; + else if(nBytes == 2) value32 &= 0xffffu; + else if(nBytes == 3) value32 &= 0xffffffu; } // Push it into the vector in an unsigned long which may be 32 or 64 bits @@ -62,17 +62,17 @@ std::vector TRandomAdaptor::put () const { return v; } -std::istream & TRandomAdaptor::get (std::istream& is) { +std::istream& TRandomAdaptor::get(std::istream& is) { Grumble(std::string("get(std::istream) not available for TRandom engines")); return getState(is); } -std::istream & TRandomAdaptor::getState (std::istream& is) { +std::istream& TRandomAdaptor::getState(std::istream& is) { Grumble(std::string("getState(std::istream) not available for TRandom engines")); return is; } -bool TRandomAdaptor::get (std::vector const& v) { +bool TRandomAdaptor::get(std::vector const& v) { if(v.empty()) return false; if(v[0] != CLHEP::engineIDulong()) return false; int32_t numItems = v.size()-1; @@ -82,7 +82,7 @@ bool TRandomAdaptor::get (std::vector const& v) { char* bufferPtr = buffer.Buffer(); for(int32_t i = 0; i < numItems; ++i) { - *reinterpret_cast(bufferPtr + i * itemSize) = static_cast(v[i+1] & 0xffffffff); + *reinterpret_cast(bufferPtr + i * itemSize) = static_cast(v[i+1] & 0xffffffff); } // Note that this will fail if the TRandom3 version (specifically the TStreamerInfo) diff --git a/IOMC/RandomEngine/test/TestRandomNumberServiceAnalyzer.cc b/IOMC/RandomEngine/test/TestRandomNumberServiceAnalyzer.cc index 3a18fae46335e..3f5ddf07ea440 100644 --- a/IOMC/RandomEngine/test/TestRandomNumberServiceAnalyzer.cc +++ b/IOMC/RandomEngine/test/TestRandomNumberServiceAnalyzer.cc @@ -2,10 +2,10 @@ // // Package: IOMC/RandomEngine // Class: TestRandomNumberServiceAnalyzer -// +// /**\class TestRandomNumberServiceAnalyzer TestRandomNumberServiceAnalyzer.cc IOMC/RandomEngine/test/TestRandomNumberServiceAnalyzer.cc - Description: Used in tests of the RandomNumberGeneratorService. + Description: Used in tests of the RandomNumberGeneratorService. Implementation: Generates some random numbers using the engines from the service. Prints them to an output file named testRandomNumberService.txt. @@ -14,28 +14,25 @@ service. Prints them to an output file named testRandomNumberService.txt. // Original Author: Chris Jones, David Dagenhart // Created: Tue Mar 7 11:57:09 EST 2006 // -// - -#include -#include -#include -#include -#include -#include +#include "DataFormats/Provenance/interface/EventAuxiliary.h" +#include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h" #include "FWCore/Framework/interface/EDAnalyzer.h" - +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/LuminosityBlock.h" #include "FWCore/Framework/interface/MakerMacros.h" - #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/RandomNumberGenerator.h" -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/LuminosityBlock.h" -#include "DataFormats/Provenance/interface/EventAuxiliary.h" -#include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h" -#include "CLHEP/Random/RandomEngine.h" #include "CLHEP/Random/RandExponential.h" +#include "CLHEP/Random/RandomEngine.h" + +#include +#include +#include +#include +#include +#include class TestRandomNumberServiceAnalyzer : public edm::EDAnalyzer { public: @@ -85,42 +82,38 @@ TestRandomNumberServiceAnalyzer::TestRandomNumberServiceAnalyzer(edm::ParameterS randomNumberLumi0_(0.0), randomNumberLumi1_(0.0), randomNumberLumi2_(0.0), - multiprocessReplay_(pset.getUntrackedParameter("multiprocessReplay", false)) -{ + multiprocessReplay_(pset.getUntrackedParameter("multiprocessReplay", false)) { edm::Service rng; - if (dump_) { + if(dump_) { rng->print(); std::cout << "*** TestRandomNumberServiceAnalyzer constructor " << rng->mySeed() << "\n"; } } -TestRandomNumberServiceAnalyzer::~TestRandomNumberServiceAnalyzer() -{ +TestRandomNumberServiceAnalyzer::~TestRandomNumberServiceAnalyzer() { } - void -TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) -{ +TestRandomNumberServiceAnalyzer::analyze(edm::Event const& iEvent, edm::EventSetup const&) { // Add some sleep for the different child processes in attempt // to ensure all the child processes get events to process. - if (multiprocess_) { + if(multiprocess_) { sleep(0.025 + childIndex_ * 0.025 + count_ * 0.3); } ++count_; edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer analyze " << rng->mySeed() << "\n"; } std::ofstream outFile; - outFile.open(outFileName_.c_str(), std::ofstream::out | std::ofstream::app); + outFile.open(outFileName_.c_str(), std::ofstream::out | std::ofstream::app); outFile << "*** TestRandomNumberServiceAnalyzer analyze() " << iEvent.eventAuxiliary().run() - << "/" << iEvent.eventAuxiliary().luminosityBlock() - << "/" << iEvent.eventAuxiliary().event() + << "/" << iEvent.eventAuxiliary().luminosityBlock() + << "/" << iEvent.eventAuxiliary().event() << "\n"; outFile << rng->mySeed() << "\n"; outFile << rng->getEngine().name() << "\n"; @@ -134,7 +127,7 @@ TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::Ev // care of those actions. CLHEP::HepRandomEngine& engine = rng->getEngine(); - // Generate random numbers distributed flatly between 0 and 1 + // Generate random numbers distributed flatly between 0 and 1 randomNumberEvent0_ = engine.flat(); randomNumberEvent1_ = engine.flat(); randomNumberEvent2_ = engine.flat(); @@ -155,16 +148,15 @@ TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::Ev outFile.close(); - if (multiprocess_ && count_ == 1U) { + if(multiprocess_ && count_ == 1U) { std::ostringstream ss; ss << "child" << childIndex_ << "FirstEvent.txt"; std::string filename = ss.str(); - if (firstInPath_) { + if(firstInPath_) { outFile.open(filename.c_str()); - } - else { - outFile.open(filename.c_str(), std::ofstream::app); + } else { + outFile.open(filename.c_str(), std::ofstream::app); } outFile << *currentContext()->moduleLabel() << "\n"; outFile << rng->mySeed() << "\n"; @@ -184,16 +176,15 @@ TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::Ev outFile.close(); } - if (multiprocess_ || multiprocessReplay_) { + if(multiprocess_ || multiprocessReplay_) { std::ostringstream ss; ss << "child" << childIndex_ << "LastEvent.txt"; std::string filename = ss.str(); - if (firstInPath_) { + if(firstInPath_) { outFile.open(filename.c_str()); - } - else { - outFile.open(filename.c_str(), std::ofstream::app); + } else { + outFile.open(filename.c_str(), std::ofstream::app); } outFile << *currentContext()->moduleLabel() << "\n"; outFile << rng->mySeed() << "\n"; @@ -213,17 +204,16 @@ TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::Ev outFile.close(); } - if (multiprocess_ || multiprocessReplay_) { + if(multiprocess_ || multiprocessReplay_) { std::ostringstream ss; ss << "testRandomServiceL" << iEvent.eventAuxiliary().luminosityBlock() - << "E" << iEvent.eventAuxiliary().event() << ".txt"; + << "E" << iEvent.eventAuxiliary().event() << ".txt"; std::string filename = ss.str(); - if (firstInPath_) { + if(firstInPath_) { outFile.open(filename.c_str()); - } - else { - outFile.open(filename.c_str(), std::ofstream::app); + } else { + outFile.open(filename.c_str(), std::ofstream::app); } outFile << *currentContext()->moduleLabel() << "\n"; outFile << rng->mySeed() << "\n"; @@ -246,7 +236,7 @@ TestRandomNumberServiceAnalyzer::analyze(const edm::Event& iEvent, const edm::Ev void TestRandomNumberServiceAnalyzer::beginJob() { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer beginJob " << rng->mySeed() << "\n"; std::cout << rng->getEngine().name() << "\n"; } @@ -254,28 +244,28 @@ void TestRandomNumberServiceAnalyzer::beginJob() { void TestRandomNumberServiceAnalyzer::endJob() { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer endJob " << rng->mySeed() << "\n"; } } -void TestRandomNumberServiceAnalyzer::beginRun(edm::Run const& run, edm::EventSetup const& es) { +void TestRandomNumberServiceAnalyzer::beginRun(edm::Run const&, edm::EventSetup const&) { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer beginRun " << rng->mySeed() << "\n"; } } -void TestRandomNumberServiceAnalyzer::endRun(edm::Run const& run, edm::EventSetup const& es) { +void TestRandomNumberServiceAnalyzer::endRun(edm::Run const&, edm::EventSetup const&) { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer endRun " << rng->mySeed() << "\n"; } } -void TestRandomNumberServiceAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const& es) { +void TestRandomNumberServiceAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer beginLuminosityBlock " << rng->mySeed() << "\n"; } @@ -284,12 +274,11 @@ void TestRandomNumberServiceAnalyzer::beginLuminosityBlock(edm::LuminosityBlock // purposes, we print out the generated random numbers and // some other things. std::ofstream outFile; - if (firstFileOpen_) { + if(firstFileOpen_) { outFile.open(outFileName_.c_str()); firstFileOpen_ = false; - } - else { - outFile.open(outFileName_.c_str(), std::ofstream::out | std::ofstream::app); + } else { + outFile.open(outFileName_.c_str(), std::ofstream::out | std::ofstream::app); } outFile << "*** TestRandomNumberServiceAnalyzer beginLumi " << lumi.luminosityBlockAuxiliary().run() @@ -299,7 +288,7 @@ void TestRandomNumberServiceAnalyzer::beginLuminosityBlock(edm::LuminosityBlock CLHEP::HepRandomEngine& engine = rng->getEngine(); - // Generate random numbers distributed flatly between 0 and 1 + // Generate random numbers distributed flatly between 0 and 1 randomNumberLumi0_ = engine.flat(); randomNumberLumi1_ = engine.flat(); randomNumberLumi2_ = engine.flat(); @@ -311,14 +300,14 @@ void TestRandomNumberServiceAnalyzer::beginLuminosityBlock(edm::LuminosityBlock outFile.close(); } -void TestRandomNumberServiceAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const& es) { +void TestRandomNumberServiceAnalyzer::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) { edm::Service rng; - if (dump_) { + if(dump_) { std::cout << "*** TestRandomNumberServiceAnalyzer endLuminosityBlock " << rng->mySeed() << "\n"; } } -void TestRandomNumberServiceAnalyzer::postForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) { +void TestRandomNumberServiceAnalyzer::postForkReacquireResources(unsigned int iChildIndex, unsigned int /*iNumberOfChildren*/) { multiprocess_ = true; childIndex_ = iChildIndex; std::ostringstream suffix;