VMC Examples Version 6.8
Loading...
Searching...
No Matches
Ex02MCApplication.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 Ex02MCApplication.cxx
11/// \brief Implementation of the Ex02MCApplication class
12///
13/// Geant4 ExampleN02 adapted to Virtual Monte Carlo
14///
15/// \date 21/04/2002
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "Ex02MCApplication.h"
20#include "Ex02MCStack.h"
21#include "Ex02MagField.h"
22
23#include <Riostream.h>
24#include <TGeoManager.h>
25#include <TInterpreter.h>
26#include <TMCRootManager.h>
27#include <TPDGCode.h>
28#include <TROOT.h>
29#include <TVirtualGeoTrack.h>
30#include <TVirtualMC.h>
31
32using namespace std;
33
34/// \cond CLASSIMP
35ClassImp(Ex02MCApplication)
36 /// \endcond
37
38 //_____________________________________________________________________________
39 Ex02MCApplication::Ex02MCApplication(const char* name, const char* title)
40 : TVirtualMCApplication(name, title),
41 fRootManager(0),
42 fStack(0),
44 fTrackerSD(0),
45 fMagField(0),
46 fOldGeometry(kFALSE)
47{
48 /// Standard constructor
49 /// \param name The MC application name
50 /// \param title The MC application description
51
52 // cout << "Ex02MCApplication::Ex02MCApplication " << this << endl;
53
54 // Create application data
55
56 // Create SD
57 fTrackerSD = new Ex02TrackerSD("Tracker Chamber");
58 // Create a user stack
59 fStack = new Ex02MCStack(100);
60 // Constant magnetic field (in kiloGauss)
61 fMagField = new Ex02MagField(20., 0., 0.);
62 // It si also possible to use TGeoUniformMagField class:
63 // fMagField = new TGeoUniformMagField(20., 0., 0.);
64}
65
66//_____________________________________________________________________________
68 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
69 fRootManager(0),
70 fStack(0),
72 fTrackerSD(0),
73 fMagField(0),
74 fOldGeometry(kFALSE)
75{
76 /// Copy constructor (for clonig on worker thread in MT mode).
77 /// \param origin The source object (on master).
78
79 // cout << "Ex02MCApplication::Ex02MCApplication " << this << endl;
80
81 // Create application data
82
83 // Create SD
84 fTrackerSD = new Ex02TrackerSD(*(origin.fTrackerSD));
85 // Create a user stack
86 fStack = new Ex02MCStack(100);
87 // Constant magnetic field (in kiloGauss)
88 fMagField = new Ex02MagField(20., 0., 0.);
89 // It si also possible to use TGeoUniformMagField class:
90 // fMagField = new TGeoUniformMagField(20., 0., 0.);
91}
92
93//_____________________________________________________________________________
96 fRootManager(0),
97 fStack(0),
99 fTrackerSD(),
100 fMagField(0),
101 fOldGeometry(kFALSE)
102{
103 /// Default constructor
104}
105
106//_____________________________________________________________________________
108{
109 /// Destructor
110
111 // cout << "Ex02MCApplication::~Ex02MCApplication " << this << endl;
112
113 delete fRootManager;
114 delete fStack;
115 delete fTrackerSD;
116 delete fMagField;
117 delete gMC;
118
119 // cout << "Done Ex02MCApplication::~Ex02MCApplication " << this << endl;
120}
121
122//
123// private methods
124//
125//_____________________________________________________________________________
127{
128 /// Register stack in the Root manager.
129
130 if (fRootManager) {
131 // cout << "Ex02MCApplication::RegisterStack: " << endl;
132 fRootManager->Register("stack", "Ex02MCStack", &fStack);
133 }
134}
135
136//
137// public methods
138//
139
140//_____________________________________________________________________________
141void Ex02MCApplication::InitMC(const char* setup)
142{
143 /// Initialize MC from Config.C macro
144 /// The selection of the concrete MC is done in the macro.
145 /// \param setup The name of the configuration macro
146
147 if (TString(setup) != "") {
148 gROOT->LoadMacro(setup);
149 gInterpreter->ProcessLine("Config()");
150 if (!gMC) {
151 Fatal(
152 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
153 }
154 }
155
156// MT support available from root v 5.34/18
157#if ROOT_VERSION_CODE >= 336402
158 // Create Root manager
159 if (!gMC->IsMT()) {
160 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
161 // fRootManager->SetDebug(true);
162 }
163#else
164 // Create Root manager
165 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
166 // fRootManager->SetDebug(true);
167#endif
168
169 // Set data to MC
170 gMC->SetStack(fStack);
171 gMC->SetMagField(fMagField);
172
173 // Init MC
174 gMC->Init();
175 gMC->BuildPhysics();
176
178}
179
180//_____________________________________________________________________________
181void Ex02MCApplication::RunMC(Int_t nofEvents)
182{
183 /// Run MC.
184 /// \param nofEvents Number of events to be processed
185
186 gMC->ProcessRun(nofEvents);
187 FinishRun();
188}
189
190//_____________________________________________________________________________
192{
193 /// Finish MC run.
194
195 // cout << "Ex02MCApplication::FinishRun: " << endl;
196 if (fRootManager) {
197 // fRootManager->WriteAndClose();
198 fRootManager->WriteAll();
199 fRootManager->Close();
200 }
201}
202
203//_____________________________________________________________________________
208
209//_____________________________________________________________________________
211{
212 // cout << "Ex02MCApplication::InitForWorker " << this << endl;
213
214 // Create Root manager
215 Int_t threadRank = 1;
216 // The real thread rank will be set in MCRootManager
217 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite, threadRank);
218 // fRootManager->SetDebug(true);
219
220 // Set data to MC
221 gMC->SetStack(fStack);
222 gMC->SetMagField(fMagField);
223
225}
226
227//_____________________________________________________________________________
229{
230 // cout << "Ex02MCApplication::FinishWorkerRun: " << endl;
231 if (fRootManager) {
232 fRootManager->WriteAll();
233 fRootManager->Close();
234 }
235}
236
237//_____________________________________________________________________________
239{
240 /// Construct geometry using detector contruction class.
241 /// The detector contruction class is using TGeo functions or
242 /// TVirtualMC functions (if oldGeometry is selected)
243
244 // Cannot use Root geometry if not supported with
245 // selected MC
246 if (!fOldGeometry && !gMC->IsRootGeometrySupported()) {
247 cerr << "Selected MC does not support TGeo geometry" << endl;
248 cerr << "Exiting program" << endl;
249 exit(1);
250 }
251
252 if (!fOldGeometry) {
253 cout << "Geometry will be defined via TGeo" << endl;
254 fDetConstruction.ConstructMaterials();
255 fDetConstruction.ConstructGeometry();
256 }
257 else {
258 cout << "Geometry will be defined via VMC" << endl;
259 Ex02DetectorConstructionOld detConstructionOld;
260 detConstructionOld.ConstructMaterials();
261 detConstructionOld.ConstructGeometry();
262 }
263}
264
265//_____________________________________________________________________________
267{
268 /// Initialize geometry
269
270 fTrackerSD->Initialize();
271}
272
273//_____________________________________________________________________________
275{
276 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
277
278 // Track ID (filled by stack)
279 Int_t ntr;
280
281 // Option: to be tracked
282 Int_t toBeDone = 1;
283
284 // Particle type
285 // Int_t pdg = 0; // geantino
286 Int_t pdg = kProton;
287
288 // Polarization
289 Double_t polx = 0.;
290 Double_t poly = 0.;
291 Double_t polz = 0.;
292
293 // Position
294 Double_t vx = 0.;
295 Double_t vy = 0.;
296 Double_t vz = -0.5 * (fDetConstruction.GetWorldFullLength());
297 Double_t tof = 0.;
298
299 // Energy
300 Double_t kinEnergy = 3.0;
301 Double_t mass = 0.9382723;
302 Double_t e = mass + kinEnergy;
303
304 // Momentum
305 Double_t px, py, pz;
306 px = 0.;
307 py = 0.;
308 pz = sqrt(e * e - mass * mass);
309
310 // Add particle to stack
311 fStack->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
312 poly, polz, kPPrimary, ntr, 1., 0);
313}
314
315//_____________________________________________________________________________
317{
318 /// User actions at beginning of event.
319 /// Nothing to be done this example
320
321 fStack->SetObjectNumber();
322}
323
324//_____________________________________________________________________________
326{
327 /// User actions at beginning of a primary track.
328 /// Nothing to be done this example
329}
330
331//_____________________________________________________________________________
333{
334 /// User actions at beginning of each track.
335 /// Nothing to be done this example
336}
337
338//_____________________________________________________________________________
340{
341 /// User actions at each step
342
343 fTrackerSD->ProcessHits();
344}
345
346//_____________________________________________________________________________
348{
349 /// User actions at each step.
350 /// Nothing to be done this example
351}
352
353//_____________________________________________________________________________
355{
356 /// User actions after finishing of a primary track.
357 /// Nothing to be done this example
358}
359
360//_____________________________________________________________________________
362{
363 /// User actions after finishing of an event
364 /// Nothing to be done this example
365
366 // Geant4 own visualization is activated via G4 macro (g4config.in)
367
368 // TGeo visualization
369 if (gGeoManager && gGeoManager->GetListOfTracks() &&
370 gGeoManager->GetTrack(0) &&
371 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
372
373 gGeoManager->SetVisOption(0);
374 gGeoManager->SetTopVisible();
375 gGeoManager->DrawTracks("/*"); // this means all tracks
376 }
377
378 fRootManager->Fill();
379
380 fTrackerSD->EndOfEvent();
381
382 fStack->Print();
383 fStack->Reset();
384}
385
386//_____________________________________________________________________________
388{
389 /// Read \em i -th event and prints hits.
390 /// \param i The number of event to be read
391
392 if (!fRootManager) {
393 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kRead);
394 }
395
396 fTrackerSD->Register();
398 fRootManager->ReadEvent(i);
399
400 fStack->Print();
401 fTrackerSD->Print();
402}
Definition of the Ex02DetectorConstructionOld class.
Definition of the Ex02MCApplication class.
Definition of the Ex02MCStack class.
Definition of the Ex02MagField class.
The old detector construction (via VMC functions).
Implementation of the TVirtualMCApplication.
virtual void FinishPrimary()
virtual void GeneratePrimaries()
TVirtualMagField * fMagField
Magnetic field.
Bool_t fOldGeometry
Option for geometry definition.
virtual TVirtualMCApplication * CloneForWorker() const
void RunMC(Int_t nofEvents)
virtual void FinishRunOnWorker()
virtual void BeginPrimary()
virtual void InitOnWorker()
TMCRootManager * fRootManager
Root manager.
virtual void FinishEvent()
void InitMC(const char *setup)
virtual void InitGeometry()
Ex02MCStack * fStack
VMC stack.
virtual void ConstructGeometry()
Ex02DetectorConstruction fDetConstruction
Dector construction.
Ex02MCApplication(const char *name, const char *title)
Ex02TrackerSD * fTrackerSD
Tracker SD.
virtual void BeginEvent()
Implementation of the TVirtualMCStack interface.
Definition Ex02MCStack.h:33
Definition of a uniform magnetic field.
The tracker sensitive detector.