CMS 3D CMS Logo

OscarMTProducer.cc
Go to the documentation of this file.
2 
6 
13 
18 
20 
23 
26 #include "Randomize.hh"
27 
29 
30 #include <iostream>
31 
32 namespace edm {
33  class StreamID;
34 }
35 
36 namespace {
37  //
38  // this machinery allows to set CLHEP static engine
39  // to the one defined by RandomNumberGenerator service
40  // at the beginning of an event, and reset it back to
41  // "default-default" at the end of the event;
42  // Dave D. has decided to implement it this way because
43  // we don't know if there're other modules using CLHEP
44  // static engine, thus we want to ensure that the one
45  // we use for OscarMTProducer is unique to OscarMTProducer
46  //
47  // !!! This not only sets the random engine used by GEANT.
48  // There are a few SimWatchers/SimProducers that generate
49  // random number and also use the global CLHEP random engine
50  // set by this code. If we ever change this design be careful
51  // not to forget about them!!!
52 
53  class StaticRandomEngineSetUnset {
54  public:
55  StaticRandomEngineSetUnset(edm::StreamID const&);
56  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
57  ~StaticRandomEngineSetUnset();
58  private:
59  CLHEP::HepRandomEngine* m_currentEngine;
60  CLHEP::HepRandomEngine* m_previousEngine;
61  };
62 }
63 
65 {
66  // Random number generation not allowed here
67  StaticRandomEngineSetUnset random(nullptr);
68 
69  m_runManagerWorker.reset(new RunManagerMTWorker(p, consumesCollector()));
70 
71  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
72  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
73  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
74  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
75  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
76  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
77  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
78  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
79  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
80  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
81  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
82  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
83  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
84  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
85 
86  produces<edm::PSimHitContainer>("TotemHitsT1");
87  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
88  produces<edm::PSimHitContainer>("TotemHitsRP");
89  produces<edm::PSimHitContainer>("FP420SI");
90  produces<edm::PSimHitContainer>("BSCHits");
91  produces<edm::PSimHitContainer>("PLTHits");
92  produces<edm::PSimHitContainer>("BCM1FHits");
93  produces<edm::PSimHitContainer>("BHMHits");
94  produces<edm::PSimHitContainer>("FastTimerHitsBarrel");
95  produces<edm::PSimHitContainer>("FastTimerHitsEndcap");
96 
97  produces<edm::PCaloHitContainer>("EcalHitsEB");
98  produces<edm::PCaloHitContainer>("EcalHitsEE");
99  produces<edm::PCaloHitContainer>("EcalHitsES");
100  produces<edm::PCaloHitContainer>("HcalHits");
101  produces<edm::PCaloHitContainer>("CaloHitsTk");
102  produces<edm::PCaloHitContainer>("HGCHitsEE");
103  produces<edm::PCaloHitContainer>("HGCHitsHEfront");
104  produces<edm::PCaloHitContainer>("HGCHitsHEback");
105 
106  produces<edm::PSimHitContainer>("MuonDTHits");
107  produces<edm::PSimHitContainer>("MuonCSCHits");
108  produces<edm::PSimHitContainer>("MuonRPCHits");
109  produces<edm::PSimHitContainer>("MuonGEMHits");
110  produces<edm::PSimHitContainer>("MuonME0Hits");
111  produces<edm::PCaloHitContainer>("CastorPL");
112  produces<edm::PCaloHitContainer>("CastorFI");
113  produces<edm::PCaloHitContainer>("CastorBU");
114  produces<edm::PCaloHitContainer>("CastorTU");
115  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
116  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
117  produces<edm::PCaloHitContainer>("ZDCHITS");
118  produces<edm::PCaloHitContainer>("ChamberHits");
119  produces<edm::PCaloHitContainer>("FibreHits");
120  produces<edm::PCaloHitContainer>("WedgeHits");
121 
122  //register any products
123  m_producers = m_runManagerWorker->producers();
124 
125  for(Producers::iterator itProd = m_producers.begin();
126  itProd != m_producers.end(); ++itProd) {
127 
128  (*itProd)->registerProducts(*this);
129  }
130 }
131 
133 { }
134 
135 std::unique_ptr<OscarMTMasterThread> OscarMTProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) {
136  // Random number generation not allowed here
137  StaticRandomEngineSetUnset random(nullptr);
138 
139  return std::unique_ptr<OscarMTMasterThread>(new OscarMTMasterThread(iConfig));
140 }
141 
142 std::shared_ptr<int> OscarMTProducer::globalBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const OscarMTMasterThread *masterThread) {
143  // Random number generation not allowed here
144  StaticRandomEngineSetUnset random(nullptr);
145 
146  masterThread->beginRun(iSetup);
147 
148  return std::shared_ptr<int>();
149 }
150 
151 void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext *iContext) {
152  iContext->global()->endRun();
153 }
154 
156  masterThread->stopThread();
157 }
158 
159 void
161 {
162  // Random number generation not allowed here
163  StaticRandomEngineSetUnset random(nullptr);
164  m_runManagerWorker->endRun();
165 }
166 
168 {
169  StaticRandomEngineSetUnset random(e.streamID());
170  LogDebug("SimG4CoreApplication")
171  << "Produce event " << e.id() << " stream " << e.streamID()
172  << " rand= " << G4UniformRand();
173 
174  std::vector<SensitiveTkDetector*>& sTk =
175  m_runManagerWorker->sensTkDetectors();
176  std::vector<SensitiveCaloDetector*>& sCalo =
177  m_runManagerWorker->sensCaloDetectors();
178 
179  try { m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster()); }
180  catch ( const SimG4Exception& simg4ex ) {
181 
182  edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! "
183  << simg4ex.what();
184 
186  << "SimG4CoreApplication exception in generation of event "
187  << e.id() << " in stream " << e.streamID() << " \n"
188  << simg4ex.what();
189  }
190 
191  std::unique_ptr<edm::SimTrackContainer>
193  std::unique_ptr<edm::SimVertexContainer>
195  G4SimEvent * evt = m_runManagerWorker->simEvent();
196  evt->load(*p1);
197  evt->load(*p2);
198 
199  e.put(std::move(p1));
200  e.put(std::move(p2));
201 
202  for (auto & tracker : sTk) {
203 
204  const std::vector<std::string>& v = tracker->getNames();
205  for (auto & name : v) {
206 
207  std::unique_ptr<edm::PSimHitContainer>
208  product(new edm::PSimHitContainer);
209  tracker->fillHits(*product,name);
210  e.put(std::move(product),name);
211  }
212  }
213  for (auto & calo : sCalo) {
214 
215  const std::vector<std::string>& v = calo->getNames();
216 
217  for (auto & name : v) {
218 
219  std::unique_ptr<edm::PCaloHitContainer>
220  product(new edm::PCaloHitContainer);
221  calo->fillHits(*product,name);
222  e.put(std::move(product),name);
223  }
224  }
225 
226  for(auto & prod : m_producers) {
227  prod.get()->produce(e,es);
228  }
229  LogDebug("SimG4CoreApplication")
230  << "Event is produced " << e.id() << " stream " << e.streamID()
231  << " rand= " << G4UniformRand();
232 }
233 
234 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
235  edm::StreamID const& streamID)
236 {
238  if ( ! rng.isAvailable()) {
239  throw cms::Exception("Configuration")
240  << "The OscarMTProducer module requires the RandomNumberGeneratorService\n"
241  "which is not present in the configuration file. You must add the service\n"
242  "in the configuration file if you want to run OscarMTProducer";
243  }
244  m_currentEngine = &(rng->getEngine(streamID));
245 
246  m_previousEngine = G4Random::getTheEngine();
247  G4Random::setTheEngine(m_currentEngine);
248 }
249 
250 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
251  CLHEP::HepRandomEngine * engine)
252 {
253  m_currentEngine = engine;
254  m_previousEngine = G4Random::getTheEngine();
255  G4Random::setTheEngine(m_currentEngine);
256 }
257 
258 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset()
259 {
260  G4Random::setTheEngine(m_previousEngine);
261 }
262 
#define LogDebug(id)
std::vector< PCaloHit > PCaloHitContainer
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
static std::shared_ptr< int > globalBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const OscarMTMasterThread *masterThread)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
~OscarMTProducer() override
TRandom random
Definition: MVATrainer.cc:138
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
void beginRun(const edm::EventSetup &iSetup) const
const char * what() const override
bool isAvailable() const
Definition: Service.h:46
void produce(edm::Event &e, const edm::EventSetup &c) override
G const * global() const
Definition: Contexts.h:35
double p2[4]
Definition: TauolaWrapper.h:90
static void globalEndRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const RunContext *iContext)
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:55
static std::unique_ptr< OscarMTMasterThread > initializeGlobalCache(const edm::ParameterSet &iConfig)
void endRun(const edm::Run &r, const edm::EventSetup &c) override
std::vector< SimVertex > SimVertexContainer
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:95
std::vector< PSimHit > PSimHitContainer
static void globalEndJob(OscarMTMasterThread *masterThread)
std::vector< SimTrack > SimTrackContainer
def move(src, dest)
Definition: eostools.py:510
Definition: Run.h:43