a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
UsefulStuff.hpp
Go to the documentation of this file.
1/*
2 * UsefulStuff.hpp
3 *
4 * Created on: Jan 6, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 *
7 * This file is part of BOLlib, released under the
8 * GNU General Public License. Please see the accompanying
9 * README.BOLlib.txt file for a full list of files, brief documentation
10 * on how to use these classes, and further details on the license.
11 */
12
13#ifndef USEFULSTUFF_HPP_
14#define USEFULSTUFF_HPP_
15
16#include <cstdlib>
17#include <ctime>
18#include <stdexcept>
19#include <string>
20#include <cmath>
21#include <fstream>
22
23namespace BOL
24{
26 {
27 public:
28 static double const notANumber;
29 static std::string const nanString;
30 static double const twicePi;
31
32 static bool
33 doublesCloseEnough( double const firstDouble,
34 double const secondDouble,
35 double maximumDifference );
36 // this returns true if the difference between firstDouble & secondDouble
37 // is less than maximumDifference.
38 static double
39 flatRandomDouble( double const lowerLimit,
40 double const upperLimit );
41 /* this returns a double from a flat probability distribution from the
42 * *inclusive* lower limit of lowerLimit to the *exclusive* upper limit
43 * of upperLimit.
44 */
45 static int
46 zeroOrOne();
47 // this returns 0 or 1 with a 50:50 chance for each.
48 static int
50 // this returns -1 or +1 with a 50:50 chance for each.
51 static bool
52 fileExists( std::string const& fileName );
53 // returns true if a file with the name fileName exists, false otherwise.
54 static void
55 runSystemCommand( std::string const& systemCommand );
56 // this runs system( systemCommand.c_str() ) & throws an exception if it
57 // returned -1.
58
59 private:
61
62 static void
64 };
65
66
67
68 inline bool
69 UsefulStuff::doublesCloseEnough( double const firstDouble,
70 double const secondDouble,
71 double maximumDifference )
72 // this returns true if the difference between firstDouble & secondDouble
73 // is less than maximumDifference.
74 {
75 return ( abs( maximumDifference ) >= abs( firstDouble - secondDouble ) );
76 }
77
78 inline double
79 UsefulStuff::flatRandomDouble( double const lowerLimit,
80 double const upperLimit )
81 /* this returns a double from a flat probability distribution from the
82 * *inclusive* lower limit of lower_limit to the *exclusive* upper limit
83 * of upper_limit.
84 */
85 {
87 return ( lowerLimit + ( (double)(rand()) / (double)RAND_MAX )
88 * ( upperLimit - lowerLimit ) );
89 }
90
91 inline int
93 // this returns 0 or 1 with a 50:50 chance for each.
94 {
96 return ( (rand()) % 2 );
97 }
98
99 inline int
101 // this returns -1 or +1 with a 50:50 chance for each.
102 {
103 if( 0 == zeroOrOne() )
104 {
105 return -1;
106 }
107 else
108 {
109 return 1;
110 }
111 }
112
113 inline void
115 // this sets the random seed if it had not already been set.
116 {
118 {
119 // debugging:
123 srand( time( NULL ) );
124 randomSeedNotYetSet = false;
125 }
126 }
127
128 inline bool
129 UsefulStuff::fileExists( std::string const& fileName )
130 // returns true if a file with the name fileName exists, false otherwise.
131 {
132 std::ifstream fileStream( fileName.c_str() );
133 if( fileStream.good() )
134 {
135 fileStream.close();
136 return true;
137 }
138 else
139 {
140 return false;
141 }
142 }
143
144 inline void
145 UsefulStuff::runSystemCommand( std::string const& systemCommand )
146 {
147 int systemReturn( system( systemCommand.c_str() ) );
148 if( -1 == systemReturn )
149 {
150 throw std::runtime_error( "system( \"" + systemCommand
151 + "\" ) returned -1" );
152 }
153 }
154
155}
156
157#endif /* USEFULSTUFF_HPP_ */
static void ensureRandomSeedIsSet()
static bool doublesCloseEnough(double const firstDouble, double const secondDouble, double maximumDifference)
Definition: UsefulStuff.hpp:69
static double const twicePi
Definition: UsefulStuff.hpp:30
static int plusOrMinusOne()
static bool fileExists(std::string const &fileName)
static bool randomSeedNotYetSet
Definition: UsefulStuff.hpp:60
static int zeroOrOne()
Definition: UsefulStuff.hpp:92
static void runSystemCommand(std::string const &systemCommand)
static double flatRandomDouble(double const lowerLimit, double const upperLimit)
Definition: UsefulStuff.hpp:79
static double const notANumber
Definition: UsefulStuff.hpp:28
static std::string const nanString
Definition: UsefulStuff.hpp:29