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