DAS  3.0
Das Analysis System
FileUtils.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPLv3-or-later
2 //
3 // SPDX-FileCopyrightText: Patrick L.S. Connor <patrick.connor@desy.de>
4 // SPDX-FileCopyrightText: Louis Moureaux <louis.moureaux@cern.ch>
5 
6 #pragma once
7 
8 #include "exceptions.h"
9 
10 #include <TChain.h>
11 #include <TFile.h>
12 #include <TH1.h>
13 #include <TTree.h>
14 
15 #include <filesystem>
16 #include <string>
17 #include <memory>
18 #include <vector>
19 
20 namespace Darwin::Tools {
21 
25 [[deprecated("Use Darwin::Tools::Flow.")]]
26 std::unique_ptr<TFile> GetOutput (std::filesystem::path output,
27  const std::string& name);
28 
35 void StandardInit ();
36 
40 std::vector<std::filesystem::path> GetROOTfiles
41  (std::vector<std::filesystem::path> inputs);
42 
47 [[deprecated("Use Darwin::Tools::Flow::GetInputTree().")]]
48 std::unique_ptr<TChain> GetChain
49  (std::vector<std::filesystem::path> inputs,
50  const char * name = "events");
51 
54 template<typename THX = TH1>
55 inline std::unique_ptr<THX> GetHist
56  (std::vector<std::filesystem::path> inputs,
57  const std::string& name = "h")
58 {
59  using namespace std;
60  namespace fs = filesystem;
61 
62  unique_ptr<THX> sum;
64  for (auto const& input: inputs) {
65  auto fIn = make_unique<TFile>(input.c_str(), "READ");
67  unique_ptr<THX> h(fIn->Get<THX>(name.c_str()));
68  if (!h) {
69  namespace DE = Darwin::Exceptions;
70  BOOST_THROW_EXCEPTION(DE::BadInput(Form("`%s` cannot be found in (one of) the "
71  " file(s).", name.c_str()), fIn));
72  }
73  if (sum)
74  sum->Add(h.get());
75  else {
76  sum = std::move(h);
77  sum->SetDirectory(nullptr);
78  }
79  }
80  return sum;
81 }
82 
88 std::string GetFirstTreeLocation
89  (const std::filesystem::path& input);
90 
93 std::shared_ptr<TFile> GetOutputFile (const std::filesystem::path&,
94  const std::source_location = std::source_location::current());
95 
101 [[deprecated("Use Darwin::Tools::Flow::GetOutputTree().")]]
102 std::unique_ptr<TTree> NewTree (const std::unique_ptr<TChain>& chain,
103  const std::string& thisFunc);
104 
105 } // namespace Darwin::Tools
106 
107 #define DT_GetOutput(output) Darwin::Tools::GetOutput(output, __func__)
108 #define DT_NewTree(input,makeFriend) Darwin::Tools::NewTree(input, __func__)
Darwin::Tools::GetFirstTreeLocation
std::string GetFirstTreeLocation(const std::filesystem::path &input)
one input ROOT file
Definition: FileUtils.cc:89
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
exceptions.h
Darwin::Tools::GetChain
std::unique_ptr< TChain > GetChain(std::vector< std::filesystem::path > inputs, const char *name="events")
internal path to TTree
Definition: FileUtils.cc:68
Darwin::Tools::GetOutputFile
std::shared_ptr< TFile > GetOutputFile(const std::filesystem::path &, const std::source_location=std::source_location::current())
Shortcut to create a reproducible output file (see ROOT Doxygen for details)
Definition: FileUtils.cc:124
Darwin::Tools::StandardInit
void StandardInit()
Definition: FileUtils.cc:29
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:36
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.input
input
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:35
Darwin::Tools::GetOutput
std::unique_ptr< TFile > GetOutput(std::filesystem::path output, const std::string &name)
Definition: FileUtils.cc:17
Darwin::Tools
Classes and functions related to the framework.
Definition: Dict_rdict.cxx:990
Darwin::Tools::GetHist
std::unique_ptr< THX > GetHist(std::vector< std::filesystem::path > inputs, const std::string &name="h")
Load a histogram from a list of files.
Definition: FileUtils.h:56
Darwin::Tools::GetROOTfiles
std::vector< std::filesystem::path > GetROOTfiles(std::vector< std::filesystem::path > inputs)
ROOT files or directories.
Definition: FileUtils.cc:37
jercExample.inputs
def inputs
Definition: jercExample.py:118
Darwin::Tools::NewTree
std::unique_ptr< TTree > NewTree(const std::unique_ptr< TChain > &chain, const std::string &thisFunc)
name of present exec
Darwin::Exceptions::BadInput
Generic exception for ill-defined input (before the event loop).
Definition: exceptions.h:83