CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:47:04 2009 for CMSSW by  doxygen 1.5.4