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  CLHEP::HepRandomEngine* getEngine() const;
59  private:
60  CLHEP::HepRandomEngine* m_currentEngine;
61  CLHEP::HepRandomEngine* m_previousEngine;
62  };
63 }
64 
66 {
67  // Random number generation not allowed here
68  StaticRandomEngineSetUnset random(nullptr);
69 
71 
72  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
73  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
74  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
75  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
76  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
77  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
78  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
79  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
80  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
81  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
82  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
83  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
84  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
85  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
86 
87  produces<edm::PSimHitContainer>("TotemHitsT1");
88  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
89  produces<edm::PSimHitContainer>("TotemHitsRP");
90  produces<edm::PSimHitContainer>("FP420SI");
91  produces<edm::PSimHitContainer>("BSCHits");
92  produces<edm::PSimHitContainer>("PLTHits");
93  produces<edm::PSimHitContainer>("BCM1FHits");
94 
95  produces<edm::PCaloHitContainer>("EcalHitsEB");
96  produces<edm::PCaloHitContainer>("EcalHitsEE");
97  produces<edm::PCaloHitContainer>("EcalHitsES");
98  produces<edm::PCaloHitContainer>("HcalHits");
99  produces<edm::PCaloHitContainer>("CaloHitsTk");
100  produces<edm::PSimHitContainer>("MuonDTHits");
101  produces<edm::PSimHitContainer>("MuonCSCHits");
102  produces<edm::PSimHitContainer>("MuonRPCHits");
103  produces<edm::PSimHitContainer>("MuonGEMHits");
104  produces<edm::PSimHitContainer>("MuonME0Hits");
105  produces<edm::PCaloHitContainer>("CastorPL");
106  produces<edm::PCaloHitContainer>("CastorFI");
107  produces<edm::PCaloHitContainer>("CastorBU");
108  produces<edm::PCaloHitContainer>("CastorTU");
109  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
110  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
111  produces<edm::PCaloHitContainer>("ZDCHITS");
112  produces<edm::PCaloHitContainer>("ChamberHits");
113  produces<edm::PCaloHitContainer>("FibreHits");
114  produces<edm::PCaloHitContainer>("WedgeHits");
115 
116  //register any products
117  m_producers = m_runManagerWorker->producers();
118 
119  for(Producers::iterator itProd = m_producers.begin();
120  itProd != m_producers.end(); ++itProd) {
121 
122  (*itProd)->registerProducts(*this);
123  }
124 }
125 
127 { }
128 
129 std::unique_ptr<OscarMTMasterThread> OscarMTProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) {
130  // Random number generation not allowed here
131  StaticRandomEngineSetUnset random(nullptr);
132 
133  return std::unique_ptr<OscarMTMasterThread>(new OscarMTMasterThread(iConfig));
134 }
135 
136 std::shared_ptr<int> OscarMTProducer::globalBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const OscarMTMasterThread *masterThread) {
137  // Random number generation not allowed here
138  StaticRandomEngineSetUnset random(nullptr);
139 
140  masterThread->beginRun(iSetup);
141 
142  return std::shared_ptr<int>();
143 }
144 
145 void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext *iContext) {
146  iContext->global()->endRun();
147 }
148 
150  masterThread->stopThread();
151 }
152 
153 void
155 {
156  // Random number generation not allowed here
157  StaticRandomEngineSetUnset random(nullptr);
158  m_runManagerWorker->endRun();
159 }
160 
162 {
163  StaticRandomEngineSetUnset random(e.streamID());
164 
165  std::vector<SensitiveTkDetector*>& sTk =
166  m_runManagerWorker->sensTkDetectors();
167  std::vector<SensitiveCaloDetector*>& sCalo =
168  m_runManagerWorker->sensCaloDetectors();
169 
170  try {
171  m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster());
172 
173  std::auto_ptr<edm::SimTrackContainer>
175  std::auto_ptr<edm::SimVertexContainer>
177  G4SimEvent * evt = m_runManagerWorker->simEvent();
178  evt->load(*p1);
179  evt->load(*p2);
180 
181  e.put(p1);
182  e.put(p2);
183 
184  for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin();
185  it != sTk.end(); ++it) {
186 
187  std::vector<std::string> v = (*it)->getNames();
188  for (std::vector<std::string>::iterator in = v.begin();
189  in!= v.end(); ++in) {
190 
191  std::auto_ptr<edm::PSimHitContainer>
192  product(new edm::PSimHitContainer);
193  (*it)->fillHits(*product,*in);
194  e.put(product,*in);
195  }
196  }
197  for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin();
198  it != sCalo.end(); ++it) {
199 
200  std::vector<std::string> v = (*it)->getNames();
201 
202  for (std::vector<std::string>::iterator in = v.begin();
203  in!= v.end(); in++) {
204 
205  std::auto_ptr<edm::PCaloHitContainer>
206  product(new edm::PCaloHitContainer);
207  (*it)->fillHits(*product,*in);
208  e.put(product,*in);
209  }
210  }
211 
212  for(Producers::iterator itProd = m_producers.begin();
213  itProd != m_producers.end(); ++itProd) {
214 
215  (*itProd)->produce(e,es);
216  }
217  } catch ( const SimG4Exception& simg4ex ) {
218 
219  edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !"
220  << simg4ex.what();
221 
222  m_runManagerWorker->abortEvent();
224  }
225 
226 }
227 
228 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
229  edm::StreamID const& streamID)
230 {
232  if ( ! rng.isAvailable()) {
233  throw cms::Exception("Configuration")
234  << "The OscarMTProducer module requires the RandomNumberGeneratorService\n"
235  "which is not present in the configuration file. You must add the service\n"
236  "in the configuration file if you want to run OscarMTProducer";
237  }
238  m_currentEngine = &(rng->getEngine(streamID));
239 
240  m_previousEngine = G4Random::getTheEngine();
241  G4Random::setTheEngine(m_currentEngine);
242 }
243 
244 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
245  CLHEP::HepRandomEngine * engine)
246 {
247  m_currentEngine = engine;
248  m_previousEngine = G4Random::getTheEngine();
249  G4Random::setTheEngine(m_currentEngine);
250 }
251 
252 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset()
253 {
254  G4Random::setTheEngine(m_previousEngine);
255 }
256 
257 CLHEP::HepRandomEngine* StaticRandomEngineSetUnset::getEngine() const
258 {
259  return m_currentEngine;
260 }
261 
std::vector< PCaloHit > PCaloHitContainer
static std::shared_ptr< int > globalBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const OscarMTMasterThread *masterThread)
std::unique_ptr< RunManagerMTWorker > m_runManagerWorker
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
TRandom random
Definition: MVATrainer.cc:138
virtual ~OscarMTProducer()
void beginRun(const edm::EventSetup &iSetup) const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
bool isAvailable() const
Definition: Service.h:46
Producers m_producers
virtual void produce(edm::Event &e, const edm::EventSetup &c) override
virtual const char * what() const
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)
virtual void endRun(const edm::Run &r, const edm::EventSetup &c) override
std::vector< SimVertex > SimVertexContainer
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &) const =0
Use this engine in event methods.
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:74
std::vector< PSimHit > PSimHitContainer
static void globalEndJob(OscarMTMasterThread *masterThread)
std::vector< SimTrack > SimTrackContainer
Definition: Run.h:41