VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex06MCApplication.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2007 - 2014 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 Ex06MCApplication.cxx
11/// \brief Implementation of the Ex06MCApplication class
12///
13/// Geant4 ExampleN06 adapted to Virtual Monte Carlo
14///
15/// \date 16/05/2005
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include <Riostream.h>
19#include <TGeoManager.h>
20#include <TGeoUniformMagField.h>
21#include <TInterpreter.h>
22#include <TLorentzVector.h>
23#include <TROOT.h>
24#include <TVirtualGeoTrack.h>
25#include <TVirtualMC.h>
26
27#include "Ex03MCStack.h"
30#include "Ex06MCApplication.h"
32
33using namespace std;
34
35/// \cond CLASSIMP
36ClassImp(Ex06MCApplication)
37 /// \endcond
38
39 //_____________________________________________________________________________
40 Ex06MCApplication::Ex06MCApplication(const char* name, const char* title)
41 : TVirtualMCApplication(name, title),
42 fGammaCounter(0),
43 fFeedbackCounter(0),
44 fRunGammaCounter(0),
45 fRunFeedbackCounter(0),
46 fVerbose(0),
47 fStack(0),
48 fMagField(0),
49 fDetConstruction(0),
50 fPrimaryGenerator(0),
51 fOldGeometry(kFALSE),
52 fTestStackPopper(kFALSE),
53 fIsMaster(kTRUE)
54{
55 /// Standard constructor
56 /// \param name The MC application name
57 /// \param title The MC application description
58
59 // Create a user stack
60 fStack = new Ex03MCStack(1000);
61
62 // create magnetic field (with zero value)
63 fMagField = new TGeoUniformMagField();
64
65 // Create detector construction
66 fDetConstruction = new Ex06DetectorConstruction();
67
68 // Create a primary generator
69 fPrimaryGenerator = new Ex06PrimaryGenerator(fStack);
70}
71
72//_____________________________________________________________________________
74 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
75 fGammaCounter(0),
76 fFeedbackCounter(0),
77 fRunGammaCounter(0),
78 fRunFeedbackCounter(0),
79 fVerbose(origin.fVerbose),
80 fStack(0),
81 fMagField(0),
82 fDetConstruction(origin.fDetConstruction),
83 fPrimaryGenerator(0),
84 fOldGeometry(origin.fOldGeometry),
85 fTestStackPopper(origin.fTestStackPopper),
86 fIsMaster(kFALSE)
87{
88 /// Copy constructor (for clonig on worker thread in MT mode).
89 /// \param origin The source object (on master).
90
91 // Create a user stack
92 fStack = new Ex03MCStack(1000);
93
94 // create magnetic field (with zero value)
95 // TODO: check copying
96 fMagField = new TGeoUniformMagField();
97
98 // Create a primary generator
101}
102
103//_____________________________________________________________________________
106 fGammaCounter(0),
107 fFeedbackCounter(0),
108 fRunGammaCounter(0),
109 fRunFeedbackCounter(0),
110 fVerbose(0),
111 fStack(0),
112 fMagField(0),
113 fDetConstruction(0),
114 fPrimaryGenerator(0),
115 fOldGeometry(kFALSE),
116 fTestStackPopper(kFALSE),
117 fIsMaster(kTRUE)
118{
119 /// Default constructor
120}
121
122//_____________________________________________________________________________
124{
125 /// Destructor
126
127 // cout << "Ex06MCApplication::~Ex06MCApplication " << this << endl;
128
129 delete fStack;
130 delete fMagField;
131 if (fIsMaster) delete fDetConstruction;
132 delete fPrimaryGenerator;
133 delete gMC;
134
135 // cout << "Done Ex06MCApplication::~Ex06MCApplication " << this << endl;
136}
137
138//
139// public methods
140//
141
142//_____________________________________________________________________________
143void Ex06MCApplication::InitMC(const char* setup)
144{
145 /// Initialize MC.
146 /// The selection of the concrete MC is done in the macro.
147 /// \param setup The name of the configuration macro
148
149 fVerbose.InitMC();
150
151 if (TString(setup) != "") {
152 gROOT->LoadMacro(setup);
153 gInterpreter->ProcessLine("Config()");
154 if (!gMC) {
155 Fatal(
156 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
157 }
158 }
159
160 gMC->SetStack(fStack);
161 gMC->SetMagField(fMagField);
162 gMC->Init();
163 gMC->BuildPhysics();
164}
165
166//_____________________________________________________________________________
167void Ex06MCApplication::RunMC(Int_t nofEvents)
168{
169 /// Run MC.
170 /// \param nofEvents Number of events to be processed
171
172 fVerbose.RunMC(nofEvents);
173
174 gMC->ProcessRun(nofEvents);
175
176 FinishRun();
177}
178
179//_____________________________________________________________________________
184
185//_____________________________________________________________________________
187{
188 // cout << "Ex06MCApplication::InitForWorker " << this << endl;
189
190 // Set data to MC
191 gMC->SetStack(fStack);
192 gMC->SetMagField(fMagField);
193}
194
195//_____________________________________________________________________________
197{
198 // cout << "Ex06MCApplication::Merge " << this << endl;
199
200 Ex06MCApplication* ex06LocalMCApplication =
201 static_cast<Ex06MCApplication*>(localMCApplication);
202
203 fRunGammaCounter += ex06LocalMCApplication->fRunGammaCounter;
204 fRunFeedbackCounter += ex06LocalMCApplication->fRunFeedbackCounter;
205}
206
207//_____________________________________________________________________________
209{
210 /// Construct geometry using detector contruction class.
211 /// The detector contruction class is using TGeo functions or
212 /// TVirtualMC functions (if oldGeometry is selected)
213
214 fVerbose.ConstructGeometry();
215
216 // Cannot use Root geometry if not supported with
217 // selected MC
218 if (!fOldGeometry && !gMC->IsRootGeometrySupported()) {
219 cerr << "Selected MC does not support TGeo geometry" << endl;
220 cerr << "Exiting program" << endl;
221 exit(1);
222 }
223
224 if (!fOldGeometry) {
225 cout << "Geometry will be defined via TGeo" << endl;
228 }
229 else {
230 cout << "Geometry will be defined via VMC" << endl;
231 Ex06DetectorConstructionOld detConstructionOld;
232 detConstructionOld.ConstructMaterials();
233 detConstructionOld.ConstructGeometry();
234 }
235}
236
237//_____________________________________________________________________________
239{
240 /// Define material optical properties
241
242 fVerbose.ConstructGeometry();
243
245}
246
247//_____________________________________________________________________________
249{
250 /// Initialize geometry
251
252 fVerbose.InitGeometry();
253}
254
255//_____________________________________________________________________________
257{
258 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
259
260 fVerbose.GeneratePrimaries();
261
263}
264
265//_____________________________________________________________________________
267{
268 /// User actions at beginning of event
269
270 fVerbose.BeginEvent();
271
272 fGammaCounter = 0;
274}
275
276//_____________________________________________________________________________
278{
279 /// User actions at beginning of a primary track
280
281 fVerbose.BeginPrimary();
282}
283
284//_____________________________________________________________________________
286{
287 /// User actions at beginning of each track
288
289 fVerbose.PreTrack();
290
291 if (gMC->TrackPid() == 50000050) {
294 }
295 if (gMC->TrackPid() == 50000051) {
298 }
299}
300
301//_____________________________________________________________________________
303{
304 /// User actions at each step
305
306 fVerbose.Stepping();
307
308 // Work around for Fluka VMC, which does not call
309 // MCApplication::PreTrack()
310 //
311 static Int_t trackId = 0;
312 if (TString(gMC->GetName()) == "TFluka" &&
313 gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
314
315 fVerbose.PreTrack();
316 trackId = gMC->GetStack()->GetCurrentTrackNumber();
317 if (gMC->TrackPid() == 50000050) fGammaCounter++;
318 if (gMC->TrackPid() == 50000051) fFeedbackCounter++;
319 }
320
321 if (!fTestStackPopper) return;
322
323 // Stack popper test
324 // Add 1 feedback photon (50000051, fixed momentum) when a charged track
325 // enters TANK and 3 feedback photons (momentum in opposite direction to
326 // parent photon)
327 // when a photon is stopped in TANK
328
329 // Charged particles entering in TANK
330 TString volName = gMC->CurrentVolName();
331 if (volName != "TANK") return;
332
333 if ((gMC->TrackCharge() != 0.) && (gMC->IsTrackEntering())) {
334 // 1 keV
335 Double_t energy = 1e-06;
336 // TLorentzVector momentum(energy, 0., 0., energy);
337 // workaround for a problem in Geant4 11 in G4OpBoundaryProcess
338 TLorentzVector momentum(
339 energy, 0.1 * energy, 0.1 * energy, 0.98994949 * energy);
340 GenerateFeedback(1, momentum);
341 }
342
343 if ((gMC->TrackPid() == 50000050) && (gMC->Edep() > 0.)) {
344 TLorentzVector momentum;
345 gMC->TrackMomentum(momentum);
346 momentum = -1. * momentum;
347 GenerateFeedback(3, momentum);
348 }
349}
350
351//_____________________________________________________________________________
353 Int_t nofPhotons, TLorentzVector momentum)
354{
355 /// Generate FeedBack photons for the current particle.
356
357 for (Int_t i = 0; i < nofPhotons; ++i) {
358 // same position as the parent track
359 TLorentzVector position;
360 gMC->TrackPosition(position);
361 // Feedback photon
362 Int_t pdgEncoding = 50000051;
363 // Fixed polarization
364 Double_t polarization[3];
365 polarization[0] = 0.3;
366 polarization[1] = 0.4;
367 polarization[2] = 0.866025403784438597;
368
369 Int_t ntrack;
370 gMC->GetStack()->PushTrack(1, gMC->GetStack()->GetCurrentTrackNumber(),
371 pdgEncoding, momentum.X(), momentum.Y(), momentum.Z(), momentum.T(),
372 position.X(), position.Y(), position.Z(), position.T(), polarization[0],
373 polarization[1], polarization[2], kPFeedBackPhoton, ntrack, 1.0, 0);
374 }
375}
376
377//_____________________________________________________________________________
379{
380 /// User actions after finishing of each track
381
382 fVerbose.PostTrack();
383}
384
385//_____________________________________________________________________________
387{
388 /// User actions after finishing of a primary track
389
390 fVerbose.FinishPrimary();
391}
392
393//_____________________________________________________________________________
395{
396 /// User actions after finishing of an event
397
398 fVerbose.FinishEvent();
399
400 // Geant3 + TGeo
401 // (use TGeo functions for visualization)
402 if (TString(gMC->GetName()) == "TGeant3TGeo") {
403
404 // Draw volume
405 gGeoManager->SetVisOption(0);
406 gGeoManager->SetTopVisible();
407 gGeoManager->GetTopVolume()->Draw();
408
409 // Draw tracks (if filled)
410 // Available when this feature is activated via
411 // gMC->SetCollectTracks(kTRUE);
412 if (gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
413 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
414
415 gGeoManager->DrawTracks("/*"); // this means all tracks
416 }
417 }
418
419 cout << "Number of optical photons produced in this event : " << fGammaCounter
420 << endl;
421
422 if (fTestStackPopper) {
423 cout << "Number of feedback photons produced in this event : "
424 << fFeedbackCounter << endl;
425 }
426 fStack->Reset();
427}
428
429//_____________________________________________________________________________
431{
432 /// User actions after finishing of a run
433
434 fVerbose.FinishRun();
435
436 cout << "Number of optical photons produced in this run : "
437 << fRunGammaCounter << endl;
438
439 if (fTestStackPopper) {
440 cout << "Number of feedback photons produced in this run : "
441 << fRunFeedbackCounter << endl;
442 }
443}
Definition of the Ex06DetectorConstructionOld class.
Definition of the Ex06DetectorConstruction class.
Definition of the Ex06MCApplication class.
Definition of the Ex06PrimaryGenerator class.
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
The detector construction (via TGeo )
The detector construction (via TGeo )
Implementation of the TVirtualMCApplication.
Bool_t fTestStackPopper
Option for stack popper test.
void RunMC(Int_t nofEvents)
virtual void GeneratePrimaries()
Int_t fFeedbackCounter
Feedback photons counter.
TVirtualMagField * fMagField
The magnetic field.
void GenerateFeedback(Int_t nofPhotons, TLorentzVector momentum)
Int_t fRunGammaCounter
Optical photons counter2.
Int_t fRunFeedbackCounter
Feedback photons counter2.
virtual void BeginPrimary()
Bool_t fIsMaster
If is on master thread.
void InitMC(const char *setup)
Int_t fGammaCounter
Optical photons counter.
Ex06DetectorConstruction * fDetConstruction
Dector construction.
virtual void InitGeometry()
Bool_t fOldGeometry
Option for geometry definition.
virtual void BeginEvent()
virtual void ConstructGeometry()
virtual TVirtualMCApplication * CloneForWorker() const
Ex03MCStack * fStack
VMC stack.
virtual void InitOnWorker()
Ex06PrimaryGenerator * fPrimaryGenerator
Primary generator.
TMCVerbose fVerbose
VMC verbose helper.
virtual void FinishEvent()
virtual void FinishPrimary()
virtual void Merge(TVirtualMCApplication *localMCApplication)
virtual void ConstructOpGeometry()
The primary generator.