| The Gaudi Framework v28r2-patches (bc4ad17a)

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