CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RandSkewNormal.cc
Go to the documentation of this file.
1//
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.h"
18
19#include <cmath>
20#include <iostream>
21#include <string>
22#include <vector>
23
24namespace CLHEP {
25
26std::string RandSkewNormal::name() const {return "RandSkewNormal";}
27HepRandomEngine & RandSkewNormal::engine() {return *localEngine;}
28
30
32{
33 return fire( shapeParameter );
34}
35
36double RandSkewNormal::operator()( double shape )
37{
38 return fire( shape );
39}
40
41//-------------
42
44{
45 // really dumb use of RandSkewNormal
46 double k = 1;
47 return gaussianSkewNormal( anEngine, k );
48}
49
50double RandSkewNormal::shoot(HepRandomEngine* anEngine, double shape)
51{
52 return gaussianSkewNormal( anEngine, shape );
53}
54
56{
57 // really dumb use of RandSkewNormal
59 double k = 1;
60 return gaussianSkewNormal( anEngine, k );
61}
62
63double RandSkewNormal::shoot(double shape)
64{
66 return gaussianSkewNormal( anEngine, shape );
67}
68
69void RandSkewNormal::shootArray( const int size, double* vect,
70 double shape )
71{
72 for( double* v = vect; v != vect+size; ++v )
73 *v = shoot(shape);
74}
75
76void RandSkewNormal::shootArray(HepRandomEngine* anEngine, const int size,
77 double* vect, double shape )
78{
79 for( double* v = vect; v != vect+size; ++v )
80 *v = shoot(anEngine, shape);
81}
82
83//-------------
84
87}
88
89double RandSkewNormal::fire(double shape) {
90 return gaussianSkewNormal( getLocalEngine(), shape );
91}
92
93void RandSkewNormal::fireArray( const int size, double* vect)
94{
95 for( double* v = vect; v != vect+size; ++v )
96 *v = fire( shapeParameter );
97}
98
99void RandSkewNormal::fireArray( const int size, double* vect,
100 double shape )
101{
102 for( double* v = vect; v != vect+size; ++v )
103 *v = fire( shape );
104}
105
106//-------------
107
109{
110 // RandSkewNormal is an implementation of Azzalini's SN generator
111 // http://azzalini.stat.unipd.it/SN/
112 // K. McFarlane, June 2010.
113 // For location parameter m = 0., scale = 1.
114 // To get a distribution with scale parameter b and location m:
115 // r = m + b * RandSkewNormal.fire(k);
116 double u[2] = {0.};
117 RandGaussT::shootArray(e, 2, u, 0, 1);
118 double delta = k/std::sqrt(1. + k*k);
119 double u1 = delta*u[0] + std::sqrt(1 - delta*delta)*u[1];
120 double r = u[0] >= 0 ? u1 : -u1;
121 return r;
122}
123
124//-------------
125
126std::ostream & RandSkewNormal::put ( std::ostream & os ) const {
127 long pr=os.precision(20);
128 std::vector<unsigned long> t(2);
129 os << " " << name() << "\n";
130 os << "Uvec" << "\n";
131 t = DoubConv::dto2longs(shapeParameter);
132 os << shapeParameter << " " << t[0] << " " << t[1] << "\n";
133 os.precision(pr);
134 return os;
135}
136
137std::istream & RandSkewNormal::get ( std::istream & is ) {
138 std::string inName;
139 is >> inName;
140 if (inName != name()) {
141 is.clear(std::ios::badbit | is.rdstate());
142 std::cerr << "Mismatch when expecting to read state of a "
143 << name() << " distribution\n"
144 << "Name found was " << inName
145 << "\nistream is left in the badbit state\n";
146 return is;
147 }
148 if (possibleKeywordInput(is, "Uvec", shapeParameter)) {
149 std::vector<unsigned long> t(2);
150 is >> shapeParameter >> t[0] >> t[1]; shapeParameter = DoubConv::longs2double(t);
151 return is;
152 }
153 // is >> shapeParameter encompassed by possibleKeywordInput
154 return is;
155}
156
157
158} // namespace CLHEP
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
static HepRandomEngine * getTheEngine()
Definition: Random.cc:270
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)
Definition: RandomEngine.h:168