// This class generates the quasi-random (aka "low discrepancy")
// sequence for dimensions up to 12 using the Niederreiter base 2
// algorithm described in Bratley, Fox, Niederreiter, ACM Trans.
// Model. Comp. Sim. 2, 195 (1992). This implementation was adapted
// from the 0.9 beta release of the GNU scientific library.
// Quasi-random number sequences are useful for improving the
// convergence of a Monte Carlo integration.
// END_HTML
#include "RooFit.h"
#include "RooQuasiRandomGenerator.h"
#include "RooQuasiRandomGenerator.h"
#include "RooMsgService.h"
#include "TMath.h"
#include "Riostream.h"
#include <assert.h>
ClassImp(RooQuasiRandomGenerator)
;
RooQuasiRandomGenerator::RooQuasiRandomGenerator()
{
if(!_coefsCalculated) {
calculateCoefs(MaxDimension);
_coefsCalculated= kTRUE;
}
_nextq= new Int_t[MaxDimension];
reset();
}
RooQuasiRandomGenerator::~RooQuasiRandomGenerator()
{
delete[] _nextq;
}
void RooQuasiRandomGenerator::reset()
{
_sequenceCount= 0;
for(Int_t dim= 0; dim < MaxDimension; dim++) _nextq[dim]= 0;
}
Bool_t RooQuasiRandomGenerator::generate(UInt_t dimension, Double_t vector[])
{
static const Double_t recip = 1.0/(double)(1U << NBits);
UInt_t dim;
for(dim=0; dim < dimension; dim++) {
vector[dim] = _nextq[dim] * recip;
}
Int_t r(0),c(_sequenceCount);
while(1) {
if((c % 2) == 1) {
++r;
c /= 2;
}
else break;
}
if(r >= NBits) {
oocoutE((TObject*)0,Integration) << "RooQuasiRandomGenerator::generate: internal error!" << endl;
return kFALSE;
}
for(dim=0; dim<dimension; dim++) {
_nextq[dim] ^= _cj[r][dim];
}
_sequenceCount++;
return kTRUE;
}
void RooQuasiRandomGenerator::calculateCoefs(UInt_t dimension)
{
int ci[NBits][NBits];
int v[NBits+MaxDegree+1];
int r;
unsigned int i_dim;
for(i_dim=0; i_dim<dimension; i_dim++) {
const int poly_index = i_dim + 1;
int j, k;
int u = 0;
int pb[MaxDegree+1];
int px[MaxDegree+1];
int px_degree = _polyDegree[poly_index];
int pb_degree = 0;
for(k=0; k<=px_degree; k++) {
px[k] = _primitivePoly[poly_index][k];
pb[k] = 0;
}
pb[0] = 1;
for(j=0; j<NBits; j++) {
if(u == 0) calculateV(px, px_degree, pb, &pb_degree, v, NBits+MaxDegree);
for(r=0; r<NBits; r++) {
ci[r][j] = v[r+u];
}
++u;
if(u == px_degree) u = 0;
}
for(r=0; r<NBits; r++) {
int term = 0;
for(j=0; j<NBits; j++) {
term = 2*term + ci[r][j];
}
_cj[r][i_dim] = term;
}
}
}
void RooQuasiRandomGenerator::calculateV(const int px[], int px_degree,
int pb[], int * pb_degree, int v[], int maxv)
{
const int nonzero_element = 1;
const int arbitrary_element = 1;
int ph[MaxDegree+1];
int bigm = *pb_degree;
int m;
int r, k, kj;
for(k=0; k<=MaxDegree; k++) {
ph[k] = pb[k];
}
polyMultiply(px, px_degree, pb, *pb_degree, pb, pb_degree);
m = *pb_degree;
kj = bigm;
for(r=0; r<kj; r++) {
v[r] = 0;
}
v[kj] = 1;
if(kj >= bigm) {
for(r=kj+1; r<m; r++) {
v[r] = arbitrary_element;
}
}
else {
int term = sub(0, ph[kj]);
for(r=kj+1; r<bigm; r++) {
v[r] = arbitrary_element;
term = sub(term, mul(ph[r], v[r]));
}
v[bigm] = add(nonzero_element, term);
for(r=bigm+1; r<m; r++) {
v[r] = arbitrary_element;
}
}
for(r=0; r<=maxv-m; r++) {
int term = 0;
for(k=0; k<m; k++) {
term = sub(term, mul(pb[k], v[r+k]));
}
v[r+m] = term;
}
}
void RooQuasiRandomGenerator::polyMultiply(const int pa[], int pa_degree, const int pb[],
int pb_degree, int pc[], int * pc_degree)
{
int j, k;
int pt[MaxDegree+1];
int pt_degree = pa_degree + pb_degree;
for(k=0; k<=pt_degree; k++) {
int term = 0;
for(j=0; j<=k; j++) {
const int conv_term = mul(pa[k-j], pb[j]);
term = add(term, conv_term);
}
pt[k] = term;
}
for(k=0; k<=pt_degree; k++) {
pc[k] = pt[k];
}
for(k=pt_degree+1; k<=MaxDegree; k++) {
pc[k] = 0;
}
*pc_degree = pt_degree;
}
Int_t RooQuasiRandomGenerator::_cj[RooQuasiRandomGenerator::NBits]
[RooQuasiRandomGenerator::MaxDimension];
const Int_t RooQuasiRandomGenerator::_primitivePoly[RooQuasiRandomGenerator::MaxDimension+1]
[RooQuasiRandomGenerator::MaxPrimitiveDegree+1] =
{
{ 1, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0 },
{ 1, 1, 0, 1, 0, 0 },
{ 1, 0, 1, 1, 0, 0 },
{ 1, 1, 0, 0, 1, 0 },
{ 1, 0, 0, 1, 1, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 1, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 1 },
{ 1, 1, 1, 0, 1, 1 }
};
const Int_t RooQuasiRandomGenerator::_polyDegree[RooQuasiRandomGenerator::MaxDimension+1] =
{
0, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5
};
Bool_t RooQuasiRandomGenerator::_coefsCalculated= kFALSE;
RooQuasiRandomGenerator.cxx:1 RooQuasiRandomGenerator.cxx:2 RooQuasiRandomGenerator.cxx:3 RooQuasiRandomGenerator.cxx:4 RooQuasiRandomGenerator.cxx:5 RooQuasiRandomGenerator.cxx:6 RooQuasiRandomGenerator.cxx:7 RooQuasiRandomGenerator.cxx:8 RooQuasiRandomGenerator.cxx:9 RooQuasiRandomGenerator.cxx:10 RooQuasiRandomGenerator.cxx:11 RooQuasiRandomGenerator.cxx:12 RooQuasiRandomGenerator.cxx:13 RooQuasiRandomGenerator.cxx:14 RooQuasiRandomGenerator.cxx:15 RooQuasiRandomGenerator.cxx:16 RooQuasiRandomGenerator.cxx:17 RooQuasiRandomGenerator.cxx:18 RooQuasiRandomGenerator.cxx:19 RooQuasiRandomGenerator.cxx:20 RooQuasiRandomGenerator.cxx:21 RooQuasiRandomGenerator.cxx:22 RooQuasiRandomGenerator.cxx:23 RooQuasiRandomGenerator.cxx:24 RooQuasiRandomGenerator.cxx:25 RooQuasiRandomGenerator.cxx:26 RooQuasiRandomGenerator.cxx:27 RooQuasiRandomGenerator.cxx:28 RooQuasiRandomGenerator.cxx:29 RooQuasiRandomGenerator.cxx:30 RooQuasiRandomGenerator.cxx:31 RooQuasiRandomGenerator.cxx:32 RooQuasiRandomGenerator.cxx:33 RooQuasiRandomGenerator.cxx:34 RooQuasiRandomGenerator.cxx:35 RooQuasiRandomGenerator.cxx:36 RooQuasiRandomGenerator.cxx:37 RooQuasiRandomGenerator.cxx:38 RooQuasiRandomGenerator.cxx:39 RooQuasiRandomGenerator.cxx:40 RooQuasiRandomGenerator.cxx:41 RooQuasiRandomGenerator.cxx:42 RooQuasiRandomGenerator.cxx:43 RooQuasiRandomGenerator.cxx:44 RooQuasiRandomGenerator.cxx:45 RooQuasiRandomGenerator.cxx:46 RooQuasiRandomGenerator.cxx:47 RooQuasiRandomGenerator.cxx:48 RooQuasiRandomGenerator.cxx:49 RooQuasiRandomGenerator.cxx:50 RooQuasiRandomGenerator.cxx:51 RooQuasiRandomGenerator.cxx:52 RooQuasiRandomGenerator.cxx:53 RooQuasiRandomGenerator.cxx:54 RooQuasiRandomGenerator.cxx:55 RooQuasiRandomGenerator.cxx:56 RooQuasiRandomGenerator.cxx:57 RooQuasiRandomGenerator.cxx:58 RooQuasiRandomGenerator.cxx:59 RooQuasiRandomGenerator.cxx:60 RooQuasiRandomGenerator.cxx:61 RooQuasiRandomGenerator.cxx:62 RooQuasiRandomGenerator.cxx:63 RooQuasiRandomGenerator.cxx:64 RooQuasiRandomGenerator.cxx:65 RooQuasiRandomGenerator.cxx:66 RooQuasiRandomGenerator.cxx:67 RooQuasiRandomGenerator.cxx:68 RooQuasiRandomGenerator.cxx:69 RooQuasiRandomGenerator.cxx:70 RooQuasiRandomGenerator.cxx:71 RooQuasiRandomGenerator.cxx:72 RooQuasiRandomGenerator.cxx:73 RooQuasiRandomGenerator.cxx:74 RooQuasiRandomGenerator.cxx:75 RooQuasiRandomGenerator.cxx:76 RooQuasiRandomGenerator.cxx:77 RooQuasiRandomGenerator.cxx:78 RooQuasiRandomGenerator.cxx:79 RooQuasiRandomGenerator.cxx:80 RooQuasiRandomGenerator.cxx:81 RooQuasiRandomGenerator.cxx:82 RooQuasiRandomGenerator.cxx:83 RooQuasiRandomGenerator.cxx:84 RooQuasiRandomGenerator.cxx:85 RooQuasiRandomGenerator.cxx:86 RooQuasiRandomGenerator.cxx:87 RooQuasiRandomGenerator.cxx:88 RooQuasiRandomGenerator.cxx:89 RooQuasiRandomGenerator.cxx:90 RooQuasiRandomGenerator.cxx:91 RooQuasiRandomGenerator.cxx:92 RooQuasiRandomGenerator.cxx:93 RooQuasiRandomGenerator.cxx:94 RooQuasiRandomGenerator.cxx:95 RooQuasiRandomGenerator.cxx:96 RooQuasiRandomGenerator.cxx:97 RooQuasiRandomGenerator.cxx:98 RooQuasiRandomGenerator.cxx:99 RooQuasiRandomGenerator.cxx:100 RooQuasiRandomGenerator.cxx:101 RooQuasiRandomGenerator.cxx:102 RooQuasiRandomGenerator.cxx:103 RooQuasiRandomGenerator.cxx:104 RooQuasiRandomGenerator.cxx:105 RooQuasiRandomGenerator.cxx:106 RooQuasiRandomGenerator.cxx:107 RooQuasiRandomGenerator.cxx:108 RooQuasiRandomGenerator.cxx:109 RooQuasiRandomGenerator.cxx:110 RooQuasiRandomGenerator.cxx:111 RooQuasiRandomGenerator.cxx:112 RooQuasiRandomGenerator.cxx:113 RooQuasiRandomGenerator.cxx:114 RooQuasiRandomGenerator.cxx:115 RooQuasiRandomGenerator.cxx:116 RooQuasiRandomGenerator.cxx:117 RooQuasiRandomGenerator.cxx:118 RooQuasiRandomGenerator.cxx:119 RooQuasiRandomGenerator.cxx:120 RooQuasiRandomGenerator.cxx:121 RooQuasiRandomGenerator.cxx:122 RooQuasiRandomGenerator.cxx:123 RooQuasiRandomGenerator.cxx:124 RooQuasiRandomGenerator.cxx:125 RooQuasiRandomGenerator.cxx:126 RooQuasiRandomGenerator.cxx:127 RooQuasiRandomGenerator.cxx:128 RooQuasiRandomGenerator.cxx:129 RooQuasiRandomGenerator.cxx:130 RooQuasiRandomGenerator.cxx:131 RooQuasiRandomGenerator.cxx:132 RooQuasiRandomGenerator.cxx:133 RooQuasiRandomGenerator.cxx:134 RooQuasiRandomGenerator.cxx:135 RooQuasiRandomGenerator.cxx:136 RooQuasiRandomGenerator.cxx:137 RooQuasiRandomGenerator.cxx:138 RooQuasiRandomGenerator.cxx:139 RooQuasiRandomGenerator.cxx:140 RooQuasiRandomGenerator.cxx:141 RooQuasiRandomGenerator.cxx:142 RooQuasiRandomGenerator.cxx:143 RooQuasiRandomGenerator.cxx:144 RooQuasiRandomGenerator.cxx:145 RooQuasiRandomGenerator.cxx:146 RooQuasiRandomGenerator.cxx:147 RooQuasiRandomGenerator.cxx:148 RooQuasiRandomGenerator.cxx:149 RooQuasiRandomGenerator.cxx:150 RooQuasiRandomGenerator.cxx:151 RooQuasiRandomGenerator.cxx:152 RooQuasiRandomGenerator.cxx:153 RooQuasiRandomGenerator.cxx:154 RooQuasiRandomGenerator.cxx:155 RooQuasiRandomGenerator.cxx:156 RooQuasiRandomGenerator.cxx:157 RooQuasiRandomGenerator.cxx:158 RooQuasiRandomGenerator.cxx:159 RooQuasiRandomGenerator.cxx:160 RooQuasiRandomGenerator.cxx:161 RooQuasiRandomGenerator.cxx:162 RooQuasiRandomGenerator.cxx:163 RooQuasiRandomGenerator.cxx:164 RooQuasiRandomGenerator.cxx:165 RooQuasiRandomGenerator.cxx:166 RooQuasiRandomGenerator.cxx:167 RooQuasiRandomGenerator.cxx:168 RooQuasiRandomGenerator.cxx:169 RooQuasiRandomGenerator.cxx:170 RooQuasiRandomGenerator.cxx:171 RooQuasiRandomGenerator.cxx:172 RooQuasiRandomGenerator.cxx:173 RooQuasiRandomGenerator.cxx:174 RooQuasiRandomGenerator.cxx:175 RooQuasiRandomGenerator.cxx:176 RooQuasiRandomGenerator.cxx:177 RooQuasiRandomGenerator.cxx:178 RooQuasiRandomGenerator.cxx:179 RooQuasiRandomGenerator.cxx:180 RooQuasiRandomGenerator.cxx:181 RooQuasiRandomGenerator.cxx:182 RooQuasiRandomGenerator.cxx:183 RooQuasiRandomGenerator.cxx:184 RooQuasiRandomGenerator.cxx:185 RooQuasiRandomGenerator.cxx:186 RooQuasiRandomGenerator.cxx:187 RooQuasiRandomGenerator.cxx:188 RooQuasiRandomGenerator.cxx:189 RooQuasiRandomGenerator.cxx:190 RooQuasiRandomGenerator.cxx:191 RooQuasiRandomGenerator.cxx:192 RooQuasiRandomGenerator.cxx:193 RooQuasiRandomGenerator.cxx:194 RooQuasiRandomGenerator.cxx:195 RooQuasiRandomGenerator.cxx:196 RooQuasiRandomGenerator.cxx:197 RooQuasiRandomGenerator.cxx:198 RooQuasiRandomGenerator.cxx:199 RooQuasiRandomGenerator.cxx:200 RooQuasiRandomGenerator.cxx:201 RooQuasiRandomGenerator.cxx:202 RooQuasiRandomGenerator.cxx:203 RooQuasiRandomGenerator.cxx:204 RooQuasiRandomGenerator.cxx:205 RooQuasiRandomGenerator.cxx:206 RooQuasiRandomGenerator.cxx:207 RooQuasiRandomGenerator.cxx:208 RooQuasiRandomGenerator.cxx:209 RooQuasiRandomGenerator.cxx:210 RooQuasiRandomGenerator.cxx:211 RooQuasiRandomGenerator.cxx:212 RooQuasiRandomGenerator.cxx:213 RooQuasiRandomGenerator.cxx:214 RooQuasiRandomGenerator.cxx:215 RooQuasiRandomGenerator.cxx:216 RooQuasiRandomGenerator.cxx:217 RooQuasiRandomGenerator.cxx:218 RooQuasiRandomGenerator.cxx:219 RooQuasiRandomGenerator.cxx:220 RooQuasiRandomGenerator.cxx:221 RooQuasiRandomGenerator.cxx:222 RooQuasiRandomGenerator.cxx:223 RooQuasiRandomGenerator.cxx:224 RooQuasiRandomGenerator.cxx:225 RooQuasiRandomGenerator.cxx:226 RooQuasiRandomGenerator.cxx:227 RooQuasiRandomGenerator.cxx:228 RooQuasiRandomGenerator.cxx:229 RooQuasiRandomGenerator.cxx:230 RooQuasiRandomGenerator.cxx:231 RooQuasiRandomGenerator.cxx:232 RooQuasiRandomGenerator.cxx:233 RooQuasiRandomGenerator.cxx:234 RooQuasiRandomGenerator.cxx:235 RooQuasiRandomGenerator.cxx:236 RooQuasiRandomGenerator.cxx:237 RooQuasiRandomGenerator.cxx:238 RooQuasiRandomGenerator.cxx:239 RooQuasiRandomGenerator.cxx:240 RooQuasiRandomGenerator.cxx:241 RooQuasiRandomGenerator.cxx:242 RooQuasiRandomGenerator.cxx:243 RooQuasiRandomGenerator.cxx:244 RooQuasiRandomGenerator.cxx:245 RooQuasiRandomGenerator.cxx:246 RooQuasiRandomGenerator.cxx:247 RooQuasiRandomGenerator.cxx:248 RooQuasiRandomGenerator.cxx:249 RooQuasiRandomGenerator.cxx:250 RooQuasiRandomGenerator.cxx:251 RooQuasiRandomGenerator.cxx:252 RooQuasiRandomGenerator.cxx:253 RooQuasiRandomGenerator.cxx:254 RooQuasiRandomGenerator.cxx:255 RooQuasiRandomGenerator.cxx:256 RooQuasiRandomGenerator.cxx:257 RooQuasiRandomGenerator.cxx:258 RooQuasiRandomGenerator.cxx:259 RooQuasiRandomGenerator.cxx:260 RooQuasiRandomGenerator.cxx:261 RooQuasiRandomGenerator.cxx:262 RooQuasiRandomGenerator.cxx:263 RooQuasiRandomGenerator.cxx:264 RooQuasiRandomGenerator.cxx:265 RooQuasiRandomGenerator.cxx:266 RooQuasiRandomGenerator.cxx:267 RooQuasiRandomGenerator.cxx:268 RooQuasiRandomGenerator.cxx:269 RooQuasiRandomGenerator.cxx:270 RooQuasiRandomGenerator.cxx:271 RooQuasiRandomGenerator.cxx:272 RooQuasiRandomGenerator.cxx:273 RooQuasiRandomGenerator.cxx:274 RooQuasiRandomGenerator.cxx:275 RooQuasiRandomGenerator.cxx:276 RooQuasiRandomGenerator.cxx:277 RooQuasiRandomGenerator.cxx:278 RooQuasiRandomGenerator.cxx:279 RooQuasiRandomGenerator.cxx:280 RooQuasiRandomGenerator.cxx:281 RooQuasiRandomGenerator.cxx:282 RooQuasiRandomGenerator.cxx:283 RooQuasiRandomGenerator.cxx:284 RooQuasiRandomGenerator.cxx:285 RooQuasiRandomGenerator.cxx:286 RooQuasiRandomGenerator.cxx:287 RooQuasiRandomGenerator.cxx:288 RooQuasiRandomGenerator.cxx:289 RooQuasiRandomGenerator.cxx:290 RooQuasiRandomGenerator.cxx:291 RooQuasiRandomGenerator.cxx:292 RooQuasiRandomGenerator.cxx:293 RooQuasiRandomGenerator.cxx:294 RooQuasiRandomGenerator.cxx:295 RooQuasiRandomGenerator.cxx:296 RooQuasiRandomGenerator.cxx:297 RooQuasiRandomGenerator.cxx:298 RooQuasiRandomGenerator.cxx:299 RooQuasiRandomGenerator.cxx:300 RooQuasiRandomGenerator.cxx:301 RooQuasiRandomGenerator.cxx:302 RooQuasiRandomGenerator.cxx:303 RooQuasiRandomGenerator.cxx:304 RooQuasiRandomGenerator.cxx:305 RooQuasiRandomGenerator.cxx:306 RooQuasiRandomGenerator.cxx:307 RooQuasiRandomGenerator.cxx:308 RooQuasiRandomGenerator.cxx:309 RooQuasiRandomGenerator.cxx:310 RooQuasiRandomGenerator.cxx:311 RooQuasiRandomGenerator.cxx:312 RooQuasiRandomGenerator.cxx:313 RooQuasiRandomGenerator.cxx:314 RooQuasiRandomGenerator.cxx:315 RooQuasiRandomGenerator.cxx:316 RooQuasiRandomGenerator.cxx:317 RooQuasiRandomGenerator.cxx:318 RooQuasiRandomGenerator.cxx:319 RooQuasiRandomGenerator.cxx:320 RooQuasiRandomGenerator.cxx:321 RooQuasiRandomGenerator.cxx:322 RooQuasiRandomGenerator.cxx:323 RooQuasiRandomGenerator.cxx:324 RooQuasiRandomGenerator.cxx:325 RooQuasiRandomGenerator.cxx:326 RooQuasiRandomGenerator.cxx:327 RooQuasiRandomGenerator.cxx:328 RooQuasiRandomGenerator.cxx:329 RooQuasiRandomGenerator.cxx:330 RooQuasiRandomGenerator.cxx:331 RooQuasiRandomGenerator.cxx:332 RooQuasiRandomGenerator.cxx:333 RooQuasiRandomGenerator.cxx:334 RooQuasiRandomGenerator.cxx:335 RooQuasiRandomGenerator.cxx:336 RooQuasiRandomGenerator.cxx:337 RooQuasiRandomGenerator.cxx:338 RooQuasiRandomGenerator.cxx:339 RooQuasiRandomGenerator.cxx:340 RooQuasiRandomGenerator.cxx:341 RooQuasiRandomGenerator.cxx:342 RooQuasiRandomGenerator.cxx:343 RooQuasiRandomGenerator.cxx:344 RooQuasiRandomGenerator.cxx:345 RooQuasiRandomGenerator.cxx:346 RooQuasiRandomGenerator.cxx:347 RooQuasiRandomGenerator.cxx:348 RooQuasiRandomGenerator.cxx:349 RooQuasiRandomGenerator.cxx:350 RooQuasiRandomGenerator.cxx:351 RooQuasiRandomGenerator.cxx:352 RooQuasiRandomGenerator.cxx:353 RooQuasiRandomGenerator.cxx:354 RooQuasiRandomGenerator.cxx:355 RooQuasiRandomGenerator.cxx:356