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