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