User Hooks

Sometimes it may be convenient to step in during the generation process: to modify the built-in cross sections, to veto undesirable events or simply to collect statistics at various stages of the evolution. There is a base class UserHooks that gives you this access at a few selected places. This class in itself does nothing; the idea is that you should write your own derived class for your task. A few very simple derived classes come with the program, mainly as illustration.

For a derived class to be called during the execution, a pointer to an object of this class should be handed in with the
pythia.setUserHooksPtr( UserHooks*) method.

There are five distinct sets of routines. They are, in no particular order:
(i) Ones that gives you access to the event record in between the process-level and parton-level steps, or in between the parton-level and hadron-level ones. You can study the event record and decide whether to veto this event.
(ii) Ones that allow you to set a scale at with the combined parton-level MI+ISR+FSR downwards evolution in pT is temporarily interrupted, so the event can be studied and either vetoed or allowed to continue the evolution.
(iii) Ones that allow you to to study the event after the first few ISR/FSR emissions, so the event can be vetoed or allowed to continue the evolution.
(iv) Ones that gives you access to the properties of the trial hard process, so that you can modify the internal Pythia cross section by your own correction factors.
(v) Ones that let you set the scale of shower evolution, specifically for matching in resonance decays.
They are described further in the following.

The first step is to construct your own derived class, of course.

UserHooks::UserHooks()  
virtual UserHooks::~UserHooks()  
The constructor and destructor do not need to do anything.

void UserHooks::initPtr(PartonSystems* partonSystemsPtr)  
this (non-virtual) method is called automatically to set a pointer to the partonSystems object.

Interrupt between the main generation levels

virtual bool UserHooks::canVetoProcessLevel()  
In the base class this method returns false. If you redefine it to return true then the method doVetoProcessLevel(...) will be called immediately after a hard process has been selected and stored in the process event record.

virtual bool UserHooks::doVetoProcessLevel(const Event& process)  
can optionally be called, as described above. You can study, but not modify, the process event record of the hard process. Based on that you can decide whether to veto the event, true, or let it continue to evolve, false. If you veto, then this event is not counted among the accepted ones, and does not contribute to the estimated cross section. The Pytha::next() method will begin a completely new event, so the vetoed event will not appear in the output of Pythia::next().
Note: the above veto is different from setting the flag PartonLevel:all = off. Also in the latter case the event generation will stop after the process level, but an event generated up to this point is considered perfectly acceptable. It can be studied and cross sections are not affected. That is, it is intended for simple studies of hard processes, where one can save time by not generating the rest of the story. By contrast, the doVetoProcessLevel() method allows you to throw away uninteresting events at an early stage to save time that way, but those events that do survive the veto are allowed to develop into complete final states (unless flags have been set otherwise).

virtual bool UserHooks::canVetoPartonLevel()  
In the base class this method returns false. If you redefine it to return true then the method doVetoPartonLevel(...) will be called immediately after the parton level has been generated and stored in the event event record. Thus showers, multiple interactions and beam remnants have been set up, but hadronization and decays have not yet been performed.

virtual bool UserHooks::doVetoPartonLevel(const Event& event)  
can optionally be called, as described above. You can study, but not modify, the event event record of the partonic process. Based on that you can decide whether to veto the event, true, or let it continue to evolve, false. If you veto, then this event is not counted among the accepted ones, and does not contribute to the estimated cross section. The Pytha::next() method will begin a completely new event, so the vetoed event will not appear in the output of Pythia::next().
Note: the above veto is different from setting the flag HadronLevel:all = off. Also in the latter case the event generation will stop after the parton level, but an event generated up to this point is considered perfectly acceptable. It can be studied and cross sections are not affected. That is, it is intended for simple studies of complete partonic states, where one can save time by not generating the complete hadronic final state. By contrast, the doVetoPartonLevel() method allows you to throw away uninteresting events at an early stage to save time that way, but those events that do survive the veto are allowed to develop into complete final states (unless flags have been set otherwise).

The effect of the vetoes can be studied in the output of the Pythia::statistics() method. The "Selected" column represents the number of events that were found acceptable by the internal Pythia machinery, whereas the "Accepted" one are the events that also survived the user cuts.

Interrupt during the parton-level evolution, at a pT scale

During the parton-level evolution, multiple interactions (MI), initial-state radiation (ISR) and final-state radiation (FSR) are normally evolved downwards in one interleaved evolution sequence of decreasing pT values. For some applications, e.g matrix-element-matching approaches, it may be convenient to stop the evolution temporarily when the "hard" emissions have been considered, but before continuing with the more time-consuming soft activity. Based on these hard partons one can make a decision whether the event at all falls in the intended event class, e.g. has the "right" number of parton-level jets. If yes then, as for the methods above, the evolution will continue all the way up to a complete event. Also as above, if no, then the event will not be considered in the final cross section.

In this subsection we outline the possibility to interrupt at a given pT scale, in the next to interrupt after a given number of emissions.

virtual bool UserHooks::canVetoPT()  
In the base class this method returns false. If you redefine it to return true then the method doVetoPT(...) will interrupt the downward evolution at scaleVetoPT().

virtual double UserHooks::scaleVetoPT()  
In the base class this method returns 0. You should redefine it to return the pT scale at which you want to study the event.

virtual bool UserHooks::doVetoPT(int iPos, const Event& event)  
can optionally be called, as described above. You can study, but not modify, the event event record of the partonic process. Based on that you can decide whether to veto the event, true, or let it continue to evolve, false. If you veto, then this event is not counted among the accepted ones, and does not contribute to the estimated cross section. The Pytha::next() method will begin a completely new event, so the vetoed event will not appear in the output of Pythia::next().
argument iPos : is the position/status when the routine is called, information that can help you decide your course of action:
argumentoption 0 : when no MI, ISR or FSR occured above the veto scale;
argumentoption 1 : when inside the interleaved MI + ISR + FSR evolution, after an MI process;
argumentoption 2 : when inside the interleaved MI + ISR + FSR evolution, after an ISR emission;
argumentoption 3 : when inside the interleaved MI + ISR + FSR evolution, after an FSR emission;
argumentoption 4 : for the optional case where FSR is deferred from the interleaved evolution and only considered separately afterward (then alternative 3 would never occur);
argumentoption 5 : is for subsequent resonance decays, and is called once for each decay in a chain such as t -> b W, W -> u dbar.
argument event : the event record contains a list of all partons generated so far, also including intermediate ones not part of the "current final state", and also those from further multiple interactions. This may not be desirable for comparisons with matrix-element calculations. You may want to make use of the subEvent(...) method below to obtain a simplified event record.

void UserHooks::subEvent(const Event& event, bool isHardest = true)  
is a protected method that you can make use of in your own methods to extract a brief list of the current partons of interest, with all irrelevant ones omitted. For the default isHardest = true only the current partons from the hardest interaction are extracted, as relevant for doVetoPT( iPos, event) with iPos = 0 - 4. With isHardest = false instead the latest "subprocess" is extracted, as relevant when iPos = 5, where it corresponds to the partons in the currently considered decay. The resut is stored in workEvent below.

Event UserHooks::workEvent  
This protected class member contains the outcome of the above subEvent(...) method, i.e. a brief list of all current partons in the hard system considered, with all other partons omitted. The daughter1() and daughter2() indices of a particle in this list both return the position of the same parton in the original event record (event; possibly process), so that you can trace the full history, if of interest. The workEvent can e.g. be sent on to a jet clustering algorithm. You are free to edit workEvent as you desire, e.g. boost to its rest frame before analysis, or remove particles that should not be analyzed.

Interrupt during the parton-level evolution, after a step

This option is closely related to the one above, so we do not repeat the introduction, nor the possibilities to study the event record, also by using subEvent(...) and workEvent. What is different is that the methods in this section give access to the event as it looks like after each of the first few steps in the downwards evolution, irrespectively of the pT scales of these branchings. Furthermore, it is here assumed that the focus normally is on the hardest subprocess, so that ISR/FSR emissions associated with additional MI's are not considered. For MI studies, however, a separate simpler alternative is offered to consider the event after a given number of interactions.

virtual bool UserHooks::canVetoStep()  
In the base class this method returns false. If you redefine it to return true then the method doVetoStep(...) will interrupt the downward ISR and FSR evolution the first numberVetoStep() times.

virtual int UserHooks::numberVetoStep()  
Returns the number of steps each of ISR and FSR, for the hardest interaction, that you want to be able to study. The number of steps defaults to the first one only, but you are free to pick another value.

virtual bool UserHooks::doVetoStep(int iPos, int nISR, int nFSR, const Event& event)  
can optionally be called, as described above. You can study, but not modify, the event event record of the partonic process. Based on that you can decide whether to veto the event, true, or let it continue to evolve, false. If you veto, then this event is not counted among the accepted ones, and does not contribute to the estimated cross section. The Pytha::next() method will begin a completely new event, so the vetoed event will not appear in the output of Pythia::next().
argument iPos : is the position/status when the routine is called, information that can help you decide your course of action. Agrees with options 2 - 5 of the doVetoPT(...) routine above, while options 0 and 1 are not relevant here.
argument nISR : is the number of ISR emissions in the hardest process so far. For resonance decays, iPos = 5, it is 0.
argument nFSR : is the number of FSR emissions in the hardest process so far. For resonance decays, iPos = 5, it is the number of emissions in the currently studied system.
argument event : the event record contains a list of all partons generated so far, also including intermediate ones not part of the "current final state", and also those from further multiple interactions. This may not be desirable for comparisons with matrix-element calculations. You may want to make use of the subEvent(...) method above to obtain a simplified event record.

virtual bool UserHooks::canVetoMIStep()  
In the base class this method returns false. If you redefine it to return true then the method doVetoMIStep(...) will interrupt the downward MI evolution the first numberVetoMIStep() times.

virtual int UserHooks::numberVetoMIStep()  
Returns the number of steps in the MI evolution that you want to be able to study, right after each new step has been taken and the subcollision has been added to the event record. The number of steps defaults to the first one only, but you are free to pick another value.

virtual bool UserHooks::doVetoMIStep(int nMI,const Event& event)  
can optionally be called, as described above. You can study, but not modify, the event event record of the partonic process. Based on that you can decide whether to veto the event, true, or let it continue to evolve, false. If you veto, then this event is not counted among the accepted ones, and does not contribute to the estimated cross section. The Pytha::next() method will begin a completely new event, so the vetoed event will not appear in the output of Pythia::next().
argument nMI : is the number of MI subprocesses has occured so far.
argument event : the event record contains a list of all partons generated so far, also including intermediate ones not part of the "current final state", e.g. leftovers from the ISR and FSR evolution of previously generated systems. The most recently added one has not had time to radiate, of course.

Modify cross-sections

virtual bool UserHooks::canVetoStep()  
In the base class this method returns false. If you redefine it to return true then the method multiplySigmaBy(...) will allow you to modify the cross section weight assigned to the current event.

virtual double UserHooks::multiplySigmaBy(const SigmaProcess* sigmaProcessPtr, const PhaseSpace* phaseSpacePtr, bool inEvent)  
when called this method should provide the factor by which you want to see the cross section weight of the current event modified by. If you return unity then the normal cross section is obtained. Note that, unlike the methods above, these modifications do not lead to a difference between the number of "selected" events and the number of "accepted" ones, since the modifications occur already before the "selected" level. The integrated cross section of a process is modified, of course. Note that the cross section is only modifiable for normal hard processes. It does not affect the cross section in further multiple interactions, nor in elastic/diffractive/minimum-bias events.
argument sigmaProcessPtr, phaseSpacePtr : : what makes this routine somewhat tricky to write is that the hard-process event has not yet been constructed, so one is restricted to use the information available in the phase-space and cross-section objects currently being accessed. Which of their methods are applicable depends on the process, in particular the number of final-state particles. The multiplySigmaBy code in UserHooks.cc contains explicit instructions about which methods provide meaningful information, and so offers a convenient starting point.
argument inEvent : : this flag is true when the method is called from within the event-generation machinery and false when it is called at the initialization stage of the run, when the cross section is explored to find a maximum for later Monte Carlo usage. Cross-section modifications should be independent of this flag, for consistency, but if multiplySigmaBy(...) is used to collect statistics on the original kinematics distributions before cuts, then it is important to be able to exclude the initialization stage from comparisons.

One derived class is supplied as an example how this facility can be used to reweight cross sections in the same spirit as is done with QCD cross sections for the minimum-bias/underlying-event description:

class  SuppressSmallPT : public UserHooks  
suppress small-pT production for 2 -> 2 processes only, while leaving other processes unaffected. The basic suppression factor is pT^4 / ((k*pT0)^2 + pT^2)^2, where pT refers to the current hard subprocess and pT0 is the same energy-dependent dampening scale as used for multiple interactions. This class contains canModifySigma() and multiplySigmaBy() methods that overload the base class ones.

SuppressSmallPT::SuppressSmallPT( double pT0timesMI = 1., int numberAlphaS = 0, bool useSameAlphaSasMI = true)  
The optional arguments of the constructor provides further variability.
argument pT0timesMI : corresponds to the additional factor k in the above formula. It is by default equal to 1 but can be used to explore deviations from the expected value.
argument numberAlphaS : if this number n is bigger than the default 0, the corresponding number of alpha_strong factors is also reweighted from the normal renormalization scale to a modified one, i.e. a further suppression factor ( alpha_s((k*pT0)^2 + Q^2_ren) / alpha_s(Q^2_ren) )^n is introduced.
argument useSameAlphaSasMI : regulates which kind of new alpha_strong value is evaluated for the numerator in the above expression. It is by default the same as set for multiple interactions (i.e. same starting value at M_Z and same order of running), but if false instead the one for hard subprocesses. The denominator alpha_s(Q^2_ren) is always the value used for the "original", unweighted cross section.

Modify scale in shower evolution

The choice of maximum shower scale in resonance decays is normally not a big issue, since the shower here is expected to cover the full phase space. In some special cases a matching scheme is intended, where hard radiation is covered by matrix elements, and only softer by showers. The below two methods support such an approach. Note that the two methods are not used in the TimeShower class itself, but when showers are called from the PartonLevel generation. Thus user calls directly to TimeShower are not affected.

virtual bool UserHooks::canSetResonanceScale()  
In the base class this method returns false. If you redefine it to return true then the method scaleResonance(...) will set the initial scale of downwards shower evolution.

virtual double UserHooks::scaleResonance( const int iRes, const Event& event)  
can optionally be called, as described above. You should return the maximum scale, in GeV, from which the shower evolution will begin. The base class method returns 0, i.e. gives no shower evolution at all. You can study, but not modify, the event event record of the partonic process to check which resonance is decaying, and into what.
argument iRes : is the location in the event record of the resonance that decayed to the particles that now will shower.
argument event : the event record contains a list of all partons generated so far, specifically the decaying resonance and its immediate decay products.

Final comments

All the possibilities above can be combined freely and also be combined with the standard flags. An event would then survive only if it survived each of the possible veto methods. There are no hidden interdependencies in this game, but of course some combinations may not be particularly meaningful. For instance, if you set PartonLevel:all = off then the doVetoPT(...) and doVetoPartonLevel(...) locations in the code are not even reached, so they would never be called.

An example how the above methods can be used for toy studies is found in main10.cc.