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/TableBuilder.hh"
15 #include "HepPDT/HeavyIonUnknownID.hh"
16 
17 //#include <iostream>
18 #include <cstdlib>
19 #include <fstream>
20 
21 #include <boost/regex.hpp>
22 
23 inline void toupper(std::string &s)
24 {
25  std::transform(s.begin(), s.end(), s.begin(),
26  (int(*)(int)) toupper);
27 }
28 
29 //*************************************************************************//
30 
31 PartPropSvc::PartPropSvc( const std::string& name, ISvcLocator* svc )
32  : base_class( name, svc ), m_upid(0), m_pdt(0), m_log(msgSvc(), name),
33  m_upid_local(false) {
34 
35  declareProperty( "InputFile", m_pdtFiles="PDGTABLE.MeV");
36 
37 }
38 
39 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
40 
42 }
43 
44 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
45 
48 
51 
52  if ( status.isFailure() ) {
53  m_log << MSG::ERROR << "Could not initialize main svc" << endmsg;
54  return StatusCode::FAILURE;
55  }
56 
57 
58  std::string key = m_pdtFiles.value();
59 
60  static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+)"};
61  static const auto tok_end = boost::sregex_iterator();
62  for (auto tok_iter = boost::sregex_iterator(begin(key), end(key), exp);
63  tok_iter != tok_end; ++tok_iter)
64  {
65  const std::string fname = (*tok_iter)[1];
66 
67  // see if input file exists in $DATAPATH
68  std::string rfile = System::PathResolver::find_file(fname,"DATAPATH");
69  if (rfile == "") {
70  m_log << MSG::ERROR << "Could not find PDT file: \"" << fname
71  << "\" in $DATAPATH" << endmsg;
72  return StatusCode::FAILURE;
73  }
74 
75 
76  // is the file readable?
77  std::ifstream pdfile( rfile.c_str() );
78  if (!pdfile) {
79  m_log << MSG::ERROR << "Could not open PDT file: \"" << rfile
80  << "\"" << endmsg;
81  return StatusCode::FAILURE;
82  }
83 
84  std::string val = (*tok_iter)[1];
85  std::string VAL = val;
86  toupper(VAL);
87 
88  // default: no type specified, assume PDG
89  if (val == fname) {
90  m_log << MSG::INFO << "No table format type specified for \"" << fname
91  << "\". Assuming PDG" << endmsg;
92  VAL = "PDG";
93  }
94 
95  bool (*pF) (std::istream &,
96  HepPDT::TableBuilder &);
97  try {
98  pF = parseTableType(VAL);
99  } catch (...) {
100  m_log << MSG::ERROR
101  << "Could not determine Particle Property table type: \""
102  << val << "\" for file \"" << fname << "\"" << endmsg;
103  return StatusCode::FAILURE;
104  }
105 
106  m_log << MSG::DEBUG << "Adding PDT file \"" << rfile << "\" type "
107  << VAL << endmsg;
108 
109  m_inputs.push_back( make_pair( rfile, pF ) );
110 
111  }
112 
113 
114  return status;
115 }
116 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
117 
120 
121  return StatusCode::SUCCESS;
122 
123 }
124 
125 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
126 
129 
130  if (m_pdt != 0) {
131  delete m_pdt;
132  m_pdt = 0;
133  }
134 
135  if (m_upid_local && m_upid != 0) {
136  m_upid_local = false;
137  // This will cause a memory leak, but we can't delete it as the
138  // destructor of HepPDT::processUnknownID is protected.
139  // We need this though to call reinitialize successfully.
140  m_upid = 0;
141  }
142 
143  MsgStream m_log( msgSvc(), name() );
144  StatusCode status = Service::finalize();
145 
146  if ( status.isSuccess() )
147  m_log << MSG::DEBUG << "Service finalised successfully" << endmsg;
148 
149  return status;
150 }
151 
152 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
153 
154 bool
155 (*PartPropSvc::parseTableType(std::string& typ))(std::istream&,
156  HepPDT::TableBuilder&) {
157 
158  bool (*pF) (std::istream &,
159  HepPDT::TableBuilder &);
160 
161  if (typ == "PDG") {
162  pF = &HepPDT::addPDGParticles;
163  } else if (typ == "PYTHIA") {
164  pF = &HepPDT::addPythiaParticles;
165  } else if (typ == "EVTGEN") {
166  pF = &HepPDT::addEvtGenParticles;
167  } else if (typ == "HERWIG") {
168  pF = &HepPDT::addHerwigParticles;
169  } else if (typ == "ISAJET") {
170  pF = &HepPDT::addIsajetParticles;
171  } else if (typ == "QQ") {
172  pF = &HepPDT::addQQParticles;
173  } else {
174  m_log << MSG::ERROR << "Unknown Particle Data file type: \""
175  << typ << "\"" << endmsg;
176  throw( std::runtime_error("error parsing particle table type") );
177  }
178 
179  return pF;
180 
181 }
182 
183 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
184 
187 
188  // use a handler for unknown heavy ions
189  if ( m_upid == 0 ) {
190  setUnknownParticleHandler(new HepPDT::HeavyIonUnknownID,
191  "Default Heavy Ion Handler");
192  m_upid_local = true;
193  }
194 
195  m_pdt = new HepPDT::ParticleDataTable(m_upid_name, m_upid);
196 
197  HepPDT::TableBuilder tb( *m_pdt );
198 
199  std::vector< std::pair<std::string,
200  bool(*) (std::istream&,HepPDT::TableBuilder&)> >::const_iterator itr;
201  for (itr = m_inputs.begin(); itr != m_inputs.end(); ++itr) {
202  std::string f = itr->first;
203  bool (*pF) (std::istream&,HepPDT::TableBuilder&) = itr->second;
204 
205  m_log << MSG::DEBUG << "Reading PDT file \"" << f << "\""
206  << endmsg;
207 
208  std::ifstream pdfile( f.c_str() );
209  // build a table from the file
210  if ( ! pF(pdfile,tb) ) {
211  m_log << MSG::ERROR << "Error reading PDT file: \"" << f
212  << "\"" << endmsg;
213  return StatusCode::FAILURE;
214  }
215 
216  }
217 
218  return StatusCode::SUCCESS;
219 
220 }
221 
222 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
223 
224 HepPDT::ParticleDataTable*
226 
227  if (m_pdt == 0) {
228  m_log << MSG::DEBUG << "creating ParticleDataTable" << endmsg;
229  if (createTable().isFailure()) {
230  m_log << MSG::FATAL << "Could not create ParticleDataTable" << endmsg;
231  m_pdt = 0;
232  }
233  }
234 
235  return m_pdt;
236 }
237 
238 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
239 
240 void
241 PartPropSvc::setUnknownParticleHandler(HepPDT::ProcessUnknownID* puid,
242  const std::string& n) {
243  if (m_pdt != 0) {
244  m_log << MSG::ERROR << "not setting Unknown Particle Handler \"" << n
245  << "\" as ParticleDataTable already instantiated" << endmsg;
246  return;
247  }
248 
249  m_log << MSG::DEBUG << "setting Unknown Particle Handler \"" << n
250  << "\" at " << puid << endmsg;
251 
252  if (m_upid != 0) {
254  << "overriding previously selected Unknown Particle Handler \""
255  << m_upid_name << "\" with \"" << n << "\"" << endmsg;
256  }
257 
258  m_upid = puid;
259  m_upid_name = n;
260 
261 }
262 
263 // Instantiation of a static factory class used by clients to create
264 // instances of this service
266 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
267 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:244
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: PartPropSvc.cpp:47
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
struct GAUDI_API vector
Parametrisation class for vector-like implementation.
return false
Definition: Bootstrap.cpp:338
MsgStream m_log
Definition: PartPropSvc.h:60
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
PartPropSvc(const std::string &name, ISvcLocator *svc)
Definition: PartPropSvc.cpp:31
HepPDT::ProcessUnknownID * m_upid
Definition: PartPropSvc.h:53
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
void toupper(std::string &s)
Definition: PartPropSvc.cpp:23
HepPDT::ParticleDataTable * PDT()
bool m_upid_local
Definition: PartPropSvc.h:62
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
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:355
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:329
tuple end
Definition: IOTest.py:101
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
HepPDT::ParticleDataTable * m_pdt
Definition: PartPropSvc.h:56
bool(*)(std::istream &, HepPDT::TableBuilder &) parseTableType(std::string &)
Definition: PartPropSvc.h:58
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
virtual ~PartPropSvc()
Definition: PartPropSvc.cpp:41
string s
Definition: gaudirun.py:244
Templated class to add the standard messaging functionalities.
void setUnknownParticleHandler(HepPDT::ProcessUnknownID *, const std::string &)
StatusCode createTable()
std::vector< std::pair< std::string, bool(*)(std::istream &, HepPDT::TableBuilder &)> > m_inputs
Definition: PartPropSvc.h:50
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
StringProperty m_pdtFiles
Definition: PartPropSvc.h:52
std::string m_upid_name
Definition: PartPropSvc.h:54