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