CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
59  private:
60  CLHEP::HepRandomEngine* m_currentEngine;
61  CLHEP::HepRandomEngine* m_previousEngine;
62  };
63 } // namespace
64 
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>("CTPPSPixelHits");
90  produces<edm::PSimHitContainer>("CTPPSTimingHits");
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  produces<edm::PCaloHitContainer>("CastorPL");
114  produces<edm::PCaloHitContainer>("CastorFI");
115  produces<edm::PCaloHitContainer>("CastorBU");
116  produces<edm::PCaloHitContainer>("CastorTU");
117  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
118  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
119  produces<edm::PCaloHitContainer>("ZDCHITS");
120  produces<edm::PCaloHitContainer>("ChamberHits");
121  produces<edm::PCaloHitContainer>("FibreHits");
122  produces<edm::PCaloHitContainer>("WedgeHits");
123  produces<edm::PCaloHitContainer>("HFNoseHits");
124 
125  //register any products
126  auto& producers = m_runManagerWorker->producers();
127 
128  for (Producers::iterator itProd = producers.begin(); itProd != producers.end(); ++itProd) {
129  (*itProd)->registerProducts(producesCollector());
130  }
131 }
132 
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,
143  const edm::EventSetup& iSetup,
144  const OscarMTMasterThread* masterThread) {
145  // Random number generation not allowed here
146  StaticRandomEngineSetUnset random(nullptr);
147 
148  masterThread->beginRun(iSetup);
149 
150  return std::shared_ptr<int>();
151 }
152 
153 void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext) {
154  iContext->global()->endRun();
155 }
156 
157 void OscarMTProducer::globalEndJob(OscarMTMasterThread* masterThread) { masterThread->stopThread(); }
158 
160  // Random number generation not allowed here
161  StaticRandomEngineSetUnset random(nullptr);
162  m_runManagerWorker->endRun();
163 }
164 
166  StaticRandomEngineSetUnset random(e.streamID());
167  LogDebug("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID()
168  << " rand= " << G4UniformRand();
169 
170  auto& sTk = m_runManagerWorker->sensTkDetectors();
171  auto& sCalo = m_runManagerWorker->sensCaloDetectors();
172 
173  std::unique_ptr<G4SimEvent> evt;
174  try {
175  evt = m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster());
176  } catch (const SimG4Exception& simg4ex) {
177  edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what();
178 
180  << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n"
181  << simg4ex.what();
182  }
183 
184  std::unique_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
185  std::unique_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
186  evt->load(*p1);
187  evt->load(*p2);
188 
189  e.put(std::move(p1));
190  e.put(std::move(p2));
191 
192  for (auto& tracker : sTk) {
193  const std::vector<std::string>& v = tracker->getNames();
194  for (auto& name : v) {
195  std::unique_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
196  tracker->fillHits(*product, name);
197  e.put(std::move(product), name);
198  }
199  }
200  for (auto& calo : sCalo) {
201  const std::vector<std::string>& v = calo->getNames();
202 
203  for (auto& name : v) {
204  std::unique_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
205  calo->fillHits(*product, name);
206  e.put(std::move(product), name);
207  }
208  }
209 
210  auto& producers = m_runManagerWorker->producers();
211  for (auto& prod : producers) {
212  prod.get()->produce(e, es);
213  }
214  LogDebug("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID()
215  << " rand= " << G4UniformRand();
216 }
217 
218 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
220  if (!rng.isAvailable()) {
221  throw cms::Exception("Configuration")
222  << "The OscarMTProducer module requires the RandomNumberGeneratorService\n"
223  "which is not present in the configuration file. You must add the service\n"
224  "in the configuration file if you want to run OscarMTProducer";
225  }
226  m_currentEngine = &(rng->getEngine(streamID));
227 
228  m_previousEngine = G4Random::getTheEngine();
229  G4Random::setTheEngine(m_currentEngine);
230 }
231 
232 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
233  m_currentEngine = engine;
234  m_previousEngine = G4Random::getTheEngine();
235  G4Random::setTheEngine(m_currentEngine);
236 }
237 
238 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() { G4Random::setTheEngine(m_previousEngine); }
239 
#define LogDebug(id)
std::vector< PCaloHit > PCaloHitContainer
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
static std::shared_ptr< int > globalBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const OscarMTMasterThread *masterThread)
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
~OscarMTProducer() override
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
void beginRun(const edm::EventSetup &iSetup) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const char * what() const override
bool isAvailable() const
Definition: Service.h:40
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)
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:59
HLT enums.
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:96
std::vector< PSimHit > PSimHitContainer
static void globalEndJob(OscarMTMasterThread *masterThread)
std::vector< SimTrack > SimTrackContainer
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45