| The Gaudi Framework v27r1

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