CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/CondLiteIO/src/RecordWriter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     FWCondLite
00004 // Class  :     RecordWriter
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  
00010 //         Created:  Wed Dec  9 17:01:03 CST 2009
00011 // $Id: RecordWriter.cc,v 1.3 2010/02/19 21:13:46 chrjones Exp $
00012 //
00013 
00014 // system include files
00015 #include <cassert>
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TBranch.h"
00019 #include "Reflex/Type.h"
00020 #include "Reflex/Object.h"
00021 
00022 // user include files
00023 #include "PhysicsTools/CondLiteIO/interface/RecordWriter.h"
00024 #include "DataFormats/FWLite/interface/format_type_name.h"
00025 #include "FWCore/Utilities/interface/Exception.h"
00026 
00027 using namespace fwlite;
00028 //
00029 // constants, enums and typedefs
00030 //
00031 
00032 //
00033 // static data member definitions
00034 //
00035 
00036 //
00037 // constructors and destructor
00038 //
00039 RecordWriter::RecordWriter(const char* iName, TFile* iFile):
00040 pAux_(&aux_)
00041 {
00042    tree_ = new TTree(fwlite::format_type_to_mangled(iName).c_str(),"Holds data for an EventSetup Record");
00043    tree_->SetDirectory(iFile);
00044    
00045    auxBranch_ = tree_->Branch("ESRecordAuxiliary","edm::ESRecordAuxiliary",&pAux_);
00046 }
00047 
00048 // RecordWriter::RecordWriter(const RecordWriter& rhs)
00049 // {
00050 //    // do actual copying here;
00051 // }
00052 
00053 RecordWriter::~RecordWriter()
00054 {
00055 }
00056 
00057 //
00058 // assignment operators
00059 //
00060 // const RecordWriter& RecordWriter::operator=(const RecordWriter& rhs)
00061 // {
00062 //   //An exception safe implementation is
00063 //   RecordWriter temp(rhs);
00064 //   swap(rhs);
00065 //
00066 //   return *this;
00067 // }
00068 
00069 //
00070 // member functions
00071 //
00072 void 
00073 RecordWriter::update(const void* iData, const std::type_info& iType, const char* iLabel)
00074 {
00075    const char* label = iLabel;
00076    if(0==iLabel) {
00077       label = "";
00078    }
00079    std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator itFound = idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType),
00080       std::string(iLabel)));
00081    if(itFound == idToBuffer_.end()) {
00082       //first request
00083       DataBuffer buffer;
00084       buffer.pBuffer_=iData;
00085       ROOT::Reflex::Type t = ROOT::Reflex::Type::ByTypeInfo(iType);
00086       assert(t != ROOT::Reflex::Type());
00087       
00088       std::string className = t.Name(ROOT::Reflex::SCOPED|ROOT::Reflex::FINAL);
00089       
00090       //now find actual type
00091       ROOT::Reflex::Object o(t,const_cast<void*>(iData));
00092       ROOT::Reflex::Type trueType = o.DynamicType();
00093       buffer.trueType_ = edm::TypeIDBase(trueType.TypeInfo());
00094       std::string trueClassName = trueType.Name(ROOT::Reflex::SCOPED|ROOT::Reflex::FINAL);
00095       
00096       buffer.branch_ = tree_->Branch((fwlite::format_type_to_mangled(className)+"__"+label).c_str(),
00097                                      trueClassName.c_str(),
00098                                      &buffer.pBuffer_);
00099       idToBuffer_.insert(std::make_pair(std::make_pair(edm::TypeIDBase(iType),std::string(iLabel)),buffer));
00100       itFound = idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType),
00101          std::string(iLabel)));
00102    }
00103    ROOT::Reflex::Type t = ROOT::Reflex::Type::ByTypeInfo(iType);
00104    ROOT::Reflex::Object o(t,const_cast<void*>(iData));
00105    ROOT::Reflex::Type trueType = o.DynamicType();
00106    assert(edm::TypeIDBase(trueType.TypeInfo())==itFound->second.trueType_);
00107    itFound->second.branch_->SetAddress(&(itFound->second.pBuffer_));
00108    itFound->second.pBuffer_ = iData;
00109 }
00110 
00111 //call update before calling write
00112 void 
00113 RecordWriter::fill(const edm::ESRecordAuxiliary& iValue)
00114 {
00115    for(std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator it=idToBuffer_.begin(),itEnd=idToBuffer_.end();
00116        it!=itEnd;++it) {
00117       if(0==it->second.pBuffer_) {
00118          throw cms::Exception("MissingESData")<<"The EventSetup data "<<it->first.first.name()<<" '"<<it->first.second<<"' was not supplied";
00119       }
00120    }
00121    
00122    aux_ = iValue;
00123    tree_->Fill();
00124    for(std::map<std::pair<edm::TypeIDBase,std::string>, DataBuffer>::iterator it=idToBuffer_.begin(),itEnd=idToBuffer_.end();
00125        it!=itEnd;++it) {
00126           it->second.pBuffer_=0;
00127    }
00128 }
00129 
00130 void
00131 RecordWriter::write()
00132 {
00133    tree_->Write();
00134 }
00135 //
00136 // const member functions
00137 //
00138 
00139 //
00140 // static member functions
00141 //