Analysis Software
Documentation for sPHENIX simulation 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 //_________________________________________________
21 
22 //_________________________________________________
24 {
25  if (!SafeTFile::file_map().empty())
26  {
27  close();
28  }
29 }
30 
31 //_________________________________________________
33 {
34  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
35  if (iter != SafeTFile::file_map().end())
36  {
37  std::ostringstream what;
38  what << "PHTFileServer::open - file " << filename << " already opened.";
39  std::cout << (what.str()) << std::endl;
40 
41  // increment counter; change TDirectory
42  iter->second->counter()++;
43  iter->second->cd();
44  }
45  else
46  {
47  std::ostringstream what;
48  what << "PHTFileServer::open - opening file " << filename << " (" << type << ")";
49  std::cout << (what.str()) << std::endl;
50 
51  // create new SafeTFile; insert in map; change TDirectory
52  SafeTFile* file(new SafeTFile(filename, type));
53  if (!file->IsOpen())
54  {
55  std::cout << ("PHTFileServer::open - error opening TFile") << std::endl;
56  }
57  SafeTFile::file_map().insert(make_pair(filename, file));
58  file->cd();
59  }
60 }
61 
62 //_________________________________________________
64 {
65  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
66  if (iter != SafeTFile::file_map().end())
67  {
68  iter->second->Flush();
69  }
70  else
71  {
72  std::ostringstream what;
73  what << "PHTFileServer::flush - file " << filename << " not found";
74  std::cout << (what.str()) << std::endl;
75  return false;
76  }
77 
78  return true;
79 }
80 
81 //_________________________________________________
83 {
84  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
85  if (iter != SafeTFile::file_map().end())
86  {
87  iter->second->cd();
88  }
89  else
90  {
91  std::ostringstream what;
92  what << "PHTFileServer::flush - file " << filename << " not found";
93  std::cout << (what.str()) << std::endl;
94  return false;
95  }
96 
97  return true;
98 }
99 
100 //_________________________________________________
102 {
103  SafeTFile::TFileMap::iterator iter(SafeTFile::file_map().find(filename));
104  if (iter != SafeTFile::file_map().end())
105  {
106  if (iter->second->counter() > 1)
107  {
108  iter->second->counter()--;
109  std::ostringstream what;
110  what << "PHTFileServer::write - file " << filename << " still in use.";
111  std::cout << (what.str()) << std::endl;
112  }
113  else if (iter->second->counter() == 1)
114  {
115  iter->second->Write();
116  iter->second->counter()--;
117  std::ostringstream what;
118  what << "PHTFileServer::write - writing file " << filename << ".";
119  std::cout << (what.str()) << std::endl;
120  }
121  else
122  {
123  iter->second->Write();
124  std::ostringstream what;
125  what << "PHTFileServer::write - warning: too many calls for file " << filename << ".";
126  std::cout << (what.str()) << std::endl;
127  }
128  }
129  else
130  {
131  std::ostringstream what;
132  what << "PHTFileServer::write - file " << filename << " not found";
133  std::cout << (what.str()) << std::endl;
134  return false;
135  }
136 
137  return true;
138 }
139 
140 //__________________________________________
142 {
143  // close
144  // MUTOO::TRACE( "PHTFileServer::close" );
145  for (auto& iter : SafeTFile::file_map())
146  {
147  if (iter.second->IsOpen())
148  {
149  if (iter.second->counter())
150  {
151  std::ostringstream what;
152  what << "PHTFileServer::close - file " << iter.first << " forced write with kWriteDelete.";
153  std::cout << (what.str()) << std::endl;
154  iter.second->Write("0", TObject::kWriteDelete);
155  }
156 
157  // close TFile
158  std::ostringstream what;
159  what << "PHTFileServer::close - closing " << iter.first << ".";
160  iter.second->Close();
161  std::cout << (what.str()) << std::endl;
162  }
163  }
164 
165  // clear file map
166  SafeTFile::file_map().clear();
167 }
168 
169 //__________________________________________________________________________________
171 {
172  // see if TFile is still open
173  if (IsOpen())
174  {
175  // check if TFile needs writing first
176  if (_counter)
177  {
178  std::ostringstream what;
179  what << "PHTFileServer::SafeTFile::~SafeTFile - file " << _filename << " forced write with kWriteDelete.";
180  std::cout << (what.str()) << std::endl;
181  Write("0", TObject::kWriteDelete);
182  }
183 
184  std::ostringstream what;
185  what << "PHTFileServer::SafeTFile::~SafeTFile - closing " << _filename << ".";
186  std::cout << (what.str()) << std::endl;
187  Close();
188  }
189 
190  /*
191  remove this filename from the make to make sure that PHTFileServer
192  does not try to write/close this TFile during the destructor
193  */
194  _map.erase(_filename);
195 }