CMS 3D CMS Logo

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