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 
25 
27 
29 
30 #include "HepPDT/defs.h"
31 #include "HepPDT/TableBuilder.hh"
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  if("" != m_FieldFile) { m_FieldFile += ".txt"; }
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  if("" != m_WriteFile) {
104  G4GDMLParser gdml;
105  gdml.Write(m_WriteFile, m_world->GetWorldVolume());
106  }
107 
108  // setup the magnetic field
110  {
111  const GlobalPoint g(0.,0.,0.);
112 
115  G4TransportationManager * tM =
116  G4TransportationManager::GetTransportationManager();
117  m_fieldBuilder->build( tM->GetFieldManager(),
118  tM->GetPropagatorInField());
119  if("" != m_FieldFile) {
120  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
121  }
122  }
123 
124  // Create physics list
125  std::unique_ptr<PhysicsListMakerBase>
126  physicsMaker(PhysicsListFactory::get()->create(
127  m_pPhysics.getParameter<std::string> ("type")));
128  if (physicsMaker.get()==0) {
129  throw SimG4Exception("Unable to find the Physics list requested");
130  }
131  m_physicsList =
132  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
133 
134  PhysicsList* phys = m_physicsList.get();
135  if (phys==0) {
136  throw SimG4Exception("Physics list construction failed!");
137  }
138 
139  // adding GFlash, Russian Roulette for eletrons and gamma,
140  // step limiters on top of any Physics Lists
141  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
142 
143  m_kernel->SetPhysics(phys);
144  m_kernel->InitializePhysics();
145  m_kernel->SetUpDecayChannels();
146  // The following line was with the following comment in
147  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
148  // needed to initialize certain singletons during the master thread
149  // initialization in order to avoid races later...
150  //
151  //BERTINI, this is needed to create pseudo-particles, to be removed
152  G4CascadeInterface::Initialize();
153  //
154 
155  m_physicsList->ResetStoredInAscii();
157  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
158  }
159 
160  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
161  else {
162  throw SimG4Exception("G4RunManagerKernel initialization failed!");
163  }
164 
166  {
167  std::ostringstream dir;
168  dir << m_PhysicsTablesDir << '\0';
169  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
170  if (!std::ifstream(dir.str().c_str(), std::ios::in))
171  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
172  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
173  }
174 
176 
177  for (unsigned it=0; it<m_G4Commands.size(); it++) {
178  edm::LogInfo("SimG4CoreApplication") << "RunManagerMT:: Requests UI: "
179  << m_G4Commands[it];
180  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
181  }
182 
183  // If the Geant4 particle table is needed, decomment the lines below
184  //
185  // G4cout << "Output of G4ParticleTable DumpTable:" << G4endl;
186  // G4ParticleTable::GetParticleTable()->DumpTable("ALL");
187  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
188  m_currentRun = new G4Run();
190 }
191 
193  m_runInterface.reset(new SimRunInterface(this, true));
194 
197 }
198 
200 {
203 }
204 
206 {
207  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
208  if(!m_runTerminated) { terminateRun(); }
209 }
210 
213  delete m_userRunAction;
214  m_userRunAction = 0;
215  // delete m_currentRun;
216  //m_currentRun = 0;
217  if(m_kernel && !m_runTerminated) {
218  m_kernel->RunTermination();
219  m_runTerminated = true;
220  }
221 }
222 
223 void RunManagerMT::DumpMagneticField(const G4Field* field) const
224 {
225  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
226  if(fout.fail()){
227  edm::LogWarning("SimG4CoreApplication")
228  << " RunManager WARNING : "
229  << "error opening file <" << m_FieldFile << "> for magnetic field";
230  } else {
231  double rmax = 9000*mm;
232  double zmax = 16000*mm;
233 
234  double dr = 5*cm;
235  double dz = 20*cm;
236 
237  int nr = (int)(rmax/dr);
238  int nz = 2*(int)(zmax/dz);
239 
240  double r = 0.0;
241  double z0 = -zmax;
242  double z;
243 
244  double phi = 0.0;
245  double cosf = cos(phi);
246  double sinf = sin(phi);
247 
248  double point[4] = {0.0,0.0,0.0,0.0};
249  double bfield[3] = {0.0,0.0,0.0};
250 
251  fout << std::setprecision(6);
252  for(int i=0; i<=nr; ++i) {
253  z = z0;
254  for(int j=0; j<=nz; ++j) {
255  point[0] = r*cosf;
256  point[1] = r*sinf;
257  point[2] = z;
258  field->GetFieldValue(point, bfield);
259  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
260  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
261  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
262  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
263  << G4endl;
264  z += dz;
265  }
266  r += dr;
267  }
268 
269  fout.close();
270  }
271 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:120
bool m_managerInitialized
Definition: RunManagerMT.h:113
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:110
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:131
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:126
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:114
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:133
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:127
G4Run * m_currentRun
Definition: RunManagerMT.h:117
#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
float float float z
std::string m_WriteFile
Definition: RunManagerMT.h:137
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
const bool m_pUseMagneticField
Definition: RunManagerMT.h:115
string cmd
Definition: asciidump.py:19
int j
Definition: DBlmapReader.cc:9
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:121
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:112
std::string m_FieldFile
Definition: RunManagerMT.h:136
edm::ParameterSet m_pField
Definition: RunManagerMT.h:124
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:129
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:125
void BeginOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:21
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:122
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:134
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:118
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
RunAction * m_userRunAction
Definition: RunManagerMT.h:116
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:130
Definition: DDAxes.h:10