VMC Examples Version 6.8
Loading...
Searching...
No Matches
MCApplication.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2007 - 2016 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 ExGarfield/src/MCApplication.cxx
11/// \brief Implementation of the ExGarfield::MCApplication class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14///
15/// \date 28/10/2015
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "MCApplication.h"
19#include "Ex03MCStack.h"
20#include "Hit.h"
21#include "PrimaryGenerator.h"
22
23#include <TMCRootManager.h>
24
25#include <Riostream.h>
26#include <TGeoManager.h>
27#include <TInterpreter.h>
28#include <TPDGCode.h>
29#include <TParticle.h>
30#include <TROOT.h>
31#include <TRandom.h>
32#include <TVector3.h>
33#include <TVirtualGeoTrack.h>
34#include <TVirtualMC.h>
35
36using namespace std;
37
38/// \cond CLASSIMP
40 /// \endcond
41
42 namespace VMC
43{
44 namespace ExGarfield
45 {
46
47 //_____________________________________________________________________________
48 MCApplication::MCApplication(const char* name, const char* title)
49 : TVirtualMCApplication(name, title),
50 fRootManager(0),
51 fEventNo(0),
52 fVerbose(0),
53 fStack(0),
57 fIsMaster(kTRUE)
58 {
59 /// Standard constructor
60 /// \param name The MC application name
61 /// \param title The MC application description
62
63 // Create a user stack
64 fStack = new Ex03MCStack(1000);
65
66 // Create detector construction
68
69 // Create a calorimeter SD
70 fSensitiveDetector = new SensitiveDetector("Calorimeter");
71
72 // Create a primary generator
74 }
75
76 //_____________________________________________________________________________
78 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
79 fRootManager(0),
80 fEventNo(0),
81 fVerbose(origin.fVerbose),
82 fStack(0),
86 fIsMaster(kFALSE)
87 {
88 /// Copy constructor for cloning application on workers (in multithreading
89 /// mode) \param origin The source MC application
90
91 // Create new user stack
92 fStack = new Ex03MCStack(1000);
93
94 // Create a calorimeter SD
96
97 // Create a primary generator
100 }
101
102 //_____________________________________________________________________________
105 fRootManager(0),
106 fEventNo(0),
107 fStack(0),
111 fIsMaster(kTRUE)
112 {
113 /// Default constructor
114 }
115
116 //_____________________________________________________________________________
118 {
119 /// Destructor
120
121 // cout << "MCApplication::~MCApplication " << this << endl;
122
123 delete fRootManager;
124 delete fStack;
125 if (fIsMaster) delete fDetConstruction;
126 delete fSensitiveDetector;
127 delete fPrimaryGenerator;
128 delete gMC;
129
130 // cout << "Done MCApplication::~MCApplication " << this << endl;
131 }
132
133 //
134 // private methods
135 //
136
137 //_____________________________________________________________________________
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 fVerbose.InitMC();
160
161 if (TString(setup) != "") {
162 gROOT->LoadMacro(setup);
163 gInterpreter->ProcessLine("Config()");
164 if (!gMC) {
165 Fatal(
166 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
167 }
168 }
169
170// MT support available from root v 5.34/18
171#if ROOT_VERSION_CODE >= 336402
172 // Create Root manager
173 if (!gMC->IsMT()) {
174 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
175 // fRootManager->SetDebug(true);
176 }
177#else
178 // Create Root manager
179 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
180 // fRootManager->SetDebug(true);
181#endif
182
183 gMC->SetStack(fStack);
184 gMC->Init();
185 gMC->BuildPhysics();
186
188 }
189
190 //_____________________________________________________________________________
191 void MCApplication::RunMC(Int_t nofEvents)
192 {
193 /// Run MC.
194 /// \param nofEvents Number of events to be processed
195
196 fVerbose.RunMC(nofEvents);
197
198 gMC->ProcessRun(nofEvents);
199 FinishRun();
200 }
201
202 //_____________________________________________________________________________
204 {
205 /// Finish MC run.
206
207 fVerbose.FinishRun();
208 // cout << "MCApplication::FinishRun: " << endl;
209 if (fRootManager) {
210 fRootManager->WriteAll();
211 fRootManager->Close();
212 }
213 }
214
215 //_____________________________________________________________________________
217 {
218 return new MCApplication(*this);
219 }
220
221 //_____________________________________________________________________________
223 {
224 // cout << "MCApplication::InitForWorker " << this << endl;
225
226 // Create Root manager
227 Int_t threadRank = 1;
228 // The real thread rank will be set in MCRootManager
229 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite, threadRank);
230 // fRootManager->SetDebug(true);
231
232 // Set data to MC
233 gMC->SetStack(fStack);
234
236 }
237
238 //_____________________________________________________________________________
240 {
241 // cout << "MCApplication::FinishWorkerRun: " << endl;
242 if (fRootManager) {
243 fRootManager->WriteAll();
244 fRootManager->Close();
245 }
246 }
247
248 //_____________________________________________________________________________
250 {
251 /// Read \em i -th event and prints hits.
252 /// \param i The number of event to be read
253
254 if ( ! fRootManager ) {
255 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kRead);
256 }
257
258 fSensitiveDetector->Register();
260 fRootManager->ReadEvent(i);
261 }
262
263 //_____________________________________________________________________________
265 {
266 /// Construct geometry using detector contruction class.
267 /// The detector contruction class is using TGeo functions or
268 /// TVirtualMC functions (if oldGeometry is selected)
269
270 fVerbose.ConstructGeometry();
271
272 fDetConstruction->Construct();
273 }
274
275 //_____________________________________________________________________________
277 {
278 /// Initialize geometry
279
280 fVerbose.InitGeometry();
281
282 fSensitiveDetector->Initialize();
283 }
284
285 //_____________________________________________________________________________
287 {
288 /// Fill the user stack (derived from TVirtualMCStack) with primary
289 /// particles.
290
291 fVerbose.GeneratePrimaries();
292
293 TVector3 origin;
294 fPrimaryGenerator->GeneratePrimaries(origin);
295 }
296
297 //_____________________________________________________________________________
299 {
300 /// User actions at beginning of event
301
302 fVerbose.BeginEvent();
303
304 // Clear TGeo tracks (if filled)
305 if (TString(gMC->GetName()) == "TGeant3TGeo" &&
306 gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
307 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
308
309 gGeoManager->ClearTracks();
310 // if (gPad) gPad->Clear();
311 }
312
313 fEventNo++;
314 cout << " Start generating event Nr " << fEventNo << endl;
315 }
316
317 //_____________________________________________________________________________
319 {
320 /// User actions at beginning of a primary track.
321 /// If test for user defined decay is activated,
322 /// the primary track ID is printed on the screen.
323
324 fVerbose.BeginPrimary();
325 }
326
327 //_____________________________________________________________________________
329 {
330 /// User actions at beginning of each track
331 /// If test for user defined decay is activated,
332 /// the decay products of the primary track (K0Short)
333 /// are printed on the screen.
334
335 fVerbose.PreTrack();
336 }
337
338 //_____________________________________________________________________________
340 {
341 /// User actions at each step
342
343 // Work around for Fluka VMC, which does not call
344 // MCApplication::PreTrack()
345 //
346 // cout << "MCApplication::Stepping" << this << endl;
347 static Int_t trackId = 0;
348 if (TString(gMC->GetName()) == "TFluka" &&
349 gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
350 fVerbose.PreTrack();
351 trackId = gMC->GetStack()->GetCurrentTrackNumber();
352 }
353
354 fVerbose.Stepping();
355
356 fSensitiveDetector->ProcessHits();
357 }
358
359 //_____________________________________________________________________________
361 {
362 /// User actions after finishing of each track
363
364 fVerbose.PostTrack();
365 }
366
367 //_____________________________________________________________________________
369 {
370 /// User actions after finishing of a primary track
371
372 fVerbose.FinishPrimary();
373 }
374
375 //_____________________________________________________________________________
377 {
378 /// User actions after finishing of an event
379
380 fVerbose.FinishEvent();
381
382 // update hit info from Garfield
383 fSensitiveDetector->UpdateFromGarfield();
384 fRootManager->Fill();
385
386 // reset data
387 fSensitiveDetector->EndOfEvent();
388 fStack->Reset();
389 }
390
391 } // namespace ExGarfield
392}
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
The detector construction (via TGeo ).
Implementation of the TVirtualMCApplication.
Int_t fEventNo
Event counter.
Bool_t fIsMaster
If is on master thread.
void InitMC(const char *setup)
MCApplication(const char *name, const char *title)
virtual TVirtualMCApplication * CloneForWorker() const
DetectorConstruction * fDetConstruction
Dector construction.
TMCRootManager * fRootManager
Root manager.
SensitiveDetector * fSensitiveDetector
Calorimeter SD.
TMCVerbose fVerbose
VMC verbose helper.
Ex03MCStack * fStack
VMC stack.
PrimaryGenerator * fPrimaryGenerator
Primary generator.
The calorimeter sensitive detector.