33 class DirectoryReset {
38 ~DirectoryReset() { m_dir.cd(); }
46 struct MyWriter :
public SH::DiskWriter
52 std::unique_ptr<TFile> m_file;
54 explicit MyWriter (std::unique_ptr<TFile> val_file)
55 : m_file (std::move (val_file))
57 if (m_file ==
nullptr)
58 throw std::runtime_error (
"encountered null pointer for output file");
59 m_path = m_file->GetName();
64 if (m_file !=
nullptr)
68 std::string getPath ()
const
94 std::unique_ptr<TFile> checkedOpenFile (
const std::string& path,
const std::string& mode)
96 std::unique_ptr<TFile>
result (TFile::Open (
path.c_str(),
mode.c_str()));
97 if (result ==
nullptr)
98 throw std::runtime_error (
"failed to open file " + path +
" with mode " + mode);
105 void OutputStreamData ::
106 testInvariant ()
const
114 OutputStreamData (
const std::string& val_fileName,
const std::string& mode)
123 OutputStreamData (std::unique_ptr<TFile>
file)
132 OutputStreamData (std::unique_ptr<SH::DiskWriter> val_writer)
140 const std::string& OutputStreamData ::
141 mainStreamName ()
const noexcept
149 void OutputStreamData ::
150 setMainStreamName (
const std::string& val_mainStreamName)
154 throw std::runtime_error (
"main stream name already set");
160 TFile *OutputStreamData ::
161 file ()
const noexcept
171 void OutputStreamData ::
181 std::string OutputStreamData ::
182 finalFileName ()
const
190 void OutputStreamData ::
196 for (std::unique_ptr<TObject>&
object :
m_output)
198 std::string name =
object->GetName();
202 TNamed *named =
dynamic_cast<TNamed*
>(
object.get());
204 named->SetName (name.c_str());
212 std::ignore =
object.release();
217 dir->WriteObject (
object.
get(), name.c_str());
227 void OutputStreamData ::
228 addOutput (std::unique_ptr<TObject> outputObject)
232 TTree *
const tree =
dynamic_cast<TTree*
> (outputObject.get());
235 std::string name =
tree->GetName();
236 std::string treeName =
tree->GetName();
241 if (name != treeName)
242 tree->SetName (treeName.c_str());
245 tree->SetDirectory (dir);
246 outputObject.release();
253 TH1 *
const hist =
dynamic_cast<TH1*
> (outputObject.get());
256 m_output.emplace_back (std::move (outputObject));
258 hist->SetDirectory (
nullptr);
264 void OutputStreamData ::
265 addClone (
const TObject& prototypeObject)
271 DirectoryReset dirReset;
275 std::string name = prototypeObject.GetName();
279 std::unique_ptr<TObject> clone {prototypeObject.Clone()};
287 TDirectory *OutputStreamData ::
288 makeDirectoryFor (std::string& name)
292 TDirectory *result =
file();
293 std::string::size_type
split = name.rfind (
"/");
294 if (
split == std::string::npos)
300 name = name.substr (
split + 1);
301 TDirectory *subdir =
dynamic_cast<TDirectory*
>(result->Get (
dirname.c_str()));
304 result->mkdir (
dirname.c_str());
305 subdir =
dynamic_cast<TDirectory*
>(result->Get (
dirname.c_str()));
316 TObject *OutputStreamData ::
317 getOutputHist (
const std::string& name)
const noexcept
327 TTree *OutputStreamData ::
328 getOutputTree (
const std::string& name)
const noexcept
#define RCU_CHANGE_INVARIANT(x)
#define RCU_REQUIRE2_SOFT(x, y)
#define RCU_NEW_INVARIANT(x)
#define RCU_READ_INVARIANT(x)
std::unordered_map< std::string, TObject * > m_outputHistMap
the output histogram map
TFile * file() const noexcept
the file we are writing to
TDirectory * makeDirectoryFor(std::string &name)
make the directory for the object of the given name
OutputStreamData(const std::string &val_fileName, const std::string &mode)
open the given file and create an output stream for it
void saveOutput()
write the list of output objects to disk and clear it
void addOutput(std::unique_ptr< TObject > outputObject)
add the given output object to this stream
std::vector< std::unique_ptr< TObject > > m_output
the list of objects to write out at the end of job
std::string m_mainStreamName
the name of the main stream
std::unique_ptr< SH::DiskWriter > m_writer
the writer we use
std::unordered_map< std::string, TTree * > m_outputTreeMap
the output tree map
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
This module defines the arguments passed from the BATCH driver to the BATCH worker.
bool SetDirectory(TObject *object, TDirectory *directory)
effects: set the directory this object is associated with returns: whether the object type actively k...
path
python interpreter configuration --------------------------------------—
std::string dirname(std::string name)