00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "generators/MaGeDecay0Interface.hh"
00018 #include "io/MJLogger.hh"
00019 #include "G4Event.hh"
00020 #include "G4PrimaryVertex.hh"
00021 #include "G4PrimaryParticle.hh"
00022 #include "generators/MaGeDecay0InterfaceMessenger.hh"
00023 #include "G4ThreeVector.hh"
00024 #include "G4ParticleTable.hh"
00025 #include <string.h>
00026
00027 MaGeDecay0Interface::MaGeDecay0Interface(G4String File) :
00028 fFileName(File)
00029 {
00030 fTheMessenger = new MaGeDecay0InterfaceMessenger(this);
00031
00032 G4ThreeVector zero;
00033 particle_position = zero;
00034 particle_time = 0.0;
00035 }
00036
00037 MaGeDecay0Interface::~MaGeDecay0Interface()
00038 {
00039 delete fTheMessenger;
00040 if (fInputFile.is_open()) fInputFile.close();
00041 }
00042
00043 void MaGeDecay0Interface::OpenFile()
00044 {
00045 char header[160];
00046 fInputFile.open(fFileName);
00047
00048 if (!(fInputFile.is_open())) {
00049 MJLog(error) << "File not valid!" << endlog;
00050 MJLog(fatal) << endlog;
00051 }
00052 G4int ifound;
00053 MJLog(debugging) << "Decay0 file " << fFileName << " found" << endlog;
00054
00055
00056 for (G4int j=0;;j++)
00057 {
00058 fInputFile.getline(header,160);
00059 ifound = strncmp (header," First",6);
00060 if (!ifound) break;
00061 if (j == 50) {
00062 MJLog(error) << "Corrupted file? " << endlog;
00063 MJLog(fatal) << endlog;
00064 }
00065 }
00066
00067 fInputFile.getline(header,160);
00068 fInputFile.getline(header,160);
00069 MJLog(trace) << "Decay0 file " << fFileName << " initialized" << endlog;
00070 }
00071
00072 void MaGeDecay0Interface::ChangeFileName(G4String newFile)
00073 {
00074 if (fFileName != newFile)
00075 {
00076 if (fInputFile.is_open()) fInputFile.close();
00077 fFileName = newFile;
00078 OpenFile();
00079 }
00080 }
00081
00082 void MaGeDecay0Interface::GeneratePrimaryVertex(G4Event* evt)
00083 {
00084 if (!(fInputFile.is_open())) {
00085 MJLog(error) << "File must be set!" << endlog;
00086 MJLog(fatal) << endlog;
00087 }
00088 G4int nEntries=0;
00089 G4int nEvent=0;
00090 G4double time=0.0;
00091 G4String particleName;
00092 G4double px,py,pz,relativeTime;
00093 G4int particleCode;
00094
00095 fInputFile >> nEvent >> time >> nEntries;
00096
00097 if (fInputFile.eof())
00098 {
00099 fInputFile.close();
00100 MJLog(error) << "File over: not enough events!" << endlog;
00101 MJLog(fatal) << endlog;
00102 return;
00103 }
00104
00105
00106 particle_time = time*s;
00107
00108 G4PrimaryVertex* vertex = new G4PrimaryVertex (particle_position,
00109 particle_time);
00110
00111 G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable();
00112
00113 for (G4int i=0;i<nEntries;i++)
00114 {
00115 fInputFile >> particleCode >> px >> py >> pz >> relativeTime;
00116
00117 particleName = ConvertCodeToName(particleCode);
00118
00119
00120 G4PrimaryParticle* particle =
00121 new G4PrimaryParticle(theParticleTable->FindParticle(particleName),
00122 px*MeV,py*MeV,pz*MeV);
00123
00124 particle->SetProperTime(relativeTime*s+particle_time);
00125
00126
00127 vertex->SetPrimary(particle);
00128 }
00129
00130 evt->AddPrimaryVertex(vertex);
00131 }
00132
00133 G4String MaGeDecay0Interface::ConvertCodeToName(G4int code)
00134 {
00135 if (code == 1)
00136 return "gamma";
00137 else if (code == 2)
00138 return "e+";
00139 else if (code == 3)
00140 return "e-";
00141 else if (code == 47)
00142 return "alpha";
00143 else
00144 MJLog(error) << "Unknown particle code" << endlog;
00145 return " ";
00146 }
00147
00148