CMS 3D CMS Logo

EventSetup.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : EventSetup
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author:
10 // Created: Thu Dec 10 15:58:26 CST 2009
11 //
12 
13 // system include files
14 #include <cassert>
15 #include <algorithm>
16 #include "TTree.h"
17 #include "TFile.h"
18 #include "TKey.h"
19 
20 // user include files
25 
26 //
27 // constants, enums and typedefs
28 //
29 using namespace fwlite;
30 static const char* const kRecordAuxiliaryBranchName = "ESRecordAuxiliary";
31 //
32 // static data member definitions
33 //
34 
35 //
36 // constructors and destructor
37 //
38 EventSetup::EventSetup(TFile* iFile) : m_file(iFile) {}
39 
40 // EventSetup::EventSetup(const EventSetup& rhs)
41 // {
42 // // do actual copying here;
43 // }
44 
46  for (auto const& record : m_records) {
47  delete record;
48  }
49 }
50 
51 //
52 // assignment operators
53 //
54 // const EventSetup& EventSetup::operator=(const EventSetup& rhs)
55 // {
56 // //An exception safe implementation is
57 // EventSetup temp(rhs);
58 // swap(rhs);
59 //
60 // return *this;
61 // }
62 
63 //
64 // member functions
65 //
66 void EventSetup::syncTo(const edm::EventID& iID, const edm::Timestamp& iTime) {
67  using std::placeholders::_1;
68  std::for_each(m_records.begin(), m_records.end(), std::bind(&Record::syncTo, _1, iID, iTime));
69 }
70 
71 //
72 // const member functions
73 //
74 bool EventSetup::exists(const char* iRecordName) const {
75  std::string realName = unformat_mangled_to_type(iRecordName);
76  TObject* obj = m_file->Get(realName.c_str());
77  if (nullptr == obj) {
78  return false;
79  }
80  TTree* tree = dynamic_cast<TTree*>(obj);
81  if (nullptr == tree) {
82  return false;
83  }
84  return nullptr != tree->FindBranch(kRecordAuxiliaryBranchName);
85 }
86 
87 RecordID EventSetup::recordID(const char* iRecordName) const {
88  std::string treeName = format_type_to_mangled(iRecordName);
89  TObject* obj = m_file->Get(treeName.c_str());
90  if (nullptr == obj) {
91  throw cms::Exception("UnknownRecord")
92  << "The TTree for the record " << iRecordName << " does not exist " << m_file->GetName();
93  }
94  TTree* tree = dynamic_cast<TTree*>(obj);
95  if (nullptr == tree) {
96  throw cms::Exception("UnknownRecord") << "The object corresponding to " << iRecordName << " in file "
97  << m_file->GetName() << " is not a TTree and therefore is not a Record";
98  }
99  if (nullptr == tree->FindBranch(kRecordAuxiliaryBranchName)) {
100  throw cms::Exception("UnknownRecord") << "The TTree corresponding to " << iRecordName << " in file "
101  << m_file->GetName() << " does not have the proper structure to be a Record";
102  }
103  //do we already have this Record?
104  std::string name(iRecordName);
105  for (std::vector<Record*>::const_iterator it = m_records.begin(), itEnd = m_records.end(); it != itEnd; ++it) {
106  if ((*it)->name() == name) {
107  return it - m_records.begin();
108  }
109  }
110 
111  //Not found so need to make a new one
112  Record* rec = new Record(iRecordName, tree);
113  m_records.push_back(rec);
114  return m_records.size() - 1;
115 }
116 
117 const Record& EventSetup::get(const RecordID& iID) const {
118  assert(iID < m_records.size());
119  return *(m_records[iID]);
120 }
121 
122 std::vector<std::string> EventSetup::namesOfAvailableRecords() const {
123  std::vector<std::string> returnValue;
124 
125  TList* keys = m_file->GetListOfKeys();
126  //this is ROOT's recommended way to iterate
127  TIter next(keys);
128  while (TObject* obj = next()) {
129  TKey* key = static_cast<TKey*>(obj);
130  if (0 == strcmp(key->GetClassName(), "TTree")) {
131  returnValue.push_back(unformat_mangled_to_type(key->GetName()));
132  }
133  }
134  return returnValue;
135 }
136 
137 //
138 // static member functions
139 //
void syncTo(const edm::EventID &, const edm::Timestamp &)
Definition: EventSetup.cc:66
EventSetup(TFile *)
Definition: EventSetup.cc:38
JetCorrectorParameters::Record record
Definition: classes.h:7
std::vector< std::string > namesOfAvailableRecords() const
Definition: EventSetup.cc:122
unsigned int RecordID
Definition: EventSetup.h:63
std::vector< Record * > m_records
Definition: EventSetup.h:108
RecordID recordID(const char *iRecordName) const
Definition: EventSetup.cc:87
std::string unformat_mangled_to_type(const std::string &)
given a mangled name return the C++ class name
virtual ~EventSetup()
Definition: EventSetup.cc:45
std::string format_type_to_mangled(const std::string &)
given a C++ class name returned a mangled name
void syncTo(const edm::EventID &, const edm::Timestamp &)
Definition: Record.cc:86
static const char *const kRecordAuxiliaryBranchName
Definition: EventSetup.cc:30
Definition: tree.py:1
bool exists(const char *iRecordName) const
Definition: EventSetup.cc:74
const Record & get(const RecordID &) const
Definition: EventSetup.cc:117