agile is hosted by Hepforge, IPPP Durham

Generator.hh

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #ifndef AGILE_GENERATOR_HH
00003 #define AGILE_GENERATOR_HH
00004 
00005 #include "AGILe/AGILe.hh"
00006 #include "AGILe/Utils.hh"
00007 #include "AGILe/Particle.hh"
00008 #include "AGILe/GeneratorState.hh"
00009 #include "HepMC/GenEvent.h"
00010 
00011 
00012 // Nice logging macros
00013 #ifndef MSG_LVL
00014 #define MSG_LVL(lvl, x) \
00015   do { \
00016     if (this->getLog().isActive(lvl)) { \
00017       this->getLog() << lvl << x << endl;   \
00018     } \
00019   } while (0)
00020 
00021 #define MSG_TRACE(x)   MSG_LVL(Log::TRACE, x)
00022 #define MSG_DEBUG(x)   MSG_LVL(Log::DEBUG, x)
00023 #define MSG_INFO(x)    MSG_LVL(Log::INFO, x)
00024 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
00025 #define MSG_ERROR(x)   MSG_LVL(Log::ERROR, x)
00026 #endif
00027 
00028 
00029 namespace AGILe {
00030 
00031 
00033   class Generator {
00034   public:
00035 
00037     enum Result { SUCCESS=1, FAILURE=0 };
00038 
00039 
00041     Generator() :
00042       _initialstateset(false),
00043       _initialized(false),
00044       _myName("GENERATOR"),
00045       _versionSet(false)
00046     { }
00047 
00048 
00050     virtual ~Generator() { }
00051 
00052 
00054     void setInitialState(int p1 = PROTON, double e1 = 7000.0,
00055                          int p2 = PROTON, double e2 = 7000.0)
00056     {
00057       setGenSpecificInitialState(p1, e1, p2, e2);
00059       _initialstateset = true;
00060       Beam beam1, beam2;
00061       beam1.name = p1;
00062       beam2.name = p2;
00063       beam1.energy = e1;
00064       beam2.energy = e2;
00065       beam1.position = 1;
00066       beam2.position = 2;
00067       _myState.setBeams(beam1, beam2);
00068     }
00069 
00070 
00072     virtual const GeneratorState& getState() {
00073       string myName = getName();
00074       _myState.setName(myName);
00075       _myState.setVersion(getVersion());
00076       _myState.setCrossSection(getCrossSection());
00077       PDF pdf;
00078       foreach (Beam b, _myState.getBeams()) {
00079         pdf.particleId = b.name;
00080         pdf.set = getPDFSet(b.name);
00081         pdf.member = getPDFMember(b.name);
00082         pdf.scheme = getPDFScheme(b.name);
00083         _myState.addPDF(pdf);
00084       }
00085       return _myState;
00086     }
00087 
00088 
00090     virtual void setState(const GeneratorState& state) {
00091       throw runtime_error("This ain't ready yet, it's just here as an interface placeholder. Sorry.");
00092     }
00093 
00094 
00096     virtual const std::string getName() const {
00097       return _myName;
00098     }
00099 
00100 
00102     virtual void setVersion(const std::string& version) {
00103       _myVersion = version;
00104       _versionSet = true;
00105     }
00106 
00107 
00112     virtual const std::string getVersion() {
00113       if (!_versionSet) {
00115         MSG_ERROR("You asked the generator " //<< getName()
00116                   << " what version it is, but it doesn't know!\n"
00117                   << "You should set the version with a call to setVersion(string)");
00118         throw runtime_error("Version unknown for generator");
00119       }
00120       return _myVersion;
00121     }
00122 
00123 
00126     virtual std::string getPDFSet(PdgCode pid) {
00127       throw runtime_error("Have not implemented getPDFSet in " + getName());
00128     }
00129 
00130 
00132     virtual int getPDFMember(PdgCode pid) {
00133       throw runtime_error("Have not implemented getPDFMember in " + getName());
00134     }
00135 
00136 
00138     virtual std::string getPDFScheme(PdgCode pid) const {
00139       throw runtime_error("Have not implemented getPDFScheme in " + getName());
00140     }
00141 
00142 
00144     virtual void setSeed(const int value) {
00145       if (_initialized) {
00146         MSG_ERROR("Can't set seed: generator has already been initialized");
00147         throw (runtime_error("Tried to set RNG seed after generator initialization"));
00148       }
00149       _myState.addSeed(value);
00150     }
00151 
00152 
00154     virtual bool setParam(const std::string& name, const int& value) {
00155       if (_initialized) {
00156         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00157         throw runtime_error("Tried to set param after generator initialization");
00158       }
00159       return setParam(name, toString(value));
00160     }
00161 
00162 
00164     virtual bool setParam(const std::string& name, const unsigned int& value) {
00165       if (_initialized) {
00166         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00167         throw runtime_error("Tried to set param after generator initialization");
00168       }
00169       return setParam(name, toString(value));
00170     }
00171 
00172 
00174     virtual bool setParam(const std::string& name, const long& value) {
00175       if (_initialized) {
00176         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00177         throw runtime_error("Tried to set param after generator initialization");
00178       }
00179       return setParam(name, toString(value));
00180     }
00181 
00182 
00184     virtual bool setParam(const std::string& name, const unsigned long& value) {
00185       if (_initialized) {
00186         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00187       throw runtime_error("Tried to set param after generator initialization");
00188       }
00189       return setParam(name, toString(value));
00190     }
00191 
00192 
00194     virtual bool setParam(const std::string& name, const double& value) {
00195       if (_initialized) {
00196         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00197         throw runtime_error("Tried to set param after generator initialization.");
00198       }
00199       return setParam(name, toString(value));
00200     }
00201 
00202 
00204     virtual bool setParam(const std::string& name, const float& value) {
00205       if (_initialized) {
00206         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00207         throw runtime_error("Tried to set param after generator initialization");
00208       }
00209       return setParam(name, toString(value));
00210     }
00211 
00212 
00214     virtual bool setParam(const std::string& name, const bool& value) {
00215       if (_initialized) {
00216         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00217         throw runtime_error("Tried to set param after generator initialization");
00218       }
00219       return setParam(name, toString(value));
00220     }
00221 
00222 
00224     virtual bool setParam(const std::string& name, const std::string& value) {
00225       if (_initialized) {
00226         MSG_ERROR("Can't set parameter " << name << ": generator has already been initialized");
00227         throw runtime_error("Tried to set param after generator initialization");
00228       }
00229       _myState.setParam(name, value);
00230       return true;
00231     }
00232 
00233 
00235     virtual void makeEvent(HepMC::GenEvent& evt) {
00236       if (!_initialized) initialize();
00237       clearEvent(evt);
00238       _myState.addEvent();
00239     }
00240 
00241 
00243     virtual const double getCrossSection() {
00244       MSG_ERROR("getCrossSection not implemented in generator " << getName());
00245       throw runtime_error("Tried to access cross-section in Generator base class");
00246       return _crossSection;
00247     }
00248 
00249 
00251     virtual void finalize() = 0;
00252 
00253 
00254   protected:
00255 
00257     Log& getLog() const {
00258       return Log::getLog("AGILe." + getName());
00259     }
00260 
00261 
00263     virtual void
00264     setGenSpecificInitialState(int p1 = PROTON, double e1 = 7000.0,
00265                                int p2 = PROTON, double e2 = 7000.0) = 0;
00266 
00267 
00269     virtual void initialize() {
00270       if (!_initialstateset) setInitialState();
00271     }
00272 
00273 
00275     virtual void clearEvent(HepMC::GenEvent& evt) {
00276       evt.clear();
00277     }
00278 
00279 
00280   protected:
00281 
00282     double _crossSection;
00283 
00285     bool _initialstateset;
00286 
00288     bool _initialized;
00289 
00291     map<PdgCode, std::string> _particleNames;
00292 
00294     string _myName;
00295 
00296 
00297   private:
00298 
00299     GeneratorState _myState;
00300 
00301     string _myVersion;
00302 
00303     bool _versionSet;
00304   };
00305 
00306 
00308 
00309 
00311   typedef Generator* (*Creator)();
00312 
00313 
00315   typedef void Destroyer(Generator*);
00316 
00317 }
00318 
00319 #endif
Generated on Tue Mar 6 10:39:38 2012 for AGILe - A Generator Interface Library (+ executable) by  doxygen 1.6.3