herwig is hosted by Hepforge, IPPP Durham
Herwig  7.2.1
BinSampler.h
1 // -*- C++ -*-
2 //
3 // BinSampler.h is a part of Herwig - A multi-purpose Monte Carlo event generator
4 // Copyright (C) 2002-2019 The Herwig Collaboration
5 //
6 // Herwig 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 Herwig_BinSampler_H
10 #define Herwig_BinSampler_H
11 //
12 // This is the declaration of the BinSampler class.
13 //
14 
15 #include "ThePEG/Handlers/StandardEventHandler.h"
16 #include "ThePEG/Utilities/Exception.h"
17 #include "ThePEG/Repository/UseRandom.h"
18 
19 #include "MultiIterationStatistics.h"
20 #include "Remapper.h"
21 
22 namespace Herwig {
23 
24 using namespace ThePEG;
25 
26 class GeneralSampler;
27 
39 
40 public:
41 
47  BinSampler();
48 
52  virtual ~BinSampler();
54 
55 public:
56 
60  Ptr<BinSampler>::ptr cloneMe() const {
61  return dynamic_ptr_cast<Ptr<BinSampler>::ptr>(clone());
62  }
63 
64 public:
65 
69  double evaluate(vector<double> p,
70  bool remap = true);
71 
76  double bias() const { return theBias; }
77 
81  void bias(double b) { theBias = b; }
82 
86  void eventHandler(tStdEHPtr eh) { theEventHandler = eh; }
87 
91  tStdEHPtr eventHandler() const { return theEventHandler; }
92 
96  void sampler(Ptr<GeneralSampler>::tptr);
97 
101  Ptr<GeneralSampler>::tptr sampler() const;
102 
106  int bin() const { return theBin; }
107 
111  void bin(int b) { theBin = b; }
112 
116  string process() const;
117 
121  string shortprocess() const;
122 
126  string id() const;
127 
131  const vector<double>& lastPoint() const { return theLastPoint; }
132 
136  vector<double>& lastPoint() { return theLastPoint; }
137 
141  double referenceWeight() const { return theReferenceWeight; }
142 
146  void referenceWeight(double w) { theReferenceWeight = w; }
147 
154  virtual bool canUnweight() const { return true; }
155 
161  virtual bool adaptsOnTheFly() const { return false; }
162 
167  virtual bool compensating() const { return false; }
168 
172  bool weighted() const { return theWeighted; }
173 
177  void doWeighted(bool yes = true) { theWeighted = yes; }
178 
182  struct NextIteration {};
183 
188  virtual double generate();
189 
193  void fillRemappers(bool progress);
194 
198  void saveRemappers() const;
199 
203  void saveIntegrationData() const;
204 
208  virtual void saveGrid() const {}
209 
213  void readIntegrationData();
214 
218  void setupRemappers(bool progress);
219 
224  void runIteration(unsigned long n, bool progress);
225 
229  virtual void adapt() {}
230 
234  virtual void initialize(bool progress);
235 
239  bool initialized() const { return theInitialized; }
240 
244  void isInitialized() { theInitialized = true; }
245 
249  bool integrated() const { return theIntegrated; }
250 
254  bool remappersFilled() const { return theRemappersFilled; }
255 
259  virtual bool existsGrid() const { return false; }
260 
264  bool hasGrids() const { return theHasGrids; }
265 
269  void didReadGrids() { theHasGrids = true; }
270 
274  virtual void finalize(bool);
275 
280  virtual CrossSection integratedXSec() const {
281  return averageWeight()*nanobarn;
282  }
283 
288  virtual CrossSection integratedXSecErr() const {
289  return sqrt(abs(averageWeightVariance()))*nanobarn;
290  }
291 
298 
302  double lower;
303 
307  map<double,double > bins;
308 
309  map<double,double > binsw1;
313  RandomNumberHistogram(double low = 0.0,
314  double up = 1.,
315  unsigned int nbins = 20);
316 
320  void book(double inv, double weight) {
321  map<double,double>::iterator b = bins.upper_bound(inv);
322  if ( b == bins.end() ) return;
323  b->second = b->second+weight;
324  map<double,double>::iterator b2 = binsw1.upper_bound(inv);
325  if ( b2 == binsw1.end() ) return;
326  b2->second = b2->second+1.;
327 
328  }
329 
333  void dump(const std::string& folder,const std::string& prefix, const std::string& process,const int NR)const;
334 
335 
336  };
337 
338  typedef pair<string,size_t > RandomNumberIndex;
339 
340  map<RandomNumberIndex,pair<RandomNumberHistogram,double> > RandomNumberHistograms;
341 
342 public:
343 
347  int dimension() const { return theEventHandler->nDim(bin()); }
348 
352  unsigned long initialPoints() const { return theInitialPoints; }
353 
357  void initialPoints(unsigned long n) { theInitialPoints = n; }
358 
362  size_t nIterations() const { return theNIterations; }
363 
367  void nIterations(size_t n) { theNIterations = n; }
368 
373  void enhancementFactor(double f) { theEnhancementFactor = f; }
374 
379  double enhancementFactor() const { return theEnhancementFactor; }
380 
384  string randomNumberString() const {return theRandomNumbers;}
385 
393  double kappa() const {return theKappa;}
394 
395 public:
396 
403  void persistentOutput(PersistentOStream & os) const;
404 
410  void persistentInput(PersistentIStream & is, int version);
412 
419  static void Init();
420 
421 protected:
422 
429  virtual IBPtr clone() const;
430 
435  virtual IBPtr fullclone() const;
437 
438 
439 // If needed, insert declarations of virtual function defined in the
440 // InterfacedBase class here (using ThePEG-interfaced-decl in Emacs).
441 
442 private:
443 
447  double theBias;
448 
453 
457  unsigned long theInitialPoints;
458 
463 
468 
474 
481 
488 
493 
497  int theBin;
498 
503 
507  vector<double> theLastPoint;
508 
512  tStdEHPtr theEventHandler;
513 
517  Ptr<GeneralSampler>::tptr theSampler;
518 
523 
527  map<size_t,Remapper> remappers;
528 
532  unsigned long theRemapperPoints;
533 
538 
542  unsigned long theLuminosityMapperBins;
543 
547  unsigned long theGeneralMapperBins;
548 
553 
558 
563 
568 
569 
570 
578  double theKappa;
579 
580 private:
581 
586  BinSampler & operator=(const BinSampler &) = delete;
587 
588 };
589 
590 }
591 
592 #endif /* Herwig_BinSampler_H */
bool theInitialized
Wether or not this sampler has already been initialized.
Definition: BinSampler.h:502
unsigned long theLuminosityMapperBins
The number of bins to be used for luminosity dimensions.
Definition: BinSampler.h:542
virtual void saveGrid() const
Save grid data.
Definition: BinSampler.h:208
map< double, double > bins
The bins, indexed by upper bound.
Definition: BinSampler.h:307
bool theWeighted
True, if weighted events should be generated.
Definition: BinSampler.h:452
bool theHalfPoints
Switch to require that we get half of the points in each iteration below the maximum weight of the it...
Definition: BinSampler.h:480
virtual void adapt()
Adapt this sampler after an iteration has been run.
Definition: BinSampler.h:229
double sqrt(int x)
bool theIntegrated
True, if integration has already be performed.
Definition: BinSampler.h:557
virtual bool compensating() const
If this sampler features a compensation algorithm, return true if more events need to be generated to...
Definition: BinSampler.h:167
bool integrated() const
Return true, if integration has already been performed.
Definition: BinSampler.h:249
double referenceWeight() const
Return the reference weight to be used.
Definition: BinSampler.h:141
bool theRemapChannelDimension
True if channels should get a remapper.
Definition: BinSampler.h:537
void didReadGrids()
Indicate that this sampler has already read grid data.
Definition: BinSampler.h:269
int theBin
The bin to be sampled.
Definition: BinSampler.h:497
void bias(double b)
Set the bias with which this sampler is selected.
Definition: BinSampler.h:81
void referenceWeight(double w)
Set the reference weight to be used.
Definition: BinSampler.h:146
bool hasGrids() const
Return true, if this sampler has already read grid data.
Definition: BinSampler.h:264
double theReferenceWeight
The reference weight to be used.
Definition: BinSampler.h:492
ThePEG::Ptr< InterfacedBase >::pointer IBPtr
bool theHasGrids
True, if this sampler has already read grid data.
Definition: BinSampler.h:567
double theEnhancementFactor
Factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:467
double enhancementFactor() const
Return the factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:379
bool theRemappersFilled
True, if remappers have been set up.
Definition: BinSampler.h:562
vector< double > theLastPoint
The last generated point.
Definition: BinSampler.h:507
virtual CrossSection integratedXSecErr() const
Return the error on the total integrated cross section determined from the Monte Carlo sampling so fa...
Definition: BinSampler.h:288
int theMaxNewMax
The maximum number of allowed new maxima, in combination with HalfPoints, in order to prevent unstabl...
Definition: BinSampler.h:487
void bin(int b)
Set the bin.
Definition: BinSampler.h:111
Exception to be thrown if cross section information should be updated.
Definition: BinSampler.h:182
virtual bool canUnweight() const
Return true, if this sampler can provide unweighted events; if the proposal density is not an overest...
Definition: BinSampler.h:154
unsigned long theRemapperPoints
The number of points to be used for initial filling of the remappers.
Definition: BinSampler.h:532
string randomNumberString() const
Return the folder for the random number plots.
Definition: BinSampler.h:384
tStdEHPtr theEventHandler
The event handler to be used.
Definition: BinSampler.h:512
unsigned long initialPoints() const
Return the number of points to be used for initial integration.
Definition: BinSampler.h:352
double kappa() const
In the AlmostUnweighted mode we do not need to unweight the events to the reference weight...
Definition: BinSampler.h:393
double theKappa
In the AlmostUnweighted mode we do not need to unweight the events to the reference weight...
Definition: BinSampler.h:578
size_t theNIterations
The number of iterations to be considered for initialization.
Definition: BinSampler.h:462
map< size_t, Remapper > remappers
Remapper objects indexed by dimension.
Definition: BinSampler.h:527
bool initialized() const
Return true, if this sampler has already been initialized.
Definition: BinSampler.h:239
BinSampler samples XCombs bins. This default implementation performs flat MC integration.
Definition: BinSampler.h:38
Define the key for the collinear subtraction data.
Definition: BinSampler.h:297
Monte Carlo statistics for multiple iterations.
string theRandomNumbers
Folder for the random number plots.
Definition: BinSampler.h:522
void doWeighted(bool yes=true)
Indicate that weighted events should be generated.
Definition: BinSampler.h:177
const vector< double > & lastPoint() const
Return the last generated point.
Definition: BinSampler.h:131
void book(double inv, double weight)
Book an event.
Definition: BinSampler.h:320
virtual bool existsGrid() const
Return true, if grid data exists for this sampler.
Definition: BinSampler.h:259
unsigned long theInitialPoints
The number of points to use for initial integration.
Definition: BinSampler.h:457
vector< double > & lastPoint()
Access the last generated point.
Definition: BinSampler.h:136
double theRemapperMinSelection
The minimum selection probability for remapper bins.
Definition: BinSampler.h:552
unsigned long theGeneralMapperBins
The number of bins to be used for any other dimension.
Definition: BinSampler.h:547
tStdEHPtr eventHandler() const
Return the event handler.
Definition: BinSampler.h:91
void initialPoints(unsigned long n)
Set the number of points to be used for initial integration.
Definition: BinSampler.h:357
void isInitialized()
Indicate that this sampler has already been initialized.
Definition: BinSampler.h:244
pair< double, double > generate(const Generator< Density > &gen, double r)
Generate a random variable and return its weight.
bool theNonZeroInPresampling
Switch to count only non zero weights in presampling.
Definition: BinSampler.h:473
Ptr< GeneralSampler >::tptr theSampler
The containing sampler.
Definition: BinSampler.h:517
virtual bool adaptsOnTheFly() const
Return true, if this sampler adapts on the fly while generating events.
Definition: BinSampler.h:161
-*- C++ -*-
virtual CrossSection integratedXSec() const
Return the total integrated cross section determined from the Monte Carlo sampling so far...
Definition: BinSampler.h:280
size_t nIterations() const
Return the number of iterations to be considered for initialization.
Definition: BinSampler.h:362
int bin() const
Return the bin.
Definition: BinSampler.h:106
double theBias
The bias with which this sampler is selected.
Definition: BinSampler.h:447
void eventHandler(tStdEHPtr eh)
Set the event handler.
Definition: BinSampler.h:86
Ptr< BinSampler >::ptr cloneMe() const
Clone this object.
Definition: BinSampler.h:60
void nIterations(size_t n)
Set the number of iterations to be considered for initialization.
Definition: BinSampler.h:367
void enhancementFactor(double f)
Set the factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:373
int dimension() const
Return the dimension.
Definition: BinSampler.h:347
double bias() const
Return the bias with which this sampler is selected.
Definition: BinSampler.h:76
bool remappersFilled() const
Return true, if remappers have been set up.
Definition: BinSampler.h:254
bool weighted() const
Return true, if weighted events should be generated.
Definition: BinSampler.h:172