DESY Hbb Analysis Framework
utilLib.h
Go to the documentation of this file.
1 
2 #ifndef Analysis_Tools_utilLib_h_
3 #define Analysis_Tools_utilLib_h_
4 
5 #include <sys/stat.h>
6 
7 #include <iostream>
8 #include <sstream>
9 #include <iomanip>
10 #include <string>
11 #include <cctype>
12 #include <algorithm>
13 #include "TFile.h"
14 #include "TTree.h"
15 
16 #include "RooFit.h"
17 #include "RooWorkspace.h"
18 
19 
20 //include boost
21 #include <boost/filesystem.hpp>
22 
23 template <typename T1, typename T2>
24 struct less_second {
25  typedef std::pair<T1, T2> type;
26  bool operator ()(type const& a, type const& b) const {
27  return a.second < b.second;
28  }
29 };
30 
31 template <typename T1, typename T2>
32 struct great_second {
33  typedef std::pair<T1, T2> type;
34  bool operator ()(type const& a, type const& b) const {
35  return a.second > b.second;
36  }
37 };
38 
39 template <typename T>
40 std::string to_string_with_precision(const T a_value, const int n = 6)
41 {
42  std::ostringstream out;
43  out << std::setprecision(n) << a_value;
44  return out.str();
45 }
46 
47 // Function to check whether file exists or not
48 inline bool file_exists(const std::string&name);
49 
50 bool findStrings(const std::string & input, const std::string & needful);
51 //Function to check whether root file isSombie or not
52 void CheckZombie(const TFile& name);
53 void CheckZombieObjectInTFile(const TFile& file, const std::string& name);
54 
55 //Function to get a Pointer to the workspace in the TFile
56 RooWorkspace* GetRooWorkspace(const std::string& path_to_file, const std::string& workspace_name = "workspace");
57 
58 int returnMassPoint(const std::string& name);
59 
60 //Function to check an output path and create a dir if doesn't exist
61 void CheckOutputDir(const std::string& oDir);
62 
63 inline bool file_exists(const std::string&name){
64  struct stat buffer;
65  return (stat (name.c_str(), &buffer) == 0);
66 }
67 
68 //Function to Get something from the TFile. Exception safety:
69 template <typename T> T* GetFromTFile(TFile& file, const std::string& obj_name){
70  auto *obj = static_cast<T*>(file.Get(obj_name.c_str()));
71  if(!obj) throw std::invalid_argument("Invalid TObject name: " + obj_name);
72  return obj;
73 }
74 template <typename T> T* GetFromTFile(const std::string& file_name, const std::string& obj_name){
75  TFile f(file_name.c_str(),"READ");
76  CheckZombie(f);
77  auto *obj = GetFromTFile<T>(f,obj_name);
78  return obj;
79 }
80 
81 #endif
T * GetFromTFile(TFile &file, const std::string &obj_name)
Definition: utilLib.h:69
int returnMassPoint(const std::string &name)
bool file_exists(const std::string &name)
Definition: utilLib.h:63
std::string to_string_with_precision(const T a_value, const int n=6)
Definition: utilLib.h:40
void CheckZombieObjectInTFile(const TFile &file, const std::string &name)
bool operator()(type const &a, type const &b) const
Definition: utilLib.h:26
bool findStrings(const std::string &input, const std::string &needful)
std::pair< T1, T2 > type
Definition: utilLib.h:33
void CheckOutputDir(const std::string &oDir)
float T
Definition: PlotsCompare.cc:18
TFile * f[10]
Definition: PlotsCompare.cc:24
RooWorkspace * GetRooWorkspace(const std::string &path_to_file, const std::string &workspace_name="workspace")
void CheckZombie(const TFile &name)
std::pair< T1, T2 > type
Definition: utilLib.h:25