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