| # Gaudi Framework, version v23r1 |

Home |  | Generated: Wed Feb 29 2012

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines