DAS  3.0
Das Analysis System
FileUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exceptions.h"
4 
5 #include <TChain.h>
6 #include <TFile.h>
7 #include <TH1.h>
8 #include <TTree.h>
9 
10 #include <filesystem>
11 #include <string>
12 #include <memory>
13 #include <vector>
14 
15 namespace Darwin::Tools {
16 
19 std::unique_ptr<TFile> GetOutput (std::filesystem::path output,
20  const char * name);
21 
28 void StandardInit ();
29 
33 std::vector<std::filesystem::path> GetROOTfiles
34  (std::vector<std::filesystem::path> inputs);
35 
38 std::unique_ptr<TChain> GetChain
39  (std::vector<std::filesystem::path> inputs,
40  const char * name = "events");
41 
44 template<typename THX = TH1>
45 inline std::unique_ptr<THX> GetHist
46  (std::vector<std::filesystem::path> inputs,
47  const char * name = "h")
48 {
49  using namespace std;
50  namespace fs = filesystem;
51 
52  unique_ptr<THX> sum;
54  for (auto const& input: inputs) {
55  auto fIn = make_unique<TFile>(input.c_str(), "READ");
56  // TODO: what if the file had already been opened?? will it be closed prematurely?
57  unique_ptr<THX> h(fIn->Get<THX>(name));
58  if (!h) {
59  namespace DE = Darwin::Exceptions;
60  BOOST_THROW_EXCEPTION(DE::BadInput(Form("`%s` cannot be found in (one of) the "
61  " file(s).", name), fIn));
62  }
63  if (sum)
64  sum->Add(h.get());
65  else {
66  sum = move(h);
67  sum->SetDirectory(nullptr);
68  }
69  }
70  return sum;
71 }
72 
78 std::string GetFirstTreeLocation
79  (const std::filesystem::path& input);
80 
90 std::unique_ptr<TTree> NewTree (const std::unique_ptr<TChain>& chain,
91  const bool makeFriend,
92  const char * thisFunc);
93 
94 } // namespace Darwin::Tools
95 
96 #define DT_GetOutput(output) Darwin::Tools::GetOutput(output, __func__)
97 #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:91
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")
Load chain from a list of files.
Definition: FileUtils.cc:67
Darwin::Tools::GetOutput
std::unique_ptr< TFile > GetOutput(std::filesystem::path output, const char *name)
Shortcut to create a reproducible output file (see ROOT Doxygen for details)
Definition: FileUtils.cc:13
Darwin::Tools::StandardInit
void StandardInit()
Definition: FileUtils.cc:25
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:34
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
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
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::Tools::NewTree
std::unique_ptr< TTree > NewTree(const std::unique_ptr< TChain > &chain, const bool makeFriend, const char *thisFunc)
name of present exec
Definition: FileUtils.cc:130
Darwin::Tools::GetHist
std::unique_ptr< THX > GetHist(std::vector< std::filesystem::path > inputs, const char *name="h")
Load a histogram from a list of files.
Definition: FileUtils.h:46
Darwin::Exceptions::BadInput
Generic exception for ill-defined input (before the event loop).
Definition: exceptions.h:68