CMS 3D CMS Logo

OscarProducer.cc
Go to the documentation of this file.
2 
6 
9 
15 
17 
20 
23 #include "Randomize.hh"
24 
27 
28 #include <iostream>
29 
30 namespace edm {
31  class StreamID;
32 }
33 
34 namespace {
35  //
36  // this machinery allows to set CLHEP static engine
37  // to the one defined by RandomNumberGenerator service
38  // at the beginning of an event, and reset it back to
39  // "default-default" at the end of the event;
40  // Dave D. has decided to implement it this way because
41  // we don't know if there're other modules using CLHEP
42  // static engine, thus we want to ensure that the one
43  // we use for OscarProducer is unique to OscarProducer
44  //
45  // !!! This not only sets the random engine used by GEANT.
46  // There are a few SimWatchers/SimProducers that generate
47  // random number and also use the global CLHEP random engine
48  // set by this code. If we ever change this design be careful
49  // not to forget about them!!!
50 
51  class StaticRandomEngineSetUnset {
52  public:
53  StaticRandomEngineSetUnset(edm::StreamID const&);
54  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
55  ~StaticRandomEngineSetUnset();
56  private:
57  CLHEP::HepRandomEngine* m_currentEngine;
58  CLHEP::HepRandomEngine* m_previousEngine;
59  };
60 }
61 
63 {
64  // Random number generation not allowed here
65  StaticRandomEngineSetUnset random(nullptr);
66 
69 
70  consumes<edm::HepMCProduct>(p.getParameter<edm::InputTag>("HepMCProductLabel"));
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  produces<edm::PSimHitContainer>("BHMHits");
96  produces<edm::PSimHitContainer>("FastTimerHitsBarrel");
97  produces<edm::PSimHitContainer>("FastTimerHitsEndcap");
98 
99  produces<edm::PCaloHitContainer>("EcalHitsEB");
100  produces<edm::PCaloHitContainer>("EcalHitsEE");
101  produces<edm::PCaloHitContainer>("EcalHitsES");
102  produces<edm::PCaloHitContainer>("HcalHits");
103  produces<edm::PCaloHitContainer>("CaloHitsTk");
104  produces<edm::PCaloHitContainer>("HGCHitsEE");
105  produces<edm::PCaloHitContainer>("HGCHitsHEfront");
106  produces<edm::PCaloHitContainer>("HGCHitsHEback");
107 
108  produces<edm::PSimHitContainer>("MuonDTHits");
109  produces<edm::PSimHitContainer>("MuonCSCHits");
110  produces<edm::PSimHitContainer>("MuonRPCHits");
111  produces<edm::PSimHitContainer>("MuonGEMHits");
112  produces<edm::PSimHitContainer>("MuonME0Hits");
113 
114  produces<edm::PCaloHitContainer>("CastorPL");
115  produces<edm::PCaloHitContainer>("CastorFI");
116  produces<edm::PCaloHitContainer>("CastorBU");
117  produces<edm::PCaloHitContainer>("CastorTU");
118  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
119  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
120  produces<edm::PCaloHitContainer>("ZDCHITS");
121  produces<edm::PCaloHitContainer>("ChamberHits");
122  produces<edm::PCaloHitContainer>("FibreHits");
123  produces<edm::PCaloHitContainer>("WedgeHits");
124  produces<edm::PCaloHitContainer>("HFNoseHits");
125 
126  //register any products
127  m_producers = m_runManager->producers();
128 
129  for(Producers::iterator itProd = m_producers.begin();
130  itProd != m_producers.end(); ++itProd) {
131 
132  (*itProd)->registerProducts(*this);
133  }
134 
135  //UIsession manager for message handling
136  m_UIsession.reset(new CustomUIsession());
137 }
138 
140 { }
141 
142 void
144 {
145  // Random number generation not allowed here
146  StaticRandomEngineSetUnset random(nullptr);
147  m_runManager->initG4(es);
148 }
149 
150 void
152 {
153  m_runManager->stopG4();
154 }
155 
157 {
158  StaticRandomEngineSetUnset random(e.streamID());
159 
160  std::vector<SensitiveTkDetector*>& sTk =
161  m_runManager->sensTkDetectors();
162  std::vector<SensitiveCaloDetector*>& sCalo =
163  m_runManager->sensCaloDetectors();
164 
165  try { m_runManager->produce(e, es); }
166  catch ( const SimG4Exception& simg4ex ) {
167 
168  edm::LogInfo("SimG4CoreApplication") << "SimG4Exception caght! "
169  << simg4ex.what();
170  m_runManager->stopG4();
171 
173  << "SimG4CoreApplication exception in generation of event "
174  << e.id() << " in stream " << e.streamID() << " \n"
175  << simg4ex.what();
176  }
177 
178  std::unique_ptr<edm::SimTrackContainer>
180  std::unique_ptr<edm::SimVertexContainer>
182  G4SimEvent * evt = m_runManager->simEvent();
183  evt->load(*p1);
184  evt->load(*p2);
185 
186  e.put(std::move(p1));
187  e.put(std::move(p2));
188 
189  for (auto & tracker : sTk) {
190 
191  const std::vector<std::string>& v = tracker->getNames();
192  for (auto & name : v) {
193 
194  std::unique_ptr<edm::PSimHitContainer>
195  product(new edm::PSimHitContainer);
196  tracker->fillHits(*product,name);
197  e.put(std::move(product),name);
198  }
199  }
200  for (auto & calo : sCalo) {
201 
202  const std::vector<std::string>& v = calo->getNames();
203 
204  for (auto & name : v) {
205 
206  std::unique_ptr<edm::PCaloHitContainer>
207  product(new edm::PCaloHitContainer);
208  calo->fillHits(*product,name);
209  e.put(std::move(product),name);
210  }
211  }
212 
213  for(auto & prod : m_producers) {
214  prod.get()->produce(e,es);
215  }
216 }
217 
218 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
219  edm::StreamID const& streamID)
220 {
222  if ( ! rng.isAvailable()) {
223  throw cms::Exception("Configuration")
224  << "The OscarProducer module requires the RandomNumberGeneratorService\n"
225  "which is not present in the configuration file. You must add the service\n"
226  "in the configuration file if you want to run OscarProducer";
227  }
228  m_currentEngine = &(rng->getEngine(streamID));
229 
230  // Must use G4Random instead of CLHEP::HepRandom even for the serial
231  // version if Geant4 has been built with MT enabled. If G4 was built
232  // with MT disabled G4Random is defined to be CLHEP::HepRandom,
233  // preserving the old behaviour.
234  m_previousEngine = G4Random::getTheEngine();
235  G4Random::setTheEngine(m_currentEngine);
236 }
237 
238 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
239  CLHEP::HepRandomEngine * engine)
240 {
241  m_currentEngine = engine;
242  m_previousEngine = G4Random::getTheEngine();
243  G4Random::setTheEngine(m_currentEngine);
244 }
245 
246 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset()
247 {
248  G4Random::setTheEngine(m_previousEngine);
249 }
250 
T getParameter(std::string const &) const
std::vector< PCaloHit > PCaloHitContainer
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
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 CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
const char * what() const override
static const std::string kGEANT
bool isAvailable() const
Definition: Service.h:46
void produce(edm::Event &e, const edm::EventSetup &c) override
static const std::string kCLHEPRandomEngine
double p2[4]
Definition: TauolaWrapper.h:90
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:40
std::vector< SimVertex > SimVertexContainer
edm::EventID id() const
Definition: EventBase.h:60
~OscarProducer() override
HLT enums.
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:96
std::vector< PSimHit > PSimHitContainer
std::vector< SimTrack > SimTrackContainer
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:44