00001 #define ROOTHISTCNV_RNTUPLECNV_CPP
00002
00003
00004
00005 #include "GaudiKernel/CnvFactory.h"
00006 #include "GaudiKernel/DataObject.h"
00007 #include "GaudiKernel/GenericLink.h"
00008 #include "GaudiKernel/IOpaqueAddress.h"
00009 #include "GaudiKernel/IDataProviderSvc.h"
00010 #include "GaudiKernel/INTupleSvc.h"
00011
00012 #include "GaudiKernel/MsgStream.h"
00013 #include "GaudiKernel/NTuple.h"
00014 #include "NTupleInfo.h"
00015 #include "RNTupleCnv.h"
00016
00017 #include <list>
00018 #include "TROOT.h"
00019 #include "TDirectory.h"
00020 #include "TFile.h"
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00032 RootHistCnv::RNTupleCnv::RNTupleCnv( ISvcLocator* svc, const CLID& clid )
00033 : RConverter(clid, svc), m_ntupleSvc(0) {
00034 }
00035
00036
00038 RootHistCnv::RNTupleCnv::~RNTupleCnv() {
00039 }
00040
00041
00043 StatusCode RootHistCnv::RNTupleCnv::initialize() {
00044 StatusCode status = Converter::initialize();
00045 if ( status.isSuccess() ) {
00046 status = serviceLocator()->getService( "NTupleSvc", IID_INTupleSvc, (IInterface*&)m_ntupleSvc);
00047 }
00048 return status;
00049 }
00050
00051
00053 StatusCode RootHistCnv::RNTupleCnv::finalize() {
00054 if ( m_ntupleSvc != 0 ) {
00055
00056 }
00057 m_ntupleSvc = 0;
00058 return Converter::finalize();
00059 }
00060
00061
00063 StatusCode RootHistCnv::RNTupleCnv::updateObj(IOpaqueAddress* pAddress, DataObject* pObject) {
00064 StatusCode status = StatusCode::FAILURE;
00065 try {
00066 GenericLinkBase::GENERIC* gen = pAddress->genericLink()->genericInfo();
00067 setDirectory(pAddress->containerName());
00068 status = readData(gen->m_info[0], dynamic_cast<INTuple*>(pObject), ++gen->m_info[1]);
00069 }
00070 catch (...) {
00071 }
00072 return status;
00073 }
00074
00075
00077 StatusCode RootHistCnv::RNTupleCnv::createObj(IOpaqueAddress* pAddress, DataObject*& refpObject) {
00078 long id = pAddress->genericLink()->genericInfo()->m_info[0];
00079 StatusCode status = readObject( pAddress->containerName(), id );
00080 if ( status.isSuccess() ) {
00081 MsgStream log(msgSvc(), "RNTupleCnv");
00082 INTuple* nt = 0;
00083
00084
00085 status = load(id, nt);
00086 refpObject = dynamic_cast<DataObject*>(nt);
00087 }
00088 return status;
00089 }
00090
00091
00093 StatusCode RootHistCnv::RNTupleCnv::createRep(DataObject* pObject, IOpaqueAddress*& pAddr) {
00094 MsgStream log (msgSvc(), "RNTupleCnv");
00095
00096
00097 pAddr = 0;
00098 try {
00099 long id = 0;
00100 const std::string& loc = pObject->location();
00101
00102 pAddr = pObject->address();
00103 if ( 0 == pAddr ) {
00104 char* stop = 0;
00105 const char* desc = pObject->localPath().data()+1;
00106 id = ::strtol(desc, &stop, 10);
00107 StatusCode status = createDirectory(loc);
00108 if ( !status.isSuccess() ) {
00109 return status;
00110 }
00111
00112 setDiskDirectory(loc);
00113 status = book(id, diskDirectory(loc), dynamic_cast<INTuple*>(pObject));
00114 if ( !status.isSuccess() ) {
00115 return status;
00116 }
00117 status = createAddress( pObject, id, pAddr );
00118 if ( !status.isSuccess() ) {
00119 return status;
00120 }
00121 }
00122
00123 id = pAddr->genericLink()->genericInfo()->m_info[0];
00124 return writeData(id, dynamic_cast<INTuple*>(pObject));
00125 }
00126 catch (...) {
00127 }
00128 return StatusCode::FAILURE;
00129 }
00130
00131
00133 StatusCode RootHistCnv::RNTupleCnv::updateRep(IOpaqueAddress* pAddress, DataObject* pObject) {
00134 if ( 0 != pAddress ) {
00135 MsgStream log(msgSvc(), "RNTupleCnv");
00136 long id = pAddress->genericLink()->genericInfo()->m_info[0];
00137
00138 std::string file = m_rootfile->GetName();
00139 file += ":/";
00140 gDirectory->cd(file.c_str());
00141
00142 std::string full = pObject->fullpath();
00143
00144
00145
00146
00147 return writeObject( pObject->location(), id );
00148 }
00149 return StatusCode::FAILURE;
00150 }
00151
00152
00154
00155 std::string RootHistCnv::RNTupleCnv::rootVarType(int type) {
00156
00157 std::string type_name;
00158
00159 switch( type ) {
00160 case DataTypeInfo::SHORT:
00161 type_name = "/S";
00162 break;
00163 case DataTypeInfo::INT:
00164 type_name = "/I";
00165 break;
00166 case DataTypeInfo::LONG:
00167 type_name = "/I";
00168 break;
00169 case DataTypeInfo::USHORT:
00170 type_name = "/s";
00171 break;
00172 case DataTypeInfo::UINT:
00173 type_name = "/i";
00174 break;
00175 case DataTypeInfo::ULONG:
00176 type_name = "/i";
00177 break;
00178 case DataTypeInfo::FLOAT:
00179 type_name = "/F";
00180 break;
00181 case DataTypeInfo::DOUBLE:
00182 type_name = "/D";
00183 break;
00184 default:
00185 break;
00186 }
00187
00188 return ( type_name );
00189
00190 }
00191
00192 bool RootHistCnv::parseName(std::string full, std::string &blk,
00193 std::string &var) {
00194 int sp;
00195 if ( (sp=full.find("/")) != -1 ) {
00196 blk = full.substr(0,sp);
00197 var = full.substr(sp+1,full.length());
00198 return true;
00199 } else {
00200 blk = "AUTO_BLK";
00201 var = full;
00202 return false;
00203 }
00204
00205 }
00206
00207
00209 #define INSTANTIATE(TYP) \
00210 template INTupleItem* createNTupleItem<TYP>(NTUPLEINFO& tags, long i, INTuple* tuple, TYP minimum, TYP maximum, long& size);
00211
00212 namespace RootHistCnv {
00213 template<class TYP>
00214 INTupleItem* createNTupleItem (NTUPLEINFO& tags, long i, INTuple* tuple, TYP minimum, TYP maximum, long& size) {
00215 size = 0;
00216 INTupleItem* col = 0;
00217 if ( tags.isRangeWithin(i, minimum,maximum)) {
00218 long ind = tags.getIndex(tags.index[i]);
00219 long dim1 = 1, dim2 = 1;
00220 TYP min, max, null;
00221 tags.getBounds(i, min, max, null);
00222 switch( tags.dim[i] ) {
00223 case 0:
00224 col = NTuple::_Item<TYP>::create (tuple, tags.name[i], min, max, null);
00225 break;
00226 case 1:
00227 dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
00228 col = NTuple::_Array<TYP>::create (tuple, tags.name[i], tags.index[i], dim1, min, max, null);\
00229 break;
00230 case 2:
00231 dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
00232 dim2 = (ind>=0) ? tags.theDim[i][0] : tags.theDim[i][1];
00233 col = NTuple::_Matrix<TYP>::create (tuple, tags.name[i],tags.index[i],dim1,dim2,min,max,null);\
00234 break;
00235 default:
00236 break;
00237 }
00238 size += dim1 * dim2 * ((sizeof(TYP) < 4) ? 4 : sizeof(TYP));
00239 }
00240 return col;
00241 }
00242
00243 INSTANTIATE(float)
00244 INSTANTIATE(double)
00245 INSTANTIATE(bool)
00246 INSTANTIATE(char)
00247 INSTANTIATE(int)
00248 INSTANTIATE(short)
00249 INSTANTIATE(long)
00250 INSTANTIATE(unsigned char)
00251 INSTANTIATE(unsigned int)
00252 INSTANTIATE(unsigned short)
00253 INSTANTIATE(unsigned long)
00254 };