ThePEG  1.8.0
ACDCGen.h
1 // -*- C++ -*-
2 //
3 // ACDCGen.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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 "ACDCGenConfig.h"
13 #include "ACDCTraits.h"
14 #include "ACDCGenCell.h"
15 #include "ThePEG/Utilities/Exception.h"
16 
17 namespace ACDCGenerator {
18 
47 template <typename Rnd, typename FncPtr>
48 class ACDCGen {
49 
50 public:
51 
53  typedef Rnd RndType;
57  typedef FncPtr FncPtrType;
59  typedef vector<ACDCGenCell*> CellVector;
61  typedef vector<FncPtrType> FncVector;
63  typedef vector<DimType> DimVector;
65  typedef DimVector::size_type size_type;
68 
69 public:
70 
75  inline ACDCGen(Rnd * r);
76 
80  inline ACDCGen();
81 
85  inline ~ACDCGen();
86 
94  inline bool addFunction(DimType dim, FncPtrType f, double maxrat = -1.0);
95 
99  inline void clear();
100 
101 public:
102 
108  inline FncPtrType generate();
109 
114  inline void reject();
115 
120  inline const DVector & lastPoint() const;
121 
125  inline double lastF() const;
126 
130  inline FncPtrType lastFunction() const;
131 
136  inline size_type last() const;
137 
138 public:
139 
147  inline void eps(double newEps);
148 
154  inline void margin(double newMargin);
155 
161  inline void nTry(size_type newNTry);
162 
167  inline void maxTry(long);
169 
170 public:
171 
178  inline double integral(FncPtrType f = FncPtrType()) const;
179 
185  inline double integralErr(FncPtrType f = FncPtrType()) const;
186 
190  inline long n() const;
191 
197  inline long N() const;
198 
203  inline double efficiency() const;
204 
208  inline int nBins() const;
209 
213  inline int depth() const;
214 
219  inline double maxInt() const;
221 
227  inline double eps() const;
228 
233  inline double margin() const;
234 
239  inline size_type nTry() const;
240 
245  inline long maxTry() const;
246 
252  inline bool cheapRandom() const;
253 
257  inline size_type size() const;
258 
265  inline bool compensating();
266 
271  inline long compleft() const;
272 
276  vector<ACDCGenCellInfo> extractCellInfo() const;
278 
279 public:
280 
288  inline void cheapRandom(bool b);
289 
293  inline void setRnd(Rnd * r);
294 
298  inline double rnd() const;
299 
303  inline double rnd(double lo, double up) const;
304 
308  inline void rnd(const DVector & lo, const DVector & up, DVector & r)const;
309 
314  inline void rnd(DimType D, DVector & r) const;
315 
319  inline long rndInt(long x) const;
321 
322 public:
323 
329  template <typename POStream>
330  void output(POStream &) const;
331 
336  template <typename PIStream>
337  void input(PIStream &);
338 
339 private:
340 
344  inline double doMaxInt();
345 
349  inline const FncVector & functions() const;
350 
354  inline FncPtrType function(size_type i) const;
355 
359  inline const DimVector & dimensions() const;
360 
364  inline DimType dimension(size_type i) const;
365 
370  inline DimType lastDimension() const;
371 
375  inline const CellVector & cells() const;
376 
380  inline ACDCGenCell * cell(size_type i) const;
381 
386  inline ACDCGenCell * lastPrimary() const;
387 
392  inline const DVector & sumMaxInts() const;
393 
397  inline ACDCGenCell * lastCell() const;
398 
399 
404  inline void chooseCell(DVector & lo, DVector & up);
405 
411  inline void compensate(const DVector & lo, const DVector & up);
412 
413 private:
414 
418  RndType * theRnd;
419 
423  long theNAcc;
424 
428  long theN;
429 
433  vector<long> theNI;
434 
439 
444 
448  double theEps;
449 
453  double theMargin;
454 
458  size_type theNTry;
459 
464  long theMaxTry;
465 
472 
476  FncVector theFunctions;
477 
481  DimVector theDimensions;
482 
486  CellVector thePrimaryCells;
487 
493 
497  size_type theLast;
498 
503 
508 
512  double theLastF;
513 
517  struct Level {
518 
522  long lastN;
523 
527  double g;
528 
533 
537  size_type index;
538 
547 
548  };
549 
553  typedef vector<Level> LevelVector;
554 
558  LevelVector levels;
559 
560 
565  struct Slicer {
566 
573  Slicer(DimType, ACDCGen &, const DVector &, const DVector &);
574 
579  Slicer(DimType Din, const Slicer & s, ACDCGenCell * cellin,
580  const DVector & loin, const DVector & xselin, const DVector & upin,
581  double fselin);
582 
586  ~Slicer();
587 
591  void divideandconquer();
592 
597  void init();
598 
604  void slice();
605 
610  double shiftmaxmin();
611 
615  void dohalf(DimType);
616 
621  void checkdiag(ACDCGenCell * cell, DimType d, double lod, double upd);
622 
627 
636 
647 
660 
665 
670 
675 
679  double fsel;
680 
685 
693 
702 
706  FncPtr f;
707 
712  double epsilon;
713 
718  double margin;
719 
725  multimap<double,DimType> rateslice;
726 
731  double minf;
732 
737  bool wholecomp;
738 
739  };
740 
741 public:
742 
744  static size_type maxsize;
745 
746 private:
747 
751  ACDCGen(const ACDCGen &);
752 
756  ACDCGen & operator=(const ACDCGen &);
757 
758 };
759 
760 }
761 
762 #include "ACDCGen.icc"
763 
764 #endif
vector< Level > LevelVector
A vector (stack) of levels.
Definition: ACDCGen.h:553
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:565
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:55
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:725
DVector lo
The lower-left corner of the current cell.
Definition: ACDCGen.h:631
ACDCGen is a general class for sampling multi-dimensional functions.
Definition: ACDCGen.h:48
vector< long > theNI
The number of attempts per function so far.
Definition: ACDCGen.h:433
DVector up
The integration limits for the cell being compensated.
Definition: ACDCGen.h:542
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:67
ACDCGen & operator=(const ACDCGen &)
Assignment is private and not implemented.
double theMargin
The factor controlling the loss of efficiency when compensating.
Definition: ACDCGen.h:453
DVector theSumW
The summed weights per function so far.
Definition: ACDCGen.h:438
DVector firstup
The upper-right corner of the &#39;first&#39; cell.
Definition: ACDCGen.h:701
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:448
DVector lo
The integration limits for the cell being compensated.
Definition: ACDCGen.h:546
DVector fhu
The function values found for the xhu point.
Definition: ACDCGen.h:669
ACDCGenCell * theLastCell
The last cell chosen.
Definition: ACDCGen.h:502
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:471
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:486
double g
The previous max value in the Cell to compensate.
Definition: ACDCGen.h:527
DVector theSumW2
The summed squared weights per function so far.
Definition: ACDCGen.h:443
vector< ACDCGenCell * > CellVector
A vector of cells.
Definition: ACDCGen.h:59
FncPtr FncPtrType
Template argument typedef.
Definition: ACDCGen.h:57
DVector xcl
The lower-left point found closest to the current point which gives a function value below the overes...
Definition: ACDCGen.h:641
DVector up
The upper-right corner of the current cell.
Definition: ACDCGen.h:635
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:65
DVector firstlo
The lower-left corner of the &#39;first&#39; cell.
Definition: ACDCGen.h:697
RndType * theRnd
The random number generator to be used for this Generator.
Definition: ACDCGen.h:418
FncVector theFunctions
A vector of functions.
Definition: ACDCGen.h:476
ACDCGenCell * first
The cell which resulted from the first slicing procedure.
Definition: ACDCGen.h:692
long theNAcc
The number of accepted points (weight > 0) so far.
Definition: ACDCGen.h:423
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:737
const FncVector & functions() const
Return the vector of functions.
double fsel
The function value in the current point.
Definition: ACDCGen.h:679
ACDCGen()
Default Constructor.
FncPtr f
A pointer to the function to be used.
Definition: ACDCGen.h:706
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:481
size_type index
The index corresponding to the cell being compensated.
Definition: ACDCGen.h:537
double epsilon
The epsilon() value obtained from the controlling ACDCGen object.
Definition: ACDCGen.h:712
static size_type maxsize
The maximum recursion depth of the compensation so far.
Definition: ACDCGen.h:744
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:731
DVector xsel
The current point around which we are slicing.
Definition: ACDCGen.h:674
LevelVector levels
The vector (stack) of levels.
Definition: ACDCGen.h:558
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:428
DVector xhl
The lower-left point furthest away from the current point which gives a function value abov the overe...
Definition: ACDCGen.h:653
A helper struct representing a level of compensation.
Definition: ACDCGen.h:517
Rnd RndType
Template argument typedef.
Definition: ACDCGen.h:53
vector< FncPtrType > FncVector
A vector of function objects.
Definition: ACDCGen.h:61
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:664
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:532
long lastN
The number of attempts after which this level disapprears.
Definition: ACDCGen.h:522
const CellVector & cells() const
Return the roots of all cell trees.
double theLastF
The function value of the last point.
Definition: ACDCGen.h:512
vector< DimType > DimVector
A vector of integers.
Definition: ACDCGen.h:63
DVector xcu
The upper-right point found closest to the current point which gives a function value below the overe...
Definition: ACDCGen.h:646
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:464
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:718
DimType D
The dimension of the cell to be sliced.
Definition: ACDCGen.h:626
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:659
~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:17
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:507
DVector theSumMaxInts
The accumulated sum of overestimated integrals of the functions in theFunctions.
Definition: ACDCGen.h:492
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:458
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:684
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:497