Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // Tell emacs that this is a C++ source
0002 //  -*- C++ -*-.
0003 //////////////////////////////////////////////////////////////////
0004 /*!
0005   \file PHTFileServer.h
0006   \brief TFile clean handling
0007   \author  Hugo Pereira
0008   \version $Revision: 1.5 $
0009   \date    $Date: 2007/10/27 14:27:57 $
0010 */
0011 //////////////////////////////////////////////////////////////////
0012 
0013 #ifndef FUN4ALL_PHTFILESERVER_H
0014 #define FUN4ALL_PHTFILESERVER_H
0015 
0016 #include <TFile.h>
0017 
0018 #include <map>
0019 #include <sstream>
0020 #include <string>
0021 
0022 /*!
0023   \brief TFile clean handling. It allow independant classes to access
0024   the same TFile and write ntuple to it. TFiles get written only when as many
0025   write request are achieved as open request.
0026   It get closed when the server is deleted
0027 */
0028 class PHTFileServer
0029 {
0030  public:
0031   //! return reference to class singleton
0032   static PHTFileServer& get(void)
0033   {
0034     static PHTFileServer singleton;
0035     return singleton;
0036   }
0037 
0038   //! destructor. All non close TFiles are closed, with a warning.
0039   virtual ~PHTFileServer();
0040 
0041   /*! \brief
0042     open a SafeTFile. If filename is not found in the map, create a new TFile
0043     and append to the map; increment counter otherwise
0044   */
0045   static void open(const std::string& filename, const std::string& type = "RECREATE");
0046 
0047   //! flush TFile matching filename
0048   static bool flush(const std::string& filename);
0049 
0050   //! change to directory of TFile matching filename
0051   static bool cd(const std::string& filename);
0052 
0053   /*! \brief
0054     if TFile is found in map and counter is 0, close the TFile,
0055     decrement counter otherwise
0056   */
0057   static bool write(const std::string& filename);
0058 
0059   //! close all TFiles
0060   static void close(void);
0061 
0062  private:
0063   //! constructor
0064   PHTFileServer(void)
0065   {
0066   }
0067 
0068   //! local class to store TFile and counter
0069   class SafeTFile : public TFile
0070   {
0071    public:
0072     //! constructor
0073     SafeTFile(const std::string& filename, const std::string& type = "RECREATE")
0074       : TFile(filename.c_str(), type.c_str())
0075       , _filename(filename)
0076       , _counter(1)
0077     {
0078     }
0079 
0080     //! destructor
0081     ~SafeTFile(void) override;
0082 
0083     //! get reference to counter
0084     int& counter()
0085     {
0086       return _counter;
0087     }
0088 
0089     //! get const reference to counter
0090     const int& counter() const
0091     {
0092       return _counter;
0093     }
0094 
0095     //! shortcut for SafeTFile map
0096     typedef std::map<std::string, SafeTFile*> TFileMap;
0097 
0098     static TFileMap& file_map(void)
0099     {
0100       return _map;
0101     }
0102 
0103    private:
0104     //! filename (for debugging)
0105     std::string _filename;
0106 
0107     //! call counter
0108     int _counter;
0109 
0110     //! filename/SafeTFile map
0111     static TFileMap _map;
0112   };
0113 };
0114 
0115 #endif