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