CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

RandSkewNormal.cc
Go to the documentation of this file.
1// $Id: RandSkewNormal.cc,v 1.1 2011/05/27 20:36:57 garren Exp $
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- RandSkewNormal ---
7// class implementation file
8// -----------------------------------------------------------------------
9
10// =======================================================================
11// M Fischler and L Garren - Created: 26 May 2011
12// =======================================================================
13
14#include "CLHEP/Random/defs.h"
15#include "CLHEP/Random/RandSkewNormal.h"
16#include "CLHEP/Random/RandGaussT.h"
17#include "CLHEP/Random/DoubConv.hh"
18
19namespace CLHEP {
20
21std::string RandSkewNormal::name() const {return "RandSkewNormal";}
22HepRandomEngine & RandSkewNormal::engine() {return *localEngine;}
23
25
27{
28 return fire( shapeParameter );
29}
30
31double RandSkewNormal::operator()( double shape )
32{
33 return fire( shape );
34}
35
36//-------------
37
39{
40 // really dumb use of RandSkewNormal
41 double k = 1;
42 return gaussianSkewNormal( anEngine, k );
43}
44
45double RandSkewNormal::shoot(HepRandomEngine* anEngine, double shape)
46{
47 return gaussianSkewNormal( anEngine, shape );
48}
49
51{
52 // really dumb use of RandSkewNormal
54 double k = 1;
55 return gaussianSkewNormal( anEngine, k );
56}
57
58double RandSkewNormal::shoot(double shape)
59{
61 return gaussianSkewNormal( anEngine, shape );
62}
63
64void RandSkewNormal::shootArray( const int size, double* vect,
65 double shape )
66{
67 for( double* v = vect; v != vect+size; ++v )
68 *v = shoot(shape);
69}
70
71void RandSkewNormal::shootArray(HepRandomEngine* anEngine, const int size,
72 double* vect, double shape )
73{
74 for( double* v = vect; v != vect+size; ++v )
75 *v = shoot(anEngine, shape);
76}
77
78//-------------
79
82}
83
84double RandSkewNormal::fire(double shape) {
85 return gaussianSkewNormal( getLocalEngine(), shape );
86}
87
88void RandSkewNormal::fireArray( const int size, double* vect)
89{
90 for( double* v = vect; v != vect+size; ++v )
91 *v = fire( shapeParameter );
92}
93
94void RandSkewNormal::fireArray( const int size, double* vect,
95 double shape )
96{
97 for( double* v = vect; v != vect+size; ++v )
98 *v = fire( shape );
99}
100
101//-------------
102
104{
105 // RandSkewNormal is an implementation of Azzalini's SN generator
106 // http://azzalini.stat.unipd.it/SN/
107 // K. McFarlane, June 2010.
108 // For location parameter m = 0., scale = 1.
109 // To get a distribution with scale parameter b and location m:
110 // r = m + b * RandSkewNormal.fire(k);
111 double u[2] = {0.};
112 RandGaussT::shootArray(e, 2, u, 0, 1);
113 double delta = k/std::sqrt(1. + k*k);
114 double u1 = delta*u[0] + std::sqrt(1 - delta*delta)*u[1];
115 double r = u[0] >= 0 ? u1 : -u1;
116 return r;
117}
118
119//-------------
120
121std::ostream & RandSkewNormal::put ( std::ostream & os ) const {
122 int pr=os.precision(20);
123 std::vector<unsigned long> t(2);
124 os << " " << name() << "\n";
125 os << "Uvec" << "\n";
126 t = DoubConv::dto2longs(shapeParameter);
127 os << shapeParameter << " " << t[0] << " " << t[1] << "\n";
128 os.precision(pr);
129 return os;
130}
131
132std::istream & RandSkewNormal::get ( std::istream & is ) {
133 std::string inName;
134 is >> inName;
135 if (inName != name()) {
136 is.clear(std::ios::badbit | is.rdstate());
137 std::cerr << "Mismatch when expecting to read state of a "
138 << name() << " distribution\n"
139 << "Name found was " << inName
140 << "\nistream is left in the badbit state\n";
141 return is;
142 }
143 if (possibleKeywordInput(is, "Uvec", shapeParameter)) {
144 std::vector<unsigned long> t(2);
145 is >> shapeParameter >> t[0] >> t[1]; shapeParameter = DoubConv::longs2double(t);
146 return is;
147 }
148 // is >> shapeParameter encompassed by possibleKeywordInput
149 return is;
150}
151
152
153} // namespace CLHEP
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:106
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:90
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
static void shootArray(const int size, double *vect, double mean=0.0, double stdDev=1.0)
Definition: RandGaussT.cc:37
static double gaussianSkewNormal(HepRandomEngine *e, double k)
static double shoot()
static void shootArray(const int size, double *vect, double shape=0.)
void fireArray(const int size, double *vect)
std::string name() const
std::istream & get(std::istream &is)
HepRandomEngine * getLocalEngine()
std::ostream & put(std::ostream &os) const
HepRandomEngine & engine()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)