Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <cassert>
00015 #include <algorithm>
00016 #include "boost/bind.hpp"
00017 #include "TTree.h"
00018 #include "TFile.h"
00019 #include "TKey.h"
00020
00021
00022 #include "DataFormats/FWLite/interface/EventSetup.h"
00023 #include "DataFormats/FWLite/interface/format_type_name.h"
00024 #include "DataFormats/FWLite/interface/Record.h"
00025 #include "FWCore/Utilities/interface/Exception.h"
00026
00027
00028
00029
00030 using namespace fwlite;
00031 static const char* const kRecordAuxiliaryBranchName="ESRecordAuxiliary";
00032
00033
00034
00035
00036
00037
00038
00039 EventSetup::EventSetup(TFile* iFile):
00040 m_event(0),
00041 m_file(iFile)
00042 {
00043 }
00044
00045
00046
00047
00048
00049
00050 EventSetup::~EventSetup()
00051 {
00052 for(std::vector<Record*>::iterator it = m_records.begin(), itEnd=m_records.end();
00053 it !=itEnd; ++it) {
00054 delete *it;
00055 }
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 void
00074 EventSetup::syncTo(const edm::EventID& iID, const edm::Timestamp& iTime) {
00075 std::for_each(m_records.begin(),
00076 m_records.end(),
00077 boost::bind(&Record::syncTo,_1,iID,iTime));
00078 }
00079
00080
00081
00082
00083 bool
00084 EventSetup::exists(const char* iRecordName) const
00085 {
00086 std::string realName = unformat_mangled_to_type(iRecordName);
00087 TObject* obj = m_file->Get(realName.c_str());
00088 if(0==obj) {
00089 return false;
00090 }
00091 TTree* tree = dynamic_cast<TTree*>(obj);
00092 if(0==tree) {
00093 return false;
00094 }
00095 return 0 != tree->FindBranch(kRecordAuxiliaryBranchName);
00096 }
00097
00098 RecordID
00099 EventSetup::recordID(const char* iRecordName) const
00100 {
00101 std::string treeName = format_type_to_mangled(iRecordName);
00102 TObject* obj = m_file->Get(treeName.c_str());
00103 if(0==obj) {
00104 throw cms::Exception("UnknownRecord")<<"The TTree for the record "<<iRecordName<<" does not exist "<<m_file->GetName();
00105 }
00106 TTree* tree = dynamic_cast<TTree*>(obj);
00107 if(0==tree) {
00108 throw cms::Exception("UnknownRecord")<<"The object corresponding to "<<iRecordName<<" in file "<<m_file->GetName()<<" is not a TTree and therefore is not a Record";
00109 }
00110 if(0 == tree->FindBranch(kRecordAuxiliaryBranchName)) {
00111 throw cms::Exception("UnknownRecord")<<"The TTree corresponding to "<<iRecordName<<" in file "<<m_file->GetName()<<" does not have the proper structure to be a Record";
00112 }
00113
00114 std::string name(iRecordName);
00115 for(std::vector<Record*>::iterator it = m_records.begin(), itEnd=m_records.end(); it!=itEnd;++it){
00116 if((*it)->name()==name) {
00117 return it - m_records.begin();
00118 }
00119 }
00120
00121
00122 Record* rec = new Record(iRecordName, tree);
00123 m_records.push_back(rec);
00124 return m_records.size()-1;
00125 }
00126
00127 const Record&
00128 EventSetup::get(const RecordID& iID) const
00129 {
00130 assert(iID<m_records.size());
00131 return *(m_records[iID]);
00132 }
00133
00134 std::vector<std::string>
00135 EventSetup::namesOfAvailableRecords() const
00136 {
00137 std::vector<std::string> returnValue;
00138
00139 TList* keys = m_file->GetListOfKeys();
00140
00141 TIter next(keys);
00142 while(TObject* obj = next() ) {
00143 TKey* key = static_cast<TKey*> (obj);
00144 if(0==strcmp(key->GetClassName(),"TTree")) {
00145 returnValue.push_back(unformat_mangled_to_type(key->GetName()));
00146 }
00147 }
00148 return returnValue;
00149 }
00150
00151
00152
00153