00001
00002
00003
00004
00005
00006 #include "gismo/Gismo.h"
00007
00008 #include "gui/DisplayControl.h"
00009 #include "gui/DisplayRep.h"
00010 #include "gui/PrintControl.h"
00011
00012 #include "geometry/Track.h"
00013 #include "geomrep/TrackRep.h"
00014
00015
00016 #include "gismo/World.h"
00017 #include "gismo/Generator.h"
00018 #include "gismo/MCParticle.h"
00019 #include "gismo/Material.h"
00020 #include "gismo/PDT.h"
00021 #include <cassert>
00022
00023 #include <stdio.h>
00024
00026
00027 class ParticleRep : public gui::DisplayRep
00028 {
00029
00030 public:
00031 ParticleRep(const MCParticle & part);
00032 void update(){}
00033 };
00034
00035
00036 ParticleRep::ParticleRep(const MCParticle & part) {
00037 if(part.track() ) {
00038 append( TrackRep(*part.track()) );
00039 }
00040 for(int i=0; i< part.numChildren(); i++)
00041 append(ParticleRep( *(MCParticle*) part.child(i)));
00042 }
00043
00044 class Gismo::DisplayEvent : public gui::DisplayRep {friend class Gismo;
00045
00046 DisplayEvent(Gismo* gismo): m_gismo(gismo){}
00047 void update() {
00048 Generator* gen = m_gismo->event();
00049 if( gen ) {
00050 markerAt(gen->position());
00051 append(ParticleRep(*gen));
00052 }
00053 }
00054 Gismo* m_gismo;
00055 };
00056
00057 class Gismo::PrintEvent : public gui::Command {friend class Gismo;
00058 PrintEvent(std::ostream* out):m_out(out){}
00059 void execute() { Gismo::instance()->event()->printOn(*m_out);}
00060 std::ostream* m_out;
00061 };
00062 class Gismo::PrintPDT : public gui::Command { friend class Gismo;
00063 PrintPDT(std::ostream* out):m_out(out){}
00064 void execute() { Gismo::instance()->event()->thePDT()->printOn(*m_out);}
00065 std::ostream* m_out;
00066 };
00067 class Gismo::PrintMaterials : public gui::Command { friend class Gismo;
00068 PrintMaterials(std::ostream* out):m_out(out){}
00069 void execute() { Material::printAll(*m_out);}
00070 std::ostream* m_out;
00071 };
00073
00074
00075 static float ke_cutoff = 0.0005f;
00076 static float max_step_size = 100;
00077
00078 Gismo::Gismo(float size, Generator* gen, gui::DisplayControl* dc)
00079 : m_world(new World(size))
00080 , m_generator(gen)
00081 {
00082 s_instance = this;
00083
00084
00085
00086
00087
00088 MCParticle::saveTrack = 10;
00089
00090
00091
00092
00093 dc->add( m_world->detectorViewer(),"Detector", -1 );
00094 dc->add( m_world->responseViewer(), "Response",1 );
00095 dc->add( new DisplayEvent(this) , "Event" ,1);
00096
00097
00098 gui::PrintControl* pc= gui::PrintControl::instance();
00099 pc->add( new PrintEvent( pc->out() ), "Event" );
00100 pc->add(m_world->responsePrinter(pc->out()), "Response" );
00101
00102 setKEcutoff(ke_cutoff);
00103 setMaxStep(max_step_size);
00104 addMaterialPath("PEGS4");
00105
00106 }
00107
00108 Gismo& Gismo::addMaterialPath(const char* path)
00109 {
00110 Material::addPath(path);
00111 return *this;
00112 }
00113 Gismo& Gismo::addInteractor(Interactor* inter)
00114 {
00115 MCParticle::addInteractor(inter);
00116 return *this;
00117 }
00118 Gismo& Gismo::setKEcutoff(float e)
00119 {
00120 m_world->setKECutOff(e);
00121 return *this;
00122 }
00123 Gismo& Gismo::setMaxStep(float step)
00124 { m_world->setMaxStep(step);
00125 return *this;
00126 }
00127 Gismo& Gismo::setGenerator(Generator* gen)
00128 {
00129 m_generator = gen;
00130 return *this;
00131 }
00132
00133 Gismo& Gismo::setField(Field* field)
00134 {
00135 m_world->setField(field);
00136 return *this;
00137 }
00138
00139
00140 void
00141 Gismo::finishSetup()
00142 {
00143 gui::PrintControl* pc= gui::PrintControl::instance();
00144 pc->add(new PrintMaterials(pc->out()) , "Materials" );
00145 pc->add(new PrintPDT(pc->out()), "PDT" );
00146 }
00147
00148
00149
00150 Gismo* Gismo::s_instance=0;
00151 Gismo* Gismo::instance()
00152 {
00153 assert( s_instance );
00154 return s_instance;
00155 }
00156
00157
00158
00159 void Gismo::generate()
00160 {
00161 m_world->processEvent(*m_generator);
00162 }
00163
00164 static char* itoa(int i)
00165 { static char buf[20];
00166 sprintf(buf,"%d",i);
00167 return buf;
00168 }
00169 const char * Gismo::eventTitle()const
00170 {
00171 static std::string title;
00172 title = std::string("Generated ");
00173 title+= m_generator->name();
00174 title+= ": event ";
00175 title+= itoa(m_generator->eventNumber());
00176 return title.c_str();
00177 }
00178
00179
00180
00181 Generator*
00182 Gismo::event()const
00183 {
00184 return m_generator;
00185 }
00186
00187 Gismo::~Gismo()
00188
00189 {
00190 delete GParticle::thePDT();
00191 delete m_world;
00192 Material::deleteAll();
00193
00194 }