Gaudi Framework, version v23r6

Home   Generated: Wed Jan 30 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RFileCnv.cpp
Go to the documentation of this file.
1 // $Id: RFileCnv.cpp,v 1.14 2007/03/15 15:53:15 hmd Exp $
2 #define ROOTHISTCNV_RFILECNV_CPP
3 
4 // Include files
9 
10 #include "TROOT.h"
11 #include "RFileCnv.h"
12 #include "TFile.h"
13 
14 // Instantiation of a static factory class used by clients to create
15 // instances of this service
16 DECLARE_NAMESPACE_CONVERTER_FACTORY(RootHistCnv,RFileCnv)
17 
18 //------------------------------------------------------------------------------
19 StatusCode RootHistCnv::RFileCnv::createObj(IOpaqueAddress* pAddress,
20  DataObject*& refpObject)
21 //------------------------------------------------------------------------------
22 {
23  MsgStream log(msgSvc(), "RFileCnv");
24  unsigned long* ipar = (unsigned long*)pAddress->ipar();
25  char mode[2] = { char(ipar[1]), 0 };
26 
27  std::string fname = pAddress->par()[0]; // Container name
28  std::string ooname = pAddress->par()[1]; // Object name
29 
30  const std::string* spar = pAddress->par();
31  // Strip of store name to get the top level RZ directory
32  std::string oname = spar[1].substr(spar[1].find("/",1)+1, spar[1].length());
33 
34  // Protect against multiple instances of TROOT
35  if ( 0 == gROOT ) {
36  static TROOT root("root","ROOT I/O");
37  // gDebug = 99;
38  } else {
39  log << MSG::VERBOSE << "ROOT already initialized, debug = "
40  << gDebug<< endmsg;
41  }
42 
43 
44  // Determine access mode:
45 
46  if ( mode[0] == 'O' ) {
47 
48  if (findTFile(ooname,rfile).isFailure()) {
49 
50  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading"
51  << endmsg;
52 
53  rfile = TFile::Open(fname.c_str(),"READ");
54  if ( rfile != 0 && rfile->IsOpen() ) {
55  regTFile(ooname,rfile).ignore();
56 
57  ipar[0] = (unsigned long)rfile;
58  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
59  pFile->setOpen(true);
60  refpObject = pFile;
61 
62  return StatusCode::SUCCESS;
63 
64  } else {
65  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading"
66  << endmsg;
67  return StatusCode::FAILURE;
68  }
69 
70  } else {
71  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened"
72  << endmsg;
73  return StatusCode::SUCCESS;
74  }
75 
76 
77  } else if ( mode[0] == 'U' ) {
78  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating"
79  << endmsg;
80 
81  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
82  exit(1);
83 
84 
85  } else if ( mode[0] == 'N' ) {
86  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing"
87  << endmsg;
88 
89 
90  rfile = TFile::Open(fname.c_str(),"RECREATE","Gaudi Trees");
91  if ( ! ( rfile && rfile->IsOpen() ) ) {
92  log << MSG::ERROR << "Could not open file " << fname << " for writing"
93  << endmsg;
94  return StatusCode::FAILURE;
95  }
96 
97  regTFile(ooname,rfile).ignore();
98 
99  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
100 
101  ipar[0] = (unsigned long)rfile;
102  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
103  pFile->setOpen(true);
104  refpObject = pFile;
105  return StatusCode::SUCCESS;
106 
107  } else {
108 
109  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
110  return StatusCode::FAILURE;
111 
112  }
113 
114  return StatusCode::FAILURE;
115 
116 
117  // TFile* rfile = TFile::Open(spar[0].c_str(),"RECREATE","Gaudi ROOT Ntuples");
118  // if (! rfile->IsOpen() ) {
119  // log << MSG::ERROR << "Could not open file " << spar[0] << " for writing" << endmsg;
120  // return StatusCode::FAILURE;
121  // }
122  // ipar[0] = (unsigned long)rfile;
123  // NTuple::File* pFile = new NTuple::File(objType(), spar[0], oname);
124  // pFile->setOpen(true);
125  // refpObject = pFile;
126  // return StatusCode::SUCCESS;
127 
128 }
129 
130 
131 //------------------------------------------------------------------------------
133  IOpaqueAddress*& refpAddress )
134 //------------------------------------------------------------------------------
135 {
136  refpAddress = pObject->registry()->address();
137  return RFileCnv::updateRep( refpAddress, pObject );
138 }
139 
140 //-----------------------------------------------------------------------------
142  DataObject* pObject )
143 //-----------------------------------------------------------------------------
144 {
145  MsgStream log(msgSvc(), "RFileCnv");
146  std::string ooname = pAddress->par()[1];
147 
148  NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
149  if ( pFile != 0 && pFile->isOpen() ) {
150 
151  unsigned long* ipar = (unsigned long*)pAddress->ipar();
152  if (findTFile(ooname,rfile).isFailure()) {
153  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
154  return StatusCode::FAILURE;
155  }
156 
157  rfile->Write(0,TObject::kOverwrite);
158  if ( log.level() <= MSG::INFO ) {
159  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
160  rfile->Print();
161  }
162  rfile->Close();
163  delete rfile;
164 
165  ipar[0] = 0;
166  pFile->setOpen(false);
167  return StatusCode::SUCCESS;
168 
169  } else {
170  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
171  }
172  return StatusCode::FAILURE;
173 }
174 
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
178 {
179 }

Generated at Wed Jan 30 2013 17:13:42 for Gaudi Framework, version v23r6 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004