/
RunManager.cc
626 lines (524 loc) · 22.9 KB
/
RunManager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include "SimG4Core/Application/interface/RunManager.h"
#include "SimG4Core/Application/interface/PrimaryTransformer.h"
#include "SimG4Core/Application/interface/SimRunInterface.h"
#include "SimG4Core/Application/interface/RunAction.h"
#include "SimG4Core/Application/interface/EventAction.h"
#include "SimG4Core/Application/interface/StackingAction.h"
#include "SimG4Core/Application/interface/TrackingAction.h"
#include "SimG4Core/Application/interface/SteppingAction.h"
#include "SimG4Core/Application/interface/ParametrisedEMPhysics.h"
#include "SimG4Core/Application/interface/CMSGDMLWriteStructure.h"
#include "SimG4Core/Application/interface/ExceptionHandler.h"
#include "SimG4Core/Geometry/interface/DDDWorld.h"
#include "SimG4Core/Geometry/interface/SensitiveDetectorCatalog.h"
#include "SimG4Core/SensitiveDetector/interface/AttachSD.h"
#include "SimG4Core/Generators/interface/Generator.h"
#include "SimG4Core/Physics/interface/PhysicsListFactory.h"
#include "SimG4Core/PhysicsLists/interface/CMSMonopolePhysics.h"
#include "SimG4Core/CustomPhysics/interface/CMSExoticaPhysics.h"
#include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
#include "SimG4Core/MagneticField/interface/FieldBuilder.h"
#include "SimG4Core/MagneticField/interface/Field.h"
#include "SimG4Core/MagneticField/interface/CMSFieldManager.h"
#include "MagneticField/Engine/interface/MagneticField.h"
#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
#include "SimG4Core/Notification/interface/G4SimEvent.h"
#include "SimG4Core/Notification/interface/SimTrackManager.h"
#include "SimG4Core/Notification/interface/BeginOfJob.h"
#include "SimG4Core/Notification/interface/CurrentG4Track.h"
#include "SimG4Core/Notification/interface/SimG4Exception.h"
#include "SimG4Core/Notification/interface/CMSSteppingVerbose.h"
#include "SimG4Core/Geometry/interface/CustomUIsession.h"
#include "SimG4Core/Geometry/interface/CMSG4CheckOverlap.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESTransientHandle.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "Geometry/Records/interface/IdealGeometryRecord.h"
#include "DetectorDescription/Core/interface/DDCompactView.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/Forward/interface/LHCTransportLinkContainer.h"
#include "HepPDT/ParticleDataTable.hh"
#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h"
#include "G4GeometryManager.hh"
#include "G4StateManager.hh"
#include "G4ApplicationState.hh"
#include "G4RunManagerKernel.hh"
#include "G4UImanager.hh"
#include "G4EventManager.hh"
#include "G4Run.hh"
#include "G4Event.hh"
#include "G4TransportationManager.hh"
#include "G4ParticleTable.hh"
#include "G4Field.hh"
#include "G4FieldManager.hh"
#include "G4LogicalVolume.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4PhysicalVolumeStore.hh"
#include "G4Region.hh"
#include "G4RegionStore.hh"
#include "G4GDMLParser.hh"
#include "G4SystemOfUnits.hh"
#include <iostream>
#include <sstream>
#include <fstream>
#include <memory>
#include "FWCore/MessageLogger/interface/MessageLogger.h"
static void createWatchers(const edm::ParameterSet& iP,
SimActivityRegistry& iReg,
std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
std::vector<std::shared_ptr<SimProducer> >& oProds) {
using namespace std;
using namespace edm;
vector<ParameterSet> watchers = iP.getParameter<vector<ParameterSet> >("Watchers");
for (vector<ParameterSet>::iterator itWatcher = watchers.begin(); itWatcher != watchers.end(); ++itWatcher) {
std::shared_ptr<SimWatcherMakerBase> maker(
SimWatcherFactory::get()->create(itWatcher->getParameter<std::string>("type")));
if (maker.get() == nullptr) {
throw edm::Exception(edm::errors::Configuration) << "Unable to find the requested Watcher";
}
std::shared_ptr<SimWatcher> watcherTemp;
std::shared_ptr<SimProducer> producerTemp;
maker->make(*itWatcher, iReg, watcherTemp, producerTemp);
oWatchers.push_back(watcherTemp);
if (producerTemp) {
oProds.push_back(producerTemp);
}
}
}
RunManager::RunManager(edm::ParameterSet const& p, edm::ConsumesCollector&& iC)
: m_generator(new Generator(p.getParameter<edm::ParameterSet>("Generator"))),
m_HepMC(iC.consumes<edm::HepMCProduct>(
p.getParameter<edm::ParameterSet>("Generator").getParameter<edm::InputTag>("HepMCProductLabel"))),
m_LHCtr(iC.consumes<edm::LHCTransportLinkContainer>(p.getParameter<edm::InputTag>("theLHCTlinkTag"))),
m_nonBeam(p.getParameter<bool>("NonBeamEvent")),
m_primaryTransformer(nullptr),
m_managerInitialized(false),
m_runInitialized(false),
m_runTerminated(false),
m_runAborted(false),
firstRun(true),
m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
m_currentRun(nullptr),
m_currentEvent(nullptr),
m_simEvent(nullptr),
m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
m_EvtMgrVerbosity(p.getUntrackedParameter<int>("G4EventManagerVerbosity", 0)),
m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
m_pGenerator(p.getParameter<edm::ParameterSet>("Generator")),
m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
m_pEventAction(p.getParameter<edm::ParameterSet>("EventAction")),
m_pStackingAction(p.getParameter<edm::ParameterSet>("StackingAction")),
m_pTrackingAction(p.getParameter<edm::ParameterSet>("TrackingAction")),
m_pSteppingAction(p.getParameter<edm::ParameterSet>("SteppingAction")),
m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
m_p(p) {
m_UIsession.reset(new CustomUIsession());
m_kernel = new G4RunManagerKernel();
G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler());
m_physicsList.reset(nullptr);
m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML", "");
m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField", "");
m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions", "");
m_userRunAction = nullptr;
m_runInterface = nullptr;
//Look for an outside SimActivityRegistry
// this is used by the visualization code
edm::Service<SimActivityRegistry> otherRegistry;
if (otherRegistry) {
m_registry.connect(*otherRegistry);
}
m_sVerbose.reset(nullptr);
std::vector<edm::ParameterSet> watchers = p.getParameter<std::vector<edm::ParameterSet> >("Watchers");
m_hasWatchers = (watchers.empty()) ? false : true;
if (m_hasWatchers) {
createWatchers(m_p, m_registry, m_watchers, m_producers);
}
}
RunManager::~RunManager() {
if (!m_runTerminated) {
terminateRun();
}
G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
G4GeometryManager::GetInstance()->OpenGeometry();
// if (m_kernel!=0) delete m_kernel;
delete m_runInterface;
delete m_generator;
}
void RunManager::initG4(const edm::EventSetup& es) {
bool geomChanged = idealGeomRcdWatcher_.check(es);
if (geomChanged && (!firstRun)) {
throw cms::Exception("BadConfig") << "[SimG4Core RunManager]\n"
<< "The Geometry configuration is changed during the job execution\n"
<< "this is not allowed, the geometry must stay unchanged\n";
}
bool geoFromDD4hep = m_p.getParameter<bool>("g4GeometryDD4hepSource");
bool cuts = m_pPhysics.getParameter<bool>("CutsPerRegion");
bool protonCut = m_pPhysics.getParameter<bool>("CutsOnProton");
int verb = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity", 0),
m_p.getUntrackedParameter<int>("SteppingVerbosity", 0));
edm::LogVerbatim("SimG4CoreApplication")
<< "RunManager: start initialising of geometry DD4Hep: " << geoFromDD4hep << "\n"
<< " cutsPerRegion: " << cuts << " cutForProton: " << protonCut << "\n"
<< " G4 verbosity: " << verb;
if (m_pUseMagneticField) {
bool magChanged = idealMagRcdWatcher_.check(es);
if (magChanged && (!firstRun)) {
throw edm::Exception(edm::errors::Configuration)
<< "[SimG4Core RunManager]\n"
<< "The MagneticField configuration is changed during the job execution\n"
<< "this is not allowed, the MagneticField must stay unchanged\n";
}
}
if (m_managerInitialized)
return;
// initialise geometry
const DDCompactView* pDD = nullptr;
const cms::DDCompactView* pDD4hep = nullptr;
if (geoFromDD4hep) {
edm::ESTransientHandle<cms::DDCompactView> ph;
es.get<IdealGeometryRecord>().get(ph);
pDD4hep = ph.product();
} else {
edm::ESTransientHandle<DDCompactView> ph;
es.get<IdealGeometryRecord>().get(ph);
pDD = ph.product();
}
SensitiveDetectorCatalog catalog;
const DDDWorld* world = new DDDWorld(pDD, pDD4hep, catalog, verb, cuts, protonCut);
G4VPhysicalVolume* pworld = world->GetWorldVolume();
const G4RegionStore* regStore = G4RegionStore::GetInstance();
const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
unsigned int numPV = pvs->size();
unsigned int numLV = lvs->size();
unsigned int nn = regStore->size();
edm::LogVerbatim("SimG4CoreApplication")
<< "###RunManager: " << numPV << " PhysVolumes; " << numLV << " LogVolumes; " << nn << " Regions.";
m_kernel->DefineWorldVolume(pworld, true);
m_registry.dddWorldSignal_(world);
if (m_pUseMagneticField) {
// setup the magnetic field
edm::ESHandle<MagneticField> pMF;
es.get<IdealMagneticFieldRecord>().get(pMF);
const GlobalPoint g(0., 0., 0.);
sim::FieldBuilder fieldBuilder(pMF.product(), m_pField);
CMSFieldManager* fieldManager = new CMSFieldManager();
G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
tM->SetFieldManager(fieldManager);
fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
if (!m_FieldFile.empty()) {
DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
}
}
// we need the track manager now
m_trackManager = std::unique_ptr<SimTrackManager>(new SimTrackManager);
// attach sensitive detector
AttachSD attach;
auto sensDets = attach.create(es, catalog, m_p, m_trackManager.get(), m_registry);
m_sensTkDets.swap(sensDets.first);
m_sensCaloDets.swap(sensDets.second);
edm::LogVerbatim("SimG4CoreApplication")
<< " RunManager: Sensitive Detector "
<< "building finished; found " << m_sensTkDets.size() << " Tk type Producers, and " << m_sensCaloDets.size()
<< " Calo type producers ";
edm::ESHandle<HepPDT::ParticleDataTable> fTable;
es.get<PDTRecord>().get(fTable);
const HepPDT::ParticleDataTable* fPDGTable = &(*fTable);
m_primaryTransformer = new PrimaryTransformer();
std::unique_ptr<PhysicsListMakerBase> physicsMaker(
PhysicsListFactory::get()->create(m_pPhysics.getParameter<std::string>("type")));
if (physicsMaker.get() == nullptr) {
throw edm::Exception(edm::errors::Configuration) << "Unable to find the Physics list requested";
}
m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
PhysicsList* phys = m_physicsList.get();
if (phys == nullptr) {
throw edm::Exception(edm::errors::Configuration) << "Physics list construction failed!";
}
// exotic particle physics
double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0.);
if (monopoleMass > 0.0) {
phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
}
bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
if (exotica) {
CMSExoticaPhysics exo(phys, m_pPhysics);
}
// adding GFlash, Russian Roulette for eletrons and gamma,
// step limiters on top of any Physics Lists
phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
std::string tableDir = m_PhysicsTablesDir;
if (m_RestorePhysicsTables) {
m_physicsList->SetPhysicsTableRetrieved(tableDir);
}
edm::LogInfo("SimG4CoreApplication") << "RunManager: start initialisation of PhysicsList";
m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
m_physicsList->SetCutsWithDefault();
m_kernel->SetPhysics(phys);
m_kernel->InitializePhysics();
if (m_kernel->RunInitialization()) {
m_managerInitialized = true;
} else {
throw edm::Exception(edm::errors::LogicError) << "G4RunManagerKernel initialization failed!";
}
if (m_StorePhysicsTables) {
std::ostringstream dir;
dir << tableDir << '\0';
std::string cmd = std::string("/control/shell mkdir -p ") + tableDir;
if (!std::ifstream(dir.str().c_str(), std::ios::in))
G4UImanager::GetUIpointer()->ApplyCommand(cmd);
m_physicsList->StorePhysicsTable(tableDir);
}
//tell all interesting parties that we are beginning the job
BeginOfJob aBeginOfJob(&es);
m_registry.beginOfJobSignal_(&aBeginOfJob);
G4int sv = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
G4double elim = m_p.getUntrackedParameter<double>("StepVerboseThreshold", 0.1) * CLHEP::GeV;
std::vector<int> ve = m_p.getUntrackedParameter<std::vector<int> >("VerboseEvents");
std::vector<int> vn = m_p.getUntrackedParameter<std::vector<int> >("VertexNumber");
std::vector<int> vt = m_p.getUntrackedParameter<std::vector<int> >("VerboseTracks");
if (sv > 0) {
m_sVerbose.reset(new CMSSteppingVerbose(sv, elim, ve, vn, vt));
}
initializeUserActions();
if (!m_G4Commands.empty()) {
G4cout << "RunManager: Requested UI commands: " << G4endl;
for (unsigned it = 0; it < m_G4Commands.size(); ++it) {
G4cout << " " << m_G4Commands[it] << G4endl;
G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
}
}
G4StateManager::GetStateManager()->SetNewState(G4State_Init);
if (!m_WriteFile.empty()) {
G4GDMLParser gdml;
gdml.SetRegionExport(true);
gdml.SetEnergyCutsExport(true);
gdml.Write(m_WriteFile, pworld, true);
}
// G4Region dump file name
auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
// Geometry checks
if (m_check || !regionFile.empty()) {
CMSG4CheckOverlap check(m_g4overlap, regionFile, m_UIsession.get(), pworld);
}
// If the Geant4 particle table is needed, decomment the lines below
//
// G4cout << "Output of G4ParticleTable DumpTable:" << G4endl;
// G4ParticleTable::GetParticleTable()->DumpTable("ALL");
initializeRun();
firstRun = false;
}
void RunManager::stopG4() {
G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
if (!m_runTerminated) {
terminateRun();
}
}
void RunManager::produce(edm::Event& inpevt, const edm::EventSetup&) {
m_currentEvent = generateEvent(inpevt);
m_simEvent = new G4SimEvent;
m_simEvent->hepEvent(m_generator->genEvent());
m_simEvent->weight(m_generator->eventWeight());
if (m_generator->genVertex() != nullptr) {
m_simEvent->collisionPoint(math::XYZTLorentzVectorD(m_generator->genVertex()->x() / centimeter,
m_generator->genVertex()->y() / centimeter,
m_generator->genVertex()->z() / centimeter,
m_generator->genVertex()->t() / second));
}
if (m_currentEvent->GetNumberOfPrimaryVertex() == 0) {
std::stringstream ss;
ss << " RunManager::produce(): event " << inpevt.id().event() << " with no G4PrimaryVertices\n";
throw SimG4Exception(ss.str());
abortRun(false);
} else {
edm::LogInfo("SimG4CoreApplication") << "RunManager::produce: start Event " << inpevt.id().event() << " of weight "
<< m_simEvent->weight() << " with " << m_simEvent->nTracks() << " tracks and "
<< m_simEvent->nVertices() << " vertices, generated by "
<< m_simEvent->nGenParts() << " particles ";
m_kernel->GetEventManager()->ProcessOneEvent(m_currentEvent);
edm::LogInfo("SimG4CoreApplication") << " RunManager::produce: ended Event " << inpevt.id().event();
}
}
G4Event* RunManager::generateEvent(edm::Event& inpevt) {
if (m_currentEvent != nullptr) {
delete m_currentEvent;
}
m_currentEvent = nullptr;
if (m_simEvent != nullptr) {
delete m_simEvent;
}
m_simEvent = nullptr;
// 64 bits event ID in CMSSW converted into Geant4 event ID
G4int evtid = (G4int)inpevt.id().event();
G4Event* evt = new G4Event(evtid);
edm::Handle<edm::HepMCProduct> HepMCEvt;
inpevt.getByToken(m_HepMC, HepMCEvt);
m_generator->setGenEvent(HepMCEvt->GetEvent());
// required to reset the GenParticle Id for particles transported
// along the beam pipe
// to their original value for SimTrack creation
resetGenParticleId(inpevt);
if (!m_nonBeam) {
m_generator->HepMC2G4(HepMCEvt->GetEvent(), evt);
} else {
m_generator->nonBeamEvent2G4(HepMCEvt->GetEvent(), evt);
}
return evt;
}
void RunManager::abortEvent() {
if (m_runTerminated) {
return;
}
G4Track* t = m_kernel->GetEventManager()->GetTrackingManager()->GetTrack();
t->SetTrackStatus(fStopAndKill);
// CMS-specific act
//
TrackingAction* uta = (TrackingAction*)m_kernel->GetEventManager()->GetUserTrackingAction();
uta->PostUserTrackingAction(t);
m_currentEvent->SetEventAborted();
m_kernel->GetEventManager()->GetStackManager()->clear();
m_kernel->GetEventManager()->GetTrackingManager()->EventAborted();
G4StateManager* stateManager = G4StateManager::GetStateManager();
stateManager->SetNewState(G4State_GeomClosed);
}
void RunManager::initializeUserActions() {
m_runInterface = new SimRunInterface(this, false);
m_userRunAction = new RunAction(m_pRunAction, m_runInterface, true);
Connect(m_userRunAction);
G4EventManager* eventManager = m_kernel->GetEventManager();
eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
if (m_generator != nullptr) {
EventAction* userEventAction =
new EventAction(m_pEventAction, m_runInterface, m_trackManager.get(), m_sVerbose.get());
Connect(userEventAction);
eventManager->SetUserAction(userEventAction);
TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
Connect(userTrackingAction);
eventManager->SetUserAction(userTrackingAction);
SteppingAction* userSteppingAction =
new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
Connect(userSteppingAction);
eventManager->SetUserAction(userSteppingAction);
eventManager->SetUserAction(new StackingAction(userTrackingAction, m_pStackingAction, m_sVerbose.get()));
} else {
edm::LogWarning("SimG4CoreApplication") << " RunManager: WARNING : "
<< "No generator; initialized "
<< "only RunAction!";
}
}
void RunManager::initializeRun() {
m_runInitialized = false;
if (m_currentRun == nullptr) {
m_currentRun = new G4Run();
}
G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
if (m_userRunAction != nullptr) {
m_userRunAction->BeginOfRunAction(m_currentRun);
}
m_runAborted = false;
m_runInitialized = true;
}
void RunManager::terminateRun() {
if (m_runTerminated) {
return;
}
if (m_userRunAction != nullptr) {
m_userRunAction->EndOfRunAction(m_currentRun);
delete m_userRunAction;
m_userRunAction = nullptr;
}
delete m_currentEvent;
m_currentEvent = nullptr;
delete m_simEvent;
m_simEvent = nullptr;
if (m_kernel != nullptr) {
m_kernel->RunTermination();
}
m_runInitialized = false;
m_runTerminated = true;
}
void RunManager::abortRun(bool softAbort) {
if (m_runAborted) {
return;
}
if (!softAbort) {
abortEvent();
}
if (m_currentRun != nullptr) {
delete m_currentRun;
m_currentRun = nullptr;
}
terminateRun();
m_runAborted = true;
}
void RunManager::resetGenParticleId(edm::Event& inpevt) {
edm::Handle<edm::LHCTransportLinkContainer> theLHCTlink;
inpevt.getByToken(m_LHCtr, theLHCTlink);
if (theLHCTlink.isValid()) {
m_trackManager->setLHCTransportLink(theLHCTlink.product());
}
}
SimTrackManager* RunManager::GetSimTrackManager() { return m_trackManager.get(); }
void RunManager::Connect(RunAction* runAction) {
runAction->m_beginOfRunSignal.connect(m_registry.beginOfRunSignal_);
runAction->m_endOfRunSignal.connect(m_registry.endOfRunSignal_);
}
void RunManager::Connect(EventAction* eventAction) {
eventAction->m_beginOfEventSignal.connect(m_registry.beginOfEventSignal_);
eventAction->m_endOfEventSignal.connect(m_registry.endOfEventSignal_);
}
void RunManager::Connect(TrackingAction* trackingAction) {
trackingAction->m_beginOfTrackSignal.connect(m_registry.beginOfTrackSignal_);
trackingAction->m_endOfTrackSignal.connect(m_registry.endOfTrackSignal_);
}
void RunManager::Connect(SteppingAction* steppingAction) {
steppingAction->m_g4StepSignal.connect(m_registry.g4StepSignal_);
}
void RunManager::DumpMagneticField(const G4Field* field) const {
std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
if (fout.fail()) {
edm::LogWarning("SimG4CoreApplication") << " RunManager WARNING : "
<< "error opening file <" << m_FieldFile << "> for magnetic field";
} else {
double rmax = 9000 * mm;
double zmax = 16000 * mm;
double dr = 5 * cm;
double dz = 20 * cm;
int nr = (int)(rmax / dr);
int nz = 2 * (int)(zmax / dz);
double r = 0.0;
double z0 = -zmax;
double z;
double phi = 0.0;
double cosf = cos(phi);
double sinf = sin(phi);
double point[4] = {0.0, 0.0, 0.0, 0.0};
double bfield[3] = {0.0, 0.0, 0.0};
fout << std::setprecision(6);
for (int i = 0; i <= nr; ++i) {
z = z0;
for (int j = 0; j <= nz; ++j) {
point[0] = r * cosf;
point[1] = r * sinf;
point[2] = z;
field->GetFieldValue(point, bfield);
fout << "R(mm)= " << r / mm << " phi(deg)= " << phi / degree << " Z(mm)= " << z / mm
<< " Bz(tesla)= " << bfield[2] / tesla << " Br(tesla)= " << (bfield[0] * cosf + bfield[1] * sinf) / tesla
<< " Bphi(tesla)= " << (bfield[0] * sinf - bfield[1] * cosf) / tesla << G4endl;
z += dz;
}
r += dr;
}
fout.close();
}
}