/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/UTIL/PIDHandler.cc

Go to the documentation of this file.
00001 #include "UTIL/PIDHandler.h"
00002 #include "Exceptions.h"
00003 
00004 #include "IMPL/ReconstructedParticleImpl.h"
00005 #include "IMPL/ClusterImpl.h"
00006 
00007 #include <sstream>
00008 
00009 using namespace IMPL;
00010 
00011 namespace UTIL{
00012 
00013 
00014   enum ObjectType {
00015     Cluster = 1 ,
00016     ReconstructedParticle  
00017   } ;
00018 
00019   
00020   PIDHandler::PIDHandler( LCCollection* col ) : 
00021     _col( col) , 
00022     _cpm( "PIDAlgorithmTypeName",  "PIDAlgorithmTypeID" ,  col ),
00023     _type(-1) , 
00024     _maxID(-1) {
00025     
00026     init( col ) ;
00027   }
00028   
00029   
00030   PIDHandler::PIDHandler(  const LCCollection* col ) : 
00031     _col( 0 ) , // not needed 
00032     _cpm( "PIDAlgorithmTypeName",  "PIDAlgorithmTypeID" ,  col ),
00033     _type(-1) , 
00034     _maxID(-1) {
00035     
00036     init( col ) ;
00037   }
00038   
00039 
00040  void  PIDHandler::init(  const LCCollection* col ) {
00041     
00042 
00043     // ---- get the information on existing ParticleID objects ----------
00044 
00045 
00046     for( CPM::map_type::iterator it = _cpm.map().begin() ; it != _cpm.map().end() ; ++it) {
00047       
00048       int id = it->second ;
00049      
00050       const std::string& aName =  it->first ;
00051 
00052       if( id > _maxID )
00053         _maxID = id ;
00054 
00055       // ensure id is unique for collection
00056       PNM::iterator nit = _pNames.find( id ) ;
00057 
00058       if( nit != _pNames.end() ){
00059 
00060         std::stringstream sstr ;
00061 
00062         sstr << " PIDHandler::PIDHandler() - duplicate algorithm type IDs: "
00063              <<  aName << " [" << id << "] " ;
00064         
00065         throw Exception( sstr.str() ) ;
00066 
00067       }
00068 
00069       // get parameter names
00070 
00071       StringVec pNames ;
00072 
00073       col->getParameters().getStringVals( std::string( "ParameterNames_" + aName   ) , pNames ) ;
00074 
00075       _pNames[ id ] = pNames ;  // save a copy of parameter names for algorithm with id
00076 
00077       _ids.push_back( id ) ;
00078 
00079       //   save inverse map for lookup algoName from algoID
00080 
00081       _cpmInv.insert( std::make_pair( id  , aName )  ) ;
00082 
00083     } 
00084 
00085     //------------------------------------------------
00086 
00087     // determine LCCollection type 
00088     std::string colType = col->getTypeName()  ;
00089     
00090     //  arghhh ...
00091     if( colType == LCIO::RECONSTRUCTEDPARTICLE ) {
00092       
00093       _type = ReconstructedParticle ;
00094       
00095     } else if( colType == LCIO::CLUSTER ){
00096       
00097       _type = Cluster ;
00098       
00099     } else {
00100       
00101       throw Exception( "PIDHandler::PIDHandler() " 
00102                        " collection type is neither Cluster nor ReconstructedParticle ") ;
00103     }
00104   }
00105   
00106   
00107 
00108 
00109   PIDHandler::~PIDHandler() {
00110   
00111 
00112     if (_col != 0 ) {
00113       // save the collection parameters
00114       
00115       for( PNM::iterator pnm = _pNames.begin() ; pnm != _pNames.end() ; ++pnm) {
00116         
00117         int id = pnm->first ;
00118         
00119         
00120         _col->parameters().setValues( std::string( "ParameterNames_" + _cpmInv[id]  ) , 
00121                                       pnm->second ) ;
00122       }
00123     }
00124   }
00125   
00126   int PIDHandler::addAlgorithm( const std::string& algoName, const StringVec& pNames ) {
00127     
00128     CPM::map_type& map = _cpm.map() ;
00129     
00130     CPM::map_type::iterator it = map.find( algoName ) ; 
00131     
00132     if( it != map.end() ){
00133       
00134       std::stringstream sstr ;
00135       
00136       sstr << " PIDHandler::addAlgorithm() - duplicate algorithm name: "
00137            <<  algoName ; 
00138       
00139       throw Exception( sstr.str() ) ;
00140     }
00141     
00142     int id  =  nextID() ;
00143 
00144     map.insert( std::make_pair( algoName , id ) ) ;
00145     
00146     // inverse map:
00147     _cpmInv.insert( std::make_pair( id  , algoName )  ) ;
00148 
00149     // save parameter names
00150     _pNames[ id ] = pNames ;      
00151 
00152     _ids.push_back( id ) ;
00153 
00154     return id ;
00155   }
00156   
00157   int PIDHandler::getAlgorithmID( const std::string& algoName ) {
00158 
00159     CPM::map_type::iterator it = _cpm.map().find( algoName ) ; 
00160     
00161     if( it == _cpm.map().end() ){
00162 
00163       throw UnknownAlgorithm(  algoName ) ;
00164     }
00165 
00166     return it->second ;
00167   }
00168   
00169   const std::string& PIDHandler::getAlgorithmName(  int algoID ) {
00170     
00171     CPMINV::iterator it = _cpmInv.find( algoID ) ; 
00172 
00173     if( it == _cpmInv.end() ){
00174       
00175       throw UnknownAlgorithm(  std::string(""+algoID) ) ;
00176     }
00177     
00178     return it->second ;
00179   }
00180   
00181 
00182 
00183   int PIDHandler::getParameterIndex( int algorithmID, const std::string& name ) {
00184     
00185     
00186     PNM::iterator nit = _pNames.find( algorithmID ) ;
00187     
00188     if( nit == _pNames.end() ){
00189 
00190       throw UnknownAlgorithm( std::string(""+algorithmID) ) ;
00191     }
00192     // brute force search:
00193     
00194     const StringVec& names =  nit->second ;
00195 
00196     unsigned n = names.size() ;
00197 
00198     for(unsigned i=0 ; i<n ; ++i){
00199 
00200       if( names[i] == name ) 
00201 
00202         return i ;
00203     }
00204 
00205     return -1 ; // or better throw sth. ?
00206   }
00207   
00208   
00209   const StringVec&  PIDHandler::getParameterNames( int id  ) {
00210 
00211     PNM::iterator nit = _pNames.find( id ) ;
00212     
00213     if( nit == _pNames.end() ){
00214 
00215       throw UnknownAlgorithm( std::string(""+id ) ) ;
00216     }
00217 
00218     return nit->second ;
00219   }
00220     
00221   const IntVec& PIDHandler::getAlgorithmIDs() {
00222 
00223     return _ids ;
00224   }
00225 
00226 
00227 
00228   void PIDHandler::setParticleIDUsed(  ReconstructedParticleImpl* p , int id  ) {
00229 
00230     PNM::iterator nit = _pNames.find( id ) ;
00231     
00232     if( nit == _pNames.end() ){
00233 
00234       throw UnknownAlgorithm( std::string(""+id ) ) ;
00235     }
00236 
00237     ParticleID* pid = 0 ;
00238     
00239 //     const ParticleIDVec* idv = 0 ;    
00240 //     if( _type == ReconstructedParticle  ){
00241       
00242 //       idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs()  ) ; 
00243 //     }
00244 //     else if( _type == Cluster  ){
00245       
00246 //       idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs()  ) ; 
00247 //     }
00248 //     const ParticleIDVec& pidV = *idv ;        
00249 
00250     const ParticleIDVec& pidV = p->getParticleIDs() ;
00251     
00252     unsigned nPid = pidV.size() ;
00253     
00254     for(unsigned i=0; i<nPid; ++i ) {
00255       
00256       if( pidV[i]->getAlgorithmType() == id ) {
00257         
00258         pid = pidV[i]  ;
00259         break ;
00260         
00261       }
00262     }
00263     
00264     if( pid == 0 ) {
00265 
00266       throw UnknownAlgorithm( std::string("pid object not found in particle for algorithmId: "+id ) ) ;
00267     }
00268 
00269     p->setParticleIDUsed( pid ) ;
00270 
00271   }
00272 
00273 
00274   const ParticleID& PIDHandler::getParticleID( LCObject* p , int id ) {
00275 
00276     PNM::iterator nit = _pNames.find( id ) ;
00277     
00278     if( nit == _pNames.end() ){
00279 
00280       throw UnknownAlgorithm( std::string(""+id ) ) ;
00281     }
00282 
00283     
00284     const ParticleIDVec* idv = 0 ;       
00285 
00286     if( _type == ReconstructedParticle  ){
00287       
00288       idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs()  ) ; 
00289     }
00290     else if( _type == Cluster  ){
00291       
00292       idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs()  ) ; 
00293     }
00294 
00295     const ParticleIDVec& pidV = *idv ;   
00296 
00297     unsigned nPid = pidV.size() ;
00298     
00299     for(unsigned i=0; i<nPid; ++i ) {
00300 
00301       if( pidV[i]->getAlgorithmType() == id ) 
00302         
00303         return *pidV[i] ;
00304 
00305     }
00306         
00307     // not returned, i.e. we need to create a new pid objects
00308 
00309     ParticleIDImpl* pid = new ParticleIDImpl ;
00310 
00311 
00312     if( _type == ReconstructedParticle  ){
00313       
00314       static_cast< ReconstructedParticleImpl* >(p)->addParticleID( pid ) ; 
00315     }
00316     else if( _type == Cluster  ){
00317       
00318       static_cast< ClusterImpl* >(p)->addParticleID( pid )  ; 
00319     }
00320 
00321 
00322     return *pid ;
00323 
00324 
00325   }
00326   
00327   void PIDHandler::setParticleID( LCObject* p ,
00328                                   int userType, 
00329                                   int PDG,
00330                                   float likelihood,
00331                                   int id, 
00332                                   const FloatVec& params ) {
00333     
00334 
00335     PNM::iterator nit = _pNames.find( id ) ;
00336     
00337     if( nit == _pNames.end() ){
00338       
00339       throw UnknownAlgorithm( std::string(""+id ) ) ;
00340     }
00341     
00342 
00343     // ---- check paramaters size -----
00344     unsigned nParam =  params.size()  ;
00345 
00346     if( nParam != _pNames[ id ].size() ) {
00347       
00348       std::stringstream sstr ;
00349       
00350       sstr << " PIDHandler::setParticleID() - wrong parmeter size specified: "
00351            <<  nParam << " - expected " << _pNames[ id ].size()  ; 
00352       
00353       throw Exception( sstr.str() ) ;
00354 
00355     }
00356 
00357     ParticleIDImpl* pid = 0 ;
00358     
00359     const ParticleIDVec* idv = 0 ;       
00360     
00361     if( _type == ReconstructedParticle  ){
00362       
00363       idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs()  ) ; 
00364     }
00365     else if( _type == Cluster  ){
00366       
00367       idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs()  ) ; 
00368     }
00369     
00370     const ParticleIDVec& pidV = *idv ;   
00371     
00372     unsigned nPid = pidV.size() ;
00373     
00374     for(unsigned i=0; i<nPid; ++i ) {
00375       
00376       if( pidV[i]->getAlgorithmType() == id ) {
00377         
00378         pid = static_cast<ParticleIDImpl*>( pidV[i] ) ;
00379         break ;
00380         
00381       }
00382     }
00383     
00384     // if nothing found we create a new object
00385     
00386     bool isNewPID = false ;
00387     
00388     if( pid == 0 ) {
00389 
00390       pid = new ParticleIDImpl ;
00391 
00392       isNewPID = true ;
00393 
00394     }
00395 
00396     // ---- now set the PID data ------------
00397     
00398     pid->setLikelihood( likelihood ) ;
00399     
00400     pid->setType( userType ) ;
00401     
00402     pid->setPDG( PDG ) ;
00403     
00404     pid->setAlgorithmType( id ) ;
00405     
00406     FloatVec& ps = pid->parameters() ;
00407     
00408     ps.resize( nParam ) ;
00409     
00410     for(unsigned k=0; k< nParam  ; k++){
00411       
00412       ps[ k ] = params[ k ] ;
00413     }
00414     
00415     // ----------------------------------------
00416 
00417 
00418     if( isNewPID ) { // need to add it to the particle/cluster
00419 
00420       if( _type == ReconstructedParticle  ){
00421         
00422         static_cast< ReconstructedParticleImpl* >(p)->addParticleID( pid ) ; 
00423       }
00424       else if( _type == Cluster  ){
00425         
00426         static_cast< ClusterImpl* >(p)->addParticleID( pid )  ; 
00427       }
00428     }
00429     
00430   }
00431 }

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7