Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

RRWNTupleCnv.cpp

Go to the documentation of this file.
00001 #define ROOTHISTCNV_RRWNTUPLECNV_CPP
00002 
00003 #define ALLOW_ALL_TYPES
00004 
00005 // Compiler include files
00006 #include <cstdio>
00007 #include <map>
00008 #include <string>
00009 
00010 // Include files
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 // Implementation of class :  RootHistCnv::RRWNTupleCnv
00028 //
00029 // Author :                   Charles Leggett
00030 //
00031 //------------------------------------------------------------------------------
00032 
00033 
00034 // Instantiation of a static factory class used by clients to create
00035 // instances of this service
00036 static CnvFactory<RootHistCnv::RRWNTupleCnv> s_factory;
00037 const ICnvFactory& RootHistRRWNTupleCnvFactory = s_factory;
00038 
00039 
00041 StatusCode RootHistCnv::RRWNTupleCnv::book(long /* idh */, const std::string& /* loc */, INTuple* nt)  {
00042   MsgStream log(msgSvc(), "RRWNTupleCnv");
00043 //    log << MSG::INFO << "book(idh, loc, ntup)" << endreq;
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     // Book the tree
00065     //    rt_id = _itoa(idh, text, 10);
00066 
00067     rtree = new TTree(rt_id.c_str(),nt->title().c_str());
00068 //      log << MSG::DEBUG << "created tree id: " << rtree->GetName() 
00069 //      << "  title: "<< nt->title() << endreq;
00070 
00071     // Put it in the treemap
00072     regNTuple(rt_fid,rtree);
00073 //      log << MSG::DEBUG << "added to treemap: " << rt_fid << endreq;
00074 
00075     // Add the branches
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 //        log << MSG::DEBUG << "item: " << i << "  name: " << items[i]->name()
00080 //        << " typeName: " << items[i]->typeName() << endreq;
00081 
00082       tag_name += rootVarType( items[i]->type() );
00083 
00084 
00085       // add the branch
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     // Fill it
00098     //      rtree->Fill();
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 /* idh */, INTuple* nt)   {
00109 
00110   MsgStream log(msgSvc(), "RRWNTupleCnv");
00111 //    log << MSG::DEBUG << "writeData()" << endreq;
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   // Fill the tree;
00134   rtree->Fill();
00135 
00136   // Reset the NTuple
00137   nt->reset();
00138 
00139 
00140   return StatusCode::SUCCESS;
00141 }
00142 
00143 
00145 StatusCode RootHistCnv::RRWNTupleCnv::readData(long /* id */, INTuple* nt, long /* ievt */)    {
00146   
00147   MsgStream log(msgSvc(), "RRWNTupleCnv");
00148 //    log << MSG::INFO << "readData()" << endreq;
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 /* idh */, INTuple*& /* refpObject */)   {
00172   MsgStream log(msgSvc(), "RRWNTupleCnv");
00173 //    log << MSG::INFO << "load()" << endreq;
00174 
00176 
00177   return StatusCode::FAILURE;
00178 
00179 }

Generated at Wed Nov 21 12:20:49 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000