RDFAnalysis  0.1.1
Physics analysis with ROOT::RDataFrame
Helpers.h
Go to the documentation of this file.
1 #ifndef RDFAnalysis_Helpers_H
2 #define RDFAnalysis_Helpers_H
3 
4 #include <type_traits>
5 #include <TDirectory.h>
6 #include <ROOT/RDataFrame.hxx>
7 #include <random>
8 
14 namespace RDFAnalysis {
21  template <typename Iterator>
22  class range_t {
23  public:
25  using itr_t = Iterator;
27  range_t(Iterator begin, Iterator end) :
28  m_begin(begin),
29  m_end(end) {}
30 
32  Iterator begin() const { return m_begin; }
34  Iterator end() const { return m_end; }
36  std::size_t size() const { return std::distance(begin(), end() ); }
37  private:
38  const Iterator m_begin;
39  const Iterator m_end;
40  };
41 
43  template <typename Container>
44  auto as_range(Container& container) {
45  return range_t<std::decay_t<decltype(container.begin() )>>(
46  std::begin(container),
47  std::end(container) );
48  }
49 
51  template <typename Container>
52  auto as_range(const Container& container) {
53  return range_t<std::decay_t<decltype(container.begin() )>>(
54  std::begin(container),
55  std::end(container) );
56  }
57 
65  inline TDirectory* getMkdir(
66  TDirectory* dir,
67  const std::string& name,
68  bool doThrow = true)
69  {
70  TDirectory* newDir = dir->GetDirectory(name.c_str() );
71  if (newDir)
72  return newDir;
73  newDir = dir->mkdir(name.c_str() );
74  if (!newDir && doThrow)
75  throw std::runtime_error("Failed to get/make directory " + name);
76  // When making a hierarchy mkdir has an unexpected return type (the root of
77  // the new directories, not the bottom step so we have to return the right
78  // thing
79  return dir->GetDirectory(name.c_str() );
80  }
81 
93  template <typename Map>
94  typename Map::mapped_type getDefaultKey(
95  const Map& theMap,
96  const typename Map::key_type& key,
97  const typename Map::key_type& defaultKey)
98  {
99  auto itr = theMap.find(key);
100  if (itr == theMap.end() )
101  return theMap.at(defaultKey);
102  else
103  return itr->second;
104  }
105 
107  inline unsigned int getNSlots() {
108  unsigned int poolSize = ROOT::GetImplicitMTPoolSize();
109  return poolSize == 0 ? 1 : poolSize;
110  }
111 
113  template <typename F, typename T>
114  using enable_ifn_string_t = std::enable_if_t<!std::is_convertible<F, std::string>{}, T>;
115 
116  /* /// Apply is C++17... */
117  /* template <typename F, typename... Ts, std::size_t... Is> */
118  /* constexpr decltype(auto) apply_impl(F&& f, std::tuple<Ts...>&& args, std::index_sequence<Is...>) */
119  /* { */
120  /* return f(std::get<Is>(args)...); */
121  /* } */
122  /* template <typename F, typename...Ts> */
123  /* constexpr decltype(auto) apply(F&& f, std::tuple<Ts...>&& args) */
124  /* { */
125  /* return apply_impl(std::forward<F&&>(f), std::forward<std::tuple<Ts...>&&>(args), std::make_index_sequence<sizeof...(Ts)>()); */
126  /* } */
127 
129  std::string uniqueBranchName(const std::string& stub = "GenBranch");
130 
131  template <typename F>
132  struct is_std_function : public std::false_type {};
133 
134  template <typename R, typename... Ts>
135  struct is_std_function<std::function<R(Ts...)>> : public std::true_type {};
136 
137 }; //> end namespace RDFAnalysis
138 
139 #endif //> !RDFAnalysis_Helpers_H
Map::mapped_type getDefaultKey(const Map &theMap, const typename Map::key_type &key, const typename Map::key_type &defaultKey)
Get a value by key, defaulting to a backup key if it is not there.
Definition: Helpers.h:94
Helper class to allow iterating through a container without allowing users to modify that container...
Definition: Helpers.h:22
Iterator itr_t
The templated iterator type.
Definition: Helpers.h:25
unsigned int getNSlots()
Get the number of slots used in this session.
Definition: Helpers.h:107
std::enable_if_t<!std::is_convertible< F, std::string >{}, T > enable_ifn_string_t
Reduce size of enable_if statements.
Definition: Helpers.h:114
Definition: CutflowDetail.h:11
auto as_range(Container &container)
Make a range_t from a container.
Definition: Helpers.h:44
Iterator end() const
The end of the range.
Definition: Helpers.h:34
Iterator begin() const
The start of the range.
Definition: Helpers.h:32
range_t(Iterator begin, Iterator end)
Construct the range from two iterators.
Definition: Helpers.h:27
Definition: Helpers.h:132
std::size_t size() const
The size of the wrapped range.
Definition: Helpers.h:36
TDirectory * getMkdir(TDirectory *dir, const std::string &name, bool doThrow=true)
Get a directory, making it if it isn&#39;t there already.
Definition: Helpers.h:65
std::string uniqueBranchName(const std::string &stub="GenBranch")
Could perhaps be more natural in the IBranchNamer?
Definition: Helpers.cxx:4