thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
SamplerBase.h
1 // -*- C++ -*-
2 //
3 // SamplerBase.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 ThePEG_SamplerBase_H
10 #define ThePEG_SamplerBase_H
11 // This is the declaration of the SamplerBase class.
12 
13 #include "ThePEG/Interface/Interfaced.h"
14 #include "SamplerBase.fh"
15 #include "ThePEG/Handlers/StandardEventHandler.fh"
16 // #include "SamplerBase.xh"
17 
18 namespace ThePEG {
19 
35 class SamplerBase: public Interfaced {
36 
37 public:
38 
41 
46  : Interfaced(),
47  theIntegrationList("") {}
48 
52  virtual ~SamplerBase();
54 
55 public:
56 
62  void setEventHandler(tStdEHPtr eh) { theEventHandler = eh; }
63 
70  virtual void initialize() = 0;
71 
76  virtual void prepare() {}
77 
82  virtual double generate() = 0;
83 
87  virtual void rejectLast() = 0;
88 
92  const vector<double> & lastPoint() const { return theLastPoint; }
93 
99  virtual int lastBin() const { return 0; }
100 
105  virtual CrossSection integratedXSec() const = 0;
106 
111  virtual CrossSection integratedXSecErr() const = 0;
112 
120  virtual CrossSection maxXSec() const {
121  if ( sumWeights2() <= 0.0 ) return ZERO;
122  return integratedXSec()*attempts()/sumWeights();
123  }
124 
132  virtual double attempts() const {
133  CrossSection sigma = integratedXSec();
134  CrossSection esigma = integratedXSecErr();
135  double sw = sumWeights(); double sw2 = sumWeights2();
136  if ( sw2 <= 0.0 ) return 0.0;
137  return
138  sqr(sw)*(sqr(esigma)-sqr(sigma))/(sqr(sw)*sqr(esigma) - sw2*sqr(sigma));
139  }
140 
145  virtual double sumWeights() const = 0;
146 
151  virtual double sumWeights2() const = 0;
152 
156  virtual bool almostUnweighted() const { return false; }
158 
161 
165  void integrationList(const string& newIntegrationList) { theIntegrationList = newIntegrationList; }
166 
170  const string& integrationList() const { return theIntegrationList; }
171 
175  enum RunLevels {
176 
177  UnknownMode = 0,
178  InitMode,
179  ReadMode,
180  BuildMode,
181  IntegrationMode,
182  RunMode
183 
184  };
185 
189  static int runLevel() {
190  return theRunLevel();
191  }
192 
196  static void setRunLevel(int level) {
197  theRunLevel() = level;
198  }
199 
203  static bool hasSetupFile() {
204  return theHasSetupFile();
205  }
206 
210  static void setupFileUsed(bool yes = true) {
211  theHasSetupFile() = yes;
212  }
213 
218  static long seed() {
219  return theSeed();
220  }
221 
226  static void setSeed(long s) {
227  theSeed() = s;
228  }
229 
233  static unsigned int integratePerJob() {
234  return theIntegratePerJob();
235  }
236 
240  static void setIntegratePerJob(unsigned int s) {
241  theIntegratePerJob() = s;
242  }
243 
247  static unsigned int integrationJobs() {
248  return theIntegrationJobs();
249  }
250 
254  static void setIntegrationJobs(unsigned int s) {
255  theIntegrationJobs() = s;
256  }
257 
259 
260 protected:
261 
265  vector<double> & lastPoint() { return theLastPoint; }
266 
270  tStdEHPtr eventHandler() const { return theEventHandler; }
271 
272 public:
273 
280  void persistentOutput(PersistentOStream & os) const;
281 
287  void persistentInput(PersistentIStream & is, int version);
289 
293  static void Init();
294 
295 private:
296 
300  tStdEHPtr theEventHandler;
301 
305  vector<double> theLastPoint;
306 
311 
315  static int& theRunLevel() {
316  static int lvl = UnknownMode;
317  return lvl;
318  }
319 
323  static bool& theHasSetupFile() {
324  static bool flag = false;
325  return flag;
326  }
327 
332  static long& theSeed() {
333  static long s = 0;
334  return s;
335  }
336 
340  static unsigned int& theIntegratePerJob() {
341  static unsigned int s = 0;
342  return s;
343  }
344 
348  static unsigned int& theIntegrationJobs() {
349  static unsigned int s = 0;
350  return s;
351  }
352 
353 private:
354 
359 
363  SamplerBase & operator=(const SamplerBase &) = delete;
364 
365 };
366 
367 }
368 
369 
370 namespace ThePEG {
371 
378 template <>
379 struct BaseClassTrait<SamplerBase,1>: public ClassTraitsType {
381  typedef Interfaced NthBase;
382 };
383 
388 template <>
389 struct ClassTraits<SamplerBase>: public ClassTraitsBase<SamplerBase> {
391  static string className() { return "ThePEG::SamplerBase"; }
392 
393 };
394 
397 }
398 
399 #endif /* ThePEG_SamplerBase_H */
virtual void rejectLast()=0
Reject the last chosen phase space point.
SamplerBase()
Constructor.
Definition: SamplerBase.h:45
PersistentIStream is used to read persistent objects from a stream where they were previously written...
vector< double > & lastPoint()
Return the last generated phase space point.
Definition: SamplerBase.h:265
void persistentOutput(PersistentOStream &os) const
Function used to write out object persistently.
virtual ~SamplerBase()
Destructor.
SamplerBase & operator=(const SamplerBase &)=delete
Private and non-existent assignment operator.
ClassTraitsType is an empty, non-polymorphic, base class.
Definition: ClassTraits.h:30
string theIntegrationList
A file containing a list of subprocesses to integrate.
Definition: SamplerBase.h:310
tStdEHPtr theEventHandler
The associated event handler.
Definition: SamplerBase.h:300
void persistentInput(PersistentIStream &is, int version)
Function used to read in object persistently.
const vector< double > & lastPoint() const
Return the last generated phase space point.
Definition: SamplerBase.h:92
static void setIntegratePerJob(unsigned int s)
Set the number of subprocesses to be integrated per job.
Definition: SamplerBase.h:240
static void Init()
Standard Init function used to initialize the interfaces.
PersistentOStream is used to write objects persistently to a stream from which they can be read in ag...
static int & theRunLevel()
The run level.
Definition: SamplerBase.h:315
A concreate implementation of ClassDescriptionBase describing an abstract class with persistent data...
const string & integrationList() const
Return a file containing a list of subprocesses to integrate.
Definition: SamplerBase.h:170
void setEventHandler(tStdEHPtr eh)
Set the event handler for which the function StandardEventHandler::dSigDR(const vector<double> &) fun...
Definition: SamplerBase.h:62
virtual void prepare()
An external hook to prepare the sampler for generating events, e.g.
Definition: SamplerBase.h:76
virtual void initialize()=0
Initialize the the sampler, possibly doing presampling of the phase space.
constexpr auto sqr(const T &x) -> decltype(x *x)
The square function should really have been included in the standard C++ library. ...
Definition: ThePEG.h:117
void integrationList(const string &newIntegrationList)
Set a file containing a list of subprocesses to integrate.
Definition: SamplerBase.h:165
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
static void setRunLevel(int level)
Set the run level.
Definition: SamplerBase.h:196
static void setIntegrationJobs(unsigned int s)
Set the maximum number of integration jobs to be created.
Definition: SamplerBase.h:254
static bool & theHasSetupFile()
True, if a setupfile is in use.
Definition: SamplerBase.h:323
static long & theSeed()
The seed that has been used for this run to disentangle grids whihch have been adapted further...
Definition: SamplerBase.h:332
static void setSeed(long s)
Set the seed that has been used for this run to disentangle grids whihch have been adapted further...
Definition: SamplerBase.h:226
static long seed()
Return the seed that has been used for this run to disentangle grids whihch have been adapted further...
Definition: SamplerBase.h:218
static unsigned int & theIntegratePerJob()
The number of subprocesses to be integrated per job.
Definition: SamplerBase.h:340
static unsigned int integratePerJob()
Return the number of subprocesses to be integrated per job.
Definition: SamplerBase.h:233
static int runLevel()
Return the run level.
Definition: SamplerBase.h:189
static void setupFileUsed(bool yes=true)
Indicate that a setupfile is in use.
Definition: SamplerBase.h:210
static AbstractClassDescription< SamplerBase > initSamplerBase
Describe an abstract base class with persistent data.
Definition: SamplerBase.h:358
This is the base class for all phase space sampler classes to be used by the EventHandler class to sa...
Definition: SamplerBase.h:35
virtual double attempts() const
Return the number of attempts.
Definition: SamplerBase.h:132
static unsigned int & theIntegrationJobs()
The maximum number of integration jobs to be created.
Definition: SamplerBase.h:348
virtual double sumWeights2() const =0
Return the sum of the weights squared returned by generate() so far (of the events that were not reje...
static unsigned int integrationJobs()
Return the maximum number of integration jobs to be created.
Definition: SamplerBase.h:247
virtual CrossSection maxXSec() const
Return the reference cross section, a.k.a.
Definition: SamplerBase.h:120
virtual CrossSection integratedXSec() const =0
Return the total integrated cross section determined from the Monte Carlo sampling so far...
static bool hasSetupFile()
Return true, if a setupfile is in use.
Definition: SamplerBase.h:203
virtual CrossSection integratedXSecErr() const =0
Return the error on the total integrated cross section determined from the Monte Carlo sampling so fa...
The Interfaced class is derived from the InterfacedBase class adding a couple of things particular to...
Definition: Interfaced.h:38
virtual bool almostUnweighted() const
Return true if this sampler is generating almost unweighted events.
Definition: SamplerBase.h:156
The default concrete implementation of ClassTraitsBase.
Definition: ClassTraits.h:134
RunLevels
Enumerate the possible run levels.
Definition: SamplerBase.h:175
tStdEHPtr eventHandler() const
Return the associated event handler.
Definition: SamplerBase.h:270
virtual double sumWeights() const =0
Return the sum of the weights returned by generate() so far (of the events that were not rejeted)...
BaseClassTraits describes the base classes of the templated class.
Definition: ClassTraits.h:156
virtual double generate()=0
Generarate a new phase space point and return a weight associated with it.
constexpr ZeroUnit ZERO
ZERO can be used as zero for any unitful quantity.
Definition: PhysicalQty.h:35
virtual int lastBin() const
If the sampler is able to sample several different functions separately, this function should return ...
Definition: SamplerBase.h:99
The templated ClassTraitsBase class defines a set of default information about classes used by ThePEG...
Definition: ClassTraits.h:52
vector< double > theLastPoint
The last generated phase space point.
Definition: SamplerBase.h:305