CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OscarProducer.cc
Go to the documentation of this file.
2 
5 
8 
13 
15 
18 
21 #include "CLHEP/Random/Random.h"
22 
25 
26 #include <iostream>
27 
28 namespace edm {
29  class StreamID;
30 }
31 
32 namespace {
33  //
34  // this machinery allows to set CLHEP static engine
35  // to the one defined by RandomNumberGenerator service
36  // at the beginning of an event, and reset it back to
37  // "default-default" at the end of the event;
38  // Dave D. has decided to implement it this way because
39  // we don't know if there're other modules using CLHEP
40  // static engine, thus we want to ensure that the one
41  // we use for OscarProducer is unique to OscarProducer
42  //
43  class StaticRandomEngineSetUnset {
44  public:
45  StaticRandomEngineSetUnset(edm::StreamID const&);
46  explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
47  ~StaticRandomEngineSetUnset();
48  CLHEP::HepRandomEngine* getEngine() const;
49  private:
50  CLHEP::HepRandomEngine* m_currentEngine;
51  CLHEP::HepRandomEngine* m_previousEngine;
52  };
53 }
54 
56 {
57  // Random number generation not allowed here
58  StaticRandomEngineSetUnset random(nullptr);
59 
62 
63  produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
64  produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
65  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
66  produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelHighTof");
67  produces<edm::PSimHitContainer>("TrackerHitsTIBLowTof");
68  produces<edm::PSimHitContainer>("TrackerHitsTIBHighTof");
69  produces<edm::PSimHitContainer>("TrackerHitsTIDLowTof");
70  produces<edm::PSimHitContainer>("TrackerHitsTIDHighTof");
71  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapLowTof");
72  produces<edm::PSimHitContainer>("TrackerHitsPixelEndcapHighTof");
73  produces<edm::PSimHitContainer>("TrackerHitsTOBLowTof");
74  produces<edm::PSimHitContainer>("TrackerHitsTOBHighTof");
75  produces<edm::PSimHitContainer>("TrackerHitsTECLowTof");
76  produces<edm::PSimHitContainer>("TrackerHitsTECHighTof");
77 
78  produces<edm::PSimHitContainer>("TotemHitsT1");
79  produces<edm::PSimHitContainer>("TotemHitsT2Gem");
80  produces<edm::PSimHitContainer>("TotemHitsRP");
81  produces<edm::PSimHitContainer>("FP420SI");
82  produces<edm::PSimHitContainer>("BSCHits");
83  produces<edm::PSimHitContainer>("PLTHits");
84 
85  produces<edm::PCaloHitContainer>("EcalHitsEB");
86  produces<edm::PCaloHitContainer>("EcalHitsEE");
87  produces<edm::PCaloHitContainer>("EcalHitsES");
88  produces<edm::PCaloHitContainer>("HcalHits");
89  produces<edm::PCaloHitContainer>("CaloHitsTk");
90  produces<edm::PSimHitContainer>("MuonDTHits");
91  produces<edm::PSimHitContainer>("MuonCSCHits");
92  produces<edm::PSimHitContainer>("MuonRPCHits");
93  produces<edm::PSimHitContainer>("MuonGEMHits");
94  produces<edm::PCaloHitContainer>("CastorPL");
95  produces<edm::PCaloHitContainer>("CastorFI");
96  produces<edm::PCaloHitContainer>("CastorBU");
97  produces<edm::PCaloHitContainer>("CastorTU");
98  produces<edm::PCaloHitContainer>("EcalTBH4BeamHits");
99  produces<edm::PCaloHitContainer>("HcalTB06BeamHits");
100  produces<edm::PCaloHitContainer>("ZDCHITS");
101  produces<edm::PCaloHitContainer>("ChamberHits");
102  produces<edm::PCaloHitContainer>("FibreHits");
103  produces<edm::PCaloHitContainer>("WedgeHits");
104 
105  //m_runManager = RunManager::init(p);
106  m_runManager.reset(new RunManager(p));
107 
108  //register any products
109  m_producers= m_runManager->producers();
110 
111  for(Producers::iterator itProd = m_producers.begin();
112  itProd != m_producers.end();
113  ++itProd) {
114  (*itProd)->registerProducts(*this);
115  }
116 
117  //UIsession manager for message handling
118  m_UIsession.reset(new CustomUIsession());
119 
120 }
121 
123 { }
124 
126 {
127  // Random number generation not allowed here
128  StaticRandomEngineSetUnset random(nullptr);
129  m_runManager->initG4(es);
130 }
131 
133 {
134  StaticRandomEngineSetUnset random(e.streamID());
135 
136  std::vector<SensitiveTkDetector*>& sTk = m_runManager->sensTkDetectors();
137  std::vector<SensitiveCaloDetector*>& sCalo = m_runManager->sensCaloDetectors();
138 
139  try
140  {
141  m_runManager->produce(e,es);
142 
143  std::auto_ptr<edm::SimTrackContainer> p1(new edm::SimTrackContainer);
144  std::auto_ptr<edm::SimVertexContainer> p2(new edm::SimVertexContainer);
145  G4SimEvent * evt = m_runManager->simEvent();
146  evt->load(*p1);
147  evt->load(*p2);
148 
149  e.put(p1);
150  e.put(p2);
151 
152  for (std::vector<SensitiveTkDetector*>::iterator it = sTk.begin(); it != sTk.end(); it++)
153  {
154  std::vector<std::string> v = (*it)->getNames();
155  for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
156  {
157  std::auto_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
158  (*it)->fillHits(*product,*in);
159  e.put(product,*in);
160  }
161  }
162  for (std::vector<SensitiveCaloDetector*>::iterator it = sCalo.begin(); it != sCalo.end(); it++)
163  {
164  std::vector<std::string> v = (*it)->getNames();
165  for (std::vector<std::string>::iterator in = v.begin(); in!= v.end(); in++)
166  {
167  std::auto_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
168  (*it)->fillHits(*product,*in);
169  e.put(product,*in);
170  }
171  }
172 
173  for(Producers::iterator itProd = m_producers.begin();
174  itProd != m_producers.end();
175  ++itProd) {
176  (*itProd)->produce(e,es);
177  }
178  }
179  catch ( const SimG4Exception& simg4ex )
180  {
181 
182  edm::LogInfo("SimG4CoreApplication") << " SimG4Exception caght !" << simg4ex.what() << std::endl ;
183 
184  m_runManager->abortEvent() ;
186  }
187 }
188 
189 
190 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
191 
193  if ( ! rng.isAvailable()) {
194  throw cms::Exception("Configuration")
195  << "The OscarProducer module requires the RandomNumberGeneratorService\n"
196  "which is not present in the configuration file. You must add the service\n"
197  "in the configuration file if you want to run OscarProducer";
198  }
199  m_currentEngine = &(rng->getEngine(streamID));
200 
201  m_previousEngine = CLHEP::HepRandom::getTheEngine();
202  CLHEP::HepRandom::setTheEngine(m_currentEngine);
203 }
204 
205 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine) {
206 
207  m_currentEngine = engine;
208 
209  m_previousEngine = CLHEP::HepRandom::getTheEngine();
210  CLHEP::HepRandom::setTheEngine(m_currentEngine);
211 }
212 
213 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() {
214  CLHEP::HepRandom::setTheEngine(m_previousEngine);
215 }
216 
217 CLHEP::HepRandomEngine*
218 StaticRandomEngineSetUnset::getEngine() const { return m_currentEngine; }
219 
Producers m_producers
Definition: OscarProducer.h:29
std::unique_ptr< RunManager > m_runManager
Definition: OscarProducer.h:28
std::vector< PCaloHit > PCaloHitContainer
OscarProducer(edm::ParameterSet const &p)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TRandom random
Definition: MVATrainer.cc:138
virtual ~OscarProducer()
virtual void beginRun(const edm::Run &r, const edm::EventSetup &c) override
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
static const std::string kGEANT
bool isAvailable() const
Definition: Service.h:46
virtual void produce(edm::Event &e, const edm::EventSetup &c) override
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
static const std::string kCLHEPRandomEngine
virtual const char * what() const
double p2[4]
Definition: TauolaWrapper.h:90
void load(edm::SimTrackContainer &c) const
Definition: G4SimEvent.cc:55
std::vector< SimVertex > SimVertexContainer
double p1[4]
Definition: TauolaWrapper.h:89
StreamID streamID() const
Definition: Event.h:75
std::vector< PSimHit > PSimHitContainer
std::vector< SimTrack > SimTrackContainer
std::unique_ptr< CustomUIsession > m_UIsession
Definition: OscarProducer.h:30
Definition: Run.h:41