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.
9 
13 
15 
21 
23 
29 
31 
33 
34 #include "HepPDT/ParticleDataTable.hh"
35 
36 #include "G4GeometryManager.hh"
37 #include "G4StateManager.hh"
38 #include "G4ApplicationState.hh"
39 #include "G4MTRunManagerKernel.hh"
40 #include "G4UImanager.hh"
41 
42 #include "G4EventManager.hh"
43 #include "G4Run.hh"
44 #include "G4Event.hh"
45 #include "G4TransportationManager.hh"
46 #include "G4ParticleTable.hh"
47 #include "G4Field.hh"
48 #include "G4FieldManager.hh"
49 #include "G4CascadeInterface.hh"
50 
51 #include "G4GDMLParser.hh"
52 #include "G4SystemOfUnits.hh"
53 
54 #include <iostream>
55 #include <sstream>
56 #include <fstream>
57 #include <memory>
58 
60 
62  m_managerInitialized(false),
63  m_runTerminated(false),
64  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
65  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
66  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
67  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
68  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
69  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
70  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
71  m_g4overlap(p.getParameter<edm::ParameterSet>("G4CheckOverlap")),
72  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
73  m_p(p),m_fieldBuilder(nullptr)
74 {
75  m_currentRun = nullptr;
76  m_UIsession.reset(new CustomUIsession());
77  m_kernel = new G4MTRunManagerKernel();
78 
79  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
80  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
81  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
82  m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions","");
83 }
84 
86 {
87  if(!m_runTerminated) { terminateRun(); }
88  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
89  G4GeometryManager::GetInstance()->OpenGeometry();
90 }
91 
92 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF,
93  const HepPDT::ParticleDataTable *fPDGTable)
94 {
95  if (m_managerInitialized) return;
96 
97  edm::LogInfo("SimG4CoreApplication")
98  << "RunManagerMT: start initialisation of geometry";
99 
100  // DDDWorld: get the DDCV from the ES and use it to build the World
102  m_world.reset(new DDDWorld(pDD, map_, m_catalog, false));
104 
105  // setup the magnetic field
106  edm::LogInfo("SimG4CoreApplication")
107  << "RunManagerMT: start initialisation of magnetic field";
108 
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  edm::LogInfo("SimG4CoreApplication")
126  << "RunManagerMT: create PhysicsList";
127 
128  std::unique_ptr<PhysicsListMakerBase>
129  physicsMaker(PhysicsListFactory::get()->create(
130  m_pPhysics.getParameter<std::string> ("type")));
131  if (physicsMaker.get()==nullptr) {
133  << "Unable to find the Physics list requested";
134  }
135  m_physicsList =
136  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
137 
138  PhysicsList* phys = m_physicsList.get();
139  if (phys==nullptr) {
141  "Physics list construction failed!");
142  }
143 
144  // adding GFlash, Russian Roulette for eletrons and gamma,
145  // step limiters on top of any Physics Lists
146  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
147 
148  m_physicsList->ResetStoredInAscii();
150  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
151  }
152  edm::LogInfo("SimG4CoreApplication")
153  << "RunManagerMT: start initialisation of PhysicsList for master";
154 
155  int verb = m_pPhysics.getUntrackedParameter<int>("Verbosity",0);
156 
157  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue")*CLHEP::cm);
158  m_physicsList->SetCutsWithDefault();
159  m_prodCuts.reset(new DDG4ProductionCuts(map_, verb, m_pPhysics));
160  m_prodCuts->update();
161 
162  m_kernel->SetPhysics(phys);
163  m_kernel->InitializePhysics();
164  m_kernel->SetUpDecayChannels();
165 
166  // The following line was with the following comment in
167  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
168  // needed to initialize certain singletons during the master thread
169  // initialization in order to avoid races later...
170  //
171  //BERTINI, this is needed to create pseudo-particles, to be removed
172  G4CascadeInterface::Initialize();
173 
174  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
175  else {
177  "G4RunManagerKernel initialization failed!");
178  }
179 
180  if (m_StorePhysicsTables) {
181  std::ostringstream dir;
182  dir << m_PhysicsTablesDir << '\0';
183  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
184  if (!std::ifstream(dir.str().c_str(), std::ios::in))
185  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
186  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
187  }
188 
190 
191  if(0 < m_G4Commands.size()) {
192  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
193  for (unsigned it=0; it<m_G4Commands.size(); ++it) {
194  G4cout << " " << m_G4Commands[it] << G4endl;
195  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
196  }
197  }
198 
199  if(verb > 1) { m_physicsList->DumpCutValuesTable(); }
200 
201  // geometry dump
202  if("" != m_WriteFile) {
203  G4GDMLParser gdml(new G4GDMLReadStructure(), new CMSGDMLWriteStructure());
204  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
205  }
206 
207  // G4Region dump
208  if("" != m_RegionFile) {
209  G4RegionReporter rrep;
211  }
212 
213  // Intersection check
215 
216  // If the Geant4 particle table is needed, decomment the lines below
217  //
218  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
219  //
220  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
221  m_currentRun = new G4Run();
223 }
224 
226  m_runInterface.reset(new SimRunInterface(this, true));
229 }
230 
232 {
235 }
236 
238 {
239  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
240  if(!m_runTerminated) { terminateRun(); }
241 }
242 
244  if(m_userRunAction) {
246  delete m_userRunAction;
247  m_userRunAction = nullptr;
248  }
249  if(m_kernel && !m_runTerminated) {
250  m_kernel->RunTermination();
251  }
252  m_runTerminated = true;
253 }
254 
255 void RunManagerMT::DumpMagneticField(const G4Field* field) const
256 {
257  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
258  if(fout.fail()){
259  edm::LogWarning("SimG4CoreApplication")
260  << " RunManager WARNING : "
261  << "error opening file <" << m_FieldFile << "> for magnetic field";
262  } else {
263  double rmax = 9000*mm;
264  double zmax = 16000*mm;
265 
266  double dr = 5*cm;
267  double dz = 20*cm;
268 
269  int nr = (int)(rmax/dr);
270  int nz = 2*(int)(zmax/dz);
271 
272  double r = 0.0;
273  double z0 = -zmax;
274  double z;
275 
276  double phi = 0.0;
277  double cosf = cos(phi);
278  double sinf = sin(phi);
279 
280  double point[4] = {0.0,0.0,0.0,0.0};
281  double bfield[3] = {0.0,0.0,0.0};
282 
283  fout << std::setprecision(6);
284  for(int i=0; i<=nr; ++i) {
285  z = z0;
286  for(int j=0; j<=nz; ++j) {
287  point[0] = r*cosf;
288  point[1] = r*sinf;
289  point[2] = z;
290  field->GetFieldValue(point, bfield);
291  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
292  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
293  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
294  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
295  << G4endl;
296  z += dz;
297  }
298  r += dr;
299  }
300 
301  fout.close();
302  }
303 }
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
std::unique_ptr< DDG4ProductionCuts > m_prodCuts
Definition: RunManagerMT.h:134
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:111
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:136
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:92
void terminateRun()
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:116
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:138
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:130
std::unique_ptr< CustomUIsession > m_UIsession
Definition: RunManagerMT.h:113
G4Run * m_currentRun
Definition: RunManagerMT.h:119
std::string m_RegionFile
Definition: RunManagerMT.h:143
#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:142
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:61
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
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
bool m_StorePhysicsTables
Definition: RunManagerMT.h:123
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:114
std::string m_FieldFile
Definition: RunManagerMT.h:141
edm::ParameterSet m_pField
Definition: RunManagerMT.h:126
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:133
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:139
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
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:135