BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
GenModule.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------
2// File: GeneratorModule/GenModule.cxx
3// Description:
4// This class is the base class used to specify the behavior of all
5// event generator modules and is meant to capture the common behavior
6// of these modules.
7//
8// Header for this module:-
9
10#include <fstream>
11
13
14// Framework Related Headers:-
15
16#include "GaudiKernel/MsgStream.h"
17#include "GaudiKernel/ISvcLocator.h"
18#include "GaudiKernel/DataSvc.h"
19#include "GaudiKernel/SmartDataPtr.h"
20
21
22#include "GaudiKernel/IIncidentSvc.h"
23#include "GaudiKernel/Incident.h"
24
25#include "GaudiKernel/IPartPropSvc.h"
26
27// Other Packages used by this class:-
28#include "CLHEP/Random/Ranlux64Engine.h"
29#include "CLHEP/Random/RandPoisson.h"
30
31//#include "BesHepMC/GenVertex.h"
32#include "HepMC/GenVertex.h"
33
35
36
37//---------------------------------------------------------------------------
38GenModule::GenModule(const std::string& name, ISvcLocator* pSvcLocator)
39 : Algorithm(name, pSvcLocator),
40 m_pRandomEngine(0),
41 m_pPoissonGenerator(0)
42 //---------------------------------------------------------------------------
43{
44 declareProperty("FixedMode",m_fixedMode=true);
45 declareProperty("MeanInt",m_meanInteractions=1.0);
46 declareProperty("RandomSeed",m_randomSeed=1234567);
47 declareProperty("StripPartons",m_StripVector);
48 m_StripVector.push_back(0);
49
50}
51
52//---------------------------------------------------------------------------
54 // Delete random number objects if they were created
55
58
59}
60//---------------------------------------------------------------------------
61
62//---------------------------------------------------------------------------
64 //---------------------------------------------------------------------------
65
66 // Inform the user what the mode and conditions are:
67 MsgStream log(messageService(), name());
68
69 // Initialize StripPartons variables
70 if (m_StripVector[0] > 0) {
72 } else {
73 m_StripPartonSwitch = false;
74 m_StripVector.clear();
75 }
76
77
78 // Get the Particle Properties Service
79 IPartPropSvc* p_PartPropSvc;
80 //static const bool CREATEIFNOTTHERE(true);
81 //StatusCode PartPropStatus = service("PartPropSvc", p_PartPropSvc, CREATEIFNOTTHERE);
82 StatusCode PartPropStatus = service("PartPropSvc", p_PartPropSvc);
83 if (!PartPropStatus.isSuccess() || 0 == p_PartPropSvc) {
84 log << MSG::ERROR << " Could not initialize Particle Properties Service" << endreq;
85 return PartPropStatus;
86 }
87
88 m_particleTable = p_PartPropSvc->PDT();
89
90 m_pRandomEngine = new CLHEP::Ranlux64Engine(m_randomSeed);
91
92 if(m_fixedMode) {
93 if(m_meanInteractions == 1.0) {
94 log << MSG::INFO << "Standard Initialization: Single Interaction Mode "
95 << endreq;
96 }
97 else {
98 log << MSG::INFO << "Fixed Number of Interactions per Event is: "
99 << m_meanInteractions << endreq;
100 }
101 }
102 else {
103 m_pPoissonGenerator = new CLHEP::RandPoisson(*m_pRandomEngine,
105
106 log << MSG::INFO <<
107 "Poisson Distribution of Interactions per Event with Mean: "
108 << m_meanInteractions << endreq;
109 }
110 // Initialize the generator itself
111 StatusCode status = this->genInitialize();
112 if (status.isFailure()) {
113 log << MSG::ERROR << "Could not initialize Generator properly" << endreq;
114 return status;
115 }
116 StatusCode status1 = this->genuserInitialize();
117 if (status1.isFailure()) {
118 log << MSG::ERROR << "Could not initialize user part properly" << endreq;
119 return status1;
120 }
121 return status;
122}
123
124//---------------------------------------------------------------------------
125StatusCode GenModule::execute() {
126 //---------------------------------------------------------------------------
127
128 int numToGenerate;
129 StatusCode status;
130
131 MsgStream log(messageService(), name());
132
133 log << MSG::DEBUG << "GenModule::execute()" << endreq;
134
135 // Decide how many interactions to generate
136 if(m_fixedMode) {
137 numToGenerate = (int) m_meanInteractions;
138 }
139 else {
140 numToGenerate = m_pPoissonGenerator->fire();
141 }
142
143 // Generate as many interactions as requested
144 for(int i=0; i<numToGenerate; i++) {
145
146 // Call the code that generates an event
147 status = this->callGenerator();
148
149 // Create the MC event and send the GeneratorEvent stored in it to fillEvt
150 GenEvent* evt = new GenEvent(1,1);
151 status = this->fillEvt(evt);
152
153 // Strip out the requested partons here.
155
156 // Check if the McCollection already exists
157 SmartDataPtr<McGenEventCol> anMcCol(eventSvc(), "/Event/Gen");
158 if (anMcCol!=0) {
159 // Add event to existing collection
160 MsgStream log(messageService(), name());
161 log << MSG::INFO << "Add McGenEvent to existing collection" << endreq;
162 McGenEvent* mcEvent = new McGenEvent(evt);
163 anMcCol->push_back(mcEvent);
164 }
165 else {
166 // Create Collection and add to the transient store
167 McGenEventCol *mcColl = new McGenEventCol;
168 McGenEvent* mcEvent = new McGenEvent(evt);
169 mcColl->push_back(mcEvent);
170
171 StatusCode sc = eventSvc()->registerObject("/Event/Gen",mcColl);
172
173 if (sc != StatusCode::SUCCESS) {
174 MsgStream log(messageService(), name());
175 log << MSG::ERROR << "Could not register McGenEvent" << endreq;
176 delete mcColl;
177 delete evt;
178 delete mcEvent;
179 return StatusCode::FAILURE;
180 }
181 else {
182// std::cout << " McGenEventCol made and stored" << std::endl;
183 }
184
185 }
186 }
187
188 // now call the incident service and set the signal.
189 IIncidentSvc *incSvc;
190 service("IncidentSvc",incSvc);
191 incSvc->fireIncident( Incident( name(), "McGenEvent Generated") );
192
193 return status;
194}
195
196//---------------------------------------------------------------------------
198 //---------------------------------------------------------------------------
199
200 StatusCode status = this->genFinalize();
201
202 return status;
203}
204
205//---------------------------------------------------------------------------
207 //---------------------------------------------------------------------------
208 return StatusCode::SUCCESS;
209}
210
211//---------------------------------------------------------------------------
213 //---------------------------------------------------------------------------
214 return StatusCode::SUCCESS;
215}
216
217//---------------------------------------------------------------------------
219 //---------------------------------------------------------------------------
220 return StatusCode::SUCCESS;
221}
222
223//---------------------------------------------------------------------------
225 //---------------------------------------------------------------------------
226 return StatusCode::SUCCESS;
227}
228
229//---------------------------------------------------------------------------
230StatusCode GenModule::fillEvt(GenEvent* /* evt */) {
231 //---------------------------------------------------------------------------
232 return StatusCode::SUCCESS;
233}
234
235void
237{
238 MsgStream log(messageService(), name());
239
240 for (int i = 1; i < 9; ++i) { m_AllPartons.push_back(i); m_AllPartons.push_back(-i); } // Quarks
241 m_AllPartons.push_back(21); // gluon
242
243 m_StripPartonSwitch = true;
244 m_StripVector.erase(m_StripVector.begin(), m_StripVector.begin()+1);
245 // When the user specifies only the StripPartonSwitch or gives a Particle Code
246 // which is NOT a parton then strip ALL partons
247 if (m_StripVector.size() == 0) {
248 log << MSG::INFO << " !!!! NO SPECIFIC PARTON FOR STRIPOUT WAS REQUESTED => STRIPOUT ALL "
249 << endreq;
251 } else {
252 bool value_ok = true;
253 std::vector<int>::const_iterator i = m_StripVector.begin();
254 do {
255 if ( std::find(m_AllPartons.begin(),
256 m_AllPartons.end(),
257 *i) == m_AllPartons.end() ) value_ok = false;
258 ++i;
259 } while (i != m_StripVector.end() && value_ok);
260 if (!value_ok) {
261 log << MSG::INFO << " !!!! ILEGAL PDG-ID FOR STRIPOUT WAS REQUESTED => STRIPOUT ALL "
262 << endreq;
264 }
265 }
266 log << MSG::INFO << " THE FOLLOWING PARTON(S) WILL BE STRIPED OUT ";
267 for (std::vector<int>::const_iterator ip = m_StripVector.begin(); ip != m_StripVector.end(); ++ip)
268 log << *ip << " ";
269 log << endreq;
270
271}
272
273void
275{
276 // for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
277 // p != evt->particles_end(); ++p )
278 // {
279 // // std::cout << " PDG, BAR " << (*p)->pdg_id() << " " << (*p)->barcode() << std::endl;
280 // if ( std::find(m_StripVector.begin(),
281 // m_StripVector.end(),
282 // (*p)->pdg_id()) != m_StripVector.end() )
283 // {
284 // // std::cout << " REMOVING " << (*p)->pdg_id() << " " << (*p)->barcode() << std::endl;
285 // HepMC::GenVertex* pvtx = (*p)->production_vertex();
286 // HepMC::GenVertex* evtx = (*p)->end_vertex();
287 // if (pvtx) pvtx->remove_particle(*p);
288 // if (evtx) evtx->remove_particle(*p);
289 // delete *p;
290 // }
291
292 // }
293
294 // Loop on all vertices of the event and remove the particle from the vertex
295 for ( HepMC::GenEvent::vertex_iterator vtx = evt->vertices_begin();
296 vtx != evt->vertices_end(); ++vtx ) {
297 // Loop on all children particles and remove the ones that should be stripped out
298 for ( HepMC::GenVertex::particle_iterator p = (*vtx)-> particles_begin(HepMC::children);
299 p != (*vtx)->particles_end(HepMC::children);
300 ++p ){
301 if ( std::find(m_StripVector.begin(),
302 m_StripVector.end(),
303 (*p)->pdg_id()) != m_StripVector.end() ) {
304 if ( (*p)->end_vertex() ) (*p)->end_vertex()->remove_particle(*p);
305 if ( (*p)->production_vertex() ) (*p)->production_vertex()->remove_particle(*p);
306 delete *p;
307 }
308 }
309 // Loop on all parents particles and remove the ones that should be stripped out
310 for ( HepMC::GenVertex::particle_iterator p = (*vtx)-> particles_begin(HepMC::parents);
311 p != (*vtx)->particles_end(HepMC::parents);
312 ++p ) {
313 if ( std::find(m_StripVector.begin(),
314 m_StripVector.end(),
315 (*p)->pdg_id()) != m_StripVector.end() ) {
316 if ( (*p)->end_vertex() ) (*p)->end_vertex()->remove_particle(*p);
317 if ( (*p)->production_vertex() ) (*p)->production_vertex()->remove_particle(*p);
318 delete *p;
319 }
320 }
321 }
322}
323
ObjectVector< McGenEvent > McGenEventCol
Definition: McGenEvent.h:39
void StripPartons(GenEvent *evt)
Definition: GenModule.cxx:274
bool m_StripPartonSwitch
Definition: GenModule.h:62
CLHEP::RandPoisson * m_pPoissonGenerator
Definition: GenModule.h:68
std::vector< int > m_StripVector
Definition: GenModule.h:61
virtual StatusCode callGenerator()
Definition: GenModule.cxx:218
StatusCode finalize()
Definition: GenModule.cxx:197
virtual StatusCode genuserInitialize()
Definition: GenModule.cxx:212
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Definition: GenModule.cxx:38
CLHEP::HepRandomEngine * m_pRandomEngine
Definition: GenModule.h:67
StatusCode initialize()
Definition: GenModule.cxx:63
double m_meanInteractions
Definition: GenModule.h:58
virtual StatusCode genFinalize()
Definition: GenModule.cxx:224
StatusCode execute()
Definition: GenModule.cxx:125
int m_randomSeed
Definition: GenModule.h:59
void StripPartonsInit(void)
Definition: GenModule.cxx:236
bool m_fixedMode
Definition: GenModule.h:57
std::vector< int > m_AllPartons
Definition: GenModule.h:60
virtual StatusCode genInitialize()
Definition: GenModule.cxx:206
HepPDT::ParticleDataTable * m_particleTable
Definition: GenModule.h:73
virtual ~GenModule()
Definition: GenModule.cxx:53
virtual StatusCode fillEvt(GenEvent *evt)
Definition: GenModule.cxx:230