ATLAS Offline Software
Loading...
Searching...
No Matches
EL::Detail::OutputStreamData Class Referencefinal

all data needed to manage a given output stream More...

#include <OutputStreamData.h>

Collaboration diagram for EL::Detail::OutputStreamData:

Public Member Functions

void testInvariant () const
 test the invariant of this object
 OutputStreamData (const std::string &val_fileName, const std::string &mode)
 open the given file and create an output stream for it
 OutputStreamData (std::unique_ptr< TFile > val_file)
 create this output stream for a pre-opened file
 OutputStreamData (std::unique_ptr< SH::DiskWriter > val_writer)
 create this output stream for a custom writer
const std::string & mainStreamName () const noexcept
 the name of the main stream
void setMainStreamName (const std::string &val_mainStreamName)
TFile * file () const noexcept
 the file we are writing to
void saveOutput ()
 write the list of output objects to disk and clear it
void close ()
 close this file
std::string finalFileName () const
 the final path of the file created
void addOutput (std::unique_ptr< TObject > outputObject)
 add the given output object to this stream
void addClone (const TObject &prototypeObject)
 add a clone of the given object to the output
TDirectory * makeDirectoryFor (std::string &name)
 make the directory for the object of the given name
TObject * getOutputHist (const std::string &name) const noexcept
 get the output histogram with the given name, or nullptr if there is no histogam with such a name
TTree * getOutputTree (const std::string &name) const noexcept
 get the output tree with the given name, or nullptr if there is no tree with such a name

Private Attributes

std::string m_mainStreamName
 the name of the main stream
std::unique_ptr< SH::DiskWriterm_writer
 the writer we use
std::vector< std::unique_ptr< TObject > > m_output
 the list of objects to write out at the end of job
std::unordered_map< std::string, TObject * > m_outputHistMap
 the output histogram map
std::unordered_map< std::string, TTree * > m_outputTreeMap
 the output tree map

Detailed Description

all data needed to manage a given output stream

Definition at line 29 of file OutputStreamData.h.

Constructor & Destructor Documentation

◆ OutputStreamData() [1/3]

EL::Detail::OutputStreamData::OutputStreamData ( const std::string & val_fileName,
const std::string & mode )
explicit

open the given file and create an output stream for it

Guarantee
strong
Failures
out of memory II

Definition at line 113 of file OutputStreamData.cxx.

115 : OutputStreamData (checkedOpenFile (val_fileName, mode))
116 {
117 // no invariant used
118 }
OutputStreamData(const std::string &val_fileName, const std::string &mode)
open the given file and create an output stream for it

◆ OutputStreamData() [2/3]

EL::Detail::OutputStreamData::OutputStreamData ( std::unique_ptr< TFile > val_file)
explicit

create this output stream for a pre-opened file

Guarantee
strong
Failures
out of memory II

Definition at line 122 of file OutputStreamData.cxx.

124 : OutputStreamData (std::make_unique<MyWriter> (std::move (file)))
125 {
126 // no invariant used
127 }
TFile * file() const noexcept
the file we are writing to

◆ OutputStreamData() [3/3]

EL::Detail::OutputStreamData::OutputStreamData ( std::unique_ptr< SH::DiskWriter > val_writer)
explicit

create this output stream for a custom writer

Guarantee
strong
Failures
out of memory II

Definition at line 131 of file OutputStreamData.cxx.

133 : m_writer (std::move (val_writer))
134 {
135 RCU_NEW_INVARIANT (this);
136 }
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
std::unique_ptr< SH::DiskWriter > m_writer
the writer we use

Member Function Documentation

◆ addClone()

void EL::Detail::OutputStreamData::addClone ( const TObject & prototypeObject)

add a clone of the given object to the output

Guarantee
basic
Failures
cloning failures
out of memory II

Definition at line 264 of file OutputStreamData.cxx.

266 {
267 // no invariant used
268
269 // Do not change the user's "current directory" during any of the
270 // following...
271 DirectoryReset dirReset;
272
273 // Make a clone of the object, and make sure we are already in
274 // the right directory if needed
275 std::string name = prototypeObject.GetName();
276 TDirectory *dir = makeDirectoryFor (name);
277 dir->cd();
278
279 std::unique_ptr<TObject> clone {prototypeObject.Clone()};
280
281 // Hold on to the pointer of the tree in our internal cache.
282 addOutput (std::move (clone));
283 }
TDirectory * makeDirectoryFor(std::string &name)
make the directory for the object of the given name
void addOutput(std::unique_ptr< TObject > outputObject)
add the given output object to this stream

◆ addOutput()

void EL::Detail::OutputStreamData::addOutput ( std::unique_ptr< TObject > outputObject)

add the given output object to this stream

While the caller transfers ownership to this object, he may retain a reference to the object until saveOutput or close is called.

Guarantee
basic
Failures
out of memory II

Definition at line 227 of file OutputStreamData.cxx.

229 {
231
232 TTree *const tree = dynamic_cast<TTree*> (outputObject.get());
233 if (tree)
234 {
235 std::string name = tree->GetName();
236 std::string treeName = tree->GetName();
237
238 TDirectory *dir = makeDirectoryFor (treeName);
239
240 // if we are in a sub-directory we need to rename the tree
241 if (name != treeName)
242 tree->SetName (treeName.c_str());
243
244 // pass ownership of the tree to the directory
245 tree->SetDirectory (dir);
246 outputObject.release();
247
249
250
251 } else
252 {
253 TH1 *const hist = dynamic_cast<TH1*> (outputObject.get());
254
255 m_outputHistMap[outputObject->GetName()] = outputObject.get();
256 m_output.emplace_back (std::move (outputObject));
257 if (hist)
258 hist->SetDirectory (nullptr);
259 }
260 }
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:219
std::unordered_map< std::string, TObject * > m_outputHistMap
the output histogram map
std::vector< std::unique_ptr< TObject > > m_output
the list of objects to write out at the end of job
std::unordered_map< std::string, TTree * > m_outputTreeMap
the output tree map
TChain * tree

◆ close()

void EL::Detail::OutputStreamData::close ( )

close this file

Guarantee
basic
Failures
i/o errors

Definition at line 171 of file OutputStreamData.cxx.

173 {
175 saveOutput ();
176 m_writer->close ();
177 }
void saveOutput()
write the list of output objects to disk and clear it

◆ file()

TFile * EL::Detail::OutputStreamData::file ( ) const
noexcept

the file we are writing to

Guarantee
no-fail
Postcondition
result != nullptr

Definition at line 160 of file OutputStreamData.cxx.

162 {
163 RCU_READ_INVARIANT (this);
164 TFile *result = m_writer->file();
165 RCU_PROVIDE (result != nullptr);
166 return result;
167 }
#define RCU_PROVIDE(x)
Definition Assert.h:203
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217

◆ finalFileName()

std::string EL::Detail::OutputStreamData::finalFileName ( ) const

the final path of the file created

Guarantee
strong
Failures
out of memory II

Definition at line 181 of file OutputStreamData.cxx.

183 {
184 RCU_READ_INVARIANT (this);
185 return m_writer->path ();
186 }

◆ getOutputHist()

TObject * EL::Detail::OutputStreamData::getOutputHist ( const std::string & name) const
noexcept

get the output histogram with the given name, or nullptr if there is no histogam with such a name

Guarantee
no-fail

Definition at line 316 of file OutputStreamData.cxx.

318 {
319 RCU_READ_INVARIANT (this);
320
321 auto iter = m_outputHistMap.find (name);
322 return iter != m_outputHistMap.end() ? iter->second : nullptr;
323 }

◆ getOutputTree()

TTree * EL::Detail::OutputStreamData::getOutputTree ( const std::string & name) const
noexcept

get the output tree with the given name, or nullptr if there is no tree with such a name

Guarantee
no-fail

Definition at line 327 of file OutputStreamData.cxx.

329 {
330 RCU_READ_INVARIANT (this);
331
332 auto iter = m_outputTreeMap.find (name);
333 return iter != m_outputTreeMap.end() ? iter->second : nullptr;
334 }

◆ mainStreamName()

const std::string & EL::Detail::OutputStreamData::mainStreamName ( ) const
noexcept

the name of the main stream

Some streams are aliases for other streams. This is the name of the main stream in this case, otherwise it is the name of this stream.

Definition at line 140 of file OutputStreamData.cxx.

142 {
143 RCU_READ_INVARIANT (this);
144 return m_mainStreamName;
145 }
std::string m_mainStreamName
the name of the main stream

◆ makeDirectoryFor()

TDirectory * EL::Detail::OutputStreamData::makeDirectoryFor ( std::string & name)

make the directory for the object of the given name

This will make sure that we pick the proper sub-directory if needed.

Guarantee
basic
Failures
directory creation failures

Definition at line 287 of file OutputStreamData.cxx.

289 {
291
292 TDirectory *result = file();
293 std::string::size_type split = name.rfind ("/");
294 if (split == std::string::npos)
295 {
296 return result;
297 } else
298 {
299 const std::string dirname = name.substr (0, split);
300 name = name.substr (split + 1);
301 TDirectory *subdir = dynamic_cast<TDirectory*>(result->Get (dirname.c_str()));
302 if (!subdir)
303 {
304 result->mkdir (dirname.c_str());
305 subdir = dynamic_cast<TDirectory*>(result->Get (dirname.c_str()));
306 RCU_ASSERT (subdir != nullptr);
307 }
308 result = subdir;
309 RCU_ASSERT (result != nullptr);
310 return result;
311 }
312 }
#define RCU_ASSERT(x)
Definition Assert.h:210
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
std::string dirname(std::string name)
Definition utils.cxx:200

◆ saveOutput()

void EL::Detail::OutputStreamData::saveOutput ( )

write the list of output objects to disk and clear it

Guarantee
basic
Failures
i/o errors

Definition at line 190 of file OutputStreamData.cxx.

192 {
194 TFile *const file {m_writer->file()};
195 RCU_ASSERT (file != nullptr);
196 for (std::unique_ptr<TObject>& object : m_output)
197 {
198 std::string name = object->GetName();
199 TDirectory *dir = makeDirectoryFor (name);
200 if (dir != file)
201 {
202 TNamed *named = dynamic_cast<TNamed*>(object.get());
203 if (named)
204 named->SetName (name.c_str());
205 }
206
207 if (RCU::SetDirectory (object.get(), dir))
208 {
209 // SetDirectory transferred ownership to the directory, so release
210 // it here to avoid a double delete.
211 //placate cppcheck using std::ignore
212 std::ignore = object.release();
213 } else
214 {
215 // WriteObject writes a copy and takes no ownership, so we keep
216 // owning the object and let the unique_ptr delete it below.
217 dir->WriteObject (object.get(), name.c_str());
218 }
219 }
220 m_outputHistMap.clear ();
221 m_outputTreeMap.clear ();
222 m_output.clear ();
223 }
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
bool SetDirectory(TObject *object, TDirectory *directory)
effects: set the directory this object is associated with returns: whether the object type actively k...
Definition RootUtils.cxx:25

◆ setMainStreamName()

void EL::Detail::OutputStreamData::setMainStreamName ( const std::string & val_mainStreamName)

Definition at line 149 of file OutputStreamData.cxx.

151 {
153 if (!m_mainStreamName.empty())
154 throw std::runtime_error ("main stream name already set");
155 m_mainStreamName = val_mainStreamName;
156 }

◆ testInvariant()

void EL::Detail::OutputStreamData::testInvariant ( ) const

test the invariant of this object

Definition at line 105 of file OutputStreamData.cxx.

107 {
108 RCU_INVARIANT (m_writer != nullptr);
109 }
#define RCU_INVARIANT(x)
Definition Assert.h:187

Member Data Documentation

◆ m_mainStreamName

std::string EL::Detail::OutputStreamData::m_mainStreamName
private

the name of the main stream

Definition at line 176 of file OutputStreamData.h.

◆ m_output

std::vector<std::unique_ptr<TObject> > EL::Detail::OutputStreamData::m_output
private

the list of objects to write out at the end of job

Definition at line 184 of file OutputStreamData.h.

◆ m_outputHistMap

std::unordered_map<std::string,TObject*> EL::Detail::OutputStreamData::m_outputHistMap
private

the output histogram map

Definition at line 188 of file OutputStreamData.h.

◆ m_outputTreeMap

std::unordered_map<std::string,TTree*> EL::Detail::OutputStreamData::m_outputTreeMap
private

the output tree map

Definition at line 192 of file OutputStreamData.h.

◆ m_writer

std::unique_ptr<SH::DiskWriter> EL::Detail::OutputStreamData::m_writer
private

the writer we use

Definition at line 180 of file OutputStreamData.h.


The documentation for this class was generated from the following files: