NAGASH 0.9.8
Next Generation Analysis System
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
TFileHelper.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Tool.h"
9#include "NAGASH/Global.h"
10#include "NAGASH/MapTool.h"
11
12namespace NAGASH
13{
18 class TFileHelper : public Tool
19 {
20 public:
24 TFileHelper(std::shared_ptr<MSGTool> msg, const std::string &name)
25 : Tool(msg)
26 {
27 TH1::AddDirectory(kFALSE);
28 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
29 infile = new TFile(name.c_str(), "READ");
30 if (infile == nullptr)
31 {
32 MSGUser()->MSG_ERROR("can't open ROOT file ", name);
33 return;
34 }
35
36 if (infile->IsZombie())
37 {
38 MSGUser()->MSG_ERROR("can't open ROOT file ", name);
39 return;
40 }
41
42 isopen = true;
43 if (isopen)
44 {
45 TIter next(infile->GetListOfKeys());
46 TKey *key;
47
48 while ((key = (TKey *)next()))
49 {
50 if (TString(key->GetClassName()).Contains("TH1") ||
51 TString(key->GetClassName()).Contains("TH2") ||
52 TString(key->GetClassName()).Contains("TH3") ||
53 TString(key->GetClassName()).Contains("THn") ||
54 TString(key->GetClassName()).Contains("TProfile"))
55 {
56 std::unique_ptr<TObject> obj(key->ReadObj());
57 map_name_hist.emplace(std::pair<std::string, TKey *>(obj->GetName(), key));
58 }
59
60 if (TString(key->GetClassName()) == "TTree")
61 {
62 std::unique_ptr<TObject> obj(key->ReadObj());
63 map_name_tree.emplace(std::pair<std::string, TKey *>(obj->GetName(), key));
64 }
65
66 if (TString(key->GetClassName()).Contains("TGraph"))
67 {
68 std::unique_ptr<TObject> obj(key->ReadObj());
69 map_name_graph.emplace(std::pair<std::string, TKey *>(obj->GetName(), key));
70 }
71 }
72 }
73
74 MapTool tool(MSGUser());
76 }
77
80 TH1 *Get(const std::string &name)
81 {
82 if (!isopen)
83 {
84 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
85 MSGUser()->MSG_ERROR("file not open");
86 return nullptr;
87 }
88 else
89 {
90 auto findresult = map_name_hist.find(name);
91 if (findresult != map_name_hist.end())
92 return (TH1 *)findresult->second->ReadObj();
93 else
94 {
95 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
96 MSGUser()->MSG_ERROR("hist ", name, " not found");
97 return nullptr;
98 }
99 }
100 }
101
104 TTree *GetTree(const std::string &name)
105 {
106
107 if (!isopen)
108 {
109 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
110 MSGUser()->MSG_ERROR("file not open");
111 return nullptr;
112 }
113 else
114 {
115 auto findresult = map_name_tree.find(name);
116 if (findresult != map_name_tree.end())
117 return (TTree *)findresult->second->ReadObj();
118 else
119 {
120 // auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
121 // MSGUser()->MSG_ERROR("tree ", name, " not found");
122 return nullptr;
123 }
124 }
125 }
126
129 TGraph *GetTGraph(const std::string &name)
130 {
131
132 if (!isopen)
133 {
134 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
135 MSGUser()->MSG_ERROR("file not open");
136 return nullptr;
137 }
138 else
139 {
140 auto findresult = map_name_graph.find(name);
141 if (findresult != map_name_graph.end())
142 return (TGraph *)findresult->second->ReadObj();
143 else
144 {
145 auto titleguard = MSGUser()->StartTitleWithGuard("TFileHelper");
146 MSGUser()->MSG_ERROR("graph ", name, " not found");
147 return nullptr;
148 }
149 }
150 }
151
153 void Close()
154 {
155 if (infile)
156 {
157 delete infile;
158 infile = nullptr;
159 isopen = false;
160
161 map_name_hist.clear();
162 map_name_graph.clear();
163 map_name_tree.clear();
164 histnames.clear();
165 }
166 }
167
170 const std::vector<std::string> &GetListOfHistNames()
171 {
172 return histnames;
173 }
174
176 virtual ~TFileHelper()
177 {
178 Close();
179 }
180
181 private:
182 std::map<std::string, TKey *> map_name_hist;
183 std::map<std::string, TKey *> map_name_graph;
184 std::map<std::string, TKey *> map_name_tree;
185 std::vector<std::string> histnames;
186 TFile *infile = nullptr;
187 bool isopen = false;
188 };
189}
Some global definitions.
Helper functions for std::map.
Definition MapTool.h:17
std::vector< T1 > GetListOfKeys(const std::map< T1, T2 > &inputmap) const
Get the vector of keys in a map.
Definition MapTool.h:135
Provide fast access to ROOT objects inside a TFile.
Definition TFileHelper.h:19
std::map< std::string, TKey * > map_name_tree
void Close()
Close the ROOT file.
const std::vector< std::string > & GetListOfHistNames()
Get a vector stores the list of the names of histogram.
virtual ~TFileHelper()
Destructor.
TFileHelper(std::shared_ptr< MSGTool > msg, const std::string &name)
Constructor.
Definition TFileHelper.h:24
TH1 * Get(const std::string &name)
Get TH1 object.
Definition TFileHelper.h:80
TTree * GetTree(const std::string &name)
Get TTree object.
TGraph * GetTGraph(const std::string &name)
Get TGraph object.
std::vector< std::string > histnames
std::map< std::string, TKey * > map_name_graph
std::map< std::string, TKey * > map_name_hist
Provide interface for all tools in NAGASH.
Definition Tool.h:72
std::shared_ptr< MSGTool > msg
Definition Tool.h:88
std::shared_ptr< MSGTool > MSGUser()
return the MSGTool inside.
Definition Tool.h:91