CMS 3D CMS Logo

OscarProducer.cc
Go to the documentation of this file.
2 
6 
7 #include "OscarProducer.h"
9 
15 
17 
20 
23 #include "Randomize.hh"
24 
27 
28 #include <iostream>
29 #include <memory>
30 
31 namespace edm {
32  class StreamID;
33 }
34 
35 namespace {
36  //
37  // this machinery allows to set CLHEP static engine
38  // to the one defined by RandomNumberGenerator service
39  // at the beginning of an event, and reset it back to
40  // "default-default" at the end of the event;
41  // Dave D. has decided to implement it this way because
42  // we don't know if there're other modules using CLHEP
43  // static engine, thus we want to ensure that the one
44  // we use for OscarProducer is unique to OscarProducer
45  //
46  // !!! This not only sets the random engine used by GEANT.
47  // There are a few SimWatchers/SimProducers that generate
48  // random number and also use the global CLHEP random engine
49  // set by this code. If we ever change this design be careful
50  // not to forget about them!!!
51 
52  class StaticRandomEngineSetUnset {
53  public:
54  StaticRandomEngineSetUnset(edm::StreamID const&);
55  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine);
56  ~StaticRandomEngineSetUnset();
57 
58  private:
59  CLHEP::HepRandomEngine* m_currentEngine;
60  CLHEP::HepRandomEngine* m_previousEngine;
61  };
62 } // namespace
63 
65  // Random number generation not allowed here
66  StaticRandomEngineSetUnset random(nullptr);
67 
70 
71  consumes<edm::HepMCProduct>(p.getParameter<edm::InputTag>("HepMCProductLabel"));
72  m_runManager = std::make_unique<RunManager>(p, consumesCollector());
73 
74  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
75  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
76  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
77  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
78  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
79  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
80  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
81  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
82  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
83  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
84  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
85  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
86  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
87  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
88 
89  produces<edm::PSimHitContainer>("TotemHitsT1");
90  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
91  produces<edm::PSimHitContainer>("TotemHitsRP");
92  produces<edm::PSimHitContainer>("CTPPSPixelHits");
93  produces<edm::PSimHitContainer>("CTPPSTimingHits");
94  produces<edm::PSimHitContainer>("FP420SI");
95  produces<edm::PSimHitContainer>("BSCHits");
96  produces<edm::PSimHitContainer>("PLTHits");
97  produces<edm::PSimHitContainer>("BCM1FHits");
98  produces<edm::PSimHitContainer>("BHMHits");
99  produces<edm::PSimHitContainer>("FastTimerHitsBarrel");
100  produces<edm::PSimHitContainer>("FastTimerHitsEndcap");
101 
102  produces<edm::PCaloHitContainer>("EcalHitsEB");
103  produces<edm::PCaloHitContainer>("EcalHitsEE");
104  produces<edm::PCaloHitContainer>("EcalHitsES");
105  produces<edm::PCaloHitContainer>("HcalHits");
106  produces<edm::PCaloHitContainer>("CaloHitsTk");
107  produces<edm::PCaloHitContainer>("HGCHitsEE");
108  produces<edm::PCaloHitContainer>("HGCHitsHEfront");
109  produces<edm::PCaloHitContainer>("HGCHitsHEback");
110 
111  produces<edm::PSimHitContainer>("MuonDTHits");
112  produces<edm::PSimHitContainer>("MuonCSCHits");
113  produces<edm::PSimHitContainer>("MuonRPCHits");
114  produces<edm::PSimHitContainer>("MuonGEMHits");
115  produces<edm::PSimHitContainer>("MuonME0Hits");
116 
117  produces<edm::PCaloHitContainer>("CastorPL");
118  produces<edm::PCaloHitContainer>("CastorFI");
119  produces<edm::PCaloHitContainer>("CastorBU");
120  produces<edm::PCaloHitContainer>("CastorTU");
121  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
122  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
123  produces<edm::PCaloHitContainer>("ZDCHITS");
124  produces<edm::PCaloHitContainer>("ChamberHits");
125  produces<edm::PCaloHitContainer>("FibreHits");
126  produces<edm::PCaloHitContainer>("WedgeHits");
127  produces<edm::PCaloHitContainer>("HFNoseHits");
128  produces<edm::PCaloHitContainer>("TotemHitsT2Scint");
129 
130  //register any products
131  m_producers = m_runManager->producers();
132 
133  for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
134  (*itProd)->registerProducts(producesCollector());
135  }
136 
137  //UIsession manager for message handling
138  m_UIsession = std::make_unique<CustomUIsession>();
139 }
140 
142 
144  // Random number generation not allowed here
145  StaticRandomEngineSetUnset random(nullptr);
146  m_runManager->initG4(es);
147 }
148 
149 void OscarProducer::endRun(const edm::Run&, const edm::EventSetup&) { m_runManager->stopG4(); }
150 
152  StaticRandomEngineSetUnset random(e.streamID());
153 
154  auto& sTk = m_runManager->sensTkDetectors();
155  auto& sCalo = m_runManager->sensCaloDetectors();
156 
157  try {
158  m_runManager->produce(e, es);
159  } catch (const SimG4Exception& simg4ex) {
160  edm::LogInfo("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what();
161  m_runManager->stopG4();
162 
164  << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n"
165  << simg4ex.what();
166  }
167 
168  std::unique_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
169  std::unique_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
170  G4SimEvent* evt = m_runManager->simEvent();
171  evt->load(*p1);
172  evt->load(*p2);
173 
174  e.put(std::move(p1));
175  e.put(std::move(p2));
176 
177  for (auto& tracker : sTk) {
178  const std::vector<std::string>& v = tracker->getNames();
179  for (auto& name : v) {
180  std::unique_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
181  tracker->fillHits(*product, name);
182  e.put(std::move(product), name);
183  }
184  }
185  for (auto& calo : sCalo) {
186  const std::vector<std::string>& v = calo->getNames();
187 
188  for (auto& name : v) {
189  std::unique_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
190  calo->fillHits(*product, name);
191  e.put(std::move(product), name);
192  }
193  }
194 
195  for (auto& prod : m_producers) {
196  prod.get()->produce(e, es);
197  }
198 }
199 
200 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
202  if (!rng.isAvailable()) {
203  throw cms::Exception("Configuration")
204  << "The OscarProducer module requires the RandomNumberGeneratorService\n"
205  "which is not present in the configuration file. You must add the service\n"
206  "in the configuration file if you want to run OscarProducer";
207  }
208  m_currentEngine = &(rng->getEngine(streamID));
209 
210  // Must use G4Random instead of CLHEP::HepRandom even for the serial
211  // version if Geant4 has been built with MT enabled. If G4 was built
212  // with MT disabled G4Random is defined to be CLHEP::HepRandom,
213  // preserving the old behaviour.
214  m_previousEngine = G4Random::getTheEngine();
215  G4Random::setTheEngine(m_currentEngine);
216 }
217 
218 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
219  m_currentEngine = engine;
220  m_previousEngine = G4Random::getTheEngine();
221  G4Random::setTheEngine(m_currentEngine);
222 }
223 
224 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() { G4Random::setTheEngine(m_previousEngine); }
225 
edm::StreamID
Definition: StreamID.h:30
edm::RandomNumberGenerator::getEngine
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
MessageLogger.h
G4SimEvent
Definition: G4SimEvent.h:14
OscarProducer::m_UIsession
std::unique_ptr< CustomUIsession > m_UIsession
Definition: OscarProducer.h:29
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
G4SimEvent::load
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:37
PSimHitContainer.h
SimG4Exception
Definition: SimG4Exception.h:13
edm::errors::EventCorruption
Definition: EDMException.h:43
edm::EDConsumerBase::consumesCollector
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
Definition: EDConsumerBase.cc:46
edm::SharedResourceNames::kCLHEPRandomEngine
static const std::string kCLHEPRandomEngine
Definition: SharedResourceNames.h:25
OscarProducer::OscarProducer
OscarProducer(edm::ParameterSet const &p)
Definition: OscarProducer.cc:64
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Service.h
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
OscarProducer.h
p2
double p2[4]
Definition: TauolaWrapper.h:90
SharedResourceNames.h
PbPb_ZMuSkimMuonDPG_cff.tracker
tracker
Definition: PbPb_ZMuSkimMuonDPG_cff.py:60
SimG4Exception.h
edm::ParameterSet
Definition: ParameterSet.h:47
G4SimEvent.h
edm::Service< edm::RandomNumberGenerator >
OscarProducer::produce
void produce(edm::Event &e, const edm::EventSetup &c) override
Definition: OscarProducer.cc:151
p1
double p1[4]
Definition: TauolaWrapper.h:89
edm::EventSetup
Definition: EventSetup.h:57
OscarProducer::~OscarProducer
~OscarProducer() override
Definition: OscarProducer.cc:141
OscarProducer::beginRun
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: OscarProducer.cc:143
eostools.move
def move(src, dest)
Definition: eostools.py:511
OscarProducer
Definition: OscarProducer.h:16
PluginManager.h
edm::SimTrackContainer
std::vector< SimTrack > SimTrackContainer
Definition: SimTrackContainer.h:12
Exception
Definition: hltDiff.cc:246
edm::SharedResourceNames::kGEANT
static const std::string kGEANT
Definition: SharedResourceNames.h:24
OscarProducer::endRun
void endRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: OscarProducer.cc:149
edm::PCaloHitContainer
std::vector< PCaloHit > PCaloHitContainer
Definition: PCaloHitContainer.h:8
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
PCaloHitContainer.h
ConsumesCollector.h
edm::PSimHitContainer
std::vector< PSimHit > PSimHitContainer
Definition: PSimHitContainer.h:11
ParameterSet.h
calo
Definition: Common.h:9
HepMCProduct.h
OscarProducer::m_producers
Producers m_producers
Definition: OscarProducer.h:28
edm::Event
Definition: Event.h:73
OscarProducer::m_runManager
std::unique_ptr< RunManager > m_runManager
Definition: OscarProducer.h:27
SimG4Exception::what
const char * what() const override
Definition: SimG4Exception.h:17
edm::ProducerBase::producesCollector
ProducesCollector producesCollector()
Definition: ProducerBase.cc:105
SimTrackContainer.h
edm::SimVertexContainer
std::vector< SimVertex > SimVertexContainer
Definition: SimVertexContainer.h:12
edm::InputTag
Definition: InputTag.h:15
SimVertexContainer.h
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37