/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/IMPL/LCEventImpl.cc

Go to the documentation of this file.
00001 
00002 #include "IMPL/LCEventImpl.h"
00003 #include "IMPL/AccessChecked.h"
00004 #include "IMPL/LCCollectionVec.h"
00005 
00006 #define EVT_WGT "_weight" 
00007 
00008 #include <iostream>
00009 #include <sstream>
00010 
00011 
00012 using namespace EVENT ;
00013 //using namespace DATA ;
00014 
00015 namespace IMPL {
00016   
00017 LCEventImpl::LCEventImpl() :
00018   _runNumber(0),
00019   _eventNumber(0),
00020   _timeStamp(0),
00021   _detectorName("unknown") {
00022 }
00023   
00024 // LCEventImpl::LCEventImpl(const LCEvent& evt) : 
00025 //   _runNumber( evt.getRunNumber() ),
00026 //   _eventNumber( evt.getEventNumber() ),
00027 //   _timeStamp( evt.getTimeStamp() ),
00028 //   _detectorName( evt.getDetectorName().c_str() ),
00029   
00030 //   std::vector<std::string>* strVec = evt.getCollectionNames() ;
00031 //   int nCol = strVec->size() ;
00032   
00033 //   for( std::vector<std::string>::iterator name = strVec->begin() ; name != strVec->end() ; name++){
00034     
00035 //     const LCCollection* col = evt.getCollection( *name ) ;
00036 //     col->getTypeName() ;
00037     
00038 //     // to be done - need to create new LCCollectionVec and add to the event ...
00039 //   }
00040 
00041 // }
00042 
00043 LCEventImpl::~LCEventImpl() {
00044   //  std::cout << " ~LCEventImpl() : " << this << std::endl ;
00045 
00046   // here we need to delete all collections in the event
00047   typedef LCCollectionMap::const_iterator LCI ;
00048   
00049   for ( LCI i=_colMap.begin() ; i != _colMap.end() ; i++ ){
00050     
00051     // except collections whose ownership we gave away
00052     if( _notOwned.find( i->second ) ==  _notOwned.end() )
00053       delete i->second ;
00054   }
00055 
00056 }
00057     
00058 int LCEventImpl::getRunNumber() const {
00059   return _runNumber ;
00060 }
00061 
00062     
00063 int LCEventImpl::getEventNumber() const {
00064   return _eventNumber ;
00065 }
00066 
00067     
00068 const std::string & LCEventImpl::getDetectorName() const {
00069   return _detectorName ;
00070 }
00071 
00072     
00073 long64 LCEventImpl::getTimeStamp() const {
00074   return _timeStamp ;
00075 }
00076 
00077 double LCEventImpl::getWeight() const {
00078   double w = _params.getFloatVal( EVT_WGT ) ;
00079     
00080   return w == 0 ? 1. : w ;
00081 
00082 }
00083 
00084    
00085 const std::vector<std::string>* LCEventImpl::getCollectionNames() const {
00086 
00087   // return pointer to updated vector _colNames 
00088   typedef LCCollectionMap::const_iterator LCI ;
00089   
00090   _colNames.clear() ;
00091 
00092   for ( LCI i=_colMap.begin() ; i != _colMap.end() ; i++ ){
00093     _colNames.push_back( i->first  ) ; 
00094   }
00095   return &_colNames ;
00096 }
00097 
00098     
00099 
00100 LCCollection * LCEventImpl::getCollection(const std::string & name) const 
00101   throw (DataNotAvailableException, std::exception) {
00102 
00103   LCCollectionMap::iterator it = _colMap.find( name )  ;
00104 
00105   if( it == _colMap.end() ) {
00106     
00107     std::stringstream ss ;
00108     ss << "LCEventImpl::getCollection: collection not in event:" << name ;
00109 
00110     throw( DataNotAvailableException( ss.str() ) ) ; 
00111   }
00112   return  it->second ;
00113 
00114   //   if( _colMap.find( name ) == _colMap.end() ) 
00115   //     throw(DataNotAvailableException( std::string("LCEventImpl::getCollection: collection not in event:" //                                                  + name) )) ; 
00116   //   return  _colMap[ name ] ;
00117   
00118 }
00119 
00120 
00121 LCCollection * LCEventImpl::takeCollection(const std::string & name) const 
00122   throw (DataNotAvailableException, std::exception) {
00123 
00124   LCCollectionVec* col = dynamic_cast<LCCollectionVec*> ( getCollection( name ) ) ;
00125 
00126   col->setTransient( true ) ;
00127   
00128   _notOwned.insert( col ) ;
00129 
00130   return  col ;
00131 }
00132 
00133 
00134     
00135 
00136 void  LCEventImpl::addCollection(LCCollection * col, const std::string & name) 
00137   throw (EventException, std::exception)  {
00138 
00139   
00140   if( ! validateCollectionName(name.c_str()) ){
00141 
00142     throw EventException( std::string("LCEventImpl::addCollection() invalid name (has to be C/C++ name): "
00143                                       +name) ) ; 
00144   }
00145       
00146   // check if name exists
00147   if( _colMap.find( name ) != _colMap.end() )
00148     
00149     throw EventException( std::string("LCEventImpl::addCollection() name already exists: "
00150                                       +name) ) ; 
00151   // check if col != 0
00152   if( col == 0  )
00153 
00154     throw EventException( std::string("LCEventImpl::addCollection()  cannot add NULL collection for : "
00155                                       +name) ) ; 
00156 
00157   _colMap[ name ]  = col ;
00158  
00159 }
00160 
00161     
00162 
00163 void LCEventImpl::removeCollection(const std::string & name) throw (ReadOnlyException, std::exception) {
00164 
00165   // remove collection only, if access mode == update
00166   checkAccess("LCEventImpl::removeCollection") ;
00167   _colMap.erase( name ) ;  
00168 
00169 }
00170 
00171     
00172 
00173 void LCEventImpl::setRunNumber( int rn ) {
00174   checkAccess("LCEventImpl::setRunNumber") ;
00175   _runNumber = rn ;
00176 }
00177 
00178 
00179 void LCEventImpl::setEventNumber( int en ) {
00180   checkAccess("LCEventImpl::setEventNumber") ;
00181   _eventNumber = en ;
00182 }
00183 
00184     
00185 void LCEventImpl::setDetectorName(const std::string& dn ) {
00186   checkAccess("LCEventImpl::setDetectorName") ;
00187   _detectorName = dn ;
00188 }
00189 
00190     
00191 void LCEventImpl::setTimeStamp(long64 ts) {
00192   checkAccess("LCEventImpl::setTimeStamp") ;
00193   _timeStamp =  ts ;
00194 }
00195 
00196 
00197 void LCEventImpl::setWeight(double w) {
00198   checkAccess("LCEventImpl::setWeight") ;
00199   _params.setValue(  EVT_WGT , (float) w ) ;
00200 }
00201 
00202      
00203 void LCEventImpl::setAccessMode( int accessMode ) {
00204 
00205   // loop over all collections and set the access mode
00206   bool readOnly = ( accessMode == LCIO::READ_ONLY ) ;
00207 
00208   setReadOnly( readOnly ) ;
00209 
00210   typedef LCCollectionMap::iterator LCI ;
00211   
00212   for ( LCI i=_colMap.begin() ; i != _colMap.end() ; i++ ){
00213 
00214     AccessChecked* col = dynamic_cast<AccessChecked*>( i->second ) ; 
00215     if( col ){
00216       col->setReadOnly( readOnly ) ;
00217     }
00218   }
00219 
00220 
00221 }
00222   
00223   /**Tests the validity of a collection name. SIO only accepts names starting with 
00224    * (regular expression) [A-Za-z_] and continuing with [A-Za-z0-9_] (C/C++ variable name).
00225    */
00226   bool LCEventImpl::validateCollectionName( const char* name ){ //copy of SIO_functions::validateName
00227     
00228     if( *name < 0 ) 
00229       return false ;
00230     
00231     if( !isalpha( (int)*name ) && *name != '_' ) 
00232       return false ;
00233     
00234     for( name += 1; *name != '\0'; name++ ){
00235       if( *name < 0 ) 
00236         return false ;
00237       if( !isalnum( (int)*name ) && *name != '_' ) 
00238         return false ;
00239     } 
00240     return true ;
00241   }
00242 
00243 }

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