CMS 3D CMS Logo

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_physicsList.reset(nullptr);
79  m_world.reset(nullptr);
80  m_kernel = new G4MTRunManagerKernel();
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 
112  if (m_pUseMagneticField && "" != m_FieldFile)
113  {
114  const GlobalPoint g(0.,0.,0.);
115  sim::FieldBuilder fieldBuilder(pMF, m_pField);
116  CMSFieldManager* fieldManager = new CMSFieldManager();
117  G4TransportationManager * tM =
118  G4TransportationManager::GetTransportationManager();
119  tM->SetFieldManager(fieldManager);
120  fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
121  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
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 = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity",0),
156  m_p.getParameter<int>("SteppingVerbosity"));
157  m_kernel->SetVerboseLevel(verb);
158 
159  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue")*CLHEP::cm);
160  m_physicsList->SetCutsWithDefault();
161 
162  if(m_pPhysics.getParameter<bool>("CutsPerRegion")) {
163  m_prodCuts.reset(new DDG4ProductionCuts(map_, verb, m_pPhysics));
164  m_prodCuts->update();
165  }
166 
167  m_kernel->SetPhysics(phys);
168  m_kernel->InitializePhysics();
169  m_kernel->SetUpDecayChannels();
170 
171  // The following line was with the following comment in
172  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
173  // needed to initialize certain singletons during the master thread
174  // initialization in order to avoid races later...
175  //
176  //BERTINI, this is needed to create pseudo-particles, to be removed
177  G4CascadeInterface::Initialize();
178 
179  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
180  else {
182  "G4RunManagerKernel initialization failed!");
183  }
184 
185  if (m_StorePhysicsTables) {
186  std::ostringstream dir;
187  dir << m_PhysicsTablesDir << '\0';
188  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
189  if (!std::ifstream(dir.str().c_str(), std::ios::in))
190  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
191  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
192  }
193 
195 
196  if(0 < m_G4Commands.size()) {
197  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
198  for (unsigned it=0; it<m_G4Commands.size(); ++it) {
199  G4cout << " " << m_G4Commands[it] << G4endl;
200  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
201  }
202  }
203 
204  if(verb > 1) { m_physicsList->DumpCutValuesTable(); }
205 
206  // geometry dump
207  if("" != m_WriteFile) {
208  G4GDMLParser gdml;
209  gdml.SetRegionExport(true);
210  gdml.SetEnergyCutsExport(true);
211  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
212  }
213 
214  // G4Region dump
215  if("" != m_RegionFile) {
216  G4RegionReporter rrep;
218  }
219 
220  // Intersection check
222 
223  // If the Geant4 particle table is needed, decomment the lines below
224  //
225  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
226  //
227  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
228  m_currentRun = new G4Run();
230 }
231 
233  m_runInterface.reset(new SimRunInterface(this, true));
236 }
237 
239 {
242 }
243 
245 {
246  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
247  if(!m_runTerminated) { terminateRun(); }
248 }
249 
251  if(m_userRunAction) {
253  delete m_userRunAction;
254  m_userRunAction = nullptr;
255  }
256  if(m_kernel && !m_runTerminated) {
257  m_kernel->RunTermination();
258  }
259  m_runTerminated = true;
260 }
261 
262 void RunManagerMT::DumpMagneticField(const G4Field* field) const
263 {
264  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
265  if(fout.fail()){
266  edm::LogWarning("SimG4CoreApplication")
267  << " RunManager WARNING : "
268  << "error opening file <" << m_FieldFile << "> for magnetic field";
269  } else {
270  double rmax = 9000*mm;
271  double zmax = 16000*mm;
272 
273  double dr = 5*cm;
274  double dz = 20*cm;
275 
276  int nr = (int)(rmax/dr);
277  int nz = 2*(int)(zmax/dz);
278 
279  double r = 0.0;
280  double z0 = -zmax;
281  double z;
282 
283  double phi = 0.0;
284  double cosf = cos(phi);
285  double sinf = sin(phi);
286 
287  double point[4] = {0.0,0.0,0.0,0.0};
288  double bfield[3] = {0.0,0.0,0.0};
289 
290  fout << std::setprecision(6);
291  for(int i=0; i<=nr; ++i) {
292  z = z0;
293  for(int j=0; j<=nz; ++j) {
294  point[0] = r*cosf;
295  point[1] = r*sinf;
296  point[2] = z;
297  field->GetFieldValue(point, bfield);
298  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
299  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
300  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
301  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
302  << G4endl;
303  z += dz;
304  }
305  r += dr;
306  }
307 
308  fout.close();
309  }
310 }
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
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
def create(alignables, pedeDump, additionalData, outputFile, config)
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
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
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)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:25
HLT enums.
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
RunAction * m_userRunAction
Definition: RunManagerMT.h:109
def check(config)
Definition: trackerTree.py:14
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