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/tokenizer.hpp>
19 #include <boost/algorithm/string/case_conv.hpp>
20 
21 
22 
23 //*************************************************************************//
24 
26  : base_class( name_, svc )
27 {
28  declareProperty( "InputFile", m_pdtFiles="PDGTABLE.MeV");
29 }
30 
31 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
32 
35 
37 
38  if ( status.isFailure() ) {
39  error() << "Could not initialize main svc" << endmsg;
40  return StatusCode::FAILURE;
41  }
42 
43 
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 
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 
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),
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 
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*
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
233 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
StatusCode initialize() override
Definition: Service.cpp:68
T empty(T...args)
StatusCode initialize() override
Definition: PartPropSvc.cpp:34
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:193
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
inputFunPtr parseTableType(const std::string &)
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
std::unique_ptr< HepPDT::ParticleDataTable > m_pdt
Definition: PartPropSvc.h:57
T end(T...args)
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
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode finalize() override
HepPDT::ProcessUnknownID * m_upid
Definition: PartPropSvc.h:54
STL class.
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:26
bool(*)(std::istream &, HepPDT::TableBuilder &) inputFunPtr
Definition: PartPropSvc.h:48
bool m_upid_local
Definition: PartPropSvc.h:61
T make_pair(T...args)
T reset(T...args)
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
T get(T...args)
T find_if(T...args)
HepPDT::ParticleDataTable * PDT() override
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T...args)
StatusCode createTable()
std::vector< std::pair< std::string, inputFunPtr > > m_inputs
Definition: PartPropSvc.h:51
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:215
list i
Definition: ana.py:128
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:244
STL class.
StringProperty m_pdtFiles
Definition: PartPropSvc.h:53
T emplace_back(T...args)
std::string m_upid_name
Definition: PartPropSvc.h:55