File indexing completed on 2024-04-06 12:04:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <cassert>
0015 #include <algorithm>
0016 #include "TTree.h"
0017 #include "TFile.h"
0018 #include "TKey.h"
0019
0020
0021 #include "DataFormats/FWLite/interface/EventSetup.h"
0022 #include "DataFormats/FWLite/interface/format_type_name.h"
0023 #include "DataFormats/FWLite/interface/Record.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025
0026
0027
0028
0029 using namespace fwlite;
0030 static const char* const kRecordAuxiliaryBranchName = "ESRecordAuxiliary";
0031
0032
0033
0034
0035
0036
0037
0038 EventSetup::EventSetup(TFile* iFile) : m_file(iFile) {}
0039
0040
0041
0042
0043
0044
0045 EventSetup::~EventSetup() {
0046 for (auto const& record : m_records) {
0047 delete record;
0048 }
0049 }
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 void EventSetup::syncTo(const edm::EventID& iID, const edm::Timestamp& iTime) {
0067 using std::placeholders::_1;
0068 std::for_each(m_records.begin(), m_records.end(), std::bind(&Record::syncTo, _1, iID, iTime));
0069 }
0070
0071
0072
0073
0074 bool EventSetup::exists(const char* iRecordName) const {
0075 std::string realName = unformat_mangled_to_type(iRecordName);
0076 TObject* obj = m_file->Get(realName.c_str());
0077 if (nullptr == obj) {
0078 return false;
0079 }
0080 TTree* tree = dynamic_cast<TTree*>(obj);
0081 if (nullptr == tree) {
0082 return false;
0083 }
0084 return nullptr != tree->FindBranch(kRecordAuxiliaryBranchName);
0085 }
0086
0087 RecordID EventSetup::recordID(const char* iRecordName) const {
0088 std::string treeName = format_type_to_mangled(iRecordName);
0089 TObject* obj = m_file->Get(treeName.c_str());
0090 if (nullptr == obj) {
0091 throw cms::Exception("UnknownRecord")
0092 << "The TTree for the record " << iRecordName << " does not exist " << m_file->GetName();
0093 }
0094 TTree* tree = dynamic_cast<TTree*>(obj);
0095 if (nullptr == tree) {
0096 throw cms::Exception("UnknownRecord") << "The object corresponding to " << iRecordName << " in file "
0097 << m_file->GetName() << " is not a TTree and therefore is not a Record";
0098 }
0099 if (nullptr == tree->FindBranch(kRecordAuxiliaryBranchName)) {
0100 throw cms::Exception("UnknownRecord") << "The TTree corresponding to " << iRecordName << " in file "
0101 << m_file->GetName() << " does not have the proper structure to be a Record";
0102 }
0103
0104 std::string name(iRecordName);
0105 for (std::vector<Record*>::const_iterator it = m_records.begin(), itEnd = m_records.end(); it != itEnd; ++it) {
0106 if ((*it)->name() == name) {
0107 return it - m_records.begin();
0108 }
0109 }
0110
0111
0112 Record* rec = new Record(iRecordName, tree);
0113 m_records.push_back(rec);
0114 return m_records.size() - 1;
0115 }
0116
0117 const Record& EventSetup::get(const RecordID& iID) const {
0118 assert(iID < m_records.size());
0119 return *(m_records[iID]);
0120 }
0121
0122 std::vector<std::string> EventSetup::namesOfAvailableRecords() const {
0123 std::vector<std::string> returnValue;
0124
0125 TList* keys = m_file->GetListOfKeys();
0126
0127 TIter next(keys);
0128 while (TObject* obj = next()) {
0129 TKey* key = static_cast<TKey*>(obj);
0130 if (0 == strcmp(key->GetClassName(), "TTree")) {
0131 returnValue.push_back(unformat_mangled_to_type(key->GetName()));
0132 }
0133 }
0134 return returnValue;
0135 }
0136
0137
0138
0139