Commit 5b3b5cc8 authored by Lynn Garren's avatar Lynn Garren
Browse files

add StdRandom class

parent 6363c3ce
2005-03-07 Lynn Garren <garren@fnal.gov>
* add StdRandom class which "saves" the random state as a string
==============================
29.11.04 Release CLHEP-1.9.1.2
==============================
......
......@@ -19,6 +19,7 @@ pkginclude_HEADERS = \
StdHep.hh \
StdHeptoGenEvent.icc \
StdHepVersion.h \
StdRandom.hh \
StdRunInfoConvert.hh \
StdRunInfoConvert.icc \
StdRunInfo.hh \
......
#ifndef STDRANDOM_HH
#define STDRANDOM_HH
// ----------------------------------------------------------------------
// StdRandom.hh
//
// Container for the random state
// ----------------------------------------------------------------------
#include <vector>
#include <string>
#include <iostream>
namespace StdHep {
/**
* @author
* @ingroup stdhep
*/
class StdRandom {
public:
// --- birth/death:
//
StdRandom( );
StdRandom( const StdRandom & orig );
StdRandom& operator=( const StdRandom & );
~StdRandom();
// --- accessors:
//
std::ostream& getStateAsStream( std::ostream& ) const;
std::vector<unsigned long> getStateAsLongs( ) const;
std::string getStateAsString() const { return myRandomState; }
void print( std::ostream & os = std::cout ) const;
// --- mutators:
//
void putState( std::istream& );
void putState( std::string& );
void putState( std::vector<unsigned long> );
void appendSeed( unsigned long sd );
void appendSeed( double sd );
void clear( );
private:
std::string myRandomState;
}; // StdRandom
}
#endif // STDRANDOM_HH
......@@ -8,6 +8,8 @@
#include <vector>
#include <string>
#include "CLHEP/StdHep/StdRandom.hh"
namespace StdHep {
class StdRunInfo {
......@@ -30,8 +32,9 @@ public:
int eventsWritten() const { return itsEventsWritten; }
double CoMEnergy() const { return itsCoMEnergy; }
double crossSection() const { return itsCrossSection; }
double seed( int i ) const { return itsSeedList[i]; }
unsigned int size() const { return itsSeedList.size(); }
std::vector<unsigned long> seedArray() const { return itsSeedList.getStateAsLongs(); }
std::string seedStream() const { return itsSeedList.getStateAsString(); }
std::ostream& seedString(std::ostream& os) const { return itsSeedList.getStateAsStream(os); }
std::string runIdentifier() const { return itsRunIdentifier; }
std::string generatorName() const { return itsGeneratorName; }
std::string pdfName() const { return itsPDFName; }
......@@ -44,8 +47,12 @@ public:
void setEventsWritten ( int nwrt ) { itsEventsWritten = nwrt; }
void setCoMEnergy ( double com ) { itsCoMEnergy = com; }
void setCrossSection ( double xsec ) { itsCrossSection = xsec; }
void appendSeed ( double sd ) { itsSeedList.push_back( sd ); }
void appendSeed ( unsigned long sd ) { itsSeedList.appendSeed( sd ); }
void appendSeed ( double sd ) { itsSeedList.appendSeed( sd ); }
void clearSeeds ( ) { itsSeedList.clear(); }
void saveSeeds ( std::istream seed ) { itsSeedList.putState(seed); }
void saveSeeds ( std::string& seed ) { itsSeedList.putState(seed); }
void saveSeeds ( std::vector<unsigned long> seed ) { itsSeedList.putState(seed); }
void setRunIdentifier ( std::string & name );
void setGeneratorName ( std::string & name );
void setPdfName ( std::string & name );
......@@ -61,7 +68,7 @@ private:
std::string itsRunIdentifier;
std::string itsGeneratorName;
std::string itsPDFName;
std::vector<double> itsSeedList;
StdRandom itsSeedList;
}; // StdRunInfo
......
// $Id: StdRunInfoConvert.icc,v 1.1.1.1 2003/07/15 20:15:05 garren Exp $
// $Id: StdRunInfoConvert.icc,v 1.1.1.1.2.1 2005/03/07 18:30:27 garren Exp $
// ----------------------------------------------------------------------
//
// StdRunInfoConvert.icc
// L. Garren
//
// defines free functions to build StdRunInfo from HEPEVT, and vice-versa
// random seeds are saved as a pair of doubles in the stdcm1 common block
//
// ----------------------------------------------------------------------
#include "CLHEP/StdHep/CBcm.hh"
#include "CLHEP/HepMC/CBInterface.h"
#include "CLHEP/HepMC/DoubleConversion.hh"
namespace StdHep {
......@@ -23,8 +25,12 @@ StdRunInfo * getRunInfoFromSTDCM( int irun )
(*cm)->stdcm1()->nevtwrt,
(double)(*cm)->stdcm1()->stdecom,
(double)(*cm)->stdcm1()->stdxsec );
run->appendSeed( (*cm)->stdcm1()->stdseed1 );
run->appendSeed( (*cm)->stdcm1()->stdseed2 );
// convert doubles to longs and save
std::vector<unsigned long> state(4);
state[0] = DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed1);
state[2] = DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed2);
run->saveSeeds( state );
// save names
std::string gname = (*cm)->stdcm2()->generatorname;
std::string pname = (*cm)->stdcm2()->pdfname;
run->setGeneratorName( gname );
......@@ -40,8 +46,17 @@ bool RunInfoToSTDCM( StdRunInfo * run )
(*cm)->stdcm1()->nevtwrt = run->eventsWritten();
(*cm)->stdcm1()->stdecom = (float)run->CoMEnergy();
(*cm)->stdcm1()->stdxsec = (float)run->crossSection();
if( run->size() > 0 ) (*cm)->stdcm1()->stdseed1 = run->seed(0);
if( run->size() > 1 ) (*cm)->stdcm1()->stdseed2 = run->seed(1);
// convert longs to double
std::vector<unsigned long> state = run->seedArray();
std::vector<double> result;
for( unsigned int i=0; i<(itsSeedList.size()-1); ++i ) {
dd = DoubleConversion::longs2double(state[i]);
++i;
result.push_back(dd);
}
if( run->size() > 1 ) (*cm)->stdcm1()->stdseed1 = result[0];
if( run->size() > 3 ) (*cm)->stdcm1()->stdseed2 = result[1];
// check run number
if( run->runNumber() == 0 ) return false;
return true;
}
......
......@@ -31,6 +31,7 @@ libCLHEP_StdHep_@VERSION@_a_SOURCES = \
StdEventVDescendants.cc \
StdEventVertex.cc \
StdEventVParents.cc \
StdRandom.cc \
StdRunInfo.cc \
tree.cc \
writeEventKey.cc \
......
// ----------------------------------------------------------------------
// StdRandom.cc
// ----------------------------------------------------------------------
#include <vector>
#include <string>
#include <sstream>
#include "CLHEP/StdHep/StdRandom.hh"
#include "CLHEP/HepMC/DoubleConversion.hh"
namespace StdHep {
StdRandom::StdRandom( )
: myRandomState( "" )
{ ; }
StdRandom::StdRandom( const StdRandom & orig )
: myRandomState( orig.myRandomState )
{ ; }
StdRandom& StdRandom::operator=( const StdRandom & rhs )
{ if( this != & rhs )
{
myRandomState = rhs.myRandomState;
}
return *this;
}
std::ostream& StdRandom::getStateAsStream( std::ostream & os ) const
{
os << myRandomState << std::endl;
return os;
}
std::vector<unsigned long> StdRandom::getStateAsLongs( ) const
{
// we assume here that this makes sense
// this method assumes that the state consists solely of a list of longs
std::vector<unsigned long> result;
std::istringstream st(myRandomState);
int i;
while(st) {
st >> i;
result.push_back(i);
}
return result;
}
void StdRandom::print( std::ostream & os ) const
{
os << "begin StdRandom state" << std::endl;
os << myRandomState << std::endl;
os << "end StdRandom state" << std::endl;
}
void StdRandom::putState( std::istream& ist )
{
std::ostringstream st;
std::string str;
while(ist) {
ist >> str;
st << str;
}
myRandomState = st.str();
}
void StdRandom::putState( std::string& st )
{
myRandomState = st;
}
void StdRandom::putState( std::vector<unsigned long> rs )
{
std::ostringstream st;
for( unsigned int i=0; i<rs.size(); ++i ) {
st << " " << rs[i];
}
myRandomState = st.str();
}
void StdRandom::appendSeed( unsigned long sd )
{
std::ostringstream st(myRandomState);
st << " " << sd;
myRandomState = st.str();
}
void StdRandom::appendSeed( double sd )
{
std::ostringstream st(myRandomState);
std::vector<unsigned long> two;
two = HepMC::DoubleConversion::dto2longs(sd);
if (two.size() > 1 ) {
st << " " << two[0] << " " << two[1];
}
myRandomState = st.str();
}
void StdRandom::clear( )
{
myRandomState = "";
}
} // StdHep
......@@ -19,7 +19,8 @@ StdRunInfo::StdRunInfo( int runno, int togen, int ngen, int nwrt,
itsCrossSection ( xsec ),
itsRunIdentifier ( " " ),
itsGeneratorName ( " " ),
itsPDFName ( " " )
itsPDFName ( " " ),
itsSeedList ( StdRandom() )
{ ; }
StdRunInfo::StdRunInfo( const StdRunInfo & orig )
......
// $Id: WriteStdHep.cc,v 1.2 2003/08/13 20:00:13 garren Exp $
// $Id: WriteStdHep.cc,v 1.2.2.1 2005/03/07 18:30:27 garren Exp $
// ----------------------------------------------------------------------
//
// WriteStdHep.cc
......@@ -38,7 +38,7 @@ std::ostream & writeStdEvent( std::ostream & os, StdEvent const * evt )
//
// output the GenEvent data including the number of primary vertices
// and the total number of vertices
std::vector<double> random = col->random_states();
std::vector<unsigned long> random = col->random_states();
os << "E" << " " << col->event_number();
HepMC::Detail::output( os, col->event_scale() );
HepMC::Detail::output( os, col->alphaQCD() );
......@@ -48,7 +48,7 @@ std::ostream & writeStdEvent( std::ostream & os, StdEvent const * evt )
col->signal_process_vertex()->barcode() : 0);
os << " " << col->vertices_size() // total number of vertices.
<< " " << (int)random.size();
for ( std::vector<double>::iterator rs = random.begin();
for ( std::vector<unsigned long>::iterator rs = random.begin();
rs != random.end(); ++rs ) {
os << " " << *rs;
}
......
// $Id: writeRunInfo.cc,v 1.2 2003/08/13 20:00:13 garren Exp $
// $Id: writeRunInfo.cc,v 1.2.2.1 2005/03/07 18:30:27 garren Exp $
// ----------------------------------------------------------------------
//
// writeRunInfo.cc
......@@ -26,7 +26,6 @@ std::ostream & writeRunInfo( std::ostream & os, StdRunInfo const * run )
os.setf(HepIOS::dec,HepIOS::basefield); // integer
os.setf(HepIOS::scientific,HepIOS::floatfield); // floating
// output StdRunInfo data
int nseed = (int)run->size();
os << "BlockType StdRunInfo" << std::endl;
os << "RunID " << run->runIdentifier() << std::endl;
os << "R " << run->runNumber() << " "
......@@ -34,14 +33,9 @@ std::ostream & writeRunInfo( std::ostream & os, StdRunInfo const * run )
<< run->eventsGenerated() << " "
<< run->eventsWritten() << " "
<< run->CoMEnergy() << " "
<< run->crossSection() << " "
<< nseed << std::endl;
<< run->crossSection() << std::endl;
// write seeds on a separate line
os << "SeedList " ;
for( int i=0; i<nseed; ++i ) {
os << " " << run->seed( i );
}
os << std::endl;
os << "SeedList " << run->seedStream() << std::endl;
// write strings
os << "RunInfo " << run->generatorName()
<< " " << run->pdfName() << std::endl;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment