CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OscarProducer.cc
Go to the documentation of this file.
2 
5 
8 
13 
15 
18 
21 #include "CLHEP/Random/Random.h"
22 
24 
25 #include <iostream>
26 
27 namespace {
28  //
29  // this machinery allows to set CLHEP static engine
30  // to the one defined by RandomNumberGenerator service
31  // at the beginning of an event, and reset it back to
32  // "default-default" at the end of the event;
33  // Dave D. has decided to implement it this way because
34  // we don't know if there're other modules using CLHEP
35  // static engine, thus we want to ensure that the one
36  // we use for OscarProducer is unique to OscarProducer
37  //
38  class StaticRandomEngineSetUnset {
39  public:
40  StaticRandomEngineSetUnset();
41  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
42  ~StaticRandomEngineSetUnset();
43  CLHEP::HepRandomEngine* getEngine() const;
44  private:
45  CLHEP::HepRandomEngine* m_currentEngine;
46  CLHEP::HepRandomEngine* m_previousEngine;
47  };
48 }
49 
51 {
52  StaticRandomEngineSetUnset random;
53  m_engine = random.getEngine();
54 
55  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
56  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
57  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
58  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
59  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
60  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
61  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
62  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
63  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
64  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
65  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
66  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
67  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
68  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
69 
70  produces<edm::PSimHitContainer>("TotemHitsT1");
71  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
72  produces<edm::PSimHitContainer>("TotemHitsRP");
73  produces<edm::PSimHitContainer>("FP420SI");
74  produces<edm::PSimHitContainer>("BSCHits");
75  produces<edm::PSimHitContainer>("PLTHits");
76 
77  produces<edm::PCaloHitContainer>("EcalHitsEB");
78  produces<edm::PCaloHitContainer>("EcalHitsEE");
79  produces<edm::PCaloHitContainer>("EcalHitsES");
80  produces<edm::PCaloHitContainer>("HcalHits");
81  produces<edm::PCaloHitContainer>("CaloHitsTk");
82  produces<edm::PSimHitContainer>("MuonDTHits");
83  produces<edm::PSimHitContainer>("MuonCSCHits");
84  produces<edm::PSimHitContainer>("MuonRPCHits");
85  produces<edm::PCaloHitContainer>("CastorPL");
86  produces<edm::PCaloHitContainer>("CastorFI");
87  produces<edm::PCaloHitContainer>("CastorBU");
88  produces<edm::PCaloHitContainer>("CastorTU");
89  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
90  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
91  produces<edm::PCaloHitContainer>("ZDCHITS");
92  produces<edm::PCaloHitContainer>("ChamberHits");
93  produces<edm::PCaloHitContainer>("FibreHits");
94  produces<edm::PCaloHitContainer>("WedgeHits");
95 
96  //m_runManager = RunManager::init(p);
97  m_runManager = new RunManager(p);
98 
99  //register any products
100  m_producers= m_runManager->producers();
101 
102  for(Producers::iterator itProd = m_producers.begin();
103  itProd != m_producers.end();
104  ++itProd) {
105  (*itProd)->registerProducts(*this);
106  }
107 
108  //UIsession manager for message handling
110 
111 }
112 
114 {
115  //this is causing a seg fault when an exception occurs while constructing
116  // an HcalSD. Need to check for memory problems.
117  if (m_runManager!=0) delete m_runManager;
118  if (m_UIsession!=0) delete m_UIsession;
119 
120 }
121 
123 {
124  m_runManager->initG4(es);
125 }
126 
127 
129 {
130  StaticRandomEngineSetUnset random(m_engine);
131 }
132 
134 
136 {
137  StaticRandomEngineSetUnset random(m_engine);
138 
139  std::vector<SensitiveTkDetector*>& sTk = m_runManager->sensTkDetectors();
140  std::vector<SensitiveCaloDetector*>& sCalo = m_runManager->sensCaloDetectors();
141 
142  try
143  {
144  m_runManager->produce(e,es);
145 
146  std::auto_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
147  std::auto_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
148  G4SimEvent * evt = m_runManager->simEvent();
149  evt->load(*p1);
150  evt->load(*p2);
151 
152  e.put(p1);
153  e.put(p2);
154 
155  for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin(); it != sTk.end(); it++)
156  {
157  std::vector<std::string> v = (*it)->getNames();
158  for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
159  {
160  std::auto_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
161  (*it)->fillHits(*product,*in);
162  e.put(product,*in);
163  }
164  }
165  for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin(); it != sCalo.end(); it++)
166  {
167  std::vector<std::string> v = (*it)->getNames();
168  for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
169  {
170  std::auto_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
171  (*it)->fillHits(*product,*in);
172  e.put(product,*in);
173  }
174  }
175 
176  for(Producers::iterator itProd = m_producers.begin();
177  itProd != m_producers.end();
178  ++itProd) {
179  (*itProd)->produce(e,es);
180  }
181  }
182  catch ( const SimG4Exception& simg4ex )
183  {
184 
185  edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !" << simg4ex.what() << std::endl ;
186 
189  }
190 }
191 
192 
193 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset() {
194 
195  using namespace edm;
197 
198  if ( ! rng.isAvailable()) {
199  throw cms::Exception("Configuration")
200  << "The OscarProducer module requires the RandomNumberGeneratorService\n"
201  "which is not present in the configuration file. You must add the service\n"
202  "in the configuration file if you want to run OscarProducer";
203  }
204  m_currentEngine = &(rng->getEngine());
205 
206  m_previousEngine = CLHEP::HepRandom::getTheEngine();
207  CLHEP::HepRandom::setTheEngine(m_currentEngine);
208 }
209 
210 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine) {
211 
212  m_currentEngine = engine;
213 
214  m_previousEngine = CLHEP::HepRandom::getTheEngine();
215  CLHEP::HepRandom::setTheEngine(m_currentEngine);
216 }
217 
218 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() {
219  CLHEP::HepRandom::setTheEngine(m_previousEngine);
220 }
221 
222 CLHEP::HepRandomEngine*
223 StaticRandomEngineSetUnset::getEngine() const { return m_currentEngine; }
224 
226 
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
Definition: RunManager.h:72
Producers m_producers
Definition: OscarProducer.h:34
virtual void endJob()
std::vector< PCaloHit > PCaloHitContainer
CLHEP::HepRandomEngine * m_engine
Definition: OscarProducer.h:38
OscarProducer(edm::ParameterSet const &p)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TRandom random
Definition: MVATrainer.cc:138
std::vector< SensitiveTkDetector * > & sensTkDetectors()
Definition: RunManager.h:71
virtual ~OscarProducer()
void abortEvent()
Definition: RunManager.cc:361
virtual void beginRun(edm::Run &r, const edm::EventSetup &c)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
virtual const char * what() const
double p2[4]
Definition: TauolaWrapper.h:90
RunManager * m_runManager
Definition: OscarProducer.h:33
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:55
CustomUIsession * m_UIsession
Definition: OscarProducer.h:35
std::vector< SimVertex > SimVertexContainer
double p1[4]
Definition: TauolaWrapper.h:89
G4SimEvent * simEvent()
Definition: RunManager.h:70
std::vector< PSimHit > PSimHitContainer
std::vector< SimTrack > SimTrackContainer
virtual void beginJob()
mathSSE::Vec4< T > v
void initG4(const edm::EventSetup &es)
Definition: RunManager.cc:167
virtual void produce(edm::Event &e, const edm::EventSetup &c)
Definition: Run.h:32
void produce(edm::Event &inpevt, const edm::EventSetup &es)
Definition: RunManager.cc:285