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 "Randomize.hh"
22 
25 
26 #include <iostream>
27 
28 namespace edm {
29  class StreamID;
30 }
31 
32 namespace {
33  //
34  // this machinery allows to set CLHEP static engine
35  // to the one defined by RandomNumberGenerator service
36  // at the beginning of an event, and reset it back to
37  // "default-default" at the end of the event;
38  // Dave D. has decided to implement it this way because
39  // we don't know if there're other modules using CLHEP
40  // static engine, thus we want to ensure that the one
41  // we use for OscarProducer is unique to OscarProducer
42  //
43  // !!! This not only sets the random engine used by GEANT.
44  // There are a few SimWatchers/SimProducers that generate
45  // random number and also use the global CLHEP random engine
46  // set by this code. If we ever change this design be careful
47  // not to forget about them!!!
48 
49  class StaticRandomEngineSetUnset {
50  public:
51  StaticRandomEngineSetUnset(edm::StreamID const&);
52  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
53  ~StaticRandomEngineSetUnset();
54  CLHEP::HepRandomEngine* getEngine() const;
55  private:
56  CLHEP::HepRandomEngine* m_currentEngine;
57  CLHEP::HepRandomEngine* m_previousEngine;
58  };
59 }
60 
62 {
63  // Random number generation not allowed here
64  StaticRandomEngineSetUnset random(nullptr);
65 
68 
69  consumes<edm::HepMCProduct>(p.getParameter<edm::InputTag>("HepMCProductLabel"));
70  m_runManager.reset(new RunManager(p));
71  //m_runManager.reset(new RunManager(p, consumesCollector()));
72 
73  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
74  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
75  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
76  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
77  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
78  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
79  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
80  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
81  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
82  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
83  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
84  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
85  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
86  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
87 
88  produces<edm::PSimHitContainer>("TotemHitsT1");
89  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
90  produces<edm::PSimHitContainer>("TotemHitsRP");
91  produces<edm::PSimHitContainer>("FP420SI");
92  produces<edm::PSimHitContainer>("BSCHits");
93  produces<edm::PSimHitContainer>("PLTHits");
94  produces<edm::PSimHitContainer>("BCM1FHits");
95 
96  produces<edm::PCaloHitContainer>("EcalHitsEB");
97  produces<edm::PCaloHitContainer>("EcalHitsEE");
98  produces<edm::PCaloHitContainer>("EcalHitsES");
99  produces<edm::PCaloHitContainer>("HcalHits");
100  produces<edm::PCaloHitContainer>("CaloHitsTk");
101  produces<edm::PSimHitContainer>("MuonDTHits");
102  produces<edm::PSimHitContainer>("MuonCSCHits");
103  produces<edm::PSimHitContainer>("MuonRPCHits");
104  produces<edm::PSimHitContainer>("MuonGEMHits");
105  produces<edm::PSimHitContainer>("MuonME0Hits");
106  produces<edm::PCaloHitContainer>("CastorPL");
107  produces<edm::PCaloHitContainer>("CastorFI");
108  produces<edm::PCaloHitContainer>("CastorBU");
109  produces<edm::PCaloHitContainer>("CastorTU");
110  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
111  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
112  produces<edm::PCaloHitContainer>("ZDCHITS");
113  produces<edm::PCaloHitContainer>("ChamberHits");
114  produces<edm::PCaloHitContainer>("FibreHits");
115  produces<edm::PCaloHitContainer>("WedgeHits");
116 
117  //register any products
118  m_producers = m_runManager->producers();
119 
120  for(Producers::iterator itProd = m_producers.begin();
121  itProd != m_producers.end(); ++itProd) {
122 
123  (*itProd)->registerProducts(*this);
124  }
125 
126  //UIsession manager for message handling
127  m_UIsession.reset(new CustomUIsession());
128 }
129 
131 { }
132 
133 void
135 {
136  // Random number generation not allowed here
137  StaticRandomEngineSetUnset random(nullptr);
138  m_runManager->initG4(es);
139 }
140 
141 void
143 {
144  m_runManager->stopG4();
145 }
146 
148 {
149  StaticRandomEngineSetUnset random(e.streamID());
150 
151  std::vector<SensitiveTkDetector*>& sTk =
152  m_runManager->sensTkDetectors();
153  std::vector<SensitiveCaloDetector*>& sCalo =
154  m_runManager->sensCaloDetectors();
155 
156  try {
157 
158  m_runManager->produce(e, es);
159 
160  std::auto_ptr<edm::SimTrackContainer>
162  std::auto_ptr<edm::SimVertexContainer>
164  G4SimEvent * evt = m_runManager->simEvent();
165  evt->load(*p1);
166  evt->load(*p2);
167 
168  e.put(p1);
169  e.put(p2);
170 
171  for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin();
172  it != sTk.end(); ++it) {
173 
174  std::vector<std::string> v = (*it)->getNames();
175  for (std::vector<std::string>::iterator in = v.begin();
176  in!= v.end(); ++in) {
177 
178  std::auto_ptr<edm::PSimHitContainer>
179  product(new edm::PSimHitContainer);
180  (*it)->fillHits(*product,*in);
181  e.put(product,*in);
182  }
183  }
184  for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin();
185  it != sCalo.end(); ++it) {
186 
187  std::vector<std::string> v = (*it)->getNames();
188 
189  for (std::vector<std::string>::iterator in = v.begin();
190  in!= v.end(); in++) {
191 
192  std::auto_ptr<edm::PCaloHitContainer>
193  product(new edm::PCaloHitContainer);
194  (*it)->fillHits(*product,*in);
195  e.put(product,*in);
196  }
197  }
198 
199  for(Producers::iterator itProd = m_producers.begin();
200  itProd != m_producers.end(); ++itProd) {
201 
202  (*itProd)->produce(e,es);
203  }
204 
205  } catch ( const SimG4Exception& simg4ex ) {
206 
207  edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !"
208  << simg4ex.what();
209 
210  m_runManager->abortEvent();
212  }
213 }
214 
215 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
216  edm::StreamID const& streamID)
217 {
219  if ( ! rng.isAvailable()) {
220  throw cms::Exception("Configuration")
221  << "The OscarProducer module requires the RandomNumberGeneratorService\n"
222  "which is not present in the configuration file. You must add the service\n"
223  "in the configuration file if you want to run OscarProducer";
224  }
225  m_currentEngine = &(rng->getEngine(streamID));
226 
227  // Must use G4Random instead of CLHEP::HepRandom even for the serial
228  // version if Geant4 has been built with MT enabled. If G4 was built
229  // with MT disabled G4Random is defined to be CLHEP::HepRandom,
230  // preserving the old behaviour.
231  m_previousEngine = G4Random::getTheEngine();
232  G4Random::setTheEngine(m_currentEngine);
233 }
234 
235 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
236  CLHEP::HepRandomEngine * engine)
237 {
238  m_currentEngine = engine;
239  m_previousEngine = G4Random::getTheEngine();
240  G4Random::setTheEngine(m_currentEngine);
241 }
242 
243 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset()
244 {
245  G4Random::setTheEngine(m_previousEngine);
246 }
247 
248 CLHEP::HepRandomEngine* StaticRandomEngineSetUnset::getEngine() const
249 {
250  return m_currentEngine;
251 }
252 
T getParameter(std::string const &) const
Producers m_producers
Definition: OscarProducer.h:31
std::unique_ptr< RunManager > m_runManager
Definition: OscarProducer.h:30
std::vector< PCaloHit > PCaloHitContainer
virtual void endRun(const edm::Run &r, const edm::EventSetup &c) override
OscarProducer(edm::ParameterSet const &p)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TRandom random
Definition: MVATrainer.cc:138
virtual ~OscarProducer()
virtual void beginRun(const edm::Run &r, const edm::EventSetup &c) override
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
static const std::string kGEANT
bool isAvailable() const
Definition: Service.h:46
virtual void produce(edm::Event &e, const edm::EventSetup &c) override
static const std::string kCLHEPRandomEngine
virtual const char * what() const
double p2[4]
Definition: TauolaWrapper.h:90
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:55
std::vector< SimVertex > SimVertexContainer
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &) const =0
Use this engine in event methods.
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:74
std::vector< PSimHit > PSimHitContainer
std::vector< SimTrack > SimTrackContainer
std::unique_ptr< CustomUIsession > m_UIsession
Definition: OscarProducer.h:32
Definition: Run.h:41