Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:31

0001 // -*- C++ -*-
0002 //
0003 // CurrentGenerator.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG 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 ThePEG_CurrentGenerator_H
0010 #define ThePEG_CurrentGenerator_H
0011 // This is the declaration of the CurrentGenerator class.
0012 
0013 #include "ThePEG/Repository/EventGenerator.h"
0014 #include "CurrentGenerator.fh"
0015 
0016 namespace ThePEG {
0017 
0018 /**
0019  * This CurrentGenerator class keeps a static stack of EventGenerators
0020  * which can be used anywhere by any class. When an EventGenerator is
0021  * initialized or run it adds itself to the stack which can be used by
0022  * any other object being initialized or run through the static
0023  * functions of the CurrentGenerator class. If someone
0024  * needs to use an alternative EventGenerator object a new
0025  * CurrentGenerator object can be constructed with a
0026  * pointer to the desired EventGenerator object as argument and that
0027  * object will the be used by the static CurrentGenerator
0028  * functions until the CurrentGenerator object is destructed.
0029  *
0030  * @see EventGenerator
0031  * 
0032  */
0033 class CurrentGenerator {
0034 
0035 public:
0036 
0037   /**
0038    * Default constructor does nothing.
0039    */
0040   CurrentGenerator() : generatorPushed(false) {}
0041 
0042   /**
0043    * Copy-constructor does nothing.
0044    */
0045   CurrentGenerator(const CurrentGenerator &)
0046     : generatorPushed(false) {}
0047 
0048   /**
0049    * Construct a new object specifying a new EventGenerator, \a eg, to
0050    * be used during this objects lifetime.
0051    */
0052   CurrentGenerator(const EGPtr & eg) : generatorPushed(false) {
0053     if ( eg ) {
0054       theGeneratorStack.push_back(eg);
0055       generatorPushed = true;
0056     }
0057   }
0058 
0059   /**
0060    * The destructor removing the EventGenerator specified in the
0061    * constructor from the stack.
0062    */
0063   ~CurrentGenerator() {
0064     if ( generatorPushed ) theGeneratorStack.pop_back();
0065   }
0066 
0067 public:
0068 
0069   /**
0070    * Returns true if there is no currently chosen EventGenerator
0071    * object.
0072    */
0073   static bool isVoid() {
0074     return theGeneratorStack.empty() || !(theGeneratorStack.back());
0075   }
0076 
0077   /**
0078    * Return a reference to the currently chosen EventGenerator object.
0079    */
0080   static EventGenerator & current() {
0081     return *theGeneratorStack.back();
0082   }
0083 
0084   /**
0085    * Return a reference to the currently chosen object.
0086    */
0087   EventGenerator & operator*() const {
0088     return *theGeneratorStack.back();
0089   }
0090 
0091   /**
0092    * Return a pointer to the currently chosen object.
0093    */
0094   EventGenerator * operator->() const {
0095     return theGeneratorStack.back();
0096   }
0097 
0098   /**
0099    *  Pointer to the stack
0100    */
0101   static EventGenerator * ptr() {
0102     return theGeneratorStack.back();
0103   }
0104 
0105   /**
0106    * Test for existance
0107    */
0108   operator bool() const {
0109     return ptr();
0110   }
0111 
0112   /**
0113    * Test for existance
0114    */
0115   bool operator!() const {
0116     return !ptr();
0117   }
0118 
0119   /**
0120    * Return a pointer to the standard model parameters used by the
0121    * current generator.
0122    */
0123   static tSMPtr standardModel() {
0124     return current().standardModel();
0125   }
0126 
0127   /**
0128    * Return a pointer to the strategy object containing eg. a set of
0129    * non-default particles to be used by the current generator.
0130    */
0131   static tStrategyPtr strategy() { return current().strategy(); }
0132 
0133   /**
0134    * Get the current standard output stream. Return a reference to the
0135    * stream connected to the file for general output of the current
0136    * generator. If no file is connected, the BaseRepository::cout()
0137    * will be used instead.
0138    */
0139   static ostream & out() { return current().out(); }
0140 
0141   /**
0142    * Get the current standard log stream. Return a reference to the
0143    * stream connected to the file for logging information of the
0144    * current generator. If no file is connected, the
0145    * BaseRepository::clog() will be used instead.
0146    */
0147   static ostream & log() { return current().log(); }
0148 
0149   /**
0150    * Get the current standard ref stream. Return a reference to the
0151    * stream connected to the file for references from used objects of
0152    * the current generator. If no file is connected, the
0153    * BaseRepository::cout() will be used instead.
0154    */
0155   static ostream & ref() { return current().ref(); }
0156 
0157   /**
0158    * Get object. Return a garbage collected pointer to a given object
0159    * in the current EventGenerator. If the object is not found, a null
0160    * pointer will be returned.
0161    */
0162   template <typename T>
0163   static typename Ptr<T>::pointer getPtr(const T & t) {
0164     return current().getPtr(t);
0165   }
0166 
0167   /**
0168    * Get object. Return a pointer to an object present in the current
0169    * EventGenerator given its full name. Return the null pointer if
0170    * non-existent.
0171    */
0172   static IBPtr getPointer(string name) {
0173     return current().getPointer(name);
0174   }
0175 
0176   /**
0177    * Get object. Return a pointer to an object of type T present in
0178    * the current EventGenerator given its full name. Return the null
0179    * pointer if non-existent.
0180    */
0181   template <typename T>
0182   static typename Ptr<T>::pointer getObject(string name) {
0183     return current().getObject<T>(name);
0184   }
0185 
0186   /**
0187    * Get default object. Return the default object for class T in the
0188    * current EventGenerator. Returns the null pointer if non-existent.
0189    */
0190   template <typename T>
0191   static typename Ptr<T>::pointer getDefault() {
0192     return current().getDefault<T>();
0193   }
0194 
0195 public:
0196 
0197   /**
0198    * Class used to temporarily redirect a given ostream to the misc()
0199    * stream of the current EventGenerator.
0200    */
0201   class Redirect {
0202 
0203   public:
0204 
0205     /**
0206      * Constructor taking the stream to be redirected as input. If the
0207      * \a internal flag false the output will be stored in the Event
0208      * Generator and written to the log file in the end of the run. If
0209      * \internal is true the output is instead stored internally in
0210      * this object and is accessible through the str() function until
0211      * the object is destroyed.
0212      */
0213     Redirect(ostream & os, bool internal = false)
0214       : theStream(&os), theBuffer(os.rdbuf()) {
0215       if ( internal ) theStream->rdbuf(intStream.rdbuf());
0216       else if ( !current().useStdOut() )
0217     theStream->rdbuf(current().misc().rdbuf());
0218     }
0219 
0220     /**
0221      * The destructor which restores the original destination of the
0222      * stream.
0223      */
0224     ~Redirect() {
0225       theStream->rdbuf(theBuffer);
0226     }
0227 
0228     /**
0229      * If output is stored internally, acces what has been written so
0230      * far.
0231      */
0232     string str() const {
0233       return intStream.str();
0234     }
0235 
0236     /**
0237      * The stream which is redirected.
0238      */
0239     ostream * theStream;
0240 
0241     /**
0242      * The original buffer of the redirected stream.
0243      */
0244     std::streambuf * theBuffer;
0245 
0246     /**
0247      * An internal buffer, the content of which will be discarded when
0248      * the this object is destructed.
0249      */
0250     ostringstream intStream;
0251 
0252   };
0253 
0254 private:
0255 
0256   /**
0257    * The stack of EventGenerators requested.
0258    */
0259   static vector<EGPtr> theGeneratorStack;
0260 
0261   /**
0262    * True if this object is responsible for pushing a EventGenerator
0263    * onto the stack.
0264    */
0265   bool generatorPushed;
0266 
0267 private:
0268 
0269   /**
0270    *  Private and non-existent assignment operator.
0271    */
0272   CurrentGenerator & operator=(const CurrentGenerator &) = delete;
0273 
0274 };
0275 
0276 }
0277 
0278 #endif /* ThePEG_CurrentGenerator_H */