Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:21

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWCondLite
0004 // Class  :     RecordWriter
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:
0010 //         Created:  Wed Dec  9 17:01:03 CST 2009
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 #include "TFile.h"
0016 #include "TTree.h"
0017 #include "TBranch.h"
0018 #include "FWCore/Reflection/interface/TypeWithDict.h"
0019 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0020 
0021 // user include files
0022 #include "PhysicsTools/CondLiteIO/interface/RecordWriter.h"
0023 #include "DataFormats/FWLite/interface/format_type_name.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025 
0026 using namespace fwlite;
0027 //
0028 // constants, enums and typedefs
0029 //
0030 
0031 //
0032 // static data member definitions
0033 //
0034 
0035 //
0036 // constructors and destructor
0037 //
0038 RecordWriter::RecordWriter(const char* iName, TFile* iFile) : pAux_(&aux_) {
0039   tree_ = new TTree(fwlite::format_type_to_mangled(iName).c_str(), "Holds data for an EventSetup Record");
0040   tree_->SetDirectory(iFile);
0041 
0042   auxBranch_ = tree_->Branch("ESRecordAuxiliary", "edm::ESRecordAuxiliary", &pAux_);
0043 }
0044 
0045 // RecordWriter::RecordWriter(const RecordWriter& rhs)
0046 // {
0047 //    // do actual copying here;
0048 // }
0049 
0050 RecordWriter::~RecordWriter() {}
0051 
0052 //
0053 // assignment operators
0054 //
0055 // const RecordWriter& RecordWriter::operator=(const RecordWriter& rhs)
0056 // {
0057 //   //An exception safe implementation is
0058 //   RecordWriter temp(rhs);
0059 //   swap(rhs);
0060 //
0061 //   return *this;
0062 // }
0063 
0064 //
0065 // member functions
0066 //
0067 void RecordWriter::update(const void* iData, const std::type_info& iType, const char* iLabel) {
0068   const char* label = iLabel;
0069   if (nullptr == iLabel) {
0070     label = "";
0071   }
0072   std::map<std::pair<edm::TypeIDBase, std::string>, DataBuffer>::iterator itFound =
0073       idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType), std::string(iLabel)));
0074   if (itFound == idToBuffer_.end()) {
0075     //first request
0076     DataBuffer buffer;
0077     buffer.pBuffer_ = iData;
0078     edm::TypeWithDict t(iType);
0079     assert(bool(t));
0080 
0081     std::string className = t.name();
0082 
0083     //now find actual type
0084     edm::ObjectWithDict o(t, const_cast<void*>(iData));
0085     edm::TypeWithDict trueType(o.dynamicType());
0086     buffer.trueType_ = edm::TypeIDBase(trueType.typeInfo());
0087     std::string trueClassName = trueType.name();
0088 
0089     buffer.branch_ = tree_->Branch(
0090         (fwlite::format_type_to_mangled(className) + "__" + label).c_str(), trueClassName.c_str(), &buffer.pBuffer_);
0091     idToBuffer_.insert(std::make_pair(std::make_pair(edm::TypeIDBase(iType), std::string(iLabel)), buffer));
0092     itFound = idToBuffer_.find(std::make_pair(edm::TypeIDBase(iType), std::string(iLabel)));
0093   }
0094   edm::TypeWithDict t(iType);
0095   edm::ObjectWithDict o(t, const_cast<void*>(iData));
0096   edm::TypeWithDict trueType(o.dynamicType());
0097   assert(edm::TypeIDBase(trueType.typeInfo()) == itFound->second.trueType_);
0098   itFound->second.branch_->SetAddress(&(itFound->second.pBuffer_));
0099   itFound->second.pBuffer_ = iData;
0100 }
0101 
0102 //call update before calling write
0103 void RecordWriter::fill(const edm::ESRecordAuxiliary& iValue) {
0104   for (std::map<std::pair<edm::TypeIDBase, std::string>, DataBuffer>::iterator it = idToBuffer_.begin(),
0105                                                                                itEnd = idToBuffer_.end();
0106        it != itEnd;
0107        ++it) {
0108     if (nullptr == it->second.pBuffer_) {
0109       throw cms::Exception("MissingESData")
0110           << "The EventSetup data " << it->first.first.name() << " '" << it->first.second << "' was not supplied";
0111     }
0112   }
0113 
0114   aux_ = iValue;
0115   tree_->Fill();
0116   for (std::map<std::pair<edm::TypeIDBase, std::string>, DataBuffer>::iterator it = idToBuffer_.begin(),
0117                                                                                itEnd = idToBuffer_.end();
0118        it != itEnd;
0119        ++it) {
0120     it->second.pBuffer_ = nullptr;
0121   }
0122 }
0123 
0124 void RecordWriter::write() { tree_->Write(); }
0125 //
0126 // const member functions
0127 //
0128 
0129 //
0130 // static member functions
0131 //