Skip to content

Commit

Permalink
move all the G4 init to the beginRun and add a protection if Bfield o…
Browse files Browse the repository at this point in the history
…r geometry will change run by run
  • Loading branch information
fambrogl committed Apr 6, 2009
1 parent 53adf9e commit 7f54a8a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 80 deletions.
5 changes: 3 additions & 2 deletions SimG4Core/Application/interface/OscarProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Run.h"

#include "SimG4Core/Application/interface/RunManager.h"

Expand All @@ -24,8 +25,8 @@ class OscarProducer : public edm::EDProducer

explicit OscarProducer(edm::ParameterSet const & p);
virtual ~OscarProducer();
virtual void beginRun(const edm::EventSetup & c);
virtual void beginJob(const edm::EventSetup & c);
virtual void beginRun(edm::Run & r,const edm::EventSetup & c);
virtual void beginJob();
virtual void endJob();
virtual void produce(edm::Event & e, const edm::EventSetup & c);
protected:
Expand Down
17 changes: 13 additions & 4 deletions SimG4Core/Application/interface/RunManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include <memory>
#include "boost/shared_ptr.hpp"

#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
#include "Geometry/Records/interface/IdealGeometryRecord.h"

#include "FWCore/Framework/interface/ESWatcher.h"

namespace CLHEP {
class HepJamesRandom;
}
Expand Down Expand Up @@ -52,8 +57,7 @@ class RunManager
//static RunManager * instance();
//static RunManager * init(edm::ParameterSet const & p);
virtual ~RunManager();
void initG4Run(const edm::EventSetup & es);
void initG4Job(const edm::EventSetup & es);
void initG4(const edm::EventSetup & es);
void initializeUserActions();
void initializeRun();
void terminateRun();
Expand Down Expand Up @@ -87,11 +91,12 @@ class RunManager
std::auto_ptr<PhysicsList> m_physicsList;
PrimaryTransformer * m_primaryTransformer;
bool m_managerInitialized;
bool m_geometryInitialized;
bool m_physicsInitialized;
// bool m_geometryInitialized;
// bool m_physicsInitialized;
bool m_runInitialized;
bool m_runTerminated;
bool m_runAborted;
bool firstRun;
bool m_pUseMagneticField;
G4Run * m_currentRun;
G4Event * m_currentEvent;
Expand Down Expand Up @@ -128,6 +133,10 @@ class RunManager
std::auto_ptr<SimTrackManager> m_trackManager;
std::auto_ptr<sim::FieldBuilder> m_fieldBuilder;

edm::ESWatcher<IdealGeometryRecord> idealGeomRcdWatcher_;
edm::ESWatcher<IdealMagneticFieldRecord> idealMagRcdWatcher_;


};

#endif
8 changes: 3 additions & 5 deletions SimG4Core/Application/plugins/OscarProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,15 @@ OscarProducer::~OscarProducer()

}

void OscarProducer::beginRun(const edm::EventSetup & es)
void OscarProducer::beginRun(edm::Run & r, const edm::EventSetup & es)
{
m_runManager->initG4Run(es);
m_runManager->initG4(es);
}


void OscarProducer::beginJob(const edm::EventSetup & es)
void OscarProducer::beginJob()
{
StaticRandomEngineSetUnset random(m_engine);

m_runManager->initG4Job(es);
}

void OscarProducer::endJob() { }
Expand Down
155 changes: 86 additions & 69 deletions SimG4Core/Application/src/RunManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ RunManager::RunManager(edm::ParameterSet const & p)
: m_generator(0), m_nonBeam(p.getParameter<bool>("NonBeamEvent")),
m_primaryTransformer(0),
m_managerInitialized(false),
m_geometryInitialized(true), m_physicsInitialized(true),
//m_geometryInitialized(true), m_physicsInitialized(true),
m_runInitialized(false), m_runTerminated(false), m_runAborted(false),
firstRun(true),
m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
m_currentRun(0), m_currentEvent(0), m_simEvent(0),
m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
Expand Down Expand Up @@ -152,98 +153,114 @@ RunManager::~RunManager()
if (m_kernel!=0) delete m_kernel;
}

void RunManager::initG4Run(const edm::EventSetup & es)



void RunManager::initG4(const edm::EventSetup & es)
{

if(!firstRun){
if(idealGeomRcdWatcher_.check(es)){
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";
}
if(idealMagRcdWatcher_.check(es)){
throw cms::Exception("BadConfig")
<< "[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;

// DDDWorld: get the DDCV from the ES and use it to build the World
edm::ESHandle<DDCompactView> pDD;
es.get<IdealGeometryRecord>().get(pDD);

G4LogicalVolumeToDDLogicalPartMap map_;
SensitiveDetectorCatalog catalog_;
const DDDWorld * world = new DDDWorld(&(*pDD), map_, catalog_, m_check);
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.);

// m_fieldBuilder = std::auto_ptr<sim::FieldBuilder>(new sim::FieldBuilder(&(*pMF), map_, m_pField));
m_fieldBuilder = std::auto_ptr<sim::FieldBuilder>(new sim::FieldBuilder(&(*pMF), m_pField));
G4TransportationManager * tM = G4TransportationManager::GetTransportationManager();
m_fieldBuilder->build( tM->GetFieldManager(),tM->GetPropagatorInField() ) ;
// m_fieldBuilder->configure("MagneticFieldType",tM->GetFieldManager(),tM->GetPropagatorInField());
}

initializeRun();

}
void RunManager::initG4Job(const edm::EventSetup & es)
{
if (m_managerInitialized) return;
// we need the track manager now
m_trackManager = std::auto_ptr<SimTrackManager>(new SimTrackManager);

// DDDWorld: get the DDCV from the ES and use it to build the World
edm::ESHandle<DDCompactView> pDD;
es.get<IdealGeometryRecord>().get(pDD);

G4LogicalVolumeToDDLogicalPartMap map_;
SensitiveDetectorCatalog catalog_;
const DDDWorld * world = new DDDWorld(&(*pDD), map_, catalog_, m_check);
m_registry.dddWorldSignal_(world);

// we need the track manager now
m_trackManager = std::auto_ptr<SimTrackManager>(new SimTrackManager);

m_attach = new AttachSD;
{
std::pair< std::vector<SensitiveTkDetector*>,
std::vector<SensitiveCaloDetector*> > sensDets = m_attach->create(*world,(*pDD),catalog_,m_p,m_trackManager.get(),m_registry);
m_attach = new AttachSD;
{
std::pair< std::vector<SensitiveTkDetector*>,
std::vector<SensitiveCaloDetector*> > sensDets = m_attach->create(*world,(*pDD),catalog_,m_p,m_trackManager.get(),m_registry);

m_sensTkDets.swap(sensDets.first);
m_sensCaloDets.swap(sensDets.second);
}
m_sensTkDets.swap(sensDets.first);
m_sensCaloDets.swap(sensDets.second);
}


edm::LogInfo("SimG4CoreApplication") <<
" RunManager: Sensitive Detector building finished; found " << m_sensTkDets.size()
<< " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";
edm::LogInfo("SimG4CoreApplication") << " RunManager: Sensitive Detector building finished; found " << m_sensTkDets.size()
<< " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";


m_generator = new Generator(m_pGenerator);
// m_InTag = m_pGenerator.getParameter<edm::InputTag>("HepMCProductLabel") ;
m_InTag = m_pGenerator.getParameter<std::string>("HepMCProductLabel") ;
m_primaryTransformer = new PrimaryTransformer();
m_generator = new Generator(m_pGenerator);
// m_InTag = m_pGenerator.getParameter<edm::InputTag>("HepMCProductLabel") ;
m_InTag = m_pGenerator.getParameter<std::string>("HepMCProductLabel") ;
m_primaryTransformer = new PrimaryTransformer();

std::auto_ptr<PhysicsListMakerBase> physicsMaker(
PhysicsListFactory::get()->create
(m_pPhysics.getParameter<std::string> ("type")));
if (physicsMaker.get()==0) throw SimG4Exception("Unable to find the Physics list requested");
m_physicsList = physicsMaker->make(map_,m_pPhysics,m_registry);
if (m_physicsList.get()==0) throw SimG4Exception("Physics list construction failed!");
m_kernel->SetPhysics(m_physicsList.get());
m_kernel->InitializePhysics();

m_physicsList->ResetStoredInAscii();
std::string tableDir = m_PhysicsTablesDir;
if (m_RestorePhysicsTables) m_physicsList->SetPhysicsTableRetrieved(tableDir);
std::auto_ptr<PhysicsListMakerBase> physicsMaker(
PhysicsListFactory::get()->create
(m_pPhysics.getParameter<std::string> ("type")));
if (physicsMaker.get()==0) throw SimG4Exception("Unable to find the Physics list requested");
m_physicsList = physicsMaker->make(map_,m_pPhysics,m_registry);
if (m_physicsList.get()==0) throw SimG4Exception("Physics list construction failed!");
m_kernel->SetPhysics(m_physicsList.get());
m_kernel->InitializePhysics();

m_physicsList->ResetStoredInAscii();
std::string tableDir = m_PhysicsTablesDir;
if (m_RestorePhysicsTables) m_physicsList->SetPhysicsTableRetrieved(tableDir);

if (m_kernel->RunInitialization()) m_managerInitialized = true;
else throw SimG4Exception("G4RunManagerKernel initialization failed!");
if (m_StorePhysicsTables)
if (m_kernel->RunInitialization()) m_managerInitialized = true;
else throw SimG4Exception("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);

initializeUserActions();

for (unsigned it=0; it<m_G4Commands.size(); it++) {
edm::LogInfo("SimG4CoreApplication") << "RunManager:: Requests UI: "
<< m_G4Commands[it];
G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
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);

initializeUserActions();

for (unsigned it=0; it<m_G4Commands.size(); it++) {
edm::LogInfo("SimG4CoreApplication") << "RunManager:: Requests UI: "
<< m_G4Commands[it];
G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
}

initializeRun();
firstRun= false;

}

Expand Down

0 comments on commit 7f54a8a

Please sign in to comment.