Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // BinSampler.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_BinSampler_H
0010 #define Herwig_BinSampler_H
0011 //
0012 // This is the declaration of the BinSampler class.
0013 //
0014 
0015 #include "ThePEG/Handlers/StandardEventHandler.h"
0016 #include "ThePEG/Utilities/Exception.h"
0017 #include "ThePEG/Repository/UseRandom.h"
0018 
0019 #include "MultiIterationStatistics.h"
0020 #include "Remapper.h"
0021 
0022 namespace Herwig {
0023 
0024 using namespace ThePEG;
0025 
0026 class GeneralSampler;
0027 
0028 /**
0029  * \ingroup Matchbox
0030  * \author Simon Platzer
0031  *
0032  * \brief BinSampler samples XCombs bins. This default implementation
0033  * performs flat MC integration.
0034  *
0035  * @see \ref BinSamplerInterfaces "The interfaces"
0036  * defined for BinSampler.
0037  */
0038 class BinSampler: public Herwig::MultiIterationStatistics {
0039 
0040 public:
0041 
0042   /**
0043    * The default constructor.
0044    */
0045   BinSampler();
0046 
0047 public:
0048 
0049   /**
0050    * Clone this object.
0051    */
0052   Ptr<BinSampler>::ptr cloneMe() const {
0053     return dynamic_ptr_cast<Ptr<BinSampler>::ptr>(clone());
0054   }
0055 
0056 public:
0057 
0058   /**
0059    * Evaluate the cross section
0060    */
0061   double evaluate(vector<double> p,
0062           bool remap = true);
0063 
0064   /**
0065    * Return the bias with which this sampler is selected. The sampler
0066    * needs to divide out this bias in its weight calculation.
0067    */
0068   double bias() const { return theBias; }
0069 
0070   /**
0071    * Set the bias with which this sampler is selected.
0072    */
0073   void bias(double b) { theBias = b; }
0074 
0075   /**
0076    * Set the event handler
0077    */
0078   void eventHandler(tStdEHPtr eh) { theEventHandler = eh; }
0079 
0080   /**
0081    * Return the event handler
0082    */
0083   tStdEHPtr eventHandler() const { return theEventHandler; }
0084 
0085   /**
0086    * Set the containing sampler
0087    */
0088   void sampler(Ptr<GeneralSampler>::tptr);
0089 
0090   /**
0091    * Get the containing sampler
0092    */
0093   Ptr<GeneralSampler>::tptr sampler() const;
0094 
0095   /**
0096    * Return the bin
0097    */
0098   int bin() const { return theBin; }
0099 
0100   /**
0101    * Set the bin
0102    */
0103   void bin(int b) { theBin = b; }
0104 
0105   /**
0106    * Return a string describing the process handled by this sampler.
0107    */
0108   string process() const;
0109 
0110   /**
0111    * Return a short string describing the process handled by this sampler.
0112    */
0113   string shortprocess() const;
0114   
0115   /**
0116    * Return a string identifying the process handled by this sampler.
0117    */
0118   string id() const;
0119 
0120   /**
0121    * Return the last generated point.
0122    */
0123   const vector<double>& lastPoint() const { return theLastPoint; }
0124 
0125   /**
0126    * Access the last generated point.
0127    */
0128   vector<double>& lastPoint() { return theLastPoint; }
0129 
0130   /**
0131    * Return the reference weight to be used
0132    */
0133   double referenceWeight() const { return theReferenceWeight; }
0134 
0135   /**
0136    * Set the reference weight to be used
0137    */
0138   void referenceWeight(double w) { theReferenceWeight = w; }
0139 
0140   /**
0141    * Return true, if this sampler can provide unweighted events; if
0142    * the proposal density is not an overestimate, weights larger than
0143    * one can be generated, the handling of these points being subject
0144    * to the GeneralSampler class.
0145    */
0146   virtual bool canUnweight() const { return true; }
0147 
0148   /**
0149    * Return true, if this sampler adapts on the fly while generating
0150    * events. Cross sections in the GeneralSampler class are calculated
0151    * from adding up the cross sections quoted by individual samplers.
0152    */
0153   virtual bool adaptsOnTheFly() const { return false; }
0154 
0155   /**
0156    * If this sampler features a compensation algorithm, return true if
0157    * more events need to be generated to finish the compensation.
0158    */
0159   virtual bool compensating() const { return false; }
0160 
0161   /**
0162    * Return true, if weighted events should be generated
0163    */
0164   bool weighted() const { return theWeighted; }
0165 
0166   /**
0167    * Indicate that weighted events should be generated
0168    */
0169   void doWeighted(bool yes = true) { theWeighted = yes; }
0170 
0171   /**
0172    * Exception to be thrown if cross section information should be updated.
0173    */
0174   struct NextIteration {};
0175 
0176   /**
0177    * Generate the next point and return its weight; store the point in
0178    * lastPoint().
0179    */
0180   virtual double generate();
0181 
0182   /**
0183    * Fill and finalize the remappers present
0184    */
0185   void fillRemappers(bool progress);
0186 
0187   /**
0188    * Write remappers to grid file
0189    */
0190   void saveRemappers() const;
0191 
0192   /**
0193    * Write integration data to grid files
0194    */
0195   void saveIntegrationData() const;
0196 
0197   /**
0198    * Save grid data
0199    */
0200   virtual void saveGrid() const {}
0201 
0202   /**
0203    * Read integration data from grid files
0204    */
0205   void readIntegrationData();
0206 
0207   /**
0208    * Read remappers from grid file
0209    */
0210   void setupRemappers(bool progress);
0211 
0212   /**
0213    * Run a single iteration of n points, optionally printing a
0214    * progress bar to cout. Calls generate n times.
0215    */
0216   void runIteration(unsigned long n, bool progress);
0217 
0218   /**
0219    * Adapt this sampler after an iteration has been run
0220    */
0221   virtual void adapt() {}
0222 
0223   /**
0224    * Initialize this bin sampler. This default version calls runIteration.
0225    */
0226   virtual void initialize(bool progress);
0227 
0228   /**
0229    * Return true, if this sampler has already been initialized.
0230    */
0231   bool initialized() const { return theInitialized; }
0232 
0233   /**
0234    * Indicate that this sampler has already been initialized.
0235    */
0236   void isInitialized() { theInitialized = true; }
0237 
0238   /**
0239    * Return true, if integration has already been performed
0240    */
0241   bool integrated() const { return theIntegrated; }
0242 
0243   /**
0244    * Return true, if remappers have been set up
0245    */
0246   bool remappersFilled() const { return theRemappersFilled; }
0247 
0248   /**
0249    * Return true, if grid data exists for this sampler.
0250    */
0251   virtual bool existsGrid() const { return false; }
0252 
0253   /**
0254    * Return true, if this sampler has already read grid data.
0255    */
0256   bool hasGrids() const { return theHasGrids; }
0257 
0258   /**
0259    * Indicate that this sampler has already read grid data.
0260    */
0261   void didReadGrids() { theHasGrids = true; }
0262 
0263   /**
0264    * Finalize this sampler.
0265    */
0266   virtual void finalize(bool); 
0267 
0268   /**
0269    * Return the total integrated cross section determined from the
0270    * Monte Carlo sampling so far.
0271    */
0272   virtual CrossSection integratedXSec() const {
0273     return averageWeight()*nanobarn;
0274   }
0275 
0276   /**
0277    * Return the error on the total integrated cross section determined
0278    * from the Monte Carlo sampling so far.
0279    */
0280   virtual CrossSection integratedXSecErr() const {
0281     return sqrt(abs(averageWeightVariance()))*nanobarn;
0282   }
0283 
0284    /**
0285    * Define the key for the collinear subtraction data.
0286    */
0287 
0288   
0289   struct RandomNumberHistogram {
0290 
0291     /**
0292      * The lower bound
0293      */
0294     double lower;
0295 
0296     /**
0297      * The bins, indexed by upper bound.
0298      */
0299     map<double,double > bins;
0300     
0301     map<double,double > binsw1;
0302     /**
0303      * Constructor
0304      */
0305     RandomNumberHistogram(double low = 0.0, 
0306              double up = 1., 
0307              unsigned int nbins = 20);
0308 
0309     /**
0310      * Book an event.
0311      */
0312     void book(double inv, double weight) {
0313       map<double,double>::iterator b =  bins.upper_bound(inv);
0314       if ( b == bins.end() ) return;
0315       b->second = b->second+weight;
0316       map<double,double>::iterator b2 = binsw1.upper_bound(inv);
0317       if ( b2 == binsw1.end() ) return;
0318       b2->second = b2->second+1.;
0319 
0320     }
0321 
0322     /**
0323      * Write to file given name and invariant.
0324      */
0325     void dump(const std::string& folder,const std::string& prefix, const std::string& process,const int NR)const;
0326 
0327 
0328   };
0329      
0330   typedef pair<string,size_t > RandomNumberIndex;
0331   
0332   map<RandomNumberIndex,pair<RandomNumberHistogram,double> > RandomNumberHistograms;
0333 
0334 public:
0335 
0336   /**
0337    * Return the dimension.
0338    */
0339   int dimension() const { return theEventHandler->nDim(bin()); }
0340 
0341   /**
0342    * Return the number of points to be used for initial integration.
0343    */
0344   unsigned long initialPoints() const { return theInitialPoints; }
0345 
0346   /**
0347    * Set the number of points to be used for initial integration.
0348    */
0349   void initialPoints(unsigned long n) { theInitialPoints = n; }
0350 
0351   /**
0352    * Return the number of iterations to be considered for initialization.
0353    */
0354   size_t nIterations() const { return theNIterations; }
0355 
0356   /**
0357    * Set the number of iterations to be considered for initialization.
0358    */
0359   void nIterations(size_t n) { theNIterations = n; }
0360 
0361   /**
0362    * Set the factor to enhance the number of points for the next
0363    * iteration.
0364    */
0365   void enhancementFactor(double f) { theEnhancementFactor = f; }
0366 
0367   /**
0368    * Return the factor to enhance the number of points for the next
0369    * iteration.
0370    */
0371   double enhancementFactor() const { return theEnhancementFactor; }
0372   
0373   /**
0374    * Return the folder for the random number plots.
0375    */
0376   string randomNumberString() const {return theRandomNumbers;}
0377 
0378   /**
0379    * In the AlmostUnweighted mode we do not need to unweight 
0380    * the events to the reference weight. 
0381    * Kappa reduces effectivly the reference weight.
0382    * This can be used for processes, where unweighting 
0383    * is hardly feasable.
0384    */
0385   double kappa() const {return theKappa;}
0386 
0387 public:
0388 
0389   /** @name Functions used by the persistent I/O system. */
0390   //@{
0391   /**
0392    * Function used to write out object persistently.
0393    * @param os the persistent output stream written to.
0394    */
0395   void persistentOutput(PersistentOStream & os) const;
0396 
0397   /**
0398    * Function used to read in object persistently.
0399    * @param is the persistent input stream read from.
0400    * @param version the version number of the object when written.
0401    */
0402   void persistentInput(PersistentIStream & is, int version);
0403   //@}
0404 
0405   /**
0406    * The standard Init function used to initialize the interfaces.
0407    * Called exactly once for each class by the class description system
0408    * before the main function starts or
0409    * when this class is dynamically loaded.
0410    */
0411   static void Init();
0412 
0413 protected:
0414 
0415   /** @name Clone Methods. */
0416   //@{
0417   /**
0418    * Make a simple clone of this object.
0419    * @return a pointer to the new object.
0420    */
0421   virtual IBPtr clone() const;
0422 
0423   /** Make a clone of this object, possibly modifying the cloned object
0424    * to make it sane.
0425    * @return a pointer to the new object.
0426    */
0427   virtual IBPtr fullclone() const;
0428   //@}
0429 
0430 
0431 // If needed, insert declarations of virtual function defined in the
0432 // InterfacedBase class here (using ThePEG-interfaced-decl in Emacs).
0433 
0434 private:
0435 
0436   /**
0437    * The bias with which this sampler is selected.
0438    */
0439   double theBias;
0440 
0441   /**
0442    * True, if weighted events should be generated
0443    */
0444   bool theWeighted;
0445 
0446   /**
0447    * The number of points to use for initial integration.
0448    */
0449   unsigned long theInitialPoints;
0450 
0451   /**
0452    * The number of iterations to be considered for initialization.
0453    */
0454   size_t theNIterations;
0455 
0456   /**
0457    * Factor to enhance the number of points for the next iteration.
0458    */
0459   double theEnhancementFactor;
0460 
0461   /**
0462    * Switch to count only non zero weights in presampling.
0463    */
0464 
0465   bool theNonZeroInPresampling; 
0466   
0467   /**
0468    * Switch to require that we get half of the points 
0469    * in each iteration below the maximum weight of the iteration.
0470    */    
0471     
0472   bool theHalfPoints;
0473 
0474   /**
0475    * The maximum number of allowed new maxima, 
0476    * in combination with HalfPoints, in order to prevent unstable
0477    * processes.
0478    */
0479   int theMaxNewMax;
0480 
0481   /**
0482    * The reference weight to be used
0483    */
0484   double theReferenceWeight;
0485 
0486   /**
0487    * The bin to be sampled.
0488    */
0489   int theBin;
0490 
0491   /**
0492    * Wether or not this sampler has already been initialized.
0493    */
0494   bool theInitialized;
0495 
0496   /**
0497    * The last generated point.
0498    */
0499   vector<double> theLastPoint;
0500 
0501   /**
0502    * The event handler to be used.
0503    */
0504   tStdEHPtr theEventHandler;
0505 
0506   /**
0507    * The containing sampler
0508    */
0509   Ptr<GeneralSampler>::tptr theSampler;
0510   
0511   /**
0512    * Folder for the random number plots.
0513    */
0514   string theRandomNumbers;
0515 
0516   /**
0517    * Remapper objects indexed by dimension
0518    */
0519   map<size_t,Remapper> remappers;
0520 
0521   /**
0522    * The number of points to be used for initial filling of the remappers
0523    */
0524   unsigned long theRemapperPoints;
0525 
0526   /**
0527    * True if channels should get a remapper
0528    */
0529   bool theRemapChannelDimension;
0530 
0531   /**
0532    * The number of bins to be used for luminosity dimensions
0533    */
0534   unsigned long theLuminosityMapperBins;
0535 
0536   /**
0537    * The number of bins to be used for any other dimension
0538    */
0539   unsigned long theGeneralMapperBins;
0540 
0541   /**
0542    * The minimum selection probability for remapper bins
0543    */
0544   double theRemapperMinSelection;
0545 
0546   /**
0547    * True, if integration has already be performed
0548    */
0549   bool theIntegrated;
0550 
0551   /**
0552    * True, if remappers have been set up
0553    */
0554   bool theRemappersFilled;
0555 
0556   /**
0557    * True, if this sampler has already read grid data.
0558    */
0559   bool theHasGrids;
0560 
0561 
0562 
0563   /**
0564    * In the AlmostUnweighted mode we do not need to unweight 
0565    * the events to the reference weight. 
0566    * Kappa reduces effectivly the reference weight.
0567    * This can be used for processes, where unweighting 
0568    * is hardly feasable.
0569    */
0570   double theKappa;
0571 
0572 private:
0573 
0574   /**
0575    * The assignment operator is private and must never be called.
0576    * In fact, it should not even be implemented.
0577    */
0578   BinSampler & operator=(const BinSampler &) = delete;
0579 
0580 };
0581 
0582 }
0583 
0584 #endif /* Herwig_BinSampler_H */