BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
BesRndmGenSvc.cxx
Go to the documentation of this file.
2
3#ifndef HAVE_NEW_IOSTREAMS /*gnu-specific*/
4#define BOOST_NO_STRINGSTREAM 1 /*FIXME should come from boost config */
5#endif
6#include <boost/lexical_cast.hpp>
7#include <boost/tokenizer.hpp>
8
9#include "GaudiKernel/MsgStream.h"
10#include "GaudiKernel/ISvcLocator.h"
11#include "GaudiKernel/IIncidentSvc.h"
12#include "GaudiKernel/Incident.h"
13using namespace std;
14
15bool interpretSeeds(const string& buffer,
16 string& stream, std::vector<unsigned long>& seed) {
17 bool status(false);
18 seed.clear();
19 //split the space-separated string in 3 words:
20 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
21 boost::char_separator<char> sep(" ");
22 tokenizer tokens(buffer, sep);
23 if (status = (distance(tokens.begin(), tokens.end()) == 32)) {
24 tokenizer::iterator token(tokens.begin());
25 stream = *token++;
26 try {
27 for(int i=0; i<31; i++) {
28 long tmp = boost::lexical_cast<long>(*token++);
29 seed.push_back(tmp);
30 }
31 } catch (boost::bad_lexical_cast e) {
32 status = false;
33 }
34 }
35 return status;
36}
37
38/// Standard Constructor
39BesRndmGenSvc::BesRndmGenSvc(const std::string& name,ISvcLocator* svc)
40 : Service(name,svc)
41{
42 // Property Default values
43 m_read_from_file = false;
44 m_file_to_read = "BesRndmGenSvc.out";
45 ofile = "BesRndmGenSvc.out";
46
47 // Set Default values
48 m_default_seed = 11000;
49 m_EVTGEN_default_seed = 35910;
50 m_PYTHIA_default_seed = 93531;
51 m_HERWIG_default_seed = 35391;
52 m_LUNDCRM_default_seed = 12456;
53 m_SIM_default_seed = 23569;
54 m_MIX_default_seed = 76543;
55 m_KKMC_default_seed = 26877;
56 // Get user's input
57 declareProperty("Seeds", m_streams_seeds);
58 declareProperty("StreamName", m_StreamName = "EVTGEN");
59 declareProperty("ReadFromFile", m_read_from_file);
60 declareProperty("FileToRead", m_file_to_read);
61 declareProperty("RdmOutFile", ofile);
62 declareProperty("RndmSeed", m_default_seed);
63
64 Stream_EVTGEN = "EVTGEN";
65 Stream_PYTHIA = "PYTHIA";
66 Stream_HERWIG = "HERWIG";
67 Stream_LUNDCRM = "LUNDCRM";
68 Stream_SIM = "SIM";
69 Stream_MIX = "MIX";
70 Stream_KKMC = "KKMC";
71
72 m_engines = new engineMap();
73}
74
75
76/// Standard Destructor
78{
79 delete m_engines;
80}
81
82// Query the interfaces.
83// Input: riid, Requested interface ID
84// ppvInterface, Pointer to requested interface
85// Return: StatusCode indicating SUCCESS or FAILURE.
86// N.B. Don't forget to release the interface after use!!!
87StatusCode
88BesRndmGenSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
89{
90 if ( IBesRndmGenSvc::interfaceID().versionMatch(riid) ) {
91 *ppvInterface = (IBesRndmGenSvc*)this;
92 }
93 else {
94 // Interface is not directly available: try out a base class
95 return Service::queryInterface(riid, ppvInterface);
96 }
97 addRef();
98 return StatusCode::SUCCESS;
99}
100
101StatusCode
103{
104 MsgStream log( messageService(), name() );
105 log << MSG::INFO << "Initializing " << name()
106 << " - package version " << PACKAGE_VERSION
107 << "\n INITIALISING RANDOM NUMBER STREAMS. " << endreq;
108
109
110 if (!(Service::initialize().isSuccess())) {
111 return StatusCode::FAILURE;
112 }
113
114 //set up different stream seeds by default seed
115 m_EVTGEN_default_seed = m_default_seed+35910;
116 m_PYTHIA_default_seed = m_default_seed+93531;
117 m_HERWIG_default_seed = m_default_seed+35391;
118 m_LUNDCRM_default_seed = m_default_seed+12456;
119 m_SIM_default_seed = m_default_seed+23569;
120 m_MIX_default_seed = m_default_seed+76543;
121 m_KKMC_default_seed = m_default_seed+26877;
122
123 /// Incident Service
124 IIncidentSvc* pIncSvc(0);
125
126 // set up the incident service:
127 if (!(service("IncidentSvc", pIncSvc, true)).isSuccess()) {
128 log << MSG::ERROR
129 << "Could not locate IncidentSvc "
130 << endreq;
131 return StatusCode::FAILURE;
132 }
133
134 //start listening to "EndEvent"
135 static const int PRIORITY = 100;
136 pIncSvc->addListener(this, "EndEvent", PRIORITY);
137
138 if (m_read_from_file)
139 {
140 // Read from a file
141 ifstream infile( m_file_to_read.c_str() );
142 if ( !infile )
143 {
144 log << MSG::ERROR << " Unable to open: " << m_file_to_read <<endreq;
145 return StatusCode::FAILURE;
146 }
147 else
148 {
149 std::string buffer;
150 while (std::getline(infile, buffer))
151 {
152 string stream;
153 std::vector<unsigned long> v;
154 //split the space-separated string in 3 words:
155 if (interpretSeeds(buffer, stream, v)) {
156 log << MSG::DEBUG << " INITIALISING " << stream << " stream, "<< " read from file " <<m_file_to_read << endreq;
157 CLHEP::HepRandomEngine* engine = GetEngine(stream);
158 engine->get(v);
159 } else {
160 log << MSG::ERROR << "bad line\n" << buffer
161 << "\n in input file " << m_file_to_read << endreq;
162 return StatusCode::FAILURE;
163 }
164 }
165
166 }
167 }
168
169 // Create the various streams according to user's request
170 for (VStrings::const_iterator i = m_streams_seeds.begin(); i != m_streams_seeds.end(); ++i)
171 {
172 string stream;
173 std::vector<unsigned long> v;
174 //split the space-separated string in 3 words:
175 if (interpretSeeds(*i, stream, v)) {
176 log << MSG::VERBOSE << "Seeds property: stream " << stream << endreq;
177 } else {
178 log << MSG::ERROR << "bad Seeds property\n" << *i << endreq;
179 return StatusCode::FAILURE;
180 }
181
182 // Check if stream already generated (e.g. from reading a file)
183 bool not_found = true;
184 if ( number_of_streams() != 0 )
185 {
187 do
188 {
189 if ((*sf).first == stream) not_found = false;
190 ++sf;
191 } while (sf != end() && not_found);
192 }
193
194 if (not_found)
195 {
196 log << MSG::DEBUG << " INITIALISING " << stream << endreq;
197 CreateStream(v[0], stream);
198 }
199
200 }
201
202 return StatusCode::SUCCESS;
203}
204
205void
206BesRndmGenSvc::handle(const Incident &inc) {
207 MsgStream log( messageService(), name() );
208 log << MSG::DEBUG << " Handle EndEvent " << endreq;
209
210 if ( inc.type() == "EndEvent")
211 {
212
213 m_engines_copy.clear();
214 for (engineConstIter i = begin(); i != end(); ++i)
215 {
216 CLHEP::HepRandomEngine* engine = GetEngine((*i).first);
217 std::vector<unsigned long> s = engine->put();
218 std::vector<long int> tseeds;
219 for(unsigned int j=0; j<s.size(); j++) {
220 tseeds.push_back(s[j]);
221 }
222 m_engines_copy.insert(
223 std::map<std::string, std::vector<long int> >::value_type( (*i).first,
224 tseeds ) );
225 }
226 // Write the status of the Service into a file
227/*
228 ofstream outfile( ofile.c_str() );
229 if ( !outfile )
230 {
231 log <<MSG::ERROR << "error: unable to open: " << ofile << endreq;
232 }
233 else
234 {
235 for (std::map<std::string, std::vector<long int> >::const_iterator i = m_engines_copy.begin();
236 i != m_engines_copy.end();
237 ++i)
238 {
239 outfile << (*i).first;
240 for(unsigned int j=0; j<((*i).second).size(); j++)
241 outfile << " " << (*i).second[j];
242 outfile <<"\n";
243 }
244 }
245*/
246// print();
247 }
248}
249
250StatusCode
252{
253 MsgStream log(messageService(), name());
254 log << MSG::INFO << " FINALISING " << endreq;
255 return Service::finalize();
256}
257
258CLHEP::HepRandomEngine*
259BesRndmGenSvc::GetEngine ( const std::string& StreamName )
260{
261 engineConstIter citer = m_engines->find(StreamName);
262 if ( citer == m_engines->end() )
263 {
264 m_engines->insert( engineValType( StreamName, new CLHEP::RanluxEngine() ) );
265 SetStreamSeeds ( StreamName );
266 }
267
268 engineIter iter = m_engines->find(StreamName);
269 return (CLHEP::HepRandomEngine*)(*iter).second;
270}
271
272void
273BesRndmGenSvc::CreateStream ( long seed, const std::string& StreamName )
274{
275 engineConstIter citer = m_engines->find(StreamName);
276 if ( citer == m_engines->end() ) m_engines->insert( engineValType( StreamName,
277 new CLHEP::RanluxEngine() ) );
278 engineIter iter = m_engines->find(StreamName);
279 ((*iter).second)->setSeed( seed, 3 );
280}
281
282void
283BesRndmGenSvc::SetStreamSeeds ( const std::string& StreamName )
284{
285 long theseed;
286 if (StreamName == "PYTHIA")
287 {
288 theseed = m_PYTHIA_default_seed;
289 }
290 else if (StreamName == "HERWIG")
291 {
292 theseed = m_HERWIG_default_seed;
293 }
294 else if (StreamName == "EVTGEN")
295 {
296 theseed = m_EVTGEN_default_seed;
297 }
298 else if (StreamName == "LUNDCRM")
299 {
300 theseed = m_LUNDCRM_default_seed;
301 }
302 else if (StreamName == "SIM")
303 {
304 theseed = m_SIM_default_seed;
305 }
306 else if (StreamName == "MIX")
307 {
308 theseed = m_MIX_default_seed;
309 }
310 else if (StreamName == "KKMC")
311 {
312 theseed = m_KKMC_default_seed;
313 }
314 else
315 {
316 theseed = m_default_seed;
317 }
318 MsgStream log(messageService(), name());
319 log << MSG::WARNING << " INITIALISING " << StreamName << " stream with DEFAULT seed "
320 << theseed << endreq;
321
322 engineIter iter = m_engines->find(StreamName);
323 ((*iter).second)->setSeed( theseed, 3);
324}
325
326void
327BesRndmGenSvc::print ( const std::string& StreamName )
328{
329 MsgStream log(messageService(), name());
330 engineConstIter citer = m_engines->find(StreamName);
331 if ( citer == m_engines->end() )
332 {
333 log << MSG::WARNING << " Stream = " << StreamName << " NOT FOUND" << endreq;
334 }
335 else
336 {
337 const long s = ((*citer).second)->getSeed();
338 log << MSG::INFO << " Stream = " << StreamName << ", Seed = "
339 << s << endreq;
340 }
341}
342
343void
345{
346 print( m_StreamName );
347}
348
349CLHEP::HepRandomEngine*
350BesRndmGenSvc::setOnDefinedSeeds (int EventNumber, const std::string& StreamName )
351{
352 engineConstIter citer = m_engines->find(StreamName);
353 if ( citer == m_engines->end() ) m_engines->insert( engineValType( StreamName,
354 new CLHEP::RanluxEngine() ) );
355 engineIter iter = m_engines->find(StreamName);
356 long hashedStream;
357 // (SG::simpleStringHash(StreamName));
358 long theseed = 10000*EventNumber + hashedStream;
359 ((*iter).second)->setSeed( theseed, 3 );
360 return (CLHEP::HepRandomEngine*)(*iter).second;
361}
362
bool interpretSeeds(const string &buffer, string &stream, std::vector< unsigned long > &seed)
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
XmlRpcServer s
Definition: HelloServer.cpp:11
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
StatusCode initialize()
Gaudi Service Implementation.
BesRndmGenSvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
engineConstIter begin(void) const
void print(void)
CLHEP::HepRandomEngine * GetEngine(const std::string &StreamName)
Interface to the CLHEP engine.
void handle(const Incident &)
IIncidentListener implementation. Handles EndEvent incident.
void CreateStream(long seed, const std::string &StreamName)
StatusCode finalize()
std::map< std::string, CLHEP::RanluxEngine * > engineMap
CLHEP engines typedefs:
Definition: BesRndmGenSvc.h:47
engineConstIter end(void) const
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
engineMap::const_iterator engineConstIter
Definition: BesRndmGenSvc.h:49
CLHEP::HepRandomEngine * setOnDefinedSeeds(int EventNumber, const std::string &StreamName)
set the seeds for an engine. First param will usually be the event number
virtual ~BesRndmGenSvc()
Standard Destructor.
engineMap::value_type engineValType
Definition: BesRndmGenSvc.h:50
unsigned int number_of_streams(void) const
engineMap::iterator engineIter
Definition: BesRndmGenSvc.h:48
manage multiple CLHEP random engines as named streams
static const InterfaceID & interfaceID()
Gaudi boilerplate.