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