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
00014
00015 namespace IMPL {
00016
00017 LCEventImpl::LCEventImpl() :
00018 _runNumber(0),
00019 _eventNumber(0),
00020 _timeStamp(0),
00021 _detectorName("unknown") {
00022 }
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 LCEventImpl::~LCEventImpl() {
00044
00045
00046
00047 typedef LCCollectionMap::const_iterator LCI ;
00048
00049 for ( LCI i=_colMap.begin() ; i != _colMap.end() ; i++ ){
00050
00051
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
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
00115
00116
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
00147 if( _colMap.find( name ) != _colMap.end() )
00148
00149 throw EventException( std::string("LCEventImpl::addCollection() name already exists: "
00150 +name) ) ;
00151
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
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
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
00224
00225
00226 bool LCEventImpl::validateCollectionName( const char* name ){
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 }