BLUserCode.hh

Go to the documentation of this file.
00001 //      BLUserCode.hh
00002 /*
00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com
00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved.
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 http://www.gnu.org/copyleft/gpl.html
00017 */
00018 
00019 #ifndef BLUSERCODE_HH
00020 #define BLUSERCODE_HH
00021 
00022 #include <vector>
00023 
00024 #include "globals.hh"
00025 #include "G4ThreeVector.hh"
00026 #include "G4Track.hh"
00027 
00028 /**     class BLUserCode is a base class for a user-defined classes.
00029  *
00030  *      See the section in the User's Guide titled "User Code for various
00031  *      commands".
00032  *
00033  *      This is used as follows: a G4beamline command that needs
00034  *      to call user-supplied code will have a name, and it will expect
00035  *      all user-supplied code to arrange so getType() returns that name.
00036  *      The G4beamline author will write a class BLUserXXX that is
00037  *      derived from BLUserCode and implements getType() with the
00038  *      correct value -- so users should NOT override this function.
00039  *      Usually this class declaration will be put into this file.
00040  *      BLUserXXX will also define some additional virtual function(s)
00041  *      for the user to implement, appropriate to the function to be 
00042  *      performed. The user must implement these functions and getName().
00043  *      There's one other requirement: the user class must call 
00044  *      registerUserCode() at the end of its default constructor, and must
00045  *      define a static instance of the class using that constructor --
00046  *      this provides linkage into G4beamline so that the underlying command
00047  *      can find and call the user code.
00048  *
00049  *      The user code should be compiled into a shared object, using the
00050  *      g4blmake command. The G4beamline input file should use the "load"
00051  *      command to load the shared object BEFORE any command references
00052  *      the user class.
00053  *      NOTE: the "load" command uses the filename built by g4blmake;
00054  *      the other command(s) use the getName() implemented by
00055  *      the user class. Multiple instances of user code can be put into a
00056  *      single .cc file (or multiple .cc files linked into one shared object);
00057  *      they must each have a unique getName() return value.
00058  *
00059  *      NOTE: this class will interface between shared objects, so no
00060  *      inline function can reference any static data member. This does NOT
00061  *      apply to user-supplied derived classes that necessarily reside in a 
00062  *      single shared object.
00063  **/
00064 class BLUserCode {
00065         static std::vector<BLUserCode*> *list;
00066 public:
00067         /// Constructor. 
00068         BLUserCode() { }
00069 
00070         /// Destructor.
00071         virtual ~BLUserCode() { }
00072 
00073         /// registerUserCode() -- register this instance with the BLManager so
00074         /// other commands can find it. Note that every final derived
00075         /// class must call this at the end of its default constructor,
00076         /// and must define a static object using that constructor.
00077         void registerUserCode();
00078 
00079         /// Name of the type -- MUST be overridden in derived classes that
00080         /// define an interface to user code from a G4beamline command.
00081         virtual const char *getType() = 0;
00082 
00083         /// Name of the instance -- MUST be overridden in user-supplied
00084         /// derived classes.
00085         virtual const char *getName() = 0;
00086 };
00087 
00088 /**     class BLUserTrackFilter -- used by BLCMDusertrackfilter.
00089  *
00090  *      User code can use any library used by G4beamline simply by putting the
00091  *      appropriate #include into the .cc file and building with g4blmake --
00092  *      it automatically adds the appropriate include paths and libraries.
00093  *      These are: CLHEP, Geant4, Root, Gsl, and the C/C++ libraries.
00094  *      Coin and Xwindows are used by G4beamline for visualization, but are
00095  *      not available to user code.
00096  *
00097  *      Additional user libraries can be used if they are added to the
00098  *      CCFLAGS and EXTRALIBS environment variables to g4blmake -- do NOT
00099  *      do this for libraries used by G4beamline. Do not attempt to use the
00100  *      Coin or Xwindows libraries.
00101  *
00102  *      The user .cc file(s) can contain local class definitions and static 
00103  *      instances of them; the latter will be constructed when the shared-
00104  *      object is loaded. NOTE: there is no guarantee that their destructors
00105  *      will be called (and they usually aren't), so if destruction is
00106  *      important, use setup() and complete().
00107  *
00108  *      filter() will be called by the "usertrackfilter" element
00109  *      whenever a track enters the physical volume of the element. The
00110  *      input track will be setup. The function can kill the track, and/or
00111  *      add pointers to new G4Track-s to the secondaries vector, as necessary.
00112  *
00113  *      Note that the Tune particle has eventID -2 and the Reference particle
00114  *      has eventID -1 -- these events may need special treatment, and
00115  *      their tracks should not be killed or modified.
00116  *
00117  *      The secondaries vector is initially empty; add pointers to G4Track-s
00118  *      to it to create secondaries.
00119  *
00120  *      verbose is the value of the parameter steppingVerbose. If it is 
00121  *      nonzero you should print debugging information.
00122  *
00123  *      Note: for secondaries the trackID will be set to a unique value and
00124  *      the parentID will be set to the trackID of the input track,
00125  *      after filter() returns; all secondaries remain part of the
00126  *      current event. So there's no need to set trackID or parentID
00127  *      in G4Track-s put into the secondaries vector, and whatever values
00128  *      they have will be ignored.
00129  *
00130  *      The secondaries are created inside the volume, and do not
00131  *      get filtered.
00132  **/
00133 class BLUserTrackFilter : public BLUserCode {
00134 public:
00135         /// Constructor
00136         BLUserTrackFilter() : BLUserCode() { }
00137 
00138         /// Destructor
00139         virtual ~BLUserTrackFilter() { }
00140 
00141         /// getType() return "usertrackfilter"
00142         const char *getType() { return "usertrackfilter"; }
00143 
00144         // Convenience functions for user code to use:
00145 
00146         /// getPDGid() returns the PDGid of a G4Track.
00147         /// User code can of course use the usual Geant4
00148         /// functions to do this, this is merely for convenience.
00149         static int getPDGid(G4Track *track);
00150 
00151         /// setMomentum() will set the momentum of a G4Track.
00152         /// User code can of course use the usual Geant4
00153         /// functions to do this, this is merely for convenience.
00154         static void setMomentum(G4Track *track, G4ThreeVector momentum);
00155 
00156         /// getMass() returns the mass of a particle with known PDGid.
00157         /// User code can of course use the usual Geant4
00158         /// functions to do this, this is merely for convenience.
00159         static G4double getMass(int PDGid);
00160 
00161         /// killTrack() will kill a track.
00162         static void killTrack(G4Track *track);
00163 
00164         /// constructTrack() creates a new G4Track from position, momentum,
00165         /// time, and PDGid. Global coordinates are used. User code can of
00166         /// course use the usual Geant4 functions to do this, this is merely
00167         /// for convenience. Note that eventID is not contained in a G4Track.
00168         static G4Track *constructTrack(G4ThreeVector position,
00169                         G4ThreeVector momentum, G4double time, int PDGid,
00170                         G4double weight=1.0);
00171 
00172         // User-supplied virtual functions:
00173 
00174         /// getName() returns the name of the user-supplied instance
00175         virtual const char *getName() = 0;
00176 
00177         /// user function to filter tracks.
00178         /// Global coordinates are used.
00179         /// NOTE: do NOT delete the input track, or those you create and
00180         /// put into secondaries -- they are deleted by Geant4 internally.
00181         virtual void filter(G4Track *track, int eventID,
00182                         std::vector<G4Track*> &secondaries, int verbose) = 0;
00183 
00184         /// setup() will be called during initialization; its
00185         /// argument is the string passed to the "usertrackfilter" command
00186         /// as its "init" argument. The init string can be used in any way
00187         /// by the user code (it passes information from the G4beamline input
00188         /// file to the user code, in a manner defined by the user code; the
00189         /// input file should be written to match this usage).
00190         /// If desired, multiple usertrackfilter commands can use a single
00191         /// user-supplied class instance, using the init string to
00192         /// differentiate among them. The init string need not be used at all.
00193         virtual void setup(const char *init) = 0;
00194 
00195         /// complete() will be called after tracking is complete.
00196         /// It is intended for printing summaries and generally closing down.
00197         /// If multiple usertrackfilter commands reference this class instance,
00198         /// complete() will be called for each one, with the corresponding 
00199         /// init string (do not depend on any particular ordering).
00200         virtual void complete(const char *init) = 0;
00201 };
00202 
00203 //
00204 // Below are definitions for the convenience of the user-defined functions
00205 // above.
00206 //
00207 
00208 /**     Particle IDs for stable particles. Use negative for anti-particle.
00209  *      ELECTRON and MUON are negative particles; PION, KAON, and PROTON
00210  *      are positive particles; KAON0, NEUTRON, and NU_* are neutrals.
00211  *      There are many other PDGid-s, listed in the Geant4 and G4beamline 
00212  *      documentation; these are just the most common ones.
00213  **/
00214 const int ELECTRON=11, MUON=13, PION=211, KAON=321, KAON0=311, NEUTRON=2112,
00215         PROTON=2212, NU_E=12, NU_MU=14, NU_TAU=16, GAMMA=22 ;
00216 
00217 #endif // BLUSERCODE_HH 
g4beamline