CMS 3D CMS Logo

OscarMTProducer.cc
Go to the documentation of this file.
2 
6 
7 #include "OscarMTProducer.h"
8 
14 
19 
21 
24 
27 #include "Randomize.hh"
28 
30 
31 #include <iostream>
32 
33 namespace edm {
34  class StreamID;
35 }
36 
37 namespace {
38  //
39  // this machinery allows to set CLHEP static engine
40  // to the one defined by RandomNumberGenerator service
41  // at the beginning of an event, and reset it back to
42  // "default-default" at the end of the event;
43  // Dave D. has decided to implement it this way because
44  // we don't know if there're other modules using CLHEP
45  // static engine, thus we want to ensure that the one
46  // we use for OscarMTProducer is unique to OscarMTProducer
47  //
48  // !!! This not only sets the random engine used by GEANT.
49  // There are a few SimWatchers/SimProducers that generate
50  // random number and also use the global CLHEP random engine
51  // set by this code. If we ever change this design be careful
52  // not to forget about them!!!
53 
54  class StaticRandomEngineSetUnset {
55  public:
56  StaticRandomEngineSetUnset(edm::StreamID const&);
57  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine);
58  ~StaticRandomEngineSetUnset();
59 
60  private:
61  CLHEP::HepRandomEngine* m_currentEngine;
62  CLHEP::HepRandomEngine* m_previousEngine;
63  };
64 } // namespace
65 
67  // Random number generation not allowed here
68  StaticRandomEngineSetUnset random(nullptr);
69 
70  m_runManagerWorker = std::make_unique<RunManagerMTWorker>(p, consumesCollector());
71  m_masterThread = ms;
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>("CTPPSPixelHits");
92  produces<edm::PSimHitContainer>("CTPPSTimingHits");
93  produces<edm::PSimHitContainer>("FP420SI");
94  produces<edm::PSimHitContainer>("BSCHits");
95  produces<edm::PSimHitContainer>("PLTHits");
96  produces<edm::PSimHitContainer>("BCM1FHits");
97  produces<edm::PSimHitContainer>("BHMHits");
98  produces<edm::PSimHitContainer>("FastTimerHitsBarrel");
99  produces<edm::PSimHitContainer>("FastTimerHitsEndcap");
100 
101  produces<edm::PCaloHitContainer>("EcalHitsEB");
102  produces<edm::PCaloHitContainer>("EcalHitsEE");
103  produces<edm::PCaloHitContainer>("EcalHitsES");
104  produces<edm::PCaloHitContainer>("HcalHits");
105  produces<edm::PCaloHitContainer>("CaloHitsTk");
106  produces<edm::PCaloHitContainer>("HGCHitsEE");
107  produces<edm::PCaloHitContainer>("HGCHitsHEfront");
108  produces<edm::PCaloHitContainer>("HGCHitsHEback");
109 
110  produces<edm::PSimHitContainer>("MuonDTHits");
111  produces<edm::PSimHitContainer>("MuonCSCHits");
112  produces<edm::PSimHitContainer>("MuonRPCHits");
113  produces<edm::PSimHitContainer>("MuonGEMHits");
114  produces<edm::PSimHitContainer>("MuonME0Hits");
115  produces<edm::PCaloHitContainer>("CastorPL");
116  produces<edm::PCaloHitContainer>("CastorFI");
117  produces<edm::PCaloHitContainer>("CastorBU");
118  produces<edm::PCaloHitContainer>("CastorTU");
119  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
120  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
121  produces<edm::PCaloHitContainer>("ZDCHITS");
122  produces<edm::PCaloHitContainer>("ChamberHits");
123  produces<edm::PCaloHitContainer>("FibreHits");
124  produces<edm::PCaloHitContainer>("WedgeHits");
125  produces<edm::PCaloHitContainer>("HFNoseHits");
126  produces<edm::PCaloHitContainer>("TotemHitsT2Scint");
127 
128  //register any products
129  auto& producers = m_runManagerWorker->producers();
130 
131  for (Producers::iterator itProd = producers.begin(); itProd != producers.end(); ++itProd) {
132  (*itProd)->registerProducts(producesCollector());
133  }
134  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed";
135 }
136 
138 
139 std::unique_ptr<OscarMTMasterThread> OscarMTProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) {
140  // Random number generation not allowed here
141  StaticRandomEngineSetUnset random(nullptr);
142  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::initializeGlobalCache";
143 
144  return std::unique_ptr<OscarMTMasterThread>(new OscarMTMasterThread(iConfig));
145 }
146 
147 std::shared_ptr<int> OscarMTProducer::globalBeginRun(const edm::Run& iRun,
148  const edm::EventSetup& iSetup,
149  const OscarMTMasterThread* masterThread) {
150  // Random number generation not allowed here
151  StaticRandomEngineSetUnset random(nullptr);
152 
153  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun";
154  masterThread->beginRun(iSetup);
155  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun done";
156  return std::shared_ptr<int>();
157 }
158 
159 void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext) {
160  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalEndRun";
161  iContext->global()->endRun();
162 }
163 
165  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalEndJob";
166  masterThread->stopThread();
167 }
168 
170  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun";
172  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun done";
173 }
174 
176  // Random number generation not allowed here
177  StaticRandomEngineSetUnset random(nullptr);
178  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun";
179  m_runManagerWorker->endRun();
180  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun done";
181 }
182 
184  StaticRandomEngineSetUnset random(e.streamID());
185  edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID();
186  LogDebug("SimG4CoreApplication") << "Before event rand= " << G4UniformRand();
187 
188  auto& sTk = m_runManagerWorker->sensTkDetectors();
189  auto& sCalo = m_runManagerWorker->sensCaloDetectors();
190 
191  std::unique_ptr<G4SimEvent> evt;
192  try {
193  evt = m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster());
194  } catch (const SimG4Exception& simg4ex) {
195  edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what();
196 
198  << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n"
199  << simg4ex.what();
200  }
201 
202  std::unique_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
203  std::unique_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
204  evt->load(*p1);
205  evt->load(*p2);
206 
207  e.put(std::move(p1));
208  e.put(std::move(p2));
209 
210  for (auto& tracker : sTk) {
211  const std::vector<std::string>& v = tracker->getNames();
212  for (auto& name : v) {
213  std::unique_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
214  tracker->fillHits(*product, name);
215  if (product != nullptr && !product->empty())
216  edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " traker hits <" << name << ">";
217  e.put(std::move(product), name);
218  }
219  }
220  for (auto& calo : sCalo) {
221  const std::vector<std::string>& v = calo->getNames();
222 
223  for (auto& name : v) {
224  std::unique_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
225  calo->fillHits(*product, name);
226  if (product != nullptr && !product->empty())
227  edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " calo hits <" << name << ">";
228  e.put(std::move(product), name);
229  }
230  }
231 
232  auto& producers = m_runManagerWorker->producers();
233  for (auto& prod : producers) {
234  prod.get()->produce(e, es);
235  }
236  edm::LogVerbatim("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID();
237  LogDebug("SimG4CoreApplication") << "End of event rand= " << G4UniformRand();
238 }
239 
240 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
242  if (!rng.isAvailable()) {
243  throw cms::Exception("Configuration")
244  << "The OscarMTProducer module requires the RandomNumberGeneratorService\n"
245  "which is not present in the configuration file. You must add the service\n"
246  "in the configuration file if you want to run OscarMTProducer";
247  }
248  m_currentEngine = &(rng->getEngine(streamID));
249 
250  m_previousEngine = G4Random::getTheEngine();
251  G4Random::setTheEngine(m_currentEngine);
252 }
253 
254 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
255  m_currentEngine = engine;
256  m_previousEngine = G4Random::getTheEngine();
257  G4Random::setTheEngine(m_currentEngine);
258 }
259 
260 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() { G4Random::setTheEngine(m_previousEngine); }
261 
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
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:159
OscarMTProducer::OscarMTProducer
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
Definition: OscarMTProducer.cc:66
HLT_FULL_cff.producers
producers
Definition: HLT_FULL_cff.py:13094
findQualityFiles.v
v
Definition: findQualityFiles.py:179
OscarMTProducer
Definition: OscarMTProducer.h:18
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
OscarMTProducer::initializeGlobalCache
static std::unique_ptr< OscarMTMasterThread > initializeGlobalCache(const edm::ParameterSet &iConfig)
Definition: OscarMTProducer.cc:139
OscarMTProducer::produce
void produce(edm::Event &e, const edm::EventSetup &c) override
Definition: OscarMTProducer.cc:183
SensitiveTkDetector.h
edm::stream::RunContextT
Definition: Contexts.h:31
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:147
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:39
PbPb_ZMuSkimMuonDPG_cff.tracker
tracker
Definition: PbPb_ZMuSkimMuonDPG_cff.py:60
SimG4Exception.h
OscarMTMasterThread::stopThread
void stopThread()
Definition: OscarMTMasterThread.cc:155
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
edm::ParameterSet
Definition: ParameterSet.h:47
RunManagerMT.h
OscarMTMasterThread::runManagerMasterPtr
RunManagerMT * runManagerMasterPtr() const
Definition: OscarMTMasterThread.h:42
G4SimEvent.h
edm::Service< edm::RandomNumberGenerator >
p1
double p1[4]
Definition: TauolaWrapper.h:89
OscarMTProducer::~OscarMTProducer
~OscarMTProducer() override
Definition: OscarMTProducer.cc:137
edm::EventSetup
Definition: EventSetup.h:58
OscarMTMasterThread
Definition: OscarMTMasterThread.h:32
OscarMTProducer::globalEndJob
static void globalEndJob(OscarMTMasterThread *masterThread)
Definition: OscarMTProducer.cc:164
eostools.move
def move(src, dest)
Definition: eostools.py:511
PluginManager.h
edm::SimTrackContainer
std::vector< SimTrack > SimTrackContainer
Definition: SimTrackContainer.h:12
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
Exception
Definition: hltDiff.cc:245
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:122
PCaloHitContainer.h
OscarMTProducer::beginRun
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: OscarMTProducer.cc:169
OscarMTProducer.h
OscarMTProducer::m_masterThread
const OscarMTMasterThread * m_masterThread
Definition: OscarMTProducer.h:40
ConsumesCollector.h
OscarMTProducer::endRun
void endRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: OscarMTProducer.cc:175
edm::PSimHitContainer
std::vector< PSimHit > PSimHitContainer
Definition: PSimHitContainer.h:11
ParameterSet.h
calo
Definition: Common.h:9
edm::Event
Definition: Event.h:73
edm::Log
Definition: MessageLogger.h:70
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