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