CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecordWriter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCondLite
4 // Class : RecordWriter
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author:
10 // Created: Wed Dec 9 17:01:03 CST 2009
11 // $Id: RecordWriter.cc,v 1.3 2010/02/19 21:13:46 chrjones Exp $
12 //
13 
14 // system include files
15 #include <cassert>
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TBranch.h"
19 #include "Reflex/Type.h"
20 #include "Reflex/Object.h"
21 
22 // user include files
26 
27 using namespace fwlite;
28 //
29 // constants, enums and typedefs
30 //
31 
32 //
33 // static data member definitions
34 //
35 
36 //
37 // constructors and destructor
38 //
39 RecordWriter::RecordWriter(const char* iName, TFile* iFile):
40 pAux_(&aux_)
41 {
42  tree_ = new TTree(fwlite::format_type_to_mangled(iName).c_str(),"Holds data for an EventSetup Record");
43  tree_->SetDirectory(iFile);
44 
45  auxBranch_ = tree_->Branch("ESRecordAuxiliary","edm::ESRecordAuxiliary",&pAux_);
46 }
47 
48 // RecordWriter::RecordWriter(const RecordWriter& rhs)
49 // {
50 // // do actual copying here;
51 // }
52 
54 {
55 }
56 
57 //
58 // assignment operators
59 //
60 // const RecordWriter& RecordWriter::operator=(const RecordWriter& rhs)
61 // {
62 // //An exception safe implementation is
63 // RecordWriter temp(rhs);
64 // swap(rhs);
65 //
66 // return *this;
67 // }
68 
69 //
70 // member functions
71 //
72 void
73 RecordWriter::update(const void* iData, const std::type_info& iType, const char* iLabel)
74 {
75  const char* label = iLabel;
76  if(0==iLabel) {
77  label = "";
78  }
79  std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator itFound = idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType),
80  std::string(iLabel)));
81  if(itFound == idToBuffer_.end()) {
82  //first request
83  DataBuffer buffer;
84  buffer.pBuffer_=iData;
85  ROOT::Reflex::Type t = ROOT::Reflex::Type::ByTypeInfo(iType);
86  assert(t != ROOT::Reflex::Type());
87 
88  std::string className = t.Name(ROOT::Reflex::SCOPED|ROOT::Reflex::FINAL);
89 
90  //now find actual type
91  ROOT::Reflex::Object o(t,const_cast<void*>(iData));
92  ROOT::Reflex::Type trueType = o.DynamicType();
93  buffer.trueType_ = edm::TypeIDBase(trueType.TypeInfo());
94  std::string trueClassName = trueType.Name(ROOT::Reflex::SCOPED|ROOT::Reflex::FINAL);
95 
96  buffer.branch_ = tree_->Branch((fwlite::format_type_to_mangled(className)+"__"+label).c_str(),
97  trueClassName.c_str(),
98  &buffer.pBuffer_);
99  idToBuffer_.insert(std::make_pair(std::make_pair(edm::TypeIDBase(iType),std::string(iLabel)),buffer));
100  itFound = idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType),
101  std::string(iLabel)));
102  }
103  ROOT::Reflex::Type t = ROOT::Reflex::Type::ByTypeInfo(iType);
104  ROOT::Reflex::Object o(t,const_cast<void*>(iData));
105  ROOT::Reflex::Type trueType = o.DynamicType();
106  assert(edm::TypeIDBase(trueType.TypeInfo())==itFound->second.trueType_);
107  itFound->second.branch_->SetAddress(&(itFound->second.pBuffer_));
108  itFound->second.pBuffer_ = iData;
109 }
110 
111 //call update before calling write
112 void
114 {
115  for(std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator it=idToBuffer_.begin(),itEnd=idToBuffer_.end();
116  it!=itEnd;++it) {
117  if(0==it->second.pBuffer_) {
118  throw cms::Exception("MissingESData")<<"The EventSetup data "<<it->first.first.name()<<" '"<<it->first.second<<"' was not supplied";
119  }
120  }
121 
122  aux_ = iValue;
123  tree_->Fill();
124  for(std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator it=idToBuffer_.begin(),itEnd=idToBuffer_.end();
125  it!=itEnd;++it) {
126  it->second.pBuffer_=0;
127  }
128 }
129 
130 void
132 {
133  tree_->Write();
134 }
135 //
136 // const member functions
137 //
138 
139 //
140 // static member functions
141 //
edm::ESRecordAuxiliary aux_
Definition: RecordWriter.h:65
std::string format_type_to_mangled(const std::string &)
given a C++ class name returned a mangled name
edm::ESRecordAuxiliary * pAux_
Definition: RecordWriter.h:66
void fill(const edm::ESRecordAuxiliary &)
std::map< std::pair< edm::TypeIDBase, std::string >, DataBuffer > idToBuffer_
Definition: RecordWriter.h:68
void update(const void *iData, const std::type_info &iType, const char *iLabel)
Definition: RecordWriter.cc:73
std::string className(const T &t)
Definition: ClassName.h:30
RecordWriter(const char *iName, TFile *iFile)
Definition: RecordWriter.cc:39