Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CDBHistos.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CDBHistos.cc
1 #include "CDBHistos.h"
2 
3 #include <TClass.h> // for TClass
4 #include <TCollection.h> // for TIter
5 #include <TDirectory.h> // for TDirectoryAtomicAdapter, TDirectory, gDirec...
6 #include <TFile.h>
7 #include <TH1.h>
8 #include <TKey.h>
9 #include <TList.h> // for TList
10 #include <TObject.h> // for TObject
11 #include <TROOT.h>
12 #include <TSystem.h>
13 
14 #include <iostream>
15 #include <utility> // for pair, make_pair
16 
18  : m_Filename(fname)
19 {
20 }
21 
23 {
24  for (auto &iter : m_HistoMap)
25  {
26  delete iter.second;
27  }
28  m_HistoMap.clear();
29 }
30 
32 {
33  if (m_HistoMap.empty())
34  {
35  std::cout << __PRETTY_FUNCTION__ << " no histograms to be saved " << std::endl;
36  return;
37  }
38  std::string currdir = gDirectory->GetPath();
39  TFile *f = TFile::Open(m_Filename.c_str(), "RECREATE");
40  for (auto &iter : m_HistoMap)
41  {
42  iter.second->Write();
43  }
44  f->Close();
45  gROOT->cd(currdir.c_str()); // restore previous directory
46 }
47 
49 {
50  std::string currdir = gDirectory->GetPath();
51  TFile *fin = TFile::Open(m_Filename.c_str());
52  if (fin == nullptr)
53  {
54  std::cout << __PRETTY_FUNCTION__ << " Could not open " << m_Filename << std::endl;
55  return;
56  }
57  TList *list = fin->GetListOfKeys();
58  if (!list)
59  {
60  std::cout << __PRETTY_FUNCTION__ << " No keys found in " << m_Filename << std::endl;
61  fin->Close();
62  gROOT->cd(currdir.c_str()); // restore previous directory
63  return;
64  }
65  TIter next(list);
66  TKey *key;
67  TObject *obj;
68  TH1 *h1 = nullptr;
69  while ((key = (TKey *) next()))
70  {
71  obj = key->ReadObj();
72  if ((obj->InheritsFrom("TH1")))
73  {
74  fin->GetObject(obj->GetName(), h1);
75  h1->SetDirectory(nullptr);
76  m_HistoMap.insert(std::make_pair(obj->GetName(), h1));
77  }
78  }
79  fin->Close();
80  gROOT->cd(currdir.c_str()); // restore previous directory
81 }
82 
83 void CDBHistos::Print() const
84 {
85  for (auto &iter : m_HistoMap)
86  {
87  std::cout << "histogram " << iter.first << ", type "
88  << iter.second->IsA()->GetName() << std::endl;
89  }
90  return;
91 }
92 
94 {
95  const auto iter = m_HistoMap.find(h1->GetName());
96  if (iter != m_HistoMap.end())
97  {
98  std::cout << __PRETTY_FUNCTION__ << " Histogram " << h1->GetName() << " already registered, use a different name and try again" << std::endl;
99  gSystem->Exit(1);
100  }
101  m_HistoMap.insert(std::make_pair(h1->GetName(), h1));
102  return;
103 }
104 
106 {
107  const auto iter = m_HistoMap.find(name);
108  if (iter == m_HistoMap.end())
109  {
110  std::cout << __PRETTY_FUNCTION__ << ": Histogram " << name << " not found" << std::endl;
111  return nullptr;
112  }
113  return iter->second;
114 }