DAS  3.0
Das Analysis System
toolbox.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <TDirectory.h>
6 #include <TKey.h>
7 #include <TString.h>
8 
9 #include "Math/VectorUtil.h"
10 
12 
13 #include <boost/exception/all.hpp>
14 #include <boost/iterator/counting_iterator.hpp>
15 
16 namespace DAS {
17 
18 inline bool pt_sort (const PhysicsObject& j1, const PhysicsObject& j2)
19 {
20  return j1.CorrPt() > j2.CorrPt();
21 }
22 
24  default_def = 0b00,
25  logarithmic = 0b01,
26  step = 0b10
27 };
28 
33 template<typename T = double> std::vector<T> MakeBinning (
34  std::pair<T, T> range,
35  double param,
37 )
38 {
39  using namespace std;
40 
41  if (range.second <= range.first)
42  BOOST_THROW_EXCEPTION(invalid_argument("Invalid binning range"));
43  if (param <= 0)
44  BOOST_THROW_EXCEPTION(invalid_argument("Invalid binning parameter"));
45  if ((opt & BinningOptions::logarithmic) && range.first <= 0)
46  BOOST_THROW_EXCEPTION(invalid_argument("Logarithmic binning requires range.first > 0"));
47  if ((opt & BinningOptions::logarithmic) && (opt & BinningOptions::step) && param <= 1)
48  BOOST_THROW_EXCEPTION(invalid_argument("Log-step binning requires param > 1"));
49 
50  if (opt & BinningOptions::logarithmic) {
51  range.first = log10(range.first);
52  range.second = log10(range.second);
53  if (opt & BinningOptions::step) param = log10(param);
54  }
55 
56  double w = opt & BinningOptions::step ? param : (range.second-range.first)/param;
58  using boost::counting_iterator;
59  vector<T> edges(counting_iterator<T>(range.first /w),
60  counting_iterator<T>(range.second/w+1));
61  ranges::for_each(edges, [w](T& edge) { edge *= w; });
63  ranges::for_each(edges, [](T& edge) { edge = pow(10,edge); });
64 
65  return edges;
66 }
67 
72 template<typename TTreePtr>
73 [[ deprecated("Use Darwin::Tools::Flow()") ]]
74 bool branchExists (const TTreePtr& tree, TString brName)
75 {
76  auto brList = tree->GetListOfBranches();
77  bool brFound = false;
78  for (auto it = brList->begin(); it != brList->end(); ++it) {
79  TString name = (*it)->GetName();
80  if (name == brName) brFound = true;
81  }
82  return brFound;
83 }
84 
87 template<typename T> std::vector<T*> GetObjects (TDirectory * dir)
88 {
89  if (!dir)
90  BOOST_THROW_EXCEPTION( std::runtime_error("Trying to access inexistent `TDirectory`") );
91  std::vector<T*> objects;
92  for (const auto&& element: *(dir->GetListOfKeys())) {
93  auto key = dynamic_cast<TKey*>(element);
94  if (auto obj = dynamic_cast<T*>(key->ReadObj()); obj)
95  objects.push_back(obj);
96  }
97  return objects;
98 }
99 
102 inline TDirectory * GetDirectory (TDirectory * dir, const std::vector<const char *>& names)
103 {
104  for (const char * name: names) {
105  TDirectory * subdir = dir->GetDirectory(name);
106  if (!subdir)
107  BOOST_THROW_EXCEPTION( std::invalid_argument(
108  Form("`%s` could not be found in `%s`", name, dir->GetName())) );
109  dir = subdir;
110  }
111  return dir;
112 }
113 
114 using ROOT::Math::VectorUtil::DeltaPhi;
115 using ROOT::Math::VectorUtil::DeltaR;
116 
117 } // end of DAS namespace
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
DAS
Definition: applyBTagSF.cc:31
DAS::logarithmic
@ logarithmic
log with param as number of bins
Definition: toolbox.h:25
DAS::default_def
@ default_def
linear with param as number of bins
Definition: toolbox.h:24
DAS::step
@ step
linear with param as step for binning
Definition: toolbox.h:26
DAS::PhysicsObject
Definition: PhysicsObject.h:41
jercExample.key
string key
Definition: jercExample.py:109
DAS::GetDirectory
TDirectory * GetDirectory(TDirectory *dir, const std::vector< const char * > &names)
Get ((...)sub)subdirectory of dir.
Definition: toolbox.h:102
DAS::pt_sort
bool pt_sort(const PhysicsObject &j1, const PhysicsObject &j2)
Definition: toolbox.h:18
PhysicsObject.h
DAS::BinningOptions
BinningOptions
Definition: toolbox.h:23
DAS::PhysicsObject::CorrPt
float CorrPt(size_t i) const
corrected transverse momentum
Definition: PhysicsObject.h:55
DAS::MakeBinning
std::vector< T > MakeBinning(std::pair< T, T > range, double param, int opt=BinningOptions::default_def)
Define a regular binning.
Definition: toolbox.h:33
DAS::branchExists
bool branchExists(const TTreePtr &tree, TString brName)
Definition: toolbox.h:74
DAS::GetObjects
std::vector< T * > GetObjects(TDirectory *dir)
Find all objects of type T directly in a TDirectory (i.e. not recursive)
Definition: toolbox.h:87
metPhiCorrectionExample.range
range
Definition: metPhiCorrectionExample.py:100