CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/SimG4Core/GeometryProducer/src/GeometryProducer.cc

Go to the documentation of this file.
00001 #include "FWCore/PluginManager/interface/PluginManager.h"
00002 
00003 #include "SimG4Core/GeometryProducer/interface/GeometryProducer.h"
00004 
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/Framework/interface/ESTransientHandle.h"
00007 #include "FWCore/ServiceRegistry/interface/Service.h"
00008 
00009 #include "SimG4Core/Watcher/interface/SimProducer.h"
00010 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
00011 #include "SimG4Core/Geometry/interface/DDDWorld.h"
00012 #include "SimG4Core/Geometry/interface/G4LogicalVolumeToDDLogicalPartMap.h"
00013 #include "SimG4Core/Geometry/interface/SensitiveDetectorCatalog.h"
00014 #include "SimG4Core/MagneticField/interface/FieldBuilder.h"
00015 #include "SimG4Core/MagneticField/interface/Field.h"
00016 #include "SimG4Core/Notification/interface/SimG4Exception.h"
00017 #include "SimG4Core/Application/interface/SimTrackManager.h"
00018 
00019 #include "MagneticField/Engine/interface/MagneticField.h"
00020 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00021 
00022 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00023 
00024 #include "DetectorDescription/Core/interface/DDCompactView.h"
00025 
00026 #include "G4RunManagerKernel.hh"
00027 #include "G4TransportationManager.hh"
00028 
00029 #include <iostream>
00030 
00031 static
00032 void createWatchers(const edm::ParameterSet& iP, SimActivityRegistry& iReg,
00033                     std::vector<boost::shared_ptr<SimWatcher> >& oWatchers,
00034                     std::vector<boost::shared_ptr<SimProducer> >& oProds)
00035 {
00036     using namespace std;
00037     using namespace edm;
00038     vector<ParameterSet> watchers;
00039     try { watchers = iP.getParameter<vector<ParameterSet> >("Watchers"); } 
00040     catch(edm::Exception) {}
00041   
00042     for(vector<ParameterSet>::iterator itWatcher = watchers.begin();
00043         itWatcher != watchers.end(); ++itWatcher) 
00044     {
00045         std::auto_ptr<SimWatcherMakerBase> 
00046             maker(SimWatcherFactory::get()->create(itWatcher->getParameter<std::string> ("type")));
00047         if(maker.get()==0) { throw SimG4Exception("Unable to find the requested Watcher"); }
00048     
00049         boost::shared_ptr<SimWatcher> watcherTemp;
00050         boost::shared_ptr<SimProducer> producerTemp;
00051         maker->make(*itWatcher,iReg,watcherTemp,producerTemp);
00052         oWatchers.push_back(watcherTemp);
00053         if(producerTemp) oProds.push_back(producerTemp);
00054   }
00055 }
00056 
00057 GeometryProducer::GeometryProducer(edm::ParameterSet const & p) :
00058     m_kernel(0), 
00059     m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
00060     m_pField(p.getParameter<edm::ParameterSet>("MagneticField")), 
00061     m_pUseSensitiveDetectors(p.getParameter<bool>("UseSensitiveDetectors")),
00062     m_attach(0), m_p(p)
00063 {
00064     //Look for an outside SimActivityRegistry
00065     //this is used by the visualization code
00066     edm::Service<SimActivityRegistry> otherRegistry;
00067     if (otherRegistry) m_registry.connect(*otherRegistry);
00068     createWatchers(m_p, m_registry, m_watchers, m_producers);
00069     produces<int>();
00070 }
00071 
00072 GeometryProducer::~GeometryProducer() 
00073 { 
00074     if (m_attach!=0) delete m_attach;
00075     if (m_kernel!=0) delete m_kernel; 
00076 }
00077 
00078 void GeometryProducer::beginJob(){
00079 }
00080  
00081 void GeometryProducer::endJob()
00082 { std::cout << " GeometryProducer terminating " << std::endl; }
00083  
00084 void GeometryProducer::produce(edm::Event & e, const edm::EventSetup & es)
00085 {
00086     m_kernel = G4RunManagerKernel::GetRunManagerKernel();
00087     if (m_kernel==0) m_kernel = new G4RunManagerKernel();
00088     std::cout << " GeometryProducer initializing " << std::endl;
00089     // DDDWorld: get the DDCV from the ES and use it to build the World
00090     edm::ESTransientHandle<DDCompactView> pDD;
00091     es.get<IdealGeometryRecord>().get(pDD);
00092    
00093     G4LogicalVolumeToDDLogicalPartMap map_;
00094     SensitiveDetectorCatalog catalog_;
00095     const DDDWorld * world = new DDDWorld(&(*pDD), map_, catalog_, false);
00096     m_registry.dddWorldSignal_(world);
00097 
00098     if (m_pUseMagneticField)
00099     {
00100         // setup the magnetic field
00101         edm::ESHandle<MagneticField> pMF;
00102         es.get<IdealMagneticFieldRecord>().get(pMF);
00103         const GlobalPoint g(0.,0.,0.);
00104         std::cout << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g) << std::endl;
00105 
00106         m_fieldBuilder = std::auto_ptr<sim::FieldBuilder>
00107           (new sim::FieldBuilder(&(*pMF), m_pField));
00108         // G4TransportationManager * tM = G4TransportationManager::GetTransportationManager();
00109         // m_fieldBuilder->configure("MagneticFieldType",tM->GetFieldManager(),tM->GetPropagatorInField());
00110     }
00111 
00112     if (m_pUseSensitiveDetectors)
00113     {
00114         std::cout << " instantiating sensitive detectors " << std::endl;
00115         // instantiate and attach the sensitive detectors
00116         m_trackManager = std::auto_ptr<SimTrackManager>(new SimTrackManager);
00117         if (m_attach==0) m_attach = new AttachSD;
00118         {
00119             std::pair< std::vector<SensitiveTkDetector*>,
00120                 std::vector<SensitiveCaloDetector*> > 
00121               sensDets = m_attach->create(*world,(*pDD),catalog_,m_p,m_trackManager.get(),m_registry);
00122       
00123             m_sensTkDets.swap(sensDets.first);
00124             m_sensCaloDets.swap(sensDets.second);
00125         }
00126 
00127         std::cout << " Sensitive Detector building finished; found " << m_sensTkDets.size()
00128                   << " Tk type Producers, and " << m_sensCaloDets.size() 
00129                   << " Calo type producers " << std::endl;
00130     }
00131     for(Producers::iterator itProd = m_producers.begin();itProd != m_producers.end();
00132         ++itProd) { (*itProd)->produce(e,es); }
00133 }
00134 
00135 DEFINE_FWK_MODULE(GeometryProducer);