CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/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::PCaloHitContainer>("CastorPL");
00086     produces<edm::PCaloHitContainer>("CastorFI");
00087     produces<edm::PCaloHitContainer>("CastorBU");
00088     produces<edm::PCaloHitContainer>("CastorTU");
00089     produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
00090     produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
00091     produces<edm::PCaloHitContainer>("ZDCHITS"); 
00092     produces<edm::PCaloHitContainer>("ChamberHits"); 
00093     produces<edm::PCaloHitContainer>("FibreHits"); 
00094     produces<edm::PCaloHitContainer>("WedgeHits"); 
00095     
00096     //m_runManager = RunManager::init(p);
00097     m_runManager = new RunManager(p);
00098 
00099     //register any products 
00100     m_producers= m_runManager->producers();
00101 
00102     for(Producers::iterator itProd = m_producers.begin();
00103         itProd != m_producers.end();
00104         ++itProd) {
00105        (*itProd)->registerProducts(*this);
00106     }
00107 
00108     //UIsession manager for message handling
00109     m_UIsession = new CustomUIsession();
00110 
00111 }
00112 
00113 OscarProducer::~OscarProducer() 
00114 { 
00115   //this is causing a seg fault when an exception occurs while constructing
00116   // an HcalSD.  Need to check for memory problems. 
00117   if (m_runManager!=0) delete m_runManager; 
00118   if (m_UIsession!=0) delete m_UIsession;
00119 
00120 }
00121 
00122 void OscarProducer::beginRun(edm::Run & r, const edm::EventSetup & es)
00123 {
00124   m_runManager->initG4(es);
00125 }
00126 
00127 
00128 void OscarProducer::beginJob()
00129 {
00130   StaticRandomEngineSetUnset random(m_engine);
00131 }
00132  
00133 void OscarProducer::endJob() { }
00134  
00135 void OscarProducer::produce(edm::Event & e, const edm::EventSetup & es)
00136 {
00137     StaticRandomEngineSetUnset random(m_engine);
00138 
00139     std::vector<SensitiveTkDetector*>& sTk = m_runManager->sensTkDetectors();
00140     std::vector<SensitiveCaloDetector*>& sCalo = m_runManager->sensCaloDetectors();
00141 
00142     try
00143     {
00144     m_runManager->produce(e,es);
00145 
00146     std::auto_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
00147     std::auto_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
00148     G4SimEvent * evt = m_runManager->simEvent();
00149     evt->load(*p1);
00150     evt->load(*p2);   
00151 
00152     e.put(p1);
00153     e.put(p2);
00154 
00155     for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin(); it != sTk.end(); it++)
00156     {
00157         std::vector<std::string> v = (*it)->getNames();
00158         for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
00159         {
00160             std::auto_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
00161             (*it)->fillHits(*product,*in);
00162             e.put(product,*in);
00163         }
00164     }
00165     for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin(); it != sCalo.end(); it++)
00166     {
00167         std::vector<std::string>  v = (*it)->getNames();
00168         for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
00169         {
00170             std::auto_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
00171             (*it)->fillHits(*product,*in);
00172             e.put(product,*in);
00173         }
00174     }
00175 
00176     for(Producers::iterator itProd = m_producers.begin();
00177         itProd != m_producers.end();
00178         ++itProd) {
00179        (*itProd)->produce(e,es);
00180     }
00181     }
00182     catch ( const SimG4Exception& simg4ex )
00183     {
00184        
00185        edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !" << simg4ex.what() << std::endl ;
00186        
00187        m_runManager->abortEvent() ;
00188        throw edm::Exception( edm::errors::EventCorruption ) ;
00189     }
00190 }
00191 
00192 
00193 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset() {
00194 
00195     using namespace edm;
00196     Service<RandomNumberGenerator> rng;
00197 
00198     if ( ! rng.isAvailable()) {
00199         throw cms::Exception("Configuration")
00200             << "The OscarProducer module requires the RandomNumberGeneratorService\n"
00201                "which is not present in the configuration file.  You must add the service\n"
00202                "in the configuration file if you want to run OscarProducer";
00203     }
00204     m_currentEngine = &(rng->getEngine());
00205 
00206     m_previousEngine = CLHEP::HepRandom::getTheEngine();
00207     CLHEP::HepRandom::setTheEngine(m_currentEngine);
00208 }
00209 
00210 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine) {
00211 
00212     m_currentEngine = engine;
00213 
00214     m_previousEngine = CLHEP::HepRandom::getTheEngine();
00215     CLHEP::HepRandom::setTheEngine(m_currentEngine);
00216 }
00217 
00218 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() {
00219     CLHEP::HepRandom::setTheEngine(m_previousEngine);
00220 }
00221 
00222 CLHEP::HepRandomEngine*
00223 StaticRandomEngineSetUnset::getEngine() const { return m_currentEngine; }
00224 
00225 DEFINE_FWK_MODULE(OscarProducer);
00226