CMS 3D CMS Logo

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