-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart 9
561 lines (326 loc) · 11.7 KB
/
Part 9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//construction.cc
#include "construction.hh"
MyDetectorConstruction::MyDetectorConstruction()
{}
MyDetectorConstruction::~MyDetectorConstruction()
{}
G4VPhysicalVolume *MyDetectorConstruction::Construct()
{
G4NistManager *nist = G4NistManager::Instance();
G4Material *SiO2 =new G4Material ("SiO2", 2.201*g/cm3,2);
SiO2->AddElement(nist->FindOrBuildElement("Si"),1);
SiO2->AddElement(nist->FindOrBuildElement("O"),2);
G4Material *H2O =new G4Material ("H2O", 1.000*g/cm3,2);
H2O->AddElement(nist->FindOrBuildElement("H"),2);
H2O->AddElement(nist->FindOrBuildElement("O"),1);
G4Element *C = nist->FindOrBuildElement("C");
G4Material *Aerogel = new G4Material("Aerogel",0.200*g/cm3,3);
Aerogel -> AddMaterial(SiO2,62.5*perCent);
Aerogel-> AddMaterial(H2O,37.4*perCent);
Aerogel-> AddElement(C,0.1*perCent);
G4double energy[2] = {1.239841939*eV/0.2 ,1.239841939*eV/0.9};
G4double rindexAerogel[2]={1.1,1.1};
G4double rindexWorld[2]={1.0,1.0};
G4MaterialPropertiesTable *mptAerogel =new G4MaterialPropertiesTable();
mptAerogel->AddProperty("RINDEX",energy,rindexAerogel,2);
Aerogel->SetMaterialPropertiesTable(mptAerogel);
G4Material *worldMat= nist->FindOrBuildMaterial("G4_AIR");
G4MaterialPropertiesTable *mptWorld= new G4MaterialPropertiesTable();
mptWorld->AddProperty("RINDEX",energy,rindexWorld, 2);
worldMat->SetMaterialPropertiesTable(mptWorld);
G4Box *solidworld=new G4Box("solidworld",0.5*m,0.5*m,0.5*m);
G4LogicalVolume *logicworld=new G4LogicalVolume(solidworld,worldMat,"logicWorld");
G4VPhysicalVolume *physWorld= new G4PVPlacement(0, G4ThreeVector(0.,0.,0.),logicworld,"physworld",0,false,0,true);
G4Box *solidRadiator =new G4Box ("solidRadiator", 0.4*m ,0.4*m, 0.05*m);
G4LogicalVolume *logicRadiator =new G4LogicalVolume(solidRadiator,Aerogel, "logicalRadiator");
G4VPhysicalVolume *physRadiator= new G4PVPlacement(0, G4ThreeVector(0.,0.,0.),logicRadiator,"physRadiator",logicworld,false,0,true);
G4Box *solidDetector =new G4Box("solidDetector",0.005*m,0.005*m,0.005*m); //solid detector define
logicDetector =new G4LogicalVolume(solidDetector, worldMat , "logicDetector");
for (G4int i=0;i<100;i++)
{
for(G4int j = 0; j<100;j++)
{
G4VPhysicalVolume *phyDetector=new G4PVPlacement(0, G4ThreeVector(-0.5*m+(i+0.5)*m/100,-0.5*m+(j+0.5)*m/100,0.49*m),logicDetector,"physDetector",logicworld,false,j+i*100,true);
}
}
return physWorld;
}
void MyDetectorConstruction::ConstructSDandField()
{
MySensitiveDetector *sensDet=new MySensitiveDetector("SensitiveDetector");
logicDetector->SetSensitiveDetector(sensDet);
}
//generator.cc
#include "generator.hh"
MyPrimaryGenerator::MyPrimaryGenerator()
{
fParticleGun = new G4ParticleGun(1);
G4ParticleTable *particleTable=G4ParticleTable::GetParticleTable();
G4String particleName="proton";
G4ParticleDefinition *particle =particleTable->FindParticle("proton");
G4ThreeVector pos(0.,0.,0.);
G4ThreeVector mom(0.,0.,1.);
fParticleGun->SetParticlePosition(pos);
fParticleGun->SetParticleMomentumDirection(mom);
fParticleGun->SetParticleMomentum(100. *GeV);
fParticleGun->SetParticleDefinition(particle);
// G4ParticleTable *particleTable = G4ParticleTable::GetParticleTable();
// G4ParticleDefinition *particle = particleTable->FindParticle("chargedgeantino");
// G4ThreeVector pos(0.,0.,-20.*km);
// G4ThreeVector mom(0.,0.,1.);
// fParticleGun->SetParticlePosition(pos);
// fParticleGun->SetParticleMomentumDirection(mom);
// fParticleGun->SetParticleMomentum(100.*GeV);
// fParticleGun->SetParticleDefinition(particle);
}
MyPrimaryGenerator::~MyPrimaryGenerator()
{
delete fParticleGun;
}
void MyPrimaryGenerator::GeneratePrimaries(G4Event *anEvent)
{
fParticleGun->GeneratePrimaryVertex(anEvent);
// G4ParticleDefinition* particle = fParticleGun->GetParticleDefinition();
//if(particle == G4ChargedGeantino::ChargedGeantino())
//{
// G4int Z = 27;
// G4int A = 60;
// G4double charge = 0.*eplus;
// G4double energy = 0.*keV;
// G4ParticleDefinition* ion = G4IonTable::GetIonTable()->GetIon(Z,A,energy);
// fParticleGun->SetParticleDefinition(ion);
// fParticleGun->SetParticleCharge(charge);
// }
// fParticleGun->GeneratePrimaryVertex(anEvent);
}
//generator.hh
#ifndef GENERATOR_HH
#define GENERATOR_HH
#include "G4VUserPrimaryGeneratorAction.hh"
#include "G4ParticleGun.hh"
#include "G4SystemOfUnits.hh"
#include "G4ParticleTable.hh"
//#include "G4IonTable.hh"
//#include "G4ChargedGeantino.hh"
//#include "Randomize.hh"
class MyPrimaryGenerator : public G4VUserPrimaryGeneratorAction
{
public:
MyPrimaryGenerator();
~MyPrimaryGenerator();
virtual void GeneratePrimaries(G4Event*);
private:
G4ParticleGun *fParticleGun;
};
#endif
//detector.hh
#ifndef DETECTOR_HH
#define DETECTOR_HH
#include "G4VSensitiveDetector.hh"
#include "G4RunManager.hh"
#include "G4SystemOfUnits.hh"
#include "g4root.hh"
class MySensitiveDetector : public G4VSensitiveDetector
{
public:
MySensitiveDetector(G4String);
~MySensitiveDetector();
private:
virtual G4bool ProcessHits(G4Step *, G4TouchableHistory *);
// G4PhysicsOrderedFreeVector *quEff;
};
#endif
//detector.cc
#include "detector.hh"
#include "g4root.hh"
MySensitiveDetector::MySensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{
/*quEff = new G4PhysicsOrderedFreeVector();
std::ifstream datafile;
datafile.open("eff.dat");
while(1)
{
G4double wlen, queff;
datafile >> wlen >> queff;
if(datafile.eof())
break;
G4cout << wlen << " " << queff << std::endl;
quEff->InsertValues(wlen, queff/100.);*/
}
// datafile.close();
// quEff->SetSpline(false);}
MySensitiveDetector::~MySensitiveDetector()
{}
G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{
G4Track *track = aStep->GetTrack();
track->SetTrackStatus(fStopAndKill); //when photon hits the detector it kills photon's trajectory
G4StepPoint *preStepPoint = aStep->GetPreStepPoint(); //photon's start point
G4StepPoint *postStepPoint = aStep->GetPostStepPoint(); //photon's end point
G4ThreeVector posPhoton = preStepPoint->GetPosition();
/* G4ThreeVector momPhoton = preStepPoint->GetMomentum();
G4double time = preStepPoint->GetGlobalTime();
G4double wlen = (1.239841939*eV/momPhoton.mag())*1E+03;*/
// G4cout << "Photon position: " << posPhoton << G4endl;
const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();
G4int copyNo = touchable->GetCopyNumber();
//G4cout << "Copy number: " << copyNo << G4endl;
G4VPhysicalVolume *physVol = touchable->GetVolume();
G4ThreeVector posDetector = physVol->GetTranslation();
G4cout << "Detector position: " << posDetector << G4endl;
// #ifndef G4MULTITHREADED
//G4cout << "Photon wavelength: " << wlen << G4endl;
/*#endif*/
G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
G4AnalysisManager *man = G4AnalysisManager::Instance();
// man->FillNtupleIColumn(0, 0, evt);
//man->FillNtupleDColumn(0, 1, posPhoton[0]);
//man->FillNtupleDColumn(0, 2, posPhoton[1]);
//man->FillNtupleDColumn(0, 3, posPhoton[2]);
// man->FillNtupleDColumn(0, 4, time);
//man->FillNtupleDColumn(0, 5, wcslen);
// man->AddNtupleRow(0);
/* if(G4UniformRand() < quEff->Value(wlen))*/
{
man->FillNtupleIColumn(0, evt);
man->FillNtupleDColumn(1, posDetector[0]);
man->FillNtupleDColumn(2, posDetector[1]);
man->FillNtupleDColumn(3, posDetector[2]);
man->AddNtupleRow(0);
}
return true;
}
//sim.cc
#include <iostream>
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "G4VisManager.hh"
#include "G4VisExecutive.hh"
#include "G4UIExecutive.hh"
#include "construction.cc"
#include "physics.cc"
#include "action.cc"
#include "generator.cc"
#include "detector.cc"
#include "run.cc"
int main(int argc, char** argv)
{ G4RunManager *runManager = new G4RunManager();
runManager->SetUserInitialization(new MyDetectorConstruction());
runManager->SetUserInitialization(new MyPhysicsList());
runManager->SetUserInitialization(new MyActionInitialization());
runManager->Initialize();
G4UIExecutive *ui = 0;
if(argc==1){
ui= new G4UIExecutive(argc, argv);
}
G4VisManager *vismanager =new G4VisExecutive ();
vismanager->Initialize();
G4UImanager *UImanager = G4UImanager::GetUIpointer();
if(ui)
{
UImanager->ApplyCommand("/control/execute vis.mac");
ui->SessionStart();
}
else
{
G4String command="/control/execute ";
G4String fileName=argv[1];
UImanager->ApplyCommand(command+fileName);
}
return 0;
}
//vis.mac
/run/initialize
/vis/open OGL
/vis/viewer/set/viewpointVector 1 1 1
/vis/drawVolume
/vis/viewer/set/autoRefresh true
/vis/scene/add/trajectories smooth
/vis/scene/endOfEventAction accumulate
/vis/scene/add/scale 10 cm
/vis/scene/add/axes
/vis/scene/add/eventID
/*/gun/particle proton
/vis/ogl/set/displayListLimit 5000000*/
//CMakeLists.txt
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(Simulation)
find_package(Geant4 REQUIRED ui_all vis_all)
include(${Geant4_USE_FILE})
file(GLOB sources ${PROJECT_SOURCE_DIR}/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/*.hh)
file(GLOB MACRO_FILES
"*.mac"
)
file(COPY ${MACRO_FILES} DESTINATION ${PROJECT_BINARY_DIR})
add_executable(sim sim.cc ${source} ${headers})
target_link_libraries(sim ${Geant4_LIBRARIES})
add_custom_target(Simulation DEPENDS sim)
//run.mac
/run/initialize
/gun/momentumAmp 0.5 GeV
/run/beamOn 100
/gun/momentumAmp 1 GeV
/run/beamOn 100
/gun/momentumAmp 5 GeV
/run/beamOn 100
//run.cc
#include "run.hh"
MyRunAction::MyRunAction()
{ G4AnalysisManager *man = G4AnalysisManager::Instance();
man->CreateNtuple("Hits", "Hits");
man->CreateNtupleIColumn("fEvent");
man->CreateNtupleDColumn("fX");
man->CreateNtupleDColumn("fY");
man->CreateNtupleDColumn("fZ");
man->FinishNtuple(0);
}
MyRunAction::~MyRunAction()
{}
void MyRunAction::BeginOfRunAction(const G4Run* run)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();
G4int runID = run->GetRunID();
std::stringstream strRunID;
strRunID << runID;
man->OpenFile("output"+strRunID.str()+".root");
//man->CreateNtuple("Photons", "Photons");
//man->CreateNtupleIColumn("fEvent"); //N means integer
//man->CreateNtupleDColumn("fX");
//man->CreateNtupleDColumn("fY");
//man->CreateNtupleDColumn("fZ");
// man->CreateNtupleDColumn("fT");
// man->CreateNtupleDColumn("fWlen");
// man->FinishNtuple(0);
//man->CreateNtuple("Scoring", "Scoring");
//man->CreateNtupleDColumn("fEdep");
//man->FinishNtuple(2);
/*
G4AnalysisManager *man = G4AnalysisManager::Instance();
G4int runID = run->GetRunID();
std::stringstream strRunID;
strRunID << runID;
man->OpenFile("output"+strRunID.str()+".root");
*/
}
void MyRunAction::EndOfRunAction(const G4Run*)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();
man->Write();
man->CloseFile();
}
//run.hh
#ifndef RUN_HH //header file
#define RUN_HH
#include "G4UserRunAction.hh"
#include "G4Run.hh"
#include "g4root.hh"
class MyRunAction : public G4UserRunAction
{
public:
MyRunAction();
~MyRunAction();
virtual void BeginOfRunAction(const G4Run*);
virtual void EndOfRunAction(const G4Run*);
};
#endif