Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // MPISampler.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_MPISampler_H
0010 #define Herwig_MPISampler_H
0011 // This is the declaration of the MPISampler class.
0012 
0013 #include "ThePEG/Handlers/SamplerBase.h"
0014 #include "ThePEG/ACDC/ACDCGen.h"
0015 #include "ThePEG/Repository/RandomGenerator.h"
0016 #include "ThePEG/Repository/UseRandom.h"
0017 #include "ThePEG/Utilities/SimplePhaseSpace.xh"
0018 #include "MPISampler.fh"
0019 
0020 #include "ProcessHandler.h"
0021 
0022 namespace Herwig {
0023 
0024   using namespace ThePEG;
0025 
0026   /**
0027    * This class inherits from SampleBase and implements
0028    * the Auto Compensating Divide-and-Conquer phase space generator,
0029    * ACDCGen. In contrast to a ThePEG::ACDCSampler, this
0030    * class can be called by ProcessHandler.
0031    *
0032    * @see \ref MPISamplerInterfaces "The interfaces"
0033    * defined for MPISampler.
0034    * @see ACDCGen
0035    * @see ProcessHandler
0036    */
0037 
0038 class MPISampler: public SamplerBase {
0039 
0040 public:
0041 
0042   /** Typedef the underlying ACDCGen class. */
0043   typedef ACDCGenerator::ACDCGen<UseRandom,tProHdlPtr> SamplerType;
0044 
0045   /** @name Standard constructors and destructors. */
0046   //@{
0047   /**
0048    * The default constructor.
0049    */
0050   inline MPISampler();
0051 
0052   /**
0053    * The copy constructor.
0054    */
0055   inline MPISampler(const MPISampler &);
0056 
0057   /**
0058    * The destructor.
0059    */
0060   virtual ~MPISampler();
0061   //@}
0062 
0063 public:
0064 
0065   /** @name Virtual functions needed for SamplerBase */
0066   //@{
0067   /**
0068    * Method to set the connected ProcessHandler pointer
0069    * 
0070    */
0071   inline void setProcessHandler(tProHdlPtr mpih);
0072 
0073   /**
0074    * Initialize the sampler, possibly doing presampling of the
0075    * phase space.
0076    */
0077   virtual void initialize();
0078 
0079   /**
0080    * Generarate a new phase space point and return a weight associated
0081    * with it. This weight should preferably be 1.
0082    */
0083   virtual double generate();
0084 
0085   /**
0086    * ACDCSampler is able to sample several different functions
0087    * separately. This function returns the last chosen
0088    * function.
0089    */
0090   virtual int lastBin() const;
0091 
0092   /**
0093    * Reject the last chosen phase space point.
0094    */
0095   virtual void rejectLast();
0096 
0097   /**
0098    * Return the total integrated cross section determined from the
0099    * Monte Carlo sampling so far.
0100    */
0101   virtual CrossSection integratedXSec() const;
0102 
0103   /**
0104    * Return the error on the total integrated cross section determined
0105    * from the Monte Carlo sampling so far.
0106    */
0107   virtual CrossSection integratedXSecErr() const;
0108 
0109   /**
0110    * Return the overestimated integrated cross section.
0111    */
0112   virtual CrossSection maxXSec() const;
0113 
0114   /**
0115    * Return the sum of the weights returned by generate() so far (of
0116    * the events that were not rejeted).
0117    */
0118   virtual double sumWeights() const;
0119 
0120   /**
0121    * Return the sum of the weights squared returned by generate() so far (of
0122    * the events that were not rejeted).
0123    */
0124   virtual double sumWeights2() const;
0125   //@}
0126 
0127 public:
0128 
0129   /** @name Functions used by the persistent I/O system. */
0130   //@{
0131   /**
0132    * Function used to write out object persistently.
0133    * @param os the persistent output stream written to.
0134    */
0135   void persistentOutput(PersistentOStream & os) const;
0136 
0137   /**
0138    * Function used to read in object persistently.
0139    * @param is the persistent input stream read from.
0140    * @param version the version number of the object when written.
0141    */
0142   void persistentInput(PersistentIStream & is, int version);
0143   //@}
0144 
0145   /**
0146    * The standard Init function used to initialize the interfaces.
0147    * Called exactly once for each class by the class description system
0148    * before the main function starts or
0149    * when this class is dynamically loaded.
0150    */
0151   static void Init();
0152 
0153 protected:
0154 
0155   /** @name Clone Methods. */
0156   //@{
0157   /**
0158    * Make a simple clone of this object.
0159    * @return a pointer to the new object.
0160    */
0161   inline virtual IBPtr clone() const;
0162 
0163   /** Make a clone of this object, possibly modifying the cloned object
0164    * to make it sane.
0165    * @return a pointer to the new object.
0166    */
0167   inline virtual IBPtr fullclone() const;
0168 
0169   //@}
0170 
0171 protected:
0172 
0173   /** @name Standard Interfaced functions. */
0174   //@{
0175   /**
0176    * Check sanity of the object during the setup phase.
0177    */
0178   inline virtual void doupdate();
0179 
0180   /**
0181    * Initialize this object after the setup phase before saving an
0182    * EventGenerator to disk.
0183    * @throws InitException if object could not be initialized properly.
0184    */
0185   inline virtual void doinit();
0186 
0187   /**
0188    * Initialize this object. Called in the run phase just before
0189    * a run begins.
0190    */
0191   virtual void doinitrun();
0192 
0193   /**
0194    * Finalize this object. Called in the run phase just after a
0195    * run has ended. Used eg. to write out statistics.
0196    */
0197   virtual void dofinish();
0198 
0199 
0200   /**
0201    * Rebind pointer to other Interfaced objects. Called in the setup phase
0202    * after all objects used in an EventGenerator has been cloned so that
0203    * the pointers will refer to the cloned objects afterwards.
0204    * @param trans a TranslationMap relating the original objects to
0205    * their respective clones.
0206    * @throws RebindException if no cloned object was found for a given pointer.
0207    */
0208   inline virtual void rebind(const TranslationMap & trans)
0209    ;
0210 
0211   /**
0212    * Return a vector of all pointers to Interfaced objects used in this object.
0213    * @return a vector of pointers.
0214    */
0215   inline virtual IVector getReferences();
0216   //@}
0217 
0218 private:
0219 
0220   /**
0221    * The actual sampler object.
0222    */
0223   SamplerType theSampler;
0224 
0225   /**
0226    * The ProcessHandler that calls us
0227    */
0228   tProHdlPtr theProcessHandler;
0229 
0230   /**
0231    * The smallest possible division allowed.
0232    */
0233   double theEps;
0234 
0235   /**
0236    * The factor controlling the loss of efficiency when compensating.
0237    */
0238   double theMargin;
0239 
0240   /**
0241    * The number of points to use to find initial average.  
0242    */
0243   int theNTry;
0244 
0245 protected:
0246 
0247   /** @cond EXCEPTIONCLASSES */
0248   /** Exception class used by ACDCSampler if the undelying ACDCGen was
0249       still in a compensating mode when the run was finished */
0250   struct ACDCStillCompensating: public Exception {};
0251 
0252   /** Exception class used by ACDCSampler if a EventHandler
0253       was not able to produce a non-zero cross section. */
0254   struct EventInitNoXSec: public Exception {};
0255 
0256   /** Exception class used if ACDCSampler was not able to produce a
0257       phase space point within the maximum allowed number of
0258       attempts. */
0259   struct EventLoopException: public Exception {};
0260   /** @endcond */
0261 
0262 private:
0263 
0264   /**
0265    *  Private and non-existent assignment operator.
0266    */
0267   MPISampler & operator=(const MPISampler &) = delete;
0268 
0269 };
0270 
0271 }
0272 
0273 namespace ACDCGenerator {
0274 
0275 /** @cond TRAITSPECIALIZATIONS */
0276 
0277 /** Specialized Traits class to define the interface to the
0278  * EventHandler object to be sampled by ACDCGen.
0279  */
0280 template <>
0281 struct ACDCFncTraits<Herwig::tProHdlPtr>: public ACDCTraitsType {
0282   /** Convenient typdef. */
0283   typedef Herwig::tProHdlPtr tProHdlPtr;
0284   /**
0285    * Call a function to be sampled by ACDCGen.
0286    * @return <code>(*f)(x)</code>.
0287    */
0288   static inline double value(const tProHdlPtr & mpih, const DVector & x) {
0289     using namespace ThePEG::Units;
0290     try {
0291       return mpih->dSigDR(x)/nanobarn;
0292     }
0293     catch ( ThePEG::ImpossibleKinematics & v ) {
0294       breakThePEG();
0295     }
0296     catch ( std::exception & e ) {
0297       breakThePEG();
0298     }
0299     catch ( ... ) {
0300       breakThePEG();
0301     }
0302     return 0.0;
0303   }
0304 
0305 };
0306 
0307 /** Specialized Traits class to inform ACDCGen how to use the
0308     static UseRandom class. */
0309 template <>
0310 struct ACDCRandomTraits<ThePEG::UseRandom>: public ACDCTraitsType {
0311   /** Convenient typedef. */
0312   typedef ThePEG::UseRandom UseRandom;
0313 
0314   /**
0315    * Return a flat random number in the interval ]0,1[.
0316    */
0317   static inline double rnd(UseRandom *) { return UseRandom::rnd(); }
0318 
0319   /**
0320    * Return a flat random number in the interval ]\a xl,\a xu[.
0321    */
0322   static inline double rnd(UseRandom * r, double xl, double xu) {
0323     return xl + (xu - xl)*rnd(r);
0324   }
0325 
0326   /**
0327    * Generate a set of random numbers.
0328    * @param r the random generator.
0329    * @param l an input iterator giving the lower limit of the interval
0330    * of the first requested random number.
0331    * @param lend an input iterator marking the end of the range of
0332    * requested random numbers.
0333    * @param u an input iterator giving the upper limit of the interval
0334    * of the first requested random number.
0335    * @param res the ouput iterator used to output the random numbers.
0336    */
0337   template <typename InputIterator, typename OutputIterator>
0338   static inline void rnd(UseRandom * r,
0339              InputIterator l, InputIterator lend,
0340              InputIterator u, OutputIterator res) {
0341     for ( ; l != lend; ++l ) *res++ = *l + (*u++ - *l)*rnd(r);
0342   }
0343 
0344   /**
0345    * Generate \a D random numbers. The numbers are put into the
0346    * OutputIterator \a res.
0347    */
0348   template <typename OutputIterator>
0349   static inline void rnd(UseRandom * r, int D, OutputIterator res) {
0350     for ( int d = 0; d < D; ++d ) *res++ = rnd(r);
0351   }
0352 
0353   /**
0354    * Return true with probability \a x.
0355    */
0356   static inline bool rndBool(UseRandom, double x) {
0357     return UseRandom::rndbool(x);
0358   }
0359 
0360   /**
0361    * Return true with probability \a x(\a x + \a y).
0362    */
0363   static inline bool rndBool(UseRandom *, double x, double y) {
0364     return UseRandom::rndbool(x, y);
0365   }
0366 
0367   /**
0368    * Return a random integer in the interval [0,\a x[.
0369    */
0370   static inline long rndInt(UseRandom *, long x) {
0371     return UseRandom::irnd(x);
0372   }
0373 
0374 };
0375 
0376 /** @endcond */
0377 
0378 }
0379 
0380 #include "MPISampler.icc"
0381 
0382 #endif /* Herwig_MPISampler_H */