| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

PartPropSvc.cpp
Go to the documentation of this file.
1 #ifdef __ICC
2 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
3 // TODO: should be removed because come from HepPDT
4 #pragma warning( disable : 1572 )
5 #endif
6
7 // Include files
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/MsgStream.h"
10 #include "GaudiKernel/PathResolver.h"
11
12 #include "PartPropSvc.h"
13
14 #include "HepPDT/HeavyIonUnknownID.hh"
15
16 #include <fstream>
17
18 #include <boost/algorithm/string/case_conv.hpp>
19 #include <boost/tokenizer.hpp>
20
21 //*************************************************************************//
22 StatusCode PartPropSvc::initialize()
23 {
24
25 StatusCode status = Service::initialize();
26
27 if ( status.isFailure() ) {
28 error() << "Could not initialize main svc" << endmsg;
29 return StatusCode::FAILURE;
30 }
31
32 std::string key = m_pdtFiles.value();
33
34 typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
35 boost::char_separator<char> sep( ", " );
36 boost::char_separator<char> sep_eq( "=" );
37
38 tokenizer tokens( key, sep );
39 for ( auto it = tokens.begin(); it != tokens.end(); ++it ) {
40
41 tokenizer tok2( *it, sep_eq );
42 int nToks( distance( tok2.begin(), tok2.end() ) );
43
44 auto it2 = tok2.begin();
45 const std::string fname = *it2;
46 std::string fmt;
47 if ( nToks == 1 ) {
48 info() << "No table format type specified for \"" << fname << "\". Assuming PDG" << endmsg;
49 fmt = "PDG";
50 } else {
51 ++it2;
52 fmt = *it2;
53 }
54
55 // see if input file exists in $DATAPATH
56 std::string rfile = System::PathResolver::find_file( fname, "DATAPATH" );
57 if ( rfile.empty() ) {
58 error() << "Could not find PDT file: \"" << fname << "\" in $DATAPATH" << endmsg;
59 return StatusCode::FAILURE;
60 }
61
62 // is the file readable?
63 std::ifstream pdfile{rfile};
64 if ( !pdfile ) {
65 error() << "Could not open PDT file: \"" << rfile << "\"" << endmsg;
66 return StatusCode::FAILURE;
67 }
68
69 std::string FMT = boost::algorithm::to_upper_copy( fmt );
70
71 inputFunPtr pF = nullptr;
72 try {
73 pF = parseTableType( FMT );
74 } catch ( ... ) {
75 error() << "Could not determine Particle Property table type: \"" << FMT << "\" for file \"" << fname << "\""
76 << endmsg;
77 return StatusCode::FAILURE;
78 }
79
80 debug() << "Adding PDT file \"" << rfile << "\" type " << FMT << endmsg;
81
82 m_inputs.emplace_back( rfile, pF );
83 }
84
85 return status;
86 }
87 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
88
89 StatusCode PartPropSvc::finalize()
90 {
91
92 m_pdt.reset();
93
94 if ( m_upid_local && m_upid ) {
95 m_upid_local = false;
96 // This will cause a memory leak, but we can't delete it as the
97 // destructor of HepPDT::processUnknownID is protected.
98 // We need this though to call reinitialize successfully.
99 m_upid = nullptr;
100 }
101
102 StatusCode status = Service::finalize();
103
104 if ( status.isSuccess() ) {
105 debug() << "Service finalised successfully" << endmsg;
106 }
107
108 return status;
109 }
110
111 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
112
113 PartPropSvc::inputFunPtr PartPropSvc::parseTableType( const std::string& typ )
114 {
115 static const auto table = {
116 std::make_pair( "PDG", &HepPDT::addPDGParticles ), std::make_pair( "PYTHIA", &HepPDT::addPythiaParticles ),
117 std::make_pair( "EVTGEN", &HepPDT::addEvtGenParticles ), std::make_pair( "HERWIG", &HepPDT::addHerwigParticles ),
118 std::make_pair( "ISAJET", &HepPDT::addIsajetParticles ), std::make_pair( "QQ", &HepPDT::addQQParticles )};
119 auto i = std::find_if( std::begin( table ), std::end( table ),
120 [&]( const std::pair<const char*, inputFunPtr>& p ) { return typ == p.first; } );
121 if ( i == std::end( table ) ) {
122 error() << "Unknown Particle Data file type: \"" << typ << "\"" << endmsg;
123 throw std::runtime_error( "error parsing particle table type" );
124 }
125 return i->second;
126 }
127
128 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
129
130 StatusCode PartPropSvc::createTable()
131 {
132
133 // use a handler for unknown heavy ions
134 if ( !m_upid ) {
135 setUnknownParticleHandler( new HepPDT::HeavyIonUnknownID, "Default Heavy Ion Handler" );
136 m_upid_local = true;
137 }
138
139 m_pdt.reset( new HepPDT::ParticleDataTable( m_upid_name, m_upid ) );
140
141 HepPDT::TableBuilder tb( *m_pdt );
142
143 for ( const auto& itr : m_inputs ) {
144 const auto& f = itr.first;
145 const auto& pF = itr.second;
146
147 debug() << "Reading PDT file \"" << f << "\"" << endmsg;
148
149 std::ifstream pdfile{f};
150 // build a table from the file
151 if ( !pF( pdfile, tb ) ) {
152 error() << "Error reading PDT file: \"" << f << "\"" << endmsg;
153 return StatusCode::FAILURE;
154 }
155 }
156
157 return StatusCode::SUCCESS;
158 }
159
160 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
161
162 HepPDT::ParticleDataTable* PartPropSvc::PDT()
163 {
164
165 if ( !m_pdt ) {
166 debug() << "creating ParticleDataTable" << endmsg;
167 if ( createTable().isFailure() ) {
168 fatal() << "Could not create ParticleDataTable" << endmsg;
169 m_pdt.reset( nullptr );
170 }
171 }
172
173 return m_pdt.get();
174 }
175
176 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
177
178 void PartPropSvc::setUnknownParticleHandler( HepPDT::ProcessUnknownID* puid, const std::string& n )
179 {
180 if ( m_pdt ) {
181 error() << "not setting Unknown Particle Handler \"" << n << "\" as ParticleDataTable already instantiated"
182 << endmsg;
183 return;
184 }
185
186 debug() << "setting Unknown Particle Handler \"" << n << "\" at " << puid << endmsg;
187
188 if ( m_upid ) {
189 warning() << "overriding previously selected Unknown Particle Handler \"" << m_upid_name << "\" with \"" << n
190 << "\"" << endmsg;
191 }
192
193 m_upid = puid;
194 m_upid_name = n;
195 }
196
197 // Instantiation of a static factory class used by clients to create
198 // instances of this service
199 DECLARE_COMPONENT( PartPropSvc )
200 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:64
StatusCode::SUCCESS
Definition: StatusCode.h:31
std::string::empty
T empty(T...args)
PartPropSvc::initialize
StatusCode initialize() override
Definition: PartPropSvc.cpp:22
StatusCode::FAILURE
Definition: StatusCode.h:31
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:174
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:112
PartPropSvc::parseTableType
inputFunPtr parseTableType(const std::string &)
Definition: PartPropSvc.cpp:113
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
PartPropSvc::inputFunPtr
bool(*)(std::istream &, HepPDT::TableBuilder &) inputFunPtr
Definition: PartPropSvc.h:45
PartPropSvc::m_pdt
std::unique_ptr< HepPDT::ParticleDataTable > m_pdt
Definition: PartPropSvc.h:55
std::end
T end(T...args)
PartPropSvc::m_pdtFiles
Gaudi::Property< std::string > m_pdtFiles
Definition: PartPropSvc.h:50
PartPropSvc::m_inputs
std::vector< std::pair< std::string, inputFunPtr > > m_inputs
Definition: PartPropSvc.h:48
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
PartPropSvc
Definition: PartPropSvc.py:1
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
PartPropSvc.h
PartPropSvc::finalize
StatusCode finalize() override
Definition: PartPropSvc.cpp:89
PartPropSvc::m_upid
HepPDT::ProcessUnknownID * m_upid
Definition: PartPropSvc.h:52
std::string
STL class.
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:106
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:109
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
std::runtime_error
STL class.
PartPropSvc::m_upid_local
bool m_upid_local
Definition: PartPropSvc.h:59
std::make_pair
T make_pair(T...args)
std::unique_ptr::reset
T reset(T...args)
System::PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cpp:116
std::pair
std::unique_ptr::get
T get(T...args)
std::find_if
T find_if(T...args)
PartPropSvc::PDT
HepPDT::ParticleDataTable * PDT() override
Definition: PartPropSvc.cpp:162
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:115
std::begin
T begin(T...args)
PartPropSvc::createTable
StatusCode createTable()
Definition: PartPropSvc.cpp:130
PathResolver.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:100
ISvcLocator.h
PartPropSvc::setUnknownParticleHandler
void setUnknownParticleHandler(HepPDT::ProcessUnknownID *, const std::string &) override
Definition: PartPropSvc.cpp:178
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
std::ifstream
STL class.
std::vector::emplace_back
T emplace_back(T...args)
PartPropSvc::m_upid_name
std::string m_upid_name
Definition: PartPropSvc.h:53