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 
27 
29 
31 
32 #include "HepPDT/ParticleDataTable.hh"
33 
34 #include "G4GeometryManager.hh"
35 #include "G4StateManager.hh"
36 #include "G4ApplicationState.hh"
37 #include "G4MTRunManagerKernel.hh"
38 #include "G4UImanager.hh"
39 
40 #include "G4EventManager.hh"
41 #include "G4Run.hh"
42 #include "G4Event.hh"
43 #include "G4TransportationManager.hh"
44 #include "G4ParticleTable.hh"
45 #include "G4Field.hh"
46 #include "G4FieldManager.hh"
47 #include "G4CascadeInterface.hh"
48 
49 #include "G4GDMLParser.hh"
50 #include "G4SystemOfUnits.hh"
51 
52 #include <iostream>
53 #include <sstream>
54 #include <fstream>
55 #include <memory>
56 
58 
60  m_managerInitialized(false),
61  m_runTerminated(false),
62  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
63  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
64  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
65  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
66  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
67  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
68  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
69  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
70  m_fieldBuilder(nullptr)
71 {
72  m_currentRun = 0;
73  G4RunManagerKernel *kernel = G4MTRunManagerKernel::GetRunManagerKernel();
74  if(!kernel) m_kernel = new G4MTRunManagerKernel();
75  else {
76  m_kernel = dynamic_cast<G4MTRunManagerKernel *>(kernel);
78  }
79 
80  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
81  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
82  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
83  m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions","");
84 }
85 
87 {
88  if(!m_runTerminated) { terminateRun(); }
89  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
90  G4GeometryManager::GetInstance()->OpenGeometry();
91 }
92 
93 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF,
94  const HepPDT::ParticleDataTable *fPDGTable)
95 {
96  if (m_managerInitialized) return;
97 
98  // DDDWorld: get the DDCV from the ES and use it to build the World
100  m_world.reset(new DDDWorld(pDD, map_, m_catalog, m_check));
102 
103  // setup the magnetic field
105  {
106  const GlobalPoint g(0.,0.,0.);
107 
110  G4TransportationManager * tM =
111  G4TransportationManager::GetTransportationManager();
112  m_fieldBuilder->build( tM->GetFieldManager(),
113  tM->GetPropagatorInField());
114  if("" != m_FieldFile) {
115  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
116  }
117  }
118 
119  // Create physics list
120  std::unique_ptr<PhysicsListMakerBase>
121  physicsMaker(PhysicsListFactory::get()->create(
122  m_pPhysics.getParameter<std::string> ("type")));
123  if (physicsMaker.get()==0) {
124  throw SimG4Exception("Unable to find the Physics list requested");
125  }
126  m_physicsList =
127  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
128 
129  PhysicsList* phys = m_physicsList.get();
130  if (phys==0) {
131  throw SimG4Exception("Physics list construction failed!");
132  }
133 
134  // adding GFlash, Russian Roulette for eletrons and gamma,
135  // step limiters on top of any Physics Lists
136  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
137 
138  m_physicsList->ResetStoredInAscii();
140  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
141  }
142  edm::LogInfo("SimG4CoreApplication")
143  << "RunManagerMT: start initialisation of PhysicsList for master";
144 
145  m_kernel->SetPhysics(phys);
146  m_kernel->InitializePhysics();
147  m_kernel->SetUpDecayChannels();
148 
149  // The following line was with the following comment in
150  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
151  // needed to initialize certain singletons during the master thread
152  // initialization in order to avoid races later...
153  //
154  //BERTINI, this is needed to create pseudo-particles, to be removed
155  G4CascadeInterface::Initialize();
156 
157  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
158  else {
159  throw SimG4Exception("G4RunManagerKernel initialization failed!");
160  }
161 
163  {
164  std::ostringstream dir;
165  dir << m_PhysicsTablesDir << '\0';
166  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
167  if (!std::ifstream(dir.str().c_str(), std::ios::in))
168  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
169  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
170  }
171 
173 
174  if(0 < m_G4Commands.size()) {
175  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
176  for (unsigned it=0; it<m_G4Commands.size(); ++it) {
177  G4cout << " " << m_G4Commands[it] << G4endl;
178  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
179  }
180  }
181  if("" != m_WriteFile) {
182  G4GDMLParser gdml(new G4GDMLReadStructure(), new CMSGDMLWriteStructure());
183  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
184  }
185 
186  if("" != m_RegionFile) {
187  G4RegionReporter rrep;
189  }
190 
191  // If the Geant4 particle table is needed, decomment the lines below
192  //
193  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
194  //
195  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
196  m_currentRun = new G4Run();
198 }
199 
201  m_runInterface.reset(new SimRunInterface(this, true));
204 }
205 
207 {
210 }
211 
213 {
214  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
215  if(!m_runTerminated) { terminateRun(); }
216 }
217 
220  delete m_userRunAction;
221  m_userRunAction = 0;
222  // delete m_currentRun;
223  //m_currentRun = 0;
224  if(m_kernel && !m_runTerminated) {
225  m_kernel->RunTermination();
226  m_runTerminated = true;
227  }
228 }
229 
230 void RunManagerMT::DumpMagneticField(const G4Field* field) const
231 {
232  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
233  if(fout.fail()){
234  edm::LogWarning("SimG4CoreApplication")
235  << " RunManager WARNING : "
236  << "error opening file <" << m_FieldFile << "> for magnetic field";
237  } else {
238  double rmax = 9000*mm;
239  double zmax = 16000*mm;
240 
241  double dr = 5*cm;
242  double dz = 20*cm;
243 
244  int nr = (int)(rmax/dr);
245  int nz = 2*(int)(zmax/dz);
246 
247  double r = 0.0;
248  double z0 = -zmax;
249  double z;
250 
251  double phi = 0.0;
252  double cosf = cos(phi);
253  double sinf = sin(phi);
254 
255  double point[4] = {0.0,0.0,0.0,0.0};
256  double bfield[3] = {0.0,0.0,0.0};
257 
258  fout << std::setprecision(6);
259  for(int i=0; i<=nr; ++i) {
260  z = z0;
261  for(int j=0; j<=nz; ++j) {
262  point[0] = r*cosf;
263  point[1] = r*sinf;
264  point[2] = z;
265  field->GetFieldValue(point, bfield);
266  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
267  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
268  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
269  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
270  << G4endl;
271  z += dz;
272  }
273  r += dr;
274  }
275 
276  fout.close();
277  }
278 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:119
bool m_managerInitialized
Definition: RunManagerMT.h:112
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:109
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:130
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:125
HepPDT::ParticleDataTable ParticleDataTable
void initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
Definition: RunManagerMT.cc:93
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:113
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:132
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:126
G4Run * m_currentRun
Definition: RunManagerMT.h:116
std::string m_RegionFile
Definition: RunManagerMT.h:137
#define nullptr
void initializeUserActions()
type of data representation of DDCompactView
Definition: DDCompactView.h:76
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:136
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:59
void build(G4FieldManager *fM=nullptr, G4PropagatorInField *fP=nullptr, ChordFinderSetter *setter=nullptr)
Definition: FieldBuilder.cc:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
string cmd
Definition: asciidump.py:19
int j
Definition: DBlmapReader.cc:9
bool m_pUseMagneticField
Definition: RunManagerMT.h:114
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:120
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:111
std::string m_FieldFile
Definition: RunManagerMT.h:135
edm::ParameterSet m_pField
Definition: RunManagerMT.h:123
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:128
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:124
void BeginOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:21
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:121
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:133
void ReportRegions(const std::string &ss)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:117
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
RunAction * m_userRunAction
Definition: RunManagerMT.h:115
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:129