Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JROOT/JRandom.hh
Go to the documentation of this file.
1#ifndef __JROOT__JRANDOM__
2#define __JROOT__JRANDOM__
3
4#include <string>
5#include <istream>
6#include <ostream>
7#include <fstream>
8#include <sstream>
9
10#include <TString.h>
11#include <TRandom.h>
12#include <TBufferJSON.h>
13
16
17/**
18 * \author mdejong
19 */
20
21namespace JROOT {}
22namespace JPP { using namespace JROOT; }
23
24namespace JROOT {
25
26 /**
27 * Auxiliary data structure to configure random number generator.
28 *
29 * The internal <tt>TString</tt> can hold any of the following data.
30 * - decimal value corresponding to seed;
31 * - name of ROOT formatted binary file; or
32 * - name of JSON formatted ASCII file.
33 */
34 struct JRandom :
35 public TString
36 {
37 /**
38 * Default constructor.
39 */
41 {}
42
43
44 /**
45 * Constructor.
46 *
47 * \param buffer buffer
48 */
49 JRandom(const TString& buffer)
50 {
51 static_cast<TString&>(*this) = buffer;
52 }
53
54
55 /**
56 * Constructor.
57 *
58 * \param seed seed
59 */
60 JRandom(const ULong_t seed)
61 {
62 static_cast<TString&>(*this) += seed;
63 }
64
65
66 /**
67 * Read random from input.
68 *
69 * \param in input stream
70 * \param object random
71 * \return input stream
72 */
73 friend inline std::istream& operator>>(std::istream& in, JRandom& object)
74 {
75 return in >> static_cast<TString&>(object);
76 }
77
78
79 /**
80 * Write random to output.
81 *
82 * \param out output stream
83 * \param object random
84 * \return output stream
85 */
86 friend inline std::ostream& operator<<(std::ostream& out, const JRandom& object)
87 {
88 return out << object.Data();
89 }
90
91
92 /**
93 * Configure random number generator.
94 *
95 * \param pr reference pointer to random number generator
96 * \return true if successful; else false
97 */
98 bool set(TRandom*& pr) const
99 {
100 using namespace std;
101 using namespace JPP;
102
103 if (pr != NULL) {
104
105 if (this->IsDec()) {
106
107 pr->SetSeed(this->Atoll());
108
109 return true;
110
111 } else {
112
113 return JRandom::read(static_cast<const TString&>(*this), pr);
114 }
115 }
116
117 return false;
118 }
119
120
121 /**
122 * Read random number generator from file.
123 *
124 * \param file_name file name
125 * \param pr reference pointer to random number generator
126 * \return true if successful; else false
127 */
128 static bool read(const TString& file_name, TRandom*& pr)
129 {
130 using namespace std;
131 using namespace JPP;
132
133 if (pr != NULL) {
134
135 if (file_name.EndsWith(".root")) {
136
137 JRootFileReader<TRandom> in(file_name.Data(), pr->GetName());
138
139 if (in.hasNext()) {
140
141 TRandom* p = in.next();
142
143 pr = (TRandom*) p->Clone();
144
145 return true;
146 }
147
148 } else if (file_name.EndsWith(".txt")) {
149
150 fstream in(file_name.Data());
151
152 stringstream io;
153
154 if (io << in.rdbuf()) {
155
156 pr = (TRandom*) TBufferJSON::ConvertFromJSON(io.str().c_str());
157
158 return true;
159 }
160 }
161 }
162
163 return false;
164 }
165
166
167 /**
168 * Write random number generator to file.
169 *
170 * \param file_name file name
171 * \param pr pointer to random number generator
172 * \return true if successful; else false
173 */
174 static bool write(const TString& file_name, TRandom* pr)
175 {
176 using namespace std;
177 using namespace JPP;
178
179 if (pr != NULL) {
180
181 if (file_name.EndsWith(".root")) {
182
183 JRootFileWriter<TRandom> out(file_name.Data());
184
185 out.put(*gRandom);
186
187 out.close();
188
189 return true;
190
191 } else if (file_name.EndsWith(".txt")) {
192
193 ofstream out(file_name.Data());
194
195 out << TBufferJSON::ConvertToJSON(gRandom).Data();
196
197 out.close();
198
199 return true;
200 }
201 }
202
203 return false;
204 }
205 };
206}
207
208#endif
virtual bool put(const T &object)=0
Object output.
ROOT file object output.
virtual void close() override
Close file.
Definition JRootFile.hh:247
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
Auxiliary data structure to configure random number generator.
JRandom(const ULong_t seed)
Constructor.
bool set(TRandom *&pr) const
Configure random number generator.
friend std::istream & operator>>(std::istream &in, JRandom &object)
Read random from input.
JRandom(const TString &buffer)
Constructor.
static bool read(const TString &file_name, TRandom *&pr)
Read random number generator from file.
JRandom()
Default constructor.
friend std::ostream & operator<<(std::ostream &out, const JRandom &object)
Write random to output.
static bool write(const TString &file_name, TRandom *pr)
Write random number generator to file.