00001 #define ROOTHISTCNV_RRWNTUPLECNV_CPP
00002
00003 #define ALLOW_ALL_TYPES
00004
00005
00006 #include <cstdio>
00007 #include <map>
00008 #include <string>
00009
00010
00011 #include "GaudiKernel/xtoa.h"
00012 #include "GaudiKernel/CnvFactory.h"
00013 #include "GaudiKernel/GenericAddress.h"
00014 #include "GaudiKernel/INTupleSvc.h"
00015
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/NTuple.h"
00018
00019 #include "RRWNTupleCnv.h"
00020
00021 #include "TROOT.h"
00022 #include "TFile.h"
00023 #include "TTree.h"
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 static CnvFactory<RootHistCnv::RRWNTupleCnv> s_factory;
00037 const ICnvFactory& RootHistRRWNTupleCnvFactory = s_factory;
00038
00039
00041 StatusCode RootHistCnv::RRWNTupleCnv::book(long , const std::string& , INTuple* nt) {
00042 MsgStream log(msgSvc(), "RRWNTupleCnv");
00043
00044
00045 std::string rt_path, rt_fid, rt_id, rt_lid;
00046 TTree* rtree;
00047
00048 DataObject* pObj = dynamic_cast<DataObject*>(nt);
00049 if (pObj != 0) {
00050 rt_lid = pObj->localPath();
00051 rt_id = "t" + rt_lid.substr(1,rt_lid.length()-1);
00052 rt_fid = pObj->fullpath();
00053 rt_path = pObj->location();
00054 } else {
00055 log << MSG::ERROR << "dynamic cast failed" << endreq;
00056 return StatusCode::FAILURE;
00057 }
00058
00059 if ( findNTuple(rt_fid, rtree).isFailure() ) {
00060
00061 const INTuple::ItemContainer& items = nt->items();
00062
00063
00064
00065
00066
00067 rtree = new TTree(rt_id.c_str(),nt->title().c_str());
00068
00069
00070
00071
00072 regNTuple(rt_fid,rtree);
00073
00074
00075
00076 for (size_t length = items.size(), i = 0; i < length; i++ ) {
00077 std::string item_name = items[i]->name();
00078 std::string tag_name = item_name;
00079
00080
00081
00082 tag_name += rootVarType( items[i]->type() );
00083
00084
00085
00086 log << MSG::INFO << "ID " << rt_id << ": added branch: "
00087 << item_name << " tag: "
00088 << tag_name << endreq;
00089 rtree->Branch(item_name.c_str(), const_cast<void*>(items[i]->buffer()),
00090 tag_name.c_str());
00091
00092 }
00093
00094 log << MSG::INFO << "Booked TTree with ID:" << rt_id
00095 << " \"" << nt->title() << "\" in directory " << rt_path << endreq;
00096
00097
00098
00099 return StatusCode::SUCCESS;
00100 }
00101 log << MSG::ERROR << "ROOT TTree " << rt_fid << " already exists."
00102 << endreq;
00103 return StatusCode::FAILURE;
00104 }
00105
00106
00108 StatusCode RootHistCnv::RRWNTupleCnv::writeData(long , INTuple* nt) {
00109
00110 MsgStream log(msgSvc(), "RRWNTupleCnv");
00111
00112
00113 std::string rt_lid, rt_id, rt_fid, rt_path;
00114 TTree* rtree;
00115
00116 DataObject* pObj = dynamic_cast<DataObject*>(nt);
00117 if (pObj != 0) {
00118 rt_lid = pObj->localPath();
00119 rt_id = "t" + rt_lid.substr(1,rt_lid.length()-1);
00120 rt_fid = pObj->fullpath();
00121 rt_path = pObj->location();
00122 } else {
00123 log << MSG::ERROR << "dynamic cast failed" << endreq;
00124 return StatusCode::FAILURE;
00125 }
00126
00127 if ( findNTuple(rt_fid,rtree).isFailure() ) {
00128 log << MSG::ERROR << "tree ID: " << rt_fid << " not found in treemap"
00129 << endreq;
00130 return StatusCode::FAILURE;
00131 }
00132
00133
00134 rtree->Fill();
00135
00136
00137 nt->reset();
00138
00139
00140 return StatusCode::SUCCESS;
00141 }
00142
00143
00145 StatusCode RootHistCnv::RRWNTupleCnv::readData(long , INTuple* nt, long ) {
00146
00147 MsgStream log(msgSvc(), "RRWNTupleCnv");
00148
00149
00150 std::string rt_lid, rt_id, rt_fid, rt_path;
00151
00152 DataObject* pObj = dynamic_cast<DataObject*>(nt);
00153 if (pObj != 0) {
00154 rt_lid = pObj->localPath();
00155 rt_id = "t" + rt_lid.substr(1,rt_lid.length()-1);
00156 rt_fid = pObj->fullpath();
00157 rt_path = pObj->location();
00158 } else {
00159 log << MSG::ERROR << "dynamic cast failed" << endreq;
00160 return StatusCode::FAILURE;
00161 }
00162
00164
00165 return StatusCode::FAILURE;
00166
00167 }
00168
00169
00171 StatusCode RootHistCnv::RRWNTupleCnv::load(long , INTuple*& ) {
00172 MsgStream log(msgSvc(), "RRWNTupleCnv");
00173
00174
00176
00177 return StatusCode::FAILURE;
00178
00179 }