Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:04

0001 #include "CDBTF.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 <TF1.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 CDBTF::CDBTF(const std::string &fname)
0018   : m_Filename(fname)
0019 {
0020 }
0021 
0022 CDBTF::~CDBTF()
0023 {
0024   for (auto &iter : m_TFMap)
0025   {
0026     delete iter.second;
0027   }
0028   m_TFMap.clear();
0029 }
0030 
0031 void CDBTF::WriteCDBTF()
0032 {
0033   if (m_TFMap.empty())
0034   {
0035     std::cout << __PRETTY_FUNCTION__ << " no TFs 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_TFMap)
0041   {
0042     iter.second->Write();
0043   }
0044   f->Close();
0045   gROOT->cd(currdir.c_str());  // restore previous directory
0046 }
0047 
0048 void CDBTF::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());  // restore previous directory
0063     return;
0064   }
0065   TIter next(list);
0066   TKey *key;
0067   TObject *obj;
0068   TF1*t1 = nullptr;
0069   while ((key = (TKey *) next()))
0070   {
0071     obj = key->ReadObj();
0072     if ((obj->InheritsFrom("TF1")))
0073     {
0074       fin->GetObject(obj->GetName(), t1);
0075       //t1->SetDirectory(nullptr);
0076       m_TFMap.insert(std::make_pair(obj->GetName(), t1));
0077     }
0078   }
0079   fin->Close();
0080   gROOT->cd(currdir.c_str());  // restore previous directory
0081 }
0082 
0083 void CDBTF::Print() const
0084 {
0085   for (auto &iter : m_TFMap)
0086   {
0087     std::cout << "TF " << iter.first << ", type "
0088               << iter.second->IsA()->GetName() << std::endl;
0089   }
0090   return;
0091 }
0092 
0093 void CDBTF::registerTF(TF1 *t1)
0094 {
0095   const auto iter = m_TFMap.find(t1->GetName());
0096   if (iter != m_TFMap.end())
0097   {
0098     std::cout << __PRETTY_FUNCTION__ << " TF " << t1->GetName() << " already registered, use a different name and try again" << std::endl;
0099     gSystem->Exit(1);
0100   }
0101   m_TFMap.insert(std::make_pair(t1->GetName(), t1));
0102   return;
0103 }
0104 
0105 TF1 *CDBTF::getTF(const std::string &name, bool printerror)
0106 {
0107   const auto iter = m_TFMap.find(name);
0108   if (iter == m_TFMap.end())
0109   {
0110     if (printerror)
0111     {
0112       std::cout << __PRETTY_FUNCTION__ << ": TF " << name << " not found" << std::endl;
0113     }
0114     return nullptr;
0115   }
0116   return iter->second;
0117 }