VMC Examples Version 6.6
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),
43 fDetConstruction(),
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),
71 fDetConstruction(origin.fDetConstruction),
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),
98 fDetConstruction(),
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 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
216 // fRootManager->SetDebug(true);
217
218 // Set data to MC
219 gMC->SetStack(fStack);
220 gMC->SetMagField(fMagField);
221
223}
224
225//_____________________________________________________________________________
227{
228 // cout << "Ex02MCApplication::FinishWorkerRun: " << endl;
229 if (fRootManager) {
230 fRootManager->WriteAll();
231 fRootManager->Close();
232 }
233}
234
235//_____________________________________________________________________________
237{
238 /// Construct geometry using detector contruction class.
239 /// The detector contruction class is using TGeo functions or
240 /// TVirtualMC functions (if oldGeometry is selected)
241
242 // Cannot use Root geometry if not supported with
243 // selected MC
244 if (!fOldGeometry && !gMC->IsRootGeometrySupported()) {
245 cerr << "Selected MC does not support TGeo geometry" << endl;
246 cerr << "Exiting program" << endl;
247 exit(1);
248 }
249
250 if (!fOldGeometry) {
251 cout << "Geometry will be defined via TGeo" << endl;
254 }
255 else {
256 cout << "Geometry will be defined via VMC" << endl;
257 Ex02DetectorConstructionOld detConstructionOld;
258 detConstructionOld.ConstructMaterials();
259 detConstructionOld.ConstructGeometry();
260 }
261}
262
263//_____________________________________________________________________________
265{
266 /// Initialize geometry
267
269}
270
271//_____________________________________________________________________________
273{
274 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
275
276 // Track ID (filled by stack)
277 Int_t ntr;
278
279 // Option: to be tracked
280 Int_t toBeDone = 1;
281
282 // Particle type
283 // Int_t pdg = 0; // geantino
284 Int_t pdg = kProton;
285
286 // Polarization
287 Double_t polx = 0.;
288 Double_t poly = 0.;
289 Double_t polz = 0.;
290
291 // Position
292 Double_t vx = 0.;
293 Double_t vy = 0.;
294 Double_t vz = -0.5 * (fDetConstruction.GetWorldFullLength());
295 Double_t tof = 0.;
296
297 // Energy
298 Double_t kinEnergy = 3.0;
299 Double_t mass = 0.9382723;
300 Double_t e = mass + kinEnergy;
301
302 // Momentum
303 Double_t px, py, pz;
304 px = 0.;
305 py = 0.;
306 pz = sqrt(e * e - mass * mass);
307
308 // Add particle to stack
309 fStack->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
310 poly, polz, kPPrimary, ntr, 1., 0);
311}
312
313//_____________________________________________________________________________
315{
316 /// User actions at beginning of event.
317 /// Nothing to be done this example
318
320}
321
322//_____________________________________________________________________________
324{
325 /// User actions at beginning of a primary track.
326 /// Nothing to be done this example
327}
328
329//_____________________________________________________________________________
331{
332 /// User actions at beginning of each track.
333 /// Nothing to be done this example
334}
335
336//_____________________________________________________________________________
338{
339 /// User actions at each step
340
342}
343
344//_____________________________________________________________________________
346{
347 /// User actions at each step.
348 /// Nothing to be done this example
349}
350
351//_____________________________________________________________________________
353{
354 /// User actions after finishing of a primary track.
355 /// Nothing to be done this example
356}
357
358//_____________________________________________________________________________
360{
361 /// User actions after finishing of an event
362 /// Nothing to be done this example
363
364 // Geant4 own visualization is activated via G4 macro (g4config.in)
365
366 // TGeo visualization
367 if (gGeoManager && gGeoManager->GetListOfTracks() &&
368 gGeoManager->GetTrack(0) &&
369 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
370
371 gGeoManager->SetVisOption(0);
372 gGeoManager->SetTopVisible();
373 gGeoManager->DrawTracks("/*"); // this means all tracks
374 }
375
376 fRootManager->Fill();
377
379
380 fStack->Print();
381 fStack->Reset();
382}
383
384//_____________________________________________________________________________
386{
387 /// Read \em i -th event and prints hits.
388 /// \param i The number of event to be read
389
390 if (!fRootManager) {
391 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kRead);
392 }
393
396 fRootManager->ReadEvent(i);
397
398 fStack->Print();
399 fTrackerSD->Print();
400}
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.
Ex02TrackerSD * fTrackerSD
Tracker SD.
virtual void BeginEvent()
Implementation of the TVirtualMCStack interface.
Definition Ex02MCStack.h:33
virtual void Print(Option_t *option="") const
virtual void PushTrack(Int_t toBeDone, Int_t parent, Int_t pdg, Double_t px, Double_t py, Double_t pz, Double_t e, Double_t vx, Double_t vy, Double_t vz, Double_t tof, Double_t polx, Double_t poly, Double_t polz, TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
void SetObjectNumber()
Definition of a uniform magnetic field.
The tracker sensitive detector.
virtual void Print(const Option_t *option=0) const