Go to the documentation of this file.00001 #include "IOMC/RandomEngine/src/TRandomAdaptor.h"
00002 #include "FWCore/Utilities/interface/EDMException.h"
00003
00004 #include "CLHEP/Random/engineIDulong.h"
00005
00006 #include "TBufferFile.h"
00007
00008 #include <cstddef>
00009 #include <iomanip>
00010 #include <iostream>
00011 #include <sstream>
00012 #include <stdint.h>
00013 #include <string>
00014
00015 namespace edm {
00016
00017 TRandomAdaptor::TRandomAdaptor(std::istream&) {
00018 Grumble(std::string("Cannot instantiate a TRandom engine from an istream"));
00019 }
00020
00021 TRandomAdaptor::~TRandomAdaptor() {
00022 }
00023
00024 std::ostream& TRandomAdaptor::put(std::ostream& os) const {
00025 Grumble(std::string("put(std::ostream) not available for TRandom engines"));
00026 return os;
00027 }
00028
00029 std::vector<unsigned long> TRandomAdaptor::put() const {
00030 std::vector<unsigned long> v;
00031
00032 int32_t itemSize = sizeof(uint32_t);
00033 TBufferFile buffer(TBuffer::kWrite, 2048 * itemSize);
00034 trand_->Streamer(buffer);
00035 buffer.SetReadMode();
00036 char* bufferPtr = buffer.Buffer();
00037 int32_t numItems = (buffer.Length() + itemSize - 1) / itemSize;
00038 v.reserve(numItems + 1);
00039 v.push_back(CLHEP::engineIDulong<TRandomAdaptor>());
00040 for(int i = 0; i < numItems; ++i) {
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 uint32_t value32 = *reinterpret_cast<uint32_t*>(bufferPtr + i * itemSize);
00051
00052 if(i == numItems - 1) {
00053 int nBytes = buffer.Length() % itemSize;
00054 if(nBytes == 1) value32 &= 0xffu;
00055 else if(nBytes == 2) value32 &= 0xffffu;
00056 else if(nBytes == 3) value32 &= 0xffffffu;
00057 }
00058
00059
00060 v.push_back(static_cast<unsigned long>(value32));
00061 }
00062 return v;
00063 }
00064
00065 std::istream& TRandomAdaptor::get(std::istream& is) {
00066 Grumble(std::string("get(std::istream) not available for TRandom engines"));
00067 return getState(is);
00068 }
00069
00070 std::istream& TRandomAdaptor::getState(std::istream& is) {
00071 Grumble(std::string("getState(std::istream) not available for TRandom engines"));
00072 return is;
00073 }
00074
00075 bool TRandomAdaptor::get(std::vector<unsigned long> const& v) {
00076 if(v.empty()) return false;
00077 if(v[0] != CLHEP::engineIDulong<TRandomAdaptor>()) return false;
00078 int32_t numItems = v.size()-1;
00079
00080 int32_t itemSize = sizeof(uint32_t);
00081 TBufferFile buffer(TBuffer::kRead, numItems * itemSize + 1024);
00082 char* bufferPtr = buffer.Buffer();
00083 for(int32_t i = 0; i < numItems; ++i) {
00084
00085 *reinterpret_cast<uint32_t*>(bufferPtr + i * itemSize) = static_cast<uint32_t>(v[i+1] & 0xffffffff);
00086 }
00087
00088
00089
00090
00091
00092
00093 trand_->Streamer(buffer);
00094
00095 return true;
00096 }
00097
00098 void TRandomAdaptor::Grumble(std::string const& errortext) const {
00099
00100
00101 std::ostringstream sstr;
00102 sstr << "Unimplemented Feature: " << errortext << '\n';
00103 edm::Exception except(edm::errors::UnimplementedFeature, sstr.str());
00104 throw except;
00105 }
00106
00107 }