CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/SimG4Core/Application/plugins/OscarProducer.cc

Go to the documentation of this file.
00001 #include "FWCore/PluginManager/interface/PluginManager.h"
00002 
00003 #include "FWCore/Framework/interface/MakerMacros.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 
00006 #include "SimG4Core/Application/interface/OscarProducer.h"
00007 #include "SimG4Core/Application/interface/G4SimEvent.h"
00008 
00009 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00010 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00011 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00012 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00013 
00014 #include "SimG4Core/Watcher/interface/SimProducer.h"
00015 
00016 #include "FWCore/Utilities/interface/Exception.h"
00017 #include "SimG4Core/Notification/interface/SimG4Exception.h"
00018 
00019 #include "FWCore/ServiceRegistry/interface/Service.h"
00020 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00021 #include "CLHEP/Random/Random.h"
00022 
00023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00024 
00025 #include <iostream>
00026 
00027 namespace {
00028     //
00029     // this machinery allows to set CLHEP static engine
00030     // to the one defined by RandomNumberGenerator service
00031     // at the beginning of an event, and reset it back to
00032     // "default-default" at the end of the event;
00033     // Dave D. has decided to implement it this way because
00034     // we don't know if there're other modules using CLHEP
00035     // static engine, thus we want to ensure that the one
00036     // we use for OscarProducer is unique to OscarProducer
00037     //
00038     class StaticRandomEngineSetUnset {
00039     public:
00040         StaticRandomEngineSetUnset();
00041         explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
00042         ~StaticRandomEngineSetUnset();
00043         CLHEP::HepRandomEngine* getEngine() const;
00044     private:
00045         CLHEP::HepRandomEngine* m_currentEngine;
00046         CLHEP::HepRandomEngine* m_previousEngine;
00047     };
00048 }
00049 
00050 OscarProducer::OscarProducer(edm::ParameterSet const & p)
00051 {   
00052     StaticRandomEngineSetUnset random;
00053     m_engine = random.getEngine();
00054     
00055     produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
00056     produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
00057     produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
00058     produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
00059     produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
00060     produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
00061     produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
00062     produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
00063     produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
00064     produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
00065     produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
00066     produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
00067     produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
00068     produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
00069     
00070     produces<edm::PSimHitContainer>("TotemHitsT1");
00071     produces<edm::PSimHitContainer>("TotemHitsT2Gem");
00072     produces<edm::PSimHitContainer>("TotemHitsRP");
00073     produces<edm::PSimHitContainer>("FP420SI");
00074     produces<edm::PSimHitContainer>("BSCHits");
00075     produces<edm::PSimHitContainer>("PLTHits");
00076 
00077     produces<edm::PCaloHitContainer>("EcalHitsEB");
00078     produces<edm::PCaloHitContainer>("EcalHitsEE");
00079     produces<edm::PCaloHitContainer>("EcalHitsES");
00080     produces<edm::PCaloHitContainer>("HcalHits");
00081     produces<edm::PCaloHitContainer>("CaloHitsTk");
00082     produces<edm::PSimHitContainer>("MuonDTHits");
00083     produces<edm::PSimHitContainer>("MuonCSCHits");
00084     produces<edm::PSimHitContainer>("MuonRPCHits");
00085     produces<edm::PSimHitContainer>("MuonGEMHits");
00086     produces<edm::PCaloHitContainer>("CastorPL");
00087     produces<edm::PCaloHitContainer>("CastorFI");
00088     produces<edm::PCaloHitContainer>("CastorBU");
00089     produces<edm::PCaloHitContainer>("CastorTU");
00090     produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
00091     produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
00092     produces<edm::PCaloHitContainer>("ZDCHITS"); 
00093     produces<edm::PCaloHitContainer>("ChamberHits"); 
00094     produces<edm::PCaloHitContainer>("FibreHits"); 
00095     produces<edm::PCaloHitContainer>("WedgeHits"); 
00096     
00097     //m_runManager = RunManager::init(p);
00098     m_runManager = new RunManager(p);
00099 
00100     //register any products 
00101     m_producers= m_runManager->producers();
00102 
00103     for(Producers::iterator itProd = m_producers.begin();
00104         itProd != m_producers.end();
00105         ++itProd) {
00106        (*itProd)->registerProducts(*this);
00107     }
00108 
00109     //UIsession manager for message handling
00110     m_UIsession = new CustomUIsession();
00111 
00112 }
00113 
00114 OscarProducer::~OscarProducer() 
00115 { 
00116   //this is causing a seg fault when an exception occurs while constructing
00117   // an HcalSD.  Need to check for memory problems. 
00118   if (m_runManager!=0) delete m_runManager; 
00119   if (m_UIsession!=0) delete m_UIsession;
00120 
00121 }
00122 
00123 void OscarProducer::beginRun(edm::Run & r, const edm::EventSetup & es)
00124 {
00125   m_runManager->initG4(es);
00126 }
00127 
00128 
00129 void OscarProducer::beginJob()
00130 {
00131   StaticRandomEngineSetUnset random(m_engine);
00132 }
00133  
00134 void OscarProducer::endJob() { }
00135  
00136 void OscarProducer::produce(edm::Event & e, const edm::EventSetup & es)
00137 {
00138     StaticRandomEngineSetUnset random(m_engine);
00139 
00140     std::vector<SensitiveTkDetector*>& sTk = m_runManager->sensTkDetectors();
00141     std::vector<SensitiveCaloDetector*>& sCalo = m_runManager->sensCaloDetectors();
00142 
00143     try
00144     {
00145     m_runManager->produce(e,es);
00146 
00147     std::auto_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
00148     std::auto_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
00149     G4SimEvent * evt = m_runManager->simEvent();
00150     evt->load(*p1);
00151     evt->load(*p2);   
00152 
00153     e.put(p1);
00154     e.put(p2);
00155 
00156     for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin(); it != sTk.end(); it++)
00157     {
00158         std::vector<std::string> v = (*it)->getNames();
00159         for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
00160         {
00161             std::auto_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
00162             (*it)->fillHits(*product,*in);
00163             e.put(product,*in);
00164         }
00165     }
00166     for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin(); it != sCalo.end(); it++)
00167     {
00168         std::vector<std::string>  v = (*it)->getNames();
00169         for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
00170         {
00171             std::auto_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
00172             (*it)->fillHits(*product,*in);
00173             e.put(product,*in);
00174         }
00175     }
00176 
00177     for(Producers::iterator itProd = m_producers.begin();
00178         itProd != m_producers.end();
00179         ++itProd) {
00180        (*itProd)->produce(e,es);
00181     }
00182     }
00183     catch ( const SimG4Exception& simg4ex )
00184     {
00185        
00186        edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !" << simg4ex.what() << std::endl ;
00187        
00188        m_runManager->abortEvent() ;
00189        throw edm::Exception( edm::errors::EventCorruption ) ;
00190     }
00191 }
00192 
00193 
00194 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset() {
00195 
00196     using namespace edm;
00197     Service<RandomNumberGenerator> rng;
00198 
00199     if ( ! rng.isAvailable()) {
00200         throw cms::Exception("Configuration")
00201             << "The OscarProducer module requires the RandomNumberGeneratorService\n"
00202                "which is not present in the configuration file.  You must add the service\n"
00203                "in the configuration file if you want to run OscarProducer";
00204     }
00205     m_currentEngine = &(rng->getEngine());
00206 
00207     m_previousEngine = CLHEP::HepRandom::getTheEngine();
00208     CLHEP::HepRandom::setTheEngine(m_currentEngine);
00209 }
00210 
00211 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine) {
00212 
00213     m_currentEngine = engine;
00214 
00215     m_previousEngine = CLHEP::HepRandom::getTheEngine();
00216     CLHEP::HepRandom::setTheEngine(m_currentEngine);
00217 }
00218 
00219 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() {
00220     CLHEP::HepRandom::setTheEngine(m_previousEngine);
00221 }
00222 
00223 CLHEP::HepRandomEngine*
00224 StaticRandomEngineSetUnset::getEngine() const { return m_currentEngine; }
00225 
00226 DEFINE_FWK_MODULE(OscarProducer);
00227