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):
39 m_file(iFile)
40 {
41 }
42 
43 // EventSetup::EventSetup(const EventSetup& rhs)
44 // {
45 // // do actual copying here;
46 // }
47 
49 {
50  for(auto const& record : m_records) {
51  delete record;
52  }
53 }
54 
55 //
56 // assignment operators
57 //
58 // const EventSetup& EventSetup::operator=(const EventSetup& rhs)
59 // {
60 // //An exception safe implementation is
61 // EventSetup temp(rhs);
62 // swap(rhs);
63 //
64 // return *this;
65 // }
66 
67 //
68 // member functions
69 //
70 void
71 EventSetup::syncTo(const edm::EventID& iID, const edm::Timestamp& iTime) {
72  using std::placeholders::_1;
73  std::for_each(m_records.begin(),
74  m_records.end(),
75  std::bind(&Record::syncTo,_1,iID,iTime));
76 }
77 
78 //
79 // const member functions
80 //
81 bool
82 EventSetup::exists(const char* iRecordName) const
83 {
84  std::string realName = unformat_mangled_to_type(iRecordName);
85  TObject* obj = m_file->Get(realName.c_str());
86  if(nullptr == obj) {
87  return false;
88  }
89  TTree* tree = dynamic_cast<TTree*>(obj);
90  if(nullptr == tree) {
91  return false;
92  }
93  return nullptr != tree->FindBranch(kRecordAuxiliaryBranchName);
94 }
95 
96 RecordID
97 EventSetup::recordID(const char* iRecordName) const
98 {
99  std::string treeName = format_type_to_mangled(iRecordName);
100  TObject* obj = m_file->Get(treeName.c_str());
101  if(nullptr == obj) {
102  throw cms::Exception("UnknownRecord")<<"The TTree for the record "<<iRecordName<<" does not exist "<<m_file->GetName();
103  }
104  TTree* tree = dynamic_cast<TTree*>(obj);
105  if(nullptr == tree) {
106  throw cms::Exception("UnknownRecord")<<"The object corresponding to "<<iRecordName<<" in file "<<m_file->GetName()<<" is not a TTree and therefore is not a Record";
107  }
108  if(nullptr == tree->FindBranch(kRecordAuxiliaryBranchName)) {
109  throw cms::Exception("UnknownRecord")<<"The TTree corresponding to "<<iRecordName<<" in file "<<m_file->GetName()<<" does not have the proper structure to be a Record";
110  }
111  //do we already have this Record?
112  std::string name(iRecordName);
113  for(std::vector<Record*>::const_iterator it = m_records.begin(), itEnd=m_records.end(); it!=itEnd;++it){
114  if((*it)->name()==name) {
115  return it - m_records.begin();
116  }
117  }
118 
119  //Not found so need to make a new one
120  Record* rec = new Record(iRecordName, tree);
121  m_records.push_back(rec);
122  return m_records.size()-1;
123 }
124 
125 const Record&
126 EventSetup::get(const RecordID& iID) const
127 {
128  assert(iID<m_records.size());
129  return *(m_records[iID]);
130 }
131 
132 std::vector<std::string>
134 {
135  std::vector<std::string> returnValue;
136 
137  TList* keys = m_file->GetListOfKeys();
138  //this is ROOT's recommended way to iterate
139  TIter next(keys);
140  while(TObject* obj = next() ) {
141  TKey* key = static_cast<TKey*> (obj);
142  if(0==strcmp(key->GetClassName(),"TTree")) {
143  returnValue.push_back(unformat_mangled_to_type(key->GetName()));
144  }
145  }
146  return returnValue;
147 }
148 
149 //
150 // static member functions
151 //
void syncTo(const edm::EventID &, const edm::Timestamp &)
Definition: EventSetup.cc:71
static const char *const kRecordAuxiliaryBranchName
Definition: EventSetup.cc:30
EventSetup(TFile *)
Definition: EventSetup.cc:38
JetCorrectorParameters::Record record
Definition: classes.h:7
std::vector< std::string > namesOfAvailableRecords() const
Definition: EventSetup.cc:133
unsigned int RecordID
Definition: EventSetup.h:64
std::vector< Record * > m_records
Definition: EventSetup.h:111
RecordID recordID(const char *iRecordName) const
Definition: EventSetup.cc:97
std::string unformat_mangled_to_type(const std::string &)
given a mangled name return the C++ class name
virtual ~EventSetup()
Definition: EventSetup.cc:48
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:90
Definition: tree.py:1
bool exists(const char *iRecordName) const
Definition: EventSetup.cc:82
const Record & get(const RecordID &) const
Definition: EventSetup.cc:126