00001 #include "UserDataCnv.h"
00002 #include "PerBaseEvent/HeaderObjectCnv.h"
00003 #include "RootIOSvc/RootIOUserData.h"
00004 #include "RootIOSvc/RootIOUserDataProxy.h"
00005
00006 using namespace DayaBay;
00007 using namespace std;
00008
00009 UserDataCnv::UserDataCnv(ISvcLocator* svc)
00010 : RootIOTypedCnv<PerHeaderObject,UserDataHeader>("PerHeaderObject",
00011 classID(),svc)
00012 {
00013 }
00014
00015 UserDataCnv::~UserDataCnv()
00016 {
00017 }
00018
00019
00020 StatusCode UserDataCnv::PerToTran(const PerHeaderObject& perobj,
00021 DayaBay::UserDataHeader& tranobj)
00022 {
00023 MsgStream log(msgSvc(), "UserDataCnv::PerToTran");
00024 StatusCode sc = HeaderObjectCnv::toTran(perobj,tranobj);
00025 if(sc.isFailure()) return sc;
00026
00027
00028 tranobj.clearData();
00029
00030 RootIOUserData ud;
00031 RootIOUserData::ProxyCollection& proxies = ud.input(m_ris->path());
00032 RootIOUserData::ProxyCollection::iterator pit, pdone = proxies.end();
00033 log << MSG::DEBUG
00034 << "got " << proxies.size() << " proxies from "
00035 << m_ris->path() << " for input" << endreq;
00036
00037 for (pit = proxies.begin(); pit != pdone; ++pit) {
00038 RootIOUserDataProxy* proxy = pit->second;
00039
00040 const std::string& typeName = proxy->cppType();
00041 const std::string& varName = proxy->varName();
00042
00043
00044 UserData* userdata = 0;
00045 if (typeName == "int") {
00046 userdata = new UserInt(varName, proxy->value<int>());
00047 }
00048 else if (typeName == "float") {
00049 userdata = new UserFloat(varName, proxy->value<float>());
00050 }
00051 else if (typeName == "vector<int>") {
00052 userdata = new UserIntArray(varName, proxy->value< std::vector<int> >());
00053 }
00054 else if (typeName == "vector<float>") {
00055 userdata = new UserFloatArray(varName, proxy->value< std::vector<float> >());
00056 }
00057 else {
00058 log << MSG::ERROR
00059 << "got unknown POD type: \"" << typeName
00060 << "\" for user data \"" << varName
00061 << "\" skipping."
00062 << endreq;
00063 return StatusCode::FAILURE;
00064 }
00065 tranobj.data().push_back(userdata);
00066 }
00067 return StatusCode::SUCCESS;
00068 }
00069
00070 StatusCode UserDataCnv::TranToPer(const DayaBay::UserDataHeader& udh,
00071 PerHeaderObject& perobj)
00072 {
00073 MsgStream log(msgSvc(), "UserDataCnv::TranToPer");
00074 std::string tespath = udh.registry()->identifier();
00075
00076 RootIOUserData ud;
00077 RootIOUserData::ProxyCollection& proxies = ud.output(tespath);
00078
00079 log << MSG::DEBUG
00080 << "got " << proxies.size() << " proxies from "
00081 << tespath << " for output" << endreq;
00082
00083 StatusCode sc;
00084
00085 if (!proxies.size()) {
00086 log << MSG::DEBUG << "No proxies for "
00087 << tespath << " initializing" << endreq;
00088 sc = this->InitializeProxies(udh, proxies);
00089 if (sc.isFailure()) return sc;
00090 }
00091
00092 sc = HeaderObjectCnv::toPer(udh,perobj);
00093 if (sc.isFailure()) return sc;
00094
00095 const UserDataHeader::UserDataList& datalist = udh.data();
00096
00097 for (size_t ind=0; ind < datalist.size(); ++ind) {
00098 const UserData* data = datalist[ind];
00099
00100 const std::string& varName = data->name();
00101
00102 RootIOUserDataProxy* proxy = proxies[varName];
00103
00104 if (!proxy) {
00105 log << MSG::ERROR << "Failed to find proxy: " << varName
00106 << " "
00107 << "Make sure you set all user data variables for every event!"
00108 << endreq;
00109 return StatusCode::FAILURE;
00110 }
00111
00112 const std::string& cppType = proxy->cppType();
00113
00114 log << MSG::DEBUG
00115 << "Proxy: name:" << varName << " and type:" << cppType
00116 << endreq;
00117
00118 switch (data->type()) {
00119
00120 case UserData::Float:
00121 proxy->value<float>() = dynamic_cast<const UserFloat*>(data)->value();
00122 break;
00123
00124 case UserData::Int:
00125 proxy->value<int>() = dynamic_cast<const UserInt*>(data)->value();
00126 break;
00127
00128
00129 case UserData::FloatArray:
00130 proxy->value< vector<float> >() = dynamic_cast<const UserFloatArray*>(data)->value();
00131 break;
00132
00133 case UserData::IntArray:
00134 proxy->value< vector<int> >() = dynamic_cast<const UserIntArray*>(data)->value();
00135 break;
00136
00137 default:
00138 log << MSG::ERROR
00139 << "unknown type: " << data->type()
00140 << " for user data \"" << varName << "\""
00141 << endreq;
00142 return StatusCode::FAILURE;
00143 break;
00144 }
00145 }
00146
00147 return StatusCode::SUCCESS;
00148 }
00149
00150 StatusCode UserDataCnv::fillRepRefs(IOpaqueAddress*, DataObject* dobj) {
00151
00152 MsgStream log(msgSvc(), "UserDataCnv::fillRepRefs");
00153 UserDataHeader* header = dynamic_cast<UserDataHeader*>(dobj);
00154
00155 log << MSG::DEBUG
00156 << "Saving links to " << header->inputHeaders().size()
00157 << " input headers" << endreq;
00158
00159 StatusCode sc = HeaderObjectCnv::fillPer(m_rioSvc,*header,*m_perOutObj);
00160
00161 if (sc.isFailure()) {
00162 log << MSG::ERROR << "Failed to fill HeaderObject part" << endreq;
00163 return sc;
00164 }
00165
00166 return sc;
00167 }
00168
00169 StatusCode UserDataCnv::fillObjRefs(IOpaqueAddress*, DataObject* dobj) {
00170 MsgStream log(msgSvc(), "UserDataCnv::fillObjRefs");
00171 HeaderObject* hobj = dynamic_cast<HeaderObject*>(dobj);
00172
00173 StatusCode sc = HeaderObjectCnv::fillTran(m_rioSvc,*m_perInObj,*hobj);
00174 if (sc.isFailure()) {
00175 log << MSG::ERROR << "Failed to fill HeaderObject part" << endreq;
00176 return sc;
00177 }
00178
00179 log << MSG::DEBUG
00180 << "Restored links to " << hobj->inputHeaders().size()
00181 << " input headers" << endreq;
00182
00183 return sc;
00184 }
00185
00186
00187 StatusCode UserDataCnv::InitializeProxies(const DayaBay::UserDataHeader& udh,
00188 RootIOUserData::ProxyCollection& proxies)
00189 {
00190
00191 MsgStream log(msgSvc(), "UserDataCnv::InitializeProxies");
00192 log << MSG::DEBUG << "Initializing dynamic branches" << endreq;
00193
00194 const UserDataHeader::UserDataList& datalist = udh.data();
00195
00196 for (size_t ind=0; ind < datalist.size(); ++ind) {
00197 const UserData* userdata = datalist[ind];
00198
00199 const std::string& varName = userdata->name();
00200
00201 log << MSG::DEBUG
00202 << "\tprocessing user data: " << varName
00203 << " type: " << userdata->type()
00204 << endreq;
00205
00206 RootIOUserDataProxy* proxy = 0;
00207 switch (userdata->type()) {
00208
00209 case UserData::Int:
00210 proxy = new RootIOUserDataProxy(varName,"int",
00211 (char*)new int);
00212 break;
00213
00214 case UserData::Float:
00215 proxy = new RootIOUserDataProxy(varName,"float",
00216 (char*)new float);
00217 break;
00218
00219 case UserData::IntArray:
00220 proxy = new RootIOUserDataProxy(varName,"vector<int>",
00221 (char*)new vector<int>);
00222 break;
00223
00224 case UserData::FloatArray:
00225 proxy = new RootIOUserDataProxy(varName,"vector<float>",
00226 (char*)new vector<float>);
00227 break;
00228
00229 default:
00230 log << MSG::ERROR << " unknown type: " << userdata->type() << endreq;
00231 return StatusCode::FAILURE;
00232 break;
00233 }
00234 proxies[varName] = proxy;
00235 }
00236 return StatusCode::SUCCESS;
00237 }