VMC Examples
Version 6.8
Toggle main menu visibility
Loading...
Searching...
No Matches
examples
Monopole
src
MCApplication.cxx
Go to the documentation of this file.
1
//------------------------------------------------
2
// The Virtual Monte Carlo examples
3
// Copyright (C) 2018 Ivana Hrivnacova
4
// All rights reserved.
5
//
6
// For the licensing terms see geant4_vmc/LICENSE.
7
// Contact: root-vmc@cern.ch
8
//-------------------------------------------------
9
10
/// \file MCApplication.cxx
11
/// \brief Implementation of the MCApplication class
12
///
13
/// Geant4 Monopole example adapted to Virtual Monte Carlo \n
14
///
15
/// \date 15/07/2018
16
/// \author I. Hrivnacova; IPN, Orsay
17
18
#include "MCApplication.h"
19
#include "DetectorConstruction.h"
20
#include "Ex03MCStack.h"
21
22
#include <TMCRootManager.h>
23
24
#include <Riostream.h>
25
#include <TDatabasePDG.h>
26
#include <TGeoManager.h>
27
#include <TGeoMaterial.h>
28
#include <TH1.h>
29
#include <TInterpreter.h>
30
#include <TLorentzVector.h>
31
#include <TROOT.h>
32
#include <TRandom.h>
33
#include <TVirtualMC.h>
34
35
using namespace
std;
36
37
/// \cond CLASSIMP
38
ClassImp(
VMC::Monopole::MCApplication
)
39
/// \endcond
40
41
namespace
42
{
43
int
G4lrint(
double
ad)
44
{
45
return
(ad > 0) ?
static_cast<
int
>
(ad + .5) : static_cast<int>(ad - .5);
46
}
47
}
48
49
namespace
VMC
50
{
51
namespace
Monopole
52
{
53
54
std::vector<TH1D*>
fHistograms
;
55
56
//_____________________________________________________________________________
57
MCApplication::MCApplication
(
const
char
* name,
const
char
* title)
58
:
TVirtualMCApplication
(name, title),
59
fRootManager
(0),
60
fStack
(0),
61
fDetConstruction
(0),
62
fMagField
(0),
63
fBinSize
(0.),
64
fOffsetX
(0.),
65
fProjRange
(0.),
66
fProjRange2
(0.),
67
fImedAl
(0),
68
fNofEvents
(0),
69
fIsMaster
(kTRUE)
70
{
71
/// Standard constructor
72
/// \param name The MC application name
73
/// \param title The MC application description
74
75
// Create a user stack
76
fStack
=
new
Ex03MCStack
(1000);
77
78
// Create detector construction
79
fDetConstruction
=
new
DetectorConstruction
();
80
81
// Constant magnetic field (in kiloGauss)
82
fMagField
=
new
TGeoUniformMagField(0, 0, 2);
83
}
84
85
//_____________________________________________________________________________
86
MCApplication::MCApplication
(
const
MCApplication
& origin)
87
:
TVirtualMCApplication
(origin.GetName(), origin.GetTitle()),
88
fRootManager
(0),
89
fStack
(0),
90
fDetConstruction
(origin.
fDetConstruction
),
91
fMagField
(0),
92
fImedAl
(origin.
fImedAl
),
93
fNofEvents
(0),
94
fIsMaster
(kFALSE)
95
{
96
/// Copy constructor for cloning application on workers (in multithreading
97
/// mode) \param origin The source MC application
98
99
// Create new user stack
100
fStack
=
new
Ex03MCStack
(1000);
101
102
// Constant magnetic field (in kiloGauss)
103
fMagField
=
new
TGeoUniformMagField(origin.
fMagField
->GetFieldValue()[0],
104
origin.
fMagField
->GetFieldValue()[1], origin.
fMagField
->GetFieldValue()[2]);
105
}
106
107
//_____________________________________________________________________________
108
MCApplication::MCApplication
()
109
:
TVirtualMCApplication
(),
110
fRootManager
(0),
111
fStack
(0),
112
fDetConstruction
(0),
113
fMagField
(0),
114
fBinSize
(0.5),
115
fImedAl
(0),
116
fNofEvents
(0),
117
fIsMaster
(kTRUE)
118
{
119
/// Default constructor
120
}
121
122
//_____________________________________________________________________________
123
MCApplication::~MCApplication
()
124
{
125
/// Destructor
126
127
delete
fStack
;
128
if
(
fIsMaster
)
delete
fDetConstruction
;
129
delete
fMagField
;
130
delete
gMC;
131
}
132
133
//
134
// private methods
135
//
136
137
//_____________________________________________________________________________
138
void
MCApplication::RegisterStack
()
const
139
{
140
/// Register stack in the Root manager.
141
142
if
(
fRootManager
) {
143
// cout << "MCApplication::RegisterStack: " << endl;
144
fRootManager
->Register(
"stack"
,
"Ex03MCStack"
, &
fStack
);
145
}
146
}
147
148
//
149
// public methods
150
//
151
152
//_____________________________________________________________________________
153
void
MCApplication::InitMC
(
const
char
* setup)
154
{
155
/// Initialize MC.
156
/// The selection of the concrete MC is done in the macro.
157
/// \param setup The name of the configuration macro
158
159
if
(TString(setup) !=
""
) {
160
gROOT->LoadMacro(setup);
161
gInterpreter->ProcessLine(
"Config()"
);
162
if
(!gMC) {
163
Fatal(
164
"InitMC"
,
"Processing Config() has failed. (No MC is instantiated.)"
);
165
}
166
}
167
168
// MT support available from root v 5.34/18
169
#if ROOT_VERSION_CODE >= 336402
170
// Create Root manager
171
if
(!gMC->IsMT()) {
172
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
173
// fRootManager->SetDebug(true);
174
}
175
#else
176
// Create Root manager
177
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
178
// fRootManager->SetDebug(true);
179
#endif
180
181
gMC->SetStack(
fStack
);
182
gMC->SetMagField(
fMagField
);
183
gMC->Init();
184
gMC->BuildPhysics();
185
186
// Notify detector construction about initialization
187
fDetConstruction
->SetGeometryInitialized(
true
);
188
189
// initialize projected range, tallies, Ebeam, and book histograms
190
// Double_t maxStepSize = 0.5;
191
fProjRange
= 0.;
192
fProjRange2
= 0.;
193
194
Double_t length =
fDetConstruction
->GetAbsorberSizeX() * 10.;
// in mm
195
Double_t binSize =
fBinSize
* 10.;
// in mm
196
Int_t numBins = G4lrint(length / binSize);
197
198
// Define offset for filling histogram
199
fOffsetX
= -0.5 * length;
// in mm
200
201
// cout << "numBins, length, offsetX "
202
// << numBins << ", " << length << ", " << fOffsetX << endl;
203
204
// Create histograms
205
// fHistograms.push_back(new TH1D("Edep_x", "Edep (MeV/mm) along absorber
206
// (mm)", numBins, 0, length));
207
fHistograms
.push_back(
208
new
TH1D(
"h1"
,
"Edep (MeV/mm) along absorber (mm)"
, numBins, 0, length));
209
// fHistograms.push_back(new TH1D("h2", "DEDX (MeV/mm) of proton", 100,
210
// -3., 7.)); fHistograms.push_back(new TH1D("h3", "DEDX (MeV/mm) of
211
// monopole", 100, -3., 7.)); fHistograms.push_back(new TH1D("h4", "Range(mm)
212
// of proton", 100, -3., 7.)); fHistograms.push_back(new TH1D("h5", "Range(mm)
213
// of monopole", 100, -3., 7.)); Control histograms fHistograms.push_back(new
214
// TH1D("x", "x", 100, -10, 10)); fHistograms.push_back(new TH1D("Edep (MeV)",
215
// "Edep (Mev)", 100, 0, 100));
216
217
RegisterStack
();
218
}
219
220
//__________________________________________________________________________
221
void
MCApplication::RunMC
(Int_t nofEvents)
222
{
223
/// Run MC.
224
/// \param nofEvents Number of events to be processed
225
226
gMC->ProcessRun(nofEvents);
227
fNofEvents
= nofEvents;
228
FinishRun
();
229
}
230
231
//_____________________________________________________________________________
232
void
MCApplication::FinishRun
()
233
{
234
/// Finish MC run.
235
236
// Get monopole mass from TDatabasePDG
237
TDatabasePDG* pdgDB = TDatabasePDG::Instance();
238
TParticlePDG* monopole = pdgDB->GetParticle(60000000);
239
Double_t mass = 0;
240
if
(monopole) {
241
mass = monopole->Mass();
242
}
243
244
// run conditions
245
cout <<
"\n The run consists of "
<<
fNofEvents
<<
" monopole "
246
<<
" of "
<< mass <<
" GeV "
247
<<
" through "
<<
fDetConstruction
->GetAbsorberSizeX() <<
" cm of "
248
<<
fDetConstruction
->GetAbsorberMaterial()
249
// << " (density: " << G4BestUnit(density,"Volumic Mass") << ")"
250
<< endl;
251
252
// compute projected range and straggling
253
254
fProjRange
/=
fNofEvents
;
255
fProjRange2
/=
fNofEvents
;
256
Double_t rms =
fProjRange2
-
fProjRange
*
fProjRange
;
257
if
(rms > 0.) {
258
rms = std::sqrt(rms);
259
}
260
else
{
261
rms = 0.;
262
}
263
264
cout.precision(5);
265
cout <<
"\n projected Range= "
<<
fProjRange
<<
" cm "
266
<<
" rms= "
<< rms <<
" cm "
<< endl;
267
268
/*
269
G4double ekin[100], dedxproton[100], dedxmp[100];
270
G4EmCalculator calc;
271
calc.SetVerbose(0);
272
G4int i;
273
for(i = 0; i < 100; ++i) {
274
ekin[i] = std::pow(10., 0.1*G4double(i)) * keV;
275
dedxproton[i] =
276
calc.ComputeElectronicDEDX(ekin[i], "proton", matName);
277
dedxmp[i] =
278
calc.ComputeElectronicDEDX(ekin[i], "monopole", matName);
279
}
280
281
if(GetVerbose() > 0){
282
G4cout << "### Stopping Powers" << G4endl;
283
for(i=0; i<100; i++) {
284
G4cout << " E(MeV)= " << ekin[i] << " dedxp= " << dedxproton[i]
285
<< " dedxmp= " << dedxmp[i]
286
<< G4endl;
287
}
288
}
289
G4cout << "### End of stopping power table" << G4endl;
290
291
if(GetVerbose() > 0){
292
G4cout << "Range table for " << matName << G4endl;
293
}
294
295
for(i=0; i<100; ++i) {
296
G4double e = std::log10(ekin[i] / MeV) + 0.05;
297
fHisto->Fill(1, e, dedxproton[i]);
298
fHisto->Fill(2, e, dedxmp[i]);
299
fHisto->Fill(3, e,
300
std::log10(calc.GetRange(ekin[i],"proton",matName)/mm)); fHisto->Fill(4, e,
301
std::log10(calc.GetRange(ekin[i],"monopole",matName)/mm));
302
}
303
*/
304
// normalize histogram
305
// G4double fac = (mm/MeV) / (nEvents * fBinLength);
306
Double_t fac = 1. / (
fNofEvents
*
fBinSize
* 10.);
307
fHistograms
[0]->Scale(fac);
308
309
if
(
fRootManager
) {
310
fRootManager
->WriteAll();
311
fRootManager
->Close();
312
}
313
}
314
315
/*
316
317
//_____________________________________________________________________________
318
TVirtualMCApplication* MCApplication::CloneForWorker() const
319
{
320
return new MCApplication(*this);
321
}
322
323
//_____________________________________________________________________________
324
void MCApplication::InitOnWorker()
325
{
326
// Create Root manager
327
Int_t threadRank = 1;
328
// The real thread rank will be set in MCRootManager
329
fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite, threadRank);
330
// fRootManager->SetDebug(true);
331
332
gMC->SetStack(fStack);
333
gMC->SetMagField(fMagField);
334
335
RegisterStack();
336
}
337
338
//_____________________________________________________________________________
339
void MCApplication::FinishRunOnWorker()
340
{
341
//cout << "Ex03MCApplication::FinishWorkerRun: " << endl;
342
if ( fRootManager ) {
343
fRootManager->WriteAll();
344
fRootManager->Close();
345
}
346
}
347
*/
348
349
//_____________________________________________________________________________
350
void
MCApplication::ConstructGeometry
()
351
{
352
/// Construct geometry using detector contruction class.
353
/// The detector contruction class is using TGeo functions.
354
355
fDetConstruction
->ConstructGeometry();
356
}
357
358
//_____________________________________________________________________________
359
void
MCApplication::InitGeometry
()
360
{
361
/// Initialize geometry.
362
363
fImedAl
= gMC->MediumId(
"Aluminium"
);
364
}
365
366
//_____________________________________________________________________________
367
void
MCApplication::GeneratePrimaries
()
368
{
369
/// Fill the user stack (derived from TVirtualMCStack) with primary particles.
370
371
// Track ID (filled by stack)
372
Int_t ntr;
373
374
// Option: to be tracked
375
Int_t toBeDone = 1;
376
377
// Monopole
378
Int_t pdg = 60000000;
379
380
// Polarization
381
Double_t polx = 0.;
382
Double_t poly = 0.;
383
Double_t polz = 0.;
384
385
// Position
386
Double_t vx = -0.5 * (
fDetConstruction
->GetWorldSizeX()) + 1e-10;
// + 1*um
387
Double_t vy = 0.;
388
Double_t vz = 0.;
389
Double_t tof = 0.08;
390
391
// Energy (in GeV)
392
Double_t kinEnergy = 100.;
393
Double_t mass = 100.;
394
Double_t e = mass + kinEnergy;
395
396
// Particle momentum
397
Double_t px, py, pz;
398
px = sqrt(e * e - mass * mass);
399
py = 0.;
400
pz = 0.;
401
402
// Add particle to stack
403
fStack
->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
404
poly, polz, kPPrimary, ntr, 1., 0);
405
}
406
407
//_____________________________________________________________________________
408
void
MCApplication::BeginEvent
()
409
{
410
/// User actions at beginning of event.
411
/// Nothing to be done this example
412
}
413
414
//_____________________________________________________________________________
415
void
MCApplication::BeginPrimary
()
416
{
417
/// User actions at beginning of a primary track.
418
/// Nothing to be done this example
419
}
420
421
//_____________________________________________________________________________
422
void
MCApplication::PreTrack
()
423
{
424
/// User actions at beginning of each track.
425
/// Print info message.
426
427
// Check the monopole properties set via configuration macro
428
// cout << endl;
429
// std::cout << "&&&&& Track mass: " << gMC->TrackMass() << std::endl;
430
// std::cout << "&&&&& Track charge: " << gMC->TrackCharge() << std::endl;
431
}
432
433
//_____________________________________________________________________________
434
void
MCApplication::Stepping
()
435
{
436
/// User actions at each step.
437
/// Print track position, the current volume and current medium names.
438
439
static
TLorentzVector prevPosition;
440
441
TLorentzVector position;
442
gMC->TrackPosition(position);
443
444
Double_t edep = gMC->Edep();
445
if
(edep <= 0.) {
446
prevPosition = position;
447
return
;
448
}
449
450
edep *= 1.e03;
// convert in MeV
451
452
// Bragg curve
453
Double_t x = position.X() * 10.;
454
Double_t dx = (position.X() - prevPosition.X()) * 10.;
455
// cout << "prevPosition "
456
// << prevPosition.X() << ", " << prevPosition.Y() << ", " <<
457
// prevPosition.Z() << endl;
458
// cout << "position "
459
// << position.X() << ", " << position.Y() << ", " << position.Z() <<
460
// endl;
461
// cout << "dx= " << dx << endl;
462
463
Double_t random = gRandom->Uniform(0, 1);
464
x += dx * random -
fOffsetX
;
465
466
fHistograms
[0]->Fill(x, edep);
467
// fHistograms[1]->Fill(x);
468
// fHistograms[2]->Fill(edep);
469
470
// keep position from previous step
471
prevPosition = position;
472
}
473
474
//_____________________________________________________________________________
475
void
MCApplication::PostTrack
()
476
{
477
/// User actions after finishing of each track
478
479
// Skip secondary tracks
480
if
(
fStack
->GetCurrentParentTrackNumber() != -1)
return
;
481
482
TLorentzVector position;
483
gMC->TrackPosition(position);
484
485
Double_t x = position.X() -
fOffsetX
/ 10.;
// in cm
486
fProjRange
+= x;
487
fProjRange2
+= x * x;
488
}
489
490
//_____________________________________________________________________________
491
void
MCApplication::FinishPrimary
()
492
{
493
/// User actions after finishing of a primary track.
494
/// Nothing to be done this example
495
}
496
497
//_____________________________________________________________________________
498
void
MCApplication::FinishEvent
()
499
{
500
/// User actions after finishing of an event
501
502
// fRootManager->Fill();
503
fStack
->Reset();
504
}
505
506
//_____________________________________________________________________________
507
void
MCApplication::SetBinSize
(Double_t binSize)
508
{
509
/// Set Edep histogram bin size (in cm)
510
511
if
(
fDetConstruction
->GetGeometryInitialized()) {
512
cerr <<
"Geometry alredy initialized: cannot set Edep histogram bin size"
513
<< endl;
514
return
;
515
}
516
517
fBinSize
= binSize;
518
}
519
520
}
// namespace Monopole
521
}
// namespace VMC
Ex03MCStack
Implementation of the TVirtualMCStack interface.
Definition
Ex03MCStack.h:36
TVirtualMCApplication
VMC::Monopole::DetectorConstruction
The detector construction (via TGeo ).
Definition
DetectorConstruction.h:38
VMC::Monopole::MCApplication
Implementation of the TVirtualMCApplication.
Definition
MCApplication.h:41
VMC::Monopole::MCApplication::Stepping
virtual void Stepping()
Definition
MCApplication.cxx:434
VMC::Monopole::MCApplication::MCApplication
MCApplication(const char *name, const char *title)
Definition
MCApplication.cxx:57
VMC::Monopole::MCApplication::fStack
Ex03MCStack * fStack
The VMC stack.
Definition
MCApplication.h:81
VMC::Monopole::MCApplication::InitMC
void InitMC(const char *setup)
Definition
MCApplication.cxx:153
VMC::Monopole::MCApplication::BeginEvent
virtual void BeginEvent()
Definition
MCApplication.cxx:408
VMC::Monopole::MCApplication::fImedAl
Int_t fImedAl
The Aluminium medium Id.
Definition
MCApplication.h:88
VMC::Monopole::MCApplication::SetBinSize
void SetBinSize(Double_t binSize)
Definition
MCApplication.cxx:507
VMC::Monopole::MCApplication::fBinSize
Double_t fBinSize
Edep histogram bin size.
Definition
MCApplication.h:84
VMC::Monopole::MCApplication::BeginPrimary
virtual void BeginPrimary()
Definition
MCApplication.cxx:415
VMC::Monopole::MCApplication::~MCApplication
virtual ~MCApplication()
Definition
MCApplication.cxx:123
VMC::Monopole::MCApplication::fProjRange
Double_t fProjRange
Projected range.
Definition
MCApplication.h:86
VMC::Monopole::MCApplication::FinishPrimary
virtual void FinishPrimary()
Definition
MCApplication.cxx:491
VMC::Monopole::MCApplication::RunMC
void RunMC(Int_t nofEvents)
Definition
MCApplication.cxx:221
VMC::Monopole::MCApplication::RegisterStack
void RegisterStack() const
Definition
MCApplication.cxx:138
VMC::Monopole::MCApplication::FinishEvent
virtual void FinishEvent()
Definition
MCApplication.cxx:498
VMC::Monopole::MCApplication::MCApplication
MCApplication()
Definition
MCApplication.cxx:108
VMC::Monopole::MCApplication::FinishRun
void FinishRun()
Definition
MCApplication.cxx:232
VMC::Monopole::MCApplication::PreTrack
virtual void PreTrack()
Definition
MCApplication.cxx:422
VMC::Monopole::MCApplication::fIsMaster
Bool_t fIsMaster
If is on master thread.
Definition
MCApplication.h:90
VMC::Monopole::MCApplication::fNofEvents
Int_t fNofEvents
Number of events.
Definition
MCApplication.h:89
VMC::Monopole::MCApplication::fOffsetX
Double_t fOffsetX
The Edep histogram offset.
Definition
MCApplication.h:85
VMC::Monopole::MCApplication::fDetConstruction
DetectorConstruction * fDetConstruction
Dector construction.
Definition
MCApplication.h:82
VMC::Monopole::MCApplication::PostTrack
virtual void PostTrack()
Definition
MCApplication.cxx:475
VMC::Monopole::MCApplication::ConstructGeometry
virtual void ConstructGeometry()
Definition
MCApplication.cxx:350
VMC::Monopole::MCApplication::fRootManager
TMCRootManager * fRootManager
Root manager.
Definition
MCApplication.h:80
VMC::Monopole::MCApplication::fMagField
TGeoUniformMagField * fMagField
Magnetic field.
Definition
MCApplication.h:83
VMC::Monopole::MCApplication::fProjRange2
Double_t fProjRange2
Projected range square.
Definition
MCApplication.h:87
VMC::Monopole::MCApplication::InitGeometry
virtual void InitGeometry()
Definition
MCApplication.cxx:359
VMC::Monopole::MCApplication::GeneratePrimaries
virtual void GeneratePrimaries()
Definition
MCApplication.cxx:367
VMC::Monopole
Definition
DetectorConstruction.h:29
VMC::Monopole::fHistograms
std::vector< TH1D * > fHistograms
Definition
MCApplication.cxx:54
VMC
Definition
FastSimulation.h:26
Generated on
for VMC Examples by
1.17.0