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 
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  produces<edm::PCaloHitContainer>("TotemHitsT2Scint");
125 
126  //register any products
127  auto& producers = m_runManagerWorker->producers();
128 
129  for (Producers::iterator itProd = producers.begin(); itProd != producers.end(); ++itProd) {
130  (*itProd)->registerProducts(producesCollector());
131  }
132 }
133 
135 
136 std::unique_ptr<OscarMTMasterThread> OscarMTProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) {
137  // Random number generation not allowed here
138  StaticRandomEngineSetUnset random(nullptr);
139 
140  return std::unique_ptr<OscarMTMasterThread>(new OscarMTMasterThread(iConfig));
141 }
142 
143 std::shared_ptr<int> OscarMTProducer::globalBeginRun(const edm::Run& iRun,
144  const edm::EventSetup& iSetup,
145  const OscarMTMasterThread* masterThread) {
146  // Random number generation not allowed here
147  StaticRandomEngineSetUnset random(nullptr);
148 
149  masterThread->beginRun(iSetup);
150 
151  return std::shared_ptr<int>();
152 }
153 
154 void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext) {
155  iContext->global()->endRun();
156 }
157 
158 void OscarMTProducer::globalEndJob(OscarMTMasterThread* masterThread) { masterThread->stopThread(); }
159 
161  // Random number generation not allowed here
162  StaticRandomEngineSetUnset random(nullptr);
163  m_runManagerWorker->endRun();
164 }
165 
167  StaticRandomEngineSetUnset random(e.streamID());
168  LogDebug("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID()
169  << " rand= " << G4UniformRand();
170 
171  auto& sTk = m_runManagerWorker->sensTkDetectors();
172  auto& sCalo = m_runManagerWorker->sensCaloDetectors();
173 
174  std::unique_ptr<G4SimEvent> evt;
175  try {
176  evt = m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster());
177  } catch (const SimG4Exception& simg4ex) {
178  edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what();
179 
181  << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n"
182  << simg4ex.what();
183  }
184 
185  std::unique_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
186  std::unique_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
187  evt->load(*p1);
188  evt->load(*p2);
189 
190  e.put(std::move(p1));
191  e.put(std::move(p2));
192 
193  for (auto& tracker : sTk) {
194  const std::vector<std::string>& v = tracker->getNames();
195  for (auto& name : v) {
196  std::unique_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
197  tracker->fillHits(*product, name);
198  e.put(std::move(product), name);
199  }
200  }
201  for (auto& calo : sCalo) {
202  const std::vector<std::string>& v = calo->getNames();
203 
204  for (auto& name : v) {
205  std::unique_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
206  calo->fillHits(*product, name);
207  e.put(std::move(product), name);
208  }
209  }
210 
211  auto& producers = m_runManagerWorker->producers();
212  for (auto& prod : producers) {
213  prod.get()->produce(e, es);
214  }
215  LogDebug("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID()
216  << " rand= " << G4UniformRand();
217 }
218 
219 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
221  if (!rng.isAvailable()) {
222  throw cms::Exception("Configuration")
223  << "The OscarMTProducer module requires the RandomNumberGeneratorService\n"
224  "which is not present in the configuration file. You must add the service\n"
225  "in the configuration file if you want to run OscarMTProducer";
226  }
227  m_currentEngine = &(rng->getEngine(streamID));
228 
229  m_previousEngine = G4Random::getTheEngine();
230  G4Random::setTheEngine(m_currentEngine);
231 }
232 
233 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
234  m_currentEngine = engine;
235  m_previousEngine = G4Random::getTheEngine();
236  G4Random::setTheEngine(m_currentEngine);
237 }
238 
239 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() { G4Random::setTheEngine(m_previousEngine); }
240 
edm::StreamID
Definition: StreamID.h:30
edm::RandomNumberGenerator::getEngine
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
MessageLogger.h
SensitiveCaloDetector.h
L1TowerCalibrationProducer_cfi.calo
calo
Definition: L1TowerCalibrationProducer_cfi.py:59
edm::Run
Definition: Run.h:45
edm
HLT enums.
Definition: AlignableModifier.h:19
SimProducer.h
RandomNumberGenerator.h
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
PSimHitContainer.h
SimG4Exception
Definition: SimG4Exception.h:13
edm::errors::EventCorruption
Definition: EDMException.h:43
RunManagerMTWorker.h
OscarMTProducer::globalEndRun
static void globalEndRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const RunContext *iContext)
Definition: OscarMTProducer.cc:154
OscarMTProducer::OscarMTProducer
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
Definition: OscarMTProducer.cc:65
findQualityFiles.v
v
Definition: findQualityFiles.py:179
OscarMTProducer
Definition: OscarMTProducer.h:18
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
OscarMTProducer::initializeGlobalCache
static std::unique_ptr< OscarMTMasterThread > initializeGlobalCache(const edm::ParameterSet &iConfig)
Definition: OscarMTProducer.cc:136
OscarMTProducer::produce
void produce(edm::Event &e, const edm::EventSetup &c) override
Definition: OscarMTProducer.cc:166
SensitiveTkDetector.h
edm::stream::RunContextT
Definition: Contexts.h:31
HLT_2018_cff.producers
producers
Definition: HLT_2018_cff.py:11702
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
OscarMTProducer::globalBeginRun
static std::shared_ptr< int > globalBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const OscarMTMasterThread *masterThread)
Definition: OscarMTProducer.cc:143
Service.h
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
p2
double p2[4]
Definition: TauolaWrapper.h:90
OscarMTProducer::m_runManagerWorker
std::unique_ptr< RunManagerMTWorker > m_runManagerWorker
Definition: OscarMTProducer.h:38
edm::LogWarning
Definition: MessageLogger.h:141
PbPb_ZMuSkimMuonDPG_cff.tracker
tracker
Definition: PbPb_ZMuSkimMuonDPG_cff.py:60
SimG4Exception.h
OscarMTMasterThread::stopThread
void stopThread()
Definition: OscarMTMasterThread.cc:160
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
edm::ParameterSet
Definition: ParameterSet.h:36
RunManagerMT.h
G4SimEvent.h
edm::Service< edm::RandomNumberGenerator >
p1
double p1[4]
Definition: TauolaWrapper.h:89
OscarMTProducer::~OscarMTProducer
~OscarMTProducer() override
Definition: OscarMTProducer.cc:134
edm::EventSetup
Definition: EventSetup.h:57
OscarMTMasterThread
Definition: OscarMTMasterThread.h:32
OscarMTProducer::globalEndJob
static void globalEndJob(OscarMTMasterThread *masterThread)
Definition: OscarMTProducer.cc:158
RunManagerMTWorker
Definition: RunManagerMTWorker.h:41
eostools.move
def move(src, dest)
Definition: eostools.py:511
PluginManager.h
edm::SimTrackContainer
std::vector< SimTrack > SimTrackContainer
Definition: SimTrackContainer.h:12
Exception
Definition: hltDiff.cc:246
edm::PCaloHitContainer
std::vector< PCaloHit > PCaloHitContainer
Definition: PCaloHitContainer.h:8
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
OscarMTMasterThread::beginRun
void beginRun(const edm::EventSetup &iSetup) const
Definition: OscarMTMasterThread.cc:127
PCaloHitContainer.h
OscarMTProducer.h
ConsumesCollector.h
OscarMTProducer::endRun
void endRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: OscarMTProducer.cc:160
edm::PSimHitContainer
std::vector< PSimHit > PSimHitContainer
Definition: PSimHitContainer.h:11
ParameterSet.h
edm::Event
Definition: Event.h:73
SimG4Exception::what
const char * what() const override
Definition: SimG4Exception.h:17
SimTrackContainer.h
edm::SimVertexContainer
std::vector< SimVertex > SimVertexContainer
Definition: SimVertexContainer.h:12
SimVertexContainer.h
edm::stream::RunContextT::global
G const * global() const
Definition: Contexts.h:35
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37