File indexing completed on 2025-08-05 08:16:04
0001 #include "CDBHistos.h"
0002
0003 #include <TClass.h> // for TClass
0004 #include <TCollection.h> // for TIter
0005 #include <TDirectory.h> // for TDirectoryAtomicAdapter, TDirectory, gDirec...
0006 #include <TFile.h>
0007 #include <TH1.h>
0008 #include <TKey.h>
0009 #include <TList.h> // for TList
0010 #include <TObject.h> // for TObject
0011 #include <TROOT.h>
0012 #include <TSystem.h>
0013
0014 #include <iostream>
0015 #include <utility> // for pair, make_pair
0016
0017 CDBHistos::CDBHistos(const std::string &fname)
0018 : m_Filename(fname)
0019 {
0020 }
0021
0022 CDBHistos::~CDBHistos()
0023 {
0024 for (auto &iter : m_HistoMap)
0025 {
0026 delete iter.second;
0027 }
0028 m_HistoMap.clear();
0029 }
0030
0031 void CDBHistos::WriteCDBHistos()
0032 {
0033 if (m_HistoMap.empty())
0034 {
0035 std::cout << __PRETTY_FUNCTION__ << " no histograms to be saved " << std::endl;
0036 return;
0037 }
0038 std::string currdir = gDirectory->GetPath();
0039 TFile *f = TFile::Open(m_Filename.c_str(), "RECREATE");
0040 for (auto &iter : m_HistoMap)
0041 {
0042 iter.second->Write();
0043 }
0044 f->Close();
0045 gROOT->cd(currdir.c_str());
0046 }
0047
0048 void CDBHistos::LoadCalibrations()
0049 {
0050 std::string currdir = gDirectory->GetPath();
0051 TFile *fin = TFile::Open(m_Filename.c_str());
0052 if (fin == nullptr)
0053 {
0054 std::cout << __PRETTY_FUNCTION__ << " Could not open " << m_Filename << std::endl;
0055 return;
0056 }
0057 TList *list = fin->GetListOfKeys();
0058 if (!list)
0059 {
0060 std::cout << __PRETTY_FUNCTION__ << " No keys found in " << m_Filename << std::endl;
0061 fin->Close();
0062 gROOT->cd(currdir.c_str());
0063 return;
0064 }
0065 TIter next(list);
0066 TKey *key;
0067 TObject *obj;
0068 TH1 *h1 = nullptr;
0069 while ((key = (TKey *) next()))
0070 {
0071 obj = key->ReadObj();
0072 if ((obj->InheritsFrom("TH1")))
0073 {
0074 fin->GetObject(obj->GetName(), h1);
0075 h1->SetDirectory(nullptr);
0076 m_HistoMap.insert(std::make_pair(obj->GetName(), h1));
0077 }
0078 }
0079 fin->Close();
0080 gROOT->cd(currdir.c_str());
0081 }
0082
0083 void CDBHistos::Print() const
0084 {
0085 for (auto &iter : m_HistoMap)
0086 {
0087 std::cout << "histogram " << iter.first << ", type "
0088 << iter.second->IsA()->GetName() << std::endl;
0089 }
0090 return;
0091 }
0092
0093 void CDBHistos::registerHisto(TH1 *h1)
0094 {
0095 const auto iter = m_HistoMap.find(h1->GetName());
0096 if (iter != m_HistoMap.end())
0097 {
0098 std::cout << __PRETTY_FUNCTION__ << " Histogram " << h1->GetName() << " already registered, use a different name and try again" << std::endl;
0099 gSystem->Exit(1);
0100 }
0101 m_HistoMap.insert(std::make_pair(h1->GetName(), h1));
0102 return;
0103 }
0104
0105 TH1 *CDBHistos::getHisto(const std::string &name, bool printerror)
0106 {
0107 const auto iter = m_HistoMap.find(name);
0108 if (iter == m_HistoMap.end())
0109 {
0110 if (printerror)
0111 {
0112 std::cout << __PRETTY_FUNCTION__ << ": Histogram " << name << " not found" << std::endl;
0113 }
0114 return nullptr;
0115 }
0116 return iter->second;
0117 }