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 <TDirectory.h>
20#include <fmt/core.h>
21#include <string_view>
22
23namespace R3B::Neuland
24{
26 {
27 public:
28 explicit ParDirCreator(
29 FairParRootFileIo* parFileIO = dynamic_cast<FairParRootFileIo*>(FairRuntimeDb::instance()->getOutput()))
30 : file_io_{ parFileIO }
31 , old_dir_{ gDirectory }
32 {
33 if (file_io_ == nullptr)
34 {
35 throw R3B::runtime_error("unable to obtain the FairParRootFileIo object!");
36 }
37 par_root_file_ = parFileIO->getParRootFile();
38
39 if (!par_root_file_->IsOpen() || !par_root_file_->IsWritable())
40 {
41 throw R3B::runtime_error("parRootFile is either closed or unable to be written!");
42 }
43 }
44
45 ~ParDirCreator() noexcept { old_dir_->cd(); }
46 ParDirCreator(const ParDirCreator&) = delete;
48 auto operator=(const ParDirCreator&) -> ParDirCreator& = delete;
50
51 auto mkdir(std::string_view dir_name) -> auto*
52 {
53 if (par_root_file_->IsZombie())
54 {
56 fmt::format("File {} is not closed properly! Please delete it and create a new one!",
57 par_root_file_->GetName()));
58 }
59 par_root_file_->cd();
60 if (dir_name.empty())
61 {
62 throw R3B::runtime_error("Cannot create a folder with empty string!");
63 }
64 if (not gDirectory->IsWritable())
65 {
66 throw R3B::runtime_error(fmt::format("Directory {} is not writable currently!", gDirectory->GetName()));
67 }
68
69 // This is very slow
70 // // rm the dir and all its contents if existing
71 // if (auto* existing_dir = gDirectory->GetDirectory(dir_name.data()); existing_dir != nullptr)
72 // {
73 // R3BLOG(info, fmt::format("removing directory {} in {}", dir_name, gDirectory->GetName()));
74 // gDirectory->rmdir(dir_name.data());
75 // }
76 // else
77 // {
78 // R3BLOG(
79 // info,
80 // fmt::format("No directory {} in {}. Creating a new directory!", dir_name,
81 // gDirectory->GetName()));
82 // }
83
84 R3BLOG(info, fmt::format(R"("Creating a directory "{}" in "{}"")", dir_name, gDirectory->GetName()));
85 auto* new_dir = gDirectory->mkdir(dir_name.data(), dir_name.data(), true);
86 if (new_dir == nullptr)
87 {
88 throw R3B::runtime_error("Cannot create a directory for the histrogams!");
89 }
90 return new_dir;
91 }
92
93 private:
94 FairParRootFileIo* file_io_ = nullptr;
95 FairParRootFile* par_root_file_ = nullptr;
96 TDirectory* old_dir_ = nullptr;
97 };
98} // namespace R3B::Neuland
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
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.