thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
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 
18 namespace ACDCGenerator {
19 
48 template <typename Rnd, typename FncPtr>
49 class ACDCGen {
50 
51 public:
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 
70 public:
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 
102 public:
103 
109  inline FncPtrType generate();
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 
139 public:
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 
171 public:
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 
280 public:
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 
323 public:
324 
330  template <typename POStream>
331  void output(POStream &) const;
332 
337  template <typename PIStream>
338  void input(PIStream &);
339 
340 private:
341 
345  inline double doMaxInt();
346 
350  inline const FncVector & functions() const;
351 
355  inline FncPtrType function(size_type i) const;
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 
414 private:
415 
419  RndType * theRnd;
420 
424  long theNAcc;
425 
429  long theN;
430 
434  vector<long> theNI;
435 
440 
445 
449  double theEps;
450 
454  double theMargin;
455 
459  size_type theNTry;
460 
465  long theMaxTry;
466 
473 
477  FncVector theFunctions;
478 
482  DimVector theDimensions;
483 
487  CellVector thePrimaryCells;
488 
494 
498  size_type theLast;
499 
504 
509 
513  double theLastF;
514 
518  struct Level {
519 
523  long lastN;
524 
528  double g;
529 
534 
538  size_type index;
539 
548 
549  };
550 
554  typedef vector<Level> LevelVector;
555 
559  LevelVector levels;
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 
587  ~Slicer();
588 
592  void divideandconquer();
593 
598  void init();
599 
605  void slice();
606 
611  double shiftmaxmin();
612 
616  void dohalf(DimType);
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 
738  bool wholecomp;
739 
740  };
741 
742 public:
743 
745  static size_type maxsize;
746 
747 private:
748 
752  ACDCGen(const ACDCGen &);
753 
757  ACDCGen & operator=(const ACDCGen &) = delete;
758 
759 };
760 
761 }
762 
763 #include "ACDCGen.icc"
764 
765 #endif
vector< Level > LevelVector
A vector (stack) of levels.
Definition: ACDCGen.h:554
DimType lastDimension() const
Return the dimension of the function chosen for the last generated point.
int nBins() const
Return the number of active cells created so far.
long maxTry() const
The maximum number of attempts to generate a phase space point, or to find non-zero points in the ini...
This is a help struct to perform the divide-and-conquer slicing of cells before starting the compensa...
Definition: ACDCGen.h:566
double integral(FncPtrType f=FncPtrType()) const
Return the current Monte Carlo estimate of the integral of the specified function (or all functions i...
ACDCRandomTraits< RndType > RndTraits
Template argument typedef.
Definition: ACDCGen.h:56
ACDCGenCell * lastPrimary() const
Return the root cell for the function chosen for the last generated point.
multimap< double, DimType > rateslice
The dimensions to slice in rated by the resulting fractional volume of the resulting slice...
Definition: ACDCGen.h:726
DVector lo
The lower-left corner of the current cell.
Definition: ACDCGen.h:632
ACDCGen is a general class for sampling multi-dimensional functions.
Definition: ACDCGen.h:49
vector< long > theNI
The number of attempts per function so far.
Definition: ACDCGen.h:434
DVector up
The integration limits for the cell being compensated.
Definition: ACDCGen.h:543
long rndInt(long x) const
Integer in the interval [0,x[.
double lastF() const
Return the value of the last chosen function in the last point.
double margin() const
The safety margin used to multiply the highest found function value in a cell when setting its overes...
const DimVector & dimensions() const
Return a vector with the dimensions of all functions.
ACDCFncTraits defines the interface to functions to be sampled by ACDCGen.
Definition: ACDCTraits.h:28
ACDCFncTraits< FncPtrType > FncTraits
Template argument typedef.
Definition: ACDCGen.h:68
double theMargin
The factor controlling the loss of efficiency when compensating.
Definition: ACDCGen.h:454
DVector theSumW
The summed weights per function so far.
Definition: ACDCGen.h:439
DVector firstup
The upper-right corner of the &#39;first&#39; cell.
Definition: ACDCGen.h:702
bool compensating()
Returns true if the generator is currently in a state of compensating an erroneous overestimation of ...
double theEps
The smallest possible division allowed.
Definition: ACDCGen.h:449
DVector lo
The integration limits for the cell being compensated.
Definition: ACDCGen.h:547
DVector fhu
The function values found for the xhu point.
Definition: ACDCGen.h:670
ACDCGenCell * theLastCell
The last cell chosen.
Definition: ACDCGen.h:503
FncPtrType lastFunction() const
Return the function chosen for the last generated point.
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
double integralErr(FncPtrType f=FncPtrType()) const
Return the error on the current Monte Carlo estimate of the integral of the specified function (or al...
double efficiency() const
The ratio of the number of accepted and number of tried points n()/N();.
long n() const
The number of accepted points so far.
double rnd() const
Double precision number in the interval ]0,1[.
CellVector thePrimaryCells
The root of the cell tree for the functions in theFunctions.
Definition: ACDCGen.h:487
double g
The previous max value in the Cell to compensate.
Definition: ACDCGen.h:528
DVector theSumW2
The summed squared weights per function so far.
Definition: ACDCGen.h:444
vector< ACDCGenCell * > CellVector
A vector of cells.
Definition: ACDCGen.h:60
FncPtr FncPtrType
Template argument typedef.
Definition: ACDCGen.h:58
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 up
The upper-right corner of the current cell.
Definition: ACDCGen.h:636
vector< ACDCGenCellInfo > extractCellInfo() const
Return a vector with information about all cells.
size_type size() const
The number of functions used.
DimVector::size_type size_type
The size type of the vectors used.
Definition: ACDCGen.h:66
DVector firstlo
The lower-left corner of the &#39;first&#39; cell.
Definition: ACDCGen.h:698
RndType * theRnd
The random number generator to be used for this Generator.
Definition: ACDCGen.h:419
FncVector theFunctions
A vector of functions.
Definition: ACDCGen.h:477
ACDCGenCell * first
The cell which resulted from the first slicing procedure.
Definition: ACDCGen.h:693
long theNAcc
The number of accepted points (weight > 0) so far.
Definition: ACDCGen.h:424
void reject()
Reject the last generated point.
double eps() const
The minimum cell size considered for this generation.
bool wholecomp
If true, then the whole original cell should compensated in the continued generation.
Definition: ACDCGen.h:738
const FncVector & functions() const
Return the vector of functions.
double fsel
The function value in the current point.
Definition: ACDCGen.h:680
ACDCGen()
Default Constructor.
FncPtr f
A pointer to the function to be used.
Definition: ACDCGen.h:707
const DVector & sumMaxInts() const
Return a vector with the incremental sum of overestimated integrals for each function.
DimVector theDimensions
The dimensions of the functions in theFunctions.
Definition: ACDCGen.h:482
size_type index
The index corresponding to the cell being compensated.
Definition: ACDCGen.h:538
double epsilon
The epsilon() value obtained from the controlling ACDCGen object.
Definition: ACDCGen.h:713
static size_type maxsize
The maximum recursion depth of the compensation so far.
Definition: ACDCGen.h:745
int depth() const
Return the maximum depth of any tree of cells used.
ACDCGenCell * lastCell() const
Return the cell chosen for the last generated point.
const DVector & lastPoint() const
Return the last generated point.
void chooseCell(DVector &lo, DVector &up)
Choose a function according to its overestimated integral and choose a cell to generate a point in...
double minf
The minimu function value found in the current sliced cell (set by shiftmaxmin()).
Definition: ACDCGen.h:732
DVector xsel
The current point around which we are slicing.
Definition: ACDCGen.h:675
LevelVector levels
The vector (stack) of levels.
Definition: ACDCGen.h:559
short DimType
The integer type used to represent the dimension of the a functions to be sampled-.
Definition: ACDCGenConfig.h:45
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.
long theN
The number of attempted points so far.
Definition: ACDCGen.h:429
DVector xhl
The lower-left point furthest away from the current point which gives a function value abov the overe...
Definition: ACDCGen.h:654
A helper struct representing a level of compensation.
Definition: ACDCGen.h:518
Rnd RndType
Template argument typedef.
Definition: ACDCGen.h:54
vector< FncPtrType > FncVector
A vector of function objects.
Definition: ACDCGen.h:62
void input(PIStream &)
This function is to be used in ThePEG for input from a persistent stream and will not work properly f...
DVector fhl
The function values found for the xhl point.
Definition: ACDCGen.h:665
ACDCGenCell * cell(size_type i) const
Return the root cell for the i&#39;th function.
ACDCGenCell * cell
The cell which is being compensated.
Definition: ACDCGen.h:533
long lastN
The number of attempts after which this level disapprears.
Definition: ACDCGen.h:523
const CellVector & cells() const
Return the roots of all cell trees.
double theLastF
The function value of the last point.
Definition: ACDCGen.h:513
vector< DimType > DimVector
A vector of integers.
Definition: ACDCGen.h:64
DVector xcu
The upper-right point found closest to the current point which gives a function value below the overe...
Definition: ACDCGen.h:647
void setRnd(Rnd *r)
Set a new random number generator.
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.
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
ACDCGen & operator=(const ACDCGen &)=delete
Assignment is private and not implemented.
vector< double > DVector
A vector of doubles.
Definition: ACDCGenConfig.h:58
is the main config header file for ACDCGen.
double margin
The margin() value obtained from the controlling ACDCGen object.
Definition: ACDCGen.h:719
DimType D
The dimension of the cell to be sliced.
Definition: ACDCGen.h:627
void clear()
Remove all added functions and reset the generator;.
size_type last() const
return the index of the function chosen for the last generated point.
size_type nTry() const
The number of points used to initialize the tree of cells to use in the generation.
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 ...
FncPtrType generate()
Generate a point, choosing between the different functions specified.
long compleft() const
Return an estimate of how many points need to be sampled before the generator finishes compensating...
DVector xhu
The upper-right point furthest away from the current point which gives a function value abov the over...
Definition: ACDCGen.h:660
~ACDCGen()
Destructor.
bool cheapRandom() const
Returns true if generating random numbers are so cheap that a new one can be thrown everytime a sub-c...
The namespace in which all ACDCGen classes are defined.
Definition: ACDCGen.h:18
void output(POStream &) const
This function is to be used in ThePEG for output to a persistent stream and will not work properly fo...
DVector theLastPoint
The last point generated.
Definition: ACDCGen.h:508
DVector theSumMaxInts
The accumulated sum of overestimated integrals of the functions in theFunctions.
Definition: ACDCGen.h:493
long N() const
The number of calls to generate() so far.
size_type theNTry
The number of points to use to find initial average.
Definition: ACDCGen.h:459
ACDCGenCell is the class representing a generation cell in ACDCGen.
Definition: ACDCGenCell.h:20
ACDCRandomTraits defines the interface to random number generator objects to be used by ACDCGen...
Definition: ACDCTraits.h:47
ACDCGenCell * current
The current cell.
Definition: ACDCGen.h:685
DimType dimension(size_type i) const
Return the dimension of the i&#39;th function.
size_type theLast
The last index chosen.
Definition: ACDCGen.h:498