DESY Hbb Analysis Framework
RooFitUtils.h
Go to the documentation of this file.
1 /*
2  * RooFitUtils.h
3  *
4  * @created on: 19 Apr 2017
5  * @author: shevchen
6  * @description:
7  * Lib for working with RooFit classes.
8  */
9 
10 #ifndef ANALYSIS_TOOLS_ROOFITUTILS_H_
11 #define ANALYSIS_TOOLS_ROOFITUTILS_H_
12 
13 #include <string>
14 #include <exception>
15 #include <iostream>
16 
17 #include "RooWorkspace.h"
18 #include "RooRealVar.h"
19 #include "RooAbsPdf.h"
20 #include "RooFormulaVar.h"
21 #include "RooDataSet.h"
22 #include "RooAbsData.h"
23 #include "RooPlot.h"
24 #include "RooCurve.h"
25 #include "RooHist.h"
26 
28 
29 namespace analysis {
30 namespace tools {
31 
32 template<typename T, typename std::enable_if< std::is_base_of<RooCurve, T>::value ||
33  std::is_base_of<RooHist, T>::value, T >::type* = nullptr>
34  T* GetFromRooPlot(RooPlot& frame, const std::string& object_name){
35  /*
36  * Function to get Object of type T from RooPlot
37  */
38  T* foo = static_cast<T*>(frame.findObject(object_name.c_str(),T::Class()));
39  if(!foo) throw std::invalid_argument("Invalid Object name: " + object_name);
40  return foo;
41 }
42 
43 template<typename T> T* GetFromRooWorkspace(RooWorkspace& w, const std::string& name){
44  /*
45  * Function to get a RooFit object from a RooWorkspace
46  * Generalisation of a template function
47  */
48  T *var = static_cast<T*>(w.obj(name.c_str()));
49  if(!var) throw std::invalid_argument("Invalid TObject name: " + name);
50  return var;
51 }
52 
53 //template<typename T> T* GetRooObjectFromTFile(const std::string& file_name)
54 template<typename T> T* GetRooObjectFromTFile( TFile& file, const std::string& obj_name, const std::string& workspace_name = "workspace"){
55  /*
56  * Function to get RooObject from TFile through the interaction with RooWorkspace
57  */
58  auto &workspace = *GetFromTFile<RooWorkspace>(file, workspace_name); //Get RooWorkspace
59  auto *obj = GetFromRooWorkspace<T>(workspace,obj_name); //Get an object
60  return obj;
61 }
62 
63 template<typename T> T* GetRooObjectFromTFile( const std::string& file_name, const std::string& obj_name, const std::string& workspace_name = "workspace"){
64  /*
65  * Function to get RooObject from TFile through the interaction with RooWorkspace
66  */
67  auto &workspace = *GetFromTFile<RooWorkspace>(file_name, workspace_name); //Get RooWorkspace
68  auto *obj = GetFromRooWorkspace<T>(workspace,obj_name); //Get an object
69  return obj;
70 }
71 
72 }
73 } /* namespace analysis */
74 
75 
76 
77 #endif /* ANALYSIS_TOOLS_INTERFACE_ROOFITUTILS_H_ */
T * GetFromRooPlot(RooPlot &frame, const std::string &object_name)
Definition: RooFitUtils.h:34
T * GetRooObjectFromTFile(TFile &file, const std::string &obj_name, const std::string &workspace_name="workspace")
Definition: RooFitUtils.h:54
T * GetFromRooWorkspace(RooWorkspace &w, const std::string &name)
Definition: RooFitUtils.h:43
float T
Definition: PlotsCompare.cc:18