SeedTestUtils.cxx
Go to the documentation of this file.
1 /**
2  * @file SeedTestUtils.cxx
3  * @brief Functions used in NuRandomService tests (implementation file)
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date March 17, 2016
6  * @see SeedTestUtils.h
7  */
8 
9 // library header
10 #include "SeedTestUtils.h"
11 
12 // art extensions
13 #include "nutools/RandomUtils/NuRandomService.h"
14 
15 // C++ includes.
16 #include <string>
17 #include <sstream>
18 #include <iomanip> // std::setw()
19 
20 // CLHEP libraries
21 #include "CLHEP/Random/RandomEngine.h" // CLHEP::HepRandomEngine
22 #include "CLHEP/Random/RandFlat.h"
23 
24 // Framework includes.
27 
28 namespace testing {
29  namespace NuRandomService {
30 
31  /// Returns whether the exception looks to be from NuRandomService
33 
34  // is it an art::Exception? (NuRandomService exceptions are)
35  art::Exception const* art_exc = dynamic_cast<art::Exception const*>(&e);
36  if (!art_exc) return false;
37 
38  // All configuration exceptions are from NuRandomService.
39  // Why? just because.
40  if (art_exc->categoryCode() == art::errors::Configuration)
41  return true;
42 
43  // does it have "NuRandomService" in the message? (NuRandomService exceptions should)
44  if ( (art_exc->explain_self().find("NuRandomService") == std::string::npos)
45  && (art_exc->explain_self().find("SeedMaster") == std::string::npos)
46  )
47  return false;
48 
49  // we can't do better than this
50  return true;
51  } // isSeedServiceException(exception)
52 
53 
54  /// Returns whether the exception looks to be from NuRandomService and has specified code
57  {
58  if (!isSeedServiceException(e)) return false;
59  // by now we know "e" is a art::Exception
60  return static_cast<art::Exception const&>(e).categoryCode() == code;
61  } // isSeedServiceException(exception, ErrorCodes)
62 
63 
64  /// Extracts "statistics" from a flat random number distribution
65  unsigned short RollStat(CLHEP::RandFlat& rand) {
66  // std::array<unsigned short, 4> rolls;
67  unsigned int min = 6, total = 0;
68  for (int i = 0; i < 4; ++i) {
69  unsigned int roll = 1 + rand.fireInt(0, 6);
70  // rolls[i] = roll;
71  if (min > roll) min = roll;
72  total += roll;
73  }
74  total -= min;
75  return total;
76  } // RollStat()
77 
78  /// Creates a "character statistics" using the specified random engine
79  std::string CreateCharacter(CLHEP::HepRandomEngine& engine) {
80  CLHEP::RandFlat flat(engine);
81  constexpr size_t NStats = 6;
82  static const std::array<std::string, NStats> statNames
83  = {{ "STR", "DEX", "CON", "INT", "WIS", "CHA" }};
84  std::array<unsigned short int, NStats> stats;
85  std::generate(stats.begin(), stats.end(), [&flat]{ return RollStat(flat); });
86 
87  short int bonus = 0;
88  std::ostringstream sstr;
89  for (size_t iStat = 0; iStat < NStats; ++iStat) {
90  sstr << " " << statNames[iStat] << "=" << std::setw(2) << stats[iStat];
91  bonus += stats[iStat] / 2 - 5;
92  } // for
93  sstr << " [bonus: " << std::setw(3) << std::showpos << bonus << "]";
94  return sstr.str();
95  } // CreateCharacter()
96 
97 
98  /// Returns the seed of the specified engine (CLHEP overload)
99  seed_t readSeed(CLHEP::HepRandomEngine const& engine)
100  { return engine.getSeed(); }
101 
102 
103  } // namespace NuRandomService
104 } // namespace testing
105 
LArSoft test utilities.
std::string string
Definition: nybbler.cc:12
art::detail::EngineCreator::seed_t seed_t
Type of seed.
Definition: SeedTestUtils.h:32
const double e
std::string CreateCharacter(CLHEP::HepRandomEngine &engine)
Creates a "character statistics" using the specified random engine.
Functions used in NuRandomService tests.
CodeOutputInterface * code
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
seed_t readSeed(CLHEP::HepRandomEngine const &engine)
Returns the seed of the specified engine (CLHEP overload)
bool isSeedServiceException(std::exception const &e)
Returns whether the exception looks to be from NuRandomService.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
unsigned short RollStat(CLHEP::RandFlat &rand)
Extracts "statistics" from a flat random number distribution.