Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 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
11 #include "GaudiKernel/Bootstrap.h"
12 
13 // ROOT
14 #include "TROOT.h"
15 #include "RFileCnv.h"
16 #include "TFile.h"
17 
18 // local
20 
21 // Instantiation of a static factory class used by clients to create
22 // instances of this service
23 DECLARE_NAMESPACE_CONVERTER_FACTORY(RootHistCnv,RFileCnv)
24 
25 // Standard constructor
26 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc )
27 : RDirectoryCnv ( svc, classID() ),
28  m_compLevel ( "" )
29 { }
30 
31 //------------------------------------------------------------------------------
33 {
34  // Set compression level property ...
36  pmgr->declareProperty( "GlobalCompression", m_compLevel );
37  ISvcLocator * svcLoc = Gaudi::svcLocator();
38  SmartIF<IJobOptionsSvc> jobSvc =
39  svcLoc->service<IJobOptionsSvc>("JobOptionsSvc");
40  const StatusCode sc = ( jobSvc.isValid() &&
41  jobSvc->setMyProperties("RFileCnv",&*pmgr) );
42 
43  // initialise base class
44  return ( sc && RDirectoryCnv::initialize() );
45 }
46 //------------------------------------------------------------------------------
47 
48 //------------------------------------------------------------------------------
50  DataObject*& refpObject )
51 //------------------------------------------------------------------------------
52 {
53  MsgStream log(msgSvc(), "RFileCnv");
54  unsigned long* ipar = (unsigned long*)pAddress->ipar();
55  char mode[2] = { char(ipar[1]), 0 };
56 
57  std::string fname = pAddress->par()[0]; // Container name
58  std::string ooname = pAddress->par()[1]; // Object name
59 
60  const std::string* spar = pAddress->par();
61  // Strip of store name to get the top level RZ directory
62  std::string oname = spar[1].substr(spar[1].find("/",1)+1, spar[1].length());
63 
64  // Protect against multiple instances of TROOT
65  if ( 0 == gROOT ) {
66  static TROOT root("root","ROOT I/O");
67  // gDebug = 99;
68  } else {
69  log << MSG::VERBOSE << "ROOT already initialized, debug = "
70  << gDebug<< endmsg;
71  }
72 
73  // Determine access mode:
74 
75  if ( mode[0] == 'O' ) {
76 
77  if (findTFile(ooname,rfile).isFailure()) {
78 
79  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading"
80  << endmsg;
81 
82  rfile = TFile::Open(fname.c_str(),"READ");
83  if ( rfile != 0 && rfile->IsOpen() ) {
84  regTFile(ooname,rfile).ignore();
85 
86  ipar[0] = (unsigned long)rfile;
87  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
88  pFile->setOpen(true);
89  refpObject = pFile;
90 
91  return StatusCode::SUCCESS;
92 
93  } else {
94  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading"
95  << endmsg;
96  return StatusCode::FAILURE;
97  }
98 
99  } else {
100  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened"
101  << endmsg;
102  return StatusCode::SUCCESS;
103  }
104 
105 
106  } else if ( mode[0] == 'U' ) {
107 
108  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating"
109  << endmsg;
110 
111  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
112  exit(1);
113 
114  } else if ( mode[0] == 'N' ) {
115 
116  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
117  if ( !m_compLevel.empty() )
118  { log << ", CompressionLevel='" << m_compLevel << "'"; }
119  log << endmsg;
120 
121  rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
122  if ( ! ( rfile && rfile->IsOpen() ) ) {
123  log << MSG::ERROR << "Could not open file " << fname << " for writing"
124  << endmsg;
125  return StatusCode::FAILURE;
126  }
127  if ( !m_compLevel.empty() )
128  {
129  const RootCompressionSettings settings(m_compLevel);
130  rfile->SetCompressionSettings(settings.level());
131  }
132 
133  regTFile(ooname,rfile).ignore();
134 
135  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
136 
137  ipar[0] = (unsigned long)rfile;
138  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
139  pFile->setOpen(true);
140  refpObject = pFile;
141  return StatusCode::SUCCESS;
142 
143  } else {
144 
145  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
146  return StatusCode::FAILURE;
147 
148  }
149 
150  return StatusCode::FAILURE;
151 }
152 
153 //------------------------------------------------------------------------------
155  IOpaqueAddress*& refpAddress )
156 //------------------------------------------------------------------------------
157 {
158  refpAddress = pObject->registry()->address();
159  return RFileCnv::updateRep( refpAddress, pObject );
160 }
161 
162 //-----------------------------------------------------------------------------
164  DataObject* pObject )
165 //-----------------------------------------------------------------------------
166 {
167  MsgStream log(msgSvc(), "RFileCnv");
168  std::string ooname = pAddress->par()[1];
169 
170  NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
171  if ( pFile != 0 && pFile->isOpen() ) {
172 
173  unsigned long* ipar = (unsigned long*)pAddress->ipar();
174  if (findTFile(ooname,rfile).isFailure()) {
175  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
176  return StatusCode::FAILURE;
177  }
178 
179  rfile->Write(0,TObject::kOverwrite);
180  if ( log.level() <= MSG::INFO ) {
181  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
182  rfile->Print();
183  }
184  rfile->Close();
185  delete rfile;
186 
187  ipar[0] = 0;
188  pFile->setOpen(false);
189  return StatusCode::SUCCESS;
190 
191  } else {
192  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
193  }
194  return StatusCode::FAILURE;
195 }
196 
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
200 {
201 }

Generated at Wed Dec 4 2013 14:33:12 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004