DAS  3.0
Das Analysis System
FileUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exceptions.h"
4 #include "FriendUtils.h"
5 #include "Looper.h"
6 
7 #include <TChain.h>
8 #include <TFile.h>
9 #include <TH1.h>
10 #include <TTree.h>
11 
12 #include <filesystem>
13 #include <string>
14 #include <memory>
15 #include <vector>
16 
17 namespace Darwin::Tools {
18 
21 std::unique_ptr<TFile> GetOutput (std::filesystem::path output,
22  const std::string& name);
23 
30 void StandardInit ();
31 
35 std::vector<std::filesystem::path> GetROOTfiles
36  (std::vector<std::filesystem::path> inputs);
37 
40 std::unique_ptr<ChainSlice> GetChain
41  (std::vector<std::filesystem::path> inputs,
42  const Slice slice = {1, 0},
43  const std::string& name = "events");
44 
49 [[deprecated("Pass a slice as second parameter.")]]
50 inline std::unique_ptr<ChainSlice> GetChain
51  (std::vector<std::filesystem::path> inputs,
52  const char * name)
53 {
54  return GetChain(inputs, {1, 0}, name);
55 }
56 
59 template<typename THX = TH1>
60 inline std::unique_ptr<THX> GetHist
61  (std::vector<std::filesystem::path> inputs,
62  const std::string& name = "h")
63 {
64  using namespace std;
65  namespace fs = filesystem;
66 
67  unique_ptr<THX> sum;
69  for (auto const& input: inputs) {
70  auto fIn = make_unique<TFile>(input.c_str(), "READ");
72  unique_ptr<THX> h(fIn->Get<THX>(name.c_str()));
73  if (!h) {
74  namespace DE = Darwin::Exceptions;
75  BOOST_THROW_EXCEPTION(DE::BadInput(Form("`%s` cannot be found in (one of) the "
76  " file(s).", name.c_str()), fIn));
77  }
78  if (sum)
79  sum->Add(h.get());
80  else {
81  sum = std::move(h);
82  sum->SetDirectory(nullptr);
83  }
84  }
85  return sum;
86 }
87 
93 std::string GetFirstTreeLocation
94  (const std::filesystem::path& input);
95 
105 std::unique_ptr<TTree> NewTree (const std::unique_ptr<ChainSlice>& chain,
106  const bool makeFriend,
107  const std::string& thisFunc);
108 
119 template<typename T,
120  typename TTreePtr>
122  const TTreePtr& tIn,
123  const std::string& name,
124  T ** t,
125  const int steering = 0,
126  bool raise_exception = true)
127 {
128  using namespace std;
129  namespace DE = Darwin::Exceptions;
130 
131  if (steering & verbose)
132  cout << "Loading branch `" << name << "`" << endl;
133 
134  if (tIn->GetBranch(name.c_str()) == nullptr) {
135  if (raise_exception) {
136  string what = name + " branch could not be found";
137  BOOST_THROW_EXCEPTION( DE::BadInput(what.c_str(), *tIn));
138  }
139  else
140  return nullptr;
141  }
142 
143  *t = nullptr;
144  if (int err = tIn->SetBranchAddress(name.c_str(), t); err < 0) {
145  string what = name + " branch could not be set "
146  "(`SetBranchAddress()` returned " + to_string(err) + ")";
147  BOOST_THROW_EXCEPTION( DE::BadInput(what.c_str(), *tIn));
148  }
149 
150  return *t;
151 }
152 
160 template<typename T,
161  typename TTreePtr>
163  TTreePtr& tOut,
164  const std::string& name,
165  T ** t,
166  const int steering = 0)
167 {
168  using namespace std;
169 
170  *t = new T;
171  if (steering & verbose)
172  cout << "Setting up new branch for `" << name << "`" << endl;
173 
174  namespace DE = Darwin::Exceptions;
175  if (tOut->Branch(name.c_str(), t) == nullptr) {
176  namespace DE = Darwin::Exceptions;
177  string what = name + " branch could not be set up";
178  BOOST_THROW_EXCEPTION( DE::BadInput(what.c_str(), *tOut) );
179  }
180 
181  return *t;
182 }
183 
192 template<typename T,
193  typename TTreePtrIn,
194  typename TTreePtrOut>
196  const TTreePtrIn& tIn,
197  TTreePtrOut& tOut,
198  const std::string& name,
199  T ** t,
200  const int steering = 0,
201  bool raise_exception = true)
202 {
203  using namespace std;
204 
205  GetBranchReadOnly<T>(tIn, name, t, steering, raise_exception);
206 
207  if (steering & Friend) {
208  if (steering & verbose)
209  cout << "Setting up new branch for `" << name << "`" << endl;
210  if (tOut->Branch(name.c_str(), t) == nullptr) {
211  namespace DE = Darwin::Exceptions;
212  string what = name + " branch could not be set up";
213  BOOST_THROW_EXCEPTION( DE::BadInput(what.c_str(), *tOut) );
214  }
215  }
216 
217  return *t;
218 }
219 
220 } // namespace Darwin::Tools
221 
222 #define DT_GetOutput(output) Darwin::Tools::GetOutput(output, __func__)
223 #define DT_NewTree(input,makeFriend) Darwin::Tools::NewTree(input, makeFriend, __func__)
Darwin::Tools::GetFirstTreeLocation
std::string GetFirstTreeLocation(const std::filesystem::path &input)
one input ROOT file
Definition: FileUtils.cc:98
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::Friend
@ Friend
activate -F to only fill the new branches
Definition: Options.h:28
Darwin::Tools::GetChain
std::unique_ptr< ChainSlice > GetChain(std::vector< std::filesystem::path > inputs, const Slice slice={1, 0}, const std::string &name="events")
Load chain from a list of files.
Definition: FileUtils.cc:67
Darwin::Tools::GetBranchWriteOnly
T * GetBranchWriteOnly(TTreePtr &tOut, const std::string &name, T **t, const int steering=0)
Wrapper to initialise write-only branches.
Definition: FileUtils.h:162
Looper.h
Darwin::Tools::Slice
std::pair< unsigned, unsigned > Slice
current slice (>=0) / total number of slices (>0)
Definition: Looper.h:20
Darwin::Tools::StandardInit
void StandardInit()
Definition: FileUtils.cc:25
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:35
Darwin::Tools::NewTree
std::unique_ptr< TTree > NewTree(const std::unique_ptr< ChainSlice > &chain, const bool makeFriend, const std::string &thisFunc)
name of present exec
Definition: FileUtils.cc:137
Darwin::Tools::GetBranchReadOnly
T * GetBranchReadOnly(const TTreePtr &tIn, const std::string &name, T **t, const int steering=0, bool raise_exception=true)
Wrapper to initialise read-only branches.
Definition: FileUtils.h:121
Darwin::Tools::verbose
@ verbose
bit for debug mode (-v is always available)
Definition: Options.h:30
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
FriendUtils.h
Darwin::Tools::GetOutput
std::unique_ptr< TFile > GetOutput(std::filesystem::path output, const std::string &name)
Shortcut to create a reproducible output file (see ROOT Doxygen for details)
Definition: FileUtils.cc:13
Darwin::Tools
Classes and functions related to the framework.
Definition: Dict_rdict.cxx:990
Darwin::Tools::GetBranchReadWrite
T * GetBranchReadWrite(const TTreePtrIn &tIn, TTreePtrOut &tOut, const std::string &name, T **t, const int steering=0, bool raise_exception=true)
Wrapper to initialise read-write branches.
Definition: FileUtils.h:195
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:61
Darwin::Tools::GetROOTfiles
std::vector< std::filesystem::path > GetROOTfiles(std::vector< std::filesystem::path > inputs)
ROOT files or directories.
Definition: FileUtils.cc:33
jercExample.inputs
def inputs
Definition: jercExample.py:118
Darwin::Exceptions::BadInput
Generic exception for ill-defined input (before the event loop).
Definition: exceptions.h:68