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