Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // EventHandler.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_EventHandler_H
0010 #define ThePEG_EventHandler_H
0011 // This is the declaration of the EventHandler class.
0012 
0013 #include "ThePEG/Handlers/HandlerBase.h"
0014 #include "ThePEG/Handlers/HandlerGroup.h"
0015 #include "ThePEG/Handlers/StepHandler.h"
0016 #include "ThePEG/EventRecord/Event.h"
0017 #include "ThePEG/Handlers/LastXCombInfo.h"
0018 #include "ThePEG/Handlers/SubProcessHandler.fh"
0019 #include "ThePEG/Cuts/Cuts.fh"
0020 #include "EventHandler.fh"
0021 
0022 namespace ThePEG {
0023 
0024 /**
0025  * The EventHandler is the base class used to implement event handlers
0026  * in ThePEG. Objects of this class is assigned to an EventGenerator
0027  * object which supervises a run. This base class is not able to
0028  * generate complete events, although it does have a virtual
0029  * generateEvent(). If the EventGenerator to which an EventGenerator
0030  * is assinged is asked to generate a full event, it will call the
0031  * generateEvent() function which will write an error message and
0032  * abort the run.
0033  *
0034  * Objects of this base class can, however, be used to administer the
0035  * evolution of a partially generated event supplied from the
0036  * outside. To specify this event evolution the EventHandler maintains
0037  * five groups of so-called StepHandlers implemented as
0038  * HandlerGroups. Each group have a main step handler:
0039  * SubProcessHandler, CascadeHandler, MultipleInteractionHandler,
0040  * HadronizationHandler and DecayHandler respectively, whereof the
0041  * first group only uses the post-handler part of the group.
0042  *
0043  * The EventHandler class inherits from the LastXCombInfo class to
0044  * have easy interface to the information in the last selected XComb
0045  * which carries information about the hard sub-process in the event.
0046  *
0047  * If a sub-class implements the generation of sub-processes and thus
0048  * becomes a full event handler it should implement the
0049  * generateEvent() function appropriately. It should also set the flag
0050  * warnIncomplete to false, to avoid warnings when initialized as the main
0051  * EventHandler of an Eventgenerator.
0052  *
0053  * @see \ref EventHandlerInterfaces "The interfaces" defined for EventHandler.
0054  * @see Collision
0055  * @see StepHandler
0056  * @see HandlerGroup
0057  * @see SubProcessHandler
0058  * @see CascadeHandler
0059  * @see MultipleInteractionHandler
0060  * @see HadronizationHandler
0061  * @see DecayHandler
0062  */
0063 class EventHandler: public HandlerBase, public LastXCombInfo<> {
0064 
0065 public:
0066 
0067   /** Enumerate the different levels of consistency checking. */
0068   enum ConsistencyLevel {
0069     clNoCheck,        /**< Do not perform consistency checks. */
0070     clCollision,      /**< Check every Collision. */
0071     clStep,           /**< Check every Step. */
0072     clPrintCollision, /**< Check every Collision. Print event if inconsistent.*/
0073     clPrintStep       /**< Check every Step. Print event if inconsistent. */
0074   };
0075 
0076   /** A vector of <code>HandlerGroup</code>s. */
0077   typedef vector<HandlerGroupBase *> GroupVector;
0078 
0079 public:
0080 
0081   /** @name Standard constructors and destructors. */
0082   //@{
0083   /**
0084    * Default constructor.
0085    */
0086   EventHandler(bool warnincomplete = true);
0087 
0088   /**
0089    * Copy-constructor.
0090    */
0091   EventHandler(const EventHandler &);
0092 
0093   /**
0094    * Destructor.
0095    */
0096   virtual ~EventHandler();
0097   //@}
0098 
0099 public:
0100 
0101   /** @name Main functions, some of which may be overridden by subclasses. */
0102   //@{
0103   /**
0104    * Initialize this event handler and all related objects needed to
0105    * generate events.
0106    */
0107   virtual void initialize();
0108 
0109   /**
0110    * Generate an event. This base class is not capable of generating
0111    * complete events and calling this function will result in an
0112    * exception. Sub-classes which are capable of generating complete
0113    * events from scratch must override this function.
0114    */
0115   virtual EventPtr generateEvent();
0116 
0117   /**
0118    * Generate an Event, where the initial state is supplied
0119    * from the outside.
0120    * @return a pointer to the generated Event.
0121    */
0122   tEventPtr generateEvent(tEventPtr e);
0123 
0124   /**
0125    * Generate an Event, where the initial state is supplied as a
0126    * single step from the outside.
0127    * @return a pointer to the generated Event.
0128    */
0129   tEventPtr generateEvent(tStepPtr s);
0130 
0131   /**
0132    * Continue generating an event if the generation has been stopped
0133    * before finishing.
0134    */
0135   virtual EventPtr continueEvent();
0136 
0137   /**
0138    * Continue the generation of a Collision. Used if the generation
0139    * was previously interrupted.
0140    */
0141   tCollPtr continueCollision();
0142 
0143   /**
0144    * Clear all step handlers, making the handler ready for a new event.
0145    */
0146   void clearEvent();
0147 
0148   /**
0149    * Change the XComb object
0150    */
0151   virtual void select(tXCombPtr newXComb);
0152 
0153   /**
0154    * Returns true if there are no step handlers left to apply to the
0155    * current event;
0156    */
0157   virtual bool empty() const;
0158 
0159   /**
0160    * Write out accumulated statistics about intergrated cross sections
0161    * and stuff.
0162    */
0163   virtual void statistics(ostream &) const;
0164 
0165   /**
0166    * Histogram scale. A histogram bin which has been filled with the
0167    * weights associated with the Event objects should be scaled by
0168    * this factor to give the correct cross section. This version of
0169    * the function will produce an error message. It is up to a
0170    * sub-class able to generate full events to return the correct
0171    * value.
0172    */
0173   virtual CrossSection histogramScale() const;
0174 
0175   /**
0176    * The total integrated cross section of the processes generated in
0177    * this run. This version of the function will produce an error
0178    * message. It is up to a sub-class able to generate full events to
0179    * return the correct value.
0180    * @return 0 if no integrated cross section could be estimated.
0181    */
0182   virtual CrossSection integratedXSec() const;
0183 
0184   /**
0185    * The estimated error in the total integrated cross section of the
0186    * processes generated in this run. This version of the function
0187    * will produce an error message. It is up to a sub-class able to
0188    * generate full events to return the correct value.
0189    * @return 0 if no integrated cross section error could be estimated.
0190    */
0191   virtual CrossSection integratedXSecErr() const;
0192   //@}
0193 
0194   /** @name Simple access functions. */
0195   //@{
0196   /**
0197    * Return the maximum number attemts allowed to select a sub-process
0198    * for each event.
0199    */
0200   long maxLoop() const { return theMaxLoop; }
0201 
0202   /**
0203    * The pair of incoming particle types. These are null if not set by
0204    * a subclass.
0205    */
0206   const cPDPair & incoming() const { return theIncoming; }
0207 
0208   /**
0209    * Access the luminosity function.
0210    */
0211   const LuminosityFunction & lumiFn() const { return *theLumiFn; }
0212 
0213   /**
0214    * Access the luminosity function.
0215    */
0216   tcLumiFnPtr lumiFnPtr() const{ return theLumiFn; }
0217 
0218   /**
0219    * Access to the luminosity function.
0220    */
0221   tLumiFnPtr lumiFnPtr(){ return theLumiFn; }
0222 
0223   /**
0224    * The kinematical cuts to used by subclasses which do not provide their own.
0225    */
0226   tCutsPtr cuts() const { return theCuts; }
0227 
0228   /**
0229    * A PartonExtractor object to be used by sub classes which do not
0230    * provide their own.
0231    */
0232   tPExtrPtr partonExtractor() const { return thePartonExtractor; }
0233 
0234   /**
0235    * Return a pointer (possibly null) to the assigned main
0236    * CascadeHandler.
0237    */
0238   tCascHdlPtr cascadeHandler() const;
0239 
0240   /**
0241    * Return a pointer (possibly null) to the assigned main
0242    * CascadeHandler to be used as CKKW-reweighter.
0243    */
0244   tCascHdlPtr CKKWHandler() const { return cascadeHandler(); }
0245 
0246   /**
0247    * Gget current event.
0248    */
0249   tEventPtr currentEvent() const { return theCurrentEvent; }
0250 
0251   /**
0252    * Get current collision.
0253    */
0254   tCollPtr currentCollision() const { return theCurrentCollision; }
0255 
0256   /**
0257    * Get current step.
0258    */
0259   tStepPtr currentStep() const { return theCurrentStep; }
0260 
0261   /**
0262    * Return true if this event handler should produce weightes events
0263    */
0264   bool weighted() const { return weightedEvents; }
0265 
0266   /**
0267    * The level of statistics. Controlls the amount of statistics
0268    * written out after each run to the <code>EventGenerator</code>s
0269    * <code>.out</code> file.
0270    */
0271   int statLevel() const { return theStatLevel; }
0272 
0273   /**
0274    * Determines how often the event handler should check for charge
0275    * and energy-momentum conservation.
0276    */
0277   ConsistencyLevel consistencyLevel() const { return theConsistencyLevel; }
0278 
0279   /**
0280    * The maximum fraction of the total invariant mass of a collision
0281    * that any of the components of the summed momentum is allowed to
0282    * change during the generation.
0283    */
0284   double consistencyEpsilon() const { return theConsistencyEpsilon; }
0285 
0286   //@}
0287 
0288   /** @name Internal functions used by main functions and possibly
0289       from the outside. */
0290   //@{
0291   /**
0292    * Perform a given step using a handler and a hint.
0293    */
0294   void performStep(tStepHdlPtr handler, tHintPtr hint);
0295 
0296   /**
0297    * In the curresnt list of step handlers to go through, add another
0298    * step handler and/or hint.
0299    */
0300   void addStep(Group::Level, Group::Handler,
0301            tStepHdlPtr = tStepHdlPtr(), tHintPtr = tHintPtr());
0302 
0303   /**
0304    * Create a new step and make it current. A StepHandler should be
0305    * supplied which will be set as the handler for the created
0306    * Step.
0307    */
0308   tStepPtr newStep(tcStepHdlPtr sh) {
0309     currentStep(currentCollision()->newStep(sh));
0310     return currentStep();
0311   }
0312 
0313   /**
0314    * Remove the last step.
0315    */
0316   void popStep() {
0317     currentCollision()->popStep();
0318     currentStep(currentCollision()->finalStep());
0319   }
0320 
0321   /**
0322    * Initialize the groups of step handlers.
0323    */
0324   virtual void initGroups();
0325 
0326   /**
0327    * Set current event.
0328    */
0329   void currentEvent(tEventPtr e) { theCurrentEvent = e; }
0330 
0331   /**
0332    * Set current collision.
0333    */
0334   void currentCollision(tCollPtr c) { theCurrentCollision = c; }
0335 
0336   /**
0337    * Set current step.
0338    */
0339   void currentStep(tStepPtr s) { theCurrentStep = s; }
0340 
0341   /**
0342    * Get current StepHandler.
0343    */
0344   tStepHdlPtr currentStepHandler() const { return theCurrentStepHandler; }
0345 
0346   /**
0347    * Set current StepHandler.
0348    */
0349   void currentStepHandler(tStepHdlPtr sh) { theCurrentStepHandler = sh; }
0350 
0351   /**
0352    * Throw away the current event/collision.
0353    */
0354   void throwCurrent();
0355 
0356   /**
0357    * Throw away the last generated event before generating a new one.
0358    */
0359   virtual void clean();
0360 
0361   /**
0362    * Check that the charge and energy-momentum in the last step of the
0363    * current collision is consistent with the incoming particles. If
0364    * not, a warning will be generated.
0365    */
0366   virtual void checkConsistency() const;
0367 
0368   //@}
0369 
0370 public:
0371 
0372   /** @name Functions used by the persistent I/O system. */
0373   //@{
0374   /**
0375    * Function used to write out object persistently.
0376    * @param os the persistent output stream written to.
0377    */
0378   void persistentOutput(PersistentOStream & os) const;
0379 
0380   /**
0381    * Function used to read in object persistently.
0382    * @param is the persistent input stream read from.
0383    * @param version the version number of the object when written.
0384    */
0385   void persistentInput(PersistentIStream & is, int version);
0386   //@}
0387 
0388   /**
0389    * Standard Init function used to initialize the interface.
0390    */
0391   static void Init();
0392 
0393 protected:
0394 
0395   /** @name Clone Methods. */
0396   //@{
0397   /**
0398    * Make a simple clone of this object.
0399    * @return a pointer to the new object.
0400    */
0401   virtual IBPtr clone() const;
0402 
0403   /** Make a clone of this object, possibly modifying the cloned object
0404    * to make it sane.
0405    * @return a pointer to the new object.
0406    */
0407   virtual IBPtr fullclone() const;
0408   //@}
0409 
0410   /** @name Standard Interfaced functions. */
0411   //@{
0412   /**
0413    * Finalize this object. Called in the run phase just after a
0414    * run has ended. Used eg. to write out statistics.
0415    */
0416   virtual void dofinish() {
0417     clean();
0418     HandlerBase::dofinish();
0419   }
0420 
0421   /**
0422    * Rebind pointer to other Interfaced objects. Called in the setup phase
0423    * after all objects used in an EventGenerator has been cloned so that
0424    * the pointers will refer to the cloned objects afterwards.
0425    * @param trans a TranslationMap relating the original objects to
0426    * their respective clones.
0427    * @throws RebindException if no cloned object was found for a given
0428    * pointer.
0429    */
0430   virtual void rebind(const TranslationMap & trans);
0431 
0432   /**
0433    * Return a vector of all pointers to Interfaced objects used in this
0434    * object.
0435    * @return a vector of pointers.
0436    */
0437   virtual IVector getReferences();
0438   //@}
0439 
0440 
0441 protected:
0442 
0443   /**
0444    * Access to the luminosity function.
0445    */
0446   LuminosityFunction & lumiFn() { return *theLumiFn; }
0447 
0448   /**
0449    * Setup the step handler groups.
0450    */
0451   void setupGroups();
0452 
0453   /**
0454    * Access the step handler groups
0455    */
0456   GroupVector & groups() { return theGroups; }
0457 
0458   /**
0459    * Access the step handler groups
0460    */
0461   const GroupVector & groups() const { return theGroups; }
0462 
0463 
0464 protected:
0465 
0466   /**
0467    * Set the luminosity function
0468    */
0469   void lumiFn(LumiFnPtr);
0470 
0471 private:
0472 
0473   /**
0474    * The maximum number of attempts to select a sub-process allowed
0475    * per event.
0476    */
0477   long theMaxLoop;
0478 
0479   /**
0480    * True if this event handler should produce weightes events
0481    */
0482   bool weightedEvents;
0483 
0484   /**
0485    * Controlls the amount of statistics written out after each run to
0486    * the EventGenerators .out file.
0487    */
0488   int theStatLevel;
0489 
0490   /**
0491    * Determines how often the event handler should check for charge
0492    * and energy-momentum conservation.
0493    */
0494   ConsistencyLevel theConsistencyLevel;
0495 
0496   /**
0497    * The maximum fraction of the total invariant mass of a collision
0498    * that any of the components of the summed momentum is allowed to
0499    * change during the generation.
0500    */
0501   double theConsistencyEpsilon;
0502 
0503   /**
0504    * Pointer to a luminosity function tobe used by subclasses.
0505    */
0506   LumiFnPtr theLumiFn;
0507 
0508   /**
0509    * The kinematical cuts to used by subclasses which do not provide
0510    * their own.
0511    */
0512   CutsPtr theCuts;
0513 
0514   /**
0515    * A PartonExtractor object to be used by sub classes which do not
0516    * provide their own.
0517    */
0518   PExtrPtr thePartonExtractor;
0519 
0520   /**
0521    * The SubProcessHandler group.
0522    */
0523   HandlerGroup<SubProcessHandler> theSubprocessGroup;
0524 
0525   /**
0526    * The CascadeHandler group.
0527    */
0528   HandlerGroup<CascadeHandler> theCascadeGroup;
0529 
0530   /**
0531    * The MultipleInteractionHandler group.
0532    */
0533   HandlerGroup<MultipleInteractionHandler> theMultiGroup;
0534 
0535   /**
0536    * The HadronizationHandler group.
0537    */
0538   HandlerGroup<HadronizationHandler> theHadronizationGroup;
0539 
0540   /**
0541    * The DecayHandler group.
0542    */
0543   HandlerGroup<DecayHandler> theDecayGroup;
0544 
0545   /**
0546    * The step handler groups.
0547    */
0548   GroupVector theGroups;
0549 
0550   /**
0551    * The current Event.
0552    */
0553   EventPtr theCurrentEvent;
0554 
0555   /**
0556    * The current Collision.
0557    */
0558   CollPtr theCurrentCollision;
0559 
0560   /**
0561    * The current Step.
0562    */
0563   StepPtr theCurrentStep;
0564 
0565   /**
0566    * The current StepHandler.
0567    */
0568   StepHdlPtr theCurrentStepHandler;
0569 
0570 protected:
0571 
0572   /**
0573    * Utility object to facilitate default selection of step handlers.
0574    */
0575   HandlerGroup<SubProcessHandler> optSubprocessGroup;
0576 
0577   /**
0578    * Utility object to facilitate default selection of step handlers.
0579    */
0580   HandlerGroup<CascadeHandler> optCascadeGroup;
0581 
0582   /**
0583    * Utility object to facilitate default selection of step handlers.
0584    */
0585   HandlerGroup<MultipleInteractionHandler> optMultiGroup;
0586 
0587   /**
0588    * Utility object to facilitate default selection of step handlers.
0589    */
0590   HandlerGroup<HadronizationHandler> optHadronizationGroup;
0591 
0592   /**
0593    * Utility object to facilitate default selection of step handlers.
0594    */
0595   HandlerGroup<DecayHandler> optDecayGroup;
0596 
0597 protected:
0598 
0599   /**
0600    * Utility object to facilitate default selection of step handlers.
0601    */
0602   GroupVector optGroups;
0603 
0604 protected:
0605 
0606   /**
0607    * Emit warning that this EventHandler is incomplete.
0608    */
0609   bool warnIncomplete;
0610 
0611   /**
0612    * The pair of incoming particle types. Should be set by a subclass
0613    * which implements a complete EventHandler.
0614    */
0615   cPDPair theIncoming;
0616 
0617 protected:
0618 
0619   /** @cond EXCEPTIONCLASSES */
0620   /**
0621    * Exception class used by EventHandler when a StepHandler of the
0622    * wrong class was added.
0623    */
0624   class EventHandlerStepError: public Exception {};
0625 
0626   /**
0627    * Exception class used by EventHandler when not able to produce a
0628    * correct histogram scale.
0629    */
0630   class EventHandlerHistError: public Exception {};
0631 
0632   /**
0633    * Exception class used by EventHandler if asked to generate a
0634    * complete event.
0635    */
0636   class EventHandlerIncompleteError: public Exception {};
0637 
0638   /** Exception class used if too many attempts to generate an event
0639    *  failed. */
0640   struct EventLoopException: public Exception {
0641     /** Standard constructor. */
0642     EventLoopException(const EventHandler &);
0643   };
0644 
0645   /**
0646    * Exception class used if the assignment of a LuminosityFunction
0647    * failed
0648    */
0649   struct LumiFuncError: public Exception {};
0650 
0651   /**
0652    * Exception class used if inconsistent charge or energy-momentum was found.
0653    */
0654   struct ConsistencyException: public Exception {};
0655 
0656   /** @endcond */
0657 
0658 private:
0659 
0660   ThePEG_DECLARE_PREPOST_GROUP(SubProcessHandler,Post);
0661   ThePEG_DECLARE_GROUPINTERFACE(CascadeHandler,CascHdlPtr);
0662   ThePEG_DECLARE_GROUPINTERFACE(MultipleInteractionHandler,MIHdlPtr);
0663   ThePEG_DECLARE_GROUPINTERFACE(HadronizationHandler,HadrHdlPtr);
0664   ThePEG_DECLARE_GROUPINTERFACE(DecayHandler,DecayHdlPtr);
0665 
0666   ThePEG_DECLARE_CLASS_DESCRIPTION(EventHandler);
0667 
0668   /**
0669    *  Private and non-existent assignment operator.
0670    */
0671   EventHandler & operator=(const EventHandler &) = delete;
0672 
0673 };
0674 
0675 /** @cond TRAITSPECIALIZATIONS */
0676 ThePEG_DECLARE_CLASS_TRAITS(EventHandler,HandlerBase);
0677 /** @endcond */
0678 
0679 }
0680 
0681 #endif /* ThePEG_EventHandler_H */