herwig is hosted by Hepforge, IPPP Durham
Herwig 7.3.0
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
22namespace Herwig {
23
24using namespace ThePEG;
25
26class GeneralSampler;
27
39
40public:
41
46
47public:
48
52 Ptr<BinSampler>::ptr cloneMe() const {
53 return dynamic_ptr_cast<Ptr<BinSampler>::ptr>(clone());
54 }
55
56public:
57
61 double evaluate(vector<double> p,
62 bool remap = true);
63
68 double bias() const { return theBias; }
69
73 void bias(double b) { theBias = b; }
74
78 void eventHandler(tStdEHPtr eh) { theEventHandler = eh; }
79
83 tStdEHPtr eventHandler() const { return theEventHandler; }
84
88 void sampler(Ptr<GeneralSampler>::tptr);
89
93 Ptr<GeneralSampler>::tptr sampler() const;
94
98 int bin() const { return theBin; }
99
103 void bin(int b) { theBin = b; }
104
108 string process() const;
109
113 string shortprocess() const;
114
118 string id() const;
119
123 const vector<double>& lastPoint() const { return theLastPoint; }
124
128 vector<double>& lastPoint() { return theLastPoint; }
129
133 double referenceWeight() const { return theReferenceWeight; }
134
138 void referenceWeight(double w) { theReferenceWeight = w; }
139
146 virtual bool canUnweight() const { return true; }
147
153 virtual bool adaptsOnTheFly() const { return false; }
154
159 virtual bool compensating() const { return false; }
160
164 bool weighted() const { return theWeighted; }
165
169 void doWeighted(bool yes = true) { theWeighted = yes; }
170
174 struct NextIteration {};
175
180 virtual double generate();
181
185 void fillRemappers(bool progress);
186
190 void saveRemappers() const;
191
196
200 virtual void saveGrid() const {}
201
206
210 void setupRemappers(bool progress);
211
216 void runIteration(unsigned long n, bool progress);
217
221 virtual void adapt() {}
222
226 virtual void initialize(bool progress);
227
231 bool initialized() const { return theInitialized; }
232
236 void isInitialized() { theInitialized = true; }
237
241 bool integrated() const { return theIntegrated; }
242
246 bool remappersFilled() const { return theRemappersFilled; }
247
251 virtual bool existsGrid() const { return false; }
252
256 bool hasGrids() const { return theHasGrids; }
257
261 void didReadGrids() { theHasGrids = true; }
262
266 virtual void finalize(bool);
267
272 virtual CrossSection integratedXSec() const {
273 return averageWeight()*nanobarn;
274 }
275
280 virtual CrossSection integratedXSecErr() const {
281 return sqrt(abs(averageWeightVariance()))*nanobarn;
282 }
283
290
294 double lower;
295
299 map<double,double > bins;
300
301 map<double,double > binsw1;
305 RandomNumberHistogram(double low = 0.0,
306 double up = 1.,
307 unsigned int nbins = 20);
308
312 void book(double inv, double weight) {
313 map<double,double>::iterator b = bins.upper_bound(inv);
314 if ( b == bins.end() ) return;
315 b->second = b->second+weight;
316 map<double,double>::iterator b2 = binsw1.upper_bound(inv);
317 if ( b2 == binsw1.end() ) return;
318 b2->second = b2->second+1.;
319
320 }
321
325 void dump(const std::string& folder,const std::string& prefix, const std::string& process,const int NR)const;
326
327
328 };
329
330 typedef pair<string,size_t > RandomNumberIndex;
331
332 map<RandomNumberIndex,pair<RandomNumberHistogram,double> > RandomNumberHistograms;
333
334public:
335
339 int dimension() const { return theEventHandler->nDim(bin()); }
340
344 unsigned long initialPoints() const { return theInitialPoints; }
345
349 void initialPoints(unsigned long n) { theInitialPoints = n; }
350
354 size_t nIterations() const { return theNIterations; }
355
359 void nIterations(size_t n) { theNIterations = n; }
360
366
371 double enhancementFactor() const { return theEnhancementFactor; }
372
376 string randomNumberString() const {return theRandomNumbers;}
377
385 double kappa() const {return theKappa;}
386
387public:
388
396
402 void persistentInput(PersistentIStream & is, int version);
404
411 static void Init();
412
413protected:
414
421 virtual IBPtr clone() const;
422
427 virtual IBPtr fullclone() const;
429
430
431// If needed, insert declarations of virtual function defined in the
432// InterfacedBase class here (using ThePEG-interfaced-decl in Emacs).
433
434private:
435
439 double theBias;
440
445
449 unsigned long theInitialPoints;
450
455
460
466
473
480
485
490
495
499 vector<double> theLastPoint;
500
505
509 Ptr<GeneralSampler>::tptr theSampler;
510
515
519 map<size_t,Remapper> remappers;
520
524 unsigned long theRemapperPoints;
525
530
535
539 unsigned long theGeneralMapperBins;
540
545
550
555
560
561
562
570 double theKappa;
571
572private:
573
578 BinSampler & operator=(const BinSampler &) = delete;
579
580};
581
582}
583
584#endif /* Herwig_BinSampler_H */
BinSampler samples XCombs bins. This default implementation performs flat MC integration.
Definition: BinSampler.h:38
bool theInitialized
Wether or not this sampler has already been initialized.
Definition: BinSampler.h:494
virtual bool canUnweight() const
Return true, if this sampler can provide unweighted events; if the proposal density is not an overest...
Definition: BinSampler.h:146
int dimension() const
Return the dimension.
Definition: BinSampler.h:339
bool integrated() const
Return true, if integration has already been performed.
Definition: BinSampler.h:241
virtual void saveGrid() const
Save grid data.
Definition: BinSampler.h:200
string theRandomNumbers
Folder for the random number plots.
Definition: BinSampler.h:514
double theKappa
In the AlmostUnweighted mode we do not need to unweight the events to the reference weight.
Definition: BinSampler.h:570
double kappa() const
In the AlmostUnweighted mode we do not need to unweight the events to the reference weight.
Definition: BinSampler.h:385
tStdEHPtr eventHandler() const
Return the event handler.
Definition: BinSampler.h:83
vector< double > theLastPoint
The last generated point.
Definition: BinSampler.h:499
Ptr< BinSampler >::ptr cloneMe() const
Clone this object.
Definition: BinSampler.h:52
virtual void initialize(bool progress)
Initialize this bin sampler.
virtual void finalize(bool)
Finalize this sampler.
double theBias
The bias with which this sampler is selected.
Definition: BinSampler.h:439
bool theIntegrated
True, if integration has already be performed.
Definition: BinSampler.h:549
virtual void adapt()
Adapt this sampler after an iteration has been run.
Definition: BinSampler.h:221
void fillRemappers(bool progress)
Fill and finalize the remappers present.
virtual IBPtr fullclone() const
Make a clone of this object, possibly modifying the cloned object to make it sane.
virtual CrossSection integratedXSec() const
Return the total integrated cross section determined from the Monte Carlo sampling so far.
Definition: BinSampler.h:272
size_t nIterations() const
Return the number of iterations to be considered for initialization.
Definition: BinSampler.h:354
int theBin
The bin to be sampled.
Definition: BinSampler.h:489
unsigned long theRemapperPoints
The number of points to be used for initial filling of the remappers.
Definition: BinSampler.h:524
tStdEHPtr theEventHandler
The event handler to be used.
Definition: BinSampler.h:504
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:472
int theMaxNewMax
The maximum number of allowed new maxima, in combination with HalfPoints, in order to prevent unstabl...
Definition: BinSampler.h:479
Ptr< GeneralSampler >::tptr sampler() const
Get the containing sampler.
bool weighted() const
Return true, if weighted events should be generated.
Definition: BinSampler.h:164
virtual bool existsGrid() const
Return true, if grid data exists for this sampler.
Definition: BinSampler.h:251
virtual IBPtr clone() const
Make a simple clone of this object.
bool theRemapChannelDimension
True if channels should get a remapper.
Definition: BinSampler.h:529
bool remappersFilled() const
Return true, if remappers have been set up.
Definition: BinSampler.h:246
double theReferenceWeight
The reference weight to be used.
Definition: BinSampler.h:484
bool theNonZeroInPresampling
Switch to count only non zero weights in presampling.
Definition: BinSampler.h:465
unsigned long initialPoints() const
Return the number of points to be used for initial integration.
Definition: BinSampler.h:344
unsigned long theGeneralMapperBins
The number of bins to be used for any other dimension.
Definition: BinSampler.h:539
unsigned long theLuminosityMapperBins
The number of bins to be used for luminosity dimensions.
Definition: BinSampler.h:534
void saveIntegrationData() const
Write integration data to grid files.
BinSampler & operator=(const BinSampler &)=delete
The assignment operator is private and must never be called.
size_t theNIterations
The number of iterations to be considered for initialization.
Definition: BinSampler.h:454
bool theRemappersFilled
True, if remappers have been set up.
Definition: BinSampler.h:554
virtual CrossSection integratedXSecErr() const
Return the error on the total integrated cross section determined from the Monte Carlo sampling so fa...
Definition: BinSampler.h:280
const vector< double > & lastPoint() const
Return the last generated point.
Definition: BinSampler.h:123
BinSampler()
The default constructor.
bool theHasGrids
True, if this sampler has already read grid data.
Definition: BinSampler.h:559
void persistentOutput(PersistentOStream &os) const
Function used to write out object persistently.
virtual bool adaptsOnTheFly() const
Return true, if this sampler adapts on the fly while generating events.
Definition: BinSampler.h:153
void eventHandler(tStdEHPtr eh)
Set the event handler.
Definition: BinSampler.h:78
Ptr< GeneralSampler >::tptr theSampler
The containing sampler.
Definition: BinSampler.h:509
static void Init()
The standard Init function used to initialize the interfaces.
string randomNumberString() const
Return the folder for the random number plots.
Definition: BinSampler.h:376
double referenceWeight() const
Return the reference weight to be used.
Definition: BinSampler.h:133
map< size_t, Remapper > remappers
Remapper objects indexed by dimension.
Definition: BinSampler.h:519
void referenceWeight(double w)
Set the reference weight to be used.
Definition: BinSampler.h:138
void bias(double b)
Set the bias with which this sampler is selected.
Definition: BinSampler.h:73
void runIteration(unsigned long n, bool progress)
Run a single iteration of n points, optionally printing a progress bar to cout.
bool hasGrids() const
Return true, if this sampler has already read grid data.
Definition: BinSampler.h:256
void nIterations(size_t n)
Set the number of iterations to be considered for initialization.
Definition: BinSampler.h:359
unsigned long theInitialPoints
The number of points to use for initial integration.
Definition: BinSampler.h:449
double theEnhancementFactor
Factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:459
int bin() const
Return the bin.
Definition: BinSampler.h:98
string id() const
Return a string identifying the process handled by this sampler.
void persistentInput(PersistentIStream &is, int version)
Function used to read in object persistently.
void didReadGrids()
Indicate that this sampler has already read grid data.
Definition: BinSampler.h:261
virtual double generate()
Generate the next point and return its weight; store the point in lastPoint().
vector< double > & lastPoint()
Access the last generated point.
Definition: BinSampler.h:128
void saveRemappers() const
Write remappers to grid file.
void initialPoints(unsigned long n)
Set the number of points to be used for initial integration.
Definition: BinSampler.h:349
void setupRemappers(bool progress)
Read remappers from grid file.
void sampler(Ptr< GeneralSampler >::tptr)
Set the containing sampler.
double bias() const
Return the bias with which this sampler is selected.
Definition: BinSampler.h:68
double enhancementFactor() const
Return the factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:371
double theRemapperMinSelection
The minimum selection probability for remapper bins.
Definition: BinSampler.h:544
bool theWeighted
True, if weighted events should be generated.
Definition: BinSampler.h:444
void isInitialized()
Indicate that this sampler has already been initialized.
Definition: BinSampler.h:236
string process() const
Return a string describing the process handled by this sampler.
void enhancementFactor(double f)
Set the factor to enhance the number of points for the next iteration.
Definition: BinSampler.h:365
virtual bool compensating() const
If this sampler features a compensation algorithm, return true if more events need to be generated to...
Definition: BinSampler.h:159
double evaluate(vector< double > p, bool remap=true)
Evaluate the cross section.
string shortprocess() const
Return a short string describing the process handled by this sampler.
bool initialized() const
Return true, if this sampler has already been initialized.
Definition: BinSampler.h:231
void readIntegrationData()
Read integration data from grid files.
void doWeighted(bool yes=true)
Indicate that weighted events should be generated.
Definition: BinSampler.h:169
void bin(int b)
Set the bin.
Definition: BinSampler.h:103
virtual double averageWeight() const
Return the average weight.
virtual double averageWeightVariance() const
Return the variance of the average weight.
Monte Carlo statistics for multiple iterations.
-*- C++ -*-
double sqrt(int x)
ThePEG::Ptr< InterfacedBase >::pointer IBPtr
Exception to be thrown if cross section information should be updated.
Definition: BinSampler.h:174
Define the key for the collinear subtraction data.
Definition: BinSampler.h:289
void dump(const std::string &folder, const std::string &prefix, const std::string &process, const int NR) const
Write to file given name and invariant.
RandomNumberHistogram(double low=0.0, double up=1., unsigned int nbins=20)
Constructor.
void book(double inv, double weight)
Book an event.
Definition: BinSampler.h:312
map< double, double > bins
The bins, indexed by upper bound.
Definition: BinSampler.h:299