00001
00002
00003 #include <string>
00004 #include <vector>
00005 #include "boost/format.hpp"
00006
00007
00008 #include "GaudiKernel/SmartIF.h"
00009 #include "GaudiKernel/IProperty.h"
00010 #include "GaudiKernel/IRndmEngine.h"
00011 #include "GaudiKernel/IRndmGenSvc.h"
00012 #include "GaudiKernel/RndmGenerators.h"
00013 #include "GaudiAlg/IGenericTool.h"
00014
00015
00016 #include "Kernel/LbAppInit.h"
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 LbAppInit::LbAppInit( const std::string& name,
00031 ISvcLocator* pSvcLocator)
00032 : GaudiAlgorithm ( name , pSvcLocator ),
00033 m_engine(0),
00034 m_randSvc(0),
00035 m_eventCounter(0),
00036 m_eventMax(0),
00037 m_appName(""),
00038 m_appVersion("")
00039 {
00040 declareProperty( "SkipFactor", m_skipFactor = 0 );
00041 declareProperty( "SingleSeed", m_singleSeed = false );
00042 declareProperty( "PreloadGeometry", m_preload = false );
00043 declareProperty( "PrintFreq", m_printFreq = 1 );
00044 declareProperty( "PrintEventTime", m_printTime = false );
00045 }
00046
00047
00048
00049 LbAppInit::~LbAppInit() {};
00050
00051
00052
00053
00054 StatusCode LbAppInit::initialize() {
00055 StatusCode sc = GaudiAlgorithm::initialize();
00056 if ( sc.isFailure() ) return sc;
00057
00058 IService* appMgr = svc<IService>( "ApplicationMgr" );
00059 SmartIF<IProperty> propMgr( appMgr );
00060 std::string value ;
00061 sc = propMgr->getProperty( "EvtMax", value );
00062 if( sc.isFailure() ) {
00063 return Error( " Fatal error while retrieving Property EvtMax " );
00064
00065 } else {
00066 m_eventMax = std::atoi(value.c_str()) ;
00067 always()
00068 << "=================================================================="
00069 << endmsg;
00070 if( -1 == m_eventMax ) {
00071 always() << "Requested to process all events on input file(s)" << endmsg;
00072 }
00073 else {
00074 always() << "Requested to process " << m_eventMax << " events" << endmsg;
00075 }
00076 always()
00077 << "=================================================================="
00078 << endmsg;
00079 }
00080
00081 sc = propMgr->getProperty( "AppName", value );
00082 if( sc.isFailure() ) {
00083 return Error( " Fatal error while retrieving Property AppName " );
00084 } else {
00085 m_appName = value;
00086 }
00087
00088 sc = propMgr->getProperty( "AppVersion", value );
00089 if( sc.isFailure() ) {
00090 return Error( " Fatal error while retrieving Property AppVersion " );
00091 } else {
00092 m_appVersion = value;
00093 }
00094
00095 if( m_preload ) {
00096 IGenericTool* preloadTool = tool<IGenericTool>( "PreloadGeometryTool" );
00097 preloadTool->execute();
00098 }
00099
00100 m_condDBInfo = svc<ICondDBInfo>("CondDBCnvSvc", true );
00101
00102 return StatusCode::SUCCESS;
00103 };
00104
00105
00106
00107
00108 StatusCode LbAppInit::execute() {
00109
00110 debug() << "==> Execute" << endmsg;
00111 ++m_eventCounter;
00112
00113 return StatusCode::SUCCESS;
00114 };
00115
00116
00117
00118
00119 StatusCode LbAppInit::finalize() {
00120
00121 always()
00122 << "=================================================================="
00123 << endmsg;;
00124 always() << m_eventCounter << " events processed" << endmsg;
00125 always()
00126 << "=================================================================="
00127 << endmsg;
00128
00129 if( (-1 != m_eventMax) && (m_eventMax != m_eventCounter) ) {
00130 warning()
00131 << "Should have processed " << m_eventMax << " events" << endmsg;
00132 warning()
00133 << "=================================================================="
00134 << endmsg;
00135 }
00136
00137 return GaudiAlgorithm::finalize();
00138 }
00139
00140
00141 void LbAppInit::printEventRun( longlong event, int run,
00142 std::vector<long int>* seeds,
00143 Gaudi::Time time ) const
00144 {
00145 if ( this->okToPrint() )
00146 {
00147 info() << "Evt " << event << ", Run " << run;
00148 if( m_printTime ) info() << ", UTC time "
00149 << time.format(false,"%Y-%m-%d %H:%M:%S")
00150 << "." << time.nanoformat(6);
00151 info() << ", Nr. in job = " << m_eventCounter;
00152 if( 0 != seeds ) info() << " with seeds " << *seeds;
00153 info() << endmsg;
00154 }
00155 }
00156
00157
00158
00159 StatusCode LbAppInit::initRndm( std::vector<long int>& seeds )
00160 {
00161
00162 if( 0 == m_randSvc ) m_randSvc = svc<IRndmGenSvc>( "RndmGenSvc", true );
00163 if( 0 == m_engine )
00164 {
00165 m_engine = m_randSvc->engine();
00166 if( 0 == m_engine ) {
00167 return Error( "Random number engine not found!" );
00168 }
00169 }
00170
00171 StatusCode sc = m_engine->setSeeds( seeds );
00172 if( sc.isFailure() ) return Error( "Unable to set random number seeds", sc );
00173
00174
00175 if( 0 < m_skipFactor )
00176 {
00177 if ( this->okToPrint() )
00178 {
00179 info() << "Skipping " << m_skipFactor << " random numbers" << endmsg;
00180 }
00181 int shots = m_skipFactor;
00182 double sum = 0.;
00183 Rndm::Numbers gauss;
00184 gauss.initialize( m_randSvc , Rndm::Gauss(0.,1.0) );
00185 while( 0 < --shots ) { sum += gauss() * sum ; }
00186 }
00187
00188 debug() << "using seeds " << seeds << endmsg;
00189
00190 return StatusCode::SUCCESS;
00191 }
00192
00193
00194
00195
00196 std::vector<long int> LbAppInit::getSeeds( unsigned int seed1, ulonglong seed2 )
00197 {
00198
00199 std::vector<long int> seeds;
00200
00201
00202 int seed1a = seed1 & 0x7FFFFFFF;
00203
00204
00205 int seed2a = seed2 & 0x7FFFFFFF;
00206 int seed2b = (seed2 >> 32) & 0x7FFFFFFF;
00207
00208 if( !m_singleSeed ) {
00209 if( 0 != seed1a ) seeds.push_back( seed1a );
00210 if( 0 != seed2a ) seeds.push_back( seed2a );
00211 if( 0 != seed2b ) seeds.push_back( seed2b );
00212 }
00213 else {
00214 warning() << "Using only one 24 bit random number seed" << endmsg;
00215 }
00216
00217
00218
00219 std::string s = name() + boost::io::str( boost::format( "_%1%_%2%" )
00220 % boost::io::group( std::setfill('0'), std::hex, std::setw(8), seed1 )
00221 % boost::io::group( std::setfill('0'), std::hex, std::setw(16), seed2 ) );
00222
00223
00224 int hash = 0;
00225 for( std::string::const_iterator iC = s.begin(); s.end() != iC; ++iC ) {
00226 hash += *iC; hash += (hash << 10); hash ^= (hash >> 6);
00227 }
00228 hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
00229
00230
00231
00232
00233
00234 seeds.push_back( abs(hash) );
00235 seeds.push_back( 0 );
00236
00237 return seeds;
00238 };
00239
00240
00241
00242
00243 const std::vector<LHCb::CondDBNameTagPair> LbAppInit::condDBTags() {
00244 std::vector<LHCb::CondDBNameTagPair> tmp;
00245 m_condDBInfo->defaultTags(tmp);
00246 return tmp;
00247 };