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 
22 
24 
30 
32 
34 
35 #include "HepPDT/ParticleDataTable.hh"
36 
37 #include "G4GeometryManager.hh"
38 #include "G4StateManager.hh"
39 #include "G4ApplicationState.hh"
40 #include "G4MTRunManagerKernel.hh"
41 #include "G4UImanager.hh"
42 
43 #include "G4EventManager.hh"
44 #include "G4Run.hh"
45 #include "G4Event.hh"
46 #include "G4TransportationManager.hh"
47 #include "G4ParticleTable.hh"
48 #include "G4Field.hh"
49 #include "G4FieldManager.hh"
50 #include "G4CascadeInterface.hh"
51 
52 #include "G4GDMLParser.hh"
53 #include "G4SystemOfUnits.hh"
54 
55 #include <iostream>
56 #include <sstream>
57 #include <fstream>
58 #include <memory>
59 
61 
63  m_managerInitialized(false),
64  m_runTerminated(false),
65  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
66  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
67  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
68  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
69  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
70  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
71  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
72  m_g4overlap(p.getParameter<edm::ParameterSet>("G4CheckOverlap")),
73  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
74  m_p(p)
75 {
76  m_currentRun = nullptr;
77  m_UIsession.reset(new CustomUIsession());
78  m_kernel = new G4MTRunManagerKernel();
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  edm::LogInfo("SimG4CoreApplication")
99  << "RunManagerMT: start initialisation of geometry";
100 
101  // DDDWorld: get the DDCV from the ES and use it to build the World
103  m_world.reset(new DDDWorld(pDD, map_, m_catalog, false));
105 
106  // setup the magnetic field
107  edm::LogInfo("SimG4CoreApplication")
108  << "RunManagerMT: start initialisation of magnetic field";
109 
110  if (m_pUseMagneticField && "" != m_FieldFile)
111  {
112  const GlobalPoint g(0.,0.,0.);
113  sim::FieldBuilder fieldBuilder(pMF, m_pField);
114  CMSFieldManager* fieldManager = new CMSFieldManager();
115  G4TransportationManager * tM =
116  G4TransportationManager::GetTransportationManager();
117  tM->SetFieldManager(fieldManager);
118  fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
119  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
120  }
121 
122  // Create physics list
123  edm::LogInfo("SimG4CoreApplication")
124  << "RunManagerMT: create PhysicsList";
125 
126  std::unique_ptr<PhysicsListMakerBase>
127  physicsMaker(PhysicsListFactory::get()->create(
128  m_pPhysics.getParameter<std::string> ("type")));
129  if (physicsMaker.get()==nullptr) {
131  << "Unable to find the Physics list requested";
132  }
133  m_physicsList =
134  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
135 
136  PhysicsList* phys = m_physicsList.get();
137  if (phys==nullptr) {
139  "Physics list construction failed!");
140  }
141 
142  // adding GFlash, Russian Roulette for eletrons and gamma,
143  // step limiters on top of any Physics Lists
144  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
145 
146  m_physicsList->ResetStoredInAscii();
148  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
149  }
150  edm::LogInfo("SimG4CoreApplication")
151  << "RunManagerMT: start initialisation of PhysicsList for master";
152 
153  int verb = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity",0),
154  m_p.getParameter<int>("SteppingVerbosity"));
155  m_kernel->SetVerboseLevel(verb);
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:113
bool m_managerInitialized
Definition: RunManagerMT.h:106
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:125
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:102
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:127
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:119
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
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:26
bool m_runTerminated
Definition: RunManagerMT.h:107
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:121
std::unique_ptr< CustomUIsession > m_UIsession
Definition: RunManagerMT.h:104
G4Run * m_currentRun
Definition: RunManagerMT.h:110
std::string m_RegionFile
Definition: RunManagerMT.h:133
void initializeUserActions()
type of data representation of DDCompactView
Definition: DDCompactView.h:90
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:132
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:62
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
bool m_pUseMagneticField
Definition: RunManagerMT.h:108
edm::ParameterSet m_p
Definition: RunManagerMT.h:122
void EndOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:40
EndOfRunSignal endOfRunSignal_
void DumpMagneticField(const G4Field *) const
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:59
void build(CMSFieldManager *fM, G4PropagatorInField *fP)
Definition: FieldBuilder.cc:36
bool m_StorePhysicsTables
Definition: RunManagerMT.h:114
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:105
std::string m_FieldFile
Definition: RunManagerMT.h:131
edm::ParameterSet m_pField
Definition: RunManagerMT.h:117
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:124
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:118
void BeginOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:22
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:115
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:129
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:25
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:111
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:120
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
RunAction * m_userRunAction
Definition: RunManagerMT.h:109
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:126