ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTFileServer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTFileServer.cc
1 
2 
9 
10 
11 #include "PHTFileServer.h"
12 
13 #include <TObject.h> // for TObject, TObject::kWriteDelete
14 
15 #include <iostream> // for operator<<, basic_ostream, ostringstream, endl
16 #include <sstream>
17 #include <utility> // for pair, make_pair
18 
19 using namespace std;
20 
21 //_________________________________________________
23 
24 //_________________________________________________
26 {
27  if (!SafeTFile::file_map().empty()) close();
28 }
29 
30 //_________________________________________________
31 void PHTFileServer::open(const string& filename, const string& type)
32 {
33  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
34  if (iter != SafeTFile::file_map().end())
35  {
36  ostringstream what;
37  what << "PHTFileServer::open - file " << filename << " already opened.";
38  cout << (what.str()) << endl;
39 
40  // increment counter; change TDirectory
41  iter->second->counter()++;
42  iter->second->cd();
43  }
44  else
45  {
46  ostringstream what;
47  what << "PHTFileServer::open - opening file " << filename << " (" << type << ")";
48  cout << (what.str()) << endl;
49 
50  // create new SafeTFile; insert in map; change TDirectory
51  SafeTFile* file(new SafeTFile(filename, type));
52  if (!file->IsOpen()) cout << ("PHTFileServer::open - error opening TFile") << endl;
53  SafeTFile::file_map().insert(make_pair(filename, file));
54  file->cd();
55  }
56 }
57 
58 //_________________________________________________
59 bool PHTFileServer::flush(const string& filename)
60 {
61  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
62  if (iter != SafeTFile::file_map().end())
63  iter->second->Flush();
64  else
65  {
66  ostringstream what;
67  what << "PHTFileServer::flush - file " << filename << " not found";
68  cout << (what.str()) << endl;
69  return false;
70  }
71 
72  return true;
73 }
74 
75 //_________________________________________________
76 bool PHTFileServer::cd(const string& filename)
77 {
78  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
79  if (iter != SafeTFile::file_map().end())
80  iter->second->cd();
81  else
82  {
83  ostringstream what;
84  what << "PHTFileServer::flush - file " << filename << " not found";
85  cout << (what.str()) << endl;
86  return false;
87  }
88 
89  return true;
90 }
91 
92 //_________________________________________________
93 bool PHTFileServer::write(const string& filename)
94 {
95  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
96  if (iter != SafeTFile::file_map().end())
97  {
98  if (iter->second->counter() > 1)
99  {
100  iter->second->counter()--;
101  ostringstream what;
102  what << "PHTFileServer::write - file " << filename << " still in use.";
103  cout << (what.str()) << endl;
104  }
105  else if (iter->second->counter() == 1)
106  {
107  iter->second->Write();
108  iter->second->counter()--;
109  ostringstream what;
110  what << "PHTFileServer::write - writing file " << filename << ".";
111  cout << (what.str()) << endl;
112  }
113  else
114  {
115  iter->second->Write();
116  ostringstream what;
117  what << "PHTFileServer::write - warning: too many calls for file " << filename << ".";
118  cout << (what.str()) << endl;
119  }
120  }
121  else
122  {
123  ostringstream what;
124  what << "PHTFileServer::write - file " << filename << " not found";
125  cout << (what.str()) << endl;
126  return false;
127  }
128 
129  return true;
130 }
131 
132 //__________________________________________
134 {
135  // close
136  // MUTOO::TRACE( "PHTFileServer::close" );
137  for (SafeTFile::TFileMap::iterator iter = SafeTFile::file_map().begin(); iter != SafeTFile::file_map().end(); ++iter)
138  {
139  if (iter->second->IsOpen())
140  {
141  if (iter->second->counter())
142  {
143  ostringstream what;
144  what << "PHTFileServer::close - file " << iter->first << " forced write with kWriteDelete.";
145  cout << (what.str()) << endl;
146  iter->second->Write("0", TObject::kWriteDelete);
147  }
148 
149  // close TFile
150  ostringstream what;
151  what << "PHTFileServer::close - closing " << iter->first << ".";
152  iter->second->Close();
153  cout << (what.str()) << endl;
154  }
155  }
156 
157  // clear file map
158  SafeTFile::file_map().clear();
159 }
160 
161 //__________________________________________________________________________________
163 {
164  // see if TFile is still open
165  if (IsOpen())
166  {
167  // check if TFile needs writing first
168  if (_counter)
169  {
170  ostringstream what;
171  what << "PHTFileServer::SafeTFile::~SafeTFile - file " << _filename << " forced write with kWriteDelete.";
172  cout << (what.str()) << endl;
173  Write("0", TObject::kWriteDelete);
174  }
175 
176  ostringstream what;
177  what << "PHTFileServer::SafeTFile::~SafeTFile - closing " << _filename << ".";
178  cout << (what.str()) << endl;
179  Close();
180  }
181 
182  /*
183  remove this filename from the make to make sure that PHTFileServer
184  does not try to write/close this TFile during the destructor
185  */
186  _map.erase(_filename);
187 }