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 //
fwlite::EventSetup::~EventSetup
virtual ~EventSetup()
Definition: EventSetup.cc:45
fwlite::EventSetup::get
const Record & get(const RecordID &) const
Definition: EventSetup.cc:117
fwlite::format_type_to_mangled
std::string format_type_to_mangled(const std::string &)
given a C++ class name returned a mangled name
Definition: format_type_name.cc:38
fwlite::EventSetup::recordID
RecordID recordID(const char *iRecordName) const
Definition: EventSetup.cc:87
fwlite::EventSetup::namesOfAvailableRecords
std::vector< std::string > namesOfAvailableRecords() const
Definition: EventSetup.cc:122
fwlite
Definition: TFileDirectory.h:16
tree
Definition: tree.py:1
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
cms::cuda::assert
assert(be >=bs)
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
EventSetup.h
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
fwlite::Record
Definition: Record.h:47
fwlite::EventSetup::EventSetup
EventSetup(TFile *)
Definition: EventSetup.cc:38
fwlite::RecordID
unsigned int RecordID
Definition: EventSetup.h:65
fwlite::EventSetup::m_file
TFile * m_file
Definition: EventSetup.h:109
fwlite::EventSetup::syncTo
void syncTo(const edm::EventID &, const edm::Timestamp &)
Definition: EventSetup.cc:66
kRecordAuxiliaryBranchName
static const char *const kRecordAuxiliaryBranchName
Definition: EventSetup.cc:30
fwlite::EventSetup::m_records
std::vector< Record * > m_records
Definition: EventSetup.h:111
fwlite::EventSetup::exists
bool exists(const char *iRecordName) const
Definition: EventSetup.cc:74
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
fwlite::unformat_mangled_to_type
std::string unformat_mangled_to_type(const std::string &)
given a mangled name return the C++ class name
Definition: format_type_name.cc:69
format_type_name.h
Record.h
edm::EventID
Definition: EventID.h:31
crabWrapper.key
key
Definition: crabWrapper.py:19
fwlite::Record::syncTo
void syncTo(const edm::EventID &, const edm::Timestamp &)
Definition: Record.cc:86
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31
edm::Timestamp
Definition: Timestamp.h:30