Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllDstOutputManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllDstOutputManager.cc
2 
3 #include "Fun4AllServer.h"
4 
5 #include <phool/PHNode.h>
7 #include <phool/PHNodeIterator.h>
8 #include <phool/phool.h> // for PHWHERE, PHReadOnly, PHRunTree
9 #include <phool/recoConsts.h>
10 
11 #include <TSystem.h>
12 
13 #include <boost/format.hpp>
14 
15 #include <cstdlib>
16 #include <filesystem>
17 #include <iostream>
18 #include <string>
19 
21  : Fun4AllOutputManager(myname, fname)
22 {
23  return;
24 }
25 
27 {
28  delete dstOut;
29  return;
30 }
31 
33 {
34  savenodes.insert(nodename);
35  return 0;
36 }
37 
39 {
40  saverunnodes.insert(nodename);
41  return 0;
42 }
43 
45 {
46  stripnodes.insert(nodename);
47  return 0;
48 }
49 
51 {
52  striprunnodes.insert(nodename);
53  return 0;
54 }
55 
57 {
58  OutFileName(fname);
59  return 0;
60 }
61 
63 {
64  if (what == "ALL" || what == "WRITENODES")
65  {
66  std::cout << Name() << " writes " << OutFileName() << std::endl;
67  if (savenodes.empty())
68  {
69  if (stripnodes.empty())
70  {
71  std::cout << Name() << ": All Nodes will be written out" << std::endl;
72  }
73  else
74  {
75  for (auto &nodename : stripnodes)
76  {
77  std::cout << Name() << ": Node " << nodename << " will be stripped" << std::endl;
78  }
79  }
80  }
81  else
82  {
83  for (auto &nodename : savenodes)
84  {
85  std::cout << Name() << ": Node " << nodename << " is written out" << std::endl;
86  }
87  }
88  }
89  // base class print method
91 
92  return;
93 }
94 
95 // All nodes are set to transient by the framework
96 // here we first change the nodes we want to write out
97 // to persistent and then call the write method
98 // of the io manager
99 // afterwards the nodes we just wrote out are changed back
100 // to transient
101 // if we want to strip nodes (only meaningful if we take the default
102 // that everything is written out), those nodes are declared transient
104 {
105  if (!m_SaveDstNodeFlag)
106  {
107  return 0;
108  }
109  if (!dstOut)
110  {
111  outfile_open_first_write(); // outfileopen(OutFileName());
112  }
113  PHNodeIterator nodeiter(startNode);
114  if (savenodes.empty())
115  {
117  se->MakeNodesPersistent(startNode);
118  if (!stripnodes.empty())
119  {
120  for (auto &nodename : stripnodes)
121  {
122  PHNode *ChosenNode = nodeiter.findFirst("PHIODataNode", nodename);
123  if (ChosenNode)
124  {
125  ChosenNode->makeTransient();
126  }
127  else
128  {
129  if (Verbosity() > 0)
130  {
131  std::cout << PHWHERE << Name() << ": Node " << nodename
132  << " does not exist" << std::endl;
133  }
134  }
135  }
136  }
137  }
138  else
139  {
140  for (auto &nodename : savenodes)
141  {
142  PHNode *ChosenNode = nodeiter.findFirst("PHIODataNode", nodename);
143  if (ChosenNode)
144  {
145  ChosenNode->makePersistent();
146  }
147  else
148  {
149  if (Verbosity() > 0)
150  {
151  std::cout << PHWHERE << Name() << ": Node " << nodename
152  << " does not exist" << std::endl;
153  }
154  }
155  }
156  }
157  dstOut->write(startNode);
158  // to save some cpu cycles we only make it globally transient if
159  // all nodes have been written (savenodes set is empty)
160  // else we only make the nodes transient which we have written (all
161  // others are transient by construction)
162  if (savenodes.empty())
163  {
165  se->MakeNodesTransient(startNode);
166  }
167  else
168  {
169  for (auto &nodename : savenodes)
170  {
171  PHNode *ChosenNode = nodeiter.findFirst("PHIODataNode", nodename);
172  if (ChosenNode)
173  {
174  ChosenNode->makeTransient();
175  }
176  }
177  }
178  return 0;
179 }
180 
182 {
183  delete dstOut;
184  if (!m_SaveRunNodeFlag)
185  {
186  dstOut = nullptr;
187  return 0;
188  }
189  PHAccessType access_type = PHUpdate;
190  if (!m_SaveDstNodeFlag)
191  {
192  access_type = PHWrite;
193  }
194 
195  if (UsedOutFileName().empty())
196  {
198  if (m_FileNameStem.empty())
199  {
200  m_FileNameStem = p.stem();
201  }
202  m_UsedOutFileName = OutFileName() + std::string("?reproducible=") + std::string(p.filename());
203  }
204  dstOut = new PHNodeIOManager(UsedOutFileName(), access_type, PHRunTree);
206  PHNodeIterator nodeiter(thisNode);
207  if (saverunnodes.empty())
208  {
209  se->MakeNodesPersistent(thisNode);
210  if (!striprunnodes.empty())
211  {
212  for (auto &nodename : striprunnodes)
213  {
214  PHNode *ChosenNode = nodeiter.findFirst("PHIODataNode", nodename);
215  if (ChosenNode)
216  {
217  ChosenNode->makeTransient();
218  }
219  else
220  {
221  if (Verbosity() > 0)
222  {
223  std::cout << PHWHERE << Name() << ": Node " << nodename
224  << " does not exist" << std::endl;
225  }
226  }
227  }
228  }
229  }
230  else
231  {
232  for (auto &nodename : saverunnodes)
233  {
234  PHNode *ChosenNode = nodeiter.findFirst("PHIODataNode", nodename);
235  if (ChosenNode)
236  {
237  ChosenNode->makePersistent();
238  }
239  else
240  {
241  if (Verbosity() > 0)
242  {
243  std::cout << PHWHERE << Name() << ": Node " << nodename
244  << " does not exist" << std::endl;
245  }
246  }
247  }
248  }
249  dstOut->write(thisNode);
250  se->MakeNodesTransient(thisNode);
251  delete dstOut;
252  dstOut = nullptr;
253  return 0;
254 }
255 
257 {
258  delete dstOut;
259  SetEventsWritten(0);
261  if (m_FileNameStem.empty())
262  {
263  m_FileNameStem = p.stem();
264  }
265  if (ApplyFileRule())
266  {
268  int runnumber = 0;
269  if (rc->FlagExist("RUNNUMBER"))
270  {
271  runnumber = rc->get_IntFlag("RUNNUMBER");
272  }
273  std::string fullpath = ".";
274  if (p.has_parent_path())
275  {
276  fullpath = p.parent_path();
277  }
278  std::string runseg = boost::str(boost::format("-%08d-%04d") % runnumber % m_CurrentSegment);
279  std::string newfile = fullpath + std::string("/") + m_FileNameStem + runseg + std::string(p.extension());
280  OutFileName(newfile);
281  m_CurrentSegment++;
282  }
283  m_UsedOutFileName = OutFileName() + std::string("?reproducible=") + std::string(p.filename());
285  if (!dstOut->isFunctional())
286  {
287  delete dstOut;
288  dstOut = nullptr;
289  std::cout << PHWHERE << " Could not open " << OutFileName() << std::endl;
290  return -1;
291  }
292 
293  dstOut->SetCompressionSetting(m_CompressionSetting);
294  return 0;
295 }