| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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