Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

ParticlePropertySvc.cpp

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiSvc/src/ParticlePropertySvc/ParticlePropertySvc.cpp,v 1.1.1.2 2001/04/18 18:32:50 tlindner Exp $
00002 
00003 //Include files
00004 #include "GaudiKernel/SvcFactory.h"
00005 #include "GaudiKernel/ISvcLocator.h"
00006 #include "GaudiKernel/MsgStream.h"
00007 
00008 #include "GaudiKernel/ParticleProperty.h"
00009 #include "ParticlePropertySvc.h"
00010 
00011 #include "CLHEP/Units/PhysicalConstants.h"
00012 
00013 #include <cstdlib>
00014 
00015 #ifdef WIN32
00016 #include <fstream>
00017 #else
00018 #include <fstream.h>
00019 #endif
00020 
00021 
00022 // Instantiation of a static factory class used by clients to create
00023 // instances of this service
00024 static SvcFactory<ParticlePropertySvc> s_factory;
00025 const ISvcFactory& ParticlePropertySvcFactory = s_factory;
00026 
00027 // ---------------------------------------------------------------------------
00028 // 
00029 // ParticlePropertySvc Class
00030 //
00031 // Implement a ParticlePropertySvc 
00032 // Author     : I. Last
00033 // Modify by  : G.Corti 09/11/1999 to read file from LHCBDBASE and
00034 //              introduce find/eraseByStdHepID methods
00035 //
00036 // ---------------------------------------------------------------------------
00037 // Routine: Constructor.
00038 // Purpose: Create service and set default ParticlePropertiesFile
00039 // ---------------------------------------------------------------------------
00040 //
00041 
00042 ParticlePropertySvc::ParticlePropertySvc( const std::string& name, ISvcLocator* svc )
00043  : Service( name, svc ) {
00044   declareProperty( "ParticlePropertiesFile", m_filename );
00045 
00046   if(getenv("LHCBDBASE") != NULL) {
00047     m_filename = std::string(getenv( "LHCBDBASE" )) + std::string( "/cdf/particle.cdf");
00048   }
00049   else {
00050     m_filename = std::string( "particle.cdf" );
00051   }
00052 }
00053 
00054 // ---------------------------------------------------------------------------
00055 // Routine: Destructor.
00056 // Purpose:
00057 // ---------------------------------------------------------------------------
00058 //
00059 
00060 ParticlePropertySvc::~ParticlePropertySvc() {
00061   for ( MapName::iterator i = m_namemap.begin(); i != m_namemap.end(); i++ ) {
00062     if ( (*i).second ) delete (*i).second; 
00063   }
00064 }
00065 
00066 // ---------------------------------------------------------------------------
00067 // Routine: type.
00068 // Purpose: Return the service type.
00069 // ---------------------------------------------------------------------------
00070 //
00071 
00072 const IID& ParticlePropertySvc::type() const { 
00073   return IID_IParticlePropertySvc; 
00074 }
00075 
00076 // ---------------------------------------------------------------------------
00077 // Routine: begin
00078 // Purpose: Get an iterator to the beginning of the container (the name map).
00079 // ---------------------------------------------------------------------------
00080 //
00081 
00082 ParticlePropertySvc::const_iterator ParticlePropertySvc::begin() const { 
00083   return m_vectpp.begin();
00084 }
00085 
00086 
00087 // ---------------------------------------------------------------------------
00088 // Routine: end
00089 // Purpose: Get an iterator to the end of the container (the name map)
00090 // ---------------------------------------------------------------------------
00091 //
00092 
00093 ParticlePropertySvc::const_iterator ParticlePropertySvc::end() const { 
00094   return m_vectpp.end();
00095 }
00096 
00097 
00098 // ---------------------------------------------------------------------------
00099 // Routine: size
00100 // Purpose: Get the size of the container.
00101 // ---------------------------------------------------------------------------
00102 //
00103 
00104 int ParticlePropertySvc::size() const { 
00105   return m_vectpp.size();
00106 }
00107 
00108 // ---------------------------------------------------------------------------
00109 // Routine: initialize
00110 // Purpose: initialize the service and setProperties (check if properties set in
00111 //          jobOptions),
00112 // ---------------------------------------------------------------------------
00113 //
00114 
00115 StatusCode ParticlePropertySvc::initialize() {
00116 
00117   StatusCode status = Service::initialize();
00118   
00119   setProperties();  
00120 
00121   MsgStream log( msgSvc(), name() );
00122 
00123   if ( status.isSuccess() ) {
00124     status = parseFile();
00125     if ( status.isSuccess() ) {
00126       log << MSG::DEBUG << "ParticleProperties parsed successfully" << endreq;
00127       log << MSG::DEBUG << "Access properties" << endreq;
00128       // For debugging purposes print out the size of the internal maps  
00129       // particle name as key: all particles in database are present here
00130       log << MSG::DEBUG << "NameMap size =" << m_namemap.size() << endreq;
00131       // Geant3 ID as key: all particles in database are present here
00132       log << MSG::DEBUG << "GeantID Map size =" << m_idmap.size() << endreq;
00133       // StdHep ID as key: some particles have no valid StdHep ID 
00134       log << MSG::DEBUG << "StdHepID Map size =" << m_stdhepidmap.size() << endreq;
00135     }
00136     else log << MSG::INFO << "Could not initialise service" << endreq;
00137   }
00138   else
00139     log << MSG::INFO << "Could not initialise service" << endreq;
00140 
00141   return status;
00142 }
00143 
00144 // ---------------------------------------------------------------------------
00145 // Routine: finalize
00146 // Purpose:
00147 // ---------------------------------------------------------------------------
00148 //
00149 
00150 StatusCode ParticlePropertySvc::finalize() {
00151 
00152   MsgStream log( msgSvc(), name() );
00153   StatusCode status = Service::finalize();
00154 
00155   if ( status.isSuccess() )
00156     log << MSG::INFO << "Service finalised successfully" << endreq;
00157   
00158   return status;
00159 }
00160 
00161 // ---------------------------------------------------------------------------
00162 // Routine: queryInterface.
00163 // Purpose: standard routine.
00164 // ---------------------------------------------------------------------------
00165 //
00166 
00167 StatusCode ParticlePropertySvc::queryInterface( const IID& riid, 
00168                                                 void** ppvInterface ) {
00169   StatusCode sc = StatusCode::FAILURE;
00170   if ( ppvInterface ) {
00171     *ppvInterface = 0;
00172     
00173     if ( riid == IID_IParticlePropertySvc ) {
00174       *ppvInterface = static_cast<IParticlePropertySvc*>(this);
00175       sc = StatusCode::SUCCESS;
00176       addRef();
00177     }
00178     else
00179       sc = Service::queryInterface( riid, ppvInterface );    
00180   }
00181   return sc;
00182 }
00183 
00184 // ---------------------------------------------------------------------------
00185 // Routine: push_back
00186 // Purpose: Create a particle property and insert it in the maps
00187 // ---------------------------------------------------------------------------
00188 //
00189 
00190 StatusCode ParticlePropertySvc::push_back( const std::string& particle,
00191                                            int geantId, int jetsetId, 
00192                                            int type, double charge, 
00193                                            double mass, double tlife ) {
00194 
00195   StatusCode status = StatusCode::FAILURE;
00196   mapped_type pp = new ParticleProperty( particle, geantId, jetsetId, 
00197                                          type, charge, mass, tlife );
00198   if ( pp ) {
00199     status = push_back( pp );
00200     if ( status.isFailure() ) delete pp;
00201   }
00202   return status;
00203 }
00204 
00205 // ---------------------------------------------------------------------------
00206 // Routine: push_back
00207 // Purpose: Add a particle property.
00208 // ---------------------------------------------------------------------------
00209 //
00210 
00211 StatusCode ParticlePropertySvc::push_back( ParticleProperty* pp ) {
00212 
00213   StatusCode status = StatusCode::FAILURE;
00214   if ( pp ) {
00215     // adding into the vector of all particle properties
00216     m_vectpp.push_back( pp );
00217 
00218     // adding into maps, when not successfull remove from maps where
00219     // previously inserted
00220     std::pair<MapID::iterator,bool> result1 =
00221          m_idmap.insert( MapID::value_type( pp->geantID(), pp ));
00222 
00223     if ( result1.second ) {
00224       std::pair<MapName::iterator,bool> result2 =
00225         m_namemap.insert( MapName::value_type( pp->particle(), pp ));
00226       
00227       // add to StdHep map only if StdHep ID different from zero and if
00228       // not Cerenkov (StdHep ID = gamma)
00229       if ( result2.second ) {
00230         if ( (pp->jetsetID() != 0) && ( pp->particle().compare("Tcherenkov")) ) {
00231           std::pair<MapStdHepID::iterator,bool> result3 = 
00232             m_stdhepidmap.insert( MapStdHepID::value_type( pp->jetsetID(), pp ));
00233           if ( result3.second ) {
00234             status = StatusCode::SUCCESS;
00235           }
00236           else {
00237             m_vectpp.pop_back();
00238             m_namemap.erase( pp->particle() );
00239             m_idmap.erase( pp->geantID() );
00240           }
00241         }
00242         else 
00243           status = StatusCode::SUCCESS;
00244       }
00245       else {
00246         m_vectpp.pop_back();
00247         m_idmap.erase( pp->geantID() );
00248       }
00249     }
00250   }
00251   return status;
00252 }
00253 
00254 // ---------------------------------------------------------------------------
00255 // Routine: find
00256 // Purpose: find a particle by geant id.
00257 // ---------------------------------------------------------------------------
00258 //
00259 
00260 ParticlePropertySvc::mapped_type ParticlePropertySvc::find( int geantID ) {
00261 
00262   mapped_type obj = 0;
00263   MapID::const_iterator i = m_idmap.find( geantID );
00264   if ( i != m_idmap.end() ) obj = (*i).second;
00265   return obj;
00266 }
00267 
00268 // ---------------------------------------------------------------------------
00269 // Routine: find
00270 // Purpose: find a particle by name.
00271 // ---------------------------------------------------------------------------
00272 //
00273 
00274 ParticlePropertySvc::mapped_type ParticlePropertySvc::find( const std::string& name ) {
00275 
00276   mapped_type obj = 0;
00277   MapName::const_iterator i = m_namemap.find( name );
00278   if ( i != m_namemap.end() ) obj = (*i).second;
00279   return obj;
00280 }
00281 
00282 // ---------------------------------------------------------------------------
00283 // Routine: findByStdHepID
00284 // Purpose: find a particle StdHepID (Jetset ID)
00285 // ---------------------------------------------------------------------------
00286 //
00287 
00288 ParticlePropertySvc::mapped_type ParticlePropertySvc::findByStdHepID( int stdHepID ) {
00289 
00290   mapped_type obj = 0;
00291   MapStdHepID:: const_iterator i = m_stdhepidmap.find( stdHepID );
00292   if ( i != m_stdhepidmap.end() ) obj = (*i).second;
00293   return obj;
00294 
00295 }
00296 
00297 // ---------------------------------------------------------------------------
00298 // Routine: erase
00299 // Purpose: Erase a particle given by its geant id 
00300 // ---------------------------------------------------------------------------
00301 //
00302 
00303 StatusCode ParticlePropertySvc::erase( int geantId ) {
00304 
00305   StatusCode status = StatusCode::FAILURE;
00306 
00307   MapID::iterator i_id = m_idmap.find( geantId );
00308   if ( i_id != m_idmap.end() ) {
00309     status = StatusCode::SUCCESS;
00310     m_idmap.erase( i_id );
00311     mapped_type pp = (*i_id).second;
00312 
00313     if ( pp ) {
00314       // remove the particle from all containers : name map
00315       MapName::iterator i_name = m_namemap.find( pp->particle() );
00316       if ( i_name != m_namemap.end() ) m_namemap.erase( i_name );
00317       // from StdHep ID map
00318       MapStdHepID::iterator i_stdhepid = m_stdhepidmap.find( pp->jetsetID() );
00319       if ( i_stdhepid != m_stdhepidmap.end() ) m_stdhepidmap.erase( i_stdhepid );
00320       // erase from vector
00321       iterator it;
00322       for ( it = m_vectpp.begin(); it != m_vectpp.end(); it++ ) {
00323         if( *(it) == pp ) {
00324           m_vectpp.erase(it);
00325           break;
00326         }
00327       }
00328       if( it != m_vectpp.end() ) delete pp;
00329     }    
00330   }
00331 
00332   return status;
00333 }
00334 
00335 // ---------------------------------------------------------------------------
00336 // Routine: erase
00337 // Purpose: Erase a particle given by its name.
00338 // ---------------------------------------------------------------------------
00339 //
00340 
00341 StatusCode ParticlePropertySvc::erase( const std::string& name ) {
00342   StatusCode status = StatusCode::FAILURE;
00343 
00344   MapName::iterator i_name = m_namemap.find( name );
00345   if ( i_name != m_namemap.end() ) {
00346     status = StatusCode::SUCCESS;
00347     m_namemap.erase( i_name );
00348     mapped_type pp = (*i_name).second;
00349 
00350     if ( pp ) {
00351       // remove the particle from all containers : Geant3 ID map
00352       MapID::iterator i_id = m_idmap.find( pp->geantID() );
00353       if ( i_id != m_idmap.end() ) m_idmap.erase( i_id );
00354       // from StdHep ID map
00355       MapStdHepID::iterator i_stdhepid = m_stdhepidmap.find( pp->jetsetID() );
00356       if ( i_stdhepid != m_stdhepidmap.end() ) m_stdhepidmap.erase( i_stdhepid );
00357       // erase from vector
00358       iterator it;
00359       for ( it = m_vectpp.begin(); it != m_vectpp.end(); it++ ) {
00360         if( *(it) == pp ) {
00361           m_vectpp.erase(it);
00362           break;
00363         }
00364       }
00365       if( it != m_vectpp.end() ) delete pp;
00366     }    
00367   }
00368 
00369   return status;
00370 }
00371 
00372 // ---------------------------------------------------------------------------
00373 // Routine: eraseByStdHepID
00374 // Purpose: erase a particle given by its StdHepID (Jetset ID)
00375 // ---------------------------------------------------------------------------
00376 //
00377 // test new function 
00378 
00379 StatusCode ParticlePropertySvc::eraseByStdHepID( int stdHepID ) {
00380 
00381   StatusCode status = StatusCode::FAILURE;
00382 
00383   MapStdHepID::iterator i_stdhepid = m_stdhepidmap.find( stdHepID );
00384   if ( i_stdhepid != m_stdhepidmap.end() ) {
00385     status = StatusCode::SUCCESS;
00386     m_stdhepidmap.erase( i_stdhepid );
00387     mapped_type pp = (*i_stdhepid).second;
00388 
00389     if ( pp ) {
00390       // remove the particle from all containers : name map
00391       MapName::iterator i_name = m_namemap.find( pp->particle() );
00392       if ( i_name != m_namemap.end() ) m_namemap.erase( i_name );
00393       // from Geant3 ID map
00394       MapID::iterator i_id = m_idmap.find( pp->geantID() );
00395       if ( i_id != m_idmap.end() ) m_idmap.erase( i_id );
00396       // erase from vector
00397       iterator it;
00398       for ( it = m_vectpp.begin(); it != m_vectpp.end(); it++ ) {
00399         if( *(it) == pp ) {
00400           m_vectpp.erase(it);
00401           break;
00402         }
00403       }
00404       if( it != m_vectpp.end() ) delete pp;
00405     }    
00406   }
00407 
00408   return status;
00409 }
00410 
00411 // ---------------------------------------------------------------------------
00412 // Routine: parseFile
00413 // Purpose: Parses the file and fill all the maps
00414 // ---------------------------------------------------------------------------
00415 //
00416 
00417 StatusCode ParticlePropertySvc::parseFile() {
00418   StatusCode sc = StatusCode::FAILURE;
00419 
00420   MsgStream log( msgSvc(), name() );
00421   char line[ 255 ];
00422   std::ifstream infile( m_filename.c_str() );
00423 
00424   if ( infile ) {
00425           sc = StatusCode::SUCCESS;
00426     log << MSG::INFO << "Opened particle properties file : " << m_filename
00427               << endreq;
00428 
00429         while( infile ) {
00430       // parse each line of the file (comment lines begin with # in the cdf
00431       // file,
00432             infile.getline( line, 255 );
00433             if ( line[0] == '#' ) continue;
00434                         
00435             std::string par, gid, jid, typ, chg, mas, lif; 
00436             char* token = strtok( line, " " );
00437             if ( token ) { par = token; token = strtok( NULL, " " );} else continue;
00438           if ( token ) { gid = token; token = strtok( NULL, " " );} else continue;
00439           if ( token ) { jid = token; token = strtok( NULL, " " );} else continue;
00440             if ( token ) { typ = token; token = strtok( NULL, " " );} else continue;
00441             if ( token ) { chg = token; token = strtok( NULL, " " );} else continue;
00442             if ( token ) { mas = token; token = strtok( NULL, " " );} else continue;
00443             if ( token ) { lif = token; token = strtok( NULL, " " );} else continue;
00444             if ( token != NULL ) continue;
00445 
00446       // In SICb cdf file mass and lifetime units are GeV and sec, specify it so
00447       // that they are converted to Gaudi units (MeV and ns)
00448       double mass = atof( mas.c_str() ) * GeV;
00449       double tlife = atof( lif.c_str() ) * s;
00450 
00451       // add a particle property
00452       push_back( par, atoi( gid.c_str() ), atoi( jid.c_str() ), 
00453                                 atoi( typ.c_str() ), atof( chg.c_str() ), mass, tlife );
00454           }
00455         infile.close();
00456   }
00457   else
00458         log << MSG::ERROR << "Unable to open properties file : " << m_filename << endreq;
00459 
00460   return sc;
00461 }

Generated at Wed Nov 21 12:22:30 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000