![]() |
![]() |
00001 #ifndef DataFormats_FWLite_Handle_h 00002 #define DataFormats_FWLite_Handle_h 00003 // -*- C++ -*- 00004 // 00005 // Package: FWLite 00006 // Class : Handle 00007 // 00016 // 00017 // Original Author: Chris Jones 00018 // Created: Tue May 8 15:01:26 EDT 2007 00019 // 00020 00021 // system include files 00022 00023 // user include files 00024 #if !defined(__CINT__) && !defined(__MAKECINT__) 00025 //CINT can't handle parsing these files 00026 #include "DataFormats/FWLite/interface/EventBase.h" 00027 #include "DataFormats/FWLite/interface/ErrorThrower.h" 00028 #include "DataFormats/Common/interface/Wrapper.h" 00029 #endif 00030 00031 // forward declarations 00032 namespace fwlite { 00033 class ErrorThrower; 00034 00035 template <class T> 00036 class Handle 00037 { 00038 00039 public: 00040 // this typedef is to avoid a Cint bug where calling 00041 // edm::Wrapper<T>::typeInfo() fails for some types with 00042 // very long expansions. First noticed for the expansion 00043 // of reco::JetTracksAssociation::Container 00044 typedef edm::Wrapper<T> TempWrapT; 00045 00046 Handle() : data_(0), errorThrower_(ErrorThrower::unsetErrorThrower()) {} 00047 ~Handle() { delete errorThrower_;} 00048 00049 Handle(const Handle<T>& iOther) : data_(iOther.data_), 00050 errorThrower_( iOther.errorThrower_? iOther.errorThrower_->clone(): 0) {} 00051 00052 const Handle<T>& operator=(const Handle<T>& iOther) { 00053 Handle<T> temp(iOther); 00054 swap(iOther); 00055 } 00056 00057 // ---------- const member functions --------------------- 00058 bool isValid() const { return data_ != 0; } 00059 00061 bool failedToGet() const {return errorThrower_ != 0; } 00062 00063 T const* product() const { check(); return data_;} 00064 00065 const T* ptr() const { check(); return data_;} 00066 const T& ref() const { check(); return *data_;} 00067 00068 const T* operator->() const { 00069 check(); 00070 return data_; 00071 } 00072 00073 const T& operator*() const { 00074 check(); 00075 return *data_; 00076 } 00077 // ---------- static member functions -------------------- 00078 00079 // ---------- member functions --------------------------- 00080 /* 00081 void getByBranchName(const fwlite::Event& iEvent, const char* iBranchName) { 00082 iEvent.getByBranchName(edm::Wrapper<T>::typeInfo(), iBranchName, data_); 00083 } 00084 */ 00085 00086 // P can be Run, Event, or LuminosityBlock 00087 00088 template <class P> void getByLabel(const P& iP, 00089 const char* iModuleLabel, 00090 const char* iProductInstanceLabel = 0, 00091 const char* iProcessLabel = 0) { 00092 TempWrapT* temp; 00093 void* pTemp = &temp; 00094 iP.getByLabel(TempWrapT::typeInfo(), 00095 iModuleLabel, 00096 iProductInstanceLabel, 00097 iProcessLabel, 00098 pTemp); 00099 delete errorThrower_; 00100 errorThrower_ = 0; 00101 if(0==temp) { 00102 errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(), 00103 iModuleLabel, 00104 iProductInstanceLabel, 00105 iProcessLabel); 00106 return; 00107 } 00108 data_ = temp->product(); 00109 if(data_==0) { 00110 errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(), 00111 iModuleLabel, 00112 iProductInstanceLabel, 00113 iProcessLabel); 00114 } 00115 } 00116 00117 00118 // void getByLabel(const fwlite::Event& iEvent, 00119 // const char* iModuleLabel, 00120 // const char* iProductInstanceLabel = 0, 00121 // const char* iProcessLabel = 0) { 00122 // TempWrapT* temp; 00123 // void* pTemp = &temp; 00124 // iEvent.getByLabel(TempWrapT::typeInfo(), 00125 // iModuleLabel, 00126 // iProductInstanceLabel, 00127 // iProcessLabel, 00128 // pTemp); 00129 // delete errorThrower_; 00130 // errorThrower_ = 0; 00131 // if(0==temp) { 00132 // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(), 00133 // iModuleLabel, 00134 // iProductInstanceLabel, 00135 // iProcessLabel); 00136 // return; 00137 // } 00138 // data_ = temp->product(); 00139 // if(data_==0) { 00140 // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(), 00141 // iModuleLabel, 00142 // iProductInstanceLabel, 00143 // iProcessLabel); 00144 // } 00145 // } 00146 // 00147 // void getByLabel(const fwlite::ChainEvent& iEvent, 00148 // const char* iModuleLabel, 00149 // const char* iProductInstanceLabel = 0, 00150 // const char* iProcessLabel = 0) { 00151 // TempWrapT* temp; 00152 // void* pTemp = &temp; 00153 // iEvent.getByLabel(TempWrapT::typeInfo(), 00154 // iModuleLabel, 00155 // iProductInstanceLabel, 00156 // iProcessLabel, 00157 // pTemp); 00158 // delete errorThrower_; 00159 // errorThrower_ = 0; 00160 // if(0==temp) { 00161 // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(), 00162 // iModuleLabel, 00163 // iProductInstanceLabel, 00164 // iProcessLabel); 00165 // return; 00166 // } 00167 // data_ = temp->product(); 00168 // if(data_==0) { 00169 // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(), 00170 // iModuleLabel, 00171 // iProductInstanceLabel, 00172 // iProcessLabel); 00173 // } 00174 // } 00175 // 00176 // 00177 // void getByLabel(const fwlite::MultiChainEvent& iEvent, 00178 // const char* iModuleLabel, 00179 // const char* iProductInstanceLabel = 0, 00180 // const char* iProcessLabel = 0) { 00181 // TempWrapT* temp; 00182 // void* pTemp = &temp; 00183 // iEvent.getByLabel(TempWrapT::typeInfo(), 00184 // iModuleLabel, 00185 // iProductInstanceLabel, 00186 // iProcessLabel, 00187 // pTemp); 00188 // if ( 0 != errorThrower_ ) delete errorThrower_; 00189 // errorThrower_ = 0; 00190 // if(0==temp) { 00191 // errorThrower_=ErrorThrower::errorThrowerBranchNotFoundException(TempWrapT::typeInfo(), 00192 // iModuleLabel, 00193 // iProductInstanceLabel, 00194 // iProcessLabel); 00195 // return; 00196 // } 00197 // data_ = temp->product(); 00198 // if(data_==0) { 00199 // errorThrower_=ErrorThrower::errorThrowerProductNotFoundException(TempWrapT::typeInfo(), 00200 // iModuleLabel, 00201 // iProductInstanceLabel, 00202 // iProcessLabel); 00203 // } 00204 // } 00205 00206 const std::string getBranchNameFor(const fwlite::EventBase& iEvent, 00207 const char* iModuleLabel, 00208 const char* iProductInstanceLabel = 0, 00209 const char* iProcessLabel = 0) 00210 { 00211 return iEvent.getBranchNameFor(TempWrapT::typeInfo(), 00212 iModuleLabel, 00213 iProductInstanceLabel, 00214 iProcessLabel); 00215 } 00216 00217 // const std::string getBranchNameFor(const fwlite::Event& iEvent, 00218 // const char* iModuleLabel, 00219 // const char* iProductInstanceLabel = 0, 00220 // const char* iProcessLabel = 0) { 00221 // return iEvent.getBranchNameFor(TempWrapT::typeInfo(), 00222 // iModuleLabel, 00223 // iProductInstanceLabel, 00224 // iProcessLabel); 00225 // } 00226 // 00227 // const std::string getBranchNameFor(const fwlite::ChainEvent& iEvent, 00228 // const char* iModuleLabel, 00229 // const char* iProductInstanceLabel = 0, 00230 // const char* iProcessLabel = 0) { 00231 // return iEvent.getBranchNameFor(TempWrapT::typeInfo(), 00232 // iModuleLabel, 00233 // iProductInstanceLabel, 00234 // iProcessLabel); 00235 // } 00236 // 00237 // 00238 // 00239 // const std::string getBranchNameFor(const fwlite::MultiChainEvent& iEvent, 00240 // const char* iModuleLabel, 00241 // const char* iProductInstanceLabel = 0, 00242 // const char* iProcessLabel = 0) { 00243 // return iEvent.getBranchNameFor(TempWrapT::typeInfo(), 00244 // iModuleLabel, 00245 // iProductInstanceLabel, 00246 // iProcessLabel); 00247 // } 00248 00249 void swap( Handle<T>& iOther) { 00250 const T* temp = data_; 00251 data_ = iOther.data_; 00252 iOther.data_ = temp; 00253 ErrorThrower* tempE = errorThrower_; 00254 errorThrower_ = iOther.errorThrower_; 00255 iOther.errorThrower = tempE; 00256 } 00257 private: 00258 void check() const { if(errorThrower_) { errorThrower_->throwIt();} } 00259 00260 00261 // ---------- member data -------------------------------- 00262 const T* data_; 00263 ErrorThrower* errorThrower_; 00264 }; 00265 00266 } 00267 #endif