ATLAS Offline Software
Public Member Functions | Private Attributes | List of all members
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 More...
 
 OutputStreamData (const std::string &val_fileName, const std::string &mode)
 open the given file and create an output stream for it More...
 
 OutputStreamData (std::unique_ptr< TFile > val_file)
 create this output stream for a pre-opened file More...
 
 OutputStreamData (std::unique_ptr< SH::DiskWriter > val_writer)
 create this output stream for a custom writer More...
 
const std::string & mainStreamName () const noexcept
 the name of the main stream More...
 
void setMainStreamName (const std::string &val_mainStreamName)
 
TFile * file () const noexcept
 the file we are writing to More...
 
void saveOutput ()
 write the list of output objects to disk and clear it More...
 
void close ()
 close this file More...
 
std::string finalFileName () const
 the final path of the file created More...
 
void addOutput (std::unique_ptr< TObject > outputObject)
 add the given output object to this stream More...
 
void addClone (const TObject &prototypeObject)
 add a clone of the given object to the output More...
 
TDirectory * makeDirectoryFor (std::string &name)
 make the directory for the object of the given name More...
 
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 More...
 
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 More...
 

Private Attributes

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

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 114 of file OutputStreamData.cxx.

116  : OutputStreamData (checkedOpenFile (val_fileName, mode))
117  {
118  // no invariant used
119  }

◆ 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 123 of file OutputStreamData.cxx.

125  : OutputStreamData (std::make_unique<MyWriter> (std::move (file)))
126  {
127  // no invariant used
128  }

◆ 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 132 of file OutputStreamData.cxx.

134  : m_writer (std::move (val_writer))
135  {
136  RCU_NEW_INVARIANT (this);
137  }

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 260 of file OutputStreamData.cxx.

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

◆ 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 223 of file OutputStreamData.cxx.

225  {
226  RCU_CHANGE_INVARIANT (this);
227 
228  TTree *const tree = dynamic_cast<TTree*> (outputObject.get());
229  if (tree)
230  {
231  std::string name = tree->GetName();
232  std::string treeName = tree->GetName();
233 
234  TDirectory *dir = makeDirectoryFor (treeName);
235 
236  // if we are in a sub-directory we need to rename the tree
237  if (name != treeName)
238  tree->SetName (treeName.c_str());
239 
240  // pass ownership of the tree to the directory
241  tree->SetDirectory (dir);
242  outputObject.release();
243 
245 
246 
247  } else
248  {
249  TH1 *const hist = dynamic_cast<TH1*> (outputObject.get());
250 
251  m_outputHistMap[outputObject->GetName()] = outputObject.get();
252  m_output.emplace_back (std::move (outputObject));
253  if (hist)
254  hist->SetDirectory (nullptr);
255  }
256  }

◆ close()

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

close this file

Guarantee
basic
Failures
i/o errors

Definition at line 172 of file OutputStreamData.cxx.

174  {
175  RCU_CHANGE_INVARIANT (this);
176  saveOutput ();
177  m_writer->close ();
178  }

◆ file()

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

the file we are writing to

Guarantee
no-fail
Postcondition
result != nullptr

Definition at line 161 of file OutputStreamData.cxx.

163  {
164  RCU_READ_INVARIANT (this);
165  TFile *result = m_writer->file();
166  RCU_PROVIDE (result != nullptr);
167  return result;
168  }

◆ 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 182 of file OutputStreamData.cxx.

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

◆ 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 312 of file OutputStreamData.cxx.

314  {
315  RCU_READ_INVARIANT (this);
316 
317  auto iter = m_outputHistMap.find (name);
318  return iter != m_outputHistMap.end() ? iter->second : nullptr;
319  }

◆ 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 323 of file OutputStreamData.cxx.

325  {
326  RCU_READ_INVARIANT (this);
327 
328  auto iter = m_outputTreeMap.find (name);
329  return iter != m_outputTreeMap.end() ? iter->second : nullptr;
330  }

◆ 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 141 of file OutputStreamData.cxx.

143  {
144  RCU_READ_INVARIANT (this);
145  return m_mainStreamName;
146  }

◆ 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 283 of file OutputStreamData.cxx.

285  {
286  RCU_CHANGE_INVARIANT (this);
287 
288  TDirectory *result = file();
289  std::string::size_type split = name.rfind ("/");
290  if (split == std::string::npos)
291  {
292  return result;
293  } else
294  {
295  const std::string dirname = name.substr (0, split);
296  name = name.substr (split + 1);
297  TDirectory *subdir = dynamic_cast<TDirectory*>(result->Get (dirname.c_str()));
298  if (!subdir)
299  {
300  result->mkdir (dirname.c_str());
301  subdir = dynamic_cast<TDirectory*>(result->Get (dirname.c_str()));
302  RCU_ASSERT (subdir != nullptr);
303  }
304  result = subdir;
305  RCU_ASSERT (result != nullptr);
306  return result;
307  }
308  }

◆ 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 191 of file OutputStreamData.cxx.

193  {
194  RCU_CHANGE_INVARIANT (this);
195  TFile *const file {m_writer->file()};
196  RCU_ASSERT (file != nullptr);
197  for (std::unique_ptr<TObject>& object : m_output)
198  {
199  std::string name = object->GetName();
200  TDirectory *dir = makeDirectoryFor (name);
201  if (dir != file)
202  {
203  TNamed *named = dynamic_cast<TNamed*>(object.get());
204  if (named)
205  named->SetName (name.c_str());
206  }
207 
208  if (!RCU::SetDirectory (object.get(), dir))
209  {
210  dir->WriteObject (object.get(), name.c_str());
211  }
212  //release object which was consumed by SetDirectory or WriteObject
213  //placate cppcheck using std::ignore
214  std::ignore = object.release();
215  }
216  m_outputHistMap.clear ();
217  m_outputTreeMap.clear ();
218  m_output.clear ();
219  }

◆ setMainStreamName()

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

Definition at line 150 of file OutputStreamData.cxx.

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

◆ 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 (this != nullptr);
109  RCU_INVARIANT (m_writer != nullptr);
110  }

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:
EL::Detail::OutputStreamData::addOutput
void addOutput(std::unique_ptr< TObject > outputObject)
add the given output object to this stream
Definition: OutputStreamData.cxx:224
EL::Detail::OutputStreamData::m_mainStreamName
std::string m_mainStreamName
the name of the main stream
Definition: OutputStreamData.h:176
EL::Detail::OutputStreamData::file
TFile * file() const noexcept
the file we are writing to
Definition: OutputStreamData.cxx:162
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
EL::Detail::OutputStreamData::m_output
std::vector< std::unique_ptr< TObject > > m_output
the list of objects to write out at the end of job
Definition: OutputStreamData.h:184
get_generator_info.result
result
Definition: get_generator_info.py:21
plotmaker.hist
hist
Definition: plotmaker.py:148
tree
TChain * tree
Definition: tile_monitor.h:30
dirname
std::string dirname(std::string name)
Definition: utils.cxx:200
EL::Detail::OutputStreamData::m_writer
std::unique_ptr< SH::DiskWriter > m_writer
the writer we use
Definition: OutputStreamData.h:180
python.Utilities.clone
clone
Definition: Utilities.py:134
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:58
RCU_PROVIDE
#define RCU_PROVIDE(x)
Definition: Assert.h:215
EL::Detail::OutputStreamData::m_outputTreeMap
std::unordered_map< std::string, TTree * > m_outputTreeMap
the output tree map
Definition: OutputStreamData.h:192
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:19
Preparation.mode
mode
Definition: Preparation.py:107
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
beamspotman.dir
string dir
Definition: beamspotman.py:621
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
RCU::SetDirectory
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:28
EL::Detail::OutputStreamData::makeDirectoryFor
TDirectory * makeDirectoryFor(std::string &name)
make the directory for the object of the given name
Definition: OutputStreamData.cxx:284
EL::Detail::OutputStreamData::m_outputHistMap
std::unordered_map< std::string, TObject * > m_outputHistMap
the output histogram map
Definition: OutputStreamData.h:188
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
EL::Detail::OutputStreamData::saveOutput
void saveOutput()
write the list of output objects to disk and clear it
Definition: OutputStreamData.cxx:192
python.LumiCalcRecover.subdir
subdir
Definition: LumiCalcRecover.py:25
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
EL::Detail::OutputStreamData::OutputStreamData
OutputStreamData(const std::string &val_fileName, const std::string &mode)
open the given file and create an output stream for it
Definition: OutputStreamData.cxx:115
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233