Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:24:23

0001 // -*- C++ -*-
0002 //
0003 // GeneralSampler.h is a part of Herwig - A multi-purpose Monte Carlo event generator
0004 // Copyright (C) 2002-2019 The Herwig Collaboration
0005 //
0006 // Herwig is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef Herwig_GeneralSampler_H
0010 #define Herwig_GeneralSampler_H
0011 //
0012 // This is the declaration of the GeneralSampler class.
0013 //
0014 
0015 #include "ThePEG/Handlers/SamplerBase.h"
0016 #include "BinSampler.h"
0017 
0018 namespace Herwig {
0019 
0020 using namespace ThePEG;
0021 
0022 /**
0023  * \ingroup Matchbox
0024  * \author Simon Platzer
0025  *
0026  * \brief A GeneralSampler class
0027  *
0028  * @see \ref GeneralSamplerInterfaces "The interfaces"
0029  * defined for GeneralSampler.
0030  */
0031 class GeneralSampler: public SamplerBase {
0032 
0033 public:
0034 
0035   /** @name Standard constructors and destructors. */
0036   //@{
0037   /**
0038    * The default constructor.
0039    */
0040   GeneralSampler();
0041 
0042   /**
0043    * The destructor.
0044    */
0045   virtual ~GeneralSampler();
0046   //@}
0047 
0048 public:
0049 
0050   /** @name Virtual functions from SamplerBase. */
0051   //@{
0052   /**
0053    * Initialize the the sampler, possibly doing presampling of the
0054    * phase space.
0055    */
0056   virtual void initialize();
0057 
0058   /**
0059    * Generarate a new phase space point and return a weight associated
0060    * with it. This weight should preferably be 1.
0061    */
0062   virtual double generate();
0063 
0064   /**
0065    * Reject the last chosen phase space point.
0066    */
0067   virtual void rejectLast();
0068 
0069   /**
0070    * If the sampler is able to sample several different functions
0071    * separately, this function should return the last chosen
0072    * function. This default version always returns 0.
0073    */
0074   virtual int lastBin() const { return lastSampler() ? lastSampler()->bin() : 0; }
0075 
0076   /**
0077    * Return the total integrated cross section determined from the
0078    * Monte Carlo sampling so far.
0079    */
0080   virtual CrossSection integratedXSec() const {
0081     currentCrossSections();
0082     return theIntegratedXSec;
0083   }
0084 
0085   /**
0086    * Return the error on the total integrated cross section determined
0087    * from the Monte Carlo sampling so far.
0088    */
0089   virtual CrossSection integratedXSecErr() const {
0090     currentCrossSections();
0091     return theIntegratedXSecErr;
0092   }
0093 
0094   /**
0095    * Return the overestimated integrated cross section.
0096    */
0097   virtual CrossSection maxXSec() const { 
0098     if ( theAddUpSamplers )
0099       return SamplerBase::maxXSec();
0100     return theMaxWeight*nanobarn;
0101   }
0102 
0103   /**
0104    * Return the sum of the weights returned by generate() so far (of
0105    * the events that were not rejeted).
0106    */
0107   virtual double sumWeights() const { return theSumWeights; }
0108 
0109   /**
0110    * Return the sum of the weights squaredreturned by generate() so far (of
0111    * the events that were not rejeted).
0112    */
0113   virtual double sumWeights2() const { return theSumWeights2; }
0114 
0115   /**
0116    * Return the number of attempts
0117    */
0118   virtual double attempts() const { 
0119     if ( theAddUpSamplers )
0120       return SamplerBase::attempts();
0121     return theAttempts;
0122   }
0123 
0124   /**
0125    * Return the number of accepts
0126    */
0127   double accepts() const { return theAccepts; }
0128 
0129   //@}
0130 
0131   /**
0132    * Return the samplers
0133    */
0134   const map<double,Ptr<BinSampler>::ptr>& samplers() const { return theSamplers; }
0135 
0136   /**
0137    * Return the bin sampler
0138    */
0139   Ptr<BinSampler>::ptr binSampler() const { return theBinSampler; }
0140 
0141   /**
0142    * Return the last selected bin sampler
0143    */
0144   Ptr<BinSampler>::tptr lastSampler() const { return theLastSampler; }
0145 
0146   /**
0147    * True if we should do weighted events
0148    */
0149   bool weighted() const { return eventHandler()->weighted(); }
0150 
0151   /** 
0152    * Return true if this sampler is generating almost unweighted events.
0153    */ 
0154   virtual bool almostUnweighted() const { return theAlmostUnweighted; }
0155 
0156 public:
0157 
0158   /**
0159    * Return the XML element containing the grids
0160    */
0161   const XML::Element& grids() const { return theGrids; }
0162 
0163   /**
0164    * Access the XML element containing the grids
0165    */
0166   XML::Element& grids() { return theGrids; }
0167 
0168   /**
0169    * Write out grids
0170    */
0171   void writeGrids() const;
0172 
0173   /**
0174    * Read in grids
0175    */
0176   void readGrids();
0177   
0178   /**
0179    * Return the number of integration jobs which were actually created.
0180    */
0181   unsigned int integrationJobsCreated() {
0182     return theIntegrationJobsCreated;
0183   }
0184 
0185   /**
0186    * An external hook to prepare the sampler for generating events, e.g. by
0187    * combining grid files from parallel integration runs.
0188    */
0189   virtual void prepare();
0190 
0191 protected:
0192 
0193   /**
0194    * Access the samplers
0195    */
0196   map<double,Ptr<BinSampler>::ptr>& samplers() { return theSamplers; }
0197 
0198   /**
0199    * Set the last selected bin sampler
0200    */
0201   void lastSampler(Ptr<BinSampler>::tptr s) { theLastSampler = s; }
0202 
0203   /**
0204    * Calculate cross sections from samplers at current state.
0205    */
0206   void currentCrossSections() const;
0207 
0208   /**
0209    * Update the sampler selection
0210    */
0211   void updateSamplers();
0212 
0213 public:
0214 
0215   /** @name Functions used by the persistent I/O system. */
0216   //@{
0217   /**
0218    * Function used to write out object persistently.
0219    * @param os the persistent output stream written to.
0220    */
0221   void persistentOutput(PersistentOStream & os) const;
0222 
0223   /**
0224    * Function used to read in object persistently.
0225    * @param is the persistent input stream read from.
0226    * @param version the version number of the object when written.
0227    */
0228   void persistentInput(PersistentIStream & is, int version);
0229   //@}
0230 
0231   /**
0232    * The standard Init function used to initialize the interfaces.
0233    * Called exactly once for each class by the class description system
0234    * before the main function starts or
0235    * when this class is dynamically loaded.
0236    */
0237   static void Init();
0238 
0239 protected:
0240 
0241   /** @name Clone Methods. */
0242   //@{
0243   /**
0244    * Make a simple clone of this object.
0245    * @return a pointer to the new object.
0246    */
0247   virtual IBPtr clone() const;
0248 
0249   /** Make a clone of this object, possibly modifying the cloned object
0250    * to make it sane.
0251    * @return a pointer to the new object.
0252    */
0253   virtual IBPtr fullclone() const;
0254   //@}
0255 
0256 
0257 // If needed, insert declarations of virtual function defined in the
0258 // InterfacedBase class here (using ThePEG-interfaced-decl in Emacs).
0259 
0260 
0261 protected:
0262 
0263   /**
0264    * Initialize this object after the setup phase before saving an
0265    * EventGenerator to disk.
0266    * @throws InitException if object could not be initialized properly.
0267    */
0268   virtual void doinit();
0269 
0270   /**
0271    * Initialize this object. Called in the run phase just before
0272    * a run begins.
0273    */
0274   virtual void doinitrun();
0275 
0276   /**
0277    * Finalize this object. Called in the run phase just after a
0278    * run has ended. Used eg. to write out statistics.
0279    */
0280   virtual void dofinish();
0281 
0282   /**
0283    * Rebind pointer to other Interfaced objects. Called in the setup phase
0284    * after all objects used in an EventGenerator has been cloned so that
0285    * the pointers will refer to the cloned objects afterwards.
0286    * @param trans a TranslationMap relating the original objects to
0287    * their respective clones.
0288    * @throws RebindException if no cloned object was found for a given
0289    * pointer.
0290    */
0291   virtual void rebind(const TranslationMap & trans);
0292 
0293   /**
0294    * Return a vector of all pointers to Interfaced objects used in this
0295    * object.
0296    * @return a vector of pointers.
0297    */
0298   virtual IVector getReferences();
0299 
0300 private:
0301 
0302   /**
0303    * Whether or not additional information should be printed to cout.
0304    */
0305   bool theVerbose;
0306 
0307   /**
0308    * The XML element containing the grids
0309    */
0310   XML::Element theGrids;
0311 
0312   /**
0313    * The bin sampler to use.
0314    */
0315   Ptr<BinSampler>::ptr theBinSampler;
0316 
0317   /**
0318    * The selector map for the bin samplers.
0319    */
0320   map<double,Ptr<BinSampler>::ptr> theSamplers;
0321 
0322   /**
0323    * The last selected bin sampler.
0324    */
0325   Ptr<BinSampler>::tptr theLastSampler;
0326 
0327   /**
0328    * The integrated cross section
0329    */
0330   mutable CrossSection theIntegratedXSec;
0331 
0332   /**
0333    * The integrated cross section error
0334    */
0335   mutable CrossSection theIntegratedXSecErr;
0336 
0337   /**
0338    * The number of events after which cross sections should truly be
0339    * updated. This is used to prevent exhaustive combination of
0340    * statistics when HepMC events are written out.
0341    */
0342   size_t theUpdateAfter;
0343 
0344   /**
0345    * The number of calls to currentCrossSections since the last
0346    * update.
0347    */
0348   mutable size_t crossSectionCalls;
0349 
0350   /**
0351    * True, if currentCrossSections has been called since the last call
0352    * to generate.
0353    */
0354   mutable bool gotCrossSections;
0355 
0356   /**
0357    * The sum of weights
0358    */
0359   double theSumWeights;
0360 
0361   /**
0362    * The sum of weights squared
0363    */
0364   double theSumWeights2;
0365 
0366   /**
0367    * The number of attempts
0368    */
0369   double theAttempts;
0370 
0371   /**
0372    * The number of accepts
0373    */
0374   double theAccepts;
0375 
0376   /**
0377    * The maximum weight encountered
0378    */
0379   double theMaxWeight;
0380 
0381   /**
0382    * True, if cross sections are to be combined from each sampler
0383    * individually
0384    */
0385   bool theAddUpSamplers;
0386 
0387   /**
0388    * True, if the global maximum weight should be used as
0389    * reference. If not, the maximum weights of individual samplers are
0390    * used, and selection probabilities fro the samplers are adjusted
0391    * accordingly.
0392    */
0393   bool theGlobalMaximumWeight;
0394 
0395   /**
0396    * True, if subprocesses should be selected flat. This is a debug
0397    * flag, cross section information and distributions will not be
0398    * correct.
0399    */
0400   bool theFlatSubprocesses;
0401 
0402   /**
0403    * True, if we are generating events.
0404    */
0405   bool isSampling;
0406 
0407   /**
0408    * A minimum selection probability for each sampler
0409    */
0410   double theMinSelection;
0411 
0412   /**
0413    * True, if information for combining unnormalized runs should be
0414    * printed out
0415    */
0416   bool runCombinationData;
0417 
0418   /**
0419    * True, if we should perform an almost unweighted sampling
0420    */
0421   bool theAlmostUnweighted;
0422 
0423   /**
0424    * Number of points which exceeded the maximum
0425    */
0426   unsigned long maximumExceeds;
0427 
0428   /**
0429    * The average relative deviation from the maximum weight
0430    */
0431   double maximumExceededBy;
0432 
0433   /**
0434    * The correct cross section as one would exspect with
0435    * almostUnweighted. 
0436    */
0437 
0438   double correctWeights;
0439 
0440   /**
0441    * Enhancement factor to the maximum weight.
0442    * This is to get less maximumExceeds. 
0443    */
0444 
0445   double theMaxEnhancement;
0446 
0447   /**
0448    * True, if grids have already been read.
0449    */
0450   bool didReadGrids;
0451 
0452   /**
0453    * True, if parallel subprocess integration should be enabled
0454    */
0455   bool theParallelIntegration;
0456 
0457   /**
0458    * The number of subprocesses to integrate per job
0459    */
0460   unsigned int theIntegratePerJob;
0461 
0462   /**
0463    * The maximum number of integration jobs to be created
0464    */
0465   unsigned int theIntegrationJobs;
0466 
0467   /**
0468    * The number of integration jobs which were actually created
0469    */
0470   unsigned int theIntegrationJobsCreated;
0471   
0472   /**
0473    * Indicate that initialization is only reading a grid.
0474    */
0475   bool justAfterIntegrate;
0476 
0477   /**
0478    * True, if grids should be written at the end of a run
0479    */
0480   bool theWriteGridsOnFinish;
0481 
0482 private:
0483 
0484   /**
0485    * The assignment operator is private and must never be called.
0486    * In fact, it should not even be implemented.
0487    */
0488   GeneralSampler & operator=(const GeneralSampler &) = delete;
0489 
0490 };
0491 
0492 }
0493 
0494 #endif /* Herwig_GeneralSampler_H */