Gaudi Framework, version v21r11

Home   Generated: 30 Sep 2010

PartPropSvc.cpp

Go to the documentation of this file.
00001 #ifdef __ICC
00002 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
00003 //  TODO: should be removed because come from HepPDT
00004 #pragma warning(disable:1572)
00005 #endif
00006 
00007 //Include files
00008 #include "GaudiKernel/SvcFactory.h"
00009 #include "GaudiKernel/ISvcLocator.h"
00010 #include "GaudiKernel/MsgStream.h"
00011 #include "GaudiKernel/PathResolver.h"
00012 #include "GaudiKernel/Tokenizer.h"
00013 
00014 #include "PartPropSvc.h"
00015 
00016 //#include "HepPDT/TableBuilder.hh"
00017 #include "HepPDT/HeavyIonUnknownID.hh"
00018 
00019 //#include <iostream>
00020 #include <cstdlib>
00021 #include <fstream>
00022 
00023 using namespace std;
00024 
00025 // Instantiation of a static factory class used by clients to create
00026 //  instances of this service
00027 DECLARE_SERVICE_FACTORY(PartPropSvc);
00028 
00029 inline void toupper(std::string &s)
00030 {
00031     std::string::iterator it=s.begin();
00032     while(it != s.end())
00033     {
00034         *it = toupper(*it);
00035         it++;
00036     }
00037 }
00038 
00039 
00040 
00041 //*************************************************************************//
00042 
00043 PartPropSvc::PartPropSvc( const std::string& name, ISvcLocator* svc )
00044   : base_class( name, svc ),  m_upid(0), m_pdt(0), m_log(msgSvc(), name),
00045     m_upid_local(false) {
00046 
00047   declareProperty( "InputFile", m_pdtFiles="PDGTABLE.MeV");
00048 
00049 }
00050 
00051 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00052 
00053 PartPropSvc::~PartPropSvc() {
00054 }
00055 
00056 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00057 
00058 StatusCode
00059 PartPropSvc::initialize() {
00060 
00061   std::vector<std::string>::const_iterator itr;
00062 
00063   StatusCode status = Service::initialize();
00064   m_log.setLevel( m_outputLevel.value() );
00065 
00066   if ( status.isFailure() ) {
00067     m_log << MSG::ERROR << "Could not initialize main svc" << endmsg;
00068     return StatusCode::FAILURE;
00069   }
00070 
00071 
00072   std::string key = m_pdtFiles.value();
00073 
00074   Tokenizer tok(true);
00075 
00076   tok.analyse( key, " ", "", "", "=", "", "");
00077 
00078   for ( Tokenizer::Items::iterator i = tok.items().begin();
00079         i != tok.items().end(); i++)    {
00080     const std::string& fname = (*i).tag();
00081 
00082     // see if input file exists in $DATAPATH
00083     std::string rfile = System::PathResolver::find_file(fname,"DATAPATH");
00084     if (rfile == "") {
00085       m_log << MSG::ERROR << "Could not find PDT file: \"" << *itr
00086             << "\" in $DATAPATH" << endmsg;
00087       return StatusCode::FAILURE;
00088     }
00089 
00090 
00091     // is the file readable?
00092     std::ifstream pdfile( rfile.c_str() );
00093     if (!pdfile) {
00094       m_log << MSG::ERROR << "Could not open PDT file: \"" << rfile
00095             << "\"" << endmsg;
00096       return StatusCode::FAILURE;
00097     }
00098 
00099     std::string val,VAL;
00100     val = (*i).value();
00101     VAL = val;
00102     toupper(VAL);
00103 
00104     // default: no type specified, assume PDG
00105     if (val == fname) {
00106       m_log << MSG::INFO << "No table format type specified for \"" << fname
00107             << "\". Assuming PDG" << endmsg;
00108       VAL = "PDG";
00109     }
00110 
00111     bool (*pF)  (std::istream &,
00112                  HepPDT::TableBuilder &);
00113     try {
00114       pF = parseTableType(VAL);
00115     } catch (...) {
00116       m_log << MSG::ERROR
00117             << "Could not determine Particle Property table type: \""
00118             << val << "\" for file \"" << fname << "\"" << endmsg;
00119       return StatusCode::FAILURE;
00120     }
00121 
00122     m_log << MSG::DEBUG << "Adding PDT file \"" << rfile << "\" type "
00123           << VAL << endmsg;
00124 
00125     m_inputs.push_back( make_pair<std::string, bool(*) (std::istream&,HepPDT::TableBuilder&)>( rfile, pF ) );
00126 
00127   }
00128 
00129 
00130   return status;
00131 }
00132 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00133 
00134 StatusCode
00135 PartPropSvc::reinitialize() {
00136 
00137   return StatusCode::SUCCESS;
00138 
00139 }
00140 
00141 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00142 
00143 StatusCode
00144 PartPropSvc::finalize() {
00145 
00146   if (m_pdt != 0) {
00147     delete m_pdt;
00148     m_pdt = 0;
00149   }
00150 
00151   if (m_upid_local && m_upid != 0) {
00152     m_upid_local = false;
00153     // This will cause a memory leak, but we can't delete it as the
00154     // destructor of HepPDT::processUnknownID is protected.
00155     // We need this though to call reinitialize successfully.
00156     m_upid = 0;
00157   }
00158 
00159   MsgStream m_log( msgSvc(), name() );
00160   StatusCode status = Service::finalize();
00161 
00162   if ( status.isSuccess() )
00163     m_log << MSG::DEBUG << "Service finalised successfully" << endmsg;
00164 
00165   return status;
00166 }
00167 
00168 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00169 
00170 bool
00171 (*PartPropSvc::parseTableType(std::string& typ))(std::istream&,
00172                                                  HepPDT::TableBuilder&) {
00173 
00174   bool (*pF)  (std::istream &,
00175                HepPDT::TableBuilder &);
00176 
00177   if (typ == "PDG") {
00178     pF = &HepPDT::addPDGParticles;
00179   } else if (typ == "PYTHIA") {
00180     pF = &HepPDT::addPythiaParticles;
00181   } else if (typ == "EVTGEN") {
00182     pF = &HepPDT::addEvtGenParticles;
00183   } else if (typ == "HERWIG") {
00184     pF = &HepPDT::addHerwigParticles;
00185   } else if (typ == "ISAJET") {
00186     pF = &HepPDT::addIsajetParticles;
00187   } else if (typ == "QQ") {
00188     pF = &HepPDT::addQQParticles;
00189   } else {
00190     m_log << MSG::ERROR << "Unknown Particle Data file type: \""
00191         << typ << "\"" << endmsg;
00192      throw( std::runtime_error("error parsing particle table type") );
00193   }
00194 
00195   return pF;
00196 
00197 }
00198 
00199 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00200 
00201 StatusCode
00202 PartPropSvc::createTable() {
00203 
00204   // use a handler for unknown heavy ions
00205   if ( m_upid == 0 ) {
00206     setUnknownParticleHandler(new HepPDT::HeavyIonUnknownID,
00207                               "Default Heavy Ion Handler");
00208     m_upid_local = true;
00209   }
00210 
00211   m_pdt = new HepPDT::ParticleDataTable(m_upid_name, m_upid);
00212 
00213   HepPDT::TableBuilder  tb( *m_pdt );
00214 
00215   std::vector< std::pair<std::string,
00216     bool(*) (std::istream&,HepPDT::TableBuilder&)> >::const_iterator itr;
00217   for (itr = m_inputs.begin(); itr != m_inputs.end(); ++itr) {
00218     string f = itr->first;
00219     bool (*pF) (std::istream&,HepPDT::TableBuilder&) = itr->second;
00220 
00221     m_log << MSG::DEBUG << "Reading PDT file \"" << f << "\""
00222           << endmsg;
00223 
00224     std::ifstream pdfile( f.c_str() );
00225     // build a table from the file
00226     if ( ! pF(pdfile,tb) ) {
00227       m_log << MSG::ERROR << "Error reading PDT file: \"" << f
00228             << "\"" << endmsg;
00229       return StatusCode::FAILURE;
00230     }
00231 
00232   }
00233 
00234   return StatusCode::SUCCESS;
00235 
00236 }
00237 
00238 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00239 
00240 HepPDT::ParticleDataTable*
00241 PartPropSvc::PDT() {
00242 
00243   if (m_pdt == 0) {
00244     m_log << MSG::DEBUG << "creating ParticleDataTable" << endmsg;
00245     if (createTable().isFailure()) {
00246       m_log << MSG::FATAL << "Could not create ParticleDataTable" << endmsg;
00247       m_pdt = 0;
00248     }
00249   }
00250 
00251   return m_pdt;
00252 }
00253 
00254 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00255 
00256 void
00257 PartPropSvc::setUnknownParticleHandler(HepPDT::ProcessUnknownID* puid,
00258                                        const std::string& n) {
00259   if (m_pdt != 0) {
00260     m_log << MSG::ERROR << "not setting Unknown Particle Handler \"" << n
00261           << "\" as ParticleDataTable already instantiated" << endmsg;
00262     return;
00263   }
00264 
00265   m_log << MSG::DEBUG << "setting Unknown Particle Handler \"" << n
00266         << "\" at " << puid << endmsg;
00267 
00268   if (m_upid != 0) {
00269     m_log << MSG::WARNING
00270           << "overriding previously selected Unknown Particle Handler \""
00271           << m_upid_name << "\" with \"" << n << "\"" << endmsg;
00272   }
00273 
00274   m_upid = puid;
00275   m_upid_name = n;
00276 
00277 }
00278 
00279 
00280 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
00281 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//

Generated at Thu Sep 30 09:57:40 2010 for Gaudi Framework, version v21r11 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004