The Gaudi Framework  v30r3 (a5ef0a68)
ParticlePropertySvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // STD&STL
5 // ============================================================================
6 #include "boost/algorithm/string/classification.hpp"
7 #include "boost/algorithm/string/split.hpp"
8 #include "boost/algorithm/string/trim.hpp"
9 #include <cctype>
10 #include <fstream>
11 namespace ba = boost::algorithm;
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
17 #include "GaudiKernel/MsgStream.h"
20 #include "GaudiKernel/System.h"
21 // ============================================================================
22 //#include "GaudiKernel/ToStream.h"
23 // ============================================================================
24 // Local
25 // ============================================================================
26 #include "ParticlePropertySvc.h"
27 // ============================================================================
28 namespace Gaudi
29 {
33  DECLARE_COMPONENT( ParticlePropertySvc )
34  // ============================================================================
46  // ============================================================================
47  ParticlePropertySvc::ParticlePropertySvc( const std::string& name, ISvcLocator* svc ) : base_class( name, svc )
48  {
50  // Redefine the default name:
51  if ( System::getEnv( "PARAMFILESROOT", m_filename.value() ) ) {
52  m_filename.value() += "/data/ParticleTable.txt";
53  }
54  }
55  // ============================================================================
57  // ============================================================================
59  {
61  if ( sc.isFailure() ) {
62  return sc;
63  }
64 
65  sc = setProperties();
66  if ( sc.isFailure() ) {
67  error() << " Could not set the properties " << endmsg;
68  return sc;
69  }
70 
71  m_fileAccess = service( "VFSSvc" );
72  if ( !m_fileAccess ) {
73  error() << " Cannot retrieve the VFS service " << endmsg;
74  return StatusCode::FAILURE;
75  }
76 
77  sc = parse();
78  if ( sc.isFailure() ) {
79  error() << " Could not parse the file " << endmsg;
80  return sc;
81  }
82  if ( !m_particles.empty() ) {
83  sc = addParticles();
84  if ( sc.isFailure() ) {
85  error() << " Could not treat particles! " << endmsg;
86  return sc;
87  }
88  }
89  debug() << "ParticleProperties parsed successfully" << endmsg;
90 
91  debug() << "Access properties" << endmsg;
92  // For debugging purposes print out the size of the internal maps
93  // particle name as key: all particles in database are present here
94  debug() << "NameMap size =" << m_namemap.size() << endmsg;
95  // Geant3 ID as key: all particles in database are present here
96  debug() << "GeantID Map size =" << m_idmap.size() << endmsg;
97  // StdHep ID as key: some particles have no valid StdHep ID
98  debug() << "StdHepID Map size =" << m_stdhepidmap.size() << endmsg;
99  // Pythia ID as key: some particles are not defined in Pythia
100  debug() << "PythiaID Map size =" << m_pythiaidmap.size() << endmsg;
101 
102  if ( !m_replaced.empty() ) {
103  info() << "Properties have been redefined for " << m_replaced.size()
104  << " particles : " << Gaudi::Utils::toString( m_replaced ) << endmsg;
105  }
106 
107  return StatusCode::SUCCESS;
108  }
109  // =============================================================================
111  // =============================================================================
113  {
114  if ( !m_other.empty() ) {
115  info() << "Additional Properties have been read from files: " << Gaudi::Utils::toString( m_other ) << endmsg;
116  }
117 
118  if ( !m_replaced.empty() ) {
119  always() << "Properties have been redefined for " << m_replaced.size()
120  << " particles : " << Gaudi::Utils::toString( m_replaced ) << endmsg;
121  }
122 
124 
126  return Service::finalize();
127  }
128  // =============================================================================
130  // =============================================================================
131  StatusCode ParticlePropertySvc::push_back( const std::string& particle, int geantId, int jetsetId, double charge,
132  double mass, double tlife, const std::string& evtName, int pythiaId,
133  double maxWidth )
134  {
135  //
136  auto i = m_owned.insert( std::make_unique<ParticleProperty>( particle, geantId, jetsetId, charge, mass, tlife,
137  evtName, pythiaId, maxWidth ) );
138  //
139  return i.second ? push_back( i.first->get() ) : StatusCode::FAILURE;
140  }
141  // =============================================================================
143  // =============================================================================
145  {
146  if ( !pp ) {
147  return StatusCode::FAILURE;
148  }
149  //
150  { // try to add into Geant(3)ID map
151  const int ID = pp->geantID();
152  // is this already in the map?
153  auto ifind = m_idmap.find( ID );
154  if ( m_idmap.end() != ifind && 0 != m_idmap[ID] ) {
155  diff( ifind->second, pp );
156  m_replaced.insert( m_idmap[ID]->particle() );
157  }
158  // put it into the map
159  m_idmap[ID] = pp;
160  }
161  //
162  { // try to add into Name map
163  const std::string& particle = pp->particle();
164  // is this already in the map?
165  auto ifind = m_namemap.find( particle );
166  if ( ifind != m_namemap.end() && ifind->second ) {
167  diff( ifind->second, pp );
168  m_replaced.insert( ifind->second->particle() );
169  }
170  // put it into the map
171  m_namemap[particle] = pp;
172  }
173  //
174  // add to StdHep map only if StdHep ID different from zero and if
175  // not Cerenkov (StdHep ID = gamma)
176  if ( 0 != pp->jetsetID() && "Tcherenkov" != pp->particle() ) { // try to add into StdHepID map
177  const int ID = pp->jetsetID();
178  // is this already in the map?
179  auto ifind = m_stdhepidmap.find( ID );
180  if ( m_stdhepidmap.end() != ifind && ifind->second ) {
181  diff( ifind->second, pp );
182  m_replaced.insert( ifind->second->particle() );
183  }
184  // put it into the map
185  m_stdhepidmap[ID] = pp;
186  }
187  //
188  // add to Pythia map only if Pythia ID is different from
189  // zero ( StdHep id is always different from zero in this case )
190  if ( 0 != pp->pythiaID() && 0 != pp->jetsetID() &&
191  "Tcherenkov" != pp->particle() ) { // try to add into PythiaID map
192  const int ID = pp->pythiaID();
193  // is this already in the map?
194  auto ifind = m_pythiaidmap.find( ID );
195  if ( m_pythiaidmap.end() != ifind && ifind->second ) {
196  diff( ifind->second, pp );
197  m_replaced.insert( ifind->second->particle() );
198  }
199  // put it into the map
200  m_pythiaidmap[ID] = pp;
201  }
202  //
203  return rebuild();
204  }
205  // =============================================================================
207  // =============================================================================
208  namespace
209  {
210  template <class MAP>
211  void _remove_( MAP& m, const ParticleProperty* pp )
212  {
213  auto i = std::find_if( m.begin(), m.end(), [&]( typename MAP::const_reference i ) { return i.second == pp; } );
214  if ( i != m.end() ) {
215  m.erase( i );
216  }
217  }
218  }
219  // ============================================================================
221  {
222  if ( !pp ) {
223  return StatusCode::FAILURE;
224  }
225 
226  _remove_( m_idmap, pp );
227  _remove_( m_namemap, pp );
228  _remove_( m_stdhepidmap, pp );
229  _remove_( m_pythiaidmap, pp );
230  //
231  return rebuild();
232  }
233  // ============================================================================
235  // ============================================================================
237  {
238 
239  // parse "the main" file
240  StatusCode sc = parse( m_filename );
241  if ( sc.isFailure() ) {
242  return sc;
243  }
244 
245  // parse "other" files
246  for ( auto& file : m_other ) {
247  sc = parse( file );
248  if ( sc.isFailure() ) {
249  return sc;
250  }
251  }
252 
253  // Now check that the file format was consistent with what parser
254  // expected
255  if ( m_namemap.empty() ) {
256  error() << "Format of input file inconsistent with what expected"
257  << " - Check you are using ParticleData.txt" << endmsg;
258  return StatusCode::FAILURE;
259  }
260 
261  return sc;
262  }
263  // ============================================================================
265  {
266  auto infile = ( m_fileAccess ? m_fileAccess->open( file ) : nullptr );
267  if ( !infile ) {
268  error() << "Unable to open properties file : " << file << endmsg;
269  return StatusCode::FAILURE;
270  }
271 
273  info() << "Opened particle properties file : " << file << endmsg;
274 
276  tokens.reserve( 9 );
278  while ( std::getline( *infile, line ) ) {
279  // parse each line of the file (comment lines begin with # in the cdf
280  // file,
281  if ( line.front() == '#' ) continue;
282 
283  tokens.clear();
284  ba::trim_left_if( line, ba::is_space() );
285  ba::split( tokens, line, ba::is_space(), boost::token_compress_on );
286  if ( tokens.size() != 9 ) continue;
287 
288  auto gid = std::stoi( tokens[1] );
289  auto jid = std::stoi( tokens[2] );
290  // Change the particles that do not correspond to a pdg number
291  if ( jid == 0 ) jid = 10000000 * gid;
292 
293  // add a particle property
294  sc = push_back( tokens[0], gid, jid, std::stod( tokens[3] ), std::stod( tokens[4] ) * Gaudi::Units::GeV,
295  std::stod( tokens[5] ) * Gaudi::Units::s, tokens[6], std::stoi( tokens[7] ),
296  std::stod( tokens[8] ) * Gaudi::Units::GeV );
297 
298  if ( sc.isFailure() ) {
299  error() << "Error from ParticlePropertySvc::push_back for particle='" << tokens[0] << "'" << endmsg;
300  }
301  }
302  return StatusCode::SUCCESS;
303  }
304  // ============================================================================
310  // ============================================================================
312  {
313  if ( !pp ) {
314  return nullptr;
315  }
316  const int ID = pp->pdgID();
317  const int antiID = -1 * ID;
318  for ( const auto& ap : m_vectpp ) {
319  if ( ap && antiID == ap->pdgID() ) {
320  return ap;
321  } // RETURN
322  };
323  //
324  return pp; // RETURN
325  }
326  // ============================================================================
331  // ============================================================================
333  {
334  // initialize particle<-->antiParticle relations
335  for ( auto& pp : m_vectpp ) {
336  if ( !pp ) {
337  continue;
338  } // CONTINUE
339  const ParticleProperty* ap = anti( pp );
340  if ( ap ) {
341  pp->setAntiParticle( ap );
342  }
343  }
344  return StatusCode::SUCCESS;
345  }
346  // ============================================================================
348  // ============================================================================
349  namespace
350  {
352  template <typename MAP, typename SET>
353  void _load_( MAP& m, SET& result )
354  {
355  for ( auto i = m.begin(); m.end() != i; ++i ) {
356  result.insert( i->second );
357  }
358  }
359  }
360  // ============================================================================
362  {
363  std::set<mapped_type> local;
364  m_vectpp.clear();
365  m_vectpp.reserve( m_idmap.size() + 100 );
366  // load information from maps into the set
367  _load_( m_idmap, local );
368  _load_( m_namemap, local );
369  _load_( m_stdhepidmap, local );
370  _load_( m_pythiaidmap, local );
371  // load information from set to the linear container vector
372  std::copy( std::begin( local ), std::end( local ), std::back_inserter( m_vectpp ) );
373  return setAntiParticles();
374  }
375  // ============================================================================
376  // treat additional particles
377  // ============================================================================
379  {
380  // loop over all "explicit" particles
381  for ( const auto& item : m_particles ) {
382  std::istringstream input( item );
383  // get the name
384  std::string p_name;
385  int p_geant;
386  int p_jetset;
387  double p_charge;
388  double p_mass;
389  double p_ltime;
390  std::string p_evtgen;
391  int p_pythia;
392  double p_maxwid;
393  if ( input >> p_name >> p_geant >> p_jetset >> p_charge >> p_mass >> p_ltime >> p_evtgen >> p_pythia >>
394  p_maxwid ) {
395  always() << " Add/Modify the particle: "
396  << " name='" << p_name << "'"
397  << " geant=" << p_geant << " jetset=" << p_jetset << " charge=" << p_charge << " mass=" << p_mass
398  << " ltime=" << p_ltime << " evtgen='" << p_evtgen << "'"
399  << " pythia=" << p_pythia << " maxwid=" << p_maxwid << endmsg;
400  //
401  StatusCode sc = push_back( p_name, p_geant, p_jetset, p_charge, p_mass * Gaudi::Units::GeV,
402  p_ltime * Gaudi::Units::s, p_evtgen, p_pythia, p_maxwid * Gaudi::Units::GeV );
403  if ( sc.isFailure() ) {
404  return sc;
405  } // RETURN
406  } else {
407  error() << " could not parse '" << item << "'" << endmsg;
408  return StatusCode::FAILURE; // RETURN
409  }
410  }
411  //
412  return StatusCode::SUCCESS;
413  }
414 // ============================================================================
415 #ifdef __ICC
416 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
417 // The comparison are meant
418 #pragma warning( push )
419 #pragma warning( disable : 1572 )
420 #endif
422  {
423  //
424  if ( o == n ) {
425  return false;
426  }
427  //
428  auto& log = msgStream();
429  log << l;
430  if ( !o || !n ) {
431  log << MSG::WARNING << " ParticleProperty* point to NULL" << endmsg;
432  return true; // RETURN
433  }
434  //
435  bool result = false;
436  if ( o->particle() != n->particle() ) {
437  result = true;
438  log << " Name:'" << o->particle() << "'/'" << n->particle() << "'";
439  }
440  if ( o->geantID() != n->geantID() ) {
441  result = true;
442  log << " G3ID:" << o->geantID() << "/" << n->geantID() << "'";
443  }
444  if ( o->pdgID() != n->pdgID() ) {
445  result = true;
446  log << " PDGID:" << o->pdgID() << "/" << n->pdgID() << "'";
447  }
448  if ( o->pythiaID() != n->pythiaID() ) {
449  result = true;
450  log << " PYID:" << o->pythiaID() << "/" << n->pythiaID() << "'";
451  }
452  if ( o->charge() != n->charge() ) {
453  result = true;
454  log << " Q:" << o->charge() << "/" << n->charge() << "'";
455  }
456  if ( o->mass() != n->mass() ) {
457  result = true;
458  log << " M:" << o->mass() << "/" << n->mass() << "'";
459  }
460  if ( o->lifetime() != n->lifetime() ) {
461  result = true;
462  log << " T:" << o->lifetime() << "/" << n->lifetime() << "'";
463  }
464  if ( o->evtGenName() != n->evtGenName() ) {
465  result = true;
466  log << " EvtGen:" << o->evtGenName() << "/" << n->evtGenName() << "'";
467  }
468  if ( o->maxWidth() != n->maxWidth() ) {
469  result = true;
470  log << " WMAX:" << o->maxWidth() << "/" << n->maxWidth() << "'";
471  }
472  if ( result ) {
473  log << endmsg;
474  }
475  //
476  return result;
477  }
478 }
479 #ifdef __ICC
480 // re-enable icc remark #1572
481 #pragma warning( pop )
482 #endif
483 // ============================================================================
484 // The END
485 // ============================================================================
int pdgID() const
Get the PDG (= JETSET) ID.
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:411
constexpr static const auto FAILURE
Definition: StatusCode.h:88
StatusCode rebuild()
rebuild "the linear container" from the map
StatusCode initialize() override
Definition: Service.cpp:63
T empty(T...args)
T copy(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
constexpr double s
int geantID() const
Get the GEANT3 ID.
StatusCode finalize() override
Definition: Service.cpp:173
This service provides access to particle properties.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
double maxWidth() const
Get the max width deviation.
T front(T...args)
A trivial class to hold information about a single particle properties.
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:356
StatusCode erase(int geantId) override
Erase a property by geant3 id.
T stod(T...args)
T getline(T...args)
StatusCode finalize() override
Finalise the service.
StatusCode initialize() override
Initialise the service.
STL namespace.
MapName m_namemap
Map for particle names.
StatusCode parse()
Parses the file and fill all the maps.
T end(T...args)
SmartIF< IFileAccess > m_fileAccess
bool isFailure() const
Definition: StatusCode.h:139
StatusCode push_back(const std::string &particle, int geantId, int jetsetId, double charge, double mass, double tlife, const std::string &evtName, int pythiaId, double maxWidth) override
Create a new particle property.
STL class.
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
std::set< std::string > m_replaced
int jetsetID() const
Get the JETSET(StdHep) ID.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
double lifetime() const
Get the particle lifetime.
constexpr double m
Definition: SystemOfUnits.h:94
T clear(T...args)
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:294
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
MapStdHepID m_stdhepidmap
Map for StdHep Ids.
dictionary l
Definition: gaudirun.py:440
T insert(T...args)
const ParticleProperty * anti(const ParticleProperty *pp) const
helper (protected) function to find an antiparticle for the given particle ID (StdHepID) ...
T find(T...args)
T size(T...args)
STL class.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T...args)
bool diff(const ParticleProperty *o, const ParticleProperty *n, const MSG::Level l=MSG::DEBUG) const
T back_inserter(T...args)
#define SET(x)
Gaudi::Property< std::vector< std::string > > m_other
double charge() const
Get the particle charge.
MsgStream & msgStream() const
Return an uninitialized MsgStream.
std::set< std::unique_ptr< ParticleProperty > > m_owned
MapID m_idmap
Map for geant IDs.
Gaudi::Property< std::string > m_filename
Gaudi::Property< std::vector< std::string > > m_particles
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn&#39;t already exist.
Definition: Service.h:84
constexpr double GeV
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
StatusCode setAntiParticles()
helper (protected) function to set the valid particle<–>antiparticle relations
VectPP m_vectpp
Vector of all particle properties.
const std::string & particle() const
Get the particle name.
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
double mass() const
Get the particle mass.
int pythiaID() const
Get the Pythia ID.
T stoi(T...args)
virtual std::unique_ptr< std::istream > open(const std::string &url)=0
Find the URL and returns a unique_ptr to an input stream interface of an object that can be used to r...
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
MapPythiaID m_pythiaidmap
Map for Pythia Ids.
T reserve(T...args)
const std::string & evtGenName() const
Get the EvtGen name.