Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TMCManager.h
Go to the documentation of this file.
1 // @(#)root/vmc:$Id$
2 // Authors: Benedikt Volkel 07/03/2019
3 
4 /*************************************************************************
5  * Copyright (C) 2019, Rene Brun and Fons Rademakers. *
6  * Copyright (C) 2019, ALICE Experiment at CERN. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOT_TMCManager
14 #define ROOT_TMCManager
15 //
16 // Class TMCManager
17 // ---------------------------
18 // manager class for handling multiple TVirtualMC engines.
19 //
20 
21 #include <functional>
22 #include <memory>
23 
24 #include "TMCtls.h"
26 #include "TMCParticleStatus.h"
27 #include "TGeoManager.h"
28 #include "TVirtualMC.h"
29 
30 class TVirtualMC;
31 class TVirtualMCApplication;
32 class TParticle;
33 class TVirtualMCStack;
34 class TMCManagerStack;
35 
36 class TMCManager {
37 
38  friend class TVirtualMCApplication;
39 
40 public:
41  /// Default constructor
42  TMCManager();
43 
44  /// Destructor
45  virtual ~TMCManager();
46 
47  /// Static access method
48  static TMCManager *Instance();
49 
50  //
51  // Methods to manage multiple engines
52  //
53 
54  /// A TVirtualMC will register itself via this method during construction
55  /// if a TMCManager was instanciated before.
56  /// The TMCManager will assign an ID to the engines.
57  void Register(TVirtualMC *engine);
58 
59  /// The user application will register itself via this method when the
60  /// manager was requested.
61  void Register(TVirtualMCApplication *application);
62 
63  /// Return the number of registered engines.
64  Int_t NEngines() const;
65 
66  /// Get registered engine pointers
67  void GetEngines(std::vector<TVirtualMC *> &engines) const;
68 
69  /// Get an engine pointer by ID
70  TVirtualMC *GetEngine(Int_t id) const;
71 
72  /// Get engine ID by its name
73  Int_t GetEngineId(const char *name) const;
74 
75  /// Get the current engine pointer
76  TVirtualMC *GetCurrentEngine() const;
77 
78  /// Connect a pointer which is updated whenever the engine is changed
79  void ConnectEnginePointer(TVirtualMC **mc);
80 
81  /// Connect a pointer which is updated whenever the engine is changed
82  void ConnectEnginePointer(TVirtualMC *&mc);
83 
84  //
85  // Stack related methods
86  //
87 
88  /// Set user stack
89  void SetUserStack(TVirtualMCStack *stack);
90 
91  /// User interface to forward particle to specifiic engine.
92  /// It is assumed that the TParticle is owned by the user. It will not be
93  /// modified by the TMCManager.
94  void ForwardTrack(Int_t toBeDone, Int_t trackId, Int_t parentId, TParticle *particle, Int_t engineId);
95 
96  /// User interface to forward particle to specifiic engine.
97  /// It is assumed that the TParticle is owned by the user. It will not be
98  /// modified by the TMCManager.
99  /// Assume current engine Id
100  void ForwardTrack(Int_t toBeDone, Int_t trackId, Int_t parentId, TParticle *particle);
101 
102  /// Transfer track from current engine to engine with engineTargetId
103  void TransferTrack(Int_t engineTargetId);
104 
105  /// Transfer track from current engine to target engine mc
106  void TransferTrack(TVirtualMC *mc);
107 
108  /// Try to restore geometry for a given track
109  Bool_t RestoreGeometryState(Int_t trackId, Bool_t checkTrackIdRange = kTRUE);
110 
111  /// Try to restore geometry for the track currently set
112  Bool_t RestoreGeometryState();
113 
114  //
115  // Steering and control
116  //
117 
118  /// Apply something to all engines
119  template <typename F>
120  void Apply(F engineLambda)
121  {
122  for (auto &mc : fEngines) {
123  // We never know whether static method TVirtualMC::GetMC() is used in any way so update before calling the
124  // lambda.
125  UpdateEnginePointers(mc);
126  engineLambda(mc);
127  }
128  }
129 
130  /// Initialize engines
131  void Init();
132  /// Further specific initialization
133  template <typename F>
134  void Init(F initFunction)
135  {
136  if (fIsInitializedUser) {
137  return;
138  }
139  Init();
140  for (auto &mc : fEngines) {
141  // Set to current engine and call user init procedure
142  UpdateEnginePointers(mc);
143  initFunction(mc);
144  }
145  fIsInitializedUser = kTRUE;
146  }
147 
148  /// Run the event loop
149  void Run(Int_t nEvents);
150 
151 private:
152  /// Do necessary steps before an event is triggered
153  void PrepareNewEvent();
154  /// Find the next engine
155  Bool_t GetNextEngine();
156  /// Update all engine pointers connected to the TMCManager
157  void UpdateEnginePointers(TVirtualMC *mc);
158  /// Terminate a run in all engines
159  void TerminateRun();
160 
161 private:
162  // static data members
163 #if !defined(__CINT__)
164  static TMCThreadLocal TMCManager *fgInstance; ///< Singleton instance
165 #else
166  static TMCManager *fgInstance; ///< Singleton instance
167 #endif
168 
169  /// Pointer to user application
170  TVirtualMCApplication *fApplication;
171  /// Pointer to current engine
172  TVirtualMC *fCurrentEngine;
173  /// Collecting pointers to all instanciated TVirtualMCs
174  std::vector<TVirtualMC *> fEngines;
175  /// Stacks connected to engines
176  std::vector<std::unique_ptr<TMCManagerStack>> fStacks;
177  /// All tracks (persistent)
178  std::vector<TParticle *> fParticles;
179  /// All particles' status (persistent)
180  std::vector<std::unique_ptr<TMCParticleStatus>> fParticlesStatus;
181  /// Total number of primaries ever pushed
182  Int_t fTotalNPrimaries;
183  /// Total number of tracks ever pushed
184  Int_t fTotalNTracks;
185  /// Connected engine pointers which will be updated everytime the current
186  /// engine changes
187  std::vector<TVirtualMC **> fConnectedEnginePointers;
188  /// Pointer to user stack
189  TVirtualMCStack *fUserStack;
190  /// Pointer to cache with geometry states
191  TGeoMCBranchArrayContainer fBranchArrayContainer;
192  /// Flag if engines are initilaized
193  Bool_t fIsInitialized;
194  /// Flag if specific initialization for engines was done
195  Bool_t fIsInitializedUser;
196 
197  ClassDef(TMCManager, 0)
198 };
199 
200 #endif