test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RunManagerMT.cc
Go to the documentation of this file.
8 
11 
13 
19 
21 
28 
30 
32 
33 #include "HepPDT/ParticleDataTable.hh"
34 
35 #include "G4GeometryManager.hh"
36 #include "G4StateManager.hh"
37 #include "G4ApplicationState.hh"
38 #include "G4MTRunManagerKernel.hh"
39 #include "G4UImanager.hh"
40 
41 #include "G4EventManager.hh"
42 #include "G4Run.hh"
43 #include "G4Event.hh"
44 #include "G4TransportationManager.hh"
45 #include "G4ParticleTable.hh"
46 #include "G4Field.hh"
47 #include "G4FieldManager.hh"
48 #include "G4CascadeInterface.hh"
49 
50 #include "G4GDMLParser.hh"
51 #include "G4SystemOfUnits.hh"
52 
53 #include <iostream>
54 #include <sstream>
55 #include <fstream>
56 #include <memory>
57 
59 
61  m_managerInitialized(false),
62  m_runTerminated(false),
63  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
64  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
65  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
66  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
67  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
68  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
69  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
70  m_g4overlap(p.getParameter<edm::ParameterSet>("G4CheckOverlap")),
71  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
72  m_fieldBuilder(nullptr)
73 {
74  m_currentRun = nullptr;
75  G4RunManagerKernel *kernel = G4MTRunManagerKernel::GetRunManagerKernel();
76  if(!kernel) m_kernel = new G4MTRunManagerKernel();
77  else {
78  m_kernel = dynamic_cast<G4MTRunManagerKernel *>(kernel);
80  }
81 
82  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
83  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
84  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
85  m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions","");
86 }
87 
89 {
90  if(!m_runTerminated) { terminateRun(); }
91  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
92  G4GeometryManager::GetInstance()->OpenGeometry();
93 }
94 
95 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF,
96  const HepPDT::ParticleDataTable *fPDGTable)
97 {
98  if (m_managerInitialized) return;
99 
100  edm::LogInfo("SimG4CoreApplication")
101  << "RunManagerMT: start initialisation of geometry";
102 
103  // DDDWorld: get the DDCV from the ES and use it to build the World
105  m_world.reset(new DDDWorld(pDD, map_, m_catalog, false));
107 
108  // setup the magnetic field
109  edm::LogInfo("SimG4CoreApplication")
110  << "RunManagerMT: start initialisation of magnetic field";
111 
113  {
114  const GlobalPoint g(0.,0.,0.);
115 
118  G4TransportationManager * tM =
119  G4TransportationManager::GetTransportationManager();
120  m_fieldBuilder->build( tM->GetFieldManager(),
121  tM->GetPropagatorInField());
122  if("" != m_FieldFile) {
123  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
124  }
125  }
126 
127  // Create physics list
128  edm::LogInfo("SimG4CoreApplication")
129  << "RunManagerMT: create PhysicsList";
130 
131  std::unique_ptr<PhysicsListMakerBase>
132  physicsMaker(PhysicsListFactory::get()->create(
133  m_pPhysics.getParameter<std::string> ("type")));
134  if (physicsMaker.get()==0) {
135  throw SimG4Exception("Unable to find the Physics list requested");
136  }
137  m_physicsList =
138  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
139 
140  PhysicsList* phys = m_physicsList.get();
141  if (phys==0) {
142  throw SimG4Exception("Physics list construction failed!");
143  }
144 
145  // adding GFlash, Russian Roulette for eletrons and gamma,
146  // step limiters on top of any Physics Lists
147  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
148 
149  m_physicsList->ResetStoredInAscii();
151  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
152  }
153  edm::LogInfo("SimG4CoreApplication")
154  << "RunManagerMT: start initialisation of PhysicsList for master";
155 
156  m_kernel->SetPhysics(phys);
157  m_kernel->InitializePhysics();
158  m_kernel->SetUpDecayChannels();
159 
160  // The following line was with the following comment in
161  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
162  // needed to initialize certain singletons during the master thread
163  // initialization in order to avoid races later...
164  //
165  //BERTINI, this is needed to create pseudo-particles, to be removed
166  G4CascadeInterface::Initialize();
167 
168  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
169  else {
170  throw SimG4Exception("G4RunManagerKernel initialization failed!");
171  }
172 
173  if (m_StorePhysicsTables) {
174  std::ostringstream dir;
175  dir << m_PhysicsTablesDir << '\0';
176  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
177  if (!std::ifstream(dir.str().c_str(), std::ios::in))
178  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
179  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
180  }
181 
183 
184  if(0 < m_G4Commands.size()) {
185  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
186  for (unsigned it=0; it<m_G4Commands.size(); ++it) {
187  G4cout << " " << m_G4Commands[it] << G4endl;
188  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
189  }
190  }
191 
192  // geometry dump
193  if("" != m_WriteFile) {
194  G4GDMLParser gdml(new G4GDMLReadStructure(), new CMSGDMLWriteStructure());
195  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
196  }
197 
198  // G4Region dump
199  if("" != m_RegionFile) {
200  G4RegionReporter rrep;
202  }
203 
204  // Intersection check
206 
207  // If the Geant4 particle table is needed, decomment the lines below
208  //
209  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
210  //
211  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
212  m_currentRun = new G4Run();
214 }
215 
217  m_runInterface.reset(new SimRunInterface(this, true));
220 }
221 
223 {
226 }
227 
229 {
230  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
231  if(!m_runTerminated) { terminateRun(); }
232 }
233 
236  delete m_userRunAction;
237  m_userRunAction = 0;
238  if(m_kernel && !m_runTerminated) {
239  m_kernel->RunTermination();
240  m_runTerminated = true;
241  }
242 }
243 
244 void RunManagerMT::DumpMagneticField(const G4Field* field) const
245 {
246  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
247  if(fout.fail()){
248  edm::LogWarning("SimG4CoreApplication")
249  << " RunManager WARNING : "
250  << "error opening file <" << m_FieldFile << "> for magnetic field";
251  } else {
252  double rmax = 9000*mm;
253  double zmax = 16000*mm;
254 
255  double dr = 5*cm;
256  double dz = 20*cm;
257 
258  int nr = (int)(rmax/dr);
259  int nz = 2*(int)(zmax/dz);
260 
261  double r = 0.0;
262  double z0 = -zmax;
263  double z;
264 
265  double phi = 0.0;
266  double cosf = cos(phi);
267  double sinf = sin(phi);
268 
269  double point[4] = {0.0,0.0,0.0,0.0};
270  double bfield[3] = {0.0,0.0,0.0};
271 
272  fout << std::setprecision(6);
273  for(int i=0; i<=nr; ++i) {
274  z = z0;
275  for(int j=0; j<=nz; ++j) {
276  point[0] = r*cosf;
277  point[1] = r*sinf;
278  point[2] = z;
279  field->GetFieldValue(point, bfield);
280  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
281  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
282  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
283  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
284  << G4endl;
285  z += dz;
286  }
287  r += dr;
288  }
289 
290  fout.close();
291  }
292 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:122
bool m_managerInitialized
Definition: RunManagerMT.h:115
T getParameter(std::string const &) const
Map map_
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:112
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:134
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:128
HepPDT::ParticleDataTable ParticleDataTable
void initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
Definition: RunManagerMT.cc:95
void terminateRun()
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
assert(m_qm.get())
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:116
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:136
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:130
G4Run * m_currentRun
Definition: RunManagerMT.h:119
std::string m_RegionFile
Definition: RunManagerMT.h:141
#define nullptr
void initializeUserActions()
type of data representation of DDCompactView
Definition: DDCompactView.h:77
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
std::string m_WriteFile
Definition: RunManagerMT.h:140
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:60
void build(G4FieldManager *fM=nullptr, G4PropagatorInField *fP=nullptr, ChordFinderSetter *setter=nullptr)
Definition: FieldBuilder.cc:48
bool check(const std::string &)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
bool m_pUseMagneticField
Definition: RunManagerMT.h:117
void EndOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:39
EndOfRunSignal endOfRunSignal_
void DumpMagneticField(const G4Field *) const
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:59
tuple out
Definition: dbtoconf.py:99
bool m_StorePhysicsTables
Definition: RunManagerMT.h:123
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:114
std::string m_FieldFile
Definition: RunManagerMT.h:139
edm::ParameterSet m_pField
Definition: RunManagerMT.h:126
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:132
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:127
void BeginOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:21
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:124
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:137
void ReportRegions(const std::string &ss)
string cmd
self.logger.debug(&quot;Path is now `%s&#39;&quot; % \ path)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:120
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:129
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
RunAction * m_userRunAction
Definition: RunManagerMT.h:118
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:55
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
SimActivityRegistry m_registry
Definition: RunManagerMT.h:133