Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:18

0001 //////////////////////////////////////////////////////////////////
0002 /*!
0003   \file PHTFileServer.cc
0004   \brief TFile clean handling
0005   \author  Hugo Pereira
0006   \version $Revision: 1.8 $
0007   \date    $Date: 2011/08/11 14:25:49 $
0008 */
0009 //////////////////////////////////////////////////////////////////
0010 
0011 #include "PHTFileServer.h"
0012 
0013 #include <TObject.h>  // for TObject, TObject::kWriteDelete
0014 
0015 #include <iostream>  // for operator<<, basic_ostream, ostringstream, endl
0016 #include <sstream>
0017 #include <utility>  // for pair, make_pair
0018 
0019 //_________________________________________________
0020 PHTFileServer::SafeTFile::TFileMap PHTFileServer::SafeTFile::_map;
0021 
0022 //_________________________________________________
0023 PHTFileServer::~PHTFileServer()
0024 {
0025   if (!SafeTFile::file_map().empty())
0026   {
0027     close();
0028   }
0029 }
0030 
0031 //_________________________________________________
0032 void PHTFileServer::open(const std::string& filename, const std::string& type)
0033 {
0034   SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
0035   if (iter != SafeTFile::file_map().end())
0036   {
0037     std::ostringstream what;
0038     what << "PHTFileServer::open - file " << filename << " already opened.";
0039     std::cout << (what.str()) << std::endl;
0040 
0041     // increment counter; change TDirectory
0042     iter->second->counter()++;
0043     iter->second->cd();
0044   }
0045   else
0046   {
0047     std::ostringstream what;
0048     what << "PHTFileServer::open - opening file " << filename << " (" << type << ")";
0049     std::cout << (what.str()) << std::endl;
0050 
0051     // create new SafeTFile; insert in map; change TDirectory
0052     SafeTFile* file(new SafeTFile(filename, type));
0053     if (!file->IsOpen())
0054     {
0055       std::cout << ("PHTFileServer::open - error opening TFile") << std::endl;
0056     }
0057     SafeTFile::file_map().insert(make_pair(filename, file));
0058     file->cd();
0059   }
0060 }
0061 
0062 //_________________________________________________
0063 bool PHTFileServer::flush(const std::string& filename)
0064 {
0065   SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
0066   if (iter != SafeTFile::file_map().end())
0067   {
0068     iter->second->Flush();
0069   }
0070   else
0071   {
0072     std::ostringstream what;
0073     what << "PHTFileServer::flush - file " << filename << " not found";
0074     std::cout << (what.str()) << std::endl;
0075     return false;
0076   }
0077 
0078   return true;
0079 }
0080 
0081 //_________________________________________________
0082 bool PHTFileServer::cd(const std::string& filename)
0083 {
0084   SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
0085   if (iter != SafeTFile::file_map().end())
0086   {
0087     iter->second->cd();
0088   }
0089   else
0090   {
0091     std::ostringstream what;
0092     what << "PHTFileServer::flush - file " << filename << " not found";
0093     std::cout << (what.str()) << std::endl;
0094     return false;
0095   }
0096 
0097   return true;
0098 }
0099 
0100 //_________________________________________________
0101 bool PHTFileServer::write(const std::string& filename)
0102 {
0103   SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
0104   if (iter != SafeTFile::file_map().end())
0105   {
0106     if (iter->second->counter() > 1)
0107     {
0108       iter->second->counter()--;
0109       std::ostringstream what;
0110       what << "PHTFileServer::write - file " << filename << " still in use.";
0111       std::cout << (what.str()) << std::endl;
0112     }
0113     else if (iter->second->counter() == 1)
0114     {
0115       iter->second->Write();
0116       iter->second->counter()--;
0117       std::ostringstream what;
0118       what << "PHTFileServer::write - writing file " << filename << ".";
0119       std::cout << (what.str()) << std::endl;
0120     }
0121     else
0122     {
0123       iter->second->Write();
0124       std::ostringstream what;
0125       what << "PHTFileServer::write - warning: too many calls for file " << filename << ".";
0126       std::cout << (what.str()) << std::endl;
0127     }
0128   }
0129   else
0130   {
0131     std::ostringstream what;
0132     what << "PHTFileServer::write - file " << filename << " not found";
0133     std::cout << (what.str()) << std::endl;
0134     return false;
0135   }
0136 
0137   return true;
0138 }
0139 
0140 //__________________________________________
0141 void PHTFileServer::close()
0142 {
0143   // close
0144   //  MUTOO::TRACE( "PHTFileServer::close" );
0145   for (auto& iter : SafeTFile::file_map())
0146   {
0147     if (iter.second->IsOpen())
0148     {
0149       if (iter.second->counter())
0150       {
0151         std::ostringstream what;
0152         what << "PHTFileServer::close - file " << iter.first << " forced write with kWriteDelete.";
0153         std::cout << (what.str()) << std::endl;
0154         iter.second->Write("0", TObject::kWriteDelete);
0155       }
0156 
0157       // close TFile
0158       std::ostringstream what;
0159       what << "PHTFileServer::close - closing " << iter.first << ".";
0160       iter.second->Close();
0161       std::cout << (what.str()) << std::endl;
0162     }
0163   }
0164 
0165   // clear file map
0166   SafeTFile::file_map().clear();
0167 }
0168 
0169 //__________________________________________________________________________________
0170 PHTFileServer::SafeTFile::~SafeTFile()
0171 {
0172   // see if TFile is still open
0173   if (IsOpen())
0174   {
0175     // check if TFile needs writing first
0176     if (_counter)
0177     {
0178       std::ostringstream what;
0179       what << "PHTFileServer::SafeTFile::~SafeTFile - file " << _filename << " forced write with kWriteDelete.";
0180       std::cout << (what.str()) << std::endl;
0181       Write("0", TObject::kWriteDelete);
0182     }
0183 
0184     std::ostringstream what;
0185     what << "PHTFileServer::SafeTFile::~SafeTFile - closing " << _filename << ".";
0186     std::cout << (what.str()) << std::endl;
0187     Close();
0188   }
0189 
0190   /*
0191   remove this filename from the make to make sure that PHTFileServer
0192   does not try to write/close this TFile during the destructor
0193   */
0194   _map.erase(_filename);
0195 }