The Gaudi Framework  v33r1 (b1225454)
ParticlePropertySvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Include files
13 // ============================================================================
14 // STD&STL
15 // ============================================================================
16 #include "boost/algorithm/string/classification.hpp"
17 #include "boost/algorithm/string/split.hpp"
18 #include "boost/algorithm/string/trim.hpp"
19 #include <cctype>
20 #include <fstream>
21 namespace ba = boost::algorithm;
22 // ============================================================================
23 // GaudiKernel
24 // ============================================================================
27 #include "GaudiKernel/MsgStream.h"
30 #include "GaudiKernel/System.h"
31 // ============================================================================
32 //#include "GaudiKernel/ToStream.h"
33 // ============================================================================
34 // Local
35 // ============================================================================
36 #include "ParticlePropertySvc.h"
37 // ============================================================================
38 namespace Gaudi {
42  DECLARE_COMPONENT( ParticlePropertySvc )
43  // ============================================================================
55  // ============================================================================
58  // Redefine the default name:
59  if ( System::getEnv( "PARAMFILESROOT", m_filename.value() ) ) { m_filename.value() += "/data/ParticleTable.txt"; }
60  }
61  // ============================================================================
63  // ============================================================================
66  if ( sc.isFailure() ) { return sc; }
67 
68  sc = setProperties();
69  if ( sc.isFailure() ) {
70  error() << " Could not set the properties " << endmsg;
71  return sc;
72  }
73 
74  m_fileAccess = service( "VFSSvc" );
75  if ( !m_fileAccess ) {
76  error() << " Cannot retrieve the VFS service " << endmsg;
77  return StatusCode::FAILURE;
78  }
79 
80  sc = parse();
81  if ( sc.isFailure() ) {
82  error() << " Could not parse the file " << endmsg;
83  return sc;
84  }
85  if ( !m_particles.empty() ) {
86  sc = addParticles();
87  if ( sc.isFailure() ) {
88  error() << " Could not treat particles! " << endmsg;
89  return sc;
90  }
91  }
92  debug() << "ParticleProperties parsed successfully" << endmsg;
93 
94  debug() << "Access properties" << endmsg;
95  // For debugging purposes print out the size of the internal maps
96  // particle name as key: all particles in database are present here
97  debug() << "NameMap size =" << m_namemap.size() << endmsg;
98  // Geant3 ID as key: all particles in database are present here
99  debug() << "GeantID Map size =" << m_idmap.size() << endmsg;
100  // StdHep ID as key: some particles have no valid StdHep ID
101  debug() << "StdHepID Map size =" << m_stdhepidmap.size() << endmsg;
102  // Pythia ID as key: some particles are not defined in Pythia
103  debug() << "PythiaID Map size =" << m_pythiaidmap.size() << endmsg;
104 
105  if ( !m_replaced.empty() ) {
106  info() << "Properties have been redefined for " << m_replaced.size()
107  << " particles : " << Gaudi::Utils::toString( m_replaced ) << endmsg;
108  }
109 
110  return StatusCode::SUCCESS;
111  }
112  // =============================================================================
114  // =============================================================================
116  if ( !m_other.empty() ) {
117  info() << "Additional Properties have been read from files: " << Gaudi::Utils::toString( m_other ) << endmsg;
118  }
119 
120  if ( !m_replaced.empty() ) {
121  always() << "Properties have been redefined for " << m_replaced.size()
122  << " particles : " << Gaudi::Utils::toString( m_replaced ) << endmsg;
123  }
124 
126 
128  return Service::finalize();
129  }
130  // =============================================================================
132  // =============================================================================
133  StatusCode ParticlePropertySvc::push_back( const std::string& particle, int geantId, int jetsetId, double charge,
134  double mass, double tlife, const std::string& evtName, int pythiaId,
135  double maxWidth ) {
136  //
137  auto& pp = m_owned.emplace_back( particle, geantId, jetsetId, charge, mass, tlife, evtName, pythiaId, maxWidth );
138  return push_back( &pp );
139  }
140  // =============================================================================
142  // =============================================================================
144  if ( !pp ) { return StatusCode::FAILURE; }
145  //
146  { // try to add into Geant(3)ID map
147  const int ID = pp->geantID();
148  // is this already in the map?
149  auto ifind = m_idmap.find( ID );
150  if ( m_idmap.end() != ifind && 0 != m_idmap[ID] ) {
151  diff( ifind->second, pp );
152  m_replaced.insert( m_idmap[ID]->particle() );
153  }
154  // put it into the map
155  m_idmap[ID] = pp;
156  }
157  //
158  { // try to add into Name map
159  const std::string& particle = pp->particle();
160  // is this already in the map?
161  auto ifind = m_namemap.find( particle );
162  if ( ifind != m_namemap.end() && ifind->second ) {
163  diff( ifind->second, pp );
164  m_replaced.insert( ifind->second->particle() );
165  }
166  // put it into the map
167  m_namemap[particle] = pp;
168  }
169  //
170  // add to StdHep map only if StdHep ID different from zero and if
171  // not Cerenkov (StdHep ID = gamma)
172  if ( 0 != pp->jetsetID() && "Tcherenkov" != pp->particle() ) { // try to add into StdHepID map
173  const int ID = pp->jetsetID();
174  // is this already in the map?
175  auto ifind = m_stdhepidmap.find( ID );
176  if ( m_stdhepidmap.end() != ifind && ifind->second ) {
177  diff( ifind->second, pp );
178  m_replaced.insert( ifind->second->particle() );
179  }
180  // put it into the map
181  m_stdhepidmap[ID] = pp;
182  }
183  //
184  // add to Pythia map only if Pythia ID is different from
185  // zero ( StdHep id is always different from zero in this case )
186  if ( 0 != pp->pythiaID() && 0 != pp->jetsetID() && "Tcherenkov" != pp->particle() ) { // try to add into PythiaID
187  // map
188  const int ID = pp->pythiaID();
189  // is this already in the map?
190  auto ifind = m_pythiaidmap.find( ID );
191  if ( m_pythiaidmap.end() != ifind && ifind->second ) {
192  diff( ifind->second, pp );
193  m_replaced.insert( ifind->second->particle() );
194  }
195  // put it into the map
196  m_pythiaidmap[ID] = pp;
197  }
198  //
199  return rebuild();
200  }
201  // =============================================================================
203  // =============================================================================
204  namespace {
205  template <class MAP>
206  void _remove_( MAP& m, const ParticleProperty* pp ) {
207  auto i = std::find_if( m.begin(), m.end(), [&]( typename MAP::const_reference i ) { return i.second == pp; } );
208  if ( i != m.end() ) { m.erase( i ); }
209  }
210  } // namespace
211  // ============================================================================
213  if ( !pp ) { return StatusCode::FAILURE; }
214 
215  _remove_( m_idmap, pp );
216  _remove_( m_namemap, pp );
217  _remove_( m_stdhepidmap, pp );
218  _remove_( m_pythiaidmap, pp );
219  //
220  return rebuild();
221  }
222  // ============================================================================
224  // ============================================================================
226 
227  // parse "the main" file
228  StatusCode sc = parse( m_filename );
229  if ( sc.isFailure() ) { return sc; }
230 
231  // parse "other" files
232  for ( auto& file : m_other ) {
233  sc = parse( file );
234  if ( sc.isFailure() ) { return sc; }
235  }
236 
237  // Now check that the file format was consistent with what parser
238  // expected
239  if ( m_namemap.empty() ) {
240  error() << "Format of input file inconsistent with what expected"
241  << " - Check you are using ParticleData.txt" << endmsg;
242  return StatusCode::FAILURE;
243  }
244 
245  return sc;
246  }
247  // ============================================================================
249  auto infile = ( m_fileAccess ? m_fileAccess->open( file ) : nullptr );
250  if ( !infile ) {
251  error() << "Unable to open properties file : " << file << endmsg;
252  return StatusCode::FAILURE;
253  }
254 
256  info() << "Opened particle properties file : " << file << endmsg;
257 
259  tokens.reserve( 9 );
261  while ( std::getline( *infile, line ) ) {
262  // parse each line of the file (comment lines begin with # in the cdf
263  // file,
264  if ( line.front() == '#' ) continue;
265 
266  tokens.clear();
267  ba::trim_left_if( line, ba::is_space() );
268  ba::split( tokens, line, ba::is_space(), boost::token_compress_on );
269  if ( tokens.size() != 9 ) continue;
270 
271  auto gid = std::stoi( tokens[1] );
272  auto jid = std::stoi( tokens[2] );
273  // Change the particles that do not correspond to a pdg number
274  if ( jid == 0 ) jid = 10000000 * gid;
275 
276  // add a particle property
277  sc = push_back( tokens[0], gid, jid, std::stod( tokens[3] ), std::stod( tokens[4] ) * Gaudi::Units::GeV,
278  std::stod( tokens[5] ) * Gaudi::Units::s, tokens[6], std::stoi( tokens[7] ),
279  std::stod( tokens[8] ) * Gaudi::Units::GeV );
280 
281  if ( sc.isFailure() ) {
282  error() << "Error from ParticlePropertySvc::push_back for particle='" << tokens[0] << "'" << endmsg;
283  }
284  }
285  return StatusCode::SUCCESS;
286  }
287  // ============================================================================
293  // ============================================================================
295  if ( !pp ) { return nullptr; }
296  const int ID = pp->pdgID();
297  const int antiID = -1 * ID;
298  for ( const auto& ap : m_vectpp ) {
299  if ( ap && antiID == ap->pdgID() ) { return ap; } // RETURN
300  };
301  //
302  return pp; // RETURN
303  }
304  // ============================================================================
309  // ============================================================================
311  // initialize particle<-->antiParticle relations
312  for ( auto& pp : m_vectpp ) {
313  if ( !pp ) { continue; } // CONTINUE
314  const ParticleProperty* ap = anti( pp );
315  if ( ap ) { pp->setAntiParticle( ap ); }
316  }
317  return StatusCode::SUCCESS;
318  }
319  // ============================================================================
321  // ============================================================================
322  namespace {
324  template <typename MAP, typename SET>
325  void _load_( MAP& m, SET& result ) {
326  for ( auto i = m.begin(); m.end() != i; ++i ) { result.insert( i->second ); }
327  }
328  } // namespace
329  // ============================================================================
331  std::set<mapped_type> local;
332  m_vectpp.clear();
333  m_vectpp.reserve( m_idmap.size() + 100 );
334  // load information from maps into the set
335  _load_( m_idmap, local );
336  _load_( m_namemap, local );
337  _load_( m_stdhepidmap, local );
338  _load_( m_pythiaidmap, local );
339  // load information from set to the linear container vector
340  std::copy( std::begin( local ), std::end( local ), std::back_inserter( m_vectpp ) );
341  return setAntiParticles();
342  }
343  // ============================================================================
344  // treat additional particles
345  // ============================================================================
347  // loop over all "explicit" particles
348  for ( const auto& item : m_particles ) {
349  std::istringstream input( item );
350  // get the name
351  std::string p_name;
352  int p_geant;
353  int p_jetset;
354  double p_charge;
355  double p_mass;
356  double p_ltime;
357  std::string p_evtgen;
358  int p_pythia;
359  double p_maxwid;
360  if ( input >> p_name >> p_geant >> p_jetset >> p_charge >> p_mass >> p_ltime >> p_evtgen >> p_pythia >>
361  p_maxwid ) {
362  always() << " Add/Modify the particle: "
363  << " name='" << p_name << "'"
364  << " geant=" << p_geant << " jetset=" << p_jetset << " charge=" << p_charge << " mass=" << p_mass
365  << " ltime=" << p_ltime << " evtgen='" << p_evtgen << "'"
366  << " pythia=" << p_pythia << " maxwid=" << p_maxwid << endmsg;
367  //
368  StatusCode sc = push_back( p_name, p_geant, p_jetset, p_charge, p_mass * Gaudi::Units::GeV,
369  p_ltime * Gaudi::Units::s, p_evtgen, p_pythia, p_maxwid * Gaudi::Units::GeV );
370  if ( sc.isFailure() ) { return sc; } // RETURN
371  } else {
372  error() << " could not parse '" << item << "'" << endmsg;
373  return StatusCode::FAILURE; // RETURN
374  }
375  }
376  //
377  return StatusCode::SUCCESS;
378  }
379 // ============================================================================
380 #ifdef __ICC
381 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
382 // The comparison are meant
383 # pragma warning( push )
384 # pragma warning( disable : 1572 )
385 #endif
387  //
388  if ( o == n ) { return false; }
389  //
390  auto& log = msgStream();
391  log << l;
392  if ( !o || !n ) {
393  log << MSG::WARNING << " ParticleProperty* point to NULL" << endmsg;
394  return true; // RETURN
395  }
396  //
397  bool result = false;
398  if ( o->particle() != n->particle() ) {
399  result = true;
400  log << " Name:'" << o->particle() << "'/'" << n->particle() << "'";
401  }
402  if ( o->geantID() != n->geantID() ) {
403  result = true;
404  log << " G3ID:" << o->geantID() << "/" << n->geantID() << "'";
405  }
406  if ( o->pdgID() != n->pdgID() ) {
407  result = true;
408  log << " PDGID:" << o->pdgID() << "/" << n->pdgID() << "'";
409  }
410  if ( o->pythiaID() != n->pythiaID() ) {
411  result = true;
412  log << " PYID:" << o->pythiaID() << "/" << n->pythiaID() << "'";
413  }
414  if ( o->charge() != n->charge() ) {
415  result = true;
416  log << " Q:" << o->charge() << "/" << n->charge() << "'";
417  }
418  if ( o->mass() != n->mass() ) {
419  result = true;
420  log << " M:" << o->mass() << "/" << n->mass() << "'";
421  }
422  if ( o->lifetime() != n->lifetime() ) {
423  result = true;
424  log << " T:" << o->lifetime() << "/" << n->lifetime() << "'";
425  }
426  if ( o->evtGenName() != n->evtGenName() ) {
427  result = true;
428  log << " EvtGen:" << o->evtGenName() << "/" << n->evtGenName() << "'";
429  }
430  if ( o->maxWidth() != n->maxWidth() ) {
431  result = true;
432  log << " WMAX:" << o->maxWidth() << "/" << n->maxWidth() << "'";
433  }
434  if ( result ) { log << endmsg; }
435  //
436  return result;
437  }
438 } // namespace Gaudi
439 #ifdef __ICC
440 // re-enable icc remark #1572
441 # pragma warning( pop )
442 #endif
443 // ============================================================================
444 // The END
445 // ============================================================================
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:379
bool diff(const ParticleProperty *o, const ParticleProperty *n, const MSG::Level l=MSG::DEBUG) const
StatusCode rebuild()
rebuild "the linear container" from the map
StatusCode initialize() override
Definition: Service.cpp:70
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:35
constexpr double s
StatusCode finalize() override
Definition: Service.cpp:174
This service provides access to particle properties.
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:341
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.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode initialize() override
Initialise the service.
STL namespace.
int pythiaID() const
Get the Pythia ID.
MapName m_namemap
Map for particle names.
StatusCode parse()
Parses the file and fill all the maps.
T end(T... args)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
#define SET(x)
SmartIF< IFileAccess > m_fileAccess
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
double maxWidth() const
Get the max width deviation.
std::deque< ParticleProperty > m_owned
const ParticleProperty * anti(const ParticleProperty *pp) const
helper (protected) function to find an antiparticle for the given particle ID (StdHepID)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
constexpr double m
int pdgID() const
Get the PDG (= JETSET) ID.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T clear(T... args)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:290
MapStdHepID m_stdhepidmap
Map for StdHep Ids.
dictionary l
Definition: gaudirun.py:543
T insert(T... args)
T find(T... args)
T size(T... args)
STL class.
const std::string & evtGenName() const
Get the EvtGen name.
T begin(T... args)
T back_inserter(T... args)
Gaudi::Property< std::vector< std::string > > m_other
constexpr static const auto FAILURE
Definition: StatusCode.h:101
int geantID() const
Get the GEANT3 ID.
MapID m_idmap
Map for geant IDs.
Gaudi::Property< std::string > m_filename
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:93
Gaudi::Property< std::vector< std::string > > m_particles
constexpr double GeV
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
bool isFailure() const
Definition: StatusCode.h:145
const std::string & particle() const
Get the particle name.
StatusCode setAntiParticles()
helper (protected) function to set the valid particle<-->antiparticle relations
VectPP m_vectpp
Vector of all particle properties.
T stoi(T... args)
double mass() const
Get the particle mass.
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...
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
double lifetime() const
Get the particle lifetime.
MapPythiaID m_pythiaidmap
Map for Pythia Ids.
double charge() const
Get the particle charge.
T reserve(T... args)
T emplace_back(T... args)