thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
ACDCGen.h
1// -*- C++ -*-
2//
3// ACDCGen.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 Leif Lonnblad
5//
6// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8//
9#ifndef ACDCGen_H
10#define ACDCGen_H
11
12#include <algorithm>
13#include "ACDCGenConfig.h"
14#include "ACDCTraits.h"
15#include "ACDCGenCell.h"
16#include "ThePEG/Utilities/Exception.h"
17
18namespace ACDCGenerator {
19
48template <typename Rnd, typename FncPtr>
49class ACDCGen {
50
51public:
52
54 typedef Rnd RndType;
58 typedef FncPtr FncPtrType;
60 typedef vector<ACDCGenCell*> CellVector;
62 typedef vector<FncPtrType> FncVector;
64 typedef vector<DimType> DimVector;
66 typedef DimVector::size_type size_type;
69
70public:
71
76 inline ACDCGen(Rnd * r);
77
81 inline ACDCGen();
82
86 inline ~ACDCGen();
87
95 inline bool addFunction(DimType dim, FncPtrType f, double maxrat = -1.0);
96
100 inline void clear();
101
102public:
103
110
115 inline void reject();
116
121 inline const DVector & lastPoint() const;
122
126 inline double lastF() const;
127
131 inline FncPtrType lastFunction() const;
132
137 inline size_type last() const;
138
139public:
140
148 inline void eps(double newEps);
149
155 inline void margin(double newMargin);
156
162 inline void nTry(size_type newNTry);
163
168 inline void maxTry(long);
170
171public:
172
179 inline double integral(FncPtrType f = FncPtrType()) const;
180
186 inline double integralErr(FncPtrType f = FncPtrType()) const;
187
191 inline long n() const;
192
198 inline long N() const;
199
204 inline double efficiency() const;
205
209 inline int nBins() const;
210
214 inline int depth() const;
215
220 inline double maxInt() const;
222
228 inline double eps() const;
229
234 inline double margin() const;
235
240 inline size_type nTry() const;
241
246 inline long maxTry() const;
247
253 inline bool cheapRandom() const;
254
258 inline size_type size() const;
259
266 inline bool compensating();
267
272 inline long compleft() const;
273
277 vector<ACDCGenCellInfo> extractCellInfo() const;
279
280public:
281
289 inline void cheapRandom(bool b);
290
294 inline void setRnd(Rnd * r);
295
299 inline double rnd() const;
300
304 inline double rnd(double lo, double up) const;
305
309 inline void rnd(const DVector & lo, const DVector & up, DVector & r)const;
310
315 inline void rnd(DimType D, DVector & r) const;
316
320 inline long rndInt(long x) const;
322
323public:
324
330 template <typename POStream>
331 void output(POStream &) const;
332
337 template <typename PIStream>
338 void input(PIStream &);
339
340private:
341
345 inline double doMaxInt();
346
350 inline const FncVector & functions() const;
351
356
360 inline const DimVector & dimensions() const;
361
365 inline DimType dimension(size_type i) const;
366
371 inline DimType lastDimension() const;
372
376 inline const CellVector & cells() const;
377
381 inline ACDCGenCell * cell(size_type i) const;
382
387 inline ACDCGenCell * lastPrimary() const;
388
393 inline const DVector & sumMaxInts() const;
394
398 inline ACDCGenCell * lastCell() const;
399
400
405 inline void chooseCell(DVector & lo, DVector & up);
406
412 inline void compensate(const DVector & lo, const DVector & up);
413
414private:
415
420
425
429 long theN;
430
434 vector<long> theNI;
435
440
445
449 double theEps;
450
454 double theMargin;
455
460
466
473
478
483
488
494
499
504
509
513 double theLastF;
514
518 struct Level {
519
523 long lastN;
524
528 double g;
529
534
539
548
549 };
550
554 typedef vector<Level> LevelVector;
555
560
561
566 struct Slicer {
567
574 Slicer(DimType, ACDCGen &, const DVector &, const DVector &);
575
580 Slicer(DimType Din, const Slicer & s, ACDCGenCell * cellin,
581 const DVector & loin, const DVector & xselin, const DVector & upin,
582 double fselin);
583
588
593
598 void init();
599
605 void slice();
606
611 double shiftmaxmin();
612
617
622 void checkdiag(ACDCGenCell * cell, DimType d, double lod, double upd);
623
628
637
648
661
666
671
676
680 double fsel;
681
686
694
703
707 FncPtr f;
708
713 double epsilon;
714
719 double margin;
720
726 multimap<double,DimType> rateslice;
727
732 double minf;
733
739
740 };
741
742public:
743
746
747private:
748
752 ACDCGen(const ACDCGen &);
753
757 ACDCGen & operator=(const ACDCGen &) = delete;
758
759};
760
761}
762
763#include "ACDCGen.icc"
764
765#endif
is the main config header file for ACDCGen.
ACDCGenCell is the class representing a generation cell in ACDCGen.
Definition: ACDCGenCell.h:20
ACDCGen is a general class for sampling multi-dimensional functions.
Definition: ACDCGen.h:49
void cheapRandom(bool b)
Set to true if generating random numbers are so cheap that a new one can be thrown everytime a sub-ce...
DVector theSumMaxInts
The accumulated sum of overestimated integrals of the functions in theFunctions.
Definition: ACDCGen.h:493
double efficiency() const
The ratio of the number of accepted and number of tried points n()/N();.
FncPtrType lastFunction() const
Return the function chosen for the last generated point.
double margin() const
The safety margin used to multiply the highest found function value in a cell when setting its overes...
RndType * theRnd
The random number generator to be used for this Generator.
Definition: ACDCGen.h:419
void setRnd(Rnd *r)
Set a new random number generator.
vector< ACDCGenCell * > CellVector
A vector of cells.
Definition: ACDCGen.h:60
ACDCGenCell * lastCell() const
Return the cell chosen for the last generated point.
long N() const
The number of calls to generate() so far.
FncPtrType generate()
Generate a point, choosing between the different functions specified.
vector< DimType > DimVector
A vector of integers.
Definition: ACDCGen.h:64
Rnd RndType
Template argument typedef.
Definition: ACDCGen.h:54
ACDCGen()
Default Constructor.
DVector theSumW2
The summed squared weights per function so far.
Definition: ACDCGen.h:444
ACDCGenCell * lastPrimary() const
Return the root cell for the function chosen for the last generated point.
double maxInt() const
Return the current overestimation of the full integral of all specified functions over the unit volum...
double doMaxInt()
Calculate the overestimated integral for all functions.
void chooseCell(DVector &lo, DVector &up)
Choose a function according to its overestimated integral and choose a cell to generate a point in.
vector< ACDCGenCellInfo > extractCellInfo() const
Return a vector with information about all cells.
double integralErr(FncPtrType f=FncPtrType()) const
Return the error on the current Monte Carlo estimate of the integral of the specified function (or al...
void nTry(size_type newNTry)
Set the number of points (with non-zero function value) used to initialize the tree of cells to use i...
DVector theSumW
The summed weights per function so far.
Definition: ACDCGen.h:439
vector< FncPtrType > FncVector
A vector of function objects.
Definition: ACDCGen.h:62
void reject()
Reject the last generated point.
void output(POStream &) const
This function is to be used in ThePEG for output to a persistent stream and will not work properly fo...
FncPtr FncPtrType
Template argument typedef.
Definition: ACDCGen.h:58
double theEps
The smallest possible division allowed.
Definition: ACDCGen.h:449
ACDCGenCell * cell(size_type i) const
Return the root cell for the i'th function.
int depth() const
Return the maximum depth of any tree of cells used.
int nBins() const
Return the number of active cells created so far.
bool cheapRandom() const
Returns true if generating random numbers are so cheap that a new one can be thrown everytime a sub-c...
long n() const
The number of accepted points so far.
vector< long > theNI
The number of attempts per function so far.
Definition: ACDCGen.h:434
void rnd(const DVector &lo, const DVector &up, DVector &r) const
Fill the r vector with doubles r[i] in the interval ]lo[i],up[i][.
double theLastF
The function value of the last point.
Definition: ACDCGen.h:513
double lastF() const
Return the value of the last chosen function in the last point.
bool addFunction(DimType dim, FncPtrType f, double maxrat=-1.0)
Add a function of a given dimension, dim, according to which points will be generated.
size_type theLast
The last index chosen.
Definition: ACDCGen.h:498
vector< Level > LevelVector
A vector (stack) of levels.
Definition: ACDCGen.h:554
void clear()
Remove all added functions and reset the generator;.
bool useCheapRandom
True if generating random numbers are so cheap that a new one can be thrown everytime a sub-cell is c...
Definition: ACDCGen.h:472
DVector theLastPoint
The last point generated.
Definition: ACDCGen.h:508
const CellVector & cells() const
Return the roots of all cell trees.
DimType dimension(size_type i) const
Return the dimension of the i'th function.
DimVector::size_type size_type
The size type of the vectors used.
Definition: ACDCGen.h:66
void input(PIStream &)
This function is to be used in ThePEG for input from a persistent stream and will not work properly f...
double rnd(double lo, double up) const
Double precision number in the interval ]lo,up[.
long rndInt(long x) const
Integer in the interval [0,x[.
long theMaxTry
The maximum number of attempts to generate a phase space point, or to find non-zero points in the ini...
Definition: ACDCGen.h:465
const DVector & lastPoint() const
Return the last generated point.
long theN
The number of attempted points so far.
Definition: ACDCGen.h:429
void margin(double newMargin)
Set the safety margin used to multiply the highest found function value in a cell when setting its ov...
size_type size() const
The number of functions used.
double theMargin
The factor controlling the loss of efficiency when compensating.
Definition: ACDCGen.h:454
DimVector theDimensions
The dimensions of the functions in theFunctions.
Definition: ACDCGen.h:482
ACDCRandomTraits< RndType > RndTraits
Template argument typedef.
Definition: ACDCGen.h:56
size_type theNTry
The number of points to use to find initial average.
Definition: ACDCGen.h:459
CellVector thePrimaryCells
The root of the cell tree for the functions in theFunctions.
Definition: ACDCGen.h:487
FncVector theFunctions
A vector of functions.
Definition: ACDCGen.h:477
ACDCGen(const ACDCGen &)
Copy constructor is private and not implemented.
double rnd() const
Double precision number in the interval ]0,1[.
size_type nTry() const
The number of points used to initialize the tree of cells to use in the generation.
LevelVector levels
The vector (stack) of levels.
Definition: ACDCGen.h:559
double integral(FncPtrType f=FncPtrType()) const
Return the current Monte Carlo estimate of the integral of the specified function (or all functions i...
const DimVector & dimensions() const
Return a vector with the dimensions of all functions.
bool compensating()
Returns true if the generator is currently in a state of compensating an erroneous overestimation of ...
const DVector & sumMaxInts() const
Return a vector with the incremental sum of overestimated integrals for each function.
void compensate(const DVector &lo, const DVector &up)
Start the compensation procedure for the last chosen cell when a function velue has been found which ...
void eps(double newEps)
Set the minimum cell size considered for this generation.
void maxTry(long)
Set the maximum number of attempts to generate a phase space point, or to find non-zero points in the...
FncPtrType function(size_type i) const
Return the i'th function.
ACDCGen & operator=(const ACDCGen &)=delete
Assignment is private and not implemented.
long compleft() const
Return an estimate of how many points need to be sampled before the generator finishes compensating.
ACDCGen(Rnd *r)
Standard constructor requiring a random generator object to be used.
size_type last() const
return the index of the function chosen for the last generated point.
static size_type maxsize
The maximum recursion depth of the compensation so far.
Definition: ACDCGen.h:745
ACDCFncTraits< FncPtrType > FncTraits
Template argument typedef.
Definition: ACDCGen.h:68
long theNAcc
The number of accepted points (weight > 0) so far.
Definition: ACDCGen.h:424
double eps() const
The minimum cell size considered for this generation.
void rnd(DimType D, DVector &r) const
Fill the D first elements in the r vector with doubles in the interval ]0,1[.
DimType lastDimension() const
Return the dimension of the function chosen for the last generated point.
const FncVector & functions() const
Return the vector of functions.
ACDCGenCell * theLastCell
The last cell chosen.
Definition: ACDCGen.h:503
long maxTry() const
The maximum number of attempts to generate a phase space point, or to find non-zero points in the ini...
The namespace in which all ACDCGen classes are defined.
Definition: ACDCGen.h:18
short DimType
The integer type used to represent the dimension of the a functions to be sampled-.
Definition: ACDCGenConfig.h:45
vector< double > DVector
A vector of doubles.
Definition: ACDCGenConfig.h:58
ACDCFncTraits defines the interface to functions to be sampled by ACDCGen.
Definition: ACDCTraits.h:28
A helper struct representing a level of compensation.
Definition: ACDCGen.h:518
DVector up
The integration limits for the cell being compensated.
Definition: ACDCGen.h:543
DVector lo
The integration limits for the cell being compensated.
Definition: ACDCGen.h:547
ACDCGenCell * cell
The cell which is being compensated.
Definition: ACDCGen.h:533
size_type index
The index corresponding to the cell being compensated.
Definition: ACDCGen.h:538
long lastN
The number of attempts after which this level disapprears.
Definition: ACDCGen.h:523
double g
The previous max value in the Cell to compensate.
Definition: ACDCGen.h:528
This is a help struct to perform the divide-and-conquer slicing of cells before starting the compensa...
Definition: ACDCGen.h:566
DVector xhl
The lower-left point furthest away from the current point which gives a function value abov the overe...
Definition: ACDCGen.h:654
void divideandconquer()
Called from both constructors to do the actual work.
double margin
The margin() value obtained from the controlling ACDCGen object.
Definition: ACDCGen.h:719
DVector fhl
The function values found for the xhl point.
Definition: ACDCGen.h:665
multimap< double, DimType > rateslice
The dimensions to slice in rated by the resulting fractional volume of the resulting slice.
Definition: ACDCGen.h:726
double minf
The minimu function value found in the current sliced cell (set by shiftmaxmin()).
Definition: ACDCGen.h:732
DVector xcu
The upper-right point found closest to the current point which gives a function value below the overe...
Definition: ACDCGen.h:647
Slicer(DimType, ACDCGen &, const DVector &, const DVector &)
The constructor takes the number of dimensions of the function approximated by the current cell,...
ACDCGenCell * current
The current cell.
Definition: ACDCGen.h:685
void slice()
Do the slicing and increase the overestimate of the function in the resulting cell.
DVector firstup
The upper-right corner of the 'first' cell.
Definition: ACDCGen.h:702
bool wholecomp
If true, then the whole original cell should compensated in the continued generation.
Definition: ACDCGen.h:738
DVector xcl
The lower-left point found closest to the current point which gives a function value below the overes...
Definition: ACDCGen.h:642
DVector fhu
The function values found for the xhu point.
Definition: ACDCGen.h:670
DVector up
The upper-right corner of the current cell.
Definition: ACDCGen.h:636
DVector firstlo
The lower-left corner of the 'first' cell.
Definition: ACDCGen.h:698
void init()
Initialize the procedure, finding the slicing points around the current point.
DimType D
The dimension of the cell to be sliced.
Definition: ACDCGen.h:627
void dohalf(DimType)
Find the slice point of the current cell in the direction given.
double shiftmaxmin()
After slicing a cell, find the maximum function value found in the resulting cell.
ACDCGenCell * first
The cell which resulted from the first slicing procedure.
Definition: ACDCGen.h:693
DVector xsel
The current point around which we are slicing.
Definition: ACDCGen.h:675
double fsel
The function value in the current point.
Definition: ACDCGen.h:680
DVector xhu
The upper-right point furthest away from the current point which gives a function value abov the over...
Definition: ACDCGen.h:660
double epsilon
The epsilon() value obtained from the controlling ACDCGen object.
Definition: ACDCGen.h:713
void checkdiag(ACDCGenCell *cell, DimType d, double lod, double upd)
If split is in more than one dimensions check the overestimate for the chopped-off cell.
Slicer(DimType Din, const Slicer &s, ACDCGenCell *cellin, const DVector &loin, const DVector &xselin, const DVector &upin, double fselin)
The constructor used internally when diagonally chopped-off cells need to be sliced themselves.
FncPtr f
A pointer to the function to be used.
Definition: ACDCGen.h:707
DVector lo
The lower-left corner of the current cell.
Definition: ACDCGen.h:632
ACDCRandomTraits defines the interface to random number generator objects to be used by ACDCGen.
Definition: ACDCTraits.h:47