R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandParDirCreator.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2023 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#pragma once
15#include <FairParRootFileIo.h>
16#include <FairRuntimeDb.h>
17#include <R3BException.h>
18#include <R3BLogger.h>
19#include <fmt/format.h>
20
21namespace R3B::Neuland
22{
24 {
25 public:
26 explicit ParDirCreator(
27 FairParRootFileIo* parFileIO = dynamic_cast<FairParRootFileIo*>(FairRuntimeDb::instance()->getOutput()))
28 : file_io_{ parFileIO }
29 , old_dir_{ gDirectory }
30 {
31 if (file_io_ == nullptr)
32 {
33 throw R3B::runtime_error("unable to obtain the FairParRootFileIo object!");
34 }
35 par_root_file_ = parFileIO->getParRootFile();
36
37 if (!par_root_file_->IsOpen() || !par_root_file_->IsWritable())
38 {
39 throw R3B::runtime_error("parRootFile is either closed or unable to be written!");
40 }
41 }
42
43 ~ParDirCreator() noexcept { old_dir_->cd(); }
44 ParDirCreator(const ParDirCreator&) = delete;
46 auto operator=(const ParDirCreator&) -> ParDirCreator& = delete;
48
49 auto mkdir(std::string_view dir_name) -> auto*
50 {
51 if (par_root_file_->IsZombie())
52 {
54 fmt::format("File {} is not closed properly! Please delete it and create a new one!",
55 par_root_file_->GetName()));
56 }
57 par_root_file_->cd();
58 if (dir_name.empty())
59 {
60 throw R3B::runtime_error("Cannot create a folder with empty string!");
61 }
62 if (not gDirectory->IsWritable())
63 {
64 throw R3B::runtime_error(fmt::format("Directory {} is not writable currently!", gDirectory->GetName()));
65 }
66
67 // This is very slow
68 // // rm the dir and all its contents if existing
69 // if (auto* existing_dir = gDirectory->GetDirectory(dir_name.data()); existing_dir != nullptr)
70 // {
71 // R3BLOG(info, fmt::format("removing directory {} in {}", dir_name, gDirectory->GetName()));
72 // gDirectory->rmdir(dir_name.data());
73 // }
74 // else
75 // {
76 // R3BLOG(
77 // info,
78 // fmt::format("No directory {} in {}. Creating a new directory!", dir_name,
79 // gDirectory->GetName()));
80 // }
81
82 R3BLOG(info, fmt::format(R"("Creating a directory "{}" in "{}"")", dir_name, gDirectory->GetName()));
83 auto* new_dir = gDirectory->mkdir(dir_name.data(), dir_name.data(), true);
84 if (new_dir == nullptr)
85 {
86 throw R3B::runtime_error("Cannot create a directory for the histrogams!");
87 }
88 return new_dir;
89 }
90
91 private:
92 FairParRootFileIo* file_io_ = nullptr;
93 FairParRootFile* par_root_file_ = nullptr;
94 TDirectory* old_dir_ = nullptr;
95 };
96} // namespace R3B::Neuland
#define R3BLOG(severity, x)
Definition R3BLogger.h:35
ParDirCreator(const ParDirCreator &)=delete
auto operator=(ParDirCreator &&) -> ParDirCreator &=delete
auto mkdir(std::string_view dir_name) -> auto *
ParDirCreator(ParDirCreator &&)=delete
auto operator=(const ParDirCreator &) -> ParDirCreator &=delete
ParDirCreator(FairParRootFileIo *parFileIO=dynamic_cast< FairParRootFileIo * >(FairRuntimeDb::instance() ->getOutput()))
Simulation of NeuLAND Bar/Paddle.