DAS  3.0
Das Analysis System
toolbox.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <vector>
5 
6 #include <TDirectory.h>
7 #include <TKey.h>
8 #include <TString.h>
9 
10 #include "Math/VectorUtil.h"
11 
13 
14 #include <boost/exception/all.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 
27 template<typename TTreePtr> bool branchExists (const TTreePtr& tree, TString brName)
28 {
29  auto brList = tree->GetListOfBranches();
30  bool brFound = false;
31  for (auto it = brList->begin(); it != brList->end(); ++it) {
32  TString name = (*it)->GetName();
33  if (name == brName) brFound = true;
34  }
35  return brFound;
36 }
37 
40 template<typename T> std::vector<T*> GetObjects (TDirectory * dir)
41 {
42  if (!dir)
43  BOOST_THROW_EXCEPTION( std::runtime_error("Trying to access inexistent `TDirectory`") );
44  std::vector<T*> objects;
45  for (const auto&& element: *(dir->GetListOfKeys())) {
46  auto key = dynamic_cast<TKey*>(element);
47  if (auto obj = dynamic_cast<T*>(key->ReadObj()); obj)
48  objects.push_back(obj);
49  }
50  return objects;
51 }
52 
55 inline TDirectory * GetDirectory (TDirectory * dir, const std::vector<const char *>& names)
56 {
57  for (const char * name: names) {
58  TDirectory * subdir = dir->GetDirectory(name);
59  if (!subdir)
60  BOOST_THROW_EXCEPTION( std::invalid_argument(
61  Form("`%s` could not be found in `%s`", name, dir->GetName())) );
62  dir = subdir;
63  }
64  return dir;
65 }
66 
67 using ROOT::Math::VectorUtil::DeltaPhi;
68 using ROOT::Math::VectorUtil::DeltaR;
69 
70 } // 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::PhysicsObject
Definition: PhysicsObject.h:38
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:55
DAS::pt_sort
bool pt_sort(const PhysicsObject &j1, const PhysicsObject &j2)
Definition: toolbox.h:18
PhysicsObject.h
DAS::PhysicsObject::CorrPt
float CorrPt(size_t i=0) const
corrected transverse momentum
Definition: PhysicsObject.h:52
DAS::branchExists
bool branchExists(const TTreePtr &tree, TString brName)
Definition: toolbox.h:27
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:40